author | bae |
Fri, 30 Jan 2009 22:30:32 +0300 | |
changeset 2383 | c6a2226cc4de |
parent 2 | 90ce3da70b43 |
child 3009 | de653b2cab31 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
2 |
* Copyright 2000-2007 Sun Microsystems, Inc. All Rights Reserved. |
|
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 |
|
7 |
* published by the Free Software Foundation. Sun designates this |
|
8 |
* particular file as subject to the "Classpath" exception as provided |
|
9 |
* by Sun in the LICENSE file that accompanied this code. |
|
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 |
* |
|
21 |
* Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
|
22 |
* CA 95054 USA or visit www.sun.com if you need additional information or |
|
23 |
* have any questions. |
|
24 |
*/ |
|
25 |
||
26 |
/* |
|
27 |
* This file contains the code to link the Java Image I/O JPEG plug-in |
|
28 |
* to the IJG library used to read and write JPEG files. Much of it has |
|
29 |
* been copied, updated, and annotated from the jpegdecoder.c AWT JPEG |
|
30 |
* decoder. Where that code was unclear, the present author has either |
|
31 |
* rewritten the relevant section or commented it for the sake of future |
|
32 |
* maintainers. |
|
33 |
* |
|
34 |
* In particular, the way the AWT code handled progressive JPEGs seems |
|
35 |
* to me to be only accidentally correct and somewhat inefficient. The |
|
36 |
* scheme used here represents the way I think it should work. (REV 11/00) |
|
37 |
*/ |
|
38 |
||
39 |
#include <stdlib.h> |
|
40 |
#include <setjmp.h> |
|
41 |
#include <assert.h> |
|
42 |
#include <string.h> |
|
43 |
||
44 |
||
45 |
/* java native interface headers */ |
|
46 |
#include "jni.h" |
|
47 |
#include "jni_util.h" |
|
48 |
||
49 |
#include "com_sun_imageio_plugins_jpeg_JPEGImageReader.h" |
|
50 |
#include "com_sun_imageio_plugins_jpeg_JPEGImageWriter.h" |
|
51 |
||
52 |
/* headers from the JPEG library */ |
|
53 |
#include <jpeglib.h> |
|
54 |
#include "jerror.h" |
|
55 |
||
56 |
#undef MAX |
|
57 |
#define MAX(a,b) ((a) > (b) ? (a) : (b)) |
|
58 |
||
59 |
/* Cached Java method ids */ |
|
60 |
static jmethodID ImageInputStream_readID; |
|
61 |
static jmethodID ImageInputStream_skipBytesID; |
|
62 |
static jmethodID JPEGImageReader_warningOccurredID; |
|
63 |
static jmethodID JPEGImageReader_warningWithMessageID; |
|
64 |
static jmethodID JPEGImageReader_setImageDataID; |
|
65 |
static jmethodID JPEGImageReader_acceptPixelsID; |
|
66 |
static jmethodID JPEGImageReader_pushBackID; |
|
67 |
static jmethodID JPEGImageReader_passStartedID; |
|
68 |
static jmethodID JPEGImageReader_passCompleteID; |
|
69 |
static jmethodID ImageOutputStream_writeID; |
|
70 |
static jmethodID JPEGImageWriter_warningOccurredID; |
|
71 |
static jmethodID JPEGImageWriter_warningWithMessageID; |
|
72 |
static jmethodID JPEGImageWriter_writeMetadataID; |
|
73 |
static jmethodID JPEGImageWriter_grabPixelsID; |
|
74 |
static jfieldID JPEGQTable_tableID; |
|
75 |
static jfieldID JPEGHuffmanTable_lengthsID; |
|
76 |
static jfieldID JPEGHuffmanTable_valuesID; |
|
77 |
||
78 |
/* |
|
79 |
* Defined in jpegdecoder.c. Copy code from there if and |
|
80 |
* when that disappears. */ |
|
81 |
extern JavaVM *jvm; |
|
82 |
||
83 |
/* |
|
84 |
* The following sets of defines must match the warning messages in the |
|
85 |
* Java code. |
|
86 |
*/ |
|
87 |
||
88 |
/* Reader warnings */ |
|
89 |
#define READ_NO_EOI 0 |
|
90 |
||
91 |
/* Writer warnings */ |
|
92 |
||
93 |
/* Return codes for various ops */ |
|
94 |
#define OK 1 |
|
95 |
#define NOT_OK 0 |
|
96 |
||
97 |
/* |
|
98 |
* First we define two objects, one for the stream and buffer and one |
|
99 |
* for pixels. Both contain references to Java objects and pointers to |
|
100 |
* pinned arrays. These objects can be used for either input or |
|
101 |
* output. Pixels can be accessed as either INT32s or bytes. |
|
102 |
* Every I/O operation will have one of each these objects, one for |
|
103 |
* the stream and the other to hold pixels, regardless of the I/O direction. |
|
104 |
*/ |
|
105 |
||
106 |
/******************** StreamBuffer definition ************************/ |
|
107 |
||
108 |
typedef struct streamBufferStruct { |
|
109 |
jobject stream; // ImageInputStream or ImageOutputStream |
|
110 |
jbyteArray hstreamBuffer; // Handle to a Java buffer for the stream |
|
111 |
JOCTET *buf; // Pinned buffer pointer */ |
|
112 |
int bufferOffset; // holds offset between unpin and the next pin |
|
113 |
int bufferLength; // Allocated, nut just used |
|
114 |
int suspendable; // Set to true to suspend input |
|
115 |
long remaining_skip; // Used only on input |
|
116 |
} streamBuffer, *streamBufferPtr; |
|
117 |
||
118 |
/* |
|
119 |
* This buffer size was set to 64K in the old classes, 4K by default in the |
|
120 |
* IJG library, with the comment "an efficiently freadable size", and 1K |
|
121 |
* in AWT. |
|
122 |
* Unlike in the other Java designs, these objects will persist, so 64K |
|
123 |
* seems too big and 1K seems too small. If 4K was good enough for the |
|
124 |
* IJG folks, it's good enough for me. |
|
125 |
*/ |
|
126 |
#define STREAMBUF_SIZE 4096 |
|
127 |
||
128 |
/* |
|
129 |
* Used to signal that no data need be restored from an unpin to a pin. |
|
130 |
* I.e. the buffer is empty. |
|
131 |
*/ |
|
132 |
#define NO_DATA -1 |
|
133 |
||
134 |
// Forward reference |
|
135 |
static void resetStreamBuffer(JNIEnv *env, streamBufferPtr sb); |
|
136 |
||
137 |
/* |
|
138 |
* Initialize a freshly allocated StreamBuffer object. The stream is left |
|
139 |
* null, as it will be set from Java by setSource, but the buffer object |
|
140 |
* is created and a global reference kept. Returns OK on success, NOT_OK |
|
141 |
* if allocating the buffer or getting a global reference for it failed. |
|
142 |
*/ |
|
143 |
static int initStreamBuffer(JNIEnv *env, streamBufferPtr sb) { |
|
144 |
/* Initialize a new buffer */ |
|
145 |
jbyteArray hInputBuffer = (*env)->NewByteArray(env, STREAMBUF_SIZE); |
|
146 |
if (hInputBuffer == NULL) { |
|
147 |
JNU_ThrowByName( env, |
|
148 |
"java/lang/OutOfMemoryError", |
|
149 |
"Initializing Reader"); |
|
150 |
return NOT_OK; |
|
151 |
} |
|
152 |
sb->bufferLength = (*env)->GetArrayLength(env, hInputBuffer); |
|
153 |
sb->hstreamBuffer = (*env)->NewGlobalRef(env, hInputBuffer); |
|
154 |
if (sb->hstreamBuffer == NULL) { |
|
155 |
JNU_ThrowByName( env, |
|
156 |
"java/lang/OutOfMemoryError", |
|
157 |
"Initializing Reader"); |
|
158 |
return NOT_OK; |
|
159 |
} |
|
160 |
||
161 |
||
162 |
sb->stream = NULL; |
|
163 |
||
164 |
sb->buf = NULL; |
|
165 |
||
166 |
resetStreamBuffer(env, sb); |
|
167 |
||
168 |
return OK; |
|
169 |
} |
|
170 |
||
171 |
/* |
|
172 |
* Free all resources associated with this streamBuffer. This must |
|
173 |
* be called to dispose the object to avoid leaking global references, as |
|
174 |
* resetStreamBuffer does not release the buffer reference. |
|
175 |
*/ |
|
176 |
static void destroyStreamBuffer(JNIEnv *env, streamBufferPtr sb) { |
|
177 |
resetStreamBuffer(env, sb); |
|
178 |
if (sb->hstreamBuffer != NULL) { |
|
179 |
(*env)->DeleteGlobalRef(env, sb->hstreamBuffer); |
|
180 |
} |
|
181 |
} |
|
182 |
||
183 |
// Forward reference |
|
184 |
static void unpinStreamBuffer(JNIEnv *env, |
|
185 |
streamBufferPtr sb, |
|
186 |
const JOCTET *next_byte); |
|
187 |
/* |
|
188 |
* Resets the state of a streamBuffer object that has been in use. |
|
189 |
* The global reference to the stream is released, but the reference |
|
190 |
* to the buffer is retained. The buffer is unpinned if it was pinned. |
|
191 |
* All other state is reset. |
|
192 |
*/ |
|
193 |
static void resetStreamBuffer(JNIEnv *env, streamBufferPtr sb) { |
|
194 |
if (sb->stream != NULL) { |
|
195 |
(*env)->DeleteGlobalRef(env, sb->stream); |
|
196 |
sb->stream = NULL; |
|
197 |
} |
|
198 |
unpinStreamBuffer(env, sb, NULL); |
|
199 |
sb->bufferOffset = NO_DATA; |
|
200 |
sb->suspendable = FALSE; |
|
201 |
sb->remaining_skip = 0; |
|
202 |
} |
|
203 |
||
204 |
/* |
|
205 |
* Pins the data buffer associated with this stream. Returns OK on |
|
206 |
* success, NOT_OK on failure, as GetPrimitiveArrayCritical may fail. |
|
207 |
*/ |
|
208 |
static int pinStreamBuffer(JNIEnv *env, |
|
209 |
streamBufferPtr sb, |
|
210 |
const JOCTET **next_byte) { |
|
211 |
if (sb->hstreamBuffer != NULL) { |
|
212 |
assert(sb->buf == NULL); |
|
213 |
sb->buf = |
|
214 |
(JOCTET *)(*env)->GetPrimitiveArrayCritical(env, |
|
215 |
sb->hstreamBuffer, |
|
216 |
NULL); |
|
217 |
if (sb->buf == NULL) { |
|
218 |
return NOT_OK; |
|
219 |
} |
|
220 |
if (sb->bufferOffset != NO_DATA) { |
|
221 |
*next_byte = sb->buf + sb->bufferOffset; |
|
222 |
} |
|
223 |
} |
|
224 |
return OK; |
|
225 |
} |
|
226 |
||
227 |
/* |
|
228 |
* Unpins the data buffer associated with this stream. |
|
229 |
*/ |
|
230 |
static void unpinStreamBuffer(JNIEnv *env, |
|
231 |
streamBufferPtr sb, |
|
232 |
const JOCTET *next_byte) { |
|
233 |
if (sb->buf != NULL) { |
|
234 |
assert(sb->hstreamBuffer != NULL); |
|
235 |
if (next_byte == NULL) { |
|
236 |
sb->bufferOffset = NO_DATA; |
|
237 |
} else { |
|
238 |
sb->bufferOffset = next_byte - sb->buf; |
|
239 |
} |
|
240 |
(*env)->ReleasePrimitiveArrayCritical(env, |
|
241 |
sb->hstreamBuffer, |
|
242 |
sb->buf, |
|
243 |
0); |
|
244 |
sb->buf = NULL; |
|
245 |
} |
|
246 |
} |
|
247 |
||
248 |
/* |
|
249 |
* Clear out the streamBuffer. This just invalidates the data in the buffer. |
|
250 |
*/ |
|
251 |
static void clearStreamBuffer(streamBufferPtr sb) { |
|
252 |
sb->bufferOffset = NO_DATA; |
|
253 |
} |
|
254 |
||
255 |
/*************************** end StreamBuffer definition *************/ |
|
256 |
||
257 |
/*************************** Pixel Buffer definition ******************/ |
|
258 |
||
259 |
typedef struct pixelBufferStruct { |
|
260 |
jobject hpixelObject; // Usually a DataBuffer bank as a byte array |
|
261 |
union pixptr { |
|
262 |
INT32 *ip; // Pinned buffer pointer, as 32-bit ints |
|
263 |
unsigned char *bp; // Pinned buffer pointer, as bytes |
|
264 |
} buf; |
|
265 |
} pixelBuffer, *pixelBufferPtr; |
|
266 |
||
267 |
/* |
|
268 |
* Initialize a freshly allocated PixelBuffer. All fields are simply |
|
269 |
* set to NULL, as we have no idea what size buffer we will need. |
|
270 |
*/ |
|
271 |
static void initPixelBuffer(pixelBufferPtr pb) { |
|
272 |
pb->hpixelObject = NULL; |
|
273 |
pb->buf.ip = NULL; |
|
274 |
} |
|
275 |
||
276 |
/* |
|
277 |
* Set the pixelBuffer to use the given buffer, acquiring a new global |
|
278 |
* reference for it. Returns OK on success, NOT_OK on failure. |
|
279 |
*/ |
|
280 |
static int setPixelBuffer(JNIEnv *env, pixelBufferPtr pb, jobject obj) { |
|
281 |
pb->hpixelObject = (*env)->NewGlobalRef(env, obj); |
|
282 |
||
283 |
if (pb->hpixelObject == NULL) { |
|
284 |
JNU_ThrowByName( env, |
|
285 |
"java/lang/OutOfMemoryError", |
|
286 |
"Setting Pixel Buffer"); |
|
287 |
return NOT_OK; |
|
288 |
} |
|
289 |
return OK; |
|
290 |
} |
|
291 |
||
292 |
// Forward reference |
|
293 |
static void unpinPixelBuffer(JNIEnv *env, pixelBufferPtr pb); |
|
294 |
||
295 |
/* |
|
296 |
* Resets a pixel buffer to its initial state. Unpins any pixel buffer, |
|
297 |
* releases the global reference, and resets fields to NULL. Use this |
|
298 |
* method to dispose the object as well (there is no destroyPixelBuffer). |
|
299 |
*/ |
|
300 |
static void resetPixelBuffer(JNIEnv *env, pixelBufferPtr pb) { |
|
301 |
if (pb->hpixelObject != NULL) { |
|
302 |
unpinPixelBuffer(env, pb); |
|
303 |
(*env)->DeleteGlobalRef(env, pb->hpixelObject); |
|
304 |
pb->hpixelObject = NULL; |
|
305 |
} |
|
306 |
} |
|
307 |
||
308 |
/* |
|
309 |
* Pins the data buffer. Returns OK on success, NOT_OK on failure. |
|
310 |
*/ |
|
311 |
static int pinPixelBuffer(JNIEnv *env, pixelBufferPtr pb) { |
|
312 |
if (pb->hpixelObject != NULL) { |
|
313 |
assert(pb->buf.ip == NULL); |
|
314 |
pb->buf.bp = (unsigned char *)(*env)->GetPrimitiveArrayCritical |
|
315 |
(env, pb->hpixelObject, NULL); |
|
316 |
if (pb->buf.bp == NULL) { |
|
317 |
return NOT_OK; |
|
318 |
} |
|
319 |
} |
|
320 |
return OK; |
|
321 |
} |
|
322 |
||
323 |
/* |
|
324 |
* Unpins the data buffer. |
|
325 |
*/ |
|
326 |
static void unpinPixelBuffer(JNIEnv *env, pixelBufferPtr pb) { |
|
327 |
||
328 |
if (pb->buf.ip != NULL) { |
|
329 |
assert(pb->hpixelObject != NULL); |
|
330 |
(*env)->ReleasePrimitiveArrayCritical(env, |
|
331 |
pb->hpixelObject, |
|
332 |
pb->buf.ip, |
|
333 |
0); |
|
334 |
pb->buf.ip = NULL; |
|
335 |
} |
|
336 |
} |
|
337 |
||
338 |
/********************* end PixelBuffer definition *******************/ |
|
339 |
||
340 |
/********************* ImageIOData definition ***********************/ |
|
341 |
||
342 |
#define MAX_BANDS 4 |
|
343 |
#define JPEG_BAND_SIZE 8 |
|
344 |
#define NUM_BAND_VALUES (1<<JPEG_BAND_SIZE) |
|
345 |
#define MAX_JPEG_BAND_VALUE (NUM_BAND_VALUES-1) |
|
346 |
#define HALF_MAX_JPEG_BAND_VALUE (MAX_JPEG_BAND_VALUE>>1) |
|
347 |
||
348 |
/* The number of possible incoming values to be scaled. */ |
|
349 |
#define NUM_INPUT_VALUES (1 << 16) |
|
350 |
||
351 |
/* |
|
352 |
* The principal imageioData object, opaque to I/O direction. |
|
353 |
* Each JPEGImageReader will have associated with it a |
|
354 |
* jpeg_decompress_struct, and similarly each JPEGImageWriter will |
|
355 |
* have associated with it a jpeg_compress_struct. In order to |
|
356 |
* ensure that these associations persist from one native call to |
|
357 |
* the next, and to provide a central locus of imageio-specific |
|
358 |
* data, we define an imageioData struct containing references |
|
359 |
* to the Java object and the IJG structs. The functions |
|
360 |
* that manipulate these objects know whether input or output is being |
|
361 |
* performed and therefore know how to manipulate the contents correctly. |
|
362 |
* If for some reason they don't, the direction can be determined by |
|
363 |
* checking the is_decompressor field of the jpegObj. |
|
364 |
* In order for lower level code to determine a |
|
365 |
* Java object given an IJG struct, such as for dispatching warnings, |
|
366 |
* we use the client_data field of the jpeg object to store a pointer |
|
367 |
* to the imageIOData object. Maintenance of this pointer is performed |
|
368 |
* exclusively within the following access functions. If you |
|
369 |
* change that, you run the risk of dangling pointers. |
|
370 |
*/ |
|
371 |
typedef struct imageIODataStruct { |
|
372 |
j_common_ptr jpegObj; // Either struct is fine |
|
373 |
jobject imageIOobj; // A JPEGImageReader or a JPEGImageWriter |
|
374 |
||
375 |
streamBuffer streamBuf; // Buffer for the stream |
|
376 |
pixelBuffer pixelBuf; // Buffer for pixels |
|
377 |
||
378 |
jboolean abortFlag; // Passed down from Java abort method |
|
379 |
} imageIOData, *imageIODataPtr; |
|
380 |
||
381 |
/* |
|
382 |
* Allocate and initialize a new imageIOData object to associate the |
|
383 |
* jpeg object and the Java object. Returns a pointer to the new object |
|
384 |
* on success, NULL on failure. |
|
385 |
*/ |
|
386 |
static imageIODataPtr initImageioData (JNIEnv *env, |
|
387 |
j_common_ptr cinfo, |
|
388 |
jobject obj) { |
|
389 |
int i, j; |
|
390 |
||
391 |
imageIODataPtr data = (imageIODataPtr) malloc (sizeof(imageIOData)); |
|
392 |
if (data == NULL) { |
|
393 |
return NULL; |
|
394 |
} |
|
395 |
||
396 |
data->jpegObj = cinfo; |
|
397 |
cinfo->client_data = data; |
|
398 |
||
2383
c6a2226cc4de
6791502: IIOException "Invalid icc profile" on jpeg after update from JDK5 to JDK6
bae
parents:
2
diff
changeset
|
399 |
#ifdef DEBUG_IIO_JPEG |
2 | 400 |
printf("new structures: data is %p, cinfo is %p\n", data, cinfo); |
401 |
#endif |
|
402 |
||
403 |
data->imageIOobj = (*env)->NewWeakGlobalRef(env, obj); |
|
404 |
if (data->imageIOobj == NULL) { |
|
405 |
free (data); |
|
406 |
return NULL; |
|
407 |
} |
|
408 |
if (initStreamBuffer(env, &data->streamBuf) == NOT_OK) { |
|
409 |
(*env)->DeleteWeakGlobalRef(env, data->imageIOobj); |
|
410 |
free (data); |
|
411 |
return NULL; |
|
412 |
} |
|
413 |
initPixelBuffer(&data->pixelBuf); |
|
414 |
||
415 |
data->abortFlag = JNI_FALSE; |
|
416 |
||
417 |
return data; |
|
418 |
} |
|
419 |
||
420 |
/* |
|
421 |
* Resets the imageIOData object to its initial state, as though |
|
422 |
* it had just been allocated and initialized. |
|
423 |
*/ |
|
424 |
static void resetImageIOData(JNIEnv *env, imageIODataPtr data) { |
|
425 |
resetStreamBuffer(env, &data->streamBuf); |
|
426 |
resetPixelBuffer(env, &data->pixelBuf); |
|
427 |
data->abortFlag = JNI_FALSE; |
|
428 |
} |
|
429 |
||
430 |
/* |
|
431 |
* Releases all resources held by this object and its subobjects, |
|
432 |
* frees the object, and returns the jpeg object. This method must |
|
433 |
* be called to avoid leaking global references. |
|
434 |
* Note that the jpeg object is not freed or destroyed, as that is |
|
435 |
* the client's responsibility, although the client_data field is |
|
436 |
* cleared. |
|
437 |
*/ |
|
438 |
static j_common_ptr destroyImageioData(JNIEnv *env, imageIODataPtr data) { |
|
439 |
j_common_ptr ret = data->jpegObj; |
|
440 |
(*env)->DeleteWeakGlobalRef(env, data->imageIOobj); |
|
441 |
destroyStreamBuffer(env, &data->streamBuf); |
|
442 |
resetPixelBuffer(env, &data->pixelBuf); |
|
443 |
ret->client_data = NULL; |
|
444 |
free(data); |
|
445 |
return ret; |
|
446 |
} |
|
447 |
||
448 |
/******************** end ImageIOData definition ***********************/ |
|
449 |
||
450 |
/******************** Java array pinning and unpinning *****************/ |
|
451 |
||
452 |
/* We use Get/ReleasePrimitiveArrayCritical functions to avoid |
|
453 |
* the need to copy array elements for the above two objects. |
|
454 |
* |
|
455 |
* MAKE SURE TO: |
|
456 |
* |
|
457 |
* - carefully insert pairs of RELEASE_ARRAYS and GET_ARRAYS around |
|
458 |
* callbacks to Java. |
|
459 |
* - call RELEASE_ARRAYS before returning to Java. |
|
460 |
* |
|
461 |
* Otherwise things will go horribly wrong. There may be memory leaks, |
|
462 |
* excessive pinning, or even VM crashes! |
|
463 |
* |
|
464 |
* Note that GetPrimitiveArrayCritical may fail! |
|
465 |
*/ |
|
466 |
||
467 |
/* |
|
468 |
* Release (unpin) all the arrays in use during a read. |
|
469 |
*/ |
|
470 |
static void RELEASE_ARRAYS(JNIEnv *env, imageIODataPtr data, const JOCTET *next_byte) |
|
471 |
{ |
|
472 |
unpinStreamBuffer(env, &data->streamBuf, next_byte); |
|
473 |
||
474 |
unpinPixelBuffer(env, &data->pixelBuf); |
|
475 |
||
476 |
} |
|
477 |
||
478 |
/* |
|
479 |
* Get (pin) all the arrays in use during a read. |
|
480 |
*/ |
|
481 |
static int GET_ARRAYS(JNIEnv *env, imageIODataPtr data, const JOCTET **next_byte) { |
|
482 |
if (pinStreamBuffer(env, &data->streamBuf, next_byte) == NOT_OK) { |
|
483 |
return NOT_OK; |
|
484 |
} |
|
485 |
||
486 |
if (pinPixelBuffer(env, &data->pixelBuf) == NOT_OK) { |
|
487 |
RELEASE_ARRAYS(env, data, *next_byte); |
|
488 |
return NOT_OK; |
|
489 |
} |
|
490 |
return OK; |
|
491 |
} |
|
492 |
||
493 |
/****** end of Java array pinning and unpinning ***********/ |
|
494 |
||
495 |
/****** Error Handling *******/ |
|
496 |
||
497 |
/* |
|
498 |
* Set up error handling to use setjmp/longjmp. This is the third such |
|
499 |
* setup, as both the AWT jpeg decoder and the com.sun... JPEG classes |
|
500 |
* setup thier own. Ultimately these should be integrated, as they all |
|
501 |
* do pretty much the same thing. |
|
502 |
*/ |
|
503 |
||
504 |
struct sun_jpeg_error_mgr { |
|
505 |
struct jpeg_error_mgr pub; /* "public" fields */ |
|
506 |
||
507 |
jmp_buf setjmp_buffer; /* for return to caller */ |
|
508 |
}; |
|
509 |
||
510 |
typedef struct sun_jpeg_error_mgr * sun_jpeg_error_ptr; |
|
511 |
||
512 |
/* |
|
513 |
* Here's the routine that will replace the standard error_exit method: |
|
514 |
*/ |
|
515 |
||
516 |
METHODDEF(void) |
|
517 |
sun_jpeg_error_exit (j_common_ptr cinfo) |
|
518 |
{ |
|
519 |
/* cinfo->err really points to a sun_jpeg_error_mgr struct */ |
|
520 |
sun_jpeg_error_ptr myerr = (sun_jpeg_error_ptr) cinfo->err; |
|
521 |
||
522 |
/* For Java, we will format the message and put it in the error we throw. */ |
|
523 |
||
524 |
/* Return control to the setjmp point */ |
|
525 |
longjmp(myerr->setjmp_buffer, 1); |
|
526 |
} |
|
527 |
||
528 |
/* |
|
529 |
* Error Message handling |
|
530 |
* |
|
531 |
* This overrides the output_message method to send JPEG messages |
|
532 |
* |
|
533 |
*/ |
|
534 |
||
535 |
METHODDEF(void) |
|
536 |
sun_jpeg_output_message (j_common_ptr cinfo) |
|
537 |
{ |
|
538 |
char buffer[JMSG_LENGTH_MAX]; |
|
539 |
jstring string; |
|
540 |
imageIODataPtr data = (imageIODataPtr) cinfo->client_data; |
|
541 |
JNIEnv *env = (JNIEnv *)JNU_GetEnv(jvm, JNI_VERSION_1_2); |
|
542 |
jobject theObject; |
|
543 |
||
544 |
/* Create the message */ |
|
545 |
(*cinfo->err->format_message) (cinfo, buffer); |
|
546 |
||
547 |
// Create a new java string from the message |
|
548 |
string = (*env)->NewStringUTF(env, buffer); |
|
549 |
||
550 |
theObject = data->imageIOobj; |
|
551 |
||
552 |
if (cinfo->is_decompressor) { |
|
553 |
(*env)->CallVoidMethod(env, theObject, |
|
554 |
JPEGImageReader_warningWithMessageID, |
|
555 |
string); |
|
556 |
} else { |
|
557 |
(*env)->CallVoidMethod(env, theObject, |
|
558 |
JPEGImageWriter_warningWithMessageID, |
|
559 |
string); |
|
560 |
} |
|
561 |
} |
|
562 |
||
563 |
/* End of verbatim copy from jpegdecoder.c */ |
|
564 |
||
565 |
/*************** end of error handling *********************/ |
|
566 |
||
567 |
/*************** Shared utility code ***********************/ |
|
568 |
||
569 |
static void imageio_set_stream(JNIEnv *env, |
|
570 |
j_common_ptr cinfo, |
|
571 |
imageIODataPtr data, |
|
572 |
jobject stream){ |
|
573 |
streamBufferPtr sb; |
|
574 |
sun_jpeg_error_ptr jerr; |
|
575 |
||
576 |
sb = &data->streamBuf; |
|
577 |
||
578 |
resetStreamBuffer(env, sb); // Removes any old stream |
|
579 |
||
580 |
/* Now we need a new global reference for the stream */ |
|
581 |
if (stream != NULL) { // Fix for 4411955 |
|
582 |
sb->stream = (*env)->NewGlobalRef(env, stream); |
|
583 |
if (sb->stream == NULL) { |
|
584 |
JNU_ThrowByName(env, |
|
585 |
"java/lang/OutOfMemoryError", |
|
586 |
"Setting Stream"); |
|
587 |
return; |
|
588 |
} |
|
589 |
} |
|
590 |
||
591 |
/* And finally reset state */ |
|
592 |
data->abortFlag = JNI_FALSE; |
|
593 |
||
594 |
/* Establish the setjmp return context for sun_jpeg_error_exit to use. */ |
|
595 |
jerr = (sun_jpeg_error_ptr) cinfo->err; |
|
596 |
||
597 |
if (setjmp(jerr->setjmp_buffer)) { |
|
598 |
/* If we get here, the JPEG code has signaled an error |
|
599 |
while aborting. */ |
|
600 |
if (!(*env)->ExceptionOccurred(env)) { |
|
601 |
char buffer[JMSG_LENGTH_MAX]; |
|
602 |
(*cinfo->err->format_message) (cinfo, |
|
603 |
buffer); |
|
604 |
JNU_ThrowByName(env, "javax/imageio/IIOException", buffer); |
|
605 |
} |
|
606 |
return; |
|
607 |
} |
|
608 |
||
609 |
jpeg_abort(cinfo); // Frees any markers, but not tables |
|
610 |
||
611 |
} |
|
612 |
||
613 |
static void imageio_reset(JNIEnv *env, |
|
614 |
j_common_ptr cinfo, |
|
615 |
imageIODataPtr data) { |
|
616 |
sun_jpeg_error_ptr jerr; |
|
617 |
||
618 |
resetImageIOData(env, data); // Mapping to jpeg object is retained. |
|
619 |
||
620 |
/* Establish the setjmp return context for sun_jpeg_error_exit to use. */ |
|
621 |
jerr = (sun_jpeg_error_ptr) cinfo->err; |
|
622 |
||
623 |
if (setjmp(jerr->setjmp_buffer)) { |
|
624 |
/* If we get here, the JPEG code has signaled an error |
|
625 |
while aborting. */ |
|
626 |
if (!(*env)->ExceptionOccurred(env)) { |
|
627 |
char buffer[JMSG_LENGTH_MAX]; |
|
628 |
(*cinfo->err->format_message) (cinfo, buffer); |
|
629 |
JNU_ThrowByName(env, "javax/imageio/IIOException", buffer); |
|
630 |
} |
|
631 |
return; |
|
632 |
} |
|
633 |
||
634 |
jpeg_abort(cinfo); // Does not reset tables |
|
635 |
||
636 |
} |
|
637 |
||
638 |
static void imageio_dispose(j_common_ptr info) { |
|
639 |
||
640 |
if (info != NULL) { |
|
641 |
free(info->err); |
|
642 |
info->err = NULL; |
|
643 |
if (info->is_decompressor) { |
|
644 |
j_decompress_ptr dinfo = (j_decompress_ptr) info; |
|
645 |
free(dinfo->src); |
|
646 |
dinfo->src = NULL; |
|
647 |
} else { |
|
648 |
j_compress_ptr cinfo = (j_compress_ptr) info; |
|
649 |
free(cinfo->dest); |
|
650 |
cinfo->dest = NULL; |
|
651 |
} |
|
652 |
jpeg_destroy(info); |
|
653 |
free(info); |
|
654 |
} |
|
655 |
} |
|
656 |
||
657 |
static void imageio_abort(JNIEnv *env, jobject this, |
|
658 |
imageIODataPtr data) { |
|
659 |
data->abortFlag = JNI_TRUE; |
|
660 |
} |
|
661 |
||
662 |
static int setQTables(JNIEnv *env, |
|
663 |
j_common_ptr cinfo, |
|
664 |
jobjectArray qtables, |
|
665 |
boolean write) { |
|
666 |
jsize qlen; |
|
667 |
jobject table; |
|
668 |
jintArray qdata; |
|
669 |
jint *qdataBody; |
|
670 |
JQUANT_TBL *quant_ptr; |
|
671 |
int i, j; |
|
672 |
j_compress_ptr comp; |
|
673 |
j_decompress_ptr decomp; |
|
674 |
||
675 |
qlen = (*env)->GetArrayLength(env, qtables); |
|
2383
c6a2226cc4de
6791502: IIOException "Invalid icc profile" on jpeg after update from JDK5 to JDK6
bae
parents:
2
diff
changeset
|
676 |
#ifdef DEBUG_IIO_JPEG |
2 | 677 |
printf("in setQTables, qlen = %d, write is %d\n", qlen, write); |
678 |
#endif |
|
679 |
for (i = 0; i < qlen; i++) { |
|
680 |
table = (*env)->GetObjectArrayElement(env, qtables, i); |
|
681 |
qdata = (*env)->GetObjectField(env, table, JPEGQTable_tableID); |
|
682 |
qdataBody = (*env)->GetPrimitiveArrayCritical(env, qdata, NULL); |
|
683 |
||
684 |
if (cinfo->is_decompressor) { |
|
685 |
decomp = (j_decompress_ptr) cinfo; |
|
686 |
if (decomp->quant_tbl_ptrs[i] == NULL) { |
|
687 |
decomp->quant_tbl_ptrs[i] = |
|
688 |
jpeg_alloc_quant_table(cinfo); |
|
689 |
} |
|
690 |
quant_ptr = decomp->quant_tbl_ptrs[i]; |
|
691 |
} else { |
|
692 |
comp = (j_compress_ptr) cinfo; |
|
693 |
if (comp->quant_tbl_ptrs[i] == NULL) { |
|
694 |
comp->quant_tbl_ptrs[i] = |
|
695 |
jpeg_alloc_quant_table(cinfo); |
|
696 |
} |
|
697 |
quant_ptr = comp->quant_tbl_ptrs[i]; |
|
698 |
} |
|
699 |
||
700 |
for (j = 0; j < 64; j++) { |
|
701 |
quant_ptr->quantval[j] = (UINT16)qdataBody[j]; |
|
702 |
} |
|
703 |
quant_ptr->sent_table = !write; |
|
704 |
(*env)->ReleasePrimitiveArrayCritical(env, |
|
705 |
qdata, |
|
706 |
qdataBody, |
|
707 |
0); |
|
708 |
} |
|
709 |
return qlen; |
|
710 |
} |
|
711 |
||
712 |
static void setHuffTable(JNIEnv *env, |
|
713 |
JHUFF_TBL *huff_ptr, |
|
714 |
jobject table) { |
|
715 |
||
716 |
jshortArray huffLens; |
|
717 |
jshortArray huffValues; |
|
718 |
jshort *hlensBody, *hvalsBody; |
|
719 |
jsize hlensLen, hvalsLen; |
|
720 |
int i; |
|
721 |
||
722 |
// lengths |
|
723 |
huffLens = (*env)->GetObjectField(env, |
|
724 |
table, |
|
725 |
JPEGHuffmanTable_lengthsID); |
|
726 |
hlensLen = (*env)->GetArrayLength(env, huffLens); |
|
727 |
hlensBody = (*env)->GetShortArrayElements(env, |
|
728 |
huffLens, |
|
729 |
NULL); |
|
730 |
for (i = 1; i <= hlensLen; i++) { |
|
731 |
huff_ptr->bits[i] = (UINT8)hlensBody[i-1]; |
|
732 |
} |
|
733 |
(*env)->ReleaseShortArrayElements(env, |
|
734 |
huffLens, |
|
735 |
hlensBody, |
|
736 |
JNI_ABORT); |
|
737 |
// values |
|
738 |
huffValues = (*env)->GetObjectField(env, |
|
739 |
table, |
|
740 |
JPEGHuffmanTable_valuesID); |
|
741 |
hvalsLen = (*env)->GetArrayLength(env, huffValues); |
|
742 |
hvalsBody = (*env)->GetShortArrayElements(env, |
|
743 |
huffValues, |
|
744 |
NULL); |
|
745 |
||
746 |
for (i = 0; i < hvalsLen; i++) { |
|
747 |
huff_ptr->huffval[i] = (UINT8)hvalsBody[i]; |
|
748 |
} |
|
749 |
(*env)->ReleaseShortArrayElements(env, |
|
750 |
huffValues, |
|
751 |
hvalsBody, |
|
752 |
JNI_ABORT); |
|
753 |
} |
|
754 |
||
755 |
static int setHTables(JNIEnv *env, |
|
756 |
j_common_ptr cinfo, |
|
757 |
jobjectArray DCHuffmanTables, |
|
758 |
jobjectArray ACHuffmanTables, |
|
759 |
boolean write) { |
|
760 |
int i; |
|
761 |
jobject table; |
|
762 |
JHUFF_TBL *huff_ptr; |
|
763 |
j_compress_ptr comp; |
|
764 |
j_decompress_ptr decomp; |
|
765 |
jsize hlen = (*env)->GetArrayLength(env, DCHuffmanTables); |
|
766 |
for (i = 0; i < hlen; i++) { |
|
767 |
if (cinfo->is_decompressor) { |
|
768 |
decomp = (j_decompress_ptr) cinfo; |
|
769 |
if (decomp->dc_huff_tbl_ptrs[i] == NULL) { |
|
770 |
decomp->dc_huff_tbl_ptrs[i] = |
|
771 |
jpeg_alloc_huff_table(cinfo); |
|
772 |
} |
|
773 |
huff_ptr = decomp->dc_huff_tbl_ptrs[i]; |
|
774 |
} else { |
|
775 |
comp = (j_compress_ptr) cinfo; |
|
776 |
if (comp->dc_huff_tbl_ptrs[i] == NULL) { |
|
777 |
comp->dc_huff_tbl_ptrs[i] = |
|
778 |
jpeg_alloc_huff_table(cinfo); |
|
779 |
} |
|
780 |
huff_ptr = comp->dc_huff_tbl_ptrs[i]; |
|
781 |
} |
|
782 |
table = (*env)->GetObjectArrayElement(env, DCHuffmanTables, i); |
|
783 |
setHuffTable(env, huff_ptr, table); |
|
784 |
huff_ptr->sent_table = !write; |
|
785 |
} |
|
786 |
hlen = (*env)->GetArrayLength(env, ACHuffmanTables); |
|
787 |
for (i = 0; i < hlen; i++) { |
|
788 |
if (cinfo->is_decompressor) { |
|
789 |
decomp = (j_decompress_ptr) cinfo; |
|
790 |
if (decomp->ac_huff_tbl_ptrs[i] == NULL) { |
|
791 |
decomp->ac_huff_tbl_ptrs[i] = |
|
792 |
jpeg_alloc_huff_table(cinfo); |
|
793 |
} |
|
794 |
huff_ptr = decomp->ac_huff_tbl_ptrs[i]; |
|
795 |
} else { |
|
796 |
comp = (j_compress_ptr) cinfo; |
|
797 |
if (comp->ac_huff_tbl_ptrs[i] == NULL) { |
|
798 |
comp->ac_huff_tbl_ptrs[i] = |
|
799 |
jpeg_alloc_huff_table(cinfo); |
|
800 |
} |
|
801 |
huff_ptr = comp->ac_huff_tbl_ptrs[i]; |
|
802 |
} |
|
803 |
table = (*env)->GetObjectArrayElement(env, ACHuffmanTables, i); |
|
804 |
setHuffTable(env, huff_ptr, table); |
|
805 |
huff_ptr->sent_table = !write; |
|
806 |
} |
|
807 |
return hlen; |
|
808 |
} |
|
809 |
||
810 |
||
811 |
/*************** end of shared utility code ****************/ |
|
812 |
||
813 |
/********************** Reader Support **************************/ |
|
814 |
||
815 |
/********************** Source Management ***********************/ |
|
816 |
||
817 |
/* |
|
818 |
* INPUT HANDLING: |
|
819 |
* |
|
820 |
* The JPEG library's input management is defined by the jpeg_source_mgr |
|
821 |
* structure which contains two fields to convey the information in the |
|
822 |
* buffer and 5 methods which perform all buffer management. The library |
|
823 |
* defines a standard input manager that uses stdio for obtaining compressed |
|
824 |
* jpeg data, but here we need to use Java to get our data. |
|
825 |
* |
|
826 |
* We use the library jpeg_source_mgr but our own routines that access |
|
827 |
* imageio-specific information in the imageIOData structure. |
|
828 |
*/ |
|
829 |
||
830 |
/* |
|
831 |
* Initialize source. This is called by jpeg_read_header() before any |
|
832 |
* data is actually read. Unlike init_destination(), it may leave |
|
833 |
* bytes_in_buffer set to 0 (in which case a fill_input_buffer() call |
|
834 |
* will occur immediately). |
|
835 |
*/ |
|
836 |
||
837 |
GLOBAL(void) |
|
838 |
imageio_init_source(j_decompress_ptr cinfo) |
|
839 |
{ |
|
840 |
struct jpeg_source_mgr *src = cinfo->src; |
|
841 |
src->next_input_byte = NULL; |
|
842 |
src->bytes_in_buffer = 0; |
|
843 |
} |
|
844 |
||
845 |
/* |
|
846 |
* This is called whenever bytes_in_buffer has reached zero and more |
|
847 |
* data is wanted. In typical applications, it should read fresh data |
|
848 |
* into the buffer (ignoring the current state of next_input_byte and |
|
849 |
* bytes_in_buffer), reset the pointer & count to the start of the |
|
850 |
* buffer, and return TRUE indicating that the buffer has been reloaded. |
|
851 |
* It is not necessary to fill the buffer entirely, only to obtain at |
|
852 |
* least one more byte. bytes_in_buffer MUST be set to a positive value |
|
853 |
* if TRUE is returned. A FALSE return should only be used when I/O |
|
854 |
* suspension is desired (this mode is discussed in the next section). |
|
855 |
*/ |
|
856 |
/* |
|
857 |
* Note that with I/O suspension turned on, this procedure should not |
|
858 |
* do any work since the JPEG library has a very simple backtracking |
|
859 |
* mechanism which relies on the fact that the buffer will be filled |
|
860 |
* only when it has backed out to the top application level. When |
|
861 |
* suspendable is turned on, imageio_fill_suspended_buffer will |
|
862 |
* do the actual work of filling the buffer. |
|
863 |
*/ |
|
864 |
||
865 |
GLOBAL(boolean) |
|
866 |
imageio_fill_input_buffer(j_decompress_ptr cinfo) |
|
867 |
{ |
|
868 |
struct jpeg_source_mgr *src = cinfo->src; |
|
869 |
imageIODataPtr data = (imageIODataPtr) cinfo->client_data; |
|
870 |
streamBufferPtr sb = &data->streamBuf; |
|
871 |
JNIEnv *env = (JNIEnv *)JNU_GetEnv(jvm, JNI_VERSION_1_2); |
|
872 |
int ret; |
|
873 |
||
874 |
/* This is where input suspends */ |
|
875 |
if (sb->suspendable) { |
|
876 |
return FALSE; |
|
877 |
} |
|
878 |
||
2383
c6a2226cc4de
6791502: IIOException "Invalid icc profile" on jpeg after update from JDK5 to JDK6
bae
parents:
2
diff
changeset
|
879 |
#ifdef DEBUG_IIO_JPEG |
2 | 880 |
printf("Filling input buffer, remaining skip is %ld, ", |
881 |
sb->remaining_skip); |
|
882 |
printf("Buffer length is %d\n", sb->bufferLength); |
|
883 |
#endif |
|
884 |
||
885 |
/* |
|
886 |
* Definitively skips. Could be left over if we tried to skip |
|
887 |
* more than a buffer's worth but suspended when getting the next |
|
888 |
* buffer. Now we aren't suspended, so we can catch up. |
|
889 |
*/ |
|
890 |
if (sb->remaining_skip) { |
|
891 |
src->skip_input_data(cinfo, 0); |
|
892 |
} |
|
893 |
||
894 |
/* |
|
895 |
* Now fill a complete buffer, or as much of one as the stream |
|
896 |
* will give us if we are near the end. |
|
897 |
*/ |
|
898 |
RELEASE_ARRAYS(env, data, src->next_input_byte); |
|
899 |
ret = (*env)->CallIntMethod(env, |
|
900 |
sb->stream, |
|
901 |
ImageInputStream_readID, |
|
902 |
sb->hstreamBuffer, 0, |
|
903 |
sb->bufferLength); |
|
904 |
if ((*env)->ExceptionOccurred(env) |
|
905 |
|| !GET_ARRAYS(env, data, &(src->next_input_byte))) { |
|
906 |
cinfo->err->error_exit((j_common_ptr) cinfo); |
|
907 |
} |
|
908 |
||
2383
c6a2226cc4de
6791502: IIOException "Invalid icc profile" on jpeg after update from JDK5 to JDK6
bae
parents:
2
diff
changeset
|
909 |
#ifdef DEBUG_IIO_JPEG |
2 | 910 |
printf("Buffer filled. ret = %d\n", ret); |
911 |
#endif |
|
912 |
/* |
|
913 |
* If we have reached the end of the stream, then the EOI marker |
|
914 |
* is missing. We accept such streams but generate a warning. |
|
915 |
* The image is likely to be corrupted, though everything through |
|
916 |
* the end of the last complete MCU should be usable. |
|
917 |
*/ |
|
918 |
if (ret <= 0) { |
|
919 |
jobject reader = data->imageIOobj; |
|
2383
c6a2226cc4de
6791502: IIOException "Invalid icc profile" on jpeg after update from JDK5 to JDK6
bae
parents:
2
diff
changeset
|
920 |
#ifdef DEBUG_IIO_JPEG |
2 | 921 |
printf("YO! Early EOI! ret = %d\n", ret); |
922 |
#endif |
|
923 |
RELEASE_ARRAYS(env, data, src->next_input_byte); |
|
924 |
(*env)->CallVoidMethod(env, reader, |
|
925 |
JPEGImageReader_warningOccurredID, |
|
926 |
READ_NO_EOI); |
|
927 |
if ((*env)->ExceptionOccurred(env) |
|
928 |
|| !GET_ARRAYS(env, data, &(src->next_input_byte))) { |
|
929 |
cinfo->err->error_exit((j_common_ptr) cinfo); |
|
930 |
} |
|
931 |
||
932 |
sb->buf[0] = (JOCTET) 0xFF; |
|
933 |
sb->buf[1] = (JOCTET) JPEG_EOI; |
|
934 |
ret = 2; |
|
935 |
} |
|
936 |
||
937 |
src->next_input_byte = sb->buf; |
|
938 |
src->bytes_in_buffer = ret; |
|
939 |
||
940 |
return TRUE; |
|
941 |
} |
|
942 |
||
943 |
/* |
|
944 |
* With I/O suspension turned on, the JPEG library requires that all |
|
945 |
* buffer filling be done at the top application level, using this |
|
946 |
* function. Due to the way that backtracking works, this procedure |
|
947 |
* saves all of the data that was left in the buffer when suspension |
|
948 |
* occured and read new data only at the end. |
|
949 |
*/ |
|
950 |
||
951 |
GLOBAL(void) |
|
952 |
imageio_fill_suspended_buffer(j_decompress_ptr cinfo) |
|
953 |
{ |
|
954 |
struct jpeg_source_mgr *src = cinfo->src; |
|
955 |
imageIODataPtr data = (imageIODataPtr) cinfo->client_data; |
|
956 |
streamBufferPtr sb = &data->streamBuf; |
|
957 |
JNIEnv *env = (JNIEnv *)JNU_GetEnv(jvm, JNI_VERSION_1_2); |
|
958 |
jint ret; |
|
959 |
int offset, buflen; |
|
960 |
||
961 |
/* |
|
962 |
* The original (jpegdecoder.c) had code here that called |
|
963 |
* InputStream.available and just returned if the number of bytes |
|
964 |
* available was less than any remaining skip. Presumably this was |
|
965 |
* to avoid blocking, although the benefit was unclear, as no more |
|
966 |
* decompression can take place until more data is available, so |
|
967 |
* the code would block on input a little further along anyway. |
|
968 |
* ImageInputStreams don't have an available method, so we'll just |
|
969 |
* block in the skip if we have to. |
|
970 |
*/ |
|
971 |
||
972 |
if (sb->remaining_skip) { |
|
973 |
src->skip_input_data(cinfo, 0); |
|
974 |
} |
|
975 |
||
976 |
/* Save the data currently in the buffer */ |
|
977 |
offset = src->bytes_in_buffer; |
|
978 |
if (src->next_input_byte > sb->buf) { |
|
979 |
memcpy(sb->buf, src->next_input_byte, offset); |
|
980 |
} |
|
981 |
RELEASE_ARRAYS(env, data, src->next_input_byte); |
|
982 |
buflen = sb->bufferLength - offset; |
|
983 |
if (buflen <= 0) { |
|
984 |
if (!GET_ARRAYS(env, data, &(src->next_input_byte))) { |
|
985 |
cinfo->err->error_exit((j_common_ptr) cinfo); |
|
986 |
} |
|
987 |
return; |
|
988 |
} |
|
989 |
||
990 |
ret = (*env)->CallIntMethod(env, sb->stream, |
|
991 |
ImageInputStream_readID, |
|
992 |
sb->hstreamBuffer, |
|
993 |
offset, buflen); |
|
994 |
if ((*env)->ExceptionOccurred(env) |
|
995 |
|| !GET_ARRAYS(env, data, &(src->next_input_byte))) { |
|
996 |
cinfo->err->error_exit((j_common_ptr) cinfo); |
|
997 |
} |
|
998 |
/* |
|
999 |
* If we have reached the end of the stream, then the EOI marker |
|
1000 |
* is missing. We accept such streams but generate a warning. |
|
1001 |
* The image is likely to be corrupted, though everything through |
|
1002 |
* the end of the last complete MCU should be usable. |
|
1003 |
*/ |
|
1004 |
if (ret <= 0) { |
|
1005 |
jobject reader = data->imageIOobj; |
|
1006 |
RELEASE_ARRAYS(env, data, src->next_input_byte); |
|
1007 |
(*env)->CallVoidMethod(env, reader, |
|
1008 |
JPEGImageReader_warningOccurredID, |
|
1009 |
READ_NO_EOI); |
|
1010 |
if ((*env)->ExceptionOccurred(env) |
|
1011 |
|| !GET_ARRAYS(env, data, &(src->next_input_byte))) { |
|
1012 |
cinfo->err->error_exit((j_common_ptr) cinfo); |
|
1013 |
} |
|
1014 |
||
1015 |
sb->buf[offset] = (JOCTET) 0xFF; |
|
1016 |
sb->buf[offset + 1] = (JOCTET) JPEG_EOI; |
|
1017 |
ret = 2; |
|
1018 |
} |
|
1019 |
||
1020 |
src->next_input_byte = sb->buf; |
|
1021 |
src->bytes_in_buffer = ret + offset; |
|
1022 |
||
1023 |
return; |
|
1024 |
} |
|
1025 |
||
1026 |
/* |
|
1027 |
* Skip num_bytes worth of data. The buffer pointer and count are |
|
1028 |
* advanced over num_bytes input bytes, using the input stream |
|
1029 |
* skipBytes method if the skip is greater than the number of bytes |
|
1030 |
* in the buffer. This is used to skip over a potentially large amount of |
|
1031 |
* uninteresting data (such as an APPn marker). bytes_in_buffer will be |
|
1032 |
* zero on return if the skip is larger than the current contents of the |
|
1033 |
* buffer. |
|
1034 |
* |
|
1035 |
* A negative skip count is treated as a no-op. A zero skip count |
|
1036 |
* skips any remaining skip from a previous skip while suspended. |
|
1037 |
* |
|
1038 |
* Note that with I/O suspension turned on, this procedure does not |
|
1039 |
* call skipBytes since the JPEG library has a very simple backtracking |
|
1040 |
* mechanism which relies on the fact that the application level has |
|
1041 |
* exclusive control over actual I/O. |
|
1042 |
*/ |
|
1043 |
||
1044 |
GLOBAL(void) |
|
1045 |
imageio_skip_input_data(j_decompress_ptr cinfo, long num_bytes) |
|
1046 |
{ |
|
1047 |
struct jpeg_source_mgr *src = cinfo->src; |
|
1048 |
imageIODataPtr data = (imageIODataPtr) cinfo->client_data; |
|
1049 |
streamBufferPtr sb = &data->streamBuf; |
|
1050 |
JNIEnv *env = (JNIEnv *)JNU_GetEnv(jvm, JNI_VERSION_1_2); |
|
1051 |
jlong ret; |
|
1052 |
jobject reader; |
|
1053 |
||
1054 |
if (num_bytes < 0) { |
|
1055 |
return; |
|
1056 |
} |
|
1057 |
num_bytes += sb->remaining_skip; |
|
1058 |
sb->remaining_skip = 0; |
|
1059 |
||
1060 |
/* First the easy case where we are skipping <= the current contents. */ |
|
1061 |
ret = src->bytes_in_buffer; |
|
1062 |
if (ret >= num_bytes) { |
|
1063 |
src->next_input_byte += num_bytes; |
|
1064 |
src->bytes_in_buffer -= num_bytes; |
|
1065 |
return; |
|
1066 |
} |
|
1067 |
||
1068 |
/* |
|
1069 |
* We are skipping more than is in the buffer. We empty the buffer and, |
|
1070 |
* if we aren't suspended, call the Java skipBytes method. We always |
|
1071 |
* leave the buffer empty, to be filled by either fill method above. |
|
1072 |
*/ |
|
1073 |
src->bytes_in_buffer = 0; |
|
1074 |
src->next_input_byte = sb->buf; |
|
1075 |
||
1076 |
num_bytes -= (long)ret; |
|
1077 |
if (sb->suspendable) { |
|
1078 |
sb->remaining_skip = num_bytes; |
|
1079 |
return; |
|
1080 |
} |
|
1081 |
||
1082 |
RELEASE_ARRAYS(env, data, src->next_input_byte); |
|
1083 |
ret = (*env)->CallLongMethod(env, |
|
1084 |
sb->stream, |
|
1085 |
ImageInputStream_skipBytesID, |
|
1086 |
(jlong) num_bytes); |
|
1087 |
if ((*env)->ExceptionOccurred(env) |
|
1088 |
|| !GET_ARRAYS(env, data, &(src->next_input_byte))) { |
|
1089 |
cinfo->err->error_exit((j_common_ptr) cinfo); |
|
1090 |
} |
|
1091 |
||
1092 |
/* |
|
1093 |
* If we have reached the end of the stream, then the EOI marker |
|
1094 |
* is missing. We accept such streams but generate a warning. |
|
1095 |
* The image is likely to be corrupted, though everything through |
|
1096 |
* the end of the last complete MCU should be usable. |
|
1097 |
*/ |
|
1098 |
if (ret <= 0) { |
|
1099 |
reader = data->imageIOobj; |
|
1100 |
RELEASE_ARRAYS(env, data, src->next_input_byte); |
|
1101 |
(*env)->CallVoidMethod(env, |
|
1102 |
reader, |
|
1103 |
JPEGImageReader_warningOccurredID, |
|
1104 |
READ_NO_EOI); |
|
1105 |
||
1106 |
if ((*env)->ExceptionOccurred(env) |
|
1107 |
|| !GET_ARRAYS(env, data, &(src->next_input_byte))) { |
|
1108 |
cinfo->err->error_exit((j_common_ptr) cinfo); |
|
1109 |
} |
|
1110 |
sb->buf[0] = (JOCTET) 0xFF; |
|
1111 |
sb->buf[1] = (JOCTET) JPEG_EOI; |
|
1112 |
src->bytes_in_buffer = 2; |
|
1113 |
src->next_input_byte = sb->buf; |
|
1114 |
} |
|
1115 |
} |
|
1116 |
||
1117 |
/* |
|
1118 |
* Terminate source --- called by jpeg_finish_decompress() after all |
|
1119 |
* data for an image has been read. In our case pushes back any |
|
1120 |
* remaining data, as it will be for another image and must be available |
|
1121 |
* for java to find out that there is another image. Also called if |
|
1122 |
* reseting state after reading a tables-only image. |
|
1123 |
*/ |
|
1124 |
||
1125 |
GLOBAL(void) |
|
1126 |
imageio_term_source(j_decompress_ptr cinfo) |
|
1127 |
{ |
|
1128 |
// To pushback, just seek back by src->bytes_in_buffer |
|
1129 |
struct jpeg_source_mgr *src = cinfo->src; |
|
1130 |
imageIODataPtr data = (imageIODataPtr) cinfo->client_data; |
|
1131 |
JNIEnv *env = (JNIEnv *)JNU_GetEnv(jvm, JNI_VERSION_1_2); |
|
1132 |
jobject reader = data->imageIOobj; |
|
1133 |
if (src->bytes_in_buffer > 0) { |
|
1134 |
RELEASE_ARRAYS(env, data, src->next_input_byte); |
|
1135 |
(*env)->CallVoidMethod(env, |
|
1136 |
reader, |
|
1137 |
JPEGImageReader_pushBackID, |
|
1138 |
src->bytes_in_buffer); |
|
1139 |
||
1140 |
if ((*env)->ExceptionOccurred(env) |
|
1141 |
|| !GET_ARRAYS(env, data, &(src->next_input_byte))) { |
|
1142 |
cinfo->err->error_exit((j_common_ptr) cinfo); |
|
1143 |
} |
|
1144 |
src->bytes_in_buffer = 0; |
|
1145 |
//src->next_input_byte = sb->buf; |
|
1146 |
} |
|
1147 |
} |
|
1148 |
||
1149 |
/********************* end of source manager ******************/ |
|
1150 |
||
1151 |
/********************* ICC profile support ********************/ |
|
1152 |
/* |
|
1153 |
* The following routines are modified versions of the ICC |
|
1154 |
* profile support routines available from the IJG website. |
|
1155 |
* The originals were written by Todd Newman |
|
1156 |
* <tdn@eccentric.esd.sgi.com> and modified by Tom Lane for |
|
1157 |
* the IJG. They are further modified to fit in the context |
|
1158 |
* of the imageio JPEG plug-in. |
|
1159 |
*/ |
|
1160 |
||
1161 |
/* |
|
1162 |
* Since an ICC profile can be larger than the maximum size of a JPEG marker |
|
1163 |
* (64K), we need provisions to split it into multiple markers. The format |
|
1164 |
* defined by the ICC specifies one or more APP2 markers containing the |
|
1165 |
* following data: |
|
1166 |
* Identifying string ASCII "ICC_PROFILE\0" (12 bytes) |
|
1167 |
* Marker sequence number 1 for first APP2, 2 for next, etc (1 byte) |
|
1168 |
* Number of markers Total number of APP2's used (1 byte) |
|
1169 |
* Profile data (remainder of APP2 data) |
|
1170 |
* Decoders should use the marker sequence numbers to reassemble the profile, |
|
1171 |
* rather than assuming that the APP2 markers appear in the correct sequence. |
|
1172 |
*/ |
|
1173 |
||
1174 |
#define ICC_MARKER (JPEG_APP0 + 2) /* JPEG marker code for ICC */ |
|
1175 |
#define ICC_OVERHEAD_LEN 14 /* size of non-profile data in APP2 */ |
|
1176 |
#define MAX_BYTES_IN_MARKER 65533 /* maximum data len of a JPEG marker */ |
|
1177 |
#define MAX_DATA_BYTES_IN_ICC_MARKER (MAX_BYTES_IN_MARKER - ICC_OVERHEAD_LEN) |
|
1178 |
||
1179 |
||
1180 |
/* |
|
1181 |
* Handy subroutine to test whether a saved marker is an ICC profile marker. |
|
1182 |
*/ |
|
1183 |
||
1184 |
static boolean |
|
1185 |
marker_is_icc (jpeg_saved_marker_ptr marker) |
|
1186 |
{ |
|
1187 |
return |
|
1188 |
marker->marker == ICC_MARKER && |
|
1189 |
marker->data_length >= ICC_OVERHEAD_LEN && |
|
1190 |
/* verify the identifying string */ |
|
1191 |
GETJOCTET(marker->data[0]) == 0x49 && |
|
1192 |
GETJOCTET(marker->data[1]) == 0x43 && |
|
1193 |
GETJOCTET(marker->data[2]) == 0x43 && |
|
1194 |
GETJOCTET(marker->data[3]) == 0x5F && |
|
1195 |
GETJOCTET(marker->data[4]) == 0x50 && |
|
1196 |
GETJOCTET(marker->data[5]) == 0x52 && |
|
1197 |
GETJOCTET(marker->data[6]) == 0x4F && |
|
1198 |
GETJOCTET(marker->data[7]) == 0x46 && |
|
1199 |
GETJOCTET(marker->data[8]) == 0x49 && |
|
1200 |
GETJOCTET(marker->data[9]) == 0x4C && |
|
1201 |
GETJOCTET(marker->data[10]) == 0x45 && |
|
1202 |
GETJOCTET(marker->data[11]) == 0x0; |
|
1203 |
} |
|
1204 |
||
1205 |
/* |
|
1206 |
* See if there was an ICC profile in the JPEG file being read; |
|
1207 |
* if so, reassemble and return the profile data as a new Java byte array. |
|
1208 |
* If there was no ICC profile, return NULL. |
|
1209 |
* |
|
1210 |
* If the file contains invalid ICC APP2 markers, we throw an IIOException |
|
1211 |
* with an appropriate message. |
|
1212 |
*/ |
|
1213 |
||
1214 |
jbyteArray |
|
1215 |
read_icc_profile (JNIEnv *env, j_decompress_ptr cinfo) |
|
1216 |
{ |
|
1217 |
jpeg_saved_marker_ptr marker; |
|
1218 |
int num_markers = 0; |
|
2383
c6a2226cc4de
6791502: IIOException "Invalid icc profile" on jpeg after update from JDK5 to JDK6
bae
parents:
2
diff
changeset
|
1219 |
int num_found_markers = 0; |
2 | 1220 |
int seq_no; |
1221 |
JOCTET *icc_data; |
|
2383
c6a2226cc4de
6791502: IIOException "Invalid icc profile" on jpeg after update from JDK5 to JDK6
bae
parents:
2
diff
changeset
|
1222 |
JOCTET *dst_ptr; |
2 | 1223 |
unsigned int total_length; |
1224 |
#define MAX_SEQ_NO 255 // sufficient since marker numbers are bytes |
|
2383
c6a2226cc4de
6791502: IIOException "Invalid icc profile" on jpeg after update from JDK5 to JDK6
bae
parents:
2
diff
changeset
|
1225 |
jpeg_saved_marker_ptr icc_markers[MAX_SEQ_NO + 1]; |
c6a2226cc4de
6791502: IIOException "Invalid icc profile" on jpeg after update from JDK5 to JDK6
bae
parents:
2
diff
changeset
|
1226 |
int first; // index of the first marker in the icc_markers array |
c6a2226cc4de
6791502: IIOException "Invalid icc profile" on jpeg after update from JDK5 to JDK6
bae
parents:
2
diff
changeset
|
1227 |
int last; // index of the last marker in the icc_markers array |
2 | 1228 |
jbyteArray data = NULL; |
1229 |
||
1230 |
/* This first pass over the saved markers discovers whether there are |
|
1231 |
* any ICC markers and verifies the consistency of the marker numbering. |
|
1232 |
*/ |
|
1233 |
||
2383
c6a2226cc4de
6791502: IIOException "Invalid icc profile" on jpeg after update from JDK5 to JDK6
bae
parents:
2
diff
changeset
|
1234 |
for (seq_no = 0; seq_no <= MAX_SEQ_NO; seq_no++) |
c6a2226cc4de
6791502: IIOException "Invalid icc profile" on jpeg after update from JDK5 to JDK6
bae
parents:
2
diff
changeset
|
1235 |
icc_markers[seq_no] = NULL; |
c6a2226cc4de
6791502: IIOException "Invalid icc profile" on jpeg after update from JDK5 to JDK6
bae
parents:
2
diff
changeset
|
1236 |
|
2 | 1237 |
|
1238 |
for (marker = cinfo->marker_list; marker != NULL; marker = marker->next) { |
|
1239 |
if (marker_is_icc(marker)) { |
|
1240 |
if (num_markers == 0) |
|
1241 |
num_markers = GETJOCTET(marker->data[13]); |
|
1242 |
else if (num_markers != GETJOCTET(marker->data[13])) { |
|
1243 |
JNU_ThrowByName(env, "javax/imageio/IIOException", |
|
1244 |
"Invalid icc profile: inconsistent num_markers fields"); |
|
1245 |
return NULL; |
|
1246 |
} |
|
1247 |
seq_no = GETJOCTET(marker->data[12]); |
|
2383
c6a2226cc4de
6791502: IIOException "Invalid icc profile" on jpeg after update from JDK5 to JDK6
bae
parents:
2
diff
changeset
|
1248 |
|
c6a2226cc4de
6791502: IIOException "Invalid icc profile" on jpeg after update from JDK5 to JDK6
bae
parents:
2
diff
changeset
|
1249 |
/* Some third-party tools produce images with profile chunk |
c6a2226cc4de
6791502: IIOException "Invalid icc profile" on jpeg after update from JDK5 to JDK6
bae
parents:
2
diff
changeset
|
1250 |
* numeration started from zero. It is inconsistent with ICC |
c6a2226cc4de
6791502: IIOException "Invalid icc profile" on jpeg after update from JDK5 to JDK6
bae
parents:
2
diff
changeset
|
1251 |
* spec, but seems to be recognized by majority of image |
c6a2226cc4de
6791502: IIOException "Invalid icc profile" on jpeg after update from JDK5 to JDK6
bae
parents:
2
diff
changeset
|
1252 |
* processing tools, so we should be more tolerant to this |
c6a2226cc4de
6791502: IIOException "Invalid icc profile" on jpeg after update from JDK5 to JDK6
bae
parents:
2
diff
changeset
|
1253 |
* departure from the spec. |
c6a2226cc4de
6791502: IIOException "Invalid icc profile" on jpeg after update from JDK5 to JDK6
bae
parents:
2
diff
changeset
|
1254 |
*/ |
c6a2226cc4de
6791502: IIOException "Invalid icc profile" on jpeg after update from JDK5 to JDK6
bae
parents:
2
diff
changeset
|
1255 |
if (seq_no < 0 || seq_no > num_markers) { |
2 | 1256 |
JNU_ThrowByName(env, "javax/imageio/IIOException", |
1257 |
"Invalid icc profile: bad sequence number"); |
|
1258 |
return NULL; |
|
1259 |
} |
|
2383
c6a2226cc4de
6791502: IIOException "Invalid icc profile" on jpeg after update from JDK5 to JDK6
bae
parents:
2
diff
changeset
|
1260 |
if (icc_markers[seq_no] != NULL) { |
2 | 1261 |
JNU_ThrowByName(env, "javax/imageio/IIOException", |
1262 |
"Invalid icc profile: duplicate sequence numbers"); |
|
1263 |
return NULL; |
|
1264 |
} |
|
2383
c6a2226cc4de
6791502: IIOException "Invalid icc profile" on jpeg after update from JDK5 to JDK6
bae
parents:
2
diff
changeset
|
1265 |
icc_markers[seq_no] = marker; |
c6a2226cc4de
6791502: IIOException "Invalid icc profile" on jpeg after update from JDK5 to JDK6
bae
parents:
2
diff
changeset
|
1266 |
num_found_markers ++; |
2 | 1267 |
} |
1268 |
} |
|
1269 |
||
1270 |
if (num_markers == 0) |
|
1271 |
return NULL; // There is no profile |
|
1272 |
||
2383
c6a2226cc4de
6791502: IIOException "Invalid icc profile" on jpeg after update from JDK5 to JDK6
bae
parents:
2
diff
changeset
|
1273 |
if (num_markers != num_found_markers) { |
c6a2226cc4de
6791502: IIOException "Invalid icc profile" on jpeg after update from JDK5 to JDK6
bae
parents:
2
diff
changeset
|
1274 |
JNU_ThrowByName(env, "javax/imageio/IIOException", |
c6a2226cc4de
6791502: IIOException "Invalid icc profile" on jpeg after update from JDK5 to JDK6
bae
parents:
2
diff
changeset
|
1275 |
"Invalid icc profile: invalid number of icc markers"); |
c6a2226cc4de
6791502: IIOException "Invalid icc profile" on jpeg after update from JDK5 to JDK6
bae
parents:
2
diff
changeset
|
1276 |
return NULL; |
c6a2226cc4de
6791502: IIOException "Invalid icc profile" on jpeg after update from JDK5 to JDK6
bae
parents:
2
diff
changeset
|
1277 |
} |
c6a2226cc4de
6791502: IIOException "Invalid icc profile" on jpeg after update from JDK5 to JDK6
bae
parents:
2
diff
changeset
|
1278 |
|
c6a2226cc4de
6791502: IIOException "Invalid icc profile" on jpeg after update from JDK5 to JDK6
bae
parents:
2
diff
changeset
|
1279 |
first = icc_markers[0] ? 0 : 1; |
c6a2226cc4de
6791502: IIOException "Invalid icc profile" on jpeg after update from JDK5 to JDK6
bae
parents:
2
diff
changeset
|
1280 |
last = num_found_markers + first; |
c6a2226cc4de
6791502: IIOException "Invalid icc profile" on jpeg after update from JDK5 to JDK6
bae
parents:
2
diff
changeset
|
1281 |
|
c6a2226cc4de
6791502: IIOException "Invalid icc profile" on jpeg after update from JDK5 to JDK6
bae
parents:
2
diff
changeset
|
1282 |
/* Check for missing markers, count total space needed. |
2 | 1283 |
*/ |
1284 |
total_length = 0; |
|
2383
c6a2226cc4de
6791502: IIOException "Invalid icc profile" on jpeg after update from JDK5 to JDK6
bae
parents:
2
diff
changeset
|
1285 |
for (seq_no = first; seq_no < last; seq_no++) { |
c6a2226cc4de
6791502: IIOException "Invalid icc profile" on jpeg after update from JDK5 to JDK6
bae
parents:
2
diff
changeset
|
1286 |
unsigned int length; |
c6a2226cc4de
6791502: IIOException "Invalid icc profile" on jpeg after update from JDK5 to JDK6
bae
parents:
2
diff
changeset
|
1287 |
if (icc_markers[seq_no] == NULL) { |
2 | 1288 |
JNU_ThrowByName(env, "javax/imageio/IIOException", |
1289 |
"Invalid icc profile: missing sequence number"); |
|
1290 |
return NULL; |
|
1291 |
} |
|
2383
c6a2226cc4de
6791502: IIOException "Invalid icc profile" on jpeg after update from JDK5 to JDK6
bae
parents:
2
diff
changeset
|
1292 |
/* check the data length correctness */ |
c6a2226cc4de
6791502: IIOException "Invalid icc profile" on jpeg after update from JDK5 to JDK6
bae
parents:
2
diff
changeset
|
1293 |
length = icc_markers[seq_no]->data_length; |
c6a2226cc4de
6791502: IIOException "Invalid icc profile" on jpeg after update from JDK5 to JDK6
bae
parents:
2
diff
changeset
|
1294 |
if (ICC_OVERHEAD_LEN > length || length > MAX_BYTES_IN_MARKER) { |
c6a2226cc4de
6791502: IIOException "Invalid icc profile" on jpeg after update from JDK5 to JDK6
bae
parents:
2
diff
changeset
|
1295 |
JNU_ThrowByName(env, "javax/imageio/IIOException", |
c6a2226cc4de
6791502: IIOException "Invalid icc profile" on jpeg after update from JDK5 to JDK6
bae
parents:
2
diff
changeset
|
1296 |
"Invalid icc profile: invalid data length"); |
c6a2226cc4de
6791502: IIOException "Invalid icc profile" on jpeg after update from JDK5 to JDK6
bae
parents:
2
diff
changeset
|
1297 |
return NULL; |
c6a2226cc4de
6791502: IIOException "Invalid icc profile" on jpeg after update from JDK5 to JDK6
bae
parents:
2
diff
changeset
|
1298 |
} |
c6a2226cc4de
6791502: IIOException "Invalid icc profile" on jpeg after update from JDK5 to JDK6
bae
parents:
2
diff
changeset
|
1299 |
total_length += (length - ICC_OVERHEAD_LEN); |
2 | 1300 |
} |
1301 |
||
1302 |
if (total_length <= 0) { |
|
1303 |
JNU_ThrowByName(env, "javax/imageio/IIOException", |
|
1304 |
"Invalid icc profile: found only empty markers"); |
|
1305 |
return NULL; |
|
1306 |
} |
|
1307 |
||
1308 |
/* Allocate a Java byte array for assembled data */ |
|
1309 |
||
1310 |
data = (*env)->NewByteArray(env, total_length); |
|
1311 |
if (data == NULL) { |
|
1312 |
JNU_ThrowByName(env, |
|
1313 |
"java/lang/OutOfMemoryError", |
|
1314 |
"Reading ICC profile"); |
|
1315 |
return NULL; |
|
1316 |
} |
|
1317 |
||
1318 |
icc_data = (JOCTET *)(*env)->GetPrimitiveArrayCritical(env, |
|
1319 |
data, |
|
1320 |
NULL); |
|
1321 |
if (icc_data == NULL) { |
|
1322 |
JNU_ThrowByName(env, "javax/imageio/IIOException", |
|
1323 |
"Unable to pin icc profile data array"); |
|
1324 |
return NULL; |
|
1325 |
} |
|
1326 |
||
1327 |
/* and fill it in */ |
|
2383
c6a2226cc4de
6791502: IIOException "Invalid icc profile" on jpeg after update from JDK5 to JDK6
bae
parents:
2
diff
changeset
|
1328 |
dst_ptr = icc_data; |
c6a2226cc4de
6791502: IIOException "Invalid icc profile" on jpeg after update from JDK5 to JDK6
bae
parents:
2
diff
changeset
|
1329 |
for (seq_no = first; seq_no < last; seq_no++) { |
c6a2226cc4de
6791502: IIOException "Invalid icc profile" on jpeg after update from JDK5 to JDK6
bae
parents:
2
diff
changeset
|
1330 |
JOCTET FAR *src_ptr = icc_markers[seq_no]->data + ICC_OVERHEAD_LEN; |
c6a2226cc4de
6791502: IIOException "Invalid icc profile" on jpeg after update from JDK5 to JDK6
bae
parents:
2
diff
changeset
|
1331 |
unsigned int length = |
c6a2226cc4de
6791502: IIOException "Invalid icc profile" on jpeg after update from JDK5 to JDK6
bae
parents:
2
diff
changeset
|
1332 |
icc_markers[seq_no]->data_length - ICC_OVERHEAD_LEN; |
c6a2226cc4de
6791502: IIOException "Invalid icc profile" on jpeg after update from JDK5 to JDK6
bae
parents:
2
diff
changeset
|
1333 |
|
c6a2226cc4de
6791502: IIOException "Invalid icc profile" on jpeg after update from JDK5 to JDK6
bae
parents:
2
diff
changeset
|
1334 |
memcpy(dst_ptr, src_ptr, length); |
c6a2226cc4de
6791502: IIOException "Invalid icc profile" on jpeg after update from JDK5 to JDK6
bae
parents:
2
diff
changeset
|
1335 |
dst_ptr += length; |
2 | 1336 |
} |
1337 |
||
1338 |
/* finally, unpin the array */ |
|
1339 |
(*env)->ReleasePrimitiveArrayCritical(env, |
|
1340 |
data, |
|
1341 |
icc_data, |
|
1342 |
0); |
|
1343 |
||
1344 |
||
1345 |
return data; |
|
1346 |
} |
|
1347 |
||
1348 |
/********************* end of ICC profile support *************/ |
|
1349 |
||
1350 |
/********************* Reader JNI calls ***********************/ |
|
1351 |
||
1352 |
JNIEXPORT void JNICALL |
|
1353 |
Java_com_sun_imageio_plugins_jpeg_JPEGImageReader_initReaderIDs |
|
1354 |
(JNIEnv *env, |
|
1355 |
jclass cls, |
|
1356 |
jclass ImageInputStreamClass, |
|
1357 |
jclass qTableClass, |
|
1358 |
jclass huffClass) { |
|
1359 |
||
1360 |
ImageInputStream_readID = (*env)->GetMethodID(env, |
|
1361 |
ImageInputStreamClass, |
|
1362 |
"read", |
|
1363 |
"([BII)I"); |
|
1364 |
ImageInputStream_skipBytesID = (*env)->GetMethodID(env, |
|
1365 |
ImageInputStreamClass, |
|
1366 |
"skipBytes", |
|
1367 |
"(J)J"); |
|
1368 |
JPEGImageReader_warningOccurredID = (*env)->GetMethodID(env, |
|
1369 |
cls, |
|
1370 |
"warningOccurred", |
|
1371 |
"(I)V"); |
|
1372 |
JPEGImageReader_warningWithMessageID = |
|
1373 |
(*env)->GetMethodID(env, |
|
1374 |
cls, |
|
1375 |
"warningWithMessage", |
|
1376 |
"(Ljava/lang/String;)V"); |
|
1377 |
JPEGImageReader_setImageDataID = (*env)->GetMethodID(env, |
|
1378 |
cls, |
|
1379 |
"setImageData", |
|
1380 |
"(IIIII[B)V"); |
|
1381 |
JPEGImageReader_acceptPixelsID = (*env)->GetMethodID(env, |
|
1382 |
cls, |
|
1383 |
"acceptPixels", |
|
1384 |
"(IZ)V"); |
|
1385 |
JPEGImageReader_passStartedID = (*env)->GetMethodID(env, |
|
1386 |
cls, |
|
1387 |
"passStarted", |
|
1388 |
"(I)V"); |
|
1389 |
JPEGImageReader_passCompleteID = (*env)->GetMethodID(env, |
|
1390 |
cls, |
|
1391 |
"passComplete", |
|
1392 |
"()V"); |
|
1393 |
JPEGImageReader_pushBackID = (*env)->GetMethodID(env, |
|
1394 |
cls, |
|
1395 |
"pushBack", |
|
1396 |
"(I)V"); |
|
1397 |
JPEGQTable_tableID = (*env)->GetFieldID(env, |
|
1398 |
qTableClass, |
|
1399 |
"qTable", |
|
1400 |
"[I"); |
|
1401 |
||
1402 |
JPEGHuffmanTable_lengthsID = (*env)->GetFieldID(env, |
|
1403 |
huffClass, |
|
1404 |
"lengths", |
|
1405 |
"[S"); |
|
1406 |
||
1407 |
JPEGHuffmanTable_valuesID = (*env)->GetFieldID(env, |
|
1408 |
huffClass, |
|
1409 |
"values", |
|
1410 |
"[S"); |
|
1411 |
} |
|
1412 |
||
1413 |
JNIEXPORT jlong JNICALL |
|
1414 |
Java_com_sun_imageio_plugins_jpeg_JPEGImageReader_initJPEGImageReader |
|
1415 |
(JNIEnv *env, |
|
1416 |
jobject this) { |
|
1417 |
||
1418 |
imageIODataPtr ret; |
|
1419 |
struct sun_jpeg_error_mgr *jerr; |
|
1420 |
||
1421 |
/* This struct contains the JPEG decompression parameters and pointers to |
|
1422 |
* working space (which is allocated as needed by the JPEG library). |
|
1423 |
*/ |
|
1424 |
struct jpeg_decompress_struct *cinfo = |
|
1425 |
malloc(sizeof(struct jpeg_decompress_struct)); |
|
1426 |
if (cinfo == NULL) { |
|
1427 |
JNU_ThrowByName( env, |
|
1428 |
"java/lang/OutOfMemoryError", |
|
1429 |
"Initializing Reader"); |
|
1430 |
return 0; |
|
1431 |
} |
|
1432 |
||
1433 |
/* We use our private extension JPEG error handler. |
|
1434 |
*/ |
|
1435 |
jerr = malloc (sizeof(struct sun_jpeg_error_mgr)); |
|
1436 |
if (jerr == NULL) { |
|
1437 |
JNU_ThrowByName( env, |
|
1438 |
"java/lang/OutOfMemoryError", |
|
1439 |
"Initializing Reader"); |
|
1440 |
return 0; |
|
1441 |
} |
|
1442 |
||
1443 |
/* We set up the normal JPEG error routines, then override error_exit. */ |
|
1444 |
cinfo->err = jpeg_std_error(&(jerr->pub)); |
|
1445 |
jerr->pub.error_exit = sun_jpeg_error_exit; |
|
1446 |
/* We need to setup our own print routines */ |
|
1447 |
jerr->pub.output_message = sun_jpeg_output_message; |
|
1448 |
/* Now we can setjmp before every call to the library */ |
|
1449 |
||
1450 |
/* Establish the setjmp return context for sun_jpeg_error_exit to use. */ |
|
1451 |
if (setjmp(jerr->setjmp_buffer)) { |
|
1452 |
/* If we get here, the JPEG code has signaled an error. */ |
|
1453 |
char buffer[JMSG_LENGTH_MAX]; |
|
1454 |
(*cinfo->err->format_message) ((struct jpeg_common_struct *) cinfo, |
|
1455 |
buffer); |
|
1456 |
JNU_ThrowByName(env, "javax/imageio/IIOException", buffer); |
|
1457 |
return 0; |
|
1458 |
} |
|
1459 |
||
1460 |
/* Perform library initialization */ |
|
1461 |
jpeg_create_decompress(cinfo); |
|
1462 |
||
1463 |
// Set up to keep any APP2 markers, as these might contain ICC profile |
|
1464 |
// data |
|
1465 |
jpeg_save_markers(cinfo, ICC_MARKER, 0xFFFF); |
|
1466 |
||
1467 |
/* |
|
1468 |
* Now set up our source. |
|
1469 |
*/ |
|
1470 |
cinfo->src = |
|
1471 |
(struct jpeg_source_mgr *) malloc (sizeof(struct jpeg_source_mgr)); |
|
1472 |
if (cinfo->src == NULL) { |
|
1473 |
JNU_ThrowByName(env, |
|
1474 |
"java/lang/OutOfMemoryError", |
|
1475 |
"Initializing Reader"); |
|
1476 |
return 0; |
|
1477 |
} |
|
1478 |
cinfo->src->bytes_in_buffer = 0; |
|
1479 |
cinfo->src->next_input_byte = NULL; |
|
1480 |
cinfo->src->init_source = imageio_init_source; |
|
1481 |
cinfo->src->fill_input_buffer = imageio_fill_input_buffer; |
|
1482 |
cinfo->src->skip_input_data = imageio_skip_input_data; |
|
1483 |
cinfo->src->resync_to_restart = jpeg_resync_to_restart; // use default |
|
1484 |
cinfo->src->term_source = imageio_term_source; |
|
1485 |
||
1486 |
/* set up the association to persist for future calls */ |
|
1487 |
ret = initImageioData(env, (j_common_ptr) cinfo, this); |
|
1488 |
if (ret == NULL) { |
|
1489 |
JNU_ThrowByName( env, |
|
1490 |
"java/lang/OutOfMemoryError", |
|
1491 |
"Initializing Reader"); |
|
1492 |
return 0; |
|
1493 |
} |
|
1494 |
return (jlong) ret; |
|
1495 |
} |
|
1496 |
||
1497 |
/* |
|
1498 |
* When we set a source from Java, we set up the stream in the streamBuf |
|
1499 |
* object. If there was an old one, it is released first. |
|
1500 |
*/ |
|
1501 |
||
1502 |
JNIEXPORT void JNICALL |
|
1503 |
Java_com_sun_imageio_plugins_jpeg_JPEGImageReader_setSource |
|
1504 |
(JNIEnv *env, |
|
1505 |
jobject this, |
|
1506 |
jlong ptr, |
|
1507 |
jobject source) { |
|
1508 |
||
1509 |
imageIODataPtr data = (imageIODataPtr) ptr; |
|
1510 |
j_common_ptr cinfo; |
|
1511 |
||
1512 |
if (data == NULL) { |
|
1513 |
JNU_ThrowByName(env, |
|
1514 |
"java/lang/IllegalStateException", |
|
1515 |
"Attempting to use reader after dispose()"); |
|
1516 |
return; |
|
1517 |
} |
|
1518 |
||
1519 |
cinfo = data->jpegObj; |
|
1520 |
||
1521 |
imageio_set_stream(env, cinfo, data, source); |
|
1522 |
||
1523 |
imageio_init_source((j_decompress_ptr) cinfo); |
|
1524 |
} |
|
1525 |
||
1526 |
#define JPEG_APP1 (JPEG_APP0 + 1) /* EXIF APP1 marker code */ |
|
1527 |
||
1528 |
/* |
|
1529 |
* For EXIF images, the APP1 will appear immediately after the SOI, |
|
1530 |
* so it's safe to only look at the first marker in the list. |
|
1531 |
* (see http://www.exif.org/Exif2-2.PDF, section 4.7, page 58) |
|
1532 |
*/ |
|
1533 |
#define IS_EXIF(c) \ |
|
1534 |
(((c)->marker_list != NULL) && ((c)->marker_list->marker == JPEG_APP1)) |
|
1535 |
||
1536 |
JNIEXPORT jboolean JNICALL |
|
1537 |
Java_com_sun_imageio_plugins_jpeg_JPEGImageReader_readImageHeader |
|
1538 |
(JNIEnv *env, |
|
1539 |
jobject this, |
|
1540 |
jlong ptr, |
|
1541 |
jboolean clearFirst, |
|
1542 |
jboolean reset) { |
|
1543 |
||
1544 |
int ret; |
|
1545 |
int h_samp0, h_samp1, h_samp2; |
|
1546 |
int v_samp0, v_samp1, v_samp2; |
|
1547 |
jboolean retval = JNI_FALSE; |
|
1548 |
imageIODataPtr data = (imageIODataPtr) ptr; |
|
1549 |
j_decompress_ptr cinfo; |
|
1550 |
struct jpeg_source_mgr *src; |
|
1551 |
sun_jpeg_error_ptr jerr; |
|
2383
c6a2226cc4de
6791502: IIOException "Invalid icc profile" on jpeg after update from JDK5 to JDK6
bae
parents:
2
diff
changeset
|
1552 |
jbyteArray profileData = NULL; |
2 | 1553 |
|
1554 |
if (data == NULL) { |
|
1555 |
JNU_ThrowByName(env, |
|
1556 |
"java/lang/IllegalStateException", |
|
1557 |
"Attempting to use reader after dispose()"); |
|
1558 |
return JNI_FALSE; |
|
1559 |
} |
|
1560 |
||
1561 |
cinfo = (j_decompress_ptr) data->jpegObj; |
|
1562 |
src = cinfo->src; |
|
1563 |
||
1564 |
/* Establish the setjmp return context for sun_jpeg_error_exit to use. */ |
|
1565 |
jerr = (sun_jpeg_error_ptr) cinfo->err; |
|
1566 |
||
1567 |
if (setjmp(jerr->setjmp_buffer)) { |
|
1568 |
/* If we get here, the JPEG code has signaled an error |
|
1569 |
while reading the header. */ |
|
1570 |
RELEASE_ARRAYS(env, data, src->next_input_byte); |
|
1571 |
if (!(*env)->ExceptionOccurred(env)) { |
|
1572 |
char buffer[JMSG_LENGTH_MAX]; |
|
1573 |
(*cinfo->err->format_message) ((struct jpeg_common_struct *) cinfo, |
|
1574 |
buffer); |
|
1575 |
JNU_ThrowByName(env, "javax/imageio/IIOException", buffer); |
|
1576 |
} |
|
1577 |
return retval; |
|
1578 |
} |
|
1579 |
||
2383
c6a2226cc4de
6791502: IIOException "Invalid icc profile" on jpeg after update from JDK5 to JDK6
bae
parents:
2
diff
changeset
|
1580 |
#ifdef DEBUG_IIO_JPEG |
2 | 1581 |
printf("In readImageHeader, data is %p cinfo is %p\n", data, cinfo); |
1582 |
printf("clearFirst is %d\n", clearFirst); |
|
1583 |
#endif |
|
1584 |
||
1585 |
if (GET_ARRAYS(env, data, &src->next_input_byte) == NOT_OK) { |
|
1586 |
JNU_ThrowByName(env, |
|
1587 |
"javax/imageio/IIOException", |
|
1588 |
"Array pin failed"); |
|
1589 |
return retval; |
|
1590 |
} |
|
1591 |
||
1592 |
/* |
|
1593 |
* Now clear the input buffer if the Java code has done a seek |
|
1594 |
* on the stream since the last call, invalidating any buffer contents. |
|
1595 |
*/ |
|
1596 |
if (clearFirst) { |
|
1597 |
clearStreamBuffer(&data->streamBuf); |
|
1598 |
src->next_input_byte = NULL; |
|
1599 |
src->bytes_in_buffer = 0; |
|
1600 |
} |
|
1601 |
||
1602 |
ret = jpeg_read_header(cinfo, FALSE); |
|
1603 |
||
1604 |
if (ret == JPEG_HEADER_TABLES_ONLY) { |
|
1605 |
retval = JNI_TRUE; |
|
1606 |
imageio_term_source(cinfo); // Pushback remaining buffer contents |
|
2383
c6a2226cc4de
6791502: IIOException "Invalid icc profile" on jpeg after update from JDK5 to JDK6
bae
parents:
2
diff
changeset
|
1607 |
#ifdef DEBUG_IIO_JPEG |
2 | 1608 |
printf("just read tables-only image; q table 0 at %p\n", |
1609 |
cinfo->quant_tbl_ptrs[0]); |
|
1610 |
#endif |
|
1611 |
RELEASE_ARRAYS(env, data, src->next_input_byte); |
|
1612 |
} else { |
|
1613 |
/* |
|
1614 |
* Now adjust the jpeg_color_space variable, which was set in |
|
1615 |
* default_decompress_parms, to reflect our differences from IJG |
|
1616 |
*/ |
|
1617 |
||
1618 |
switch (cinfo->jpeg_color_space) { |
|
1619 |
default : |
|
1620 |
break; |
|
1621 |
case JCS_YCbCr: |
|
1622 |
||
1623 |
/* |
|
1624 |
* There are several possibilities: |
|
1625 |
* - we got image with embeded colorspace |
|
1626 |
* Use it. User knows what he is doing. |
|
1627 |
* - we got JFIF image |
|
1628 |
* Must be YCbCr (see http://www.w3.org/Graphics/JPEG/jfif3.pdf, page 2) |
|
1629 |
* - we got EXIF image |
|
1630 |
* Must be YCbCr (see http://www.exif.org/Exif2-2.PDF, section 4.7, page 63) |
|
1631 |
* - something else |
|
1632 |
* Apply heuristical rules to identify actual colorspace. |
|
1633 |
*/ |
|
1634 |
||
1635 |
if (cinfo->saw_Adobe_marker) { |
|
1636 |
if (cinfo->Adobe_transform != 1) { |
|
1637 |
/* |
|
1638 |
* IJG guesses this is YCbCr and emits a warning |
|
1639 |
* We would rather not guess. Then the user knows |
|
1640 |
* To read this as a Raster if at all |
|
1641 |
*/ |
|
1642 |
cinfo->jpeg_color_space = JCS_UNKNOWN; |
|
1643 |
cinfo->out_color_space = JCS_UNKNOWN; |
|
1644 |
} |
|
1645 |
} else if (!cinfo->saw_JFIF_marker && !IS_EXIF(cinfo)) { |
|
1646 |
/* |
|
1647 |
* IJG assumes all unidentified 3-channels are YCbCr. |
|
1648 |
* We assume that only if the second two channels are |
|
1649 |
* subsampled (either horizontally or vertically). If not, |
|
1650 |
* we assume RGB. |
|
1651 |
* |
|
1652 |
* 4776576: Some digital cameras output YCbCr JPEG images |
|
1653 |
* that do not contain a JFIF APP0 marker but are only |
|
1654 |
* vertically subsampled (no horizontal subsampling). |
|
1655 |
* We should only assume this is RGB data if the subsampling |
|
1656 |
* factors for the second two channels are the same as the |
|
1657 |
* first (check both horizontal and vertical factors). |
|
1658 |
*/ |
|
1659 |
h_samp0 = cinfo->comp_info[0].h_samp_factor; |
|
1660 |
h_samp1 = cinfo->comp_info[1].h_samp_factor; |
|
1661 |
h_samp2 = cinfo->comp_info[2].h_samp_factor; |
|
1662 |
||
1663 |
v_samp0 = cinfo->comp_info[0].v_samp_factor; |
|
1664 |
v_samp1 = cinfo->comp_info[1].v_samp_factor; |
|
1665 |
v_samp2 = cinfo->comp_info[2].v_samp_factor; |
|
1666 |
||
1667 |
if ((h_samp1 == h_samp0) && (h_samp2 == h_samp0) && |
|
1668 |
(v_samp1 == v_samp0) && (v_samp2 == v_samp0)) |
|
1669 |
{ |
|
1670 |
cinfo->jpeg_color_space = JCS_RGB; |
|
1671 |
/* output is already RGB, so it stays the same */ |
|
1672 |
} |
|
1673 |
} |
|
1674 |
break; |
|
1675 |
#ifdef YCCALPHA |
|
1676 |
case JCS_YCC: |
|
1677 |
cinfo->out_color_space = JCS_YCC; |
|
1678 |
break; |
|
1679 |
#endif |
|
1680 |
case JCS_YCCK: |
|
1681 |
if ((cinfo->saw_Adobe_marker) && (cinfo->Adobe_transform != 2)) { |
|
1682 |
/* |
|
1683 |
* IJG guesses this is YCCK and emits a warning |
|
1684 |
* We would rather not guess. Then the user knows |
|
1685 |
* To read this as a Raster if at all |
|
1686 |
*/ |
|
1687 |
cinfo->jpeg_color_space = JCS_UNKNOWN; |
|
1688 |
cinfo->out_color_space = JCS_UNKNOWN; |
|
1689 |
} |
|
1690 |
break; |
|
1691 |
case JCS_CMYK: |
|
1692 |
/* |
|
1693 |
* IJG assumes all unidentified 4-channels are CMYK. |
|
1694 |
* We assume that only if the second two channels are |
|
1695 |
* not subsampled (either horizontally or vertically). |
|
1696 |
* If they are, we assume YCCK. |
|
1697 |
*/ |
|
1698 |
h_samp0 = cinfo->comp_info[0].h_samp_factor; |
|
1699 |
h_samp1 = cinfo->comp_info[1].h_samp_factor; |
|
1700 |
h_samp2 = cinfo->comp_info[2].h_samp_factor; |
|
1701 |
||
1702 |
v_samp0 = cinfo->comp_info[0].v_samp_factor; |
|
1703 |
v_samp1 = cinfo->comp_info[1].v_samp_factor; |
|
1704 |
v_samp2 = cinfo->comp_info[2].v_samp_factor; |
|
1705 |
||
1706 |
if ((h_samp1 > h_samp0) && (h_samp2 > h_samp0) || |
|
1707 |
(v_samp1 > v_samp0) && (v_samp2 > v_samp0)) |
|
1708 |
{ |
|
1709 |
cinfo->jpeg_color_space = JCS_YCCK; |
|
1710 |
/* Leave the output space as CMYK */ |
|
1711 |
} |
|
1712 |
} |
|
1713 |
RELEASE_ARRAYS(env, data, src->next_input_byte); |
|
2383
c6a2226cc4de
6791502: IIOException "Invalid icc profile" on jpeg after update from JDK5 to JDK6
bae
parents:
2
diff
changeset
|
1714 |
|
c6a2226cc4de
6791502: IIOException "Invalid icc profile" on jpeg after update from JDK5 to JDK6
bae
parents:
2
diff
changeset
|
1715 |
/* read icc profile data */ |
c6a2226cc4de
6791502: IIOException "Invalid icc profile" on jpeg after update from JDK5 to JDK6
bae
parents:
2
diff
changeset
|
1716 |
profileData = read_icc_profile(env, cinfo); |
c6a2226cc4de
6791502: IIOException "Invalid icc profile" on jpeg after update from JDK5 to JDK6
bae
parents:
2
diff
changeset
|
1717 |
|
c6a2226cc4de
6791502: IIOException "Invalid icc profile" on jpeg after update from JDK5 to JDK6
bae
parents:
2
diff
changeset
|
1718 |
if ((*env)->ExceptionCheck(env)) { |
c6a2226cc4de
6791502: IIOException "Invalid icc profile" on jpeg after update from JDK5 to JDK6
bae
parents:
2
diff
changeset
|
1719 |
return retval; |
c6a2226cc4de
6791502: IIOException "Invalid icc profile" on jpeg after update from JDK5 to JDK6
bae
parents:
2
diff
changeset
|
1720 |
} |
c6a2226cc4de
6791502: IIOException "Invalid icc profile" on jpeg after update from JDK5 to JDK6
bae
parents:
2
diff
changeset
|
1721 |
|
2 | 1722 |
(*env)->CallVoidMethod(env, this, |
1723 |
JPEGImageReader_setImageDataID, |
|
1724 |
cinfo->image_width, |
|
1725 |
cinfo->image_height, |
|
1726 |
cinfo->jpeg_color_space, |
|
1727 |
cinfo->out_color_space, |
|
1728 |
cinfo->num_components, |
|
2383
c6a2226cc4de
6791502: IIOException "Invalid icc profile" on jpeg after update from JDK5 to JDK6
bae
parents:
2
diff
changeset
|
1729 |
profileData); |
2 | 1730 |
if (reset) { |
1731 |
jpeg_abort_decompress(cinfo); |
|
1732 |
} |
|
1733 |
} |
|
1734 |
||
1735 |
return retval; |
|
1736 |
} |
|
1737 |
||
1738 |
||
1739 |
JNIEXPORT void JNICALL |
|
1740 |
Java_com_sun_imageio_plugins_jpeg_JPEGImageReader_setOutColorSpace |
|
1741 |
(JNIEnv *env, |
|
1742 |
jobject this, |
|
1743 |
jlong ptr, |
|
1744 |
jint code) { |
|
1745 |
||
1746 |
imageIODataPtr data = (imageIODataPtr) ptr; |
|
1747 |
j_decompress_ptr cinfo; |
|
1748 |
||
1749 |
if (data == NULL) { |
|
1750 |
JNU_ThrowByName(env, |
|
1751 |
"java/lang/IllegalStateException", |
|
1752 |
"Attempting to use reader after dispose()"); |
|
1753 |
return; |
|
1754 |
} |
|
1755 |
||
1756 |
cinfo = (j_decompress_ptr) data->jpegObj; |
|
1757 |
||
1758 |
cinfo->out_color_space = code; |
|
1759 |
||
1760 |
} |
|
1761 |
||
1762 |
JNIEXPORT jboolean JNICALL |
|
1763 |
Java_com_sun_imageio_plugins_jpeg_JPEGImageReader_readImage |
|
1764 |
(JNIEnv *env, |
|
1765 |
jobject this, |
|
1766 |
jlong ptr, |
|
1767 |
jbyteArray buffer, |
|
1768 |
jint numBands, |
|
1769 |
jintArray srcBands, |
|
1770 |
jintArray bandSizes, |
|
1771 |
jint sourceXStart, |
|
1772 |
jint sourceYStart, |
|
1773 |
jint sourceWidth, |
|
1774 |
jint sourceHeight, |
|
1775 |
jint stepX, |
|
1776 |
jint stepY, |
|
1777 |
jobjectArray qtables, |
|
1778 |
jobjectArray DCHuffmanTables, |
|
1779 |
jobjectArray ACHuffmanTables, |
|
1780 |
jint minProgressivePass, // Counts from 0 |
|
1781 |
jint maxProgressivePass, |
|
1782 |
jboolean wantUpdates) { |
|
1783 |
||
1784 |
||
1785 |
struct jpeg_source_mgr *src; |
|
1786 |
JSAMPROW scanLinePtr; |
|
1787 |
jint bands[MAX_BANDS]; |
|
1788 |
int i, j; |
|
1789 |
jint *body; |
|
1790 |
int scanlineLimit; |
|
1791 |
int pixelStride; |
|
1792 |
unsigned char *in, *out, *pixelLimit; |
|
1793 |
int targetLine; |
|
1794 |
int skipLines, linesLeft; |
|
1795 |
pixelBufferPtr pb; |
|
1796 |
sun_jpeg_error_ptr jerr; |
|
1797 |
boolean done; |
|
1798 |
jint *bandSize; |
|
1799 |
int maxBandValue, halfMaxBandValue; |
|
1800 |
boolean mustScale = FALSE; |
|
1801 |
boolean progressive = FALSE; |
|
1802 |
boolean orderedBands = TRUE; |
|
1803 |
imageIODataPtr data = (imageIODataPtr) ptr; |
|
1804 |
j_decompress_ptr cinfo; |
|
1805 |
||
1806 |
/* verify the inputs */ |
|
1807 |
||
1808 |
if (data == NULL) { |
|
1809 |
JNU_ThrowByName(env, |
|
1810 |
"java/lang/IllegalStateException", |
|
1811 |
"Attempting to use reader after dispose()"); |
|
1812 |
return JNI_FALSE; |
|
1813 |
} |
|
1814 |
||
1815 |
if ((buffer == NULL) || (srcBands == NULL)) { |
|
1816 |
JNU_ThrowNullPointerException(env, 0); |
|
1817 |
return JNI_FALSE; |
|
1818 |
} |
|
1819 |
||
1820 |
cinfo = (j_decompress_ptr) data->jpegObj; |
|
1821 |
||
1822 |
if ((numBands < 1) || (numBands > cinfo->num_components) || |
|
1823 |
(sourceXStart < 0) || (sourceXStart >= (jint)cinfo->image_width) || |
|
1824 |
(sourceYStart < 0) || (sourceYStart >= (jint)cinfo->image_height) || |
|
1825 |
(sourceWidth < 1) || (sourceWidth > (jint)cinfo->image_width) || |
|
1826 |
(sourceHeight < 1) || (sourceHeight > (jint)cinfo->image_height) || |
|
1827 |
(stepX < 1) || (stepY < 1) || |
|
1828 |
(minProgressivePass < 0) || |
|
1829 |
(maxProgressivePass < minProgressivePass)) |
|
1830 |
{ |
|
1831 |
JNU_ThrowByName(env, "javax/imageio/IIOException", |
|
1832 |
"Invalid argument to native readImage"); |
|
1833 |
return JNI_FALSE; |
|
1834 |
} |
|
1835 |
||
1836 |
/* |
|
1837 |
* First get the source bands array and copy it to our local array |
|
1838 |
* so we don't have to worry about pinning and unpinning it again. |
|
1839 |
*/ |
|
1840 |
||
1841 |
body = (*env)->GetIntArrayElements(env, srcBands, NULL); |
|
1842 |
if (body == NULL) { |
|
1843 |
JNU_ThrowByName( env, |
|
1844 |
"java/lang/OutOfMemoryError", |
|
1845 |
"Initializing Read"); |
|
1846 |
return JNI_FALSE; |
|
1847 |
} |
|
1848 |
||
1849 |
for (i = 0; i < numBands; i++) { |
|
1850 |
bands[i] = body[i]; |
|
1851 |
if (orderedBands && (bands[i] != i)) { |
|
1852 |
orderedBands = FALSE; |
|
1853 |
} |
|
1854 |
} |
|
1855 |
||
1856 |
(*env)->ReleaseIntArrayElements(env, srcBands, body, JNI_ABORT); |
|
1857 |
||
2383
c6a2226cc4de
6791502: IIOException "Invalid icc profile" on jpeg after update from JDK5 to JDK6
bae
parents:
2
diff
changeset
|
1858 |
#ifdef DEBUG_IIO_JPEG |
2 | 1859 |
printf("---- in reader.read ----\n"); |
1860 |
printf("numBands is %d\n", numBands); |
|
1861 |
printf("bands array: "); |
|
1862 |
for (i = 0; i < numBands; i++) { |
|
1863 |
printf("%d ", bands[i]); |
|
1864 |
} |
|
1865 |
printf("\n"); |
|
1866 |
printf("jq table 0 at %p\n", |
|
1867 |
cinfo->quant_tbl_ptrs[0]); |
|
1868 |
#endif |
|
1869 |
||
1870 |
data = (imageIODataPtr) cinfo->client_data; |
|
1871 |
src = cinfo->src; |
|
1872 |
||
1873 |
/* Set the buffer as our PixelBuffer */ |
|
1874 |
pb = &data->pixelBuf; |
|
1875 |
||
1876 |
if (setPixelBuffer(env, pb, buffer) == NOT_OK) { |
|
1877 |
return data->abortFlag; // We already threw an out of memory exception |
|
1878 |
} |
|
1879 |
||
1880 |
// Allocate a 1-scanline buffer |
|
1881 |
scanLinePtr = (JSAMPROW)malloc(cinfo->image_width*cinfo->num_components); |
|
1882 |
if (scanLinePtr == NULL) { |
|
1883 |
RELEASE_ARRAYS(env, data, src->next_input_byte); |
|
1884 |
JNU_ThrowByName( env, |
|
1885 |
"java/lang/OutOfMemoryError", |
|
1886 |
"Reading JPEG Stream"); |
|
1887 |
return data->abortFlag; |
|
1888 |
} |
|
1889 |
||
1890 |
/* Establish the setjmp return context for sun_jpeg_error_exit to use. */ |
|
1891 |
jerr = (sun_jpeg_error_ptr) cinfo->err; |
|
1892 |
||
1893 |
if (setjmp(jerr->setjmp_buffer)) { |
|
1894 |
/* If we get here, the JPEG code has signaled an error |
|
1895 |
while reading. */ |
|
1896 |
RELEASE_ARRAYS(env, data, src->next_input_byte); |
|
1897 |
if (!(*env)->ExceptionOccurred(env)) { |
|
1898 |
char buffer[JMSG_LENGTH_MAX]; |
|
1899 |
(*cinfo->err->format_message) ((struct jpeg_common_struct *) cinfo, |
|
1900 |
buffer); |
|
1901 |
JNU_ThrowByName(env, "javax/imageio/IIOException", buffer); |
|
1902 |
} |
|
1903 |
free(scanLinePtr); |
|
1904 |
return data->abortFlag; |
|
1905 |
} |
|
1906 |
||
1907 |
if (GET_ARRAYS(env, data, &src->next_input_byte) == NOT_OK) { |
|
1908 |
JNU_ThrowByName(env, |
|
1909 |
"javax/imageio/IIOException", |
|
1910 |
"Array pin failed"); |
|
1911 |
return data->abortFlag; |
|
1912 |
} |
|
1913 |
||
1914 |
// If there are no tables in our structure and table arguments aren't |
|
1915 |
// NULL, use the table arguments. |
|
1916 |
if ((qtables != NULL) && (cinfo->quant_tbl_ptrs[0] == NULL)) { |
|
1917 |
(void) setQTables(env, (j_common_ptr) cinfo, qtables, TRUE); |
|
1918 |
} |
|
1919 |
||
1920 |
if ((DCHuffmanTables != NULL) && (cinfo->dc_huff_tbl_ptrs[0] == NULL)) { |
|
1921 |
setHTables(env, (j_common_ptr) cinfo, |
|
1922 |
DCHuffmanTables, |
|
1923 |
ACHuffmanTables, |
|
1924 |
TRUE); |
|
1925 |
} |
|
1926 |
||
1927 |
progressive = jpeg_has_multiple_scans(cinfo); |
|
1928 |
if (progressive) { |
|
1929 |
cinfo->buffered_image = TRUE; |
|
1930 |
cinfo->input_scan_number = minProgressivePass+1; // Java count from 0 |
|
1931 |
#define MAX_JAVA_INT 2147483647 // XXX Is this defined in JNI somewhere? |
|
1932 |
if (maxProgressivePass < MAX_JAVA_INT) { |
|
1933 |
maxProgressivePass++; // For testing |
|
1934 |
} |
|
1935 |
} |
|
1936 |
||
1937 |
data->streamBuf.suspendable = FALSE; |
|
1938 |
||
1939 |
jpeg_start_decompress(cinfo); |
|
1940 |
||
1941 |
// loop over progressive passes |
|
1942 |
done = FALSE; |
|
1943 |
while (!done) { |
|
1944 |
if (progressive) { |
|
1945 |
// initialize the next pass. Note that this skips up to |
|
1946 |
// the first interesting pass. |
|
1947 |
jpeg_start_output(cinfo, cinfo->input_scan_number); |
|
1948 |
if (wantUpdates) { |
|
1949 |
(*env)->CallVoidMethod(env, this, |
|
1950 |
JPEGImageReader_passStartedID, |
|
1951 |
cinfo->input_scan_number-1); |
|
1952 |
} |
|
1953 |
} else if (wantUpdates) { |
|
1954 |
(*env)->CallVoidMethod(env, this, |
|
1955 |
JPEGImageReader_passStartedID, |
|
1956 |
0); |
|
1957 |
||
1958 |
} |
|
1959 |
||
1960 |
// Skip until the first interesting line |
|
1961 |
while ((data->abortFlag == JNI_FALSE) |
|
1962 |
&& ((jint)cinfo->output_scanline < sourceYStart)) { |
|
1963 |
jpeg_read_scanlines(cinfo, &scanLinePtr, 1); |
|
1964 |
} |
|
1965 |
||
1966 |
scanlineLimit = sourceYStart+sourceHeight; |
|
1967 |
pixelLimit = scanLinePtr |
|
1968 |
+(sourceXStart+sourceWidth)*cinfo->num_components; |
|
1969 |
||
1970 |
pixelStride = stepX*cinfo->num_components; |
|
1971 |
targetLine = 0; |
|
1972 |
||
1973 |
while ((data->abortFlag == JNI_FALSE) |
|
1974 |
&& ((jint)cinfo->output_scanline < scanlineLimit)) { |
|
1975 |
||
1976 |
jpeg_read_scanlines(cinfo, &scanLinePtr, 1); |
|
1977 |
||
1978 |
// Now mangle it into our buffer |
|
1979 |
out = data->pixelBuf.buf.bp; |
|
1980 |
||
1981 |
if (orderedBands && (pixelStride == numBands)) { |
|
1982 |
// Optimization: The component bands are ordered sequentially, |
|
1983 |
// so we can simply use memcpy() to copy the intermediate |
|
1984 |
// scanline buffer into the raster. |
|
1985 |
in = scanLinePtr + (sourceXStart * cinfo->num_components); |
|
1986 |
if (pixelLimit > in) { |
|
1987 |
memcpy(out, in, pixelLimit - in); |
|
1988 |
} |
|
1989 |
} else { |
|
1990 |
for (in = scanLinePtr+sourceXStart*cinfo->num_components; |
|
1991 |
in < pixelLimit; |
|
1992 |
in += pixelStride) { |
|
1993 |
for (i = 0; i < numBands; i++) { |
|
1994 |
*out++ = *(in+bands[i]); |
|
1995 |
} |
|
1996 |
} |
|
1997 |
} |
|
1998 |
||
1999 |
// And call it back to Java |
|
2000 |
RELEASE_ARRAYS(env, data, src->next_input_byte); |
|
2001 |
(*env)->CallVoidMethod(env, |
|
2002 |
this, |
|
2003 |
JPEGImageReader_acceptPixelsID, |
|
2004 |
targetLine++, |
|
2005 |
progressive); |
|
2006 |
||
2007 |
if ((*env)->ExceptionOccurred(env) |
|
2008 |
|| !GET_ARRAYS(env, data, &(src->next_input_byte))) { |
|
2009 |
cinfo->err->error_exit((j_common_ptr) cinfo); |
|
2010 |
} |
|
2011 |
||
2012 |
// And skip over uninteresting lines to the next subsampled line |
|
2013 |
// Ensure we don't go past the end of the image |
|
2014 |
||
2015 |
// Lines to skip based on subsampling |
|
2016 |
skipLines = stepY - 1; |
|
2017 |
// Lines left in the image |
|
2018 |
linesLeft = scanlineLimit - cinfo->output_scanline; |
|
2019 |
// Take the minimum |
|
2020 |
if (skipLines > linesLeft) { |
|
2021 |
skipLines = linesLeft; |
|
2022 |
} |
|
2023 |
for(i = 0; i < skipLines; i++) { |
|
2024 |
jpeg_read_scanlines(cinfo, &scanLinePtr, 1); |
|
2025 |
} |
|
2026 |
} |
|
2027 |
if (progressive) { |
|
2028 |
jpeg_finish_output(cinfo); // Increments pass counter |
|
2029 |
// Call Java to notify pass complete |
|
2030 |
if (jpeg_input_complete(cinfo) |
|
2031 |
|| (cinfo->input_scan_number > maxProgressivePass)) { |
|
2032 |
done = TRUE; |
|
2033 |
} |
|
2034 |
} else { |
|
2035 |
done = TRUE; |
|
2036 |
} |
|
2037 |
if (wantUpdates) { |
|
2038 |
(*env)->CallVoidMethod(env, this, |
|
2039 |
JPEGImageReader_passCompleteID); |
|
2040 |
} |
|
2041 |
||
2042 |
} |
|
2043 |
/* |
|
2044 |
* We are done, but we might not have read all the lines, or all |
|
2045 |
* the passes, so use jpeg_abort instead of jpeg_finish_decompress. |
|
2046 |
*/ |
|
2047 |
if (cinfo->output_scanline == cinfo->output_height) { |
|
2048 |
// if ((cinfo->output_scanline == cinfo->output_height) && |
|
2049 |
//(jpeg_input_complete(cinfo))) { // We read the whole file |
|
2050 |
jpeg_finish_decompress(cinfo); |
|
2051 |
} else { |
|
2052 |
jpeg_abort_decompress(cinfo); |
|
2053 |
} |
|
2054 |
||
2055 |
free(scanLinePtr); |
|
2056 |
||
2057 |
RELEASE_ARRAYS(env, data, src->next_input_byte); |
|
2058 |
||
2059 |
return data->abortFlag; |
|
2060 |
} |
|
2061 |
||
2062 |
JNIEXPORT void JNICALL |
|
2063 |
Java_com_sun_imageio_plugins_jpeg_JPEGImageReader_abortRead |
|
2064 |
(JNIEnv *env, |
|
2065 |
jobject this, |
|
2066 |
jlong ptr) { |
|
2067 |
||
2068 |
imageIODataPtr data = (imageIODataPtr) ptr; |
|
2069 |
||
2070 |
if (data == NULL) { |
|
2071 |
JNU_ThrowByName(env, |
|
2072 |
"java/lang/IllegalStateException", |
|
2073 |
"Attempting to use reader after dispose()"); |
|
2074 |
return; |
|
2075 |
} |
|
2076 |
||
2077 |
imageio_abort(env, this, data); |
|
2078 |
||
2079 |
} |
|
2080 |
||
2081 |
JNIEXPORT void JNICALL |
|
2082 |
Java_com_sun_imageio_plugins_jpeg_JPEGImageReader_resetLibraryState |
|
2083 |
(JNIEnv *env, |
|
2084 |
jobject this, |
|
2085 |
jlong ptr) { |
|
2086 |
imageIODataPtr data = (imageIODataPtr) ptr; |
|
2087 |
j_decompress_ptr cinfo; |
|
2088 |
||
2089 |
if (data == NULL) { |
|
2090 |
JNU_ThrowByName(env, |
|
2091 |
"java/lang/IllegalStateException", |
|
2092 |
"Attempting to use reader after dispose()"); |
|
2093 |
return; |
|
2094 |
} |
|
2095 |
||
2096 |
cinfo = (j_decompress_ptr) data->jpegObj; |
|
2097 |
||
2098 |
jpeg_abort_decompress(cinfo); |
|
2099 |
} |
|
2100 |
||
2101 |
||
2102 |
JNIEXPORT void JNICALL |
|
2103 |
Java_com_sun_imageio_plugins_jpeg_JPEGImageReader_resetReader |
|
2104 |
(JNIEnv *env, |
|
2105 |
jobject this, |
|
2106 |
jlong ptr) { |
|
2107 |
||
2108 |
imageIODataPtr data = (imageIODataPtr) ptr; |
|
2109 |
j_decompress_ptr cinfo; |
|
2110 |
sun_jpeg_error_ptr jerr; |
|
2111 |
||
2112 |
if (data == NULL) { |
|
2113 |
JNU_ThrowByName(env, |
|
2114 |
"java/lang/IllegalStateException", |
|
2115 |
"Attempting to use reader after dispose()"); |
|
2116 |
return; |
|
2117 |
} |
|
2118 |
||
2119 |
cinfo = (j_decompress_ptr) data->jpegObj; |
|
2120 |
||
2121 |
jerr = (sun_jpeg_error_ptr) cinfo->err; |
|
2122 |
||
2123 |
imageio_reset(env, (j_common_ptr) cinfo, data); |
|
2124 |
||
2125 |
/* |
|
2126 |
* The tables have not been reset, and there is no way to do so |
|
2127 |
* in IJG without leaking memory. The only situation in which |
|
2128 |
* this will cause a problem is if an image-only stream is read |
|
2129 |
* with this object without initializing the correct tables first. |
|
2130 |
* This situation, which should cause an error, might work but |
|
2131 |
* produce garbage instead. If the huffman tables are wrong, |
|
2132 |
* it will fail during the decode. If the q tables are wrong, it |
|
2133 |
* will look strange. This is very unlikely, so don't worry about |
|
2134 |
* it. To be really robust, we would keep a flag for table state |
|
2135 |
* and consult it to catch exceptional situations. |
|
2136 |
*/ |
|
2137 |
||
2138 |
/* above does not clean up the source, so we have to */ |
|
2139 |
||
2140 |
/* |
|
2141 |
We need to explicitly initialize exception handler or we may |
|
2142 |
longjump to random address from the term_source() |
|
2143 |
*/ |
|
2144 |
||
2145 |
if (setjmp(jerr->setjmp_buffer)) { |
|
2146 |
||
2147 |
/* |
|
2148 |
We may get IOException from pushBack() here. |
|
2149 |
||
2150 |
However it could be legal if original input stream was closed |
|
2151 |
earlier (for example because network connection was closed). |
|
2152 |
Unfortunately, image inputstream API has no way to check whether |
|
2153 |
stream is already closed or IOException was thrown because of some |
|
2154 |
other IO problem, |
|
2155 |
And we can not avoid call to pushBack() on closed stream for the |
|
2156 |
same reason. |
|
2157 |
||
2158 |
So, for now we will silently eat this exception. |
|
2159 |
||
2160 |
NB: this may be changed in future when ImageInputStream API will |
|
2161 |
become more flexible. |
|
2162 |
*/ |
|
2163 |
||
2164 |
if ((*env)->ExceptionOccurred(env)) { |
|
2165 |
(*env)->ExceptionClear(env); |
|
2166 |
} |
|
2167 |
} else { |
|
2168 |
cinfo->src->term_source(cinfo); |
|
2169 |
} |
|
2170 |
||
2171 |
cinfo->src->bytes_in_buffer = 0; |
|
2172 |
cinfo->src->next_input_byte = NULL; |
|
2173 |
} |
|
2174 |
||
2175 |
JNIEXPORT void JNICALL |
|
2176 |
Java_com_sun_imageio_plugins_jpeg_JPEGImageReader_disposeReader |
|
2177 |
(JNIEnv *env, |
|
2178 |
jclass reader, |
|
2179 |
jlong ptr) { |
|
2180 |
||
2181 |
imageIODataPtr data = (imageIODataPtr) ptr; |
|
2182 |
j_common_ptr info = destroyImageioData(env, data); |
|
2183 |
||
2184 |
imageio_dispose(info); |
|
2185 |
} |
|
2186 |
||
2187 |
/********************** end of Reader *************************/ |
|
2188 |
||
2189 |
/********************** Writer Support ************************/ |
|
2190 |
||
2191 |
/********************** Destination Manager *******************/ |
|
2192 |
||
2193 |
METHODDEF(void) |
|
2194 |
/* |
|
2195 |
* Initialize destination --- called by jpeg_start_compress |
|
2196 |
* before any data is actually written. The data arrays |
|
2197 |
* must be pinned before this is called. |
|
2198 |
*/ |
|
2199 |
imageio_init_destination (j_compress_ptr cinfo) |
|
2200 |
{ |
|
2201 |
struct jpeg_destination_mgr *dest = cinfo->dest; |
|
2202 |
imageIODataPtr data = (imageIODataPtr) cinfo->client_data; |
|
2203 |
streamBufferPtr sb = &data->streamBuf; |
|
2204 |
JNIEnv *env = (JNIEnv *)JNU_GetEnv(jvm, JNI_VERSION_1_2); |
|
2205 |
||
2206 |
if (sb->buf == NULL) { |
|
2207 |
// We forgot to pin the array |
|
2208 |
(*env)->FatalError(env, "Output buffer not pinned!"); |
|
2209 |
} |
|
2210 |
||
2211 |
dest->next_output_byte = sb->buf; |
|
2212 |
dest->free_in_buffer = sb->bufferLength; |
|
2213 |
} |
|
2214 |
||
2215 |
/* |
|
2216 |
* Empty the output buffer --- called whenever buffer fills up. |
|
2217 |
* |
|
2218 |
* This routine writes the entire output buffer |
|
2219 |
* (ignoring the current state of next_output_byte & free_in_buffer), |
|
2220 |
* resets the pointer & count to the start of the buffer, and returns TRUE |
|
2221 |
* indicating that the buffer has been dumped. |
|
2222 |
*/ |
|
2223 |
||
2224 |
METHODDEF(boolean) |
|
2225 |
imageio_empty_output_buffer (j_compress_ptr cinfo) |
|
2226 |
{ |
|
2227 |
struct jpeg_destination_mgr *dest = cinfo->dest; |
|
2228 |
imageIODataPtr data = (imageIODataPtr) cinfo->client_data; |
|
2229 |
streamBufferPtr sb = &data->streamBuf; |
|
2230 |
JNIEnv *env = (JNIEnv *)JNU_GetEnv(jvm, JNI_VERSION_1_2); |
|
2231 |
||
2232 |
RELEASE_ARRAYS(env, data, (const JOCTET *)(dest->next_output_byte)); |
|
2233 |
||
2234 |
(*env)->CallVoidMethod(env, |
|
2235 |
sb->stream, |
|
2236 |
ImageOutputStream_writeID, |
|
2237 |
sb->hstreamBuffer, |
|
2238 |
0, |
|
2239 |
sb->bufferLength); |
|
2240 |
if ((*env)->ExceptionOccurred(env) |
|
2241 |
|| !GET_ARRAYS(env, data, |
|
2242 |
(const JOCTET **)(&dest->next_output_byte))) { |
|
2243 |
cinfo->err->error_exit((j_common_ptr) cinfo); |
|
2244 |
} |
|
2245 |
||
2246 |
dest->next_output_byte = sb->buf; |
|
2247 |
dest->free_in_buffer = sb->bufferLength; |
|
2248 |
||
2249 |
return TRUE; |
|
2250 |
} |
|
2251 |
||
2252 |
/* |
|
2253 |
* After all of the data has been encoded there may still be some |
|
2254 |
* more left over in some of the working buffers. Now is the |
|
2255 |
* time to clear them out. |
|
2256 |
*/ |
|
2257 |
METHODDEF(void) |
|
2258 |
imageio_term_destination (j_compress_ptr cinfo) |
|
2259 |
{ |
|
2260 |
struct jpeg_destination_mgr *dest = cinfo->dest; |
|
2261 |
imageIODataPtr data = (imageIODataPtr) cinfo->client_data; |
|
2262 |
streamBufferPtr sb = &data->streamBuf; |
|
2263 |
JNIEnv *env = (JNIEnv *)JNU_GetEnv(jvm, JNI_VERSION_1_2); |
|
2264 |
||
2265 |
/* find out how much needs to be written */ |
|
2266 |
jint datacount = sb->bufferLength - dest->free_in_buffer; |
|
2267 |
||
2268 |
if (datacount != 0) { |
|
2269 |
RELEASE_ARRAYS(env, data, (const JOCTET *)(dest->next_output_byte)); |
|
2270 |
||
2271 |
(*env)->CallVoidMethod(env, |
|
2272 |
sb->stream, |
|
2273 |
ImageOutputStream_writeID, |
|
2274 |
sb->hstreamBuffer, |
|
2275 |
0, |
|
2276 |
datacount); |
|
2277 |
||
2278 |
if ((*env)->ExceptionOccurred(env) |
|
2279 |
|| !GET_ARRAYS(env, data, |
|
2280 |
(const JOCTET **)(&dest->next_output_byte))) { |
|
2281 |
cinfo->err->error_exit((j_common_ptr) cinfo); |
|
2282 |
} |
|
2283 |
} |
|
2284 |
||
2285 |
dest->next_output_byte = NULL; |
|
2286 |
dest->free_in_buffer = 0; |
|
2287 |
||
2288 |
} |
|
2289 |
||
2290 |
/* |
|
2291 |
* Flush the destination buffer. This is not called by the library, |
|
2292 |
* but by our code below. This is the simplest implementation, though |
|
2293 |
* certainly not the most efficient. |
|
2294 |
*/ |
|
2295 |
METHODDEF(void) |
|
2296 |
imageio_flush_destination(j_compress_ptr cinfo) |
|
2297 |
{ |
|
2298 |
imageio_term_destination(cinfo); |
|
2299 |
imageio_init_destination(cinfo); |
|
2300 |
} |
|
2301 |
||
2302 |
/********************** end of destination manager ************/ |
|
2303 |
||
2304 |
/********************** Writer JNI calls **********************/ |
|
2305 |
||
2306 |
||
2307 |
JNIEXPORT void JNICALL |
|
2308 |
Java_com_sun_imageio_plugins_jpeg_JPEGImageWriter_initWriterIDs |
|
2309 |
(JNIEnv *env, |
|
2310 |
jclass cls, |
|
2311 |
jclass IOSClass, |
|
2312 |
jclass qTableClass, |
|
2313 |
jclass huffClass) { |
|
2314 |
||
2315 |
ImageOutputStream_writeID = (*env)->GetMethodID(env, |
|
2316 |
IOSClass, |
|
2317 |
"write", |
|
2318 |
"([BII)V"); |
|
2319 |
||
2320 |
JPEGImageWriter_warningOccurredID = (*env)->GetMethodID(env, |
|
2321 |
cls, |
|
2322 |
"warningOccurred", |
|
2323 |
"(I)V"); |
|
2324 |
JPEGImageWriter_warningWithMessageID = |
|
2325 |
(*env)->GetMethodID(env, |
|
2326 |
cls, |
|
2327 |
"warningWithMessage", |
|
2328 |
"(Ljava/lang/String;)V"); |
|
2329 |
||
2330 |
JPEGImageWriter_writeMetadataID = (*env)->GetMethodID(env, |
|
2331 |
cls, |
|
2332 |
"writeMetadata", |
|
2333 |
"()V"); |
|
2334 |
JPEGImageWriter_grabPixelsID = (*env)->GetMethodID(env, |
|
2335 |
cls, |
|
2336 |
"grabPixels", |
|
2337 |
"(I)V"); |
|
2338 |
||
2339 |
JPEGQTable_tableID = (*env)->GetFieldID(env, |
|
2340 |
qTableClass, |
|
2341 |
"qTable", |
|
2342 |
"[I"); |
|
2343 |
||
2344 |
JPEGHuffmanTable_lengthsID = (*env)->GetFieldID(env, |
|
2345 |
huffClass, |
|
2346 |
"lengths", |
|
2347 |
"[S"); |
|
2348 |
||
2349 |
JPEGHuffmanTable_valuesID = (*env)->GetFieldID(env, |
|
2350 |
huffClass, |
|
2351 |
"values", |
|
2352 |
"[S"); |
|
2353 |
} |
|
2354 |
||
2355 |
JNIEXPORT jlong JNICALL |
|
2356 |
Java_com_sun_imageio_plugins_jpeg_JPEGImageWriter_initJPEGImageWriter |
|
2357 |
(JNIEnv *env, |
|
2358 |
jobject this) { |
|
2359 |
||
2360 |
imageIODataPtr ret; |
|
2361 |
struct sun_jpeg_error_mgr *jerr; |
|
2362 |
struct jpeg_destination_mgr *dest; |
|
2363 |
||
2364 |
/* This struct contains the JPEG compression parameters and pointers to |
|
2365 |
* working space (which is allocated as needed by the JPEG library). |
|
2366 |
*/ |
|
2367 |
struct jpeg_compress_struct *cinfo = |
|
2368 |
malloc(sizeof(struct jpeg_compress_struct)); |
|
2369 |
if (cinfo == NULL) { |
|
2370 |
JNU_ThrowByName( env, |
|
2371 |
"java/lang/OutOfMemoryError", |
|
2372 |
"Initializing Writer"); |
|
2373 |
return 0; |
|
2374 |
} |
|
2375 |
||
2376 |
/* We use our private extension JPEG error handler. |
|
2377 |
*/ |
|
2378 |
jerr = malloc (sizeof(struct sun_jpeg_error_mgr)); |
|
2379 |
if (jerr == NULL) { |
|
2380 |
JNU_ThrowByName( env, |
|
2381 |
"java/lang/OutOfMemoryError", |
|
2382 |
"Initializing Writer"); |
|
2383 |
free(cinfo); |
|
2384 |
return 0; |
|
2385 |
} |
|
2386 |
||
2387 |
/* We set up the normal JPEG error routines, then override error_exit. */ |
|
2388 |
cinfo->err = jpeg_std_error(&(jerr->pub)); |
|
2389 |
jerr->pub.error_exit = sun_jpeg_error_exit; |
|
2390 |
/* We need to setup our own print routines */ |
|
2391 |
jerr->pub.output_message = sun_jpeg_output_message; |
|
2392 |
/* Now we can setjmp before every call to the library */ |
|
2393 |
||
2394 |
/* Establish the setjmp return context for sun_jpeg_error_exit to use. */ |
|
2395 |
if (setjmp(jerr->setjmp_buffer)) { |
|
2396 |
/* If we get here, the JPEG code has signaled an error. */ |
|
2397 |
char buffer[JMSG_LENGTH_MAX]; |
|
2398 |
(*cinfo->err->format_message) ((struct jpeg_common_struct *) cinfo, |
|
2399 |
buffer); |
|
2400 |
JNU_ThrowByName(env, "javax/imageio/IIOException", buffer); |
|
2401 |
return 0; |
|
2402 |
} |
|
2403 |
||
2404 |
/* Perform library initialization */ |
|
2405 |
jpeg_create_compress(cinfo); |
|
2406 |
||
2407 |
/* Now set up the destination */ |
|
2408 |
dest = malloc(sizeof(struct jpeg_destination_mgr)); |
|
2409 |
if (dest == NULL) { |
|
2410 |
JNU_ThrowByName( env, |
|
2411 |
"java/lang/OutOfMemoryError", |
|
2412 |
"Initializing Writer"); |
|
2413 |
free(cinfo); |
|
2414 |
free(jerr); |
|
2415 |
return 0; |
|
2416 |
} |
|
2417 |
||
2418 |
dest->init_destination = imageio_init_destination; |
|
2419 |
dest->empty_output_buffer = imageio_empty_output_buffer; |
|
2420 |
dest->term_destination = imageio_term_destination; |
|
2421 |
dest->next_output_byte = NULL; |
|
2422 |
dest->free_in_buffer = 0; |
|
2423 |
||
2424 |
cinfo->dest = dest; |
|
2425 |
||
2426 |
/* set up the association to persist for future calls */ |
|
2427 |
ret = initImageioData(env, (j_common_ptr) cinfo, this); |
|
2428 |
if (ret == NULL) { |
|
2429 |
JNU_ThrowByName( env, |
|
2430 |
"java/lang/OutOfMemoryError", |
|
2431 |
"Initializing Writer"); |
|
2432 |
free(cinfo); |
|
2433 |
free(jerr); |
|
2434 |
return 0; |
|
2435 |
} |
|
2436 |
return (jlong) ret; |
|
2437 |
} |
|
2438 |
||
2439 |
JNIEXPORT void JNICALL |
|
2440 |
Java_com_sun_imageio_plugins_jpeg_JPEGImageWriter_setDest |
|
2441 |
(JNIEnv *env, |
|
2442 |
jobject this, |
|
2443 |
jlong ptr, |
|
2444 |
jobject destination) { |
|
2445 |
||
2446 |
imageIODataPtr data = (imageIODataPtr) ptr; |
|
2447 |
j_compress_ptr cinfo; |
|
2448 |
||
2449 |
if (data == NULL) { |
|
2450 |
JNU_ThrowByName(env, |
|
2451 |
"java/lang/IllegalStateException", |
|
2452 |
"Attempting to use writer after dispose()"); |
|
2453 |
return; |
|
2454 |
} |
|
2455 |
||
2456 |
cinfo = (j_compress_ptr) data->jpegObj; |
|
2457 |
||
2458 |
imageio_set_stream(env, data->jpegObj, data, destination); |
|
2459 |
||
2460 |
||
2461 |
// Don't call the init method, as that depends on pinned arrays |
|
2462 |
cinfo->dest->next_output_byte = NULL; |
|
2463 |
cinfo->dest->free_in_buffer = 0; |
|
2464 |
} |
|
2465 |
||
2466 |
JNIEXPORT void JNICALL |
|
2467 |
Java_com_sun_imageio_plugins_jpeg_JPEGImageWriter_writeTables |
|
2468 |
(JNIEnv *env, |
|
2469 |
jobject this, |
|
2470 |
jlong ptr, |
|
2471 |
jobjectArray qtables, |
|
2472 |
jobjectArray DCHuffmanTables, |
|
2473 |
jobjectArray ACHuffmanTables) { |
|
2474 |
||
2475 |
struct jpeg_destination_mgr *dest; |
|
2476 |
sun_jpeg_error_ptr jerr; |
|
2477 |
imageIODataPtr data = (imageIODataPtr) ptr; |
|
2478 |
j_compress_ptr cinfo; |
|
2479 |
||
2480 |
if (data == NULL) { |
|
2481 |
JNU_ThrowByName(env, |
|
2482 |
"java/lang/IllegalStateException", |
|
2483 |
"Attempting to use writer after dispose()"); |
|
2484 |
return; |
|
2485 |
} |
|
2486 |
||
2487 |
cinfo = (j_compress_ptr) data->jpegObj; |
|
2488 |
dest = cinfo->dest; |
|
2489 |
||
2490 |
/* Establish the setjmp return context for sun_jpeg_error_exit to use. */ |
|
2491 |
jerr = (sun_jpeg_error_ptr) cinfo->err; |
|
2492 |
||
2493 |
if (setjmp(jerr->setjmp_buffer)) { |
|
2494 |
/* If we get here, the JPEG code has signaled an error |
|
2495 |
while writing. */ |
|
2496 |
RELEASE_ARRAYS(env, data, (const JOCTET *)(dest->next_output_byte)); |
|
2497 |
if (!(*env)->ExceptionOccurred(env)) { |
|
2498 |
char buffer[JMSG_LENGTH_MAX]; |
|
2499 |
(*cinfo->err->format_message) ((j_common_ptr) cinfo, |
|
2500 |
buffer); |
|
2501 |
JNU_ThrowByName(env, "javax/imageio/IIOException", buffer); |
|
2502 |
} |
|
2503 |
return; |
|
2504 |
} |
|
2505 |
||
2506 |
if (GET_ARRAYS(env, data, |
|
2507 |
(const JOCTET **)(&dest->next_output_byte)) == NOT_OK) { |
|
2508 |
JNU_ThrowByName(env, |
|
2509 |
"javax/imageio/IIOException", |
|
2510 |
"Array pin failed"); |
|
2511 |
return; |
|
2512 |
} |
|
2513 |
||
2514 |
jpeg_suppress_tables(cinfo, TRUE); // Suppress writing of any current |
|
2515 |
||
2516 |
data->streamBuf.suspendable = FALSE; |
|
2517 |
if (qtables != NULL) { |
|
2383
c6a2226cc4de
6791502: IIOException "Invalid icc profile" on jpeg after update from JDK5 to JDK6
bae
parents:
2
diff
changeset
|
2518 |
#ifdef DEBUG_IIO_JPEG |
2 | 2519 |
printf("in writeTables: qtables not NULL\n"); |
2520 |
#endif |
|
2521 |
setQTables(env, (j_common_ptr) cinfo, qtables, TRUE); |
|
2522 |
} |
|
2523 |
||
2524 |
if (DCHuffmanTables != NULL) { |
|
2525 |
setHTables(env, (j_common_ptr) cinfo, |
|
2526 |
DCHuffmanTables, ACHuffmanTables, TRUE); |
|
2527 |
} |
|
2528 |
||
2529 |
jpeg_write_tables(cinfo); // Flushes the buffer for you |
|
2530 |
RELEASE_ARRAYS(env, data, NULL); |
|
2531 |
} |
|
2532 |
||
2533 |
JNIEXPORT jboolean JNICALL |
|
2534 |
Java_com_sun_imageio_plugins_jpeg_JPEGImageWriter_writeImage |
|
2535 |
(JNIEnv *env, |
|
2536 |
jobject this, |
|
2537 |
jlong ptr, |
|
2538 |
jbyteArray buffer, |
|
2539 |
jint inCs, jint outCs, |
|
2540 |
jint numBands, |
|
2541 |
jintArray bandSizes, |
|
2542 |
jint srcWidth, |
|
2543 |
jint destWidth, jint destHeight, |
|
2544 |
jint stepX, jint stepY, |
|
2545 |
jobjectArray qtables, |
|
2546 |
jboolean writeDQT, |
|
2547 |
jobjectArray DCHuffmanTables, |
|
2548 |
jobjectArray ACHuffmanTables, |
|
2549 |
jboolean writeDHT, |
|
2550 |
jboolean optimize, |
|
2551 |
jboolean progressive, |
|
2552 |
jint numScans, |
|
2553 |
jintArray scanInfo, |
|
2554 |
jintArray componentIds, |
|
2555 |
jintArray HsamplingFactors, |
|
2556 |
jintArray VsamplingFactors, |
|
2557 |
jintArray QtableSelectors, |
|
2558 |
jboolean haveMetadata, |
|
2559 |
jint restartInterval) { |
|
2560 |
||
2561 |
struct jpeg_destination_mgr *dest; |
|
2562 |
JSAMPROW scanLinePtr; |
|
2563 |
int i, j; |
|
2564 |
int pixelStride; |
|
2565 |
unsigned char *in, *out, *pixelLimit; |
|
2566 |
int targetLine; |
|
2567 |
pixelBufferPtr pb; |
|
2568 |
sun_jpeg_error_ptr jerr; |
|
2569 |
jint *ids, *hfactors, *vfactors, *qsels; |
|
2570 |
jsize qlen, hlen; |
|
2571 |
int *scanptr; |
|
2572 |
jint *scanData; |
|
2573 |
jint *bandSize; |
|
2574 |
int maxBandValue, halfMaxBandValue; |
|
2575 |
imageIODataPtr data = (imageIODataPtr) ptr; |
|
2576 |
j_compress_ptr cinfo; |
|
2577 |
UINT8** scale = NULL; |
|
2578 |
||
2579 |
/* verify the inputs */ |
|
2580 |
||
2581 |
if (data == NULL) { |
|
2582 |
JNU_ThrowByName(env, |
|
2583 |
"java/lang/IllegalStateException", |
|
2584 |
"Attempting to use writer after dispose()"); |
|
2585 |
return JNI_FALSE; |
|
2586 |
} |
|
2587 |
||
2588 |
if ((buffer == NULL) || |
|
2589 |
(qtables == NULL) || |
|
2590 |
// H tables can be null if optimizing |
|
2591 |
(componentIds == NULL) || |
|
2592 |
(HsamplingFactors == NULL) || (VsamplingFactors == NULL) || |
|
2593 |
(QtableSelectors == NULL) || |
|
2594 |
((numScans != 0) && (scanInfo != NULL))) { |
|
2595 |
||
2596 |
JNU_ThrowNullPointerException(env, 0); |
|
2597 |
return JNI_FALSE; |
|
2598 |
||
2599 |
} |
|
2600 |
||
2601 |
if ((inCs < 0) || (inCs > JCS_YCCK) || |
|
2602 |
(outCs < 0) || (outCs > JCS_YCCK) || |
|
2603 |
(numBands < 1) || (numBands > MAX_BANDS) || |
|
2604 |
(srcWidth < 0) || |
|
2605 |
(destWidth < 0) || (destWidth > srcWidth) || |
|
2606 |
(destHeight < 0) || |
|
2607 |
(stepX < 0) || (stepY < 0)) |
|
2608 |
{ |
|
2609 |
JNU_ThrowByName(env, "javax/imageio/IIOException", |
|
2610 |
"Invalid argument to native writeImage"); |
|
2611 |
return JNI_FALSE; |
|
2612 |
} |
|
2613 |
||
2614 |
bandSize = (*env)->GetIntArrayElements(env, bandSizes, NULL); |
|
2615 |
||
2616 |
for (i = 0; i < numBands; i++) { |
|
2617 |
if (bandSize[i] != JPEG_BAND_SIZE) { |
|
2618 |
if (scale == NULL) { |
|
2619 |
scale = (UINT8**) calloc(numBands, sizeof(UINT8*)); |
|
2620 |
||
2621 |
if (scale == NULL) { |
|
2622 |
JNU_ThrowByName( env, "java/lang/OutOfMemoryError", |
|
2623 |
"Writing JPEG Stream"); |
|
2624 |
return JNI_FALSE; |
|
2625 |
} |
|
2626 |
} |
|
2627 |
||
2628 |
maxBandValue = (1 << bandSize[i]) - 1; |
|
2629 |
||
2630 |
scale[i] = (UINT8*) malloc((maxBandValue + 1) * sizeof(UINT8)); |
|
2631 |
||
2632 |
if (scale[i] == NULL) { |
|
2633 |
JNU_ThrowByName( env, "java/lang/OutOfMemoryError", |
|
2634 |
"Writing JPEG Stream"); |
|
2635 |
return JNI_FALSE; |
|
2636 |
} |
|
2637 |
||
2638 |
halfMaxBandValue = maxBandValue >> 1; |
|
2639 |
||
2640 |
for (j = 0; j <= maxBandValue; j++) { |
|
2641 |
scale[i][j] = (UINT8) |
|
2642 |
((j*MAX_JPEG_BAND_VALUE + halfMaxBandValue)/maxBandValue); |
|
2643 |
} |
|
2644 |
} |
|
2645 |
} |
|
2646 |
||
2647 |
(*env)->ReleaseIntArrayElements(env, bandSizes, |
|
2648 |
bandSize, JNI_ABORT); |
|
2649 |
||
2650 |
cinfo = (j_compress_ptr) data->jpegObj; |
|
2651 |
dest = cinfo->dest; |
|
2652 |
||
2653 |
/* Set the buffer as our PixelBuffer */ |
|
2654 |
pb = &data->pixelBuf; |
|
2655 |
||
2656 |
if (setPixelBuffer(env, pb, buffer) == NOT_OK) { |
|
2657 |
return data->abortFlag; // We already threw an out of memory exception |
|
2658 |
} |
|
2659 |
||
2660 |
// Allocate a 1-scanline buffer |
|
2661 |
scanLinePtr = (JSAMPROW)malloc(destWidth*numBands); |
|
2662 |
if (scanLinePtr == NULL) { |
|
2663 |
RELEASE_ARRAYS(env, data, (const JOCTET *)(dest->next_output_byte)); |
|
2664 |
JNU_ThrowByName( env, |
|
2665 |
"java/lang/OutOfMemoryError", |
|
2666 |
"Writing JPEG Stream"); |
|
2667 |
return data->abortFlag; |
|
2668 |
} |
|
2669 |
||
2670 |
/* Establish the setjmp return context for sun_jpeg_error_exit to use. */ |
|
2671 |
jerr = (sun_jpeg_error_ptr) cinfo->err; |
|
2672 |
||
2673 |
if (setjmp(jerr->setjmp_buffer)) { |
|
2674 |
/* If we get here, the JPEG code has signaled an error |
|
2675 |
while writing. */ |
|
2676 |
RELEASE_ARRAYS(env, data, (const JOCTET *)(dest->next_output_byte)); |
|
2677 |
if (!(*env)->ExceptionOccurred(env)) { |
|
2678 |
char buffer[JMSG_LENGTH_MAX]; |
|
2679 |
(*cinfo->err->format_message) ((j_common_ptr) cinfo, |
|
2680 |
buffer); |
|
2681 |
JNU_ThrowByName(env, "javax/imageio/IIOException", buffer); |
|
2682 |
} |
|
2683 |
free(scanLinePtr); |
|
2684 |
return data->abortFlag; |
|
2685 |
} |
|
2686 |
||
2687 |
// set up parameters |
|
2688 |
cinfo->image_width = destWidth; |
|
2689 |
cinfo->image_height = destHeight; |
|
2690 |
cinfo->input_components = numBands; |
|
2691 |
cinfo->in_color_space = inCs; |
|
2692 |
||
2693 |
jpeg_set_defaults(cinfo); |
|
2694 |
||
2695 |
jpeg_set_colorspace(cinfo, outCs); |
|
2696 |
||
2697 |
cinfo->optimize_coding = optimize; |
|
2698 |
||
2699 |
cinfo->write_JFIF_header = FALSE; |
|
2700 |
cinfo->write_Adobe_marker = FALSE; |
|
2701 |
// copy componentIds |
|
2702 |
ids = (*env)->GetIntArrayElements(env, componentIds, NULL); |
|
2703 |
hfactors = (*env)->GetIntArrayElements(env, HsamplingFactors, NULL); |
|
2704 |
vfactors = (*env)->GetIntArrayElements(env, VsamplingFactors, NULL); |
|
2705 |
qsels = (*env)->GetIntArrayElements(env, QtableSelectors, NULL); |
|
2706 |
||
2707 |
if ((ids == NULL) || |
|
2708 |
(hfactors == NULL) || (vfactors == NULL) || |
|
2709 |
(qsels == NULL)) { |
|
2710 |
JNU_ThrowByName( env, |
|
2711 |
"java/lang/OutOfMemoryError", |
|
2712 |
"Writing JPEG"); |
|
2713 |
return JNI_FALSE; |
|
2714 |
} |
|
2715 |
||
2716 |
for (i = 0; i < numBands; i++) { |
|
2717 |
cinfo->comp_info[i].component_id = ids[i]; |
|
2718 |
cinfo->comp_info[i].h_samp_factor = hfactors[i]; |
|
2719 |
cinfo->comp_info[i].v_samp_factor = vfactors[i]; |
|
2720 |
cinfo->comp_info[i].quant_tbl_no = qsels[i]; |
|
2721 |
} |
|
2722 |
||
2723 |
(*env)->ReleaseIntArrayElements(env, componentIds, |
|
2724 |
ids, JNI_ABORT); |
|
2725 |
(*env)->ReleaseIntArrayElements(env, HsamplingFactors, |
|
2726 |
hfactors, JNI_ABORT); |
|
2727 |
(*env)->ReleaseIntArrayElements(env, VsamplingFactors, |
|
2728 |
vfactors, JNI_ABORT); |
|
2729 |
(*env)->ReleaseIntArrayElements(env, QtableSelectors, |
|
2730 |
qsels, JNI_ABORT); |
|
2731 |
||
2732 |
jpeg_suppress_tables(cinfo, TRUE); // Disable writing any current |
|
2733 |
||
2734 |
qlen = setQTables(env, (j_common_ptr) cinfo, qtables, writeDQT); |
|
2735 |
||
2736 |
if (!optimize) { |
|
2737 |
// Set the h tables |
|
2738 |
hlen = setHTables(env, |
|
2739 |
(j_common_ptr) cinfo, |
|
2740 |
DCHuffmanTables, |
|
2741 |
ACHuffmanTables, |
|
2742 |
writeDHT); |
|
2743 |
} |
|
2744 |
||
2745 |
if (GET_ARRAYS(env, data, |
|
2746 |
(const JOCTET **)(&dest->next_output_byte)) == NOT_OK) { |
|
2747 |
JNU_ThrowByName(env, |
|
2748 |
"javax/imageio/IIOException", |
|
2749 |
"Array pin failed"); |
|
2750 |
return data->abortFlag; |
|
2751 |
} |
|
2752 |
||
2753 |
data->streamBuf.suspendable = FALSE; |
|
2754 |
||
2755 |
if (progressive) { |
|
2756 |
if (numScans == 0) { // then use default scans |
|
2757 |
jpeg_simple_progression(cinfo); |
|
2758 |
} else { |
|
2759 |
cinfo->num_scans = numScans; |
|
2760 |
// Copy the scanInfo to a local array |
|
2761 |
// The following is copied from jpeg_simple_progression: |
|
2762 |
/* Allocate space for script. |
|
2763 |
* We need to put it in the permanent pool in case the application performs |
|
2764 |
* multiple compressions without changing the settings. To avoid a memory |
|
2765 |
* leak if jpeg_simple_progression is called repeatedly for the same JPEG |
|
2766 |
* object, we try to re-use previously allocated space, and we allocate |
|
2767 |
* enough space to handle YCbCr even if initially asked for grayscale. |
|
2768 |
*/ |
|
2769 |
if (cinfo->script_space == NULL |
|
2770 |
|| cinfo->script_space_size < numScans) { |
|
2771 |
cinfo->script_space_size = MAX(numScans, 10); |
|
2772 |
cinfo->script_space = (jpeg_scan_info *) |
|
2773 |
(*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, |
|
2774 |
JPOOL_PERMANENT, |
|
2775 |
cinfo->script_space_size |
|
2776 |
* sizeof(jpeg_scan_info)); |
|
2777 |
} |
|
2778 |
cinfo->scan_info = cinfo->script_space; |
|
2779 |
scanptr = (int *) cinfo->script_space; |
|
2780 |
scanData = (*env)->GetIntArrayElements(env, scanInfo, NULL); |
|
2781 |
// number of jints per scan is 9 |
|
2782 |
// We avoid a memcpy to handle different size ints |
|
2783 |
for (i = 0; i < numScans*9; i++) { |
|
2784 |
scanptr[i] = scanData[i]; |
|
2785 |
} |
|
2786 |
(*env)->ReleaseIntArrayElements(env, scanInfo, |
|
2787 |
scanData, JNI_ABORT); |
|
2788 |
||
2789 |
} |
|
2790 |
} |
|
2791 |
||
2792 |
cinfo->restart_interval = restartInterval; |
|
2793 |
||
2383
c6a2226cc4de
6791502: IIOException "Invalid icc profile" on jpeg after update from JDK5 to JDK6
bae
parents:
2
diff
changeset
|
2794 |
#ifdef DEBUG_IIO_JPEG |
2 | 2795 |
printf("writer setup complete, starting compressor\n"); |
2796 |
#endif |
|
2797 |
||
2798 |
// start the compressor; tables must already be set |
|
2799 |
jpeg_start_compress(cinfo, FALSE); // Leaves sent_table alone |
|
2800 |
||
2801 |
if (haveMetadata) { |
|
2802 |
// Flush the buffer |
|
2803 |
imageio_flush_destination(cinfo); |
|
2804 |
// Call Java to write the metadata |
|
2805 |
RELEASE_ARRAYS(env, data, (const JOCTET *)(dest->next_output_byte)); |
|
2806 |
(*env)->CallVoidMethod(env, |
|
2807 |
this, |
|
2808 |
JPEGImageWriter_writeMetadataID); |
|
2809 |
if ((*env)->ExceptionOccurred(env) |
|
2810 |
|| !GET_ARRAYS(env, data, |
|
2811 |
(const JOCTET **)(&dest->next_output_byte))) { |
|
2812 |
cinfo->err->error_exit((j_common_ptr) cinfo); |
|
2813 |
} |
|
2814 |
} |
|
2815 |
||
2816 |
targetLine = 0; |
|
2817 |
||
2818 |
// for each line in destHeight |
|
2819 |
while ((data->abortFlag == JNI_FALSE) |
|
2820 |
&& (cinfo->next_scanline < cinfo->image_height)) { |
|
2821 |
// get the line from Java |
|
2822 |
RELEASE_ARRAYS(env, data, (const JOCTET *)(dest->next_output_byte)); |
|
2823 |
(*env)->CallVoidMethod(env, |
|
2824 |
this, |
|
2825 |
JPEGImageWriter_grabPixelsID, |
|
2826 |
targetLine); |
|
2827 |
if ((*env)->ExceptionOccurred(env) |
|
2828 |
|| !GET_ARRAYS(env, data, |
|
2829 |
(const JOCTET **)(&dest->next_output_byte))) { |
|
2830 |
cinfo->err->error_exit((j_common_ptr) cinfo); |
|
2831 |
} |
|
2832 |
||
2833 |
// subsample it into our buffer |
|
2834 |
||
2835 |
in = data->pixelBuf.buf.bp; |
|
2836 |
out = scanLinePtr; |
|
2837 |
pixelLimit = in + srcWidth*numBands; |
|
2838 |
pixelStride = numBands*stepX; |
|
2839 |
for (; in < pixelLimit; in += pixelStride) { |
|
2840 |
for (i = 0; i < numBands; i++) { |
|
2841 |
if (scale !=NULL && scale[i] != NULL) { |
|
2842 |
*out++ = scale[i][*(in+i)]; |
|
2383
c6a2226cc4de
6791502: IIOException "Invalid icc profile" on jpeg after update from JDK5 to JDK6
bae
parents:
2
diff
changeset
|
2843 |
#ifdef DEBUG_IIO_JPEG |
2 | 2844 |
if (in == data->pixelBuf.buf.bp){ // Just the first pixel |
2845 |
printf("in %d -> out %d, ", *(in+i), *(out-i-1)); |
|
2846 |
} |
|
2847 |
#endif |
|
2848 |
||
2383
c6a2226cc4de
6791502: IIOException "Invalid icc profile" on jpeg after update from JDK5 to JDK6
bae
parents:
2
diff
changeset
|
2849 |
#ifdef DEBUG_IIO_JPEG |
2 | 2850 |
if (in == data->pixelBuf.buf.bp){ // Just the first pixel |
2851 |
printf("\n"); |
|
2852 |
} |
|
2853 |
#endif |
|
2854 |
} else { |
|
2855 |
*out++ = *(in+i); |
|
2856 |
} |
|
2857 |
} |
|
2858 |
} |
|
2859 |
// write it out |
|
2860 |
jpeg_write_scanlines(cinfo, (JSAMPARRAY)&scanLinePtr, 1); |
|
2861 |
targetLine += stepY; |
|
2862 |
} |
|
2863 |
||
2864 |
/* |
|
2865 |
* We are done, but we might not have done all the lines, |
|
2866 |
* so use jpeg_abort instead of jpeg_finish_compress. |
|
2867 |
*/ |
|
2868 |
if (cinfo->next_scanline == cinfo->image_height) { |
|
2869 |
jpeg_finish_compress(cinfo); // Flushes buffer with term_dest |
|
2870 |
} else { |
|
2871 |
jpeg_abort((j_common_ptr)cinfo); |
|
2872 |
} |
|
2873 |
||
2874 |
if (scale != NULL) { |
|
2875 |
for (i = 0; i < numBands; i++) { |
|
2876 |
if (scale[i] != NULL) { |
|
2877 |
free(scale[i]); |
|
2878 |
} |
|
2879 |
} |
|
2880 |
free(scale); |
|
2881 |
} |
|
2882 |
||
2883 |
free(scanLinePtr); |
|
2884 |
RELEASE_ARRAYS(env, data, NULL); |
|
2885 |
return data->abortFlag; |
|
2886 |
} |
|
2887 |
||
2888 |
JNIEXPORT void JNICALL |
|
2889 |
Java_com_sun_imageio_plugins_jpeg_JPEGImageWriter_abortWrite |
|
2890 |
(JNIEnv *env, |
|
2891 |
jobject this, |
|
2892 |
jlong ptr) { |
|
2893 |
||
2894 |
imageIODataPtr data = (imageIODataPtr) ptr; |
|
2895 |
||
2896 |
if (data == NULL) { |
|
2897 |
JNU_ThrowByName(env, |
|
2898 |
"java/lang/IllegalStateException", |
|
2899 |
"Attempting to use writer after dispose()"); |
|
2900 |
return; |
|
2901 |
} |
|
2902 |
||
2903 |
imageio_abort(env, this, data); |
|
2904 |
} |
|
2905 |
||
2906 |
JNIEXPORT void JNICALL |
|
2907 |
Java_com_sun_imageio_plugins_jpeg_JPEGImageWriter_resetWriter |
|
2908 |
(JNIEnv *env, |
|
2909 |
jobject this, |
|
2910 |
jlong ptr) { |
|
2911 |
imageIODataPtr data = (imageIODataPtr) ptr; |
|
2912 |
j_compress_ptr cinfo; |
|
2913 |
||
2914 |
if (data == NULL) { |
|
2915 |
JNU_ThrowByName(env, |
|
2916 |
"java/lang/IllegalStateException", |
|
2917 |
"Attempting to use writer after dispose()"); |
|
2918 |
return; |
|
2919 |
} |
|
2920 |
||
2921 |
cinfo = (j_compress_ptr) data->jpegObj; |
|
2922 |
||
2923 |
imageio_reset(env, (j_common_ptr) cinfo, data); |
|
2924 |
||
2925 |
/* |
|
2926 |
* The tables have not been reset, and there is no way to do so |
|
2927 |
* in IJG without leaking memory. The only situation in which |
|
2928 |
* this will cause a problem is if an image-only stream is written |
|
2929 |
* with this object without initializing the correct tables first, |
|
2930 |
* which should not be possible. |
|
2931 |
*/ |
|
2932 |
||
2933 |
cinfo->dest->next_output_byte = NULL; |
|
2934 |
cinfo->dest->free_in_buffer = 0; |
|
2935 |
} |
|
2936 |
||
2937 |
JNIEXPORT void JNICALL |
|
2938 |
Java_com_sun_imageio_plugins_jpeg_JPEGImageWriter_disposeWriter |
|
2939 |
(JNIEnv *env, |
|
2940 |
jclass writer, |
|
2941 |
jlong ptr) { |
|
2942 |
||
2943 |
imageIODataPtr data = (imageIODataPtr) ptr; |
|
2944 |
j_common_ptr info = destroyImageioData(env, data); |
|
2945 |
||
2946 |
imageio_dispose(info); |
|
2947 |
} |