author | bae |
Tue, 26 Feb 2013 00:41:40 +0400 | |
changeset 16881 | f105ea43e3aa |
parent 14342 | 8435a30053c1 |
child 20798 | d03e6abc2b51 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
14342
8435a30053c1
7197491: update copyright year to match last edit in jdk8 jdk repository
alanb
parents:
12559
diff
changeset
|
2 |
* Copyright (c) 2000, 2012, Oracle and/or its affiliates. All rights reserved. |
2 | 3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 |
* |
|
5 |
* This code is free software; you can redistribute it and/or modify it |
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
|
5506 | 7 |
* published by the Free Software Foundation. Oracle designates this |
2 | 8 |
* particular file as subject to the "Classpath" exception as provided |
5506 | 9 |
* by Oracle in the LICENSE file that accompanied this code. |
2 | 10 |
* |
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
15 |
* accompanied this code). |
|
16 |
* |
|
17 |
* You should have received a copy of the GNU General Public License version |
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 |
* |
|
5506 | 21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
22 |
* or visit www.oracle.com if you need additional information or have any |
|
23 |
* questions. |
|
2 | 24 |
*/ |
25 |
||
26 |
package com.sun.imageio.plugins.jpeg; |
|
27 |
||
28 |
import javax.imageio.IIOException; |
|
29 |
import javax.imageio.ImageReader; |
|
30 |
import javax.imageio.ImageReadParam; |
|
31 |
import javax.imageio.ImageTypeSpecifier; |
|
32 |
import javax.imageio.metadata.IIOMetadata; |
|
33 |
import javax.imageio.spi.ImageReaderSpi; |
|
34 |
import javax.imageio.stream.ImageInputStream; |
|
35 |
import javax.imageio.plugins.jpeg.JPEGImageReadParam; |
|
36 |
import javax.imageio.plugins.jpeg.JPEGQTable; |
|
37 |
import javax.imageio.plugins.jpeg.JPEGHuffmanTable; |
|
38 |
||
39 |
import java.awt.Point; |
|
40 |
import java.awt.Rectangle; |
|
41 |
import java.awt.color.ColorSpace; |
|
42 |
import java.awt.color.ICC_Profile; |
|
43 |
import java.awt.color.ICC_ColorSpace; |
|
3013 | 44 |
import java.awt.color.CMMException; |
2 | 45 |
import java.awt.image.BufferedImage; |
46 |
import java.awt.image.Raster; |
|
47 |
import java.awt.image.WritableRaster; |
|
48 |
import java.awt.image.DataBuffer; |
|
49 |
import java.awt.image.DataBufferByte; |
|
50 |
import java.awt.image.ColorModel; |
|
51 |
import java.awt.image.IndexColorModel; |
|
52 |
import java.awt.image.ColorConvertOp; |
|
53 |
import java.io.IOException; |
|
54 |
import java.util.List; |
|
55 |
import java.util.Iterator; |
|
56 |
import java.util.ArrayList; |
|
3009
de653b2cab31
4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents:
2381
diff
changeset
|
57 |
import java.util.NoSuchElementException; |
2 | 58 |
|
59 |
import sun.java2d.Disposer; |
|
60 |
import sun.java2d.DisposerRecord; |
|
61 |
||
62 |
public class JPEGImageReader extends ImageReader { |
|
63 |
||
64 |
private boolean debug = false; |
|
65 |
||
66 |
/** |
|
67 |
* The following variable contains a pointer to the IJG library |
|
68 |
* structure for this reader. It is assigned in the constructor |
|
69 |
* and then is passed in to every native call. It is set to 0 |
|
70 |
* by dispose to avoid disposing twice. |
|
71 |
*/ |
|
72 |
private long structPointer = 0; |
|
73 |
||
74 |
/** The input stream we read from */ |
|
75 |
private ImageInputStream iis = null; |
|
76 |
||
77 |
/** |
|
78 |
* List of stream positions for images, reinitialized every time |
|
79 |
* a new input source is set. |
|
80 |
*/ |
|
81 |
private List imagePositions = null; |
|
82 |
||
83 |
/** |
|
84 |
* The number of images in the stream, or 0. |
|
85 |
*/ |
|
86 |
private int numImages = 0; |
|
87 |
||
88 |
static { |
|
89 |
java.security.AccessController.doPrivileged( |
|
12559
9456ceada8b1
7164376: Replace use of sun.security.action.LoadLibraryAction with System.loadLibrary
mchung
parents:
5506
diff
changeset
|
90 |
new java.security.PrivilegedAction<Void>() { |
9456ceada8b1
7164376: Replace use of sun.security.action.LoadLibraryAction with System.loadLibrary
mchung
parents:
5506
diff
changeset
|
91 |
public Void run() { |
9456ceada8b1
7164376: Replace use of sun.security.action.LoadLibraryAction with System.loadLibrary
mchung
parents:
5506
diff
changeset
|
92 |
System.loadLibrary("jpeg"); |
9456ceada8b1
7164376: Replace use of sun.security.action.LoadLibraryAction with System.loadLibrary
mchung
parents:
5506
diff
changeset
|
93 |
return null; |
9456ceada8b1
7164376: Replace use of sun.security.action.LoadLibraryAction with System.loadLibrary
mchung
parents:
5506
diff
changeset
|
94 |
} |
9456ceada8b1
7164376: Replace use of sun.security.action.LoadLibraryAction with System.loadLibrary
mchung
parents:
5506
diff
changeset
|
95 |
}); |
2 | 96 |
initReaderIDs(ImageInputStream.class, |
97 |
JPEGQTable.class, |
|
98 |
JPEGHuffmanTable.class); |
|
99 |
} |
|
100 |
||
101 |
// The following warnings are converted to strings when used |
|
102 |
// as keys to get localized resources from JPEGImageReaderResources |
|
103 |
// and its children. |
|
104 |
||
105 |
/** |
|
106 |
* Warning code to be passed to warningOccurred to indicate |
|
107 |
* that the EOI marker is missing from the end of the stream. |
|
108 |
* This usually signals that the stream is corrupted, but |
|
109 |
* everything up to the last MCU should be usable. |
|
110 |
*/ |
|
111 |
protected static final int WARNING_NO_EOI = 0; |
|
112 |
||
113 |
/** |
|
114 |
* Warning code to be passed to warningOccurred to indicate |
|
115 |
* that a JFIF segment was encountered inside a JFXX JPEG |
|
116 |
* thumbnail and is being ignored. |
|
117 |
*/ |
|
118 |
protected static final int WARNING_NO_JFIF_IN_THUMB = 1; |
|
119 |
||
120 |
/** |
|
121 |
* Warning code to be passed to warningOccurred to indicate |
|
122 |
* that embedded ICC profile is invalid and will be ignored. |
|
123 |
*/ |
|
124 |
protected static final int WARNING_IGNORE_INVALID_ICC = 2; |
|
125 |
||
126 |
private static final int MAX_WARNING = WARNING_IGNORE_INVALID_ICC; |
|
127 |
||
128 |
/** |
|
129 |
* Image index of image for which header information |
|
130 |
* is available. |
|
131 |
*/ |
|
132 |
private int currentImage = -1; |
|
133 |
||
134 |
// The following is copied out from C after reading the header. |
|
135 |
// Unlike metadata, which may never be retrieved, we need this |
|
136 |
// if we are to read an image at all. |
|
137 |
||
138 |
/** Set by setImageData native code callback */ |
|
139 |
private int width; |
|
140 |
/** Set by setImageData native code callback */ |
|
141 |
private int height; |
|
142 |
/** |
|
143 |
* Set by setImageData native code callback. A modified |
|
144 |
* IJG+NIFTY colorspace code. |
|
145 |
*/ |
|
146 |
private int colorSpaceCode; |
|
147 |
/** |
|
148 |
* Set by setImageData native code callback. A modified |
|
149 |
* IJG+NIFTY colorspace code. |
|
150 |
*/ |
|
151 |
private int outColorSpaceCode; |
|
152 |
/** Set by setImageData native code callback */ |
|
153 |
private int numComponents; |
|
154 |
/** Set by setImageData native code callback */ |
|
155 |
private ColorSpace iccCS = null; |
|
156 |
||
157 |
||
158 |
/** If we need to post-convert in Java, convert with this op */ |
|
159 |
private ColorConvertOp convert = null; |
|
160 |
||
161 |
/** The image we are going to fill */ |
|
162 |
private BufferedImage image = null; |
|
163 |
||
164 |
/** An intermediate Raster to hold decoded data */ |
|
165 |
private WritableRaster raster = null; |
|
166 |
||
167 |
/** A view of our target Raster that we can setRect to */ |
|
168 |
private WritableRaster target = null; |
|
169 |
||
170 |
/** The databuffer for the above Raster */ |
|
171 |
private DataBufferByte buffer = null; |
|
172 |
||
173 |
/** The region in the destination where we will write pixels */ |
|
174 |
private Rectangle destROI = null; |
|
175 |
||
176 |
/** The list of destination bands, if any */ |
|
177 |
private int [] destinationBands = null; |
|
178 |
||
179 |
/** Stream metadata, cached, even when the stream is changed. */ |
|
180 |
private JPEGMetadata streamMetadata = null; |
|
181 |
||
182 |
/** Image metadata, valid for the imageMetadataIndex only. */ |
|
183 |
private JPEGMetadata imageMetadata = null; |
|
184 |
private int imageMetadataIndex = -1; |
|
185 |
||
186 |
/** |
|
187 |
* Set to true every time we seek in the stream; used to |
|
188 |
* invalidate the native buffer contents in C. |
|
189 |
*/ |
|
190 |
private boolean haveSeeked = false; |
|
191 |
||
192 |
/** |
|
193 |
* Tables that have been read from a tables-only image at the |
|
194 |
* beginning of a stream. |
|
195 |
*/ |
|
196 |
private JPEGQTable [] abbrevQTables = null; |
|
197 |
private JPEGHuffmanTable[] abbrevDCHuffmanTables = null; |
|
198 |
private JPEGHuffmanTable[] abbrevACHuffmanTables = null; |
|
199 |
||
200 |
private int minProgressivePass = 0; |
|
201 |
private int maxProgressivePass = Integer.MAX_VALUE; |
|
202 |
||
203 |
/** |
|
204 |
* Variables used by progress monitoring. |
|
205 |
*/ |
|
206 |
private static final int UNKNOWN = -1; // Number of passes |
|
207 |
private static final int MIN_ESTIMATED_PASSES = 10; // IJG default |
|
208 |
private int knownPassCount = UNKNOWN; |
|
209 |
private int pass = 0; |
|
210 |
private float percentToDate = 0.0F; |
|
211 |
private float previousPassPercentage = 0.0F; |
|
212 |
private int progInterval = 0; |
|
213 |
||
214 |
/** |
|
215 |
* Set to true once stream has been checked for stream metadata |
|
216 |
*/ |
|
217 |
private boolean tablesOnlyChecked = false; |
|
218 |
||
219 |
/** The referent to be registered with the Disposer. */ |
|
220 |
private Object disposerReferent = new Object(); |
|
221 |
||
222 |
/** The DisposerRecord that handles the actual disposal of this reader. */ |
|
223 |
private DisposerRecord disposerRecord; |
|
224 |
||
225 |
/** Sets up static C structures. */ |
|
226 |
private static native void initReaderIDs(Class iisClass, |
|
227 |
Class qTableClass, |
|
228 |
Class huffClass); |
|
229 |
||
230 |
public JPEGImageReader(ImageReaderSpi originator) { |
|
231 |
super(originator); |
|
232 |
structPointer = initJPEGImageReader(); |
|
233 |
disposerRecord = new JPEGReaderDisposerRecord(structPointer); |
|
234 |
Disposer.addRecord(disposerReferent, disposerRecord); |
|
235 |
} |
|
236 |
||
237 |
/** Sets up per-reader C structure and returns a pointer to it. */ |
|
238 |
private native long initJPEGImageReader(); |
|
239 |
||
240 |
/** |
|
241 |
* Called by the native code or other classes to signal a warning. |
|
242 |
* The code is used to lookup a localized message to be used when |
|
243 |
* sending warnings to listeners. |
|
244 |
*/ |
|
245 |
protected void warningOccurred(int code) { |
|
16881 | 246 |
cbLock.lock(); |
247 |
try { |
|
248 |
if ((code < 0) || (code > MAX_WARNING)){ |
|
249 |
throw new InternalError("Invalid warning index"); |
|
250 |
} |
|
251 |
processWarningOccurred |
|
252 |
("com.sun.imageio.plugins.jpeg.JPEGImageReaderResources", |
|
253 |
Integer.toString(code)); |
|
254 |
} finally { |
|
255 |
cbLock.unlock(); |
|
2 | 256 |
} |
257 |
} |
|
258 |
||
259 |
/** |
|
260 |
* The library has it's own error facility that emits warning messages. |
|
261 |
* This routine is called by the native code when it has already |
|
262 |
* formatted a string for output. |
|
263 |
* XXX For truly complete localization of all warning messages, |
|
264 |
* the sun_jpeg_output_message routine in the native code should |
|
265 |
* send only the codes and parameters to a method here in Java, |
|
266 |
* which will then format and send the warnings, using localized |
|
267 |
* strings. This method will have to deal with all the parameters |
|
268 |
* and formats (%u with possibly large numbers, %02d, %02x, etc.) |
|
269 |
* that actually occur in the JPEG library. For now, this prevents |
|
270 |
* library warnings from being printed to stderr. |
|
271 |
*/ |
|
272 |
protected void warningWithMessage(String msg) { |
|
16881 | 273 |
cbLock.lock(); |
274 |
try { |
|
275 |
processWarningOccurred(msg); |
|
276 |
} finally { |
|
277 |
cbLock.unlock(); |
|
278 |
} |
|
2 | 279 |
} |
280 |
||
281 |
public void setInput(Object input, |
|
282 |
boolean seekForwardOnly, |
|
283 |
boolean ignoreMetadata) |
|
284 |
{ |
|
285 |
setThreadLock(); |
|
286 |
try { |
|
16881 | 287 |
cbLock.check(); |
288 |
||
2 | 289 |
super.setInput(input, seekForwardOnly, ignoreMetadata); |
290 |
this.ignoreMetadata = ignoreMetadata; |
|
291 |
resetInternalState(); |
|
292 |
iis = (ImageInputStream) input; // Always works |
|
16881 | 293 |
setSource(structPointer); |
2 | 294 |
} finally { |
295 |
clearThreadLock(); |
|
296 |
} |
|
297 |
} |
|
298 |
||
16881 | 299 |
/** |
300 |
* This method is called from native code in order to fill |
|
301 |
* native input buffer. |
|
302 |
* |
|
303 |
* We block any attempt to change the reading state during this |
|
304 |
* method, in order to prevent a corruption of the native decoder |
|
305 |
* state. |
|
306 |
* |
|
307 |
* @return number of bytes read from the stream. |
|
308 |
*/ |
|
309 |
private int readInputData(byte[] buf, int off, int len) throws IOException { |
|
310 |
cbLock.lock(); |
|
311 |
try { |
|
312 |
return iis.read(buf, off, len); |
|
313 |
} finally { |
|
314 |
cbLock.unlock(); |
|
315 |
} |
|
316 |
} |
|
317 |
||
318 |
/** |
|
319 |
* This method is called from the native code in order to |
|
320 |
* skip requested number of bytes in the input stream. |
|
321 |
* |
|
322 |
* @param n |
|
323 |
* @return |
|
324 |
* @throws IOException |
|
325 |
*/ |
|
326 |
private long skipInputBytes(long n) throws IOException { |
|
327 |
cbLock.lock(); |
|
328 |
try { |
|
329 |
return iis.skipBytes(n); |
|
330 |
} finally { |
|
331 |
cbLock.unlock(); |
|
332 |
} |
|
333 |
} |
|
334 |
||
335 |
private native void setSource(long structPointer); |
|
2 | 336 |
|
337 |
private void checkTablesOnly() throws IOException { |
|
338 |
if (debug) { |
|
339 |
System.out.println("Checking for tables-only image"); |
|
340 |
} |
|
341 |
long savePos = iis.getStreamPosition(); |
|
342 |
if (debug) { |
|
343 |
System.out.println("saved pos is " + savePos); |
|
344 |
System.out.println("length is " + iis.length()); |
|
345 |
} |
|
346 |
// Read the first header |
|
347 |
boolean tablesOnly = readNativeHeader(true); |
|
348 |
if (tablesOnly) { |
|
349 |
if (debug) { |
|
350 |
System.out.println("tables-only image found"); |
|
351 |
long pos = iis.getStreamPosition(); |
|
352 |
System.out.println("pos after return from native is " + pos); |
|
353 |
} |
|
354 |
// This reads the tables-only image twice, once from C |
|
355 |
// and once from Java, but only if ignoreMetadata is false |
|
356 |
if (ignoreMetadata == false) { |
|
357 |
iis.seek(savePos); |
|
358 |
haveSeeked = true; |
|
359 |
streamMetadata = new JPEGMetadata(true, false, |
|
360 |
iis, this); |
|
361 |
long pos = iis.getStreamPosition(); |
|
362 |
if (debug) { |
|
363 |
System.out.println |
|
364 |
("pos after constructing stream metadata is " + pos); |
|
365 |
} |
|
366 |
} |
|
367 |
// Now we are at the first image if there are any, so add it |
|
368 |
// to the list |
|
369 |
if (hasNextImage()) { |
|
370 |
imagePositions.add(new Long(iis.getStreamPosition())); |
|
371 |
} |
|
372 |
} else { // Not tables only, so add original pos to the list |
|
373 |
imagePositions.add(new Long(savePos)); |
|
374 |
// And set current image since we've read it now |
|
375 |
currentImage = 0; |
|
376 |
} |
|
377 |
if (seekForwardOnly) { |
|
378 |
Long pos = (Long) imagePositions.get(imagePositions.size()-1); |
|
379 |
iis.flushBefore(pos.longValue()); |
|
380 |
} |
|
381 |
tablesOnlyChecked = true; |
|
382 |
} |
|
383 |
||
384 |
public int getNumImages(boolean allowSearch) throws IOException { |
|
385 |
setThreadLock(); |
|
386 |
try { // locked thread |
|
16881 | 387 |
cbLock.check(); |
388 |
||
2 | 389 |
return getNumImagesOnThread(allowSearch); |
390 |
} finally { |
|
391 |
clearThreadLock(); |
|
392 |
} |
|
393 |
} |
|
394 |
||
395 |
private int getNumImagesOnThread(boolean allowSearch) |
|
396 |
throws IOException { |
|
397 |
if (numImages != 0) { |
|
398 |
return numImages; |
|
399 |
} |
|
400 |
if (iis == null) { |
|
401 |
throw new IllegalStateException("Input not set"); |
|
402 |
} |
|
403 |
if (allowSearch == true) { |
|
404 |
if (seekForwardOnly) { |
|
405 |
throw new IllegalStateException( |
|
406 |
"seekForwardOnly and allowSearch can't both be true!"); |
|
407 |
} |
|
408 |
// Otherwise we have to read the entire stream |
|
409 |
||
410 |
if (!tablesOnlyChecked) { |
|
411 |
checkTablesOnly(); |
|
412 |
} |
|
413 |
||
414 |
iis.mark(); |
|
415 |
||
416 |
gotoImage(0); |
|
417 |
||
418 |
JPEGBuffer buffer = new JPEGBuffer(iis); |
|
419 |
buffer.loadBuf(0); |
|
420 |
||
421 |
boolean done = false; |
|
422 |
while (!done) { |
|
423 |
done = buffer.scanForFF(this); |
|
424 |
switch (buffer.buf[buffer.bufPtr] & 0xff) { |
|
425 |
case JPEG.SOI: |
|
426 |
numImages++; |
|
427 |
// FALL THROUGH to decrement buffer vars |
|
428 |
// This first set doesn't have a length |
|
429 |
case 0: // not a marker, just a data 0xff |
|
430 |
case JPEG.RST0: |
|
431 |
case JPEG.RST1: |
|
432 |
case JPEG.RST2: |
|
433 |
case JPEG.RST3: |
|
434 |
case JPEG.RST4: |
|
435 |
case JPEG.RST5: |
|
436 |
case JPEG.RST6: |
|
437 |
case JPEG.RST7: |
|
438 |
case JPEG.EOI: |
|
439 |
buffer.bufAvail--; |
|
440 |
buffer.bufPtr++; |
|
441 |
break; |
|
442 |
// All the others have a length |
|
443 |
default: |
|
444 |
buffer.bufAvail--; |
|
445 |
buffer.bufPtr++; |
|
446 |
buffer.loadBuf(2); |
|
447 |
int length = ((buffer.buf[buffer.bufPtr++] & 0xff) << 8) | |
|
448 |
(buffer.buf[buffer.bufPtr++] & 0xff); |
|
449 |
buffer.bufAvail -= 2; |
|
450 |
length -= 2; // length includes itself |
|
451 |
buffer.skipData(length); |
|
452 |
} |
|
453 |
} |
|
454 |
||
455 |
||
456 |
iis.reset(); |
|
457 |
||
458 |
return numImages; |
|
459 |
} |
|
460 |
||
461 |
return -1; // Search is necessary for JPEG |
|
462 |
} |
|
463 |
||
464 |
/** |
|
465 |
* Sets the input stream to the start of the requested image. |
|
466 |
* <pre> |
|
467 |
* @exception IllegalStateException if the input source has not been |
|
468 |
* set. |
|
469 |
* @exception IndexOutOfBoundsException if the supplied index is |
|
470 |
* out of bounds. |
|
471 |
* </pre> |
|
472 |
*/ |
|
473 |
private void gotoImage(int imageIndex) throws IOException { |
|
474 |
if (iis == null) { |
|
475 |
throw new IllegalStateException("Input not set"); |
|
476 |
} |
|
477 |
if (imageIndex < minIndex) { |
|
478 |
throw new IndexOutOfBoundsException(); |
|
479 |
} |
|
480 |
if (!tablesOnlyChecked) { |
|
481 |
checkTablesOnly(); |
|
482 |
} |
|
483 |
if (imageIndex < imagePositions.size()) { |
|
484 |
iis.seek(((Long)(imagePositions.get(imageIndex))).longValue()); |
|
485 |
} else { |
|
486 |
// read to start of image, saving positions |
|
487 |
// First seek to the last position we already have, and skip the |
|
488 |
// entire image |
|
489 |
Long pos = (Long) imagePositions.get(imagePositions.size()-1); |
|
490 |
iis.seek(pos.longValue()); |
|
491 |
skipImage(); |
|
492 |
// Now add all intervening positions, skipping images |
|
493 |
for (int index = imagePositions.size(); |
|
494 |
index <= imageIndex; |
|
495 |
index++) { |
|
496 |
// Is there an image? |
|
497 |
if (!hasNextImage()) { |
|
498 |
throw new IndexOutOfBoundsException(); |
|
499 |
} |
|
500 |
pos = new Long(iis.getStreamPosition()); |
|
501 |
imagePositions.add(pos); |
|
502 |
if (seekForwardOnly) { |
|
503 |
iis.flushBefore(pos.longValue()); |
|
504 |
} |
|
505 |
if (index < imageIndex) { |
|
506 |
skipImage(); |
|
507 |
} // Otherwise we are where we want to be |
|
508 |
} |
|
509 |
} |
|
510 |
||
511 |
if (seekForwardOnly) { |
|
512 |
minIndex = imageIndex; |
|
513 |
} |
|
514 |
||
515 |
haveSeeked = true; // No way is native buffer still valid |
|
516 |
} |
|
517 |
||
518 |
/** |
|
519 |
* Skip over a complete image in the stream, leaving the stream |
|
520 |
* positioned such that the next byte to be read is the first |
|
521 |
* byte of the next image. For JPEG, this means that we read |
|
522 |
* until we encounter an EOI marker or until the end of the stream. |
|
523 |
* If the stream ends before an EOI marker is encountered, an |
|
524 |
* IndexOutOfBoundsException is thrown. |
|
525 |
*/ |
|
526 |
private void skipImage() throws IOException { |
|
527 |
if (debug) { |
|
528 |
System.out.println("skipImage called"); |
|
529 |
} |
|
530 |
boolean foundFF = false; |
|
531 |
for (int byteval = iis.read(); |
|
532 |
byteval != -1; |
|
533 |
byteval = iis.read()) { |
|
534 |
||
535 |
if (foundFF == true) { |
|
536 |
if (byteval == JPEG.EOI) { |
|
537 |
return; |
|
538 |
} |
|
539 |
} |
|
540 |
foundFF = (byteval == 0xff) ? true : false; |
|
541 |
} |
|
542 |
throw new IndexOutOfBoundsException(); |
|
543 |
} |
|
544 |
||
545 |
/** |
|
546 |
* Returns <code>true</code> if there is an image beyond |
|
547 |
* the current stream position. Does not disturb the |
|
548 |
* stream position. |
|
549 |
*/ |
|
550 |
private boolean hasNextImage() throws IOException { |
|
551 |
if (debug) { |
|
552 |
System.out.print("hasNextImage called; returning "); |
|
553 |
} |
|
554 |
iis.mark(); |
|
555 |
boolean foundFF = false; |
|
556 |
for (int byteval = iis.read(); |
|
557 |
byteval != -1; |
|
558 |
byteval = iis.read()) { |
|
559 |
||
560 |
if (foundFF == true) { |
|
561 |
if (byteval == JPEG.SOI) { |
|
562 |
iis.reset(); |
|
563 |
if (debug) { |
|
564 |
System.out.println("true"); |
|
565 |
} |
|
566 |
return true; |
|
567 |
} |
|
568 |
} |
|
569 |
foundFF = (byteval == 0xff) ? true : false; |
|
570 |
} |
|
571 |
// We hit the end of the stream before we hit an SOI, so no image |
|
572 |
iis.reset(); |
|
573 |
if (debug) { |
|
574 |
System.out.println("false"); |
|
575 |
} |
|
576 |
return false; |
|
577 |
} |
|
578 |
||
579 |
/** |
|
580 |
* Push back the given number of bytes to the input stream. |
|
581 |
* Called by the native code at the end of each image so |
|
582 |
* that the next one can be identified from Java. |
|
583 |
*/ |
|
584 |
private void pushBack(int num) throws IOException { |
|
585 |
if (debug) { |
|
586 |
System.out.println("pushing back " + num + " bytes"); |
|
587 |
} |
|
16881 | 588 |
cbLock.lock(); |
589 |
try { |
|
590 |
iis.seek(iis.getStreamPosition()-num); |
|
591 |
// The buffer is clear after this, so no need to set haveSeeked. |
|
592 |
} finally { |
|
593 |
cbLock.unlock(); |
|
594 |
} |
|
2 | 595 |
} |
596 |
||
597 |
/** |
|
598 |
* Reads header information for the given image, if possible. |
|
599 |
*/ |
|
600 |
private void readHeader(int imageIndex, boolean reset) |
|
601 |
throws IOException { |
|
602 |
gotoImage(imageIndex); |
|
603 |
readNativeHeader(reset); // Ignore return |
|
604 |
currentImage = imageIndex; |
|
605 |
} |
|
606 |
||
607 |
private boolean readNativeHeader(boolean reset) throws IOException { |
|
608 |
boolean retval = false; |
|
609 |
retval = readImageHeader(structPointer, haveSeeked, reset); |
|
610 |
haveSeeked = false; |
|
611 |
return retval; |
|
612 |
} |
|
613 |
||
614 |
/** |
|
615 |
* Read in the header information starting from the current |
|
616 |
* stream position, returning <code>true</code> if the |
|
617 |
* header was a tables-only image. After this call, the |
|
618 |
* native IJG decompression struct will contain the image |
|
619 |
* information required by most query calls below |
|
620 |
* (e.g. getWidth, getHeight, etc.), if the header was not |
|
621 |
* a tables-only image. |
|
622 |
* If reset is <code>true</code>, the state of the IJG |
|
623 |
* object is reset so that it can read a header again. |
|
624 |
* This happens automatically if the header was a tables-only |
|
625 |
* image. |
|
626 |
*/ |
|
627 |
private native boolean readImageHeader(long structPointer, |
|
628 |
boolean clearBuffer, |
|
629 |
boolean reset) |
|
630 |
throws IOException; |
|
631 |
||
632 |
/* |
|
633 |
* Called by the native code whenever an image header has been |
|
634 |
* read. Whether we read metadata or not, we always need this |
|
635 |
* information, so it is passed back independently of |
|
636 |
* metadata, which may never be read. |
|
637 |
*/ |
|
638 |
private void setImageData(int width, |
|
639 |
int height, |
|
640 |
int colorSpaceCode, |
|
641 |
int outColorSpaceCode, |
|
642 |
int numComponents, |
|
643 |
byte [] iccData) { |
|
644 |
this.width = width; |
|
645 |
this.height = height; |
|
646 |
this.colorSpaceCode = colorSpaceCode; |
|
647 |
this.outColorSpaceCode = outColorSpaceCode; |
|
648 |
this.numComponents = numComponents; |
|
649 |
||
650 |
if (iccData == null) { |
|
651 |
iccCS = null; |
|
652 |
return; |
|
653 |
} |
|
654 |
||
655 |
ICC_Profile newProfile = null; |
|
656 |
try { |
|
657 |
newProfile = ICC_Profile.getInstance(iccData); |
|
658 |
} catch (IllegalArgumentException e) { |
|
659 |
/* |
|
660 |
* Color profile data seems to be invalid. |
|
661 |
* Ignore this profile. |
|
662 |
*/ |
|
663 |
iccCS = null; |
|
664 |
warningOccurred(WARNING_IGNORE_INVALID_ICC); |
|
665 |
||
666 |
return; |
|
667 |
} |
|
668 |
byte[] newData = newProfile.getData(); |
|
669 |
||
670 |
ICC_Profile oldProfile = null; |
|
671 |
if (iccCS instanceof ICC_ColorSpace) { |
|
672 |
oldProfile = ((ICC_ColorSpace)iccCS).getProfile(); |
|
673 |
} |
|
674 |
byte[] oldData = null; |
|
675 |
if (oldProfile != null) { |
|
676 |
oldData = oldProfile.getData(); |
|
677 |
} |
|
678 |
||
679 |
/* |
|
680 |
* At the moment we can't rely on the ColorSpace.equals() |
|
681 |
* and ICC_Profile.equals() because they do not detect |
|
682 |
* the case when two profiles are created from same data. |
|
683 |
* |
|
684 |
* So, we have to do data comparison in order to avoid |
|
685 |
* creation of different ColorSpace instances for the same |
|
686 |
* embedded data. |
|
687 |
*/ |
|
688 |
if (oldData == null || |
|
689 |
!java.util.Arrays.equals(oldData, newData)) |
|
690 |
{ |
|
691 |
iccCS = new ICC_ColorSpace(newProfile); |
|
3013 | 692 |
// verify new color space |
693 |
try { |
|
694 |
float[] colors = iccCS.fromRGB(new float[] {1f, 0f, 0f}); |
|
695 |
} catch (CMMException e) { |
|
696 |
/* |
|
697 |
* Embedded profile seems to be corrupted. |
|
698 |
* Ignore this profile. |
|
699 |
*/ |
|
700 |
iccCS = null; |
|
16881 | 701 |
cbLock.lock(); |
702 |
try { |
|
703 |
warningOccurred(WARNING_IGNORE_INVALID_ICC); |
|
704 |
} finally { |
|
705 |
cbLock.unlock(); |
|
706 |
} |
|
3013 | 707 |
} |
2 | 708 |
} |
709 |
} |
|
710 |
||
711 |
public int getWidth(int imageIndex) throws IOException { |
|
712 |
setThreadLock(); |
|
713 |
try { |
|
714 |
if (currentImage != imageIndex) { |
|
16881 | 715 |
cbLock.check(); |
2 | 716 |
readHeader(imageIndex, true); |
717 |
} |
|
718 |
return width; |
|
719 |
} finally { |
|
720 |
clearThreadLock(); |
|
721 |
} |
|
722 |
} |
|
723 |
||
724 |
public int getHeight(int imageIndex) throws IOException { |
|
725 |
setThreadLock(); |
|
726 |
try { |
|
727 |
if (currentImage != imageIndex) { |
|
16881 | 728 |
cbLock.check(); |
2 | 729 |
readHeader(imageIndex, true); |
730 |
} |
|
731 |
return height; |
|
732 |
} finally { |
|
733 |
clearThreadLock(); |
|
734 |
} |
|
735 |
} |
|
736 |
||
737 |
/////////// Color Conversion and Image Types |
|
738 |
||
739 |
/** |
|
740 |
* Return an ImageTypeSpecifier corresponding to the given |
|
741 |
* color space code, or null if the color space is unsupported. |
|
742 |
*/ |
|
3009
de653b2cab31
4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents:
2381
diff
changeset
|
743 |
private ImageTypeProducer getImageType(int code) { |
de653b2cab31
4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents:
2381
diff
changeset
|
744 |
ImageTypeProducer ret = null; |
2 | 745 |
|
746 |
if ((code > 0) && (code < JPEG.NUM_JCS_CODES)) { |
|
3009
de653b2cab31
4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents:
2381
diff
changeset
|
747 |
ret = ImageTypeProducer.getTypeProducer(code); |
2 | 748 |
} |
749 |
return ret; |
|
750 |
} |
|
751 |
||
752 |
public ImageTypeSpecifier getRawImageType(int imageIndex) |
|
753 |
throws IOException { |
|
754 |
setThreadLock(); |
|
755 |
try { |
|
756 |
if (currentImage != imageIndex) { |
|
16881 | 757 |
cbLock.check(); |
758 |
||
2 | 759 |
readHeader(imageIndex, true); |
760 |
} |
|
761 |
||
762 |
// Returns null if it can't be represented |
|
3009
de653b2cab31
4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents:
2381
diff
changeset
|
763 |
return getImageType(colorSpaceCode).getType(); |
2 | 764 |
} finally { |
765 |
clearThreadLock(); |
|
766 |
} |
|
767 |
} |
|
768 |
||
769 |
public Iterator getImageTypes(int imageIndex) |
|
770 |
throws IOException { |
|
771 |
setThreadLock(); |
|
772 |
try { |
|
773 |
return getImageTypesOnThread(imageIndex); |
|
774 |
} finally { |
|
775 |
clearThreadLock(); |
|
776 |
} |
|
777 |
} |
|
778 |
||
779 |
private Iterator getImageTypesOnThread(int imageIndex) |
|
780 |
throws IOException { |
|
781 |
if (currentImage != imageIndex) { |
|
16881 | 782 |
cbLock.check(); |
2 | 783 |
readHeader(imageIndex, true); |
784 |
} |
|
785 |
||
786 |
// We return an iterator containing the default, any |
|
787 |
// conversions that the library provides, and |
|
788 |
// all the other default types with the same number |
|
789 |
// of components, as we can do these as a post-process. |
|
790 |
// As we convert Rasters rather than images, images |
|
791 |
// with alpha cannot be converted in a post-process. |
|
792 |
||
793 |
// If this image can't be interpreted, this method |
|
794 |
// returns an empty Iterator. |
|
795 |
||
796 |
// Get the raw ITS, if there is one. Note that this |
|
797 |
// won't always be the same as the default. |
|
3009
de653b2cab31
4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents:
2381
diff
changeset
|
798 |
ImageTypeProducer raw = getImageType(colorSpaceCode); |
2 | 799 |
|
800 |
// Given the encoded colorspace, build a list of ITS's |
|
801 |
// representing outputs you could handle starting |
|
802 |
// with the default. |
|
803 |
||
3009
de653b2cab31
4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents:
2381
diff
changeset
|
804 |
ArrayList<ImageTypeProducer> list = new ArrayList<ImageTypeProducer>(1); |
2 | 805 |
|
806 |
switch (colorSpaceCode) { |
|
807 |
case JPEG.JCS_GRAYSCALE: |
|
808 |
list.add(raw); |
|
809 |
list.add(getImageType(JPEG.JCS_RGB)); |
|
810 |
break; |
|
811 |
case JPEG.JCS_RGB: |
|
812 |
list.add(raw); |
|
813 |
list.add(getImageType(JPEG.JCS_GRAYSCALE)); |
|
3009
de653b2cab31
4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents:
2381
diff
changeset
|
814 |
list.add(getImageType(JPEG.JCS_YCC)); |
2 | 815 |
break; |
816 |
case JPEG.JCS_RGBA: |
|
817 |
list.add(raw); |
|
818 |
break; |
|
819 |
case JPEG.JCS_YCC: |
|
820 |
if (raw != null) { // Might be null if PYCC.pf not installed |
|
821 |
list.add(raw); |
|
822 |
list.add(getImageType(JPEG.JCS_RGB)); |
|
823 |
} |
|
824 |
break; |
|
825 |
case JPEG.JCS_YCCA: |
|
826 |
if (raw != null) { // Might be null if PYCC.pf not installed |
|
827 |
list.add(raw); |
|
828 |
} |
|
829 |
break; |
|
830 |
case JPEG.JCS_YCbCr: |
|
831 |
// As there is no YCbCr ColorSpace, we can't support |
|
832 |
// the raw type. |
|
833 |
||
834 |
// due to 4705399, use RGB as default in order to avoid |
|
835 |
// slowing down of drawing operations with result image. |
|
836 |
list.add(getImageType(JPEG.JCS_RGB)); |
|
837 |
||
838 |
if (iccCS != null) { |
|
3009
de653b2cab31
4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents:
2381
diff
changeset
|
839 |
list.add(new ImageTypeProducer() { |
de653b2cab31
4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents:
2381
diff
changeset
|
840 |
protected ImageTypeSpecifier produce() { |
de653b2cab31
4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents:
2381
diff
changeset
|
841 |
return ImageTypeSpecifier.createInterleaved |
2 | 842 |
(iccCS, |
843 |
JPEG.bOffsRGB, // Assume it's for RGB |
|
844 |
DataBuffer.TYPE_BYTE, |
|
845 |
false, |
|
3009
de653b2cab31
4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents:
2381
diff
changeset
|
846 |
false); |
de653b2cab31
4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents:
2381
diff
changeset
|
847 |
} |
de653b2cab31
4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents:
2381
diff
changeset
|
848 |
}); |
2 | 849 |
|
850 |
} |
|
851 |
||
852 |
list.add(getImageType(JPEG.JCS_GRAYSCALE)); |
|
3009
de653b2cab31
4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents:
2381
diff
changeset
|
853 |
list.add(getImageType(JPEG.JCS_YCC)); |
2 | 854 |
break; |
855 |
case JPEG.JCS_YCbCrA: // Default is to convert to RGBA |
|
856 |
// As there is no YCbCr ColorSpace, we can't support |
|
857 |
// the raw type. |
|
858 |
list.add(getImageType(JPEG.JCS_RGBA)); |
|
859 |
break; |
|
860 |
} |
|
861 |
||
3009
de653b2cab31
4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents:
2381
diff
changeset
|
862 |
return new ImageTypeIterator(list.iterator()); |
2 | 863 |
} |
864 |
||
865 |
/** |
|
866 |
* Checks the implied color conversion between the stream and |
|
867 |
* the target image, altering the IJG output color space if necessary. |
|
868 |
* If a java color conversion is required, then this sets up |
|
869 |
* <code>convert</code>. |
|
870 |
* If bands are being rearranged at all (either source or destination |
|
871 |
* bands are specified in the param), then the default color |
|
872 |
* conversions are assumed to be correct. |
|
873 |
* Throws an IIOException if there is no conversion available. |
|
874 |
*/ |
|
875 |
private void checkColorConversion(BufferedImage image, |
|
876 |
ImageReadParam param) |
|
877 |
throws IIOException { |
|
878 |
||
879 |
// If we are rearranging channels at all, the default |
|
880 |
// conversions remain in place. If the user wants |
|
881 |
// raw channels then he should do this while reading |
|
882 |
// a Raster. |
|
883 |
if (param != null) { |
|
884 |
if ((param.getSourceBands() != null) || |
|
885 |
(param.getDestinationBands() != null)) { |
|
886 |
// Accept default conversions out of decoder, silently |
|
887 |
return; |
|
888 |
} |
|
889 |
} |
|
890 |
||
891 |
// XXX - We do not currently support any indexed color models, |
|
892 |
// though we could, as IJG will quantize for us. |
|
893 |
// This is a performance and memory-use issue, as |
|
894 |
// users can read RGB and then convert to indexed in Java. |
|
895 |
||
896 |
ColorModel cm = image.getColorModel(); |
|
897 |
||
898 |
if (cm instanceof IndexColorModel) { |
|
899 |
throw new IIOException("IndexColorModel not supported"); |
|
900 |
} |
|
901 |
||
902 |
// Now check the ColorSpace type against outColorSpaceCode |
|
903 |
// We may want to tweak the default |
|
904 |
ColorSpace cs = cm.getColorSpace(); |
|
905 |
int csType = cs.getType(); |
|
906 |
convert = null; |
|
907 |
switch (outColorSpaceCode) { |
|
908 |
case JPEG.JCS_GRAYSCALE: // Its gray in the file |
|
909 |
if (csType == ColorSpace.TYPE_RGB) { // We want RGB |
|
910 |
// IJG can do this for us more efficiently |
|
911 |
setOutColorSpace(structPointer, JPEG.JCS_RGB); |
|
3009
de653b2cab31
4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents:
2381
diff
changeset
|
912 |
// Update java state according to changes |
de653b2cab31
4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents:
2381
diff
changeset
|
913 |
// in the native part of decoder. |
de653b2cab31
4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents:
2381
diff
changeset
|
914 |
outColorSpaceCode = JPEG.JCS_RGB; |
de653b2cab31
4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents:
2381
diff
changeset
|
915 |
numComponents = 3; |
2 | 916 |
} else if (csType != ColorSpace.TYPE_GRAY) { |
917 |
throw new IIOException("Incompatible color conversion"); |
|
918 |
} |
|
919 |
break; |
|
920 |
case JPEG.JCS_RGB: // IJG wants to go to RGB |
|
921 |
if (csType == ColorSpace.TYPE_GRAY) { // We want gray |
|
922 |
if (colorSpaceCode == JPEG.JCS_YCbCr) { |
|
923 |
// If the jpeg space is YCbCr, IJG can do it |
|
924 |
setOutColorSpace(structPointer, JPEG.JCS_GRAYSCALE); |
|
3009
de653b2cab31
4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents:
2381
diff
changeset
|
925 |
// Update java state according to changes |
de653b2cab31
4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents:
2381
diff
changeset
|
926 |
// in the native part of decoder. |
de653b2cab31
4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents:
2381
diff
changeset
|
927 |
outColorSpaceCode = JPEG.JCS_GRAYSCALE; |
de653b2cab31
4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents:
2381
diff
changeset
|
928 |
numComponents = 1; |
2 | 929 |
} |
930 |
} else if ((iccCS != null) && |
|
931 |
(cm.getNumComponents() == numComponents) && |
|
932 |
(cs != iccCS)) { |
|
933 |
// We have an ICC profile but it isn't used in the dest |
|
934 |
// image. So convert from the profile cs to the target cs |
|
935 |
convert = new ColorConvertOp(iccCS, cs, null); |
|
936 |
// Leave IJG conversion in place; we still need it |
|
937 |
} else if ((iccCS == null) && |
|
938 |
(!cs.isCS_sRGB()) && |
|
939 |
(cm.getNumComponents() == numComponents)) { |
|
940 |
// Target isn't sRGB, so convert from sRGB to the target |
|
2381
0afc5d868c46
6631559: Registration of ImageIO plugins should not cause loading of jpeg.dlli and cmm.dll
bae
parents:
2
diff
changeset
|
941 |
convert = new ColorConvertOp(JPEG.JCS.sRGB, cs, null); |
2 | 942 |
} else if (csType != ColorSpace.TYPE_RGB) { |
943 |
throw new IIOException("Incompatible color conversion"); |
|
944 |
} |
|
945 |
break; |
|
946 |
case JPEG.JCS_RGBA: |
|
947 |
// No conversions available; image must be RGBA |
|
948 |
if ((csType != ColorSpace.TYPE_RGB) || |
|
949 |
(cm.getNumComponents() != numComponents)) { |
|
950 |
throw new IIOException("Incompatible color conversion"); |
|
951 |
} |
|
952 |
break; |
|
953 |
case JPEG.JCS_YCC: |
|
3009
de653b2cab31
4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents:
2381
diff
changeset
|
954 |
{ |
de653b2cab31
4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents:
2381
diff
changeset
|
955 |
ColorSpace YCC = JPEG.JCS.getYCC(); |
de653b2cab31
4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents:
2381
diff
changeset
|
956 |
if (YCC == null) { // We can't do YCC at all |
de653b2cab31
4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents:
2381
diff
changeset
|
957 |
throw new IIOException("Incompatible color conversion"); |
de653b2cab31
4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents:
2381
diff
changeset
|
958 |
} |
de653b2cab31
4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents:
2381
diff
changeset
|
959 |
if ((cs != YCC) && |
de653b2cab31
4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents:
2381
diff
changeset
|
960 |
(cm.getNumComponents() == numComponents)) { |
de653b2cab31
4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents:
2381
diff
changeset
|
961 |
convert = new ColorConvertOp(YCC, cs, null); |
de653b2cab31
4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents:
2381
diff
changeset
|
962 |
} |
2 | 963 |
} |
964 |
break; |
|
965 |
case JPEG.JCS_YCCA: |
|
3009
de653b2cab31
4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents:
2381
diff
changeset
|
966 |
{ |
de653b2cab31
4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents:
2381
diff
changeset
|
967 |
ColorSpace YCC = JPEG.JCS.getYCC(); |
de653b2cab31
4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents:
2381
diff
changeset
|
968 |
// No conversions available; image must be YCCA |
de653b2cab31
4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents:
2381
diff
changeset
|
969 |
if ((YCC == null) || // We can't do YCC at all |
de653b2cab31
4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents:
2381
diff
changeset
|
970 |
(cs != YCC) || |
de653b2cab31
4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents:
2381
diff
changeset
|
971 |
(cm.getNumComponents() != numComponents)) { |
de653b2cab31
4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents:
2381
diff
changeset
|
972 |
throw new IIOException("Incompatible color conversion"); |
de653b2cab31
4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents:
2381
diff
changeset
|
973 |
} |
2 | 974 |
} |
975 |
break; |
|
976 |
default: |
|
977 |
// Anything else we can't handle at all |
|
978 |
throw new IIOException("Incompatible color conversion"); |
|
979 |
} |
|
980 |
} |
|
981 |
||
982 |
/** |
|
983 |
* Set the IJG output space to the given value. The library will |
|
984 |
* perform the appropriate colorspace conversions. |
|
985 |
*/ |
|
986 |
private native void setOutColorSpace(long structPointer, int id); |
|
987 |
||
988 |
/////// End of Color Conversion & Image Types |
|
989 |
||
990 |
public ImageReadParam getDefaultReadParam() { |
|
991 |
return new JPEGImageReadParam(); |
|
992 |
} |
|
993 |
||
994 |
public IIOMetadata getStreamMetadata() throws IOException { |
|
995 |
setThreadLock(); |
|
996 |
try { |
|
997 |
if (!tablesOnlyChecked) { |
|
16881 | 998 |
cbLock.check(); |
2 | 999 |
checkTablesOnly(); |
1000 |
} |
|
1001 |
return streamMetadata; |
|
1002 |
} finally { |
|
1003 |
clearThreadLock(); |
|
1004 |
} |
|
1005 |
} |
|
1006 |
||
1007 |
public IIOMetadata getImageMetadata(int imageIndex) |
|
1008 |
throws IOException { |
|
1009 |
setThreadLock(); |
|
1010 |
try { |
|
1011 |
// imageMetadataIndex will always be either a valid index or |
|
1012 |
// -1, in which case imageMetadata will not be null. |
|
1013 |
// So we can leave checking imageIndex for gotoImage. |
|
1014 |
if ((imageMetadataIndex == imageIndex) |
|
1015 |
&& (imageMetadata != null)) { |
|
1016 |
return imageMetadata; |
|
1017 |
} |
|
1018 |
||
16881 | 1019 |
cbLock.check(); |
1020 |
||
2 | 1021 |
gotoImage(imageIndex); |
1022 |
||
1023 |
imageMetadata = new JPEGMetadata(false, false, iis, this); |
|
1024 |
||
1025 |
imageMetadataIndex = imageIndex; |
|
1026 |
||
1027 |
return imageMetadata; |
|
1028 |
} finally { |
|
1029 |
clearThreadLock(); |
|
1030 |
} |
|
1031 |
} |
|
1032 |
||
1033 |
public BufferedImage read(int imageIndex, ImageReadParam param) |
|
1034 |
throws IOException { |
|
1035 |
setThreadLock(); |
|
1036 |
try { |
|
16881 | 1037 |
cbLock.check(); |
2 | 1038 |
try { |
1039 |
readInternal(imageIndex, param, false); |
|
1040 |
} catch (RuntimeException e) { |
|
1041 |
resetLibraryState(structPointer); |
|
1042 |
throw e; |
|
1043 |
} catch (IOException e) { |
|
1044 |
resetLibraryState(structPointer); |
|
1045 |
throw e; |
|
1046 |
} |
|
1047 |
||
1048 |
BufferedImage ret = image; |
|
1049 |
image = null; // don't keep a reference here |
|
1050 |
return ret; |
|
1051 |
} finally { |
|
1052 |
clearThreadLock(); |
|
1053 |
} |
|
1054 |
} |
|
1055 |
||
1056 |
private Raster readInternal(int imageIndex, |
|
1057 |
ImageReadParam param, |
|
1058 |
boolean wantRaster) throws IOException { |
|
1059 |
readHeader(imageIndex, false); |
|
1060 |
||
1061 |
WritableRaster imRas = null; |
|
1062 |
int numImageBands = 0; |
|
1063 |
||
1064 |
if (!wantRaster){ |
|
1065 |
// Can we read this image? |
|
1066 |
Iterator imageTypes = getImageTypes(imageIndex); |
|
1067 |
if (imageTypes.hasNext() == false) { |
|
1068 |
throw new IIOException("Unsupported Image Type"); |
|
1069 |
} |
|
1070 |
||
1071 |
image = getDestination(param, imageTypes, width, height); |
|
1072 |
imRas = image.getRaster(); |
|
1073 |
||
1074 |
// The destination may still be incompatible. |
|
1075 |
||
1076 |
numImageBands = image.getSampleModel().getNumBands(); |
|
1077 |
||
1078 |
// Check whether we can handle any implied color conversion |
|
1079 |
||
1080 |
// Throws IIOException if the stream and the image are |
|
1081 |
// incompatible, and sets convert if a java conversion |
|
1082 |
// is necessary |
|
1083 |
checkColorConversion(image, param); |
|
1084 |
||
1085 |
// Check the source and destination bands in the param |
|
1086 |
checkReadParamBandSettings(param, numComponents, numImageBands); |
|
1087 |
} else { |
|
1088 |
// Set the output color space equal to the input colorspace |
|
1089 |
// This disables all conversions |
|
1090 |
setOutColorSpace(structPointer, colorSpaceCode); |
|
1091 |
image = null; |
|
1092 |
} |
|
1093 |
||
1094 |
// Create an intermediate 1-line Raster that will hold the decoded, |
|
1095 |
// subsampled, clipped, band-selected image data in a single |
|
1096 |
// byte-interleaved buffer. The above transformations |
|
1097 |
// will occur in C for performance. Every time this Raster |
|
1098 |
// is filled we will call back to acceptPixels below to copy |
|
1099 |
// this to whatever kind of buffer our image has. |
|
1100 |
||
1101 |
int [] srcBands = JPEG.bandOffsets[numComponents-1]; |
|
1102 |
int numRasterBands = (wantRaster ? numComponents : numImageBands); |
|
1103 |
destinationBands = null; |
|
1104 |
||
1105 |
Rectangle srcROI = new Rectangle(0, 0, 0, 0); |
|
1106 |
destROI = new Rectangle(0, 0, 0, 0); |
|
1107 |
computeRegions(param, width, height, image, srcROI, destROI); |
|
1108 |
||
1109 |
int periodX = 1; |
|
1110 |
int periodY = 1; |
|
1111 |
||
1112 |
minProgressivePass = 0; |
|
1113 |
maxProgressivePass = Integer.MAX_VALUE; |
|
1114 |
||
1115 |
if (param != null) { |
|
1116 |
periodX = param.getSourceXSubsampling(); |
|
1117 |
periodY = param.getSourceYSubsampling(); |
|
1118 |
||
1119 |
int[] sBands = param.getSourceBands(); |
|
1120 |
if (sBands != null) { |
|
1121 |
srcBands = sBands; |
|
1122 |
numRasterBands = srcBands.length; |
|
1123 |
} |
|
1124 |
if (!wantRaster) { // ignore dest bands for Raster |
|
1125 |
destinationBands = param.getDestinationBands(); |
|
1126 |
} |
|
1127 |
||
1128 |
minProgressivePass = param.getSourceMinProgressivePass(); |
|
1129 |
maxProgressivePass = param.getSourceMaxProgressivePass(); |
|
1130 |
||
1131 |
if (param instanceof JPEGImageReadParam) { |
|
1132 |
JPEGImageReadParam jparam = (JPEGImageReadParam) param; |
|
1133 |
if (jparam.areTablesSet()) { |
|
1134 |
abbrevQTables = jparam.getQTables(); |
|
1135 |
abbrevDCHuffmanTables = jparam.getDCHuffmanTables(); |
|
1136 |
abbrevACHuffmanTables = jparam.getACHuffmanTables(); |
|
1137 |
} |
|
1138 |
} |
|
1139 |
} |
|
1140 |
||
1141 |
int lineSize = destROI.width*numRasterBands; |
|
1142 |
||
1143 |
buffer = new DataBufferByte(lineSize); |
|
1144 |
||
1145 |
int [] bandOffs = JPEG.bandOffsets[numRasterBands-1]; |
|
1146 |
||
1147 |
raster = Raster.createInterleavedRaster(buffer, |
|
1148 |
destROI.width, 1, |
|
1149 |
lineSize, |
|
1150 |
numRasterBands, |
|
1151 |
bandOffs, |
|
1152 |
null); |
|
1153 |
||
1154 |
// Now that we have the Raster we'll decode to, get a view of the |
|
1155 |
// target Raster that will permit a simple setRect for each scanline |
|
1156 |
if (wantRaster) { |
|
1157 |
target = Raster.createInterleavedRaster(DataBuffer.TYPE_BYTE, |
|
1158 |
destROI.width, |
|
1159 |
destROI.height, |
|
1160 |
lineSize, |
|
1161 |
numRasterBands, |
|
1162 |
bandOffs, |
|
1163 |
null); |
|
1164 |
} else { |
|
1165 |
target = imRas; |
|
1166 |
} |
|
1167 |
int [] bandSizes = target.getSampleModel().getSampleSize(); |
|
1168 |
||
1169 |
/* |
|
1170 |
* If the process is sequential, and we have restart markers, |
|
1171 |
* we could skip to the correct restart marker, if the library |
|
1172 |
* lets us. That's an optimization to investigate later. |
|
1173 |
*/ |
|
1174 |
||
1175 |
// Check for update listeners (don't call back if none) |
|
1176 |
boolean callbackUpdates = ((updateListeners != null) |
|
1177 |
|| (progressListeners != null)); |
|
1178 |
||
1179 |
// Set up progression data |
|
1180 |
initProgressData(); |
|
1181 |
// if we have a metadata object, we can count the scans |
|
1182 |
// and set knownPassCount |
|
1183 |
if (imageIndex == imageMetadataIndex) { // We have metadata |
|
1184 |
knownPassCount = 0; |
|
1185 |
for (Iterator iter = imageMetadata.markerSequence.iterator(); |
|
1186 |
iter.hasNext();) { |
|
1187 |
if (iter.next() instanceof SOSMarkerSegment) { |
|
1188 |
knownPassCount++; |
|
1189 |
} |
|
1190 |
} |
|
1191 |
} |
|
1192 |
progInterval = Math.max((target.getHeight()-1) / 20, 1); |
|
1193 |
if (knownPassCount > 0) { |
|
1194 |
progInterval *= knownPassCount; |
|
1195 |
} else if (maxProgressivePass != Integer.MAX_VALUE) { |
|
1196 |
progInterval *= (maxProgressivePass - minProgressivePass + 1); |
|
1197 |
} |
|
1198 |
||
1199 |
if (debug) { |
|
1200 |
System.out.println("**** Read Data *****"); |
|
1201 |
System.out.println("numRasterBands is " + numRasterBands); |
|
1202 |
System.out.print("srcBands:"); |
|
1203 |
for (int i = 0; i<srcBands.length;i++) |
|
1204 |
System.out.print(" " + srcBands[i]); |
|
1205 |
System.out.println(); |
|
1206 |
System.out.println("destination bands is " + destinationBands); |
|
1207 |
if (destinationBands != null) { |
|
1208 |
for (int i = 0; i < destinationBands.length; i++) { |
|
1209 |
System.out.print(" " + destinationBands[i]); |
|
1210 |
} |
|
1211 |
System.out.println(); |
|
1212 |
} |
|
1213 |
System.out.println("sourceROI is " + srcROI); |
|
1214 |
System.out.println("destROI is " + destROI); |
|
1215 |
System.out.println("periodX is " + periodX); |
|
1216 |
System.out.println("periodY is " + periodY); |
|
1217 |
System.out.println("minProgressivePass is " + minProgressivePass); |
|
1218 |
System.out.println("maxProgressivePass is " + maxProgressivePass); |
|
1219 |
System.out.println("callbackUpdates is " + callbackUpdates); |
|
1220 |
} |
|
1221 |
||
1222 |
// Finally, we are ready to read |
|
1223 |
||
1224 |
processImageStarted(currentImage); |
|
1225 |
||
1226 |
boolean aborted = false; |
|
1227 |
||
1228 |
// Note that getData disables acceleration on buffer, but it is |
|
1229 |
// just a 1-line intermediate data transfer buffer that will not |
|
1230 |
// affect the acceleration of the resulting image. |
|
1231 |
aborted = readImage(structPointer, |
|
1232 |
buffer.getData(), |
|
1233 |
numRasterBands, |
|
1234 |
srcBands, |
|
1235 |
bandSizes, |
|
1236 |
srcROI.x, srcROI.y, |
|
1237 |
srcROI.width, srcROI.height, |
|
1238 |
periodX, periodY, |
|
1239 |
abbrevQTables, |
|
1240 |
abbrevDCHuffmanTables, |
|
1241 |
abbrevACHuffmanTables, |
|
1242 |
minProgressivePass, maxProgressivePass, |
|
1243 |
callbackUpdates); |
|
1244 |
||
1245 |
if (aborted) { |
|
1246 |
processReadAborted(); |
|
1247 |
} else { |
|
1248 |
processImageComplete(); |
|
1249 |
} |
|
1250 |
||
1251 |
return target; |
|
1252 |
||
1253 |
} |
|
1254 |
||
1255 |
/** |
|
1256 |
* This method is called back from C when the intermediate Raster |
|
1257 |
* is full. The parameter indicates the scanline in the target |
|
1258 |
* Raster to which the intermediate Raster should be copied. |
|
1259 |
* After the copy, we notify update listeners. |
|
1260 |
*/ |
|
1261 |
private void acceptPixels(int y, boolean progressive) { |
|
1262 |
if (convert != null) { |
|
1263 |
convert.filter(raster, raster); |
|
1264 |
} |
|
1265 |
target.setRect(destROI.x, destROI.y + y, raster); |
|
1266 |
||
16881 | 1267 |
cbLock.lock(); |
1268 |
try { |
|
1269 |
processImageUpdate(image, |
|
1270 |
destROI.x, destROI.y+y, |
|
1271 |
raster.getWidth(), 1, |
|
1272 |
1, 1, |
|
1273 |
destinationBands); |
|
1274 |
if ((y > 0) && (y%progInterval == 0)) { |
|
1275 |
int height = target.getHeight()-1; |
|
1276 |
float percentOfPass = ((float)y)/height; |
|
1277 |
if (progressive) { |
|
1278 |
if (knownPassCount != UNKNOWN) { |
|
1279 |
processImageProgress((pass + percentOfPass)*100.0F |
|
1280 |
/ knownPassCount); |
|
1281 |
} else if (maxProgressivePass != Integer.MAX_VALUE) { |
|
1282 |
// Use the range of allowed progressive passes |
|
1283 |
processImageProgress((pass + percentOfPass)*100.0F |
|
1284 |
/ (maxProgressivePass - minProgressivePass + 1)); |
|
1285 |
} else { |
|
1286 |
// Assume there are a minimum of MIN_ESTIMATED_PASSES |
|
1287 |
// and that there is always one more pass |
|
1288 |
// Compute the percentage as the percentage at the end |
|
1289 |
// of the previous pass, plus the percentage of this |
|
1290 |
// pass scaled to be the percentage of the total remaining, |
|
1291 |
// assuming a minimum of MIN_ESTIMATED_PASSES passes and |
|
1292 |
// that there is always one more pass. This is monotonic |
|
1293 |
// and asymptotic to 1.0, which is what we need. |
|
1294 |
int remainingPasses = // including this one |
|
1295 |
Math.max(2, MIN_ESTIMATED_PASSES-pass); |
|
1296 |
int totalPasses = pass + remainingPasses-1; |
|
1297 |
progInterval = Math.max(height/20*totalPasses, |
|
1298 |
totalPasses); |
|
1299 |
if (y%progInterval == 0) { |
|
1300 |
percentToDate = previousPassPercentage + |
|
1301 |
(1.0F - previousPassPercentage) |
|
1302 |
* (percentOfPass)/remainingPasses; |
|
1303 |
if (debug) { |
|
1304 |
System.out.print("pass= " + pass); |
|
1305 |
System.out.print(", y= " + y); |
|
1306 |
System.out.print(", progInt= " + progInterval); |
|
1307 |
System.out.print(", % of pass: " + percentOfPass); |
|
1308 |
System.out.print(", rem. passes: " |
|
1309 |
+ remainingPasses); |
|
1310 |
System.out.print(", prev%: " |
|
1311 |
+ previousPassPercentage); |
|
1312 |
System.out.print(", %ToDate: " + percentToDate); |
|
1313 |
System.out.print(" "); |
|
1314 |
} |
|
1315 |
processImageProgress(percentToDate*100.0F); |
|
1316 |
} |
|
1317 |
} |
|
2 | 1318 |
} else { |
16881 | 1319 |
processImageProgress(percentOfPass * 100.0F); |
2 | 1320 |
} |
1321 |
} |
|
16881 | 1322 |
} finally { |
1323 |
cbLock.unlock(); |
|
2 | 1324 |
} |
1325 |
} |
|
1326 |
||
1327 |
private void initProgressData() { |
|
1328 |
knownPassCount = UNKNOWN; |
|
1329 |
pass = 0; |
|
1330 |
percentToDate = 0.0F; |
|
1331 |
previousPassPercentage = 0.0F; |
|
1332 |
progInterval = 0; |
|
1333 |
} |
|
1334 |
||
1335 |
private void passStarted (int pass) { |
|
16881 | 1336 |
cbLock.lock(); |
1337 |
try { |
|
1338 |
this.pass = pass; |
|
1339 |
previousPassPercentage = percentToDate; |
|
1340 |
processPassStarted(image, |
|
1341 |
pass, |
|
1342 |
minProgressivePass, |
|
1343 |
maxProgressivePass, |
|
1344 |
0, 0, |
|
1345 |
1,1, |
|
1346 |
destinationBands); |
|
1347 |
} finally { |
|
1348 |
cbLock.unlock(); |
|
1349 |
} |
|
2 | 1350 |
} |
1351 |
||
1352 |
private void passComplete () { |
|
16881 | 1353 |
cbLock.lock(); |
1354 |
try { |
|
1355 |
processPassComplete(image); |
|
1356 |
} finally { |
|
1357 |
cbLock.unlock(); |
|
1358 |
} |
|
2 | 1359 |
} |
1360 |
||
1361 |
void thumbnailStarted(int thumbnailIndex) { |
|
16881 | 1362 |
cbLock.lock(); |
1363 |
try { |
|
1364 |
processThumbnailStarted(currentImage, thumbnailIndex); |
|
1365 |
} finally { |
|
1366 |
cbLock.unlock(); |
|
1367 |
} |
|
2 | 1368 |
} |
1369 |
||
1370 |
// Provide access to protected superclass method |
|
1371 |
void thumbnailProgress(float percentageDone) { |
|
16881 | 1372 |
cbLock.lock(); |
1373 |
try { |
|
1374 |
processThumbnailProgress(percentageDone); |
|
1375 |
} finally { |
|
1376 |
cbLock.unlock(); |
|
1377 |
} |
|
2 | 1378 |
} |
1379 |
||
1380 |
// Provide access to protected superclass method |
|
1381 |
void thumbnailComplete() { |
|
16881 | 1382 |
cbLock.lock(); |
1383 |
try { |
|
1384 |
processThumbnailComplete(); |
|
1385 |
} finally { |
|
1386 |
cbLock.unlock(); |
|
1387 |
} |
|
2 | 1388 |
} |
1389 |
||
1390 |
/** |
|
1391 |
* Returns <code>true</code> if the read was aborted. |
|
1392 |
*/ |
|
1393 |
private native boolean readImage(long structPointer, |
|
1394 |
byte [] buffer, |
|
1395 |
int numRasterBands, |
|
1396 |
int [] srcBands, |
|
1397 |
int [] bandSizes, |
|
1398 |
int sourceXOffset, int sourceYOffset, |
|
1399 |
int sourceWidth, int sourceHeight, |
|
1400 |
int periodX, int periodY, |
|
1401 |
JPEGQTable [] abbrevQTables, |
|
1402 |
JPEGHuffmanTable [] abbrevDCHuffmanTables, |
|
1403 |
JPEGHuffmanTable [] abbrevACHuffmanTables, |
|
1404 |
int minProgressivePass, |
|
1405 |
int maxProgressivePass, |
|
1406 |
boolean wantUpdates); |
|
1407 |
||
1408 |
public void abort() { |
|
1409 |
setThreadLock(); |
|
1410 |
try { |
|
16881 | 1411 |
/** |
1412 |
* NB: we do not check the call back lock here, |
|
1413 |
* we allow to abort the reader any time. |
|
1414 |
*/ |
|
1415 |
||
2 | 1416 |
super.abort(); |
1417 |
abortRead(structPointer); |
|
1418 |
} finally { |
|
1419 |
clearThreadLock(); |
|
1420 |
} |
|
1421 |
} |
|
1422 |
||
1423 |
/** Set the C level abort flag. Keep it atomic for thread safety. */ |
|
1424 |
private native void abortRead(long structPointer); |
|
1425 |
||
1426 |
/** Resets library state when an exception occurred during a read. */ |
|
1427 |
private native void resetLibraryState(long structPointer); |
|
1428 |
||
1429 |
public boolean canReadRaster() { |
|
1430 |
return true; |
|
1431 |
} |
|
1432 |
||
1433 |
public Raster readRaster(int imageIndex, ImageReadParam param) |
|
1434 |
throws IOException { |
|
1435 |
setThreadLock(); |
|
1436 |
Raster retval = null; |
|
1437 |
try { |
|
16881 | 1438 |
cbLock.check(); |
2 | 1439 |
/* |
1440 |
* This could be further optimized by not resetting the dest. |
|
1441 |
* offset and creating a translated raster in readInternal() |
|
1442 |
* (see bug 4994702 for more info). |
|
1443 |
*/ |
|
1444 |
||
1445 |
// For Rasters, destination offset is logical, not physical, so |
|
1446 |
// set it to 0 before calling computeRegions, so that the destination |
|
1447 |
// region is not clipped. |
|
1448 |
Point saveDestOffset = null; |
|
1449 |
if (param != null) { |
|
1450 |
saveDestOffset = param.getDestinationOffset(); |
|
1451 |
param.setDestinationOffset(new Point(0, 0)); |
|
1452 |
} |
|
1453 |
retval = readInternal(imageIndex, param, true); |
|
1454 |
// Apply the destination offset, if any, as a logical offset |
|
1455 |
if (saveDestOffset != null) { |
|
1456 |
target = target.createWritableTranslatedChild(saveDestOffset.x, |
|
1457 |
saveDestOffset.y); |
|
1458 |
} |
|
1459 |
} catch (RuntimeException e) { |
|
1460 |
resetLibraryState(structPointer); |
|
1461 |
throw e; |
|
1462 |
} catch (IOException e) { |
|
1463 |
resetLibraryState(structPointer); |
|
1464 |
throw e; |
|
1465 |
} finally { |
|
1466 |
clearThreadLock(); |
|
1467 |
} |
|
1468 |
return retval; |
|
1469 |
} |
|
1470 |
||
1471 |
public boolean readerSupportsThumbnails() { |
|
1472 |
return true; |
|
1473 |
} |
|
1474 |
||
1475 |
public int getNumThumbnails(int imageIndex) throws IOException { |
|
1476 |
setThreadLock(); |
|
1477 |
try { |
|
16881 | 1478 |
cbLock.check(); |
1479 |
||
2 | 1480 |
getImageMetadata(imageIndex); // checks iis state for us |
1481 |
// Now check the jfif segments |
|
1482 |
JFIFMarkerSegment jfif = |
|
1483 |
(JFIFMarkerSegment) imageMetadata.findMarkerSegment |
|
1484 |
(JFIFMarkerSegment.class, true); |
|
1485 |
int retval = 0; |
|
1486 |
if (jfif != null) { |
|
1487 |
retval = (jfif.thumb == null) ? 0 : 1; |
|
1488 |
retval += jfif.extSegments.size(); |
|
1489 |
} |
|
1490 |
return retval; |
|
1491 |
} finally { |
|
1492 |
clearThreadLock(); |
|
1493 |
} |
|
1494 |
} |
|
1495 |
||
1496 |
public int getThumbnailWidth(int imageIndex, int thumbnailIndex) |
|
1497 |
throws IOException { |
|
1498 |
setThreadLock(); |
|
1499 |
try { |
|
16881 | 1500 |
cbLock.check(); |
1501 |
||
2 | 1502 |
if ((thumbnailIndex < 0) |
1503 |
|| (thumbnailIndex >= getNumThumbnails(imageIndex))) { |
|
1504 |
throw new IndexOutOfBoundsException("No such thumbnail"); |
|
1505 |
} |
|
1506 |
// Now we know that there is a jfif segment |
|
1507 |
JFIFMarkerSegment jfif = |
|
1508 |
(JFIFMarkerSegment) imageMetadata.findMarkerSegment |
|
1509 |
(JFIFMarkerSegment.class, true); |
|
1510 |
return jfif.getThumbnailWidth(thumbnailIndex); |
|
1511 |
} finally { |
|
1512 |
clearThreadLock(); |
|
1513 |
} |
|
1514 |
} |
|
1515 |
||
1516 |
public int getThumbnailHeight(int imageIndex, int thumbnailIndex) |
|
1517 |
throws IOException { |
|
1518 |
setThreadLock(); |
|
1519 |
try { |
|
16881 | 1520 |
cbLock.check(); |
1521 |
||
2 | 1522 |
if ((thumbnailIndex < 0) |
1523 |
|| (thumbnailIndex >= getNumThumbnails(imageIndex))) { |
|
1524 |
throw new IndexOutOfBoundsException("No such thumbnail"); |
|
1525 |
} |
|
1526 |
// Now we know that there is a jfif segment |
|
1527 |
JFIFMarkerSegment jfif = |
|
1528 |
(JFIFMarkerSegment) imageMetadata.findMarkerSegment |
|
1529 |
(JFIFMarkerSegment.class, true); |
|
1530 |
return jfif.getThumbnailHeight(thumbnailIndex); |
|
1531 |
} finally { |
|
1532 |
clearThreadLock(); |
|
1533 |
} |
|
1534 |
} |
|
1535 |
||
1536 |
public BufferedImage readThumbnail(int imageIndex, |
|
1537 |
int thumbnailIndex) |
|
1538 |
throws IOException { |
|
1539 |
setThreadLock(); |
|
1540 |
try { |
|
16881 | 1541 |
cbLock.check(); |
1542 |
||
2 | 1543 |
if ((thumbnailIndex < 0) |
1544 |
|| (thumbnailIndex >= getNumThumbnails(imageIndex))) { |
|
1545 |
throw new IndexOutOfBoundsException("No such thumbnail"); |
|
1546 |
} |
|
1547 |
// Now we know that there is a jfif segment and that iis is good |
|
1548 |
JFIFMarkerSegment jfif = |
|
1549 |
(JFIFMarkerSegment) imageMetadata.findMarkerSegment |
|
1550 |
(JFIFMarkerSegment.class, true); |
|
1551 |
return jfif.getThumbnail(iis, thumbnailIndex, this); |
|
1552 |
} finally { |
|
1553 |
clearThreadLock(); |
|
1554 |
} |
|
1555 |
} |
|
1556 |
||
1557 |
private void resetInternalState() { |
|
1558 |
// reset C structures |
|
1559 |
resetReader(structPointer); |
|
1560 |
||
1561 |
// reset local Java structures |
|
1562 |
numImages = 0; |
|
1563 |
imagePositions = new ArrayList(); |
|
1564 |
currentImage = -1; |
|
1565 |
image = null; |
|
1566 |
raster = null; |
|
1567 |
target = null; |
|
1568 |
buffer = null; |
|
1569 |
destROI = null; |
|
1570 |
destinationBands = null; |
|
1571 |
streamMetadata = null; |
|
1572 |
imageMetadata = null; |
|
1573 |
imageMetadataIndex = -1; |
|
1574 |
haveSeeked = false; |
|
1575 |
tablesOnlyChecked = false; |
|
1576 |
iccCS = null; |
|
1577 |
initProgressData(); |
|
1578 |
} |
|
1579 |
||
1580 |
public void reset() { |
|
1581 |
setThreadLock(); |
|
1582 |
try { |
|
16881 | 1583 |
cbLock.check(); |
2 | 1584 |
super.reset(); |
1585 |
} finally { |
|
1586 |
clearThreadLock(); |
|
1587 |
} |
|
1588 |
} |
|
1589 |
||
1590 |
private native void resetReader(long structPointer); |
|
1591 |
||
1592 |
public void dispose() { |
|
1593 |
setThreadLock(); |
|
1594 |
try { |
|
16881 | 1595 |
cbLock.check(); |
1596 |
||
2 | 1597 |
if (structPointer != 0) { |
1598 |
disposerRecord.dispose(); |
|
1599 |
structPointer = 0; |
|
1600 |
} |
|
1601 |
} finally { |
|
1602 |
clearThreadLock(); |
|
1603 |
} |
|
1604 |
} |
|
1605 |
||
1606 |
private static native void disposeReader(long structPointer); |
|
1607 |
||
1608 |
private static class JPEGReaderDisposerRecord implements DisposerRecord { |
|
1609 |
private long pData; |
|
1610 |
||
1611 |
public JPEGReaderDisposerRecord(long pData) { |
|
1612 |
this.pData = pData; |
|
1613 |
} |
|
1614 |
||
1615 |
public synchronized void dispose() { |
|
1616 |
if (pData != 0) { |
|
1617 |
disposeReader(pData); |
|
1618 |
pData = 0; |
|
1619 |
} |
|
1620 |
} |
|
1621 |
} |
|
1622 |
||
1623 |
private Thread theThread = null; |
|
1624 |
private int theLockCount = 0; |
|
1625 |
||
1626 |
private synchronized void setThreadLock() { |
|
1627 |
Thread currThread = Thread.currentThread(); |
|
1628 |
if (theThread != null) { |
|
1629 |
if (theThread != currThread) { |
|
1630 |
// it looks like that this reader instance is used |
|
1631 |
// by multiple threads. |
|
1632 |
throw new IllegalStateException("Attempt to use instance of " + |
|
1633 |
this + " locked on thread " + |
|
1634 |
theThread + " from thread " + |
|
1635 |
currThread); |
|
1636 |
} else { |
|
1637 |
theLockCount ++; |
|
1638 |
} |
|
1639 |
} else { |
|
1640 |
theThread = currThread; |
|
1641 |
theLockCount = 1; |
|
1642 |
} |
|
1643 |
} |
|
1644 |
||
1645 |
private synchronized void clearThreadLock() { |
|
1646 |
Thread currThread = Thread.currentThread(); |
|
1647 |
if (theThread == null || theThread != currThread) { |
|
1648 |
throw new IllegalStateException("Attempt to clear thread lock " + |
|
1649 |
" form wrong thread." + |
|
1650 |
" Locked thread: " + theThread + |
|
1651 |
"; current thread: " + currThread); |
|
1652 |
} |
|
1653 |
theLockCount --; |
|
1654 |
if (theLockCount == 0) { |
|
1655 |
theThread = null; |
|
1656 |
} |
|
1657 |
} |
|
16881 | 1658 |
|
1659 |
private CallBackLock cbLock = new CallBackLock(); |
|
1660 |
||
1661 |
private static class CallBackLock { |
|
1662 |
||
1663 |
private State lockState; |
|
1664 |
||
1665 |
CallBackLock() { |
|
1666 |
lockState = State.Unlocked; |
|
1667 |
} |
|
1668 |
||
1669 |
void check() { |
|
1670 |
if (lockState != State.Unlocked) { |
|
1671 |
throw new IllegalStateException("Access to the reader is not allowed"); |
|
1672 |
} |
|
1673 |
} |
|
1674 |
||
1675 |
private void lock() { |
|
1676 |
lockState = State.Locked; |
|
1677 |
} |
|
1678 |
||
1679 |
private void unlock() { |
|
1680 |
lockState = State.Unlocked; |
|
1681 |
} |
|
1682 |
||
1683 |
private static enum State { |
|
1684 |
Unlocked, |
|
1685 |
Locked |
|
1686 |
} |
|
1687 |
} |
|
2 | 1688 |
} |
3009
de653b2cab31
4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents:
2381
diff
changeset
|
1689 |
|
de653b2cab31
4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents:
2381
diff
changeset
|
1690 |
/** |
de653b2cab31
4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents:
2381
diff
changeset
|
1691 |
* An internal helper class that wraps producer's iterator |
de653b2cab31
4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents:
2381
diff
changeset
|
1692 |
* and extracts specifier instances on demand. |
de653b2cab31
4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents:
2381
diff
changeset
|
1693 |
*/ |
de653b2cab31
4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents:
2381
diff
changeset
|
1694 |
class ImageTypeIterator implements Iterator<ImageTypeSpecifier> { |
de653b2cab31
4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents:
2381
diff
changeset
|
1695 |
private Iterator<ImageTypeProducer> producers; |
de653b2cab31
4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents:
2381
diff
changeset
|
1696 |
private ImageTypeSpecifier theNext = null; |
de653b2cab31
4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents:
2381
diff
changeset
|
1697 |
|
de653b2cab31
4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents:
2381
diff
changeset
|
1698 |
public ImageTypeIterator(Iterator<ImageTypeProducer> producers) { |
de653b2cab31
4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents:
2381
diff
changeset
|
1699 |
this.producers = producers; |
de653b2cab31
4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents:
2381
diff
changeset
|
1700 |
} |
de653b2cab31
4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents:
2381
diff
changeset
|
1701 |
|
de653b2cab31
4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents:
2381
diff
changeset
|
1702 |
public boolean hasNext() { |
de653b2cab31
4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents:
2381
diff
changeset
|
1703 |
if (theNext != null) { |
de653b2cab31
4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents:
2381
diff
changeset
|
1704 |
return true; |
de653b2cab31
4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents:
2381
diff
changeset
|
1705 |
} |
de653b2cab31
4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents:
2381
diff
changeset
|
1706 |
if (!producers.hasNext()) { |
de653b2cab31
4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents:
2381
diff
changeset
|
1707 |
return false; |
de653b2cab31
4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents:
2381
diff
changeset
|
1708 |
} |
de653b2cab31
4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents:
2381
diff
changeset
|
1709 |
do { |
de653b2cab31
4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents:
2381
diff
changeset
|
1710 |
theNext = producers.next().getType(); |
de653b2cab31
4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents:
2381
diff
changeset
|
1711 |
} while (theNext == null && producers.hasNext()); |
de653b2cab31
4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents:
2381
diff
changeset
|
1712 |
|
de653b2cab31
4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents:
2381
diff
changeset
|
1713 |
return (theNext != null); |
de653b2cab31
4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents:
2381
diff
changeset
|
1714 |
} |
de653b2cab31
4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents:
2381
diff
changeset
|
1715 |
|
de653b2cab31
4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents:
2381
diff
changeset
|
1716 |
public ImageTypeSpecifier next() { |
de653b2cab31
4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents:
2381
diff
changeset
|
1717 |
if (theNext != null || hasNext()) { |
de653b2cab31
4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents:
2381
diff
changeset
|
1718 |
ImageTypeSpecifier t = theNext; |
de653b2cab31
4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents:
2381
diff
changeset
|
1719 |
theNext = null; |
de653b2cab31
4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents:
2381
diff
changeset
|
1720 |
return t; |
de653b2cab31
4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents:
2381
diff
changeset
|
1721 |
} else { |
de653b2cab31
4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents:
2381
diff
changeset
|
1722 |
throw new NoSuchElementException(); |
de653b2cab31
4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents:
2381
diff
changeset
|
1723 |
} |
de653b2cab31
4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents:
2381
diff
changeset
|
1724 |
} |
de653b2cab31
4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents:
2381
diff
changeset
|
1725 |
|
de653b2cab31
4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents:
2381
diff
changeset
|
1726 |
public void remove() { |
de653b2cab31
4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents:
2381
diff
changeset
|
1727 |
producers.remove(); |
de653b2cab31
4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents:
2381
diff
changeset
|
1728 |
} |
de653b2cab31
4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents:
2381
diff
changeset
|
1729 |
} |
de653b2cab31
4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents:
2381
diff
changeset
|
1730 |
|
de653b2cab31
4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents:
2381
diff
changeset
|
1731 |
/** |
de653b2cab31
4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents:
2381
diff
changeset
|
1732 |
* An internal helper class that provides means for deferred creation |
de653b2cab31
4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents:
2381
diff
changeset
|
1733 |
* of ImageTypeSpecifier instance required to describe available |
de653b2cab31
4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents:
2381
diff
changeset
|
1734 |
* destination types. |
de653b2cab31
4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents:
2381
diff
changeset
|
1735 |
* |
de653b2cab31
4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents:
2381
diff
changeset
|
1736 |
* This implementation only supports standard |
de653b2cab31
4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents:
2381
diff
changeset
|
1737 |
* jpeg color spaces (defined by corresponding JCS color space code). |
de653b2cab31
4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents:
2381
diff
changeset
|
1738 |
* |
de653b2cab31
4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents:
2381
diff
changeset
|
1739 |
* To support other color spaces one can override produce() method to |
de653b2cab31
4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents:
2381
diff
changeset
|
1740 |
* return custom instance of ImageTypeSpecifier. |
de653b2cab31
4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents:
2381
diff
changeset
|
1741 |
*/ |
de653b2cab31
4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents:
2381
diff
changeset
|
1742 |
class ImageTypeProducer { |
de653b2cab31
4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents:
2381
diff
changeset
|
1743 |
|
de653b2cab31
4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents:
2381
diff
changeset
|
1744 |
private ImageTypeSpecifier type = null; |
de653b2cab31
4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents:
2381
diff
changeset
|
1745 |
boolean failed = false; |
de653b2cab31
4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents:
2381
diff
changeset
|
1746 |
private int csCode; |
de653b2cab31
4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents:
2381
diff
changeset
|
1747 |
|
de653b2cab31
4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents:
2381
diff
changeset
|
1748 |
public ImageTypeProducer(int csCode) { |
de653b2cab31
4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents:
2381
diff
changeset
|
1749 |
this.csCode = csCode; |
de653b2cab31
4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents:
2381
diff
changeset
|
1750 |
} |
de653b2cab31
4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents:
2381
diff
changeset
|
1751 |
|
de653b2cab31
4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents:
2381
diff
changeset
|
1752 |
public ImageTypeProducer() { |
de653b2cab31
4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents:
2381
diff
changeset
|
1753 |
csCode = -1; // undefined |
de653b2cab31
4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents:
2381
diff
changeset
|
1754 |
} |
de653b2cab31
4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents:
2381
diff
changeset
|
1755 |
|
de653b2cab31
4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents:
2381
diff
changeset
|
1756 |
public synchronized ImageTypeSpecifier getType() { |
de653b2cab31
4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents:
2381
diff
changeset
|
1757 |
if (!failed && type == null) { |
de653b2cab31
4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents:
2381
diff
changeset
|
1758 |
try { |
de653b2cab31
4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents:
2381
diff
changeset
|
1759 |
type = produce(); |
de653b2cab31
4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents:
2381
diff
changeset
|
1760 |
} catch (Throwable e) { |
de653b2cab31
4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents:
2381
diff
changeset
|
1761 |
failed = true; |
de653b2cab31
4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents:
2381
diff
changeset
|
1762 |
} |
de653b2cab31
4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents:
2381
diff
changeset
|
1763 |
} |
de653b2cab31
4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents:
2381
diff
changeset
|
1764 |
return type; |
de653b2cab31
4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents:
2381
diff
changeset
|
1765 |
} |
de653b2cab31
4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents:
2381
diff
changeset
|
1766 |
|
de653b2cab31
4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents:
2381
diff
changeset
|
1767 |
private static final ImageTypeProducer [] defaultTypes = |
de653b2cab31
4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents:
2381
diff
changeset
|
1768 |
new ImageTypeProducer [JPEG.NUM_JCS_CODES]; |
de653b2cab31
4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents:
2381
diff
changeset
|
1769 |
|
de653b2cab31
4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents:
2381
diff
changeset
|
1770 |
public synchronized static ImageTypeProducer getTypeProducer(int csCode) { |
de653b2cab31
4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents:
2381
diff
changeset
|
1771 |
if (csCode < 0 || csCode >= JPEG.NUM_JCS_CODES) { |
de653b2cab31
4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents:
2381
diff
changeset
|
1772 |
return null; |
de653b2cab31
4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents:
2381
diff
changeset
|
1773 |
} |
de653b2cab31
4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents:
2381
diff
changeset
|
1774 |
if (defaultTypes[csCode] == null) { |
de653b2cab31
4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents:
2381
diff
changeset
|
1775 |
defaultTypes[csCode] = new ImageTypeProducer(csCode); |
de653b2cab31
4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents:
2381
diff
changeset
|
1776 |
} |
de653b2cab31
4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents:
2381
diff
changeset
|
1777 |
return defaultTypes[csCode]; |
de653b2cab31
4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents:
2381
diff
changeset
|
1778 |
} |
de653b2cab31
4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents:
2381
diff
changeset
|
1779 |
|
de653b2cab31
4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents:
2381
diff
changeset
|
1780 |
protected ImageTypeSpecifier produce() { |
de653b2cab31
4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents:
2381
diff
changeset
|
1781 |
switch (csCode) { |
de653b2cab31
4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents:
2381
diff
changeset
|
1782 |
case JPEG.JCS_GRAYSCALE: |
de653b2cab31
4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents:
2381
diff
changeset
|
1783 |
return ImageTypeSpecifier.createFromBufferedImageType |
de653b2cab31
4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents:
2381
diff
changeset
|
1784 |
(BufferedImage.TYPE_BYTE_GRAY); |
de653b2cab31
4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents:
2381
diff
changeset
|
1785 |
case JPEG.JCS_RGB: |
de653b2cab31
4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents:
2381
diff
changeset
|
1786 |
return ImageTypeSpecifier.createInterleaved(JPEG.JCS.sRGB, |
de653b2cab31
4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents:
2381
diff
changeset
|
1787 |
JPEG.bOffsRGB, |
de653b2cab31
4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents:
2381
diff
changeset
|
1788 |
DataBuffer.TYPE_BYTE, |
de653b2cab31
4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents:
2381
diff
changeset
|
1789 |
false, |
de653b2cab31
4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents:
2381
diff
changeset
|
1790 |
false); |
de653b2cab31
4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents:
2381
diff
changeset
|
1791 |
case JPEG.JCS_RGBA: |
de653b2cab31
4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents:
2381
diff
changeset
|
1792 |
return ImageTypeSpecifier.createPacked(JPEG.JCS.sRGB, |
de653b2cab31
4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents:
2381
diff
changeset
|
1793 |
0xff000000, |
de653b2cab31
4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents:
2381
diff
changeset
|
1794 |
0x00ff0000, |
de653b2cab31
4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents:
2381
diff
changeset
|
1795 |
0x0000ff00, |
de653b2cab31
4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents:
2381
diff
changeset
|
1796 |
0x000000ff, |
de653b2cab31
4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents:
2381
diff
changeset
|
1797 |
DataBuffer.TYPE_INT, |
de653b2cab31
4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents:
2381
diff
changeset
|
1798 |
false); |
de653b2cab31
4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents:
2381
diff
changeset
|
1799 |
case JPEG.JCS_YCC: |
de653b2cab31
4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents:
2381
diff
changeset
|
1800 |
if (JPEG.JCS.getYCC() != null) { |
de653b2cab31
4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents:
2381
diff
changeset
|
1801 |
return ImageTypeSpecifier.createInterleaved( |
de653b2cab31
4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents:
2381
diff
changeset
|
1802 |
JPEG.JCS.getYCC(), |
de653b2cab31
4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents:
2381
diff
changeset
|
1803 |
JPEG.bandOffsets[2], |
de653b2cab31
4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents:
2381
diff
changeset
|
1804 |
DataBuffer.TYPE_BYTE, |
de653b2cab31
4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents:
2381
diff
changeset
|
1805 |
false, |
de653b2cab31
4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents:
2381
diff
changeset
|
1806 |
false); |
de653b2cab31
4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents:
2381
diff
changeset
|
1807 |
} else { |
de653b2cab31
4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents:
2381
diff
changeset
|
1808 |
return null; |
de653b2cab31
4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents:
2381
diff
changeset
|
1809 |
} |
de653b2cab31
4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents:
2381
diff
changeset
|
1810 |
case JPEG.JCS_YCCA: |
de653b2cab31
4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents:
2381
diff
changeset
|
1811 |
if (JPEG.JCS.getYCC() != null) { |
de653b2cab31
4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents:
2381
diff
changeset
|
1812 |
return ImageTypeSpecifier.createInterleaved( |
de653b2cab31
4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents:
2381
diff
changeset
|
1813 |
JPEG.JCS.getYCC(), |
de653b2cab31
4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents:
2381
diff
changeset
|
1814 |
JPEG.bandOffsets[3], |
de653b2cab31
4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents:
2381
diff
changeset
|
1815 |
DataBuffer.TYPE_BYTE, |
de653b2cab31
4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents:
2381
diff
changeset
|
1816 |
true, |
de653b2cab31
4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents:
2381
diff
changeset
|
1817 |
false); |
de653b2cab31
4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents:
2381
diff
changeset
|
1818 |
} else { |
de653b2cab31
4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents:
2381
diff
changeset
|
1819 |
return null; |
de653b2cab31
4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents:
2381
diff
changeset
|
1820 |
} |
de653b2cab31
4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents:
2381
diff
changeset
|
1821 |
default: |
de653b2cab31
4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents:
2381
diff
changeset
|
1822 |
return null; |
de653b2cab31
4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents:
2381
diff
changeset
|
1823 |
} |
de653b2cab31
4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents:
2381
diff
changeset
|
1824 |
} |
de653b2cab31
4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents:
2381
diff
changeset
|
1825 |
} |