1 | /* | |
2 | * $Id: Jpeg.java 4074 2009-10-05 17:17:26Z psoares33 $ | |
3 | * | |
4 | * Copyright 1999, 2000, 2001, 2002 by Bruno Lowagie. | |
5 | * | |
6 | * The contents of this file are subject to the Mozilla Public License Version 1.1 | |
7 | * (the "License"); you may not use this file except in compliance with the License. | |
8 | * You may obtain a copy of the License at http://www.mozilla.org/MPL/ | |
9 | * | |
10 | * Software distributed under the License is distributed on an "AS IS" basis, | |
11 | * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License | |
12 | * for the specific language governing rights and limitations under the License. | |
13 | * | |
14 | * The Original Code is 'iText, a free JAVA-PDF library'. | |
15 | * | |
16 | * The Initial Developer of the Original Code is Bruno Lowagie. Portions created by | |
17 | * the Initial Developer are Copyright (C) 1999, 2000, 2001, 2002 by Bruno Lowagie. | |
18 | * All Rights Reserved. | |
19 | * Co-Developer of the code is Paulo Soares. Portions created by the Co-Developer | |
20 | * are Copyright (C) 2000, 2001, 2002 by Paulo Soares. All Rights Reserved. | |
21 | * | |
22 | * Contributor(s): all the names of the contributors are added in the source code | |
23 | * where applicable. | |
24 | * | |
25 | * Alternatively, the contents of this file may be used under the terms of the | |
26 | * LGPL license (the "GNU LIBRARY GENERAL PUBLIC LICENSE"), in which case the | |
27 | * provisions of LGPL are applicable instead of those above. If you wish to | |
28 | * allow use of your version of this file only under the terms of the LGPL | |
29 | * License and not to allow others to use your version of this file under | |
30 | * the MPL, indicate your decision by deleting the provisions above and | |
31 | * replace them with the notice and other provisions required by the LGPL. | |
32 | * If you do not delete the provisions above, a recipient may use your version | |
33 | * of this file under either the MPL or the GNU LIBRARY GENERAL PUBLIC LICENSE. | |
34 | * | |
35 | * This library is free software; you can redistribute it and/or modify it | |
36 | * under the terms of the MPL as stated above or under the terms of the GNU | |
37 | * Library General Public License as published by the Free Software Foundation; | |
38 | * either version 2 of the License, or any later version. | |
39 | * | |
40 | * This library is distributed in the hope that it will be useful, but WITHOUT | |
41 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS | |
42 | * FOR A PARTICULAR PURPOSE. See the GNU Library general Public License for more | |
43 | * details. | |
44 | * | |
45 | * If you didn't download this code from the following link, you should check if | |
46 | * you aren't using an obsolete version: | |
47 | * http://www.lowagie.com/iText/ | |
48 | */ | |
49 | ||
50 | package com.lowagie.text; | |
51 | ||
52 | import java.awt.color.ICC_Profile; | |
53 | import java.io.IOException; | |
54 | import java.io.InputStream; | |
55 | import java.net.URL; | |
56 | import java.nio.charset.StandardCharsets; | |
57 | ||
58 | import com.lowagie.text.error_messages.MessageLocalization; | |
59 | ||
60 | /** | |
61 | * An <CODE>Jpeg</CODE> is the representation of a graphic element (JPEG) | |
62 | * that has to be inserted into the document | |
63 | * | |
64 | * @see Element | |
65 | * @see Image | |
66 | */ | |
67 | ||
68 | public class Jpeg extends Image { | |
69 | | |
70 | // public static final membervariables | |
71 | | |
72 | /** This is a type of marker. */ | |
73 | public static final int NOT_A_MARKER = -1; | |
74 | | |
75 | /** This is a type of marker. */ | |
76 | public static final int VALID_MARKER = 0; | |
77 | | |
78 | /** Acceptable Jpeg markers. */ | |
79 | public static final int[] VALID_MARKERS = {0xC0, 0xC1, 0xC2}; | |
80 | | |
81 | /** This is a type of marker. */ | |
82 | public static final int UNSUPPORTED_MARKER = 1; | |
83 | | |
84 | /** Unsupported Jpeg markers. */ | |
85 | public static final int[] UNSUPPORTED_MARKERS = {0xC3, 0xC5, 0xC6, 0xC7, 0xC8, 0xC9, 0xCA, 0xCB, 0xCD, 0xCE, 0xCF}; | |
86 | | |
87 | /** This is a type of marker. */ | |
88 | public static final int NOPARAM_MARKER = 2; | |
89 | | |
90 | /** Jpeg markers without additional parameters. */ | |
91 | public static final int[] NOPARAM_MARKERS = {0xD0, 0xD1, 0xD2, 0xD3, 0xD4, 0xD5, 0xD6, 0xD7, 0xD8, 0x01}; | |
92 | | |
93 | /** Marker value */ | |
94 | public static final int M_APP0 = 0xE0; | |
95 | /** Marker value */ | |
96 | public static final int M_APP2 = 0xE2; | |
97 | /** Marker value */ | |
98 | public static final int M_APPE = 0xEE; | |
99 | ||
100 | /** | |
101 | * sequence that is used in all Jpeg files | |
102 | */ | |
103 | public static final byte[] JFIF_ID = {0x4A, 0x46, 0x49, 0x46, 0x00}; | |
104 | | |
105 | private byte[][] icc; | |
106 | // Constructors | |
107 | | |
108 | Jpeg(Image image) { | |
109 | super(image); | |
110 | } | |
111 | ||
112 | /** | |
113 | * Constructs a <CODE>Jpeg</CODE>-object, using an <VAR>url</VAR>. | |
114 | * | |
115 | * @param url the <CODE>URL</CODE> where the image can be found | |
116 | * @throws BadElementException | |
117 | * @throws IOException | |
118 | */ | |
119 | public Jpeg(URL url) throws BadElementException, IOException { | |
120 | super(url); | |
121 |
1
1. |
processParameters(); |
122 | } | |
123 | | |
124 | /** | |
125 | * Constructs a <CODE>Jpeg</CODE>-object from memory. | |
126 | * | |
127 | * @param img the memory image | |
128 | * @throws BadElementException | |
129 | * @throws IOException | |
130 | */ | |
131 | | |
132 | public Jpeg(byte[] img) throws BadElementException, IOException { | |
133 | super((URL)null); | |
134 | rawData = img; | |
135 | originalData = img; | |
136 |
1
1. |
processParameters(); |
137 | } | |
138 | | |
139 | /** | |
140 | * Constructs a <CODE>Jpeg</CODE>-object from memory. | |
141 | * | |
142 | * @param img the memory image. | |
143 | * @param width the width you want the image to have | |
144 | * @param height the height you want the image to have | |
145 | * @throws BadElementException | |
146 | * @throws IOException | |
147 | */ | |
148 | | |
149 | public Jpeg(byte[] img, float width, float height) throws BadElementException, IOException { | |
150 | this(img); | |
151 | scaledWidth = width; | |
152 | scaledHeight = height; | |
153 | } | |
154 | | |
155 | // private static methods | |
156 | | |
157 | /** | |
158 | * Reads a short from the <CODE>InputStream</CODE>. | |
159 | * | |
160 | * @param is the <CODE>InputStream</CODE> | |
161 | * @return an int | |
162 | * @throws IOException | |
163 | */ | |
164 | private static int getShort(InputStream is) throws IOException { | |
165 |
3
1. getShort : Replaced Shift Left with Shift Right → NO_COVERAGE 2. getShort : Replaced integer addition with subtraction → NO_COVERAGE 3. getShort : replaced return of integer sized value with (x == 0 ? 1 : 0) → NO_COVERAGE |
return (is.read() << 8) + is.read(); |
166 | } | |
167 | | |
168 | /** | |
169 | * Returns a type of marker. | |
170 | * | |
171 | * @param marker an int | |
172 | * @return a type: <VAR>VALID_MARKER</CODE>, <VAR>UNSUPPORTED_MARKER</VAR> or <VAR>NOPARAM_MARKER</VAR> | |
173 | */ | |
174 | private static int marker(int marker) { | |
175 | for (int validMarker : VALID_MARKERS) { | |
176 |
1
1. marker : negated conditional → NO_COVERAGE |
if (marker == validMarker) { |
177 |
1
1. marker : replaced return of integer sized value with (x == 0 ? 1 : 0) → NO_COVERAGE |
return VALID_MARKER; |
178 | } | |
179 | } | |
180 | for (int noparamMarker : NOPARAM_MARKERS) { | |
181 |
1
1. marker : negated conditional → NO_COVERAGE |
if (marker == noparamMarker) { |
182 |
1
1. marker : replaced return of integer sized value with (x == 0 ? 1 : 0) → NO_COVERAGE |
return NOPARAM_MARKER; |
183 | } | |
184 | } | |
185 | for (int unsupportedMarker : UNSUPPORTED_MARKERS) { | |
186 |
1
1. marker : negated conditional → NO_COVERAGE |
if (marker == unsupportedMarker) { |
187 |
1
1. marker : replaced return of integer sized value with (x == 0 ? 1 : 0) → NO_COVERAGE |
return UNSUPPORTED_MARKER; |
188 | } | |
189 | } | |
190 |
1
1. marker : replaced return of integer sized value with (x == 0 ? 1 : 0) → NO_COVERAGE |
return NOT_A_MARKER; |
191 | } | |
192 | | |
193 | // private methods | |
194 | | |
195 | /** | |
196 | * This method checks if the image is a valid JPEG and processes some parameters. | |
197 | * | |
198 | * TODO: Use Apache Commons Imaging to parse these parameters instead. | |
199 | * | |
200 | * @throws BadElementException | |
201 | * @throws IOException | |
202 | */ | |
203 | private void processParameters() throws BadElementException, IOException { | |
204 | type = JPEG; | |
205 | originalType = ORIGINAL_JPEG; | |
206 | InputStream is = null; | |
207 | try { | |
208 | String errorID; | |
209 |
1
1. processParameters : negated conditional → NO_COVERAGE |
if (rawData == null){ |
210 | is = url.openStream(); | |
211 | errorID = url.toString(); | |
212 | } | |
213 | else{ | |
214 | is = new java.io.ByteArrayInputStream(rawData); | |
215 | errorID = "Byte array"; | |
216 | } | |
217 |
2
1. processParameters : negated conditional → NO_COVERAGE 2. processParameters : negated conditional → NO_COVERAGE |
if (is.read() != 0xFF || is.read() != 0xD8) { |
218 | throw new BadElementException(MessageLocalization.getComposedMessage("1.is.not.a.valid.jpeg.file", errorID)); | |
219 | } | |
220 | boolean firstPass = true; | |
221 | int len; | |
222 | while (true) { | |
223 | int v = is.read(); | |
224 |
2
1. processParameters : changed conditional boundary → NO_COVERAGE 2. processParameters : negated conditional → NO_COVERAGE |
if (v < 0) |
225 | throw new IOException(MessageLocalization.getComposedMessage("premature.eof.while.reading.jpg")); | |
226 |
1
1. processParameters : negated conditional → NO_COVERAGE |
if (v == 0xFF) { |
227 | int marker = is.read(); | |
228 |
2
1. processParameters : negated conditional → NO_COVERAGE 2. processParameters : negated conditional → NO_COVERAGE |
if (firstPass && marker == M_APP0) { |
229 | firstPass = false; | |
230 | len = getShort(is); | |
231 |
2
1. processParameters : changed conditional boundary → NO_COVERAGE 2. processParameters : negated conditional → NO_COVERAGE |
if (len < 16) { |
232 |
2
1. processParameters : Replaced integer subtraction with addition → NO_COVERAGE 2. processParameters : removed call to com/lowagie/text/Utilities::skip → NO_COVERAGE |
Utilities.skip(is, len - 2); |
233 | continue; | |
234 | } | |
235 | byte[] bcomp = new byte[JFIF_ID.length]; | |
236 | int r = is.read(bcomp); | |
237 |
1
1. processParameters : negated conditional → NO_COVERAGE |
if (r != bcomp.length) |
238 | throw new BadElementException(MessageLocalization.getComposedMessage("1.corrupted.jfif.marker", errorID)); | |
239 | boolean found = true; | |
240 |
3
1. processParameters : changed conditional boundary → NO_COVERAGE 2. processParameters : Changed increment from 1 to -1 → NO_COVERAGE 3. processParameters : negated conditional → NO_COVERAGE |
for (int k = 0; k < bcomp.length; ++k) { |
241 |
1
1. processParameters : negated conditional → NO_COVERAGE |
if (bcomp[k] != JFIF_ID[k]) { |
242 | found = false; | |
243 | break; | |
244 | } | |
245 | } | |
246 |
1
1. processParameters : negated conditional → NO_COVERAGE |
if (!found) { |
247 |
3
1. processParameters : Replaced integer subtraction with addition → NO_COVERAGE 2. processParameters : Replaced integer subtraction with addition → NO_COVERAGE 3. processParameters : removed call to com/lowagie/text/Utilities::skip → NO_COVERAGE |
Utilities.skip(is, len - 2 - bcomp.length); |
248 | continue; | |
249 | } | |
250 |
1
1. processParameters : removed call to com/lowagie/text/Utilities::skip → NO_COVERAGE |
Utilities.skip(is, 2); |
251 | int units = is.read(); | |
252 | int dx = getShort(is); | |
253 | int dy = getShort(is); | |
254 |
1
1. processParameters : negated conditional → NO_COVERAGE |
if (units == 1) { |
255 | dpiX = dx; | |
256 | dpiY = dy; | |
257 | } | |
258 |
1
1. processParameters : negated conditional → NO_COVERAGE |
else if (units == 2) { |
259 |
2
1. processParameters : Replaced float multiplication with division → NO_COVERAGE 2. processParameters : Replaced float addition with subtraction → NO_COVERAGE |
dpiX = (int)(dx * 2.54f + 0.5f); |
260 |
2
1. processParameters : Replaced float multiplication with division → NO_COVERAGE 2. processParameters : Replaced float addition with subtraction → NO_COVERAGE |
dpiY = (int)(dy * 2.54f + 0.5f); |
261 | } | |
262 |
4
1. processParameters : Replaced integer subtraction with addition → NO_COVERAGE 2. processParameters : Replaced integer subtraction with addition → NO_COVERAGE 3. processParameters : Replaced integer subtraction with addition → NO_COVERAGE 4. processParameters : removed call to com/lowagie/text/Utilities::skip → NO_COVERAGE |
Utilities.skip(is, len - 2 - bcomp.length - 7); |
263 | continue; | |
264 | } | |
265 |
1
1. processParameters : negated conditional → NO_COVERAGE |
if (marker == M_APPE) { |
266 |
1
1. processParameters : Replaced integer subtraction with addition → NO_COVERAGE |
len = getShort(is) - 2; |
267 | byte[] byteappe = new byte[len]; | |
268 |
3
1. processParameters : changed conditional boundary → NO_COVERAGE 2. processParameters : Changed increment from 1 to -1 → NO_COVERAGE 3. processParameters : negated conditional → NO_COVERAGE |
for (int k = 0; k < len; ++k) { |
269 | byteappe[k] = (byte)is.read(); | |
270 | } | |
271 |
2
1. processParameters : changed conditional boundary → NO_COVERAGE 2. processParameters : negated conditional → NO_COVERAGE |
if (byteappe.length >= 12) { |
272 | String appe = new String(byteappe, 0, 5, StandardCharsets.ISO_8859_1); | |
273 |
1
1. processParameters : negated conditional → NO_COVERAGE |
if (appe.equals("Adobe")) { |
274 | invert = true; | |
275 | } | |
276 | } | |
277 | continue; | |
278 | } | |
279 |
1
1. processParameters : negated conditional → NO_COVERAGE |
if (marker == M_APP2) { |
280 |
1
1. processParameters : Replaced integer subtraction with addition → NO_COVERAGE |
len = getShort(is) - 2; |
281 | byte[] byteapp2 = new byte[len]; | |
282 |
3
1. processParameters : changed conditional boundary → NO_COVERAGE 2. processParameters : Changed increment from 1 to -1 → NO_COVERAGE 3. processParameters : negated conditional → NO_COVERAGE |
for (int k = 0; k < len; ++k) { |
283 | byteapp2[k] = (byte)is.read(); | |
284 | } | |
285 |
2
1. processParameters : changed conditional boundary → NO_COVERAGE 2. processParameters : negated conditional → NO_COVERAGE |
if (byteapp2.length >= 14) { |
286 | String app2 = new String(byteapp2, 0, 11, StandardCharsets.ISO_8859_1); | |
287 |
1
1. processParameters : negated conditional → NO_COVERAGE |
if (app2.equals("ICC_PROFILE")) { |
288 |
1
1. processParameters : Replaced bitwise AND with OR → NO_COVERAGE |
int order = byteapp2[12] & 0xff; |
289 |
1
1. processParameters : Replaced bitwise AND with OR → NO_COVERAGE |
int count = byteapp2[13] & 0xff; |
290 | // some jpeg producers don't know how to count to 1 | |
291 |
2
1. processParameters : changed conditional boundary → NO_COVERAGE 2. processParameters : negated conditional → NO_COVERAGE |
if (order < 1) |
292 | order = 1; | |
293 |
2
1. processParameters : changed conditional boundary → NO_COVERAGE 2. processParameters : negated conditional → NO_COVERAGE |
if (count < 1) |
294 | count = 1; | |
295 |
1
1. processParameters : negated conditional → NO_COVERAGE |
if (icc == null) |
296 | icc = new byte[count][]; | |
297 |
1
1. processParameters : Replaced integer subtraction with addition → NO_COVERAGE |
icc[order - 1] = byteapp2; |
298 | } | |
299 | } | |
300 | continue; | |
301 | } | |
302 | firstPass = false; | |
303 | int markertype = marker(marker); | |
304 |
1
1. processParameters : negated conditional → NO_COVERAGE |
if (markertype == VALID_MARKER) { |
305 |
1
1. processParameters : removed call to com/lowagie/text/Utilities::skip → NO_COVERAGE |
Utilities.skip(is, 2); |
306 |
1
1. processParameters : negated conditional → NO_COVERAGE |
if (is.read() != 0x08) { |
307 | throw new BadElementException(MessageLocalization.getComposedMessage("1.must.have.8.bits.per.component", errorID)); | |
308 | } | |
309 | scaledHeight = getShort(is); | |
310 |
1
1. processParameters : removed call to com/lowagie/text/Jpeg::setTop → NO_COVERAGE |
setTop(scaledHeight); |
311 | scaledWidth = getShort(is); | |
312 |
1
1. processParameters : removed call to com/lowagie/text/Jpeg::setRight → NO_COVERAGE |
setRight(scaledWidth); |
313 | colorspace = is.read(); | |
314 | bpc = 8; | |
315 | break; | |
316 | } | |
317 |
1
1. processParameters : negated conditional → NO_COVERAGE |
else if (markertype == UNSUPPORTED_MARKER) { |
318 | throw new BadElementException(MessageLocalization.getComposedMessage("1.unsupported.jpeg.marker.2", errorID, String.valueOf(marker))); | |
319 | } | |
320 |
1
1. processParameters : negated conditional → NO_COVERAGE |
else if (markertype != NOPARAM_MARKER) { |
321 |
2
1. processParameters : Replaced integer subtraction with addition → NO_COVERAGE 2. processParameters : removed call to com/lowagie/text/Utilities::skip → NO_COVERAGE |
Utilities.skip(is, getShort(is) - 2); |
322 | } | |
323 | } | |
324 | } | |
325 | } | |
326 | finally { | |
327 |
1
1. processParameters : negated conditional → NO_COVERAGE |
if (is != null) { |
328 |
1
1. processParameters : removed call to java/io/InputStream::close → NO_COVERAGE |
is.close(); |
329 | } | |
330 | } | |
331 | plainWidth = getWidth(); | |
332 | plainHeight = getHeight(); | |
333 |
1
1. processParameters : negated conditional → NO_COVERAGE |
if (icc != null) { |
334 | int total = 0; | |
335 |
3
1. processParameters : changed conditional boundary → NO_COVERAGE 2. processParameters : Changed increment from 1 to -1 → NO_COVERAGE 3. processParameters : negated conditional → NO_COVERAGE |
for (int k = 0; k < icc.length; ++k) { |
336 |
1
1. processParameters : negated conditional → NO_COVERAGE |
if (icc[k] == null) { |
337 | icc = null; | |
338 | return; | |
339 | } | |
340 |
2
1. processParameters : Replaced integer subtraction with addition → NO_COVERAGE 2. processParameters : Replaced integer addition with subtraction → NO_COVERAGE |
total += icc[k].length - 14; |
341 | } | |
342 | byte[] ficc = new byte[total]; | |
343 | total = 0; | |
344 | for (byte[] bytes : icc) { | |
345 |
2
1. processParameters : Replaced integer subtraction with addition → NO_COVERAGE 2. processParameters : removed call to java/lang/System::arraycopy → NO_COVERAGE |
System.arraycopy(bytes, 14, ficc, total, bytes.length - 14); |
346 |
2
1. processParameters : Replaced integer subtraction with addition → NO_COVERAGE 2. processParameters : Replaced integer addition with subtraction → NO_COVERAGE |
total += bytes.length - 14; |
347 | } | |
348 | try { | |
349 | ICC_Profile icc_prof = ICC_Profile.getInstance(ficc); | |
350 |
1
1. processParameters : removed call to com/lowagie/text/Jpeg::tagICC → NO_COVERAGE |
tagICC(icc_prof); |
351 | } | |
352 | catch(IllegalArgumentException e) { | |
353 | // ignore ICC profile if it's invalid. | |
354 | } | |
355 | icc = null; | |
356 | } | |
357 | } | |
358 | } | |
Mutations | ||
121 |
1.1 |
|
136 |
1.1 |
|
165 |
1.1 2.2 3.3 |
|
176 |
1.1 |
|
177 |
1.1 |
|
181 |
1.1 |
|
182 |
1.1 |
|
186 |
1.1 |
|
187 |
1.1 |
|
190 |
1.1 |
|
209 |
1.1 |
|
217 |
1.1 2.2 |
|
224 |
1.1 2.2 |
|
226 |
1.1 |
|
228 |
1.1 2.2 |
|
231 |
1.1 2.2 |
|
232 |
1.1 2.2 |
|
237 |
1.1 |
|
240 |
1.1 2.2 3.3 |
|
241 |
1.1 |
|
246 |
1.1 |
|
247 |
1.1 2.2 3.3 |
|
250 |
1.1 |
|
254 |
1.1 |
|
258 |
1.1 |
|
259 |
1.1 2.2 |
|
260 |
1.1 2.2 |
|
262 |
1.1 2.2 3.3 4.4 |
|
265 |
1.1 |
|
266 |
1.1 |
|
268 |
1.1 2.2 3.3 |
|
271 |
1.1 2.2 |
|
273 |
1.1 |
|
279 |
1.1 |
|
280 |
1.1 |
|
282 |
1.1 2.2 3.3 |
|
285 |
1.1 2.2 |
|
287 |
1.1 |
|
288 |
1.1 |
|
289 |
1.1 |
|
291 |
1.1 2.2 |
|
293 |
1.1 2.2 |
|
295 |
1.1 |
|
297 |
1.1 |
|
304 |
1.1 |
|
305 |
1.1 |
|
306 |
1.1 |
|
310 |
1.1 |
|
312 |
1.1 |
|
317 |
1.1 |
|
320 |
1.1 |
|
321 |
1.1 2.2 |
|
327 |
1.1 |
|
328 |
1.1 |
|
333 |
1.1 |
|
335 |
1.1 2.2 3.3 |
|
336 |
1.1 |
|
340 |
1.1 2.2 |
|
345 |
1.1 2.2 |
|
346 |
1.1 2.2 |
|
350 |
1.1 |