~hjd/ubuntu/wily/xmlgraphics-commons/debian-merged

« back to all changes in this revision

Viewing changes to src/java/org/apache/xmlgraphics/image/writer/internal/TIFFImageWriter.java

  • Committer: Bazaar Package Importer
  • Author(s): Vincent Fourmond
  • Date: 2011-02-11 14:15:14 UTC
  • mfrom: (8.1.2 experimental)
  • Revision ID: james.westby@ubuntu.com-20110211141514-h67achft6x31gju1
Tags: 1.4.dfsg-3
Uploading to unstable, hoping we won't break too many things ;-)...

Show diffs side-by-side

added added

removed removed

Lines of Context:
5
5
 * The ASF licenses this file to You under the Apache License, Version 2.0
6
6
 * (the "License"); you may not use this file except in compliance with
7
7
 * the License.  You may obtain a copy of the License at
8
 
 * 
 
8
 *
9
9
 *      http://www.apache.org/licenses/LICENSE-2.0
10
 
 * 
 
10
 *
11
11
 * Unless required by applicable law or agreed to in writing, software
12
12
 * distributed under the License is distributed on an "AS IS" BASIS,
13
13
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
15
 * limitations under the License.
16
16
 */
17
17
 
18
 
/* $Id$ */
 
18
/* $Id: TIFFImageWriter.java 750418 2009-03-05 11:03:54Z vhennebert $ */
19
19
 
20
20
package org.apache.xmlgraphics.image.writer.internal;
21
21
 
38
38
import com.sun.image.codec.jpeg.JPEGEncodeParam;
39
39
 
40
40
/**
41
 
 * ImageWriter implementation that uses the internal TIFF codec to 
 
41
 * ImageWriter implementation that uses the internal TIFF codec to
42
42
 * write TIFF files.
43
43
 *
44
 
 * @version $Id$
 
44
 * @version $Id: TIFFImageWriter.java 750418 2009-03-05 11:03:54Z vhennebert $
45
45
 */
46
46
public class TIFFImageWriter extends AbstractImageWriter {
47
47
 
70
70
     * @param encodeParams the TIFF encoding parameters
71
71
     * @param image the image to be encoded
72
72
     */
73
 
    private void updateParams(TIFFEncodeParam encodeParams, ImageWriterParams params, 
 
73
    private void updateParams(TIFFEncodeParam encodeParams, ImageWriterParams params,
74
74
            RenderedImage image) {
75
75
        if (encodeParams.getCompression() == TIFFEncodeParam.COMPRESSION_JPEG_TTN2) {
76
76
            ColorModel cm = image.getColorModel();
99
99
                colorID = JPEGEncodeParam.COLOR_ID_CMYK;
100
100
                break;
101
101
            default:
102
 
                //TODO Don't know how to determine whether image is PYCC or not 
 
102
                //TODO Don't know how to determine whether image is PYCC or not
103
103
                //(see JPEGEncodeParam)
104
104
                colorID = JPEGEncodeParam.COLOR_ID_UNKNOWN;
105
105
            }
106
106
            JPEGEncodeParam jpegParam = JPEGCodec.getDefaultJPEGEncodeParam(
107
107
                    image.getData(), colorID);
108
108
            if (params.getJPEGQuality() != null || params.getJPEGForceBaseline() != null) {
109
 
                float qual = (params.getJPEGQuality() != null 
 
109
                float qual = (params.getJPEGQuality() != null
110
110
                        ? params.getJPEGQuality().floatValue() : 0.75f);
111
111
                boolean force = (params.getJPEGForceBaseline() != null
112
112
                        ? params.getJPEGForceBaseline().booleanValue() : false);
113
 
                jpegParam.setQuality(qual, force); 
 
113
                jpegParam.setQuality(qual, force);
114
114
            }
115
115
            encodeParams.setJPEGEncodeParam(jpegParam);
116
116
        }
136
136
                throw new UnsupportedOperationException("Compression method not supported: "
137
137
                        + params.getCompressionMethod());
138
138
            }
139
 
            
 
139
 
140
140
            if (params.getResolution() != null) {
141
141
                // Set target resolution
142
142
                float pixSzMM = 25.4f / params.getResolution().floatValue();
143
143
                // num Pixs in 100 Meters
144
 
                int numPix = (int)(((1000 * 100) / pixSzMM) + 0.5); 
 
144
                int numPix = (int)(((1000 * 100) / pixSzMM) + 0.5);
145
145
                int denom = 100 * 100;  // Centimeters per 100 Meters;
146
146
                long [] rational = {numPix, denom};
147
147
                TIFFField [] fields = {
148
 
                    new TIFFField(TIFFImageDecoder.TIFF_RESOLUTION_UNIT, 
149
 
                                  TIFFField.TIFF_SHORT, 1, 
 
148
                    new TIFFField(TIFFImageDecoder.TIFF_RESOLUTION_UNIT,
 
149
                                  TIFFField.TIFF_SHORT, 1,
150
150
                                  new char[] {(char)3}),
151
 
                    new TIFFField(TIFFImageDecoder.TIFF_X_RESOLUTION, 
152
 
                                  TIFFField.TIFF_RATIONAL, 1, 
 
151
                    new TIFFField(TIFFImageDecoder.TIFF_X_RESOLUTION,
 
152
                                  TIFFField.TIFF_RATIONAL, 1,
153
153
                                  new long[][] {rational}),
154
 
                    new TIFFField(TIFFImageDecoder.TIFF_Y_RESOLUTION, 
155
 
                                  TIFFField.TIFF_RATIONAL, 1, 
156
 
                                  new long[][] {rational}) 
 
154
                    new TIFFField(TIFFImageDecoder.TIFF_Y_RESOLUTION,
 
155
                                  TIFFField.TIFF_RATIONAL, 1,
 
156
                                  new long[][] {rational})
157
157
                        };
158
158
                encodeParams.setExtraFields(fields);
159
159
            }
160
160
        }
161
161
        return encodeParams;
162
162
    }
163
 
    
 
163
 
164
164
    /**
165
165
     * @see ImageWriter#getMIMEType()
166
166
     */
167
167
    public String getMIMEType() {
168
168
        return "image/tiff";
169
169
    }
170
 
    
 
170
 
171
171
    /**
172
172
     * @see org.apache.xmlgraphics.image.writer.ImageWriter#createMultiImageWriter(
173
173
     *          java.io.OutputStream)
187
187
        private TIFFEncodeParam encodeParams;
188
188
        private TIFFImageEncoder encoder;
189
189
        private Object context;
190
 
        
 
190
 
191
191
        public TIFFMultiImageWriter(OutputStream out) throws IOException {
192
192
            this.out = out;
193
193
        }
194
 
        
 
194
 
195
195
        public void writeImage(RenderedImage image, ImageWriterParams params) throws IOException {
196
196
            if (encoder == null) {
197
197
                encodeParams = createTIFFEncodeParams(params);
200
200
            }
201
201
            context = encoder.encodeMultiple(context, image);
202
202
        }
203
 
        
 
203
 
204
204
        public void close() throws IOException {
205
205
            if (encoder != null) {
206
206
                encoder.finishMultiple(context);
212
212
 
213
213
    }
214
214
 
215
 
    
 
215
 
216
216
}