Skip to content

Commit faedebb

Browse files
Make PdfImageXObject more independent of underlying object flushing
Previously PdfImageXObject reusing after flushing relied on wrapper flush() being called, so width and height were preserved. Now they are extracted on wrapper creation. Image XObject dictionary width and height are based on the underlying image bytes and are not expected to be changed: we are not aware of such scenarios and if someone manually changes those entries, they would need to create a new wrapper for changed object. Also, PDF image decoding functionality is extracted to a separate class. DEVSIX-2835
1 parent c10d8f3 commit faedebb

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

55 files changed

+562
-241
lines changed
Lines changed: 220 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,220 @@
1+
/*
2+
This file is part of the iText (R) project.
3+
Copyright (c) 1998-2019 iText Group NV
4+
Authors: iText Software.
5+
6+
This program is free software; you can redistribute it and/or modify
7+
it under the terms of the GNU Affero General Public License version 3
8+
as published by the Free Software Foundation with the addition of the
9+
following permission added to Section 15 as permitted in Section 7(a):
10+
FOR ANY PART OF THE COVERED WORK IN WHICH THE COPYRIGHT IS OWNED BY
11+
ITEXT GROUP. ITEXT GROUP DISCLAIMS THE WARRANTY OF NON INFRINGEMENT
12+
OF THIRD PARTY RIGHTS
13+
14+
This program is distributed in the hope that it will be useful, but
15+
WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
16+
or FITNESS FOR A PARTICULAR PURPOSE.
17+
See the GNU Affero General Public License for more details.
18+
You should have received a copy of the GNU Affero General Public License
19+
along with this program; if not, see http://www.gnu.org/licenses or write to
20+
the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
21+
Boston, MA, 02110-1301 USA, or download the license from the following URL:
22+
http://itextpdf.com/terms-of-use/
23+
24+
The interactive user interfaces in modified source and object code versions
25+
of this program must display Appropriate Legal Notices, as required under
26+
Section 5 of the GNU Affero General Public License.
27+
28+
In accordance with Section 7(b) of the GNU Affero General Public License,
29+
a covered work must retain the producer line in every PDF that is created
30+
or manipulated using iText.
31+
32+
You can be released from the requirements of the license by purchasing
33+
a commercial license. Buying such a license is mandatory as soon as you
34+
develop commercial activities involving the iText software without
35+
disclosing the source code of your own applications.
36+
These activities include: offering paid services to customers as an ASP,
37+
serving PDFs on the fly in a web application, shipping iText with a closed
38+
source product.
39+
40+
For more information, please contact iText Software Corp. at this
41+
42+
*/
43+
package com.itextpdf.kernel.pdf.xobject;
44+
45+
import com.itextpdf.io.codec.PngWriter;
46+
import com.itextpdf.io.codec.TIFFConstants;
47+
import com.itextpdf.io.codec.TiffWriter;
48+
import com.itextpdf.kernel.Version;
49+
import com.itextpdf.kernel.pdf.PdfArray;
50+
import com.itextpdf.kernel.pdf.PdfName;
51+
import com.itextpdf.kernel.pdf.PdfObject;
52+
import com.itextpdf.kernel.pdf.PdfStream;
53+
import com.itextpdf.kernel.pdf.PdfString;
54+
import java.io.IOException;
55+
56+
class ImagePdfBytesInfo {
57+
private int pngColorType;
58+
private int pngBitDepth;
59+
private int bpc;
60+
private byte[] palette;
61+
private byte[] icc;
62+
private int stride;
63+
private int width;
64+
private int height;
65+
private PdfObject colorspace;
66+
private PdfArray decode;
67+
68+
public ImagePdfBytesInfo(PdfImageXObject imageXObject) {
69+
pngColorType = -1;
70+
bpc = imageXObject.getPdfObject().getAsNumber(PdfName.BitsPerComponent).intValue();
71+
pngBitDepth = bpc;
72+
73+
palette = null;
74+
icc = null;
75+
stride = 0;
76+
width = (int) imageXObject.getWidth();
77+
height = (int) imageXObject.getHeight();
78+
colorspace = imageXObject.getPdfObject().get(PdfName.ColorSpace);
79+
decode = imageXObject.getPdfObject().getAsArray(PdfName.Decode);
80+
findColorspace(colorspace, true);
81+
}
82+
83+
public int getPngColorType() {
84+
return pngColorType;
85+
}
86+
87+
public byte[] decodeTiffAndPngBytes(byte[] imageBytes) throws IOException {
88+
java.io.ByteArrayOutputStream ms = new java.io.ByteArrayOutputStream();
89+
if (pngColorType < 0) {
90+
if (bpc != 8)
91+
throw new com.itextpdf.io.IOException(com.itextpdf.io.IOException.ColorDepthIsNotSupported).setMessageParams(bpc);
92+
93+
if (colorspace instanceof PdfArray) {
94+
PdfArray ca = (PdfArray) colorspace;
95+
PdfObject tyca = ca.get(0);
96+
if (!PdfName.ICCBased.equals(tyca))
97+
throw new com.itextpdf.io.IOException(com.itextpdf.io.IOException.ColorSpaceIsNotSupported).setMessageParams(tyca.toString());
98+
PdfStream pr = (PdfStream) ca.get(1);
99+
int n = pr.getAsNumber(PdfName.N).intValue();
100+
if (n != 4) {
101+
throw new com.itextpdf.io.IOException(com.itextpdf.io.IOException.NValueIsNotSupported).setMessageParams(n);
102+
}
103+
icc = pr.getBytes();
104+
} else if (!PdfName.DeviceCMYK.equals(colorspace)) {
105+
throw new com.itextpdf.io.IOException(com.itextpdf.io.IOException.ColorSpaceIsNotSupported).setMessageParams(colorspace.toString());
106+
}
107+
stride = 4 * width;
108+
TiffWriter wr = new TiffWriter();
109+
wr.addField(new TiffWriter.FieldShort(TIFFConstants.TIFFTAG_SAMPLESPERPIXEL, 4));
110+
wr.addField(new TiffWriter.FieldShort(TIFFConstants.TIFFTAG_BITSPERSAMPLE, new int[]{8, 8, 8, 8}));
111+
wr.addField(new TiffWriter.FieldShort(TIFFConstants.TIFFTAG_PHOTOMETRIC, TIFFConstants.PHOTOMETRIC_SEPARATED));
112+
wr.addField(new TiffWriter.FieldLong(TIFFConstants.TIFFTAG_IMAGEWIDTH, (int) width));
113+
wr.addField(new TiffWriter.FieldLong(TIFFConstants.TIFFTAG_IMAGELENGTH, (int) height));
114+
wr.addField(new TiffWriter.FieldShort(TIFFConstants.TIFFTAG_COMPRESSION, TIFFConstants.COMPRESSION_LZW));
115+
wr.addField(new TiffWriter.FieldShort(TIFFConstants.TIFFTAG_PREDICTOR, TIFFConstants.PREDICTOR_HORIZONTAL_DIFFERENCING));
116+
wr.addField(new TiffWriter.FieldLong(TIFFConstants.TIFFTAG_ROWSPERSTRIP, (int) height));
117+
wr.addField(new TiffWriter.FieldRational(TIFFConstants.TIFFTAG_XRESOLUTION, new int[]{300, 1}));
118+
wr.addField(new TiffWriter.FieldRational(TIFFConstants.TIFFTAG_YRESOLUTION, new int[]{300, 1}));
119+
wr.addField(new TiffWriter.FieldShort(TIFFConstants.TIFFTAG_RESOLUTIONUNIT, TIFFConstants.RESUNIT_INCH));
120+
wr.addField(new TiffWriter.FieldAscii(TIFFConstants.TIFFTAG_SOFTWARE, Version.getInstance().getVersion()));
121+
java.io.ByteArrayOutputStream comp = new java.io.ByteArrayOutputStream();
122+
TiffWriter.compressLZW(comp, 2, imageBytes, (int) height, 4, stride);
123+
byte[] buf = comp.toByteArray();
124+
wr.addField(new TiffWriter.FieldImage(buf));
125+
wr.addField(new TiffWriter.FieldLong(TIFFConstants.TIFFTAG_STRIPBYTECOUNTS, buf.length));
126+
if (icc != null) {
127+
wr.addField(new TiffWriter.FieldUndefined(TIFFConstants.TIFFTAG_ICCPROFILE, icc));
128+
}
129+
wr.writeFile(ms);
130+
131+
imageBytes = ms.toByteArray();
132+
return imageBytes;
133+
} else {
134+
PngWriter png = new PngWriter(ms);
135+
if (decode != null) {
136+
if (pngBitDepth == 1) {
137+
// if the decode array is 1,0, then we need to invert the image
138+
if (decode.getAsNumber(0).intValue() == 1 && decode.getAsNumber(1).intValue() == 0) {
139+
int len = imageBytes.length;
140+
for (int t = 0; t < len; ++t) {
141+
imageBytes[t] ^= 0xff;
142+
}
143+
} else {
144+
// if the decode array is 0,1, do nothing. It's possible that the array could be 0,0 or 1,1 - but that would be silly, so we'll just ignore that case
145+
}
146+
} else {
147+
// todo: add decode transformation for other depths
148+
}
149+
}
150+
png.writeHeader(width, height, pngBitDepth, pngColorType);
151+
if (icc != null) {
152+
png.writeIccProfile(icc);
153+
}
154+
if (palette != null) {
155+
png.writePalette(palette);
156+
}
157+
png.writeData(imageBytes, stride);
158+
png.writeEnd();
159+
imageBytes = ms.toByteArray();
160+
return imageBytes;
161+
}
162+
}
163+
164+
/**
165+
* Sets state of this object according to the color space
166+
*
167+
* @param csObj the colorspace to use
168+
* @param allowIndexed whether indexed color spaces will be resolved (used for recursive call)
169+
* @param width
170+
* @param height
171+
* @throws IOException if there is a problem with reading from the underlying stream
172+
*/
173+
private void findColorspace(PdfObject csObj, boolean allowIndexed) {
174+
if (PdfName.DeviceGray.equals(csObj) || (csObj == null && bpc == 1)) { // handle imagemasks
175+
stride = (width * bpc + 7) / 8;
176+
pngColorType = 0;
177+
} else if (PdfName.DeviceRGB.equals(csObj)) {
178+
if (bpc == 8 || bpc == 16) {
179+
stride = (width * bpc * 3 + 7) / 8;
180+
pngColorType = 2;
181+
}
182+
} else if (csObj instanceof PdfArray) {
183+
PdfArray ca = (PdfArray) csObj;
184+
PdfObject tyca = ca.get(0);
185+
if (PdfName.CalGray.equals(tyca)) {
186+
stride = (width * bpc + 7) / 8;
187+
pngColorType = 0;
188+
} else if (PdfName.CalRGB.equals(tyca)) {
189+
if (bpc == 8 || bpc == 16) {
190+
stride = (width * bpc * 3 + 7) / 8;
191+
pngColorType = 2;
192+
}
193+
} else if (PdfName.ICCBased.equals(tyca)) {
194+
PdfStream pr = (PdfStream) ca.get(1);
195+
int n = pr.getAsNumber(PdfName.N).intValue();
196+
if (n == 1) {
197+
stride = (width * bpc + 7) / 8;
198+
pngColorType = 0;
199+
icc = pr.getBytes();
200+
} else if (n == 3) {
201+
stride = (width * bpc * 3 + 7) / 8;
202+
pngColorType = 2;
203+
icc = pr.getBytes();
204+
}
205+
} else if (allowIndexed && PdfName.Indexed.equals(tyca)) {
206+
findColorspace(ca.get(1), false);
207+
if (pngColorType == 2) {
208+
PdfObject id2 = ca.get(3);
209+
if (id2 instanceof PdfString) {
210+
palette = ((PdfString) id2).getValueBytes();
211+
} else if (id2 instanceof PdfStream) {
212+
palette = ((PdfStream) id2).getBytes();
213+
}
214+
stride = (width * bpc + 7) / 8;
215+
pngColorType = 3;
216+
}
217+
}
218+
}
219+
}
220+
}

0 commit comments

Comments
 (0)