01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. See the NOTICE file distributed with
04: * this work for additional information regarding copyright ownership.
05: * The ASF licenses this file to You under the Apache License, Version 2.0
06: * (the "License"); you may not use this file except in compliance with
07: * the License. You may obtain a copy of the License at
08: *
09: * http://www.apache.org/licenses/LICENSE-2.0
10: *
11: * Unless required by applicable law or agreed to in writing, software
12: * distributed under the License is distributed on an "AS IS" BASIS,
13: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14: * See the License for the specific language governing permissions and
15: * limitations under the License.
16: */
17:
18: /* $Id: JPEGImageWriter.java 496556 2007-01-16 00:59:48Z cam $ */
19:
20: package org.apache.xmlgraphics.image.writer.internal;
21:
22: import java.awt.image.BufferedImage;
23: import java.awt.image.RenderedImage;
24: import java.io.IOException;
25: import java.io.OutputStream;
26:
27: import org.apache.xmlgraphics.image.GraphicsUtil;
28: import org.apache.xmlgraphics.image.writer.AbstractImageWriter;
29: import org.apache.xmlgraphics.image.writer.ImageWriter;
30: import org.apache.xmlgraphics.image.writer.ImageWriterParams;
31:
32: import com.sun.image.codec.jpeg.JPEGCodec;
33: import com.sun.image.codec.jpeg.JPEGEncodeParam;
34: import com.sun.image.codec.jpeg.JPEGImageEncoder;
35:
36: /**
37: * ImageWriter implementation that uses the sun.com.image.codec.jpeg
38: * intefaces to write JPEG files.
39: * <p>
40: * Note: This class depends on a Sun runtime environment and is therefore not compatible with
41: * non-Sun VMs (such as Harmony or GNU Classpath based VMs).
42: *
43: * @version $Id: JPEGImageWriter.java 496556 2007-01-16 00:59:48Z cam $
44: */
45: public class JPEGImageWriter extends AbstractImageWriter {
46:
47: /**
48: * @see ImageWriter#writeImage(java.awt.image.RenderedImage, java.io.OutputStream)
49: */
50: public void writeImage(RenderedImage image, OutputStream out)
51: throws IOException {
52: writeImage(image, out, null);
53: }
54:
55: /**
56: * @see ImageWriter#writeImage(java.awt.image.RenderedImage, java.io.OutputStream, ImageWriterParams)
57: */
58: public void writeImage(RenderedImage image, OutputStream out,
59: ImageWriterParams params) throws IOException {
60: BufferedImage bi;
61: if (image instanceof BufferedImage) {
62: bi = (BufferedImage) image;
63: } else {
64: //TODO Is this the right way?
65: bi = GraphicsUtil.makeLinearBufferedImage(image.getWidth(),
66: image.getHeight(), false);
67: }
68: JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
69: if (params != null) {
70: JPEGEncodeParam param = encoder
71: .getDefaultJPEGEncodeParam(bi);
72: if (params.getJPEGQuality() != null) {
73: param.setQuality(params.getJPEGQuality().floatValue(),
74: params.getJPEGForceBaseline().booleanValue());
75: }
76: encoder.encode(bi, param);
77: } else {
78: encoder.encode(bi);
79: }
80: }
81:
82: /**
83: * @see ImageWriter#getMIMEType()
84: */
85: public String getMIMEType() {
86: return "image/jpeg";
87: }
88: }
|