author | bae |
Tue, 14 May 2013 12:51:59 +0400 | |
changeset 20799 | 4820649a142d |
parent 18258 | ab42088d6e10 |
child 23010 | 6dadb192ad81 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
9035
1255eb81cc2f
7033660: Update copyright year to 2011 on any files changed in 2011
ohair
parents:
7746
diff
changeset
|
2 |
* Copyright (c) 1997, 2010, Oracle and/or its affiliates. All rights reserved. |
2 | 3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 |
* |
|
5 |
* This code is free software; you can redistribute it and/or modify it |
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
|
5506 | 7 |
* published by the Free Software Foundation. Oracle designates this |
2 | 8 |
* particular file as subject to the "Classpath" exception as provided |
5506 | 9 |
* by Oracle in the LICENSE file that accompanied this code. |
2 | 10 |
* |
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
15 |
* accompanied this code). |
|
16 |
* |
|
17 |
* You should have received a copy of the GNU General Public License version |
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 |
* |
|
5506 | 21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
22 |
* or visit www.oracle.com if you need additional information or have any |
|
23 |
* questions. |
|
2 | 24 |
*/ |
25 |
||
26 |
#include <stdio.h> |
|
27 |
#include <stdlib.h> |
|
28 |
#include "awt_parseImage.h" |
|
29 |
#include "imageInitIDs.h" |
|
30 |
#include "java_awt_Transparency.h" |
|
31 |
#include "java_awt_image_BufferedImage.h" |
|
32 |
#include "sun_awt_image_IntegerComponentRaster.h" |
|
33 |
#include "sun_awt_image_ImagingLib.h" |
|
34 |
#include "java_awt_color_ColorSpace.h" |
|
35 |
#include "awt_Mlib.h" |
|
36 |
#include "safe_alloc.h" |
|
16868 | 37 |
#include "safe_math.h" |
2 | 38 |
|
39 |
static int setHints(JNIEnv *env, BufImageS_t *imageP); |
|
40 |
||
41 |
||
42 |
||
43 |
/* Parse the buffered image. All of the raster information is returned in the |
|
44 |
* imagePP structure. |
|
45 |
* |
|
46 |
* The handleCustom parameter specifies whether or not the caller |
|
47 |
* can use custom channels. If it is false and a custom channel |
|
48 |
* is encountered, the returned value will be 0 and all structures |
|
49 |
* will be deallocated. |
|
50 |
* |
|
51 |
* Return value: |
|
52 |
* -1: Exception |
|
53 |
* 0: Can't do it. |
|
54 |
* 1: Success |
|
55 |
*/ |
|
56 |
int awt_parseImage(JNIEnv *env, jobject jimage, BufImageS_t **imagePP, |
|
57 |
int handleCustom) { |
|
58 |
BufImageS_t *imageP; |
|
59 |
int status; |
|
60 |
jobject jraster; |
|
61 |
jobject jcmodel; |
|
62 |
||
63 |
/* Make sure the image exists */ |
|
64 |
if (JNU_IsNull(env, jimage)) { |
|
65 |
JNU_ThrowNullPointerException(env, "null BufferedImage object"); |
|
66 |
return -1; |
|
67 |
} |
|
68 |
||
69 |
if ((imageP = (BufImageS_t *) calloc(1, sizeof(BufImageS_t))) == NULL) { |
|
70 |
JNU_ThrowOutOfMemoryError(env, "Out of memory"); |
|
71 |
return -1; |
|
72 |
} |
|
73 |
imageP->jimage = jimage; |
|
74 |
||
75 |
/* Retrieve the raster */ |
|
76 |
if ((jraster = (*env)->GetObjectField(env, jimage, |
|
77 |
g_BImgRasterID)) == NULL) { |
|
78 |
free((void *) imageP); |
|
79 |
JNU_ThrowNullPointerException(env, "null Raster object"); |
|
80 |
return 0; |
|
81 |
} |
|
82 |
||
83 |
/* Retrieve the image type */ |
|
84 |
imageP->imageType = (*env)->GetIntField(env, jimage, g_BImgTypeID); |
|
85 |
||
86 |
/* Parse the raster */ |
|
87 |
if ((status = awt_parseRaster(env, jraster, &imageP->raster)) <= 0) { |
|
88 |
free((void *)imageP); |
|
89 |
return status; |
|
90 |
} |
|
91 |
||
92 |
/* Retrieve the color model */ |
|
93 |
if ((jcmodel = (*env)->GetObjectField(env, jimage, g_BImgCMID)) == NULL) { |
|
94 |
free((void *) imageP); |
|
95 |
JNU_ThrowNullPointerException(env, "null Raster object"); |
|
96 |
return 0; |
|
97 |
} |
|
98 |
||
99 |
/* Parse the color model */ |
|
100 |
if ((status = awt_parseColorModel(env, jcmodel, imageP->imageType, |
|
101 |
&imageP->cmodel)) <= 0) { |
|
102 |
awt_freeParsedRaster(&imageP->raster, FALSE); |
|
103 |
free((void *)imageP); |
|
104 |
return 0; |
|
105 |
} |
|
106 |
||
107 |
/* Set hints */ |
|
108 |
if ((status = setHints(env, imageP)) <= 0) { |
|
109 |
awt_freeParsedImage(imageP, TRUE); |
|
110 |
return 0; |
|
111 |
} |
|
112 |
||
113 |
*imagePP = imageP; |
|
114 |
||
115 |
return status; |
|
116 |
} |
|
117 |
||
16093 | 118 |
/* Verifies whether the channel offsets are sane and correspond to the type of |
119 |
* the raster. |
|
120 |
* |
|
121 |
* Return value: |
|
122 |
* 0: Failure: channel offsets are invalid |
|
123 |
* 1: Success |
|
124 |
*/ |
|
125 |
static int checkChannelOffsets(RasterS_t *rasterP, int dataArrayLength) { |
|
126 |
int i, lastPixelOffset, lastScanOffset; |
|
127 |
switch (rasterP->rasterType) { |
|
128 |
case COMPONENT_RASTER_TYPE: |
|
129 |
if (!SAFE_TO_MULT(rasterP->height, rasterP->scanlineStride)) { |
|
130 |
return 0; |
|
131 |
} |
|
132 |
if (!SAFE_TO_MULT(rasterP->width, rasterP->pixelStride)) { |
|
133 |
return 0; |
|
134 |
} |
|
135 |
||
136 |
lastScanOffset = (rasterP->height - 1) * rasterP->scanlineStride; |
|
137 |
lastPixelOffset = (rasterP->width - 1) * rasterP->pixelStride; |
|
138 |
||
139 |
||
140 |
if (!SAFE_TO_ADD(lastPixelOffset, lastScanOffset)) { |
|
141 |
return 0; |
|
142 |
} |
|
143 |
||
144 |
lastPixelOffset += lastScanOffset; |
|
145 |
||
146 |
for (i = 0; i < rasterP->numDataElements; i++) { |
|
147 |
int off = rasterP->chanOffsets[i]; |
|
148 |
int size = lastPixelOffset + off; |
|
149 |
||
150 |
if (off < 0 || !SAFE_TO_ADD(lastPixelOffset, off)) { |
|
151 |
return 0; |
|
152 |
} |
|
153 |
||
154 |
if (size < lastPixelOffset || size >= dataArrayLength) { |
|
155 |
// an overflow, or insufficient buffer capacity |
|
156 |
return 0; |
|
157 |
} |
|
158 |
} |
|
159 |
return 1; |
|
160 |
case BANDED_RASTER_TYPE: |
|
161 |
// NB:caller does not support the banded rasters yet, |
|
162 |
// so this branch of the code must be re-defined in |
|
163 |
// order to provide valid criteria for the data offsets |
|
164 |
// verification, when/if banded rasters will be supported. |
|
165 |
// At the moment, we prohibit banded rasters as well. |
|
166 |
return 0; |
|
167 |
default: |
|
168 |
// PACKED_RASTER_TYPE: does not support channel offsets |
|
169 |
// UNKNOWN_RASTER_TYPE: should not be used, likely indicates an error |
|
170 |
return 0; |
|
171 |
} |
|
172 |
} |
|
173 |
||
2 | 174 |
/* Parse the raster. All of the raster information is returned in the |
175 |
* rasterP structure. |
|
176 |
* |
|
177 |
* Return value: |
|
178 |
* -1: Exception |
|
179 |
* 0: Can't do it (Custom channel) |
|
180 |
* 1: Success |
|
181 |
*/ |
|
182 |
int awt_parseRaster(JNIEnv *env, jobject jraster, RasterS_t *rasterP) { |
|
183 |
jobject joffs = NULL; |
|
184 |
/* int status;*/ |
|
185 |
||
186 |
if (JNU_IsNull(env, jraster)) { |
|
187 |
JNU_ThrowNullPointerException(env, "null Raster object"); |
|
188 |
return -1; |
|
189 |
} |
|
190 |
||
191 |
rasterP->jraster = jraster; |
|
192 |
rasterP->width = (*env)->GetIntField(env, jraster, g_RasterWidthID); |
|
193 |
rasterP->height = (*env)->GetIntField(env, jraster, g_RasterHeightID); |
|
194 |
rasterP->numDataElements = (*env)->GetIntField(env, jraster, |
|
195 |
g_RasterNumDataElementsID); |
|
196 |
rasterP->numBands = (*env)->GetIntField(env, jraster, |
|
197 |
g_RasterNumBandsID); |
|
198 |
||
199 |
rasterP->baseOriginX = (*env)->GetIntField(env, jraster, |
|
200 |
g_RasterBaseOriginXID); |
|
201 |
rasterP->baseOriginY = (*env)->GetIntField(env, jraster, |
|
202 |
g_RasterBaseOriginYID); |
|
203 |
rasterP->minX = (*env)->GetIntField(env, jraster, g_RasterMinXID); |
|
204 |
rasterP->minY = (*env)->GetIntField(env, jraster, g_RasterMinYID); |
|
205 |
||
206 |
rasterP->jsampleModel = (*env)->GetObjectField(env, jraster, |
|
207 |
g_RasterSampleModelID); |
|
208 |
||
209 |
if (JNU_IsNull(env, rasterP->jsampleModel)) { |
|
210 |
JNU_ThrowNullPointerException(env, "null Raster object"); |
|
211 |
return -1; |
|
212 |
} |
|
213 |
||
16093 | 214 |
// make sure that the raster type is initialized |
215 |
rasterP->rasterType = UNKNOWN_RASTER_TYPE; |
|
216 |
||
2 | 217 |
if (rasterP->numBands <= 0 || |
218 |
rasterP->numBands > MAX_NUMBANDS) |
|
219 |
{ |
|
220 |
/* |
|
221 |
* we can't handle such kind of rasters due to limitations |
|
222 |
* of SPPSampleModelS_t structure and expand/set methods. |
|
223 |
*/ |
|
224 |
return 0; |
|
225 |
} |
|
226 |
||
16096 | 227 |
rasterP->sppsm.isUsed = 0; |
228 |
||
2 | 229 |
if ((*env)->IsInstanceOf(env, rasterP->jsampleModel, |
230 |
(*env)->FindClass(env,"java/awt/image/SinglePixelPackedSampleModel"))) { |
|
231 |
jobject jmask, joffs, jnbits; |
|
16096 | 232 |
|
233 |
rasterP->sppsm.isUsed = 1; |
|
234 |
||
2 | 235 |
rasterP->sppsm.maxBitSize = (*env)->GetIntField(env, |
236 |
rasterP->jsampleModel, |
|
237 |
g_SPPSMmaxBitID); |
|
238 |
jmask = (*env)->GetObjectField(env, rasterP->jsampleModel, |
|
239 |
g_SPPSMmaskArrID); |
|
240 |
joffs = (*env)->GetObjectField(env, rasterP->jsampleModel, |
|
241 |
g_SPPSMmaskOffID); |
|
242 |
jnbits = (*env)->GetObjectField(env, rasterP->jsampleModel, |
|
243 |
g_SPPSMnBitsID); |
|
244 |
if (jmask == NULL || joffs == NULL || jnbits == NULL || |
|
7746
3cdbc9c4de5b
6782574: AffineTransformOp.filter(BufferedImage, BufferedImage) fails with InternalError
bae
parents:
5506
diff
changeset
|
245 |
rasterP->sppsm.maxBitSize < 0) |
2 | 246 |
{ |
247 |
JNU_ThrowInternalError(env, "Can't grab SPPSM fields"); |
|
248 |
return -1; |
|
249 |
} |
|
250 |
(*env)->GetIntArrayRegion(env, jmask, 0, |
|
251 |
rasterP->numBands, rasterP->sppsm.maskArray); |
|
252 |
(*env)->GetIntArrayRegion(env, joffs, 0, |
|
253 |
rasterP->numBands, rasterP->sppsm.offsets); |
|
254 |
(*env)->GetIntArrayRegion(env, jnbits, 0, |
|
255 |
rasterP->numBands, rasterP->sppsm.nBits); |
|
256 |
||
257 |
} |
|
258 |
rasterP->baseRasterWidth = (*env)->GetIntField(env, rasterP->jsampleModel, |
|
259 |
g_SMWidthID); |
|
260 |
rasterP->baseRasterHeight = (*env)->GetIntField(env, |
|
261 |
rasterP->jsampleModel, |
|
262 |
g_SMHeightID); |
|
263 |
||
264 |
if ((*env)->IsInstanceOf(env, jraster, |
|
265 |
(*env)->FindClass(env, "sun/awt/image/IntegerComponentRaster"))){ |
|
266 |
rasterP->jdata = (*env)->GetObjectField(env, jraster, g_ICRdataID); |
|
267 |
rasterP->dataType = INT_DATA_TYPE; |
|
268 |
rasterP->dataSize = 4; |
|
269 |
rasterP->dataIsShared = TRUE; |
|
270 |
rasterP->rasterType = COMPONENT_RASTER_TYPE; |
|
271 |
rasterP->type = (*env)->GetIntField(env, jraster, g_ICRtypeID); |
|
272 |
rasterP->scanlineStride = (*env)->GetIntField(env, jraster, g_ICRscanstrID); |
|
273 |
rasterP->pixelStride = (*env)->GetIntField(env, jraster, g_ICRpixstrID); |
|
274 |
joffs = (*env)->GetObjectField(env, jraster, g_ICRdataOffsetsID); |
|
275 |
} |
|
276 |
else if ((*env)->IsInstanceOf(env, jraster, |
|
277 |
(*env)->FindClass(env, "sun/awt/image/ByteComponentRaster"))){ |
|
278 |
rasterP->jdata = (*env)->GetObjectField(env, jraster, g_BCRdataID); |
|
279 |
rasterP->dataType = BYTE_DATA_TYPE; |
|
280 |
rasterP->dataSize = 1; |
|
281 |
rasterP->dataIsShared = TRUE; |
|
282 |
rasterP->rasterType = COMPONENT_RASTER_TYPE; |
|
283 |
rasterP->type = (*env)->GetIntField(env, jraster, g_BCRtypeID); |
|
284 |
rasterP->scanlineStride = (*env)->GetIntField(env, jraster, g_BCRscanstrID); |
|
285 |
rasterP->pixelStride = (*env)->GetIntField(env, jraster, g_BCRpixstrID); |
|
286 |
joffs = (*env)->GetObjectField(env, jraster, g_BCRdataOffsetsID); |
|
287 |
} |
|
288 |
else if ((*env)->IsInstanceOf(env, jraster, |
|
289 |
(*env)->FindClass(env, "sun/awt/image/ShortComponentRaster"))){ |
|
290 |
rasterP->jdata = (*env)->GetObjectField(env, jraster, g_SCRdataID); |
|
291 |
rasterP->dataType = SHORT_DATA_TYPE; |
|
292 |
rasterP->dataSize = 2; |
|
293 |
rasterP->dataIsShared = TRUE; |
|
294 |
rasterP->rasterType = COMPONENT_RASTER_TYPE; |
|
295 |
rasterP->type = (*env)->GetIntField(env, jraster, g_SCRtypeID); |
|
296 |
rasterP->scanlineStride = (*env)->GetIntField(env, jraster, g_SCRscanstrID); |
|
297 |
rasterP->pixelStride = (*env)->GetIntField(env, jraster, g_SCRpixstrID); |
|
298 |
joffs = (*env)->GetObjectField(env, jraster, g_SCRdataOffsetsID); |
|
299 |
} |
|
300 |
else if ((*env)->IsInstanceOf(env, jraster, |
|
301 |
(*env)->FindClass(env, "sun/awt/image/BytePackedRaster"))){ |
|
302 |
rasterP->rasterType = PACKED_RASTER_TYPE; |
|
303 |
rasterP->dataType = BYTE_DATA_TYPE; |
|
304 |
rasterP->dataSize = 1; |
|
305 |
rasterP->scanlineStride = (*env)->GetIntField(env, jraster, g_BPRscanstrID); |
|
306 |
rasterP->pixelStride = (*env)->GetIntField(env, jraster, g_BPRpixstrID); |
|
307 |
rasterP->jdata = (*env)->GetObjectField(env, jraster, g_BPRdataID); |
|
308 |
rasterP->type = (*env)->GetIntField(env, jraster, g_BPRtypeID); |
|
309 |
rasterP->chanOffsets = NULL; |
|
310 |
if (SAFE_TO_ALLOC_2(rasterP->numDataElements, sizeof(jint))) { |
|
311 |
rasterP->chanOffsets = |
|
312 |
(jint *)malloc(rasterP->numDataElements * sizeof(jint)); |
|
313 |
} |
|
314 |
if (rasterP->chanOffsets == NULL) { |
|
315 |
/* Out of memory */ |
|
316 |
JNU_ThrowOutOfMemoryError(env, "Out of memory"); |
|
317 |
return -1; |
|
318 |
} |
|
319 |
rasterP->chanOffsets[0] = (*env)->GetIntField(env, jraster, g_BPRdataBitOffsetID); |
|
320 |
rasterP->dataType = BYTE_DATA_TYPE; |
|
321 |
} |
|
322 |
else { |
|
323 |
rasterP->type = sun_awt_image_IntegerComponentRaster_TYPE_CUSTOM; |
|
324 |
rasterP->dataType = UNKNOWN_DATA_TYPE; |
|
325 |
rasterP->rasterType = UNKNOWN_RASTER_TYPE; |
|
326 |
rasterP->chanOffsets = NULL; |
|
327 |
/* Custom raster */ |
|
328 |
return 0; |
|
329 |
} |
|
330 |
||
16093 | 331 |
// do basic validation of the raster structure |
332 |
if (rasterP->width <= 0 || rasterP->height <= 0 || |
|
333 |
rasterP->pixelStride <= 0 || rasterP->scanlineStride <= 0) |
|
334 |
{ |
|
335 |
// invalid raster |
|
336 |
return -1; |
|
337 |
} |
|
338 |
||
339 |
// channel (data) offsets |
|
340 |
switch (rasterP->rasterType) { |
|
341 |
case COMPONENT_RASTER_TYPE: |
|
342 |
case BANDED_RASTER_TYPE: // note that this routine does not support banded rasters at the moment |
|
343 |
// get channel (data) offsets |
|
2 | 344 |
rasterP->chanOffsets = NULL; |
345 |
if (SAFE_TO_ALLOC_2(rasterP->numDataElements, sizeof(jint))) { |
|
346 |
rasterP->chanOffsets = |
|
347 |
(jint *)malloc(rasterP->numDataElements * sizeof(jint)); |
|
348 |
} |
|
349 |
if (rasterP->chanOffsets == NULL) { |
|
350 |
/* Out of memory */ |
|
351 |
JNU_ThrowOutOfMemoryError(env, "Out of memory"); |
|
352 |
return -1; |
|
353 |
} |
|
354 |
(*env)->GetIntArrayRegion(env, joffs, 0, rasterP->numDataElements, |
|
355 |
rasterP->chanOffsets); |
|
16093 | 356 |
if (rasterP->jdata == NULL) { |
357 |
// unable to verify the raster |
|
358 |
return -1; |
|
359 |
} |
|
360 |
// verify whether channel offsets look sane |
|
361 |
if (!checkChannelOffsets(rasterP, (*env)->GetArrayLength(env, rasterP->jdata))) { |
|
362 |
return -1; |
|
363 |
} |
|
364 |
break; |
|
365 |
default: |
|
366 |
; // PACKED_RASTER_TYPE does not use the channel offsets. |
|
2 | 367 |
} |
368 |
||
16093 | 369 |
/* additional check for sppsm fields validity: make sure that |
370 |
* size of raster samples doesn't exceed the data type capacity. |
|
7746
3cdbc9c4de5b
6782574: AffineTransformOp.filter(BufferedImage, BufferedImage) fails with InternalError
bae
parents:
5506
diff
changeset
|
371 |
*/ |
3cdbc9c4de5b
6782574: AffineTransformOp.filter(BufferedImage, BufferedImage) fails with InternalError
bae
parents:
5506
diff
changeset
|
372 |
if (rasterP->dataType > UNKNOWN_DATA_TYPE && /* data type has been recognized */ |
3cdbc9c4de5b
6782574: AffineTransformOp.filter(BufferedImage, BufferedImage) fails with InternalError
bae
parents:
5506
diff
changeset
|
373 |
rasterP->sppsm.maxBitSize > 0 && /* raster has SPP sample model */ |
3cdbc9c4de5b
6782574: AffineTransformOp.filter(BufferedImage, BufferedImage) fails with InternalError
bae
parents:
5506
diff
changeset
|
374 |
rasterP->sppsm.maxBitSize > (rasterP->dataSize * 8)) |
3cdbc9c4de5b
6782574: AffineTransformOp.filter(BufferedImage, BufferedImage) fails with InternalError
bae
parents:
5506
diff
changeset
|
375 |
{ |
3cdbc9c4de5b
6782574: AffineTransformOp.filter(BufferedImage, BufferedImage) fails with InternalError
bae
parents:
5506
diff
changeset
|
376 |
JNU_ThrowInternalError(env, "Raster samples are too big"); |
3cdbc9c4de5b
6782574: AffineTransformOp.filter(BufferedImage, BufferedImage) fails with InternalError
bae
parents:
5506
diff
changeset
|
377 |
return -1; |
3cdbc9c4de5b
6782574: AffineTransformOp.filter(BufferedImage, BufferedImage) fails with InternalError
bae
parents:
5506
diff
changeset
|
378 |
} |
3cdbc9c4de5b
6782574: AffineTransformOp.filter(BufferedImage, BufferedImage) fails with InternalError
bae
parents:
5506
diff
changeset
|
379 |
|
2 | 380 |
#if 0 |
381 |
fprintf(stderr,"---------------------\n"); |
|
382 |
fprintf(stderr,"Width : %d\n",rasterP->width); |
|
383 |
fprintf(stderr,"Height : %d\n",rasterP->height); |
|
384 |
fprintf(stderr,"X : %d\n",rasterP->x); |
|
385 |
fprintf(stderr,"Y : %d\n",rasterP->y); |
|
386 |
fprintf(stderr,"numC : %d\n",rasterP->numDataElements); |
|
387 |
fprintf(stderr,"SS : %d\n",rasterP->scanlineStride); |
|
388 |
fprintf(stderr,"PS : %d\n",rasterP->pixelStride); |
|
389 |
fprintf(stderr,"CO : %d\n",rasterP->chanOffsets); |
|
390 |
fprintf(stderr,"shared?: %d\n",rasterP->dataIsShared); |
|
391 |
fprintf(stderr,"RasterT: %d\n",rasterP->rasterType); |
|
392 |
fprintf(stderr,"DataT : %d\n",rasterP->dataType); |
|
393 |
fprintf(stderr,"---------------------\n"); |
|
394 |
#endif |
|
395 |
||
396 |
return 1; |
|
397 |
} |
|
398 |
||
18258 | 399 |
static int getColorModelType(JNIEnv *env, jobject jcmodel) { |
400 |
int type = UNKNOWN_CM_TYPE; |
|
401 |
||
402 |
if ((*env)->IsInstanceOf(env, jcmodel, |
|
403 |
(*env)->FindClass(env, "java/awt/image/IndexColorModel"))) |
|
404 |
{ |
|
405 |
type = INDEX_CM_TYPE; |
|
406 |
} else if ((*env)->IsInstanceOf(env, jcmodel, |
|
407 |
(*env)->FindClass(env, "java/awt/image/PackedColorModel"))) |
|
408 |
{ |
|
409 |
if ((*env)->IsInstanceOf(env, jcmodel, |
|
410 |
(*env)->FindClass(env, "java/awt/image/DirectColorModel"))) { |
|
411 |
type = DIRECT_CM_TYPE; |
|
412 |
} |
|
413 |
else { |
|
414 |
type = PACKED_CM_TYPE; |
|
415 |
} |
|
416 |
} |
|
417 |
else if ((*env)->IsInstanceOf(env, jcmodel, |
|
418 |
(*env)->FindClass(env, "java/awt/image/ComponentColorModel"))) |
|
419 |
{ |
|
420 |
type = COMPONENT_CM_TYPE; |
|
421 |
} |
|
422 |
||
423 |
return type; |
|
424 |
} |
|
425 |
||
2 | 426 |
int awt_parseColorModel (JNIEnv *env, jobject jcmodel, int imageType, |
427 |
ColorModelS_t *cmP) { |
|
428 |
/*jmethodID jID; */ |
|
429 |
jobject jnBits; |
|
18258 | 430 |
jsize nBitsLength; |
431 |
||
2 | 432 |
int i; |
433 |
static jobject s_jdefCM = NULL; |
|
434 |
||
435 |
if (JNU_IsNull(env, jcmodel)) { |
|
436 |
JNU_ThrowNullPointerException(env, "null ColorModel object"); |
|
437 |
return -1; |
|
438 |
} |
|
439 |
||
440 |
cmP->jcmodel = jcmodel; |
|
441 |
||
442 |
cmP->jcspace = (*env)->GetObjectField(env, jcmodel, g_CMcspaceID); |
|
443 |
||
444 |
cmP->numComponents = (*env)->GetIntField(env, jcmodel, |
|
445 |
g_CMnumComponentsID); |
|
446 |
cmP->supportsAlpha = (*env)->GetBooleanField(env, jcmodel, |
|
447 |
g_CMsuppAlphaID); |
|
448 |
cmP->isAlphaPre = (*env)->GetBooleanField(env,jcmodel, |
|
449 |
g_CMisAlphaPreID); |
|
450 |
cmP->transparency = (*env)->GetIntField(env, jcmodel, |
|
451 |
g_CMtransparencyID); |
|
452 |
||
18258 | 453 |
jnBits = (*env)->GetObjectField(env, jcmodel, g_CMnBitsID); |
454 |
if (jnBits == NULL) { |
|
455 |
JNU_ThrowNullPointerException(env, "null nBits structure in CModel"); |
|
456 |
return -1; |
|
457 |
} |
|
458 |
||
459 |
nBitsLength = (*env)->GetArrayLength(env, jnBits); |
|
460 |
if (nBitsLength != cmP->numComponents) { |
|
461 |
// invalid number of components? |
|
462 |
return -1; |
|
463 |
} |
|
464 |
||
465 |
cmP->nBits = NULL; |
|
466 |
if (SAFE_TO_ALLOC_2(cmP->numComponents, sizeof(jint))) { |
|
467 |
cmP->nBits = (jint *)malloc(cmP->numComponents * sizeof(jint)); |
|
468 |
} |
|
469 |
||
470 |
if (cmP->nBits == NULL){ |
|
471 |
JNU_ThrowOutOfMemoryError(env, "Out of memory"); |
|
472 |
return -1; |
|
473 |
} |
|
474 |
(*env)->GetIntArrayRegion(env, jnBits, 0, cmP->numComponents, |
|
475 |
cmP->nBits); |
|
476 |
cmP->maxNbits = 0; |
|
477 |
for (i=0; i < cmP->numComponents; i++) { |
|
478 |
if (cmP->maxNbits < cmP->nBits[i]) { |
|
479 |
cmP->maxNbits = cmP->nBits[i]; |
|
480 |
} |
|
481 |
} |
|
482 |
||
483 |
cmP->is_sRGB = (*env)->GetBooleanField(env, cmP->jcmodel, g_CMis_sRGBID); |
|
484 |
||
485 |
cmP->csType = (*env)->GetIntField(env, cmP->jcmodel, g_CMcsTypeID); |
|
486 |
||
487 |
cmP->cmType = getColorModelType(env, jcmodel); |
|
488 |
||
489 |
cmP->isDefaultCM = FALSE; |
|
490 |
cmP->isDefaultCompatCM = FALSE; |
|
491 |
||
492 |
/* look for standard cases */ |
|
2 | 493 |
if (imageType == java_awt_image_BufferedImage_TYPE_INT_ARGB) { |
494 |
cmP->isDefaultCM = TRUE; |
|
495 |
cmP->isDefaultCompatCM = TRUE; |
|
496 |
} else if (imageType == java_awt_image_BufferedImage_TYPE_INT_ARGB_PRE || |
|
18258 | 497 |
imageType == java_awt_image_BufferedImage_TYPE_INT_RGB || |
498 |
imageType == java_awt_image_BufferedImage_TYPE_INT_BGR || |
|
2 | 499 |
imageType == java_awt_image_BufferedImage_TYPE_4BYTE_ABGR || |
18258 | 500 |
imageType == java_awt_image_BufferedImage_TYPE_4BYTE_ABGR_PRE) |
501 |
{ |
|
2 | 502 |
cmP->isDefaultCompatCM = TRUE; |
503 |
} |
|
504 |
else { |
|
505 |
/* Figure out if this is the default CM */ |
|
506 |
if (s_jdefCM == NULL) { |
|
507 |
jobject defCM; |
|
508 |
jclass jcm = (*env)->FindClass(env, "java/awt/image/ColorModel"); |
|
509 |
defCM = (*env)->CallStaticObjectMethod(env, jcm, |
|
510 |
g_CMgetRGBdefaultMID, NULL); |
|
511 |
s_jdefCM = (*env)->NewGlobalRef(env, defCM); |
|
512 |
if (defCM == NULL || s_jdefCM == NULL) { |
|
513 |
JNU_ThrowNullPointerException(env, |
|
514 |
"Unable to find default CM"); |
|
515 |
return -1; |
|
516 |
} |
|
517 |
} |
|
518 |
cmP->isDefaultCM = ((*env)->IsSameObject(env, s_jdefCM, jcmodel)); |
|
519 |
cmP->isDefaultCompatCM = cmP->isDefaultCM; |
|
520 |
} |
|
521 |
||
18258 | 522 |
/* check whether image attributes correspond to default cm */ |
2 | 523 |
if (cmP->isDefaultCompatCM) { |
18258 | 524 |
if (cmP->csType != java_awt_color_ColorSpace_TYPE_RGB || |
525 |
!cmP->is_sRGB) |
|
526 |
{ |
|
527 |
return -1; |
|
528 |
} |
|
2 | 529 |
|
18258 | 530 |
for (i = 0; i < cmP->numComponents; i++) { |
531 |
if (cmP->nBits[i] != 8) { |
|
532 |
return -1; |
|
533 |
} |
|
2 | 534 |
} |
535 |
} |
|
536 |
||
18258 | 537 |
/* Get index color model attributes */ |
2 | 538 |
if (imageType == java_awt_image_BufferedImage_TYPE_BYTE_INDEXED || |
18258 | 539 |
cmP->cmType == INDEX_CM_TYPE) |
2 | 540 |
{ |
541 |
cmP->transIdx = (*env)->GetIntField(env, jcmodel, g_ICMtransIdxID); |
|
542 |
cmP->mapSize = (*env)->GetIntField(env, jcmodel, g_ICMmapSizeID); |
|
543 |
cmP->jrgb = (*env)->GetObjectField(env, jcmodel, g_ICMrgbID); |
|
544 |
if (cmP->transIdx == -1) { |
|
545 |
/* Need to find the transparent index */ |
|
546 |
int *rgb = (int *) (*env)->GetPrimitiveArrayCritical(env, |
|
547 |
cmP->jrgb, |
|
548 |
NULL); |
|
549 |
if (rgb == NULL) { |
|
550 |
return -1; |
|
551 |
} |
|
552 |
for (i=0; i < cmP->mapSize; i++) { |
|
553 |
if ((rgb[i]&0xff000000) == 0) { |
|
554 |
cmP->transIdx = i; |
|
555 |
break; |
|
556 |
} |
|
557 |
} |
|
558 |
(*env)->ReleasePrimitiveArrayCritical(env, cmP->jrgb, rgb, |
|
559 |
JNI_ABORT); |
|
560 |
if (cmP->transIdx == -1) { |
|
561 |
/* Now what? No transparent pixel... */ |
|
562 |
cmP->transIdx = 0; |
|
563 |
} |
|
564 |
} |
|
565 |
} |
|
566 |
||
567 |
return 1; |
|
568 |
} |
|
569 |
||
570 |
void awt_freeParsedRaster(RasterS_t *rasterP, int freeRasterP) { |
|
571 |
if (rasterP->chanOffsets) { |
|
572 |
free((void *) rasterP->chanOffsets); |
|
573 |
} |
|
574 |
||
575 |
if (freeRasterP) { |
|
576 |
free((void *) rasterP); |
|
577 |
} |
|
578 |
} |
|
579 |
||
580 |
void awt_freeParsedImage(BufImageS_t *imageP, int freeImageP) { |
|
581 |
if (imageP->hints.colorOrder) { |
|
582 |
free ((void *) imageP->hints.colorOrder); |
|
583 |
} |
|
584 |
||
585 |
if (imageP->cmodel.nBits) { |
|
586 |
free ((void *) imageP->cmodel.nBits); |
|
587 |
} |
|
588 |
||
589 |
/* Free the raster */ |
|
590 |
awt_freeParsedRaster(&imageP->raster, FALSE); |
|
591 |
||
592 |
if (freeImageP) { |
|
593 |
free((void *) imageP); |
|
594 |
} |
|
595 |
} |
|
596 |
||
15502
6aad6647696c
8004801: The image of BufferedImage.TYPE_INT_ARGB is blank.
bae
parents:
9035
diff
changeset
|
597 |
static void |
6aad6647696c
8004801: The image of BufferedImage.TYPE_INT_ARGB is blank.
bae
parents:
9035
diff
changeset
|
598 |
awt_getBIColorOrder(int type, int *colorOrder) { |
6aad6647696c
8004801: The image of BufferedImage.TYPE_INT_ARGB is blank.
bae
parents:
9035
diff
changeset
|
599 |
switch(type) { |
6aad6647696c
8004801: The image of BufferedImage.TYPE_INT_ARGB is blank.
bae
parents:
9035
diff
changeset
|
600 |
case java_awt_image_BufferedImage_TYPE_INT_ARGB: |
6aad6647696c
8004801: The image of BufferedImage.TYPE_INT_ARGB is blank.
bae
parents:
9035
diff
changeset
|
601 |
case java_awt_image_BufferedImage_TYPE_INT_ARGB_PRE: |
6aad6647696c
8004801: The image of BufferedImage.TYPE_INT_ARGB is blank.
bae
parents:
9035
diff
changeset
|
602 |
#ifdef _LITTLE_ENDIAN |
6aad6647696c
8004801: The image of BufferedImage.TYPE_INT_ARGB is blank.
bae
parents:
9035
diff
changeset
|
603 |
colorOrder[0] = 2; |
6aad6647696c
8004801: The image of BufferedImage.TYPE_INT_ARGB is blank.
bae
parents:
9035
diff
changeset
|
604 |
colorOrder[1] = 1; |
6aad6647696c
8004801: The image of BufferedImage.TYPE_INT_ARGB is blank.
bae
parents:
9035
diff
changeset
|
605 |
colorOrder[2] = 0; |
6aad6647696c
8004801: The image of BufferedImage.TYPE_INT_ARGB is blank.
bae
parents:
9035
diff
changeset
|
606 |
colorOrder[3] = 3; |
6aad6647696c
8004801: The image of BufferedImage.TYPE_INT_ARGB is blank.
bae
parents:
9035
diff
changeset
|
607 |
#else |
6aad6647696c
8004801: The image of BufferedImage.TYPE_INT_ARGB is blank.
bae
parents:
9035
diff
changeset
|
608 |
colorOrder[0] = 1; |
6aad6647696c
8004801: The image of BufferedImage.TYPE_INT_ARGB is blank.
bae
parents:
9035
diff
changeset
|
609 |
colorOrder[1] = 2; |
6aad6647696c
8004801: The image of BufferedImage.TYPE_INT_ARGB is blank.
bae
parents:
9035
diff
changeset
|
610 |
colorOrder[2] = 3; |
6aad6647696c
8004801: The image of BufferedImage.TYPE_INT_ARGB is blank.
bae
parents:
9035
diff
changeset
|
611 |
colorOrder[3] = 0; |
6aad6647696c
8004801: The image of BufferedImage.TYPE_INT_ARGB is blank.
bae
parents:
9035
diff
changeset
|
612 |
#endif |
6aad6647696c
8004801: The image of BufferedImage.TYPE_INT_ARGB is blank.
bae
parents:
9035
diff
changeset
|
613 |
break; |
6aad6647696c
8004801: The image of BufferedImage.TYPE_INT_ARGB is blank.
bae
parents:
9035
diff
changeset
|
614 |
case java_awt_image_BufferedImage_TYPE_INT_BGR: |
6aad6647696c
8004801: The image of BufferedImage.TYPE_INT_ARGB is blank.
bae
parents:
9035
diff
changeset
|
615 |
#ifdef _LITTLE_ENDIAN |
6aad6647696c
8004801: The image of BufferedImage.TYPE_INT_ARGB is blank.
bae
parents:
9035
diff
changeset
|
616 |
colorOrder[0] = 0; |
6aad6647696c
8004801: The image of BufferedImage.TYPE_INT_ARGB is blank.
bae
parents:
9035
diff
changeset
|
617 |
colorOrder[1] = 1; |
6aad6647696c
8004801: The image of BufferedImage.TYPE_INT_ARGB is blank.
bae
parents:
9035
diff
changeset
|
618 |
colorOrder[2] = 2; |
6aad6647696c
8004801: The image of BufferedImage.TYPE_INT_ARGB is blank.
bae
parents:
9035
diff
changeset
|
619 |
#else |
6aad6647696c
8004801: The image of BufferedImage.TYPE_INT_ARGB is blank.
bae
parents:
9035
diff
changeset
|
620 |
colorOrder[0] = 3; |
6aad6647696c
8004801: The image of BufferedImage.TYPE_INT_ARGB is blank.
bae
parents:
9035
diff
changeset
|
621 |
colorOrder[1] = 2; |
6aad6647696c
8004801: The image of BufferedImage.TYPE_INT_ARGB is blank.
bae
parents:
9035
diff
changeset
|
622 |
colorOrder[2] = 1; |
6aad6647696c
8004801: The image of BufferedImage.TYPE_INT_ARGB is blank.
bae
parents:
9035
diff
changeset
|
623 |
#endif |
6aad6647696c
8004801: The image of BufferedImage.TYPE_INT_ARGB is blank.
bae
parents:
9035
diff
changeset
|
624 |
break; |
6aad6647696c
8004801: The image of BufferedImage.TYPE_INT_ARGB is blank.
bae
parents:
9035
diff
changeset
|
625 |
case java_awt_image_BufferedImage_TYPE_INT_RGB: |
6aad6647696c
8004801: The image of BufferedImage.TYPE_INT_ARGB is blank.
bae
parents:
9035
diff
changeset
|
626 |
#ifdef _LITTLE_ENDIAN |
6aad6647696c
8004801: The image of BufferedImage.TYPE_INT_ARGB is blank.
bae
parents:
9035
diff
changeset
|
627 |
colorOrder[0] = 2; |
6aad6647696c
8004801: The image of BufferedImage.TYPE_INT_ARGB is blank.
bae
parents:
9035
diff
changeset
|
628 |
colorOrder[1] = 1; |
6aad6647696c
8004801: The image of BufferedImage.TYPE_INT_ARGB is blank.
bae
parents:
9035
diff
changeset
|
629 |
colorOrder[2] = 0; |
6aad6647696c
8004801: The image of BufferedImage.TYPE_INT_ARGB is blank.
bae
parents:
9035
diff
changeset
|
630 |
#else |
6aad6647696c
8004801: The image of BufferedImage.TYPE_INT_ARGB is blank.
bae
parents:
9035
diff
changeset
|
631 |
colorOrder[0] = 1; |
6aad6647696c
8004801: The image of BufferedImage.TYPE_INT_ARGB is blank.
bae
parents:
9035
diff
changeset
|
632 |
colorOrder[1] = 2; |
6aad6647696c
8004801: The image of BufferedImage.TYPE_INT_ARGB is blank.
bae
parents:
9035
diff
changeset
|
633 |
colorOrder[2] = 3; |
6aad6647696c
8004801: The image of BufferedImage.TYPE_INT_ARGB is blank.
bae
parents:
9035
diff
changeset
|
634 |
#endif |
6aad6647696c
8004801: The image of BufferedImage.TYPE_INT_ARGB is blank.
bae
parents:
9035
diff
changeset
|
635 |
break; |
6aad6647696c
8004801: The image of BufferedImage.TYPE_INT_ARGB is blank.
bae
parents:
9035
diff
changeset
|
636 |
case java_awt_image_BufferedImage_TYPE_4BYTE_ABGR: |
6aad6647696c
8004801: The image of BufferedImage.TYPE_INT_ARGB is blank.
bae
parents:
9035
diff
changeset
|
637 |
case java_awt_image_BufferedImage_TYPE_4BYTE_ABGR_PRE: |
6aad6647696c
8004801: The image of BufferedImage.TYPE_INT_ARGB is blank.
bae
parents:
9035
diff
changeset
|
638 |
colorOrder[0] = 3; |
6aad6647696c
8004801: The image of BufferedImage.TYPE_INT_ARGB is blank.
bae
parents:
9035
diff
changeset
|
639 |
colorOrder[1] = 2; |
6aad6647696c
8004801: The image of BufferedImage.TYPE_INT_ARGB is blank.
bae
parents:
9035
diff
changeset
|
640 |
colorOrder[2] = 1; |
6aad6647696c
8004801: The image of BufferedImage.TYPE_INT_ARGB is blank.
bae
parents:
9035
diff
changeset
|
641 |
colorOrder[3] = 0; |
6aad6647696c
8004801: The image of BufferedImage.TYPE_INT_ARGB is blank.
bae
parents:
9035
diff
changeset
|
642 |
break; |
6aad6647696c
8004801: The image of BufferedImage.TYPE_INT_ARGB is blank.
bae
parents:
9035
diff
changeset
|
643 |
case java_awt_image_BufferedImage_TYPE_3BYTE_BGR: |
6aad6647696c
8004801: The image of BufferedImage.TYPE_INT_ARGB is blank.
bae
parents:
9035
diff
changeset
|
644 |
colorOrder[0] = 2; |
6aad6647696c
8004801: The image of BufferedImage.TYPE_INT_ARGB is blank.
bae
parents:
9035
diff
changeset
|
645 |
colorOrder[1] = 1; |
6aad6647696c
8004801: The image of BufferedImage.TYPE_INT_ARGB is blank.
bae
parents:
9035
diff
changeset
|
646 |
colorOrder[2] = 0; |
6aad6647696c
8004801: The image of BufferedImage.TYPE_INT_ARGB is blank.
bae
parents:
9035
diff
changeset
|
647 |
break; |
6aad6647696c
8004801: The image of BufferedImage.TYPE_INT_ARGB is blank.
bae
parents:
9035
diff
changeset
|
648 |
case java_awt_image_BufferedImage_TYPE_USHORT_565_RGB: |
6aad6647696c
8004801: The image of BufferedImage.TYPE_INT_ARGB is blank.
bae
parents:
9035
diff
changeset
|
649 |
case java_awt_image_BufferedImage_TYPE_USHORT_555_RGB: |
6aad6647696c
8004801: The image of BufferedImage.TYPE_INT_ARGB is blank.
bae
parents:
9035
diff
changeset
|
650 |
colorOrder[0] = 0; |
6aad6647696c
8004801: The image of BufferedImage.TYPE_INT_ARGB is blank.
bae
parents:
9035
diff
changeset
|
651 |
colorOrder[1] = 1; |
6aad6647696c
8004801: The image of BufferedImage.TYPE_INT_ARGB is blank.
bae
parents:
9035
diff
changeset
|
652 |
colorOrder[2] = 2; |
6aad6647696c
8004801: The image of BufferedImage.TYPE_INT_ARGB is blank.
bae
parents:
9035
diff
changeset
|
653 |
break; |
6aad6647696c
8004801: The image of BufferedImage.TYPE_INT_ARGB is blank.
bae
parents:
9035
diff
changeset
|
654 |
case java_awt_image_BufferedImage_TYPE_BYTE_GRAY: |
6aad6647696c
8004801: The image of BufferedImage.TYPE_INT_ARGB is blank.
bae
parents:
9035
diff
changeset
|
655 |
case java_awt_image_BufferedImage_TYPE_USHORT_GRAY: |
6aad6647696c
8004801: The image of BufferedImage.TYPE_INT_ARGB is blank.
bae
parents:
9035
diff
changeset
|
656 |
case java_awt_image_BufferedImage_TYPE_BYTE_BINARY: |
6aad6647696c
8004801: The image of BufferedImage.TYPE_INT_ARGB is blank.
bae
parents:
9035
diff
changeset
|
657 |
case java_awt_image_BufferedImage_TYPE_BYTE_INDEXED: |
6aad6647696c
8004801: The image of BufferedImage.TYPE_INT_ARGB is blank.
bae
parents:
9035
diff
changeset
|
658 |
colorOrder[0] = 0; |
6aad6647696c
8004801: The image of BufferedImage.TYPE_INT_ARGB is blank.
bae
parents:
9035
diff
changeset
|
659 |
break; |
6aad6647696c
8004801: The image of BufferedImage.TYPE_INT_ARGB is blank.
bae
parents:
9035
diff
changeset
|
660 |
} |
6aad6647696c
8004801: The image of BufferedImage.TYPE_INT_ARGB is blank.
bae
parents:
9035
diff
changeset
|
661 |
} |
2 | 662 |
|
663 |
static int |
|
664 |
setHints(JNIEnv *env, BufImageS_t *imageP) { |
|
665 |
HintS_t *hintP = &imageP->hints; |
|
666 |
RasterS_t *rasterP = &imageP->raster; |
|
667 |
ColorModelS_t *cmodelP = &imageP->cmodel; |
|
668 |
int imageType = imageP->imageType; |
|
669 |
||
18258 | 670 |
// check whether raster and color model are compatible |
671 |
if (cmodelP->numComponents != rasterP->numBands) { |
|
672 |
if (cmodelP->cmType != INDEX_CM_TYPE) { |
|
673 |
return -1; |
|
674 |
} |
|
675 |
} |
|
676 |
||
2 | 677 |
hintP->numChans = imageP->cmodel.numComponents; |
678 |
hintP->colorOrder = NULL; |
|
679 |
if (SAFE_TO_ALLOC_2(hintP->numChans, sizeof(int))) { |
|
680 |
hintP->colorOrder = (int *)malloc(hintP->numChans * sizeof(int)); |
|
681 |
} |
|
682 |
if (hintP->colorOrder == NULL) { |
|
683 |
JNU_ThrowOutOfMemoryError(env, "Out of memory"); |
|
684 |
return -1; |
|
685 |
} |
|
686 |
if (imageType != java_awt_image_BufferedImage_TYPE_CUSTOM) { |
|
687 |
awt_getBIColorOrder(imageType, hintP->colorOrder); |
|
688 |
} |
|
689 |
if (imageType == java_awt_image_BufferedImage_TYPE_INT_ARGB || |
|
690 |
imageType == java_awt_image_BufferedImage_TYPE_INT_ARGB_PRE || |
|
691 |
imageType == java_awt_image_BufferedImage_TYPE_INT_RGB) |
|
692 |
{ |
|
693 |
hintP->channelOffset = rasterP->chanOffsets[0]; |
|
694 |
/* These hints are #bytes */ |
|
695 |
hintP->dataOffset = hintP->channelOffset*rasterP->dataSize; |
|
696 |
hintP->sStride = rasterP->scanlineStride*rasterP->dataSize; |
|
697 |
hintP->pStride = rasterP->pixelStride*rasterP->dataSize; |
|
698 |
hintP->packing = BYTE_INTERLEAVED; |
|
699 |
} else if (imageType ==java_awt_image_BufferedImage_TYPE_4BYTE_ABGR || |
|
700 |
imageType==java_awt_image_BufferedImage_TYPE_4BYTE_ABGR_PRE|| |
|
701 |
imageType == java_awt_image_BufferedImage_TYPE_3BYTE_BGR || |
|
702 |
imageType == java_awt_image_BufferedImage_TYPE_INT_BGR) |
|
703 |
{ |
|
704 |
if (imageType == java_awt_image_BufferedImage_TYPE_INT_BGR) { |
|
705 |
hintP->channelOffset = rasterP->chanOffsets[0]; |
|
706 |
} |
|
707 |
else { |
|
708 |
hintP->channelOffset = rasterP->chanOffsets[hintP->numChans-1]; |
|
709 |
} |
|
710 |
hintP->dataOffset = hintP->channelOffset*rasterP->dataSize; |
|
711 |
hintP->sStride = rasterP->scanlineStride*rasterP->dataSize; |
|
712 |
hintP->pStride = rasterP->pixelStride*rasterP->dataSize; |
|
713 |
hintP->packing = BYTE_INTERLEAVED; |
|
714 |
} else if (imageType==java_awt_image_BufferedImage_TYPE_USHORT_565_RGB || |
|
715 |
imageType==java_awt_image_BufferedImage_TYPE_USHORT_555_RGB) { |
|
716 |
hintP->needToExpand = TRUE; |
|
717 |
hintP->expandToNbits = 8; |
|
718 |
hintP->packing = PACKED_SHORT_INTER; |
|
719 |
} else if (cmodelP->cmType == INDEX_CM_TYPE) { |
|
720 |
int i; |
|
721 |
hintP->numChans = 1; |
|
722 |
hintP->channelOffset = rasterP->chanOffsets[0]; |
|
723 |
hintP->dataOffset = hintP->channelOffset*rasterP->dataSize; |
|
724 |
hintP->sStride = rasterP->scanlineStride*rasterP->dataSize; |
|
725 |
hintP->pStride = rasterP->pixelStride*rasterP->dataSize; |
|
726 |
switch(rasterP->dataType ) { |
|
727 |
case BYTE_DATA_TYPE: |
|
728 |
if (rasterP->rasterType == PACKED_RASTER_TYPE) { |
|
729 |
hintP->needToExpand = TRUE; |
|
730 |
hintP->expandToNbits = 8; |
|
731 |
hintP->packing = BYTE_PACKED_BAND; |
|
732 |
} |
|
733 |
else { |
|
734 |
hintP->packing = BYTE_SINGLE_BAND; |
|
735 |
} |
|
736 |
break; |
|
737 |
case SHORT_DATA_TYPE: |
|
738 |
hintP->packing = SHORT_SINGLE_BAND; |
|
739 |
break; |
|
740 |
case INT_DATA_TYPE: |
|
741 |
default: |
|
742 |
hintP->packing = UNKNOWN_PACKING; |
|
743 |
break; |
|
744 |
} |
|
745 |
for (i=0; i < hintP->numChans; i++) { |
|
746 |
hintP->colorOrder[i] = i; |
|
747 |
} |
|
748 |
} |
|
749 |
else if (cmodelP->cmType == COMPONENT_CM_TYPE) { |
|
750 |
/* Figure out if it is interleaved */ |
|
751 |
int bits=1; |
|
752 |
int i; |
|
753 |
int low = rasterP->chanOffsets[0]; |
|
754 |
int diff; |
|
755 |
int banded = 0; |
|
756 |
for (i=1; i < hintP->numChans; i++) { |
|
757 |
if (rasterP->chanOffsets[i] < low) { |
|
758 |
low = rasterP->chanOffsets[i]; |
|
759 |
} |
|
760 |
} |
|
761 |
for (i=1; i < hintP->numChans; i++) { |
|
762 |
diff = rasterP->chanOffsets[i]-low; |
|
763 |
if (diff < hintP->numChans) { |
|
764 |
if (bits & (1<<diff)) { |
|
765 |
/* Overlapping samples */ |
|
766 |
/* Could just copy */ |
|
767 |
return -1; |
|
768 |
} |
|
769 |
bits |= (1<<diff); |
|
770 |
} |
|
771 |
else if (diff >= rasterP->width) { |
|
772 |
banded = 1; |
|
773 |
} |
|
774 |
/* Ignore the case if bands are overlapping */ |
|
775 |
} |
|
776 |
hintP->channelOffset = low; |
|
777 |
hintP->dataOffset = low*rasterP->dataSize; |
|
778 |
hintP->sStride = rasterP->scanlineStride*rasterP->dataSize; |
|
779 |
hintP->pStride = rasterP->pixelStride*rasterP->dataSize; |
|
780 |
switch(rasterP->dataType) { |
|
781 |
case BYTE_DATA_TYPE: |
|
782 |
hintP->packing = BYTE_COMPONENTS; |
|
783 |
break; |
|
784 |
case SHORT_DATA_TYPE: |
|
785 |
hintP->packing = SHORT_COMPONENTS; |
|
786 |
break; |
|
787 |
default: |
|
788 |
/* Don't handle any other case */ |
|
789 |
return -1; |
|
790 |
} |
|
791 |
if (bits == ((1<<hintP->numChans)-1)) { |
|
792 |
hintP->packing |= INTERLEAVED; |
|
793 |
for (i=0; i < hintP->numChans; i++) { |
|
794 |
hintP->colorOrder[rasterP->chanOffsets[i]-low] = i; |
|
795 |
} |
|
796 |
} |
|
797 |
else if (banded == 1) { |
|
798 |
int bandSize = rasterP->width*rasterP->height; |
|
799 |
hintP->packing |= BANDED; |
|
800 |
for (i=0; i < hintP->numChans; i++) { |
|
801 |
/* REMIND: Not necessarily correct */ |
|
802 |
hintP->colorOrder[(rasterP->chanOffsets[i]-low)%bandSize] = i; |
|
803 |
} |
|
804 |
} |
|
805 |
else { |
|
806 |
return -1; |
|
807 |
} |
|
808 |
} |
|
809 |
else if (cmodelP->cmType == DIRECT_CM_TYPE || cmodelP->cmType == PACKED_CM_TYPE) { |
|
810 |
int i; |
|
16096 | 811 |
|
812 |
/* do some sanity check first: make sure that |
|
813 |
* - sample model is SinglePixelPackedSampleModel |
|
814 |
* - number of bands in the raster corresponds to the number |
|
815 |
* of color components in the color model |
|
816 |
*/ |
|
817 |
if (!rasterP->sppsm.isUsed || |
|
818 |
rasterP->numBands != cmodelP->numComponents) |
|
819 |
{ |
|
820 |
/* given raster is not compatible with the color model, |
|
821 |
* so the operation has to be aborted. |
|
822 |
*/ |
|
823 |
return -1; |
|
824 |
} |
|
825 |
||
2 | 826 |
if (cmodelP->maxNbits > 8) { |
827 |
hintP->needToExpand = TRUE; |
|
828 |
hintP->expandToNbits = cmodelP->maxNbits; |
|
829 |
} |
|
830 |
else if (rasterP->sppsm.offsets != NULL) { |
|
831 |
for (i=0; i < rasterP->numBands; i++) { |
|
832 |
if (!(rasterP->sppsm.offsets[i] % 8)) { |
|
833 |
hintP->needToExpand = TRUE; |
|
834 |
hintP->expandToNbits = 8; |
|
835 |
break; |
|
836 |
} |
|
837 |
else { |
|
838 |
hintP->colorOrder[i] = rasterP->sppsm.offsets[i]>>3; |
|
839 |
} |
|
840 |
} |
|
841 |
} |
|
842 |
||
843 |
hintP->channelOffset = rasterP->chanOffsets[0]; |
|
844 |
hintP->dataOffset = hintP->channelOffset*rasterP->dataSize; |
|
845 |
hintP->sStride = rasterP->scanlineStride*rasterP->dataSize; |
|
846 |
hintP->pStride = rasterP->pixelStride*rasterP->dataSize; |
|
847 |
if (hintP->needToExpand) { |
|
848 |
switch(rasterP->dataType) { |
|
849 |
case BYTE_DATA_TYPE: |
|
850 |
hintP->packing = PACKED_BYTE_INTER; |
|
851 |
break; |
|
852 |
case SHORT_DATA_TYPE: |
|
853 |
hintP->packing = PACKED_SHORT_INTER; |
|
854 |
break; |
|
855 |
case INT_DATA_TYPE: |
|
856 |
hintP->packing = PACKED_INT_INTER; |
|
857 |
break; |
|
858 |
default: |
|
859 |
/* Don't know what it is */ |
|
860 |
return -1; |
|
861 |
} |
|
862 |
} |
|
863 |
else { |
|
864 |
hintP->packing = BYTE_INTERLEAVED; |
|
865 |
||
866 |
} |
|
867 |
} |
|
868 |
else { |
|
869 |
/* REMIND: Need to handle more cases */ |
|
870 |
return -1; |
|
871 |
} |
|
872 |
||
873 |
return 1; |
|
874 |
} |
|
875 |
||
876 |
#define MAX_TO_GRAB (10240) |
|
877 |
||
20799 | 878 |
typedef union { |
879 |
void *pv; |
|
880 |
unsigned char *pb; |
|
881 |
unsigned short *ps; |
|
882 |
} PixelData_t; |
|
883 |
||
884 |
||
885 |
int awt_getPixels(JNIEnv *env, RasterS_t *rasterP, void *bufferP) { |
|
886 |
const int w = rasterP->width; |
|
887 |
const int h = rasterP->height; |
|
888 |
const int numBands = rasterP->numBands; |
|
2 | 889 |
int y; |
890 |
int i; |
|
20799 | 891 |
int maxLines; |
2 | 892 |
jobject jsm; |
20799 | 893 |
int off = 0; |
2 | 894 |
jarray jdata = NULL; |
895 |
jobject jdatabuffer; |
|
896 |
int *dataP; |
|
20799 | 897 |
int maxSamples; |
898 |
PixelData_t p; |
|
899 |
||
900 |
if (bufferP == NULL) { |
|
901 |
return -1; |
|
902 |
} |
|
903 |
||
904 |
if (rasterP->dataType != BYTE_DATA_TYPE && |
|
905 |
rasterP->dataType != SHORT_DATA_TYPE) |
|
906 |
{ |
|
907 |
return -1; |
|
908 |
} |
|
909 |
||
910 |
p.pv = bufferP; |
|
911 |
||
912 |
if (!SAFE_TO_MULT(w, numBands)) { |
|
913 |
return -1; |
|
914 |
} |
|
915 |
maxSamples = w * numBands; |
|
916 |
||
917 |
maxLines = maxSamples > MAX_TO_GRAB ? 1 : (MAX_TO_GRAB / maxSamples); |
|
918 |
if (maxLines > h) { |
|
919 |
maxLines = h; |
|
920 |
} |
|
921 |
||
922 |
if (!SAFE_TO_MULT(maxSamples, maxLines)) { |
|
923 |
return -1; |
|
924 |
} |
|
925 |
||
926 |
maxSamples *= maxLines; |
|
2 | 927 |
|
928 |
jsm = (*env)->GetObjectField(env, rasterP->jraster, g_RasterSampleModelID); |
|
929 |
jdatabuffer = (*env)->GetObjectField(env, rasterP->jraster, |
|
930 |
g_RasterDataBufferID); |
|
20799 | 931 |
|
932 |
jdata = (*env)->NewIntArray(env, maxSamples); |
|
2 | 933 |
if (JNU_IsNull(env, jdata)) { |
934 |
JNU_ThrowOutOfMemoryError(env, "Out of Memory"); |
|
935 |
return -1; |
|
936 |
} |
|
937 |
||
20799 | 938 |
for (y = 0; y < h; y += maxLines) { |
939 |
if (y + maxLines > h) { |
|
940 |
maxLines = h - y; |
|
941 |
maxSamples = w * numBands * maxLines; |
|
942 |
} |
|
943 |
||
944 |
(*env)->CallObjectMethod(env, jsm, g_SMGetPixelsMID, |
|
945 |
0, y, w, |
|
946 |
maxLines, jdata, jdatabuffer); |
|
947 |
||
948 |
if ((*env)->ExceptionOccurred(env)) { |
|
2 | 949 |
(*env)->DeleteLocalRef(env, jdata); |
950 |
return -1; |
|
951 |
} |
|
952 |
||
20799 | 953 |
dataP = (int *) (*env)->GetPrimitiveArrayCritical(env, jdata, |
954 |
NULL); |
|
955 |
if (dataP == NULL) { |
|
956 |
(*env)->DeleteLocalRef(env, jdata); |
|
957 |
return -1; |
|
2 | 958 |
} |
959 |
||
20799 | 960 |
switch (rasterP->dataType) { |
961 |
case BYTE_DATA_TYPE: |
|
962 |
for (i = 0; i < maxSamples; i ++) { |
|
963 |
p.pb[off++] = (unsigned char) dataP[i]; |
|
964 |
} |
|
965 |
break; |
|
966 |
case SHORT_DATA_TYPE: |
|
967 |
for (i = 0; i < maxSamples; i ++) { |
|
968 |
p.ps[off++] = (unsigned short) dataP[i]; |
|
969 |
} |
|
970 |
break; |
|
971 |
} |
|
972 |
||
973 |
(*env)->ReleasePrimitiveArrayCritical(env, jdata, dataP, |
|
974 |
JNI_ABORT); |
|
2 | 975 |
} |
976 |
(*env)->DeleteLocalRef(env, jdata); |
|
977 |
||
20799 | 978 |
return 1; |
2 | 979 |
} |
20799 | 980 |
|
981 |
int awt_setPixels(JNIEnv *env, RasterS_t *rasterP, void *bufferP) { |
|
982 |
const int w = rasterP->width; |
|
983 |
const int h = rasterP->height; |
|
984 |
const int numBands = rasterP->numBands; |
|
985 |
||
2 | 986 |
int y; |
987 |
int i; |
|
20799 | 988 |
int maxLines; |
2 | 989 |
jobject jsm; |
20799 | 990 |
int off = 0; |
2 | 991 |
jarray jdata = NULL; |
992 |
jobject jdatabuffer; |
|
993 |
int *dataP; |
|
20799 | 994 |
int maxSamples; |
995 |
PixelData_t p; |
|
996 |
||
997 |
if (bufferP == NULL) { |
|
998 |
return -1; |
|
999 |
} |
|
1000 |
||
1001 |
if (rasterP->dataType != BYTE_DATA_TYPE && |
|
1002 |
rasterP->dataType != SHORT_DATA_TYPE) |
|
1003 |
{ |
|
1004 |
return -1; |
|
1005 |
} |
|
1006 |
||
1007 |
p.pv = bufferP; |
|
1008 |
||
1009 |
if (!SAFE_TO_MULT(w, numBands)) { |
|
1010 |
return -1; |
|
1011 |
} |
|
1012 |
maxSamples = w * numBands; |
|
1013 |
||
1014 |
maxLines = maxSamples > MAX_TO_GRAB ? 1 : (MAX_TO_GRAB / maxSamples); |
|
1015 |
if (maxLines > h) { |
|
1016 |
maxLines = h; |
|
1017 |
} |
|
1018 |
||
1019 |
if (!SAFE_TO_MULT(maxSamples, maxLines)) { |
|
1020 |
return -1; |
|
1021 |
} |
|
1022 |
||
1023 |
maxSamples *= maxLines; |
|
2 | 1024 |
|
1025 |
jsm = (*env)->GetObjectField(env, rasterP->jraster, g_RasterSampleModelID); |
|
1026 |
jdatabuffer = (*env)->GetObjectField(env, rasterP->jraster, |
|
1027 |
g_RasterDataBufferID); |
|
20799 | 1028 |
|
1029 |
jdata = (*env)->NewIntArray(env, maxSamples); |
|
2 | 1030 |
if (JNU_IsNull(env, jdata)) { |
1031 |
JNU_ThrowOutOfMemoryError(env, "Out of Memory"); |
|
1032 |
return -1; |
|
1033 |
} |
|
20799 | 1034 |
|
1035 |
for (y = 0; y < h; y += maxLines) { |
|
1036 |
if (y + maxLines > h) { |
|
1037 |
maxLines = h - y; |
|
1038 |
maxSamples = w * numBands * maxLines; |
|
1039 |
} |
|
1040 |
dataP = (int *) (*env)->GetPrimitiveArrayCritical(env, jdata, |
|
1041 |
NULL); |
|
1042 |
if (dataP == NULL) { |
|
2 | 1043 |
(*env)->DeleteLocalRef(env, jdata); |
1044 |
return -1; |
|
1045 |
} |
|
20799 | 1046 |
|
1047 |
switch (rasterP->dataType) { |
|
1048 |
case BYTE_DATA_TYPE: |
|
1049 |
for (i = 0; i < maxSamples; i ++) { |
|
1050 |
dataP[i] = p.pb[off++]; |
|
2 | 1051 |
} |
20799 | 1052 |
break; |
1053 |
case SHORT_DATA_TYPE: |
|
1054 |
for (i = 0; i < maxSamples; i ++) { |
|
1055 |
dataP[i] = p.ps[off++]; |
|
2 | 1056 |
} |
20799 | 1057 |
break; |
2 | 1058 |
} |
1059 |
||
20799 | 1060 |
(*env)->ReleasePrimitiveArrayCritical(env, jdata, dataP, |
1061 |
JNI_ABORT); |
|
1062 |
||
1063 |
(*env)->CallVoidMethod(env, jsm, g_SMSetPixelsMID, |
|
1064 |
0, y, w, |
|
1065 |
maxLines, jdata, jdatabuffer); |
|
1066 |
||
1067 |
if ((*env)->ExceptionOccurred(env)) { |
|
1068 |
(*env)->DeleteLocalRef(env, jdata); |
|
1069 |
return -1; |
|
1070 |
} |
|
2 | 1071 |
} |
1072 |
||
1073 |
(*env)->DeleteLocalRef(env, jdata); |
|
1074 |
||
20799 | 1075 |
return 1; |
2 | 1076 |
} |