author | bpb |
Mon, 23 Nov 2015 12:26:12 -0800 | |
changeset 34416 | 68c0d866db5d |
parent 25859 | 3317bb8137f4 |
child 35667 | ed476aba94de |
permissions | -rw-r--r-- |
2 | 1 |
/* |
34416
68c0d866db5d
8143342: Integrate Java Image I/O support for TIFF per JEP 262
bpb
parents:
25859
diff
changeset
|
2 |
* Copyright (c) 2003, 2015, Oracle and/or its affiliates. All rights reserved. |
2 | 3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 |
* |
|
5 |
* This code is free software; you can redistribute it and/or modify it |
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
|
5506 | 7 |
* published by the Free Software Foundation. Oracle designates this |
2 | 8 |
* particular file as subject to the "Classpath" exception as provided |
5506 | 9 |
* by Oracle in the LICENSE file that accompanied this code. |
2 | 10 |
* |
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
15 |
* accompanied this code). |
|
16 |
* |
|
17 |
* You should have received a copy of the GNU General Public License version |
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 |
* |
|
5506 | 21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
22 |
* or visit www.oracle.com if you need additional information or have any |
|
23 |
* questions. |
|
2 | 24 |
*/ |
25 |
||
26 |
package com.sun.imageio.plugins.common; |
|
27 |
||
28 |
import java.awt.Point; |
|
29 |
import java.awt.Rectangle; |
|
30 |
import java.awt.Transparency; |
|
31 |
import java.awt.color.ColorSpace; |
|
34416
68c0d866db5d
8143342: Integrate Java Image I/O support for TIFF per JEP 262
bpb
parents:
25859
diff
changeset
|
32 |
import java.awt.color.ICC_ColorSpace; |
2 | 33 |
import java.awt.image.BufferedImage; |
34 |
import java.awt.image.ColorModel; |
|
35 |
import java.awt.image.ComponentColorModel; |
|
36 |
import java.awt.image.ComponentSampleModel; |
|
37 |
import java.awt.image.DataBuffer; |
|
38 |
import java.awt.image.DataBufferByte; |
|
39 |
import java.awt.image.DataBufferInt; |
|
40 |
import java.awt.image.DataBufferShort; |
|
41 |
import java.awt.image.DataBufferUShort; |
|
42 |
import java.awt.image.DirectColorModel; |
|
43 |
import java.awt.image.IndexColorModel; |
|
44 |
import java.awt.image.MultiPixelPackedSampleModel; |
|
45 |
import java.awt.image.Raster; |
|
46 |
import java.awt.image.RenderedImage; |
|
47 |
import java.awt.image.SampleModel; |
|
48 |
import java.awt.image.SinglePixelPackedSampleModel; |
|
49 |
import java.awt.image.WritableRaster; |
|
50 |
import java.util.Arrays; |
|
34416
68c0d866db5d
8143342: Integrate Java Image I/O support for TIFF per JEP 262
bpb
parents:
25859
diff
changeset
|
51 |
import java.util.Iterator; |
2 | 52 |
import javax.imageio.IIOException; |
53 |
import javax.imageio.IIOImage; |
|
34416
68c0d866db5d
8143342: Integrate Java Image I/O support for TIFF per JEP 262
bpb
parents:
25859
diff
changeset
|
54 |
import javax.imageio.ImageReadParam; |
2 | 55 |
import javax.imageio.ImageTypeSpecifier; |
56 |
import javax.imageio.ImageWriter; |
|
57 |
import javax.imageio.spi.ImageWriterSpi; |
|
58 |
||
59 |
public class ImageUtil { |
|
60 |
/** |
|
61 |
* Creates a <code>ColorModel</code> that may be used with the |
|
62 |
* specified <code>SampleModel</code>. If a suitable |
|
63 |
* <code>ColorModel</code> cannot be found, this method returns |
|
64 |
* <code>null</code>. |
|
65 |
* |
|
66 |
* <p> Suitable <code>ColorModel</code>s are guaranteed to exist |
|
67 |
* for all instances of <code>ComponentSampleModel</code>. |
|
68 |
* For 1- and 3- banded <code>SampleModel</code>s, the returned |
|
69 |
* <code>ColorModel</code> will be opaque. For 2- and 4-banded |
|
70 |
* <code>SampleModel</code>s, the output will use alpha transparency |
|
71 |
* which is not premultiplied. 1- and 2-banded data will use a |
|
72 |
* grayscale <code>ColorSpace</code>, and 3- and 4-banded data a sRGB |
|
73 |
* <code>ColorSpace</code>. Data with 5 or more bands will have a |
|
74 |
* <code>BogusColorSpace</code>.</p> |
|
75 |
* |
|
76 |
* <p>An instance of <code>DirectColorModel</code> will be created for |
|
77 |
* instances of <code>SinglePixelPackedSampleModel</code> with no more |
|
78 |
* than 4 bands.</p> |
|
79 |
* |
|
80 |
* <p>An instance of <code>IndexColorModel</code> will be created for |
|
81 |
* instances of <code>MultiPixelPackedSampleModel</code>. The colormap |
|
82 |
* will be a grayscale ramp with <code>1 << numberOfBits</code> |
|
83 |
* entries ranging from zero to at most 255.</p> |
|
84 |
* |
|
85 |
* @return An instance of <code>ColorModel</code> that is suitable for |
|
86 |
* the supplied <code>SampleModel</code>, or <code>null</code>. |
|
87 |
* |
|
88 |
* @throws IllegalArgumentException If <code>sampleModel</code> is |
|
89 |
* <code>null</code>. |
|
90 |
*/ |
|
91 |
public static final ColorModel createColorModel(SampleModel sampleModel) { |
|
92 |
// Check the parameter. |
|
93 |
if(sampleModel == null) { |
|
94 |
throw new IllegalArgumentException("sampleModel == null!"); |
|
95 |
} |
|
96 |
||
97 |
// Get the data type. |
|
98 |
int dataType = sampleModel.getDataType(); |
|
99 |
||
100 |
// Check the data type |
|
101 |
switch(dataType) { |
|
102 |
case DataBuffer.TYPE_BYTE: |
|
103 |
case DataBuffer.TYPE_USHORT: |
|
104 |
case DataBuffer.TYPE_SHORT: |
|
105 |
case DataBuffer.TYPE_INT: |
|
106 |
case DataBuffer.TYPE_FLOAT: |
|
107 |
case DataBuffer.TYPE_DOUBLE: |
|
108 |
break; |
|
109 |
default: |
|
110 |
// Return null for other types. |
|
111 |
return null; |
|
112 |
} |
|
113 |
||
114 |
// The return variable. |
|
115 |
ColorModel colorModel = null; |
|
116 |
||
117 |
// Get the sample size. |
|
118 |
int[] sampleSize = sampleModel.getSampleSize(); |
|
119 |
||
120 |
// Create a Component ColorModel. |
|
121 |
if(sampleModel instanceof ComponentSampleModel) { |
|
122 |
// Get the number of bands. |
|
123 |
int numBands = sampleModel.getNumBands(); |
|
124 |
||
125 |
// Determine the color space. |
|
126 |
ColorSpace colorSpace = null; |
|
127 |
if(numBands <= 2) { |
|
128 |
colorSpace = ColorSpace.getInstance(ColorSpace.CS_GRAY); |
|
129 |
} else if(numBands <= 4) { |
|
130 |
colorSpace = ColorSpace.getInstance(ColorSpace.CS_sRGB); |
|
131 |
} else { |
|
132 |
colorSpace = new BogusColorSpace(numBands); |
|
133 |
} |
|
134 |
||
135 |
boolean hasAlpha = (numBands == 2) || (numBands == 4); |
|
136 |
boolean isAlphaPremultiplied = false; |
|
137 |
int transparency = hasAlpha ? |
|
138 |
Transparency.TRANSLUCENT : Transparency.OPAQUE; |
|
139 |
||
140 |
colorModel = new ComponentColorModel(colorSpace, |
|
141 |
sampleSize, |
|
142 |
hasAlpha, |
|
143 |
isAlphaPremultiplied, |
|
144 |
transparency, |
|
145 |
dataType); |
|
146 |
} else if (sampleModel.getNumBands() <= 4 && |
|
147 |
sampleModel instanceof SinglePixelPackedSampleModel) { |
|
148 |
SinglePixelPackedSampleModel sppsm = |
|
149 |
(SinglePixelPackedSampleModel)sampleModel; |
|
150 |
||
151 |
int[] bitMasks = sppsm.getBitMasks(); |
|
152 |
int rmask = 0; |
|
153 |
int gmask = 0; |
|
154 |
int bmask = 0; |
|
155 |
int amask = 0; |
|
156 |
||
157 |
int numBands = bitMasks.length; |
|
158 |
if (numBands <= 2) { |
|
159 |
rmask = gmask = bmask = bitMasks[0]; |
|
160 |
if (numBands == 2) { |
|
161 |
amask = bitMasks[1]; |
|
162 |
} |
|
163 |
} else { |
|
164 |
rmask = bitMasks[0]; |
|
165 |
gmask = bitMasks[1]; |
|
166 |
bmask = bitMasks[2]; |
|
167 |
if (numBands == 4) { |
|
168 |
amask = bitMasks[3]; |
|
169 |
} |
|
170 |
} |
|
171 |
||
172 |
int bits = 0; |
|
173 |
for (int i = 0; i < sampleSize.length; i++) { |
|
174 |
bits += sampleSize[i]; |
|
175 |
} |
|
176 |
||
177 |
return new DirectColorModel(bits, rmask, gmask, bmask, amask); |
|
178 |
||
179 |
} else if(sampleModel instanceof MultiPixelPackedSampleModel) { |
|
180 |
// Load the colormap with a ramp. |
|
181 |
int bitsPerSample = sampleSize[0]; |
|
182 |
int numEntries = 1 << bitsPerSample; |
|
183 |
byte[] map = new byte[numEntries]; |
|
184 |
for (int i = 0; i < numEntries; i++) { |
|
185 |
map[i] = (byte)(i*255/(numEntries - 1)); |
|
186 |
} |
|
187 |
||
188 |
colorModel = new IndexColorModel(bitsPerSample, numEntries, |
|
189 |
map, map, map); |
|
190 |
||
191 |
} |
|
192 |
||
193 |
return colorModel; |
|
194 |
} |
|
195 |
||
196 |
/** |
|
197 |
* For the case of binary data (<code>isBinary()</code> returns |
|
198 |
* <code>true</code>), return the binary data as a packed byte array. |
|
199 |
* The data will be packed as eight bits per byte with no bit offset, |
|
200 |
* i.e., the first bit in each image line will be the left-most of the |
|
201 |
* first byte of the line. The line stride in bytes will be |
|
202 |
* <code>(int)((getWidth()+7)/8)</code>. The length of the returned |
|
203 |
* array will be the line stride multiplied by <code>getHeight()</code> |
|
204 |
* |
|
205 |
* @return the binary data as a packed array of bytes with zero offset |
|
206 |
* of <code>null</code> if the data are not binary. |
|
207 |
* @throws IllegalArgumentException if <code>isBinary()</code> returns |
|
208 |
* <code>false</code> with the <code>SampleModel</code> of the |
|
209 |
* supplied <code>Raster</code> as argument. |
|
210 |
*/ |
|
211 |
public static byte[] getPackedBinaryData(Raster raster, |
|
212 |
Rectangle rect) { |
|
213 |
SampleModel sm = raster.getSampleModel(); |
|
214 |
if(!isBinary(sm)) { |
|
215 |
throw new IllegalArgumentException(I18N.getString("ImageUtil0")); |
|
216 |
} |
|
217 |
||
218 |
int rectX = rect.x; |
|
219 |
int rectY = rect.y; |
|
220 |
int rectWidth = rect.width; |
|
221 |
int rectHeight = rect.height; |
|
222 |
||
223 |
DataBuffer dataBuffer = raster.getDataBuffer(); |
|
224 |
||
225 |
int dx = rectX - raster.getSampleModelTranslateX(); |
|
226 |
int dy = rectY - raster.getSampleModelTranslateY(); |
|
227 |
||
228 |
MultiPixelPackedSampleModel mpp = (MultiPixelPackedSampleModel)sm; |
|
229 |
int lineStride = mpp.getScanlineStride(); |
|
230 |
int eltOffset = dataBuffer.getOffset() + mpp.getOffset(dx, dy); |
|
231 |
int bitOffset = mpp.getBitOffset(dx); |
|
232 |
||
233 |
int numBytesPerRow = (rectWidth + 7)/8; |
|
234 |
if(dataBuffer instanceof DataBufferByte && |
|
235 |
eltOffset == 0 && bitOffset == 0 && |
|
236 |
numBytesPerRow == lineStride && |
|
237 |
((DataBufferByte)dataBuffer).getData().length == |
|
238 |
numBytesPerRow*rectHeight) { |
|
239 |
return ((DataBufferByte)dataBuffer).getData(); |
|
240 |
} |
|
241 |
||
242 |
byte[] binaryDataArray = new byte[numBytesPerRow*rectHeight]; |
|
243 |
||
244 |
int b = 0; |
|
245 |
||
246 |
if(bitOffset == 0) { |
|
247 |
if(dataBuffer instanceof DataBufferByte) { |
|
248 |
byte[] data = ((DataBufferByte)dataBuffer).getData(); |
|
249 |
int stride = numBytesPerRow; |
|
250 |
int offset = 0; |
|
251 |
for(int y = 0; y < rectHeight; y++) { |
|
252 |
System.arraycopy(data, eltOffset, |
|
253 |
binaryDataArray, offset, |
|
254 |
stride); |
|
255 |
offset += stride; |
|
256 |
eltOffset += lineStride; |
|
257 |
} |
|
258 |
} else if(dataBuffer instanceof DataBufferShort || |
|
259 |
dataBuffer instanceof DataBufferUShort) { |
|
260 |
short[] data = dataBuffer instanceof DataBufferShort ? |
|
261 |
((DataBufferShort)dataBuffer).getData() : |
|
262 |
((DataBufferUShort)dataBuffer).getData(); |
|
263 |
||
264 |
for(int y = 0; y < rectHeight; y++) { |
|
265 |
int xRemaining = rectWidth; |
|
266 |
int i = eltOffset; |
|
267 |
while(xRemaining > 8) { |
|
268 |
short datum = data[i++]; |
|
269 |
binaryDataArray[b++] = (byte)((datum >>> 8) & 0xFF); |
|
270 |
binaryDataArray[b++] = (byte)(datum & 0xFF); |
|
271 |
xRemaining -= 16; |
|
272 |
} |
|
273 |
if(xRemaining > 0) { |
|
274 |
binaryDataArray[b++] = (byte)((data[i] >>> 8) & 0XFF); |
|
275 |
} |
|
276 |
eltOffset += lineStride; |
|
277 |
} |
|
278 |
} else if(dataBuffer instanceof DataBufferInt) { |
|
279 |
int[] data = ((DataBufferInt)dataBuffer).getData(); |
|
280 |
||
281 |
for(int y = 0; y < rectHeight; y++) { |
|
282 |
int xRemaining = rectWidth; |
|
283 |
int i = eltOffset; |
|
284 |
while(xRemaining > 24) { |
|
285 |
int datum = data[i++]; |
|
286 |
binaryDataArray[b++] = (byte)((datum >>> 24) & 0xFF); |
|
287 |
binaryDataArray[b++] = (byte)((datum >>> 16) & 0xFF); |
|
288 |
binaryDataArray[b++] = (byte)((datum >>> 8) & 0xFF); |
|
289 |
binaryDataArray[b++] = (byte)(datum & 0xFF); |
|
290 |
xRemaining -= 32; |
|
291 |
} |
|
292 |
int shift = 24; |
|
293 |
while(xRemaining > 0) { |
|
294 |
binaryDataArray[b++] = |
|
295 |
(byte)((data[i] >>> shift) & 0xFF); |
|
296 |
shift -= 8; |
|
297 |
xRemaining -= 8; |
|
298 |
} |
|
299 |
eltOffset += lineStride; |
|
300 |
} |
|
301 |
} |
|
302 |
} else { // bitOffset != 0 |
|
303 |
if(dataBuffer instanceof DataBufferByte) { |
|
304 |
byte[] data = ((DataBufferByte)dataBuffer).getData(); |
|
305 |
||
306 |
if((bitOffset & 7) == 0) { |
|
307 |
int stride = numBytesPerRow; |
|
308 |
int offset = 0; |
|
309 |
for(int y = 0; y < rectHeight; y++) { |
|
310 |
System.arraycopy(data, eltOffset, |
|
311 |
binaryDataArray, offset, |
|
312 |
stride); |
|
313 |
offset += stride; |
|
314 |
eltOffset += lineStride; |
|
315 |
} |
|
316 |
} else { // bitOffset % 8 != 0 |
|
317 |
int leftShift = bitOffset & 7; |
|
318 |
int rightShift = 8 - leftShift; |
|
319 |
for(int y = 0; y < rectHeight; y++) { |
|
320 |
int i = eltOffset; |
|
321 |
int xRemaining = rectWidth; |
|
322 |
while(xRemaining > 0) { |
|
323 |
if(xRemaining > rightShift) { |
|
324 |
binaryDataArray[b++] = |
|
325 |
(byte)(((data[i++]&0xFF) << leftShift) | |
|
326 |
((data[i]&0xFF) >>> rightShift)); |
|
327 |
} else { |
|
328 |
binaryDataArray[b++] = |
|
329 |
(byte)((data[i]&0xFF) << leftShift); |
|
330 |
} |
|
331 |
xRemaining -= 8; |
|
332 |
} |
|
333 |
eltOffset += lineStride; |
|
334 |
} |
|
335 |
} |
|
336 |
} else if(dataBuffer instanceof DataBufferShort || |
|
337 |
dataBuffer instanceof DataBufferUShort) { |
|
338 |
short[] data = dataBuffer instanceof DataBufferShort ? |
|
339 |
((DataBufferShort)dataBuffer).getData() : |
|
340 |
((DataBufferUShort)dataBuffer).getData(); |
|
341 |
||
342 |
for(int y = 0; y < rectHeight; y++) { |
|
343 |
int bOffset = bitOffset; |
|
344 |
for(int x = 0; x < rectWidth; x += 8, bOffset += 8) { |
|
345 |
int i = eltOffset + bOffset/16; |
|
346 |
int mod = bOffset % 16; |
|
347 |
int left = data[i] & 0xFFFF; |
|
348 |
if(mod <= 8) { |
|
349 |
binaryDataArray[b++] = (byte)(left >>> (8 - mod)); |
|
350 |
} else { |
|
351 |
int delta = mod - 8; |
|
352 |
int right = data[i+1] & 0xFFFF; |
|
353 |
binaryDataArray[b++] = |
|
354 |
(byte)((left << delta) | |
|
355 |
(right >>> (16 - delta))); |
|
356 |
} |
|
357 |
} |
|
358 |
eltOffset += lineStride; |
|
359 |
} |
|
360 |
} else if(dataBuffer instanceof DataBufferInt) { |
|
361 |
int[] data = ((DataBufferInt)dataBuffer).getData(); |
|
362 |
||
363 |
for(int y = 0; y < rectHeight; y++) { |
|
364 |
int bOffset = bitOffset; |
|
365 |
for(int x = 0; x < rectWidth; x += 8, bOffset += 8) { |
|
366 |
int i = eltOffset + bOffset/32; |
|
367 |
int mod = bOffset % 32; |
|
368 |
int left = data[i]; |
|
369 |
if(mod <= 24) { |
|
370 |
binaryDataArray[b++] = |
|
371 |
(byte)(left >>> (24 - mod)); |
|
372 |
} else { |
|
373 |
int delta = mod - 24; |
|
374 |
int right = data[i+1]; |
|
375 |
binaryDataArray[b++] = |
|
376 |
(byte)((left << delta) | |
|
377 |
(right >>> (32 - delta))); |
|
378 |
} |
|
379 |
} |
|
380 |
eltOffset += lineStride; |
|
381 |
} |
|
382 |
} |
|
383 |
} |
|
384 |
||
385 |
return binaryDataArray; |
|
386 |
} |
|
387 |
||
388 |
/** |
|
389 |
* Returns the binary data unpacked into an array of bytes. |
|
390 |
* The line stride will be the width of the <code>Raster</code>. |
|
391 |
* |
|
392 |
* @throws IllegalArgumentException if <code>isBinary()</code> returns |
|
393 |
* <code>false</code> with the <code>SampleModel</code> of the |
|
394 |
* supplied <code>Raster</code> as argument. |
|
395 |
*/ |
|
396 |
public static byte[] getUnpackedBinaryData(Raster raster, |
|
397 |
Rectangle rect) { |
|
398 |
SampleModel sm = raster.getSampleModel(); |
|
399 |
if(!isBinary(sm)) { |
|
400 |
throw new IllegalArgumentException(I18N.getString("ImageUtil0")); |
|
401 |
} |
|
402 |
||
403 |
int rectX = rect.x; |
|
404 |
int rectY = rect.y; |
|
405 |
int rectWidth = rect.width; |
|
406 |
int rectHeight = rect.height; |
|
407 |
||
408 |
DataBuffer dataBuffer = raster.getDataBuffer(); |
|
409 |
||
410 |
int dx = rectX - raster.getSampleModelTranslateX(); |
|
411 |
int dy = rectY - raster.getSampleModelTranslateY(); |
|
412 |
||
413 |
MultiPixelPackedSampleModel mpp = (MultiPixelPackedSampleModel)sm; |
|
414 |
int lineStride = mpp.getScanlineStride(); |
|
415 |
int eltOffset = dataBuffer.getOffset() + mpp.getOffset(dx, dy); |
|
416 |
int bitOffset = mpp.getBitOffset(dx); |
|
417 |
||
418 |
byte[] bdata = new byte[rectWidth*rectHeight]; |
|
419 |
int maxY = rectY + rectHeight; |
|
420 |
int maxX = rectX + rectWidth; |
|
421 |
int k = 0; |
|
422 |
||
423 |
if(dataBuffer instanceof DataBufferByte) { |
|
424 |
byte[] data = ((DataBufferByte)dataBuffer).getData(); |
|
425 |
for(int y = rectY; y < maxY; y++) { |
|
426 |
int bOffset = eltOffset*8 + bitOffset; |
|
427 |
for(int x = rectX; x < maxX; x++) { |
|
428 |
byte b = data[bOffset/8]; |
|
429 |
bdata[k++] = |
|
430 |
(byte)((b >>> (7 - bOffset & 7)) & 0x0000001); |
|
431 |
bOffset++; |
|
432 |
} |
|
433 |
eltOffset += lineStride; |
|
434 |
} |
|
435 |
} else if(dataBuffer instanceof DataBufferShort || |
|
436 |
dataBuffer instanceof DataBufferUShort) { |
|
437 |
short[] data = dataBuffer instanceof DataBufferShort ? |
|
438 |
((DataBufferShort)dataBuffer).getData() : |
|
439 |
((DataBufferUShort)dataBuffer).getData(); |
|
440 |
for(int y = rectY; y < maxY; y++) { |
|
441 |
int bOffset = eltOffset*16 + bitOffset; |
|
442 |
for(int x = rectX; x < maxX; x++) { |
|
443 |
short s = data[bOffset/16]; |
|
444 |
bdata[k++] = |
|
445 |
(byte)((s >>> (15 - bOffset % 16)) & |
|
446 |
0x0000001); |
|
447 |
bOffset++; |
|
448 |
} |
|
449 |
eltOffset += lineStride; |
|
450 |
} |
|
451 |
} else if(dataBuffer instanceof DataBufferInt) { |
|
452 |
int[] data = ((DataBufferInt)dataBuffer).getData(); |
|
453 |
for(int y = rectY; y < maxY; y++) { |
|
454 |
int bOffset = eltOffset*32 + bitOffset; |
|
455 |
for(int x = rectX; x < maxX; x++) { |
|
456 |
int i = data[bOffset/32]; |
|
457 |
bdata[k++] = |
|
458 |
(byte)((i >>> (31 - bOffset % 32)) & |
|
459 |
0x0000001); |
|
460 |
bOffset++; |
|
461 |
} |
|
462 |
eltOffset += lineStride; |
|
463 |
} |
|
464 |
} |
|
465 |
||
466 |
return bdata; |
|
467 |
} |
|
468 |
||
469 |
/** |
|
470 |
* Sets the supplied <code>Raster</code>'s data from an array |
|
471 |
* of packed binary data of the form returned by |
|
472 |
* <code>getPackedBinaryData()</code>. |
|
473 |
* |
|
474 |
* @throws IllegalArgumentException if <code>isBinary()</code> returns |
|
475 |
* <code>false</code> with the <code>SampleModel</code> of the |
|
476 |
* supplied <code>Raster</code> as argument. |
|
477 |
*/ |
|
478 |
public static void setPackedBinaryData(byte[] binaryDataArray, |
|
479 |
WritableRaster raster, |
|
480 |
Rectangle rect) { |
|
481 |
SampleModel sm = raster.getSampleModel(); |
|
482 |
if(!isBinary(sm)) { |
|
483 |
throw new IllegalArgumentException(I18N.getString("ImageUtil0")); |
|
484 |
} |
|
485 |
||
486 |
int rectX = rect.x; |
|
487 |
int rectY = rect.y; |
|
488 |
int rectWidth = rect.width; |
|
489 |
int rectHeight = rect.height; |
|
490 |
||
491 |
DataBuffer dataBuffer = raster.getDataBuffer(); |
|
492 |
||
493 |
int dx = rectX - raster.getSampleModelTranslateX(); |
|
494 |
int dy = rectY - raster.getSampleModelTranslateY(); |
|
495 |
||
496 |
MultiPixelPackedSampleModel mpp = (MultiPixelPackedSampleModel)sm; |
|
497 |
int lineStride = mpp.getScanlineStride(); |
|
498 |
int eltOffset = dataBuffer.getOffset() + mpp.getOffset(dx, dy); |
|
499 |
int bitOffset = mpp.getBitOffset(dx); |
|
500 |
||
501 |
int b = 0; |
|
502 |
||
503 |
if(bitOffset == 0) { |
|
504 |
if(dataBuffer instanceof DataBufferByte) { |
|
505 |
byte[] data = ((DataBufferByte)dataBuffer).getData(); |
|
506 |
if(data == binaryDataArray) { |
|
507 |
// Optimal case: simply return. |
|
508 |
return; |
|
509 |
} |
|
510 |
int stride = (rectWidth + 7)/8; |
|
511 |
int offset = 0; |
|
512 |
for(int y = 0; y < rectHeight; y++) { |
|
513 |
System.arraycopy(binaryDataArray, offset, |
|
514 |
data, eltOffset, |
|
515 |
stride); |
|
516 |
offset += stride; |
|
517 |
eltOffset += lineStride; |
|
518 |
} |
|
519 |
} else if(dataBuffer instanceof DataBufferShort || |
|
520 |
dataBuffer instanceof DataBufferUShort) { |
|
521 |
short[] data = dataBuffer instanceof DataBufferShort ? |
|
522 |
((DataBufferShort)dataBuffer).getData() : |
|
523 |
((DataBufferUShort)dataBuffer).getData(); |
|
524 |
||
525 |
for(int y = 0; y < rectHeight; y++) { |
|
526 |
int xRemaining = rectWidth; |
|
527 |
int i = eltOffset; |
|
528 |
while(xRemaining > 8) { |
|
529 |
data[i++] = |
|
530 |
(short)(((binaryDataArray[b++] & 0xFF) << 8) | |
|
531 |
(binaryDataArray[b++] & 0xFF)); |
|
532 |
xRemaining -= 16; |
|
533 |
} |
|
534 |
if(xRemaining > 0) { |
|
535 |
data[i++] = |
|
536 |
(short)((binaryDataArray[b++] & 0xFF) << 8); |
|
537 |
} |
|
538 |
eltOffset += lineStride; |
|
539 |
} |
|
540 |
} else if(dataBuffer instanceof DataBufferInt) { |
|
541 |
int[] data = ((DataBufferInt)dataBuffer).getData(); |
|
542 |
||
543 |
for(int y = 0; y < rectHeight; y++) { |
|
544 |
int xRemaining = rectWidth; |
|
545 |
int i = eltOffset; |
|
546 |
while(xRemaining > 24) { |
|
547 |
data[i++] = |
|
22584 | 548 |
(((binaryDataArray[b++] & 0xFF) << 24) | |
549 |
((binaryDataArray[b++] & 0xFF) << 16) | |
|
550 |
((binaryDataArray[b++] & 0xFF) << 8) | |
|
551 |
(binaryDataArray[b++] & 0xFF)); |
|
2 | 552 |
xRemaining -= 32; |
553 |
} |
|
554 |
int shift = 24; |
|
555 |
while(xRemaining > 0) { |
|
22584 | 556 |
data[i] |= ((binaryDataArray[b++] & 0xFF) << shift); |
2 | 557 |
shift -= 8; |
558 |
xRemaining -= 8; |
|
559 |
} |
|
560 |
eltOffset += lineStride; |
|
561 |
} |
|
562 |
} |
|
563 |
} else { // bitOffset != 0 |
|
564 |
int stride = (rectWidth + 7)/8; |
|
565 |
int offset = 0; |
|
566 |
if(dataBuffer instanceof DataBufferByte) { |
|
567 |
byte[] data = ((DataBufferByte)dataBuffer).getData(); |
|
568 |
||
569 |
if((bitOffset & 7) == 0) { |
|
570 |
for(int y = 0; y < rectHeight; y++) { |
|
571 |
System.arraycopy(binaryDataArray, offset, |
|
572 |
data, eltOffset, |
|
573 |
stride); |
|
574 |
offset += stride; |
|
575 |
eltOffset += lineStride; |
|
576 |
} |
|
577 |
} else { // bitOffset % 8 != 0 |
|
578 |
int rightShift = bitOffset & 7; |
|
579 |
int leftShift = 8 - rightShift; |
|
580 |
int leftShift8 = 8 + leftShift; |
|
581 |
int mask = (byte)(255<<leftShift); |
|
582 |
int mask1 = (byte)~mask; |
|
583 |
||
584 |
for(int y = 0; y < rectHeight; y++) { |
|
585 |
int i = eltOffset; |
|
586 |
int xRemaining = rectWidth; |
|
587 |
while(xRemaining > 0) { |
|
588 |
byte datum = binaryDataArray[b++]; |
|
589 |
||
590 |
if (xRemaining > leftShift8) { |
|
591 |
// when all the bits in this BYTE will be set |
|
592 |
// into the data buffer. |
|
593 |
data[i] = (byte)((data[i] & mask ) | |
|
594 |
((datum&0xFF) >>> rightShift)); |
|
595 |
data[++i] = (byte)((datum & 0xFF) << leftShift); |
|
596 |
} else if (xRemaining > leftShift) { |
|
597 |
// All the "leftShift" high bits will be set |
|
598 |
// into the data buffer. But not all the |
|
599 |
// "rightShift" low bits will be set. |
|
600 |
data[i] = (byte)((data[i] & mask ) | |
|
601 |
((datum&0xFF) >>> rightShift)); |
|
602 |
i++; |
|
603 |
data[i] = |
|
604 |
(byte)((data[i] & mask1) | ((datum & 0xFF) << leftShift)); |
|
605 |
} |
|
606 |
else { |
|
607 |
// Less than "leftShift" high bits will be set. |
|
608 |
int remainMask = (1 << leftShift - xRemaining) - 1; |
|
609 |
data[i] = |
|
610 |
(byte)((data[i] & (mask | remainMask)) | |
|
611 |
(datum&0xFF) >>> rightShift & ~remainMask); |
|
612 |
} |
|
613 |
xRemaining -= 8; |
|
614 |
} |
|
615 |
eltOffset += lineStride; |
|
616 |
} |
|
617 |
} |
|
618 |
} else if(dataBuffer instanceof DataBufferShort || |
|
619 |
dataBuffer instanceof DataBufferUShort) { |
|
620 |
short[] data = dataBuffer instanceof DataBufferShort ? |
|
621 |
((DataBufferShort)dataBuffer).getData() : |
|
622 |
((DataBufferUShort)dataBuffer).getData(); |
|
623 |
||
624 |
int rightShift = bitOffset & 7; |
|
625 |
int leftShift = 8 - rightShift; |
|
626 |
int leftShift16 = 16 + leftShift; |
|
627 |
int mask = (short)(~(255 << leftShift)); |
|
628 |
int mask1 = (short)(65535 << leftShift); |
|
629 |
int mask2 = (short)~mask1; |
|
630 |
||
631 |
for(int y = 0; y < rectHeight; y++) { |
|
632 |
int bOffset = bitOffset; |
|
633 |
int xRemaining = rectWidth; |
|
634 |
for(int x = 0; x < rectWidth; |
|
635 |
x += 8, bOffset += 8, xRemaining -= 8) { |
|
636 |
int i = eltOffset + (bOffset >> 4); |
|
637 |
int mod = bOffset & 15; |
|
638 |
int datum = binaryDataArray[b++] & 0xFF; |
|
639 |
if(mod <= 8) { |
|
640 |
// This BYTE is set into one SHORT |
|
641 |
if (xRemaining < 8) { |
|
642 |
// Mask the bits to be set. |
|
643 |
datum &= 255 << 8 - xRemaining; |
|
644 |
} |
|
645 |
data[i] = (short)((data[i] & mask) | (datum << leftShift)); |
|
646 |
} else if (xRemaining > leftShift16) { |
|
647 |
// This BYTE will be set into two SHORTs |
|
648 |
data[i] = (short)((data[i] & mask1) | ((datum >>> rightShift)&0xFFFF)); |
|
649 |
data[++i] = |
|
650 |
(short)((datum << leftShift)&0xFFFF); |
|
651 |
} else if (xRemaining > leftShift) { |
|
652 |
// This BYTE will be set into two SHORTs; |
|
653 |
// But not all the low bits will be set into SHORT |
|
654 |
data[i] = (short)((data[i] & mask1) | ((datum >>> rightShift)&0xFFFF)); |
|
655 |
i++; |
|
656 |
data[i] = |
|
657 |
(short)((data[i] & mask2) | ((datum << leftShift)&0xFFFF)); |
|
658 |
} else { |
|
659 |
// Only some of the high bits will be set into |
|
660 |
// SHORTs |
|
661 |
int remainMask = (1 << leftShift - xRemaining) - 1; |
|
662 |
data[i] = (short)((data[i] & (mask1 | remainMask)) | |
|
663 |
((datum >>> rightShift)&0xFFFF & ~remainMask)); |
|
664 |
} |
|
665 |
} |
|
666 |
eltOffset += lineStride; |
|
667 |
} |
|
668 |
} else if(dataBuffer instanceof DataBufferInt) { |
|
669 |
int[] data = ((DataBufferInt)dataBuffer).getData(); |
|
670 |
int rightShift = bitOffset & 7; |
|
671 |
int leftShift = 8 - rightShift; |
|
672 |
int leftShift32 = 32 + leftShift; |
|
673 |
int mask = 0xFFFFFFFF << leftShift; |
|
674 |
int mask1 = ~mask; |
|
675 |
||
676 |
for(int y = 0; y < rectHeight; y++) { |
|
677 |
int bOffset = bitOffset; |
|
678 |
int xRemaining = rectWidth; |
|
679 |
for(int x = 0; x < rectWidth; |
|
680 |
x += 8, bOffset += 8, xRemaining -= 8) { |
|
681 |
int i = eltOffset + (bOffset >> 5); |
|
682 |
int mod = bOffset & 31; |
|
683 |
int datum = binaryDataArray[b++] & 0xFF; |
|
684 |
if(mod <= 24) { |
|
685 |
// This BYTE is set into one INT |
|
686 |
int shift = 24 - mod; |
|
687 |
if (xRemaining < 8) { |
|
688 |
// Mask the bits to be set. |
|
689 |
datum &= 255 << 8 - xRemaining; |
|
690 |
} |
|
691 |
data[i] = (data[i] & (~(255 << shift))) | (datum << shift); |
|
692 |
} else if (xRemaining > leftShift32) { |
|
693 |
// All the bits of this BYTE will be set into two INTs |
|
694 |
data[i] = (data[i] & mask) | (datum >>> rightShift); |
|
695 |
data[++i] = datum << leftShift; |
|
696 |
} else if (xRemaining > leftShift) { |
|
697 |
// This BYTE will be set into two INTs; |
|
698 |
// But not all the low bits will be set into INT |
|
699 |
data[i] = (data[i] & mask) | (datum >>> rightShift); |
|
700 |
i++; |
|
701 |
data[i] = (data[i] & mask1) | (datum << leftShift); |
|
702 |
} else { |
|
703 |
// Only some of the high bits will be set into INT |
|
704 |
int remainMask = (1 << leftShift - xRemaining) - 1; |
|
705 |
data[i] = (data[i] & (mask | remainMask)) | |
|
706 |
(datum >>> rightShift & ~remainMask); |
|
707 |
} |
|
708 |
} |
|
709 |
eltOffset += lineStride; |
|
710 |
} |
|
711 |
} |
|
712 |
} |
|
713 |
} |
|
714 |
||
715 |
/** |
|
716 |
* Copies data into the packed array of the <code>Raster</code> |
|
717 |
* from an array of unpacked data of the form returned by |
|
718 |
* <code>getUnpackedBinaryData()</code>. |
|
719 |
* |
|
720 |
* <p> If the data are binary, then the target bit will be set if |
|
721 |
* and only if the corresponding byte is non-zero. |
|
722 |
* |
|
723 |
* @throws IllegalArgumentException if <code>isBinary()</code> returns |
|
724 |
* <code>false</code> with the <code>SampleModel</code> of the |
|
725 |
* supplied <code>Raster</code> as argument. |
|
726 |
*/ |
|
727 |
public static void setUnpackedBinaryData(byte[] bdata, |
|
728 |
WritableRaster raster, |
|
729 |
Rectangle rect) { |
|
730 |
SampleModel sm = raster.getSampleModel(); |
|
731 |
if(!isBinary(sm)) { |
|
732 |
throw new IllegalArgumentException(I18N.getString("ImageUtil0")); |
|
733 |
} |
|
734 |
||
735 |
int rectX = rect.x; |
|
736 |
int rectY = rect.y; |
|
737 |
int rectWidth = rect.width; |
|
738 |
int rectHeight = rect.height; |
|
739 |
||
740 |
DataBuffer dataBuffer = raster.getDataBuffer(); |
|
741 |
||
742 |
int dx = rectX - raster.getSampleModelTranslateX(); |
|
743 |
int dy = rectY - raster.getSampleModelTranslateY(); |
|
744 |
||
745 |
MultiPixelPackedSampleModel mpp = (MultiPixelPackedSampleModel)sm; |
|
746 |
int lineStride = mpp.getScanlineStride(); |
|
747 |
int eltOffset = dataBuffer.getOffset() + mpp.getOffset(dx, dy); |
|
748 |
int bitOffset = mpp.getBitOffset(dx); |
|
749 |
||
750 |
int k = 0; |
|
751 |
||
752 |
if(dataBuffer instanceof DataBufferByte) { |
|
753 |
byte[] data = ((DataBufferByte)dataBuffer).getData(); |
|
754 |
for(int y = 0; y < rectHeight; y++) { |
|
755 |
int bOffset = eltOffset*8 + bitOffset; |
|
756 |
for(int x = 0; x < rectWidth; x++) { |
|
757 |
if(bdata[k++] != (byte)0) { |
|
758 |
data[bOffset/8] |= |
|
759 |
(byte)(0x00000001 << (7 - bOffset & 7)); |
|
760 |
} |
|
761 |
bOffset++; |
|
762 |
} |
|
763 |
eltOffset += lineStride; |
|
764 |
} |
|
765 |
} else if(dataBuffer instanceof DataBufferShort || |
|
766 |
dataBuffer instanceof DataBufferUShort) { |
|
767 |
short[] data = dataBuffer instanceof DataBufferShort ? |
|
768 |
((DataBufferShort)dataBuffer).getData() : |
|
769 |
((DataBufferUShort)dataBuffer).getData(); |
|
770 |
for(int y = 0; y < rectHeight; y++) { |
|
771 |
int bOffset = eltOffset*16 + bitOffset; |
|
772 |
for(int x = 0; x < rectWidth; x++) { |
|
773 |
if(bdata[k++] != (byte)0) { |
|
774 |
data[bOffset/16] |= |
|
775 |
(short)(0x00000001 << |
|
776 |
(15 - bOffset % 16)); |
|
777 |
} |
|
778 |
bOffset++; |
|
779 |
} |
|
780 |
eltOffset += lineStride; |
|
781 |
} |
|
782 |
} else if(dataBuffer instanceof DataBufferInt) { |
|
783 |
int[] data = ((DataBufferInt)dataBuffer).getData(); |
|
784 |
for(int y = 0; y < rectHeight; y++) { |
|
785 |
int bOffset = eltOffset*32 + bitOffset; |
|
786 |
for(int x = 0; x < rectWidth; x++) { |
|
787 |
if(bdata[k++] != (byte)0) { |
|
788 |
data[bOffset/32] |= |
|
22584 | 789 |
(0x00000001 << (31 - bOffset % 32)); |
2 | 790 |
} |
791 |
bOffset++; |
|
792 |
} |
|
793 |
eltOffset += lineStride; |
|
794 |
} |
|
795 |
} |
|
796 |
} |
|
797 |
||
798 |
public static boolean isBinary(SampleModel sm) { |
|
799 |
return sm instanceof MultiPixelPackedSampleModel && |
|
800 |
((MultiPixelPackedSampleModel)sm).getPixelBitStride() == 1 && |
|
801 |
sm.getNumBands() == 1; |
|
802 |
} |
|
803 |
||
804 |
public static ColorModel createColorModel(ColorSpace colorSpace, |
|
805 |
SampleModel sampleModel) { |
|
806 |
ColorModel colorModel = null; |
|
807 |
||
808 |
if(sampleModel == null) { |
|
809 |
throw new IllegalArgumentException(I18N.getString("ImageUtil1")); |
|
810 |
} |
|
811 |
||
812 |
int numBands = sampleModel.getNumBands(); |
|
813 |
if (numBands < 1 || numBands > 4) { |
|
814 |
return null; |
|
815 |
} |
|
816 |
||
817 |
int dataType = sampleModel.getDataType(); |
|
818 |
if (sampleModel instanceof ComponentSampleModel) { |
|
819 |
if (dataType < DataBuffer.TYPE_BYTE || |
|
820 |
//dataType == DataBuffer.TYPE_SHORT || |
|
821 |
dataType > DataBuffer.TYPE_DOUBLE) { |
|
822 |
return null; |
|
823 |
} |
|
824 |
||
825 |
if (colorSpace == null) |
|
826 |
colorSpace = |
|
827 |
numBands <= 2 ? |
|
828 |
ColorSpace.getInstance(ColorSpace.CS_GRAY) : |
|
829 |
ColorSpace.getInstance(ColorSpace.CS_sRGB); |
|
830 |
||
831 |
boolean useAlpha = (numBands == 2) || (numBands == 4); |
|
832 |
int transparency = useAlpha ? |
|
833 |
Transparency.TRANSLUCENT : Transparency.OPAQUE; |
|
834 |
||
835 |
boolean premultiplied = false; |
|
836 |
||
837 |
int dataTypeSize = DataBuffer.getDataTypeSize(dataType); |
|
838 |
int[] bits = new int[numBands]; |
|
839 |
for (int i = 0; i < numBands; i++) { |
|
840 |
bits[i] = dataTypeSize; |
|
841 |
} |
|
842 |
||
843 |
colorModel = new ComponentColorModel(colorSpace, |
|
844 |
bits, |
|
845 |
useAlpha, |
|
846 |
premultiplied, |
|
847 |
transparency, |
|
848 |
dataType); |
|
849 |
} else if (sampleModel instanceof SinglePixelPackedSampleModel) { |
|
850 |
SinglePixelPackedSampleModel sppsm = |
|
851 |
(SinglePixelPackedSampleModel)sampleModel; |
|
852 |
||
853 |
int[] bitMasks = sppsm.getBitMasks(); |
|
854 |
int rmask = 0; |
|
855 |
int gmask = 0; |
|
856 |
int bmask = 0; |
|
857 |
int amask = 0; |
|
858 |
||
859 |
numBands = bitMasks.length; |
|
860 |
if (numBands <= 2) { |
|
861 |
rmask = gmask = bmask = bitMasks[0]; |
|
862 |
if (numBands == 2) { |
|
863 |
amask = bitMasks[1]; |
|
864 |
} |
|
865 |
} else { |
|
866 |
rmask = bitMasks[0]; |
|
867 |
gmask = bitMasks[1]; |
|
868 |
bmask = bitMasks[2]; |
|
869 |
if (numBands == 4) { |
|
870 |
amask = bitMasks[3]; |
|
871 |
} |
|
872 |
} |
|
873 |
||
874 |
int[] sampleSize = sppsm.getSampleSize(); |
|
875 |
int bits = 0; |
|
876 |
for (int i = 0; i < sampleSize.length; i++) { |
|
877 |
bits += sampleSize[i]; |
|
878 |
} |
|
879 |
||
880 |
if (colorSpace == null) |
|
881 |
colorSpace = ColorSpace.getInstance(ColorSpace.CS_sRGB); |
|
882 |
||
883 |
colorModel = |
|
884 |
new DirectColorModel(colorSpace, |
|
885 |
bits, rmask, gmask, bmask, amask, |
|
886 |
false, |
|
887 |
sampleModel.getDataType()); |
|
888 |
} else if (sampleModel instanceof MultiPixelPackedSampleModel) { |
|
889 |
int bits = |
|
890 |
((MultiPixelPackedSampleModel)sampleModel).getPixelBitStride(); |
|
891 |
int size = 1 << bits; |
|
892 |
byte[] comp = new byte[size]; |
|
893 |
||
894 |
for (int i = 0; i < size; i++) |
|
895 |
comp[i] = (byte)(255 * i / (size - 1)); |
|
896 |
||
897 |
colorModel = new IndexColorModel(bits, size, comp, comp, comp); |
|
898 |
} |
|
899 |
||
900 |
return colorModel; |
|
901 |
} |
|
902 |
||
903 |
public static int getElementSize(SampleModel sm) { |
|
904 |
int elementSize = DataBuffer.getDataTypeSize(sm.getDataType()); |
|
905 |
||
906 |
if (sm instanceof MultiPixelPackedSampleModel) { |
|
907 |
MultiPixelPackedSampleModel mppsm = |
|
908 |
(MultiPixelPackedSampleModel)sm; |
|
909 |
return mppsm.getSampleSize(0) * mppsm.getNumBands(); |
|
910 |
} else if (sm instanceof ComponentSampleModel) { |
|
911 |
return sm.getNumBands() * elementSize; |
|
912 |
} else if (sm instanceof SinglePixelPackedSampleModel) { |
|
913 |
return elementSize; |
|
914 |
} |
|
915 |
||
916 |
return elementSize * sm.getNumBands(); |
|
917 |
||
918 |
} |
|
919 |
||
920 |
public static long getTileSize(SampleModel sm) { |
|
921 |
int elementSize = DataBuffer.getDataTypeSize(sm.getDataType()); |
|
922 |
||
923 |
if (sm instanceof MultiPixelPackedSampleModel) { |
|
924 |
MultiPixelPackedSampleModel mppsm = |
|
925 |
(MultiPixelPackedSampleModel)sm; |
|
926 |
return (mppsm.getScanlineStride() * mppsm.getHeight() + |
|
927 |
(mppsm.getDataBitOffset() + elementSize -1) / elementSize) * |
|
928 |
((elementSize + 7) / 8); |
|
929 |
} else if (sm instanceof ComponentSampleModel) { |
|
930 |
ComponentSampleModel csm = (ComponentSampleModel)sm; |
|
931 |
int[] bandOffsets = csm.getBandOffsets(); |
|
932 |
int maxBandOff = bandOffsets[0]; |
|
933 |
for (int i=1; i<bandOffsets.length; i++) |
|
934 |
maxBandOff = Math.max(maxBandOff, bandOffsets[i]); |
|
935 |
||
936 |
long size = 0; |
|
937 |
int pixelStride = csm.getPixelStride(); |
|
938 |
int scanlineStride = csm.getScanlineStride(); |
|
939 |
if (maxBandOff >= 0) |
|
940 |
size += maxBandOff + 1; |
|
941 |
if (pixelStride > 0) |
|
942 |
size += pixelStride * (sm.getWidth() - 1); |
|
943 |
if (scanlineStride > 0) |
|
944 |
size += scanlineStride * (sm.getHeight() - 1); |
|
945 |
||
946 |
int[] bankIndices = csm.getBankIndices(); |
|
947 |
maxBandOff = bankIndices[0]; |
|
948 |
for (int i=1; i<bankIndices.length; i++) |
|
949 |
maxBandOff = Math.max(maxBandOff, bankIndices[i]); |
|
950 |
return size * (maxBandOff + 1) * ((elementSize + 7) / 8); |
|
951 |
} else if (sm instanceof SinglePixelPackedSampleModel) { |
|
952 |
SinglePixelPackedSampleModel sppsm = |
|
953 |
(SinglePixelPackedSampleModel)sm; |
|
954 |
long size = sppsm.getScanlineStride() * (sppsm.getHeight() - 1) + |
|
955 |
sppsm.getWidth(); |
|
956 |
return size * ((elementSize + 7) / 8); |
|
957 |
} |
|
958 |
||
959 |
return 0; |
|
960 |
} |
|
961 |
||
962 |
public static long getBandSize(SampleModel sm) { |
|
963 |
int elementSize = DataBuffer.getDataTypeSize(sm.getDataType()); |
|
964 |
||
965 |
if (sm instanceof ComponentSampleModel) { |
|
966 |
ComponentSampleModel csm = (ComponentSampleModel)sm; |
|
967 |
int pixelStride = csm.getPixelStride(); |
|
968 |
int scanlineStride = csm.getScanlineStride(); |
|
969 |
long size = Math.min(pixelStride, scanlineStride); |
|
970 |
||
971 |
if (pixelStride > 0) |
|
972 |
size += pixelStride * (sm.getWidth() - 1); |
|
973 |
if (scanlineStride > 0) |
|
974 |
size += scanlineStride * (sm.getHeight() - 1); |
|
975 |
return size * ((elementSize + 7) / 8); |
|
976 |
} else |
|
977 |
return getTileSize(sm); |
|
978 |
} |
|
979 |
/** |
|
980 |
* Tests whether the color indices represent a gray-scale image. |
|
981 |
* |
|
982 |
* @param r The red channel color indices. |
|
983 |
* @param g The green channel color indices. |
|
984 |
* @param b The blue channel color indices. |
|
985 |
* @return If all the indices have 256 entries, and are identical mappings, |
|
986 |
* return <code>true</code>; otherwise, return <code>false</code>. |
|
987 |
*/ |
|
988 |
public static boolean isIndicesForGrayscale(byte[] r, byte[] g, byte[] b) { |
|
989 |
if (r.length != g.length || r.length != b.length) |
|
990 |
return false; |
|
991 |
||
992 |
int size = r.length; |
|
993 |
||
994 |
if (size != 256) |
|
995 |
return false; |
|
996 |
||
997 |
for (int i = 0; i < size; i++) { |
|
998 |
byte temp = (byte) i; |
|
999 |
||
1000 |
if (r[i] != temp || g[i] != temp || b[i] != temp) |
|
1001 |
return false; |
|
1002 |
} |
|
1003 |
||
1004 |
return true; |
|
1005 |
} |
|
1006 |
||
1007 |
/** Converts the provided object to <code>String</code> */ |
|
1008 |
public static String convertObjectToString(Object obj) { |
|
1009 |
if (obj == null) |
|
1010 |
return ""; |
|
1011 |
||
1012 |
String s = ""; |
|
1013 |
if (obj instanceof byte[]) { |
|
1014 |
byte[] bArray = (byte[])obj; |
|
1015 |
for (int i = 0; i < bArray.length; i++) |
|
1016 |
s += bArray[i] + " "; |
|
1017 |
return s; |
|
1018 |
} |
|
1019 |
||
1020 |
if (obj instanceof int[]) { |
|
1021 |
int[] iArray = (int[])obj; |
|
1022 |
for (int i = 0; i < iArray.length; i++) |
|
1023 |
s += iArray[i] + " " ; |
|
1024 |
return s; |
|
1025 |
} |
|
1026 |
||
1027 |
if (obj instanceof short[]) { |
|
1028 |
short[] sArray = (short[])obj; |
|
1029 |
for (int i = 0; i < sArray.length; i++) |
|
1030 |
s += sArray[i] + " " ; |
|
1031 |
return s; |
|
1032 |
} |
|
1033 |
||
1034 |
return obj.toString(); |
|
1035 |
||
1036 |
} |
|
1037 |
||
1038 |
/** Checks that the provided <code>ImageWriter</code> can encode |
|
1039 |
* the provided <code>ImageTypeSpecifier</code> or not. If not, an |
|
1040 |
* <code>IIOException</code> will be thrown. |
|
1041 |
* @param writer The provided <code>ImageWriter</code>. |
|
1042 |
* @param type The image to be tested. |
|
1043 |
* @throws IIOException If the writer cannot encoded the provided image. |
|
1044 |
*/ |
|
1045 |
public static final void canEncodeImage(ImageWriter writer, |
|
1046 |
ImageTypeSpecifier type) |
|
1047 |
throws IIOException { |
|
1048 |
ImageWriterSpi spi = writer.getOriginatingProvider(); |
|
1049 |
||
1050 |
if(type != null && spi != null && !spi.canEncodeImage(type)) { |
|
1051 |
throw new IIOException(I18N.getString("ImageUtil2")+" "+ |
|
1052 |
writer.getClass().getName()); |
|
1053 |
} |
|
1054 |
} |
|
1055 |
||
1056 |
/** Checks that the provided <code>ImageWriter</code> can encode |
|
1057 |
* the provided <code>ColorModel</code> and <code>SampleModel</code>. |
|
1058 |
* If not, an <code>IIOException</code> will be thrown. |
|
1059 |
* @param writer The provided <code>ImageWriter</code>. |
|
1060 |
* @param colorModel The provided <code>ColorModel</code>. |
|
1061 |
* @param sampleModel The provided <code>SampleModel</code>. |
|
1062 |
* @throws IIOException If the writer cannot encoded the provided image. |
|
1063 |
*/ |
|
1064 |
public static final void canEncodeImage(ImageWriter writer, |
|
1065 |
ColorModel colorModel, |
|
1066 |
SampleModel sampleModel) |
|
1067 |
throws IIOException { |
|
1068 |
ImageTypeSpecifier type = null; |
|
1069 |
if (colorModel != null && sampleModel != null) |
|
1070 |
type = new ImageTypeSpecifier(colorModel, sampleModel); |
|
1071 |
canEncodeImage(writer, type); |
|
1072 |
} |
|
1073 |
||
1074 |
/** |
|
1075 |
* Returns whether the image has contiguous data across rows. |
|
1076 |
*/ |
|
1077 |
public static final boolean imageIsContiguous(RenderedImage image) { |
|
1078 |
SampleModel sm; |
|
1079 |
if(image instanceof BufferedImage) { |
|
1080 |
WritableRaster ras = ((BufferedImage)image).getRaster(); |
|
1081 |
sm = ras.getSampleModel(); |
|
1082 |
} else { |
|
1083 |
sm = image.getSampleModel(); |
|
1084 |
} |
|
1085 |
||
1086 |
if (sm instanceof ComponentSampleModel) { |
|
1087 |
// Ensure image rows samples are stored contiguously |
|
1088 |
// in a single bank. |
|
1089 |
ComponentSampleModel csm = (ComponentSampleModel)sm; |
|
1090 |
||
1091 |
if (csm.getPixelStride() != csm.getNumBands()) { |
|
1092 |
return false; |
|
1093 |
} |
|
1094 |
||
1095 |
int[] bandOffsets = csm.getBandOffsets(); |
|
1096 |
for (int i = 0; i < bandOffsets.length; i++) { |
|
1097 |
if (bandOffsets[i] != i) { |
|
1098 |
return false; |
|
1099 |
} |
|
1100 |
} |
|
1101 |
||
1102 |
int[] bankIndices = csm.getBankIndices(); |
|
1103 |
for (int i = 0; i < bandOffsets.length; i++) { |
|
1104 |
if (bankIndices[i] != 0) { |
|
1105 |
return false; |
|
1106 |
} |
|
1107 |
} |
|
1108 |
||
1109 |
return true; |
|
1110 |
} |
|
1111 |
||
1112 |
// Otherwise true if and only if it's a bilevel image with |
|
1113 |
// a MultiPixelPackedSampleModel, 1 bit per pixel, and 1 bit |
|
1114 |
// pixel stride. |
|
1115 |
return ImageUtil.isBinary(sm); |
|
1116 |
} |
|
34416
68c0d866db5d
8143342: Integrate Java Image I/O support for TIFF per JEP 262
bpb
parents:
25859
diff
changeset
|
1117 |
|
68c0d866db5d
8143342: Integrate Java Image I/O support for TIFF per JEP 262
bpb
parents:
25859
diff
changeset
|
1118 |
/** |
68c0d866db5d
8143342: Integrate Java Image I/O support for TIFF per JEP 262
bpb
parents:
25859
diff
changeset
|
1119 |
* Gets the destination image type. |
68c0d866db5d
8143342: Integrate Java Image I/O support for TIFF per JEP 262
bpb
parents:
25859
diff
changeset
|
1120 |
*/ |
68c0d866db5d
8143342: Integrate Java Image I/O support for TIFF per JEP 262
bpb
parents:
25859
diff
changeset
|
1121 |
public static final ImageTypeSpecifier |
68c0d866db5d
8143342: Integrate Java Image I/O support for TIFF per JEP 262
bpb
parents:
25859
diff
changeset
|
1122 |
getDestinationType(ImageReadParam param, |
68c0d866db5d
8143342: Integrate Java Image I/O support for TIFF per JEP 262
bpb
parents:
25859
diff
changeset
|
1123 |
Iterator<ImageTypeSpecifier> imageTypes) throws IIOException { |
68c0d866db5d
8143342: Integrate Java Image I/O support for TIFF per JEP 262
bpb
parents:
25859
diff
changeset
|
1124 |
|
68c0d866db5d
8143342: Integrate Java Image I/O support for TIFF per JEP 262
bpb
parents:
25859
diff
changeset
|
1125 |
if (imageTypes == null || !imageTypes.hasNext()) { |
68c0d866db5d
8143342: Integrate Java Image I/O support for TIFF per JEP 262
bpb
parents:
25859
diff
changeset
|
1126 |
throw new IllegalArgumentException("imageTypes null or empty!"); |
68c0d866db5d
8143342: Integrate Java Image I/O support for TIFF per JEP 262
bpb
parents:
25859
diff
changeset
|
1127 |
} |
68c0d866db5d
8143342: Integrate Java Image I/O support for TIFF per JEP 262
bpb
parents:
25859
diff
changeset
|
1128 |
|
68c0d866db5d
8143342: Integrate Java Image I/O support for TIFF per JEP 262
bpb
parents:
25859
diff
changeset
|
1129 |
ImageTypeSpecifier imageType = null; |
68c0d866db5d
8143342: Integrate Java Image I/O support for TIFF per JEP 262
bpb
parents:
25859
diff
changeset
|
1130 |
|
68c0d866db5d
8143342: Integrate Java Image I/O support for TIFF per JEP 262
bpb
parents:
25859
diff
changeset
|
1131 |
// If param is non-null, use it |
68c0d866db5d
8143342: Integrate Java Image I/O support for TIFF per JEP 262
bpb
parents:
25859
diff
changeset
|
1132 |
if (param != null) { |
68c0d866db5d
8143342: Integrate Java Image I/O support for TIFF per JEP 262
bpb
parents:
25859
diff
changeset
|
1133 |
imageType = param.getDestinationType(); |
68c0d866db5d
8143342: Integrate Java Image I/O support for TIFF per JEP 262
bpb
parents:
25859
diff
changeset
|
1134 |
} |
68c0d866db5d
8143342: Integrate Java Image I/O support for TIFF per JEP 262
bpb
parents:
25859
diff
changeset
|
1135 |
|
68c0d866db5d
8143342: Integrate Java Image I/O support for TIFF per JEP 262
bpb
parents:
25859
diff
changeset
|
1136 |
// No info from param, use fallback image type |
68c0d866db5d
8143342: Integrate Java Image I/O support for TIFF per JEP 262
bpb
parents:
25859
diff
changeset
|
1137 |
if (imageType == null) { |
68c0d866db5d
8143342: Integrate Java Image I/O support for TIFF per JEP 262
bpb
parents:
25859
diff
changeset
|
1138 |
Object o = imageTypes.next(); |
68c0d866db5d
8143342: Integrate Java Image I/O support for TIFF per JEP 262
bpb
parents:
25859
diff
changeset
|
1139 |
if (!(o instanceof ImageTypeSpecifier)) { |
68c0d866db5d
8143342: Integrate Java Image I/O support for TIFF per JEP 262
bpb
parents:
25859
diff
changeset
|
1140 |
throw new IllegalArgumentException |
68c0d866db5d
8143342: Integrate Java Image I/O support for TIFF per JEP 262
bpb
parents:
25859
diff
changeset
|
1141 |
("Non-ImageTypeSpecifier retrieved from imageTypes!"); |
68c0d866db5d
8143342: Integrate Java Image I/O support for TIFF per JEP 262
bpb
parents:
25859
diff
changeset
|
1142 |
} |
68c0d866db5d
8143342: Integrate Java Image I/O support for TIFF per JEP 262
bpb
parents:
25859
diff
changeset
|
1143 |
imageType = (ImageTypeSpecifier)o; |
68c0d866db5d
8143342: Integrate Java Image I/O support for TIFF per JEP 262
bpb
parents:
25859
diff
changeset
|
1144 |
} else { |
68c0d866db5d
8143342: Integrate Java Image I/O support for TIFF per JEP 262
bpb
parents:
25859
diff
changeset
|
1145 |
boolean foundIt = false; |
68c0d866db5d
8143342: Integrate Java Image I/O support for TIFF per JEP 262
bpb
parents:
25859
diff
changeset
|
1146 |
while (imageTypes.hasNext()) { |
68c0d866db5d
8143342: Integrate Java Image I/O support for TIFF per JEP 262
bpb
parents:
25859
diff
changeset
|
1147 |
ImageTypeSpecifier type = |
68c0d866db5d
8143342: Integrate Java Image I/O support for TIFF per JEP 262
bpb
parents:
25859
diff
changeset
|
1148 |
imageTypes.next(); |
68c0d866db5d
8143342: Integrate Java Image I/O support for TIFF per JEP 262
bpb
parents:
25859
diff
changeset
|
1149 |
if (type.equals(imageType)) { |
68c0d866db5d
8143342: Integrate Java Image I/O support for TIFF per JEP 262
bpb
parents:
25859
diff
changeset
|
1150 |
foundIt = true; |
68c0d866db5d
8143342: Integrate Java Image I/O support for TIFF per JEP 262
bpb
parents:
25859
diff
changeset
|
1151 |
break; |
68c0d866db5d
8143342: Integrate Java Image I/O support for TIFF per JEP 262
bpb
parents:
25859
diff
changeset
|
1152 |
} |
68c0d866db5d
8143342: Integrate Java Image I/O support for TIFF per JEP 262
bpb
parents:
25859
diff
changeset
|
1153 |
} |
68c0d866db5d
8143342: Integrate Java Image I/O support for TIFF per JEP 262
bpb
parents:
25859
diff
changeset
|
1154 |
|
68c0d866db5d
8143342: Integrate Java Image I/O support for TIFF per JEP 262
bpb
parents:
25859
diff
changeset
|
1155 |
if (!foundIt) { |
68c0d866db5d
8143342: Integrate Java Image I/O support for TIFF per JEP 262
bpb
parents:
25859
diff
changeset
|
1156 |
throw new IIOException |
68c0d866db5d
8143342: Integrate Java Image I/O support for TIFF per JEP 262
bpb
parents:
25859
diff
changeset
|
1157 |
("Destination type from ImageReadParam does not match!"); |
68c0d866db5d
8143342: Integrate Java Image I/O support for TIFF per JEP 262
bpb
parents:
25859
diff
changeset
|
1158 |
} |
68c0d866db5d
8143342: Integrate Java Image I/O support for TIFF per JEP 262
bpb
parents:
25859
diff
changeset
|
1159 |
} |
68c0d866db5d
8143342: Integrate Java Image I/O support for TIFF per JEP 262
bpb
parents:
25859
diff
changeset
|
1160 |
|
68c0d866db5d
8143342: Integrate Java Image I/O support for TIFF per JEP 262
bpb
parents:
25859
diff
changeset
|
1161 |
return imageType; |
68c0d866db5d
8143342: Integrate Java Image I/O support for TIFF per JEP 262
bpb
parents:
25859
diff
changeset
|
1162 |
} |
68c0d866db5d
8143342: Integrate Java Image I/O support for TIFF per JEP 262
bpb
parents:
25859
diff
changeset
|
1163 |
|
68c0d866db5d
8143342: Integrate Java Image I/O support for TIFF per JEP 262
bpb
parents:
25859
diff
changeset
|
1164 |
/** |
68c0d866db5d
8143342: Integrate Java Image I/O support for TIFF per JEP 262
bpb
parents:
25859
diff
changeset
|
1165 |
* Returns <code>true</code> if the given <code>ColorSpace</code> object is |
68c0d866db5d
8143342: Integrate Java Image I/O support for TIFF per JEP 262
bpb
parents:
25859
diff
changeset
|
1166 |
* an instance of <code>ICC_ColorSpace</code> but is not one of the standard |
68c0d866db5d
8143342: Integrate Java Image I/O support for TIFF per JEP 262
bpb
parents:
25859
diff
changeset
|
1167 |
* <code>ColorSpace</code>s returned by <code>ColorSpace.getInstance()</code>. |
68c0d866db5d
8143342: Integrate Java Image I/O support for TIFF per JEP 262
bpb
parents:
25859
diff
changeset
|
1168 |
* |
68c0d866db5d
8143342: Integrate Java Image I/O support for TIFF per JEP 262
bpb
parents:
25859
diff
changeset
|
1169 |
* @param cs The <code>ColorSpace</code> to test. |
68c0d866db5d
8143342: Integrate Java Image I/O support for TIFF per JEP 262
bpb
parents:
25859
diff
changeset
|
1170 |
*/ |
68c0d866db5d
8143342: Integrate Java Image I/O support for TIFF per JEP 262
bpb
parents:
25859
diff
changeset
|
1171 |
public static boolean isNonStandardICCColorSpace(ColorSpace cs) { |
68c0d866db5d
8143342: Integrate Java Image I/O support for TIFF per JEP 262
bpb
parents:
25859
diff
changeset
|
1172 |
boolean retval = false; |
68c0d866db5d
8143342: Integrate Java Image I/O support for TIFF per JEP 262
bpb
parents:
25859
diff
changeset
|
1173 |
|
68c0d866db5d
8143342: Integrate Java Image I/O support for TIFF per JEP 262
bpb
parents:
25859
diff
changeset
|
1174 |
try { |
68c0d866db5d
8143342: Integrate Java Image I/O support for TIFF per JEP 262
bpb
parents:
25859
diff
changeset
|
1175 |
// Check the standard ColorSpaces in decreasing order of |
68c0d866db5d
8143342: Integrate Java Image I/O support for TIFF per JEP 262
bpb
parents:
25859
diff
changeset
|
1176 |
// likelihood except check CS_PYCC last as in some JREs |
68c0d866db5d
8143342: Integrate Java Image I/O support for TIFF per JEP 262
bpb
parents:
25859
diff
changeset
|
1177 |
// PYCC.pf used not to be installed. |
68c0d866db5d
8143342: Integrate Java Image I/O support for TIFF per JEP 262
bpb
parents:
25859
diff
changeset
|
1178 |
retval = |
68c0d866db5d
8143342: Integrate Java Image I/O support for TIFF per JEP 262
bpb
parents:
25859
diff
changeset
|
1179 |
(cs instanceof ICC_ColorSpace) && |
68c0d866db5d
8143342: Integrate Java Image I/O support for TIFF per JEP 262
bpb
parents:
25859
diff
changeset
|
1180 |
!(cs.isCS_sRGB() || |
68c0d866db5d
8143342: Integrate Java Image I/O support for TIFF per JEP 262
bpb
parents:
25859
diff
changeset
|
1181 |
cs.equals(ColorSpace.getInstance(ColorSpace.CS_LINEAR_RGB)) || |
68c0d866db5d
8143342: Integrate Java Image I/O support for TIFF per JEP 262
bpb
parents:
25859
diff
changeset
|
1182 |
cs.equals(ColorSpace.getInstance(ColorSpace.CS_GRAY)) || |
68c0d866db5d
8143342: Integrate Java Image I/O support for TIFF per JEP 262
bpb
parents:
25859
diff
changeset
|
1183 |
cs.equals(ColorSpace.getInstance(ColorSpace.CS_CIEXYZ)) || |
68c0d866db5d
8143342: Integrate Java Image I/O support for TIFF per JEP 262
bpb
parents:
25859
diff
changeset
|
1184 |
cs.equals(ColorSpace.getInstance(ColorSpace.CS_PYCC))); |
68c0d866db5d
8143342: Integrate Java Image I/O support for TIFF per JEP 262
bpb
parents:
25859
diff
changeset
|
1185 |
} catch(IllegalArgumentException e) { |
68c0d866db5d
8143342: Integrate Java Image I/O support for TIFF per JEP 262
bpb
parents:
25859
diff
changeset
|
1186 |
// PYCC.pf not installed: ignore it - 'retval' is still 'false'. |
68c0d866db5d
8143342: Integrate Java Image I/O support for TIFF per JEP 262
bpb
parents:
25859
diff
changeset
|
1187 |
} |
68c0d866db5d
8143342: Integrate Java Image I/O support for TIFF per JEP 262
bpb
parents:
25859
diff
changeset
|
1188 |
|
68c0d866db5d
8143342: Integrate Java Image I/O support for TIFF per JEP 262
bpb
parents:
25859
diff
changeset
|
1189 |
return retval; |
68c0d866db5d
8143342: Integrate Java Image I/O support for TIFF per JEP 262
bpb
parents:
25859
diff
changeset
|
1190 |
} |
2 | 1191 |
} |