1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
|
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/* $Id$ */
package org.apache.fop.render.bitmap;
// Code originaly contributed by Oleg Tkachenko of Multiconn International Ltd
// (olegt@multiconn.com).
import java.awt.image.BufferedImage;
import java.awt.image.DataBuffer;
import java.awt.image.PixelInterleavedSampleModel;
import java.awt.image.RenderedImage;
import java.awt.image.SampleModel;
import java.awt.image.SinglePixelPackedSampleModel;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Iterator;
import org.apache.commons.logging.Log;
import org.apache.xmlgraphics.image.GraphicsUtil;
import org.apache.xmlgraphics.image.rendered.FormatRed;
import org.apache.xmlgraphics.image.writer.ImageWriter;
import org.apache.xmlgraphics.image.writer.ImageWriterParams;
import org.apache.xmlgraphics.image.writer.ImageWriterRegistry;
import org.apache.xmlgraphics.image.writer.MultiImageWriter;
import org.apache.fop.apps.FOPException;
import org.apache.fop.apps.FOUserAgent;
import org.apache.fop.render.java2d.Java2DRenderer;
/**
* <p>
* This class represents renderer to TIFF (Tagged Image File Format) format. It
* is one of the most popular and flexible of the current public domain raster
* file formats, which was is primarily designed for raster data interchange.
* Supported compression types are:
* <ul>
* <li>Raw noncompressed data</li>
* <li>Byte-oriented run-length encoding "PackBits" compression.</li>
* <li>Modified Huffman Compression (CCITT Group 3 1D facsimile compression)</li>
* <li>CCITT T.4 bilevel compression (CCITT Group 3 2D facsimile compression)</li>
* <li>CCITT T.6 bilevel compression (CCITT Group 4 facsimile compression)</li>
* <li>JPEG-in-TIFF compression</li>
* <li>DEFLATE lossless compression (also known as "Zip-in-TIFF")</li>
* <li>LZW compression</li>
* TODO
* <p>
* This class actually does not render itself, instead it extends
* <code>org.apache.fop.render.java2D.Java2DRenderer</code> and just encode
* rendering results into TIFF format using Batik's image codec
*/
public class TIFFRenderer extends Java2DRenderer implements TIFFConstants {
/** ImageWriter parameters */
private ImageWriterParams writerParams;
/** Image Type as parameter for the BufferedImage constructor (see BufferedImage.TYPE_*) */
private int bufferedImageType = BufferedImage.TYPE_INT_ARGB;
private OutputStream outputStream;
/** {@inheritDoc} */
public String getMimeType() {
return MIME_TYPE;
}
/** Creates TIFF renderer. */
public TIFFRenderer() {
writerParams = new ImageWriterParams();
writerParams.setCompressionMethod(COMPRESSION_PACKBITS);
}
/**
* {@inheritDoc}
* org.apache.fop.apps.FOUserAgent)
*/
public void setUserAgent(FOUserAgent foUserAgent) {
super.setUserAgent(foUserAgent);
//Set target resolution
int dpi = Math.round(userAgent.getTargetResolution());
writerParams.setResolution(dpi);
}
/** {@inheritDoc} */
public void startRenderer(OutputStream outputStream) throws IOException {
this.outputStream = outputStream;
super.startRenderer(outputStream);
}
/** {@inheritDoc} */
public void stopRenderer() throws IOException {
super.stopRenderer();
log.debug("Starting TIFF encoding ...");
// Creates lazy iterator over generated page images
Iterator pageImagesItr = new LazyPageImagesIterator(getNumberOfPages(), log);
// Creates writer
ImageWriter writer = ImageWriterRegistry.getInstance().getWriterFor(getMimeType());
if (writer == null) {
BitmapRendererEventProducer eventProducer
= BitmapRendererEventProducer.Provider.get(
getUserAgent().getEventBroadcaster());
eventProducer.noImageWriterFound(this, getMimeType());
}
if (writer.supportsMultiImageWriter()) {
MultiImageWriter multiWriter = writer.createMultiImageWriter(outputStream);
try {
// Write all pages/images
while (pageImagesItr.hasNext()) {
RenderedImage img = (RenderedImage) pageImagesItr.next();
multiWriter.writeImage(img, writerParams);
}
} finally {
multiWriter.close();
}
} else {
writer.writeImage((RenderedImage) pageImagesItr.next(), outputStream, writerParams);
if (pageImagesItr.hasNext()) {
BitmapRendererEventProducer eventProducer
= BitmapRendererEventProducer.Provider.get(
getUserAgent().getEventBroadcaster());
eventProducer.stoppingAfterFirstPageNoFilename(this);
}
}
// Cleaning
outputStream.flush();
clearViewportList();
log.debug("TIFF encoding done.");
}
/** {@inheritDoc} */
protected BufferedImage getBufferedImage(int bitmapWidth, int bitmapHeight) {
return new BufferedImage(bitmapWidth, bitmapHeight, bufferedImageType);
}
/** Private inner class to lazy page rendering. */
private class LazyPageImagesIterator implements Iterator {
/** logging instance */
private Log log;
private int count;
private int current = 0;
/**
* Main constructor
* @param c number of pages to iterate over
* @param log the logger to use (this is a hack so this compiles under JDK 1.3)
*/
public LazyPageImagesIterator(int c, Log log) {
count = c;
this.log = log;
}
public boolean hasNext() {
return current < count;
}
public Object next() {
if (log.isDebugEnabled()) {
log.debug("[" + (current + 1) + "]");
}
// Renders current page as image
BufferedImage pageImage = null;
try {
pageImage = getPageImage(current++);
} catch (FOPException e) {
log.error(e);
return null;
}
if (COMPRESSION_CCITT_T4.equalsIgnoreCase(writerParams.getCompressionMethod())
|| COMPRESSION_CCITT_T6.equalsIgnoreCase(writerParams.getCompressionMethod())) {
return pageImage;
} else {
//Decorate the image with a packed sample model for encoding by the codec
SinglePixelPackedSampleModel sppsm;
sppsm = (SinglePixelPackedSampleModel)pageImage.getSampleModel();
int bands = sppsm.getNumBands();
int[] off = new int[bands];
int w = pageImage.getWidth();
int h = pageImage.getHeight();
for (int i = 0; i < bands; i++) {
off[i] = i;
}
SampleModel sm = new PixelInterleavedSampleModel(
DataBuffer.TYPE_BYTE, w, h, bands, w * bands, off);
RenderedImage rimg = new FormatRed(GraphicsUtil.wrap(pageImage), sm);
return rimg;
}
}
public void remove() {
throw new UnsupportedOperationException(
"Method 'remove' is not supported.");
}
}
/** @param bufferedImageType an image type */
public void setBufferedImageType(int bufferedImageType) {
this.bufferedImageType = bufferedImageType;
}
/** @return image writer parameters */
public ImageWriterParams getWriterParams() {
return writerParams;
}
}
|