author | bae |
Tue, 19 Feb 2013 12:06:28 +0400 | |
changeset 16899 | 666a37b19844 |
parent 14884 | 74d1acdb7ee4 |
child 16900 | 5e0400ee2917 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
5506 | 2 |
* Copyright (c) 2007, 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 sun.java2d.cmm.lcms; |
|
27 |
||
28 |
import java.awt.Graphics2D; |
|
29 |
import java.awt.image.BufferedImage; |
|
30 |
import java.awt.image.ComponentColorModel; |
|
31 |
import java.awt.image.Raster; |
|
32 |
import java.awt.image.WritableRaster; |
|
33 |
import java.awt.image.SinglePixelPackedSampleModel; |
|
34 |
import java.awt.image.ComponentSampleModel; |
|
35 |
import java.awt.image.DataBuffer; |
|
36 |
import java.awt.image.DataBufferByte; |
|
37 |
import java.awt.image.DataBufferUShort; |
|
38 |
import java.awt.image.DataBufferInt; |
|
39 |
import java.awt.image.ColorModel; |
|
40 |
import sun.awt.image.ByteComponentRaster; |
|
41 |
import sun.awt.image.ShortComponentRaster; |
|
42 |
import sun.awt.image.IntegerComponentRaster; |
|
43 |
||
44 |
||
45 |
class LCMSImageLayout { |
|
46 |
||
47 |
public static int BYTES_SH(int x) { |
|
48 |
return x; |
|
49 |
} |
|
50 |
||
51 |
public static int EXTRA_SH(int x) { |
|
52 |
return x<<7; |
|
53 |
} |
|
54 |
||
55 |
public static int CHANNELS_SH(int x) { |
|
56 |
return x<<3; |
|
57 |
} |
|
58 |
||
59 |
public static final int SWAPFIRST = 1<<14; |
|
60 |
||
61 |
public static final int DOSWAP = 1<<10; |
|
62 |
||
63 |
public static final int PT_RGB_8 = |
|
64 |
CHANNELS_SH(3) | BYTES_SH(1); |
|
65 |
||
66 |
public static final int PT_GRAY_8 = |
|
67 |
CHANNELS_SH(1) | BYTES_SH(1); |
|
68 |
||
69 |
public static final int PT_GRAY_16 = |
|
70 |
CHANNELS_SH(1) | BYTES_SH(2); |
|
71 |
||
72 |
public static final int PT_RGBA_8 = |
|
73 |
EXTRA_SH(1) | CHANNELS_SH(3) | BYTES_SH(1); |
|
74 |
||
75 |
public static final int PT_ARGB_8 = |
|
76 |
EXTRA_SH(1) | CHANNELS_SH(3) | BYTES_SH(1) | SWAPFIRST; |
|
77 |
||
78 |
public static final int PT_BGR_8 = |
|
79 |
DOSWAP | CHANNELS_SH(3) | BYTES_SH(1); |
|
80 |
||
81 |
public static final int PT_ABGR_8 = |
|
82 |
DOSWAP | EXTRA_SH(1) | CHANNELS_SH(3) | BYTES_SH(1); |
|
83 |
||
84 |
public static final int PT_BGRA_8 = EXTRA_SH(1) | CHANNELS_SH(3) | |
|
85 |
BYTES_SH(1) | DOSWAP | SWAPFIRST; |
|
86 |
||
87 |
public static final int DT_BYTE = 0; |
|
88 |
public static final int DT_SHORT = 1; |
|
89 |
public static final int DT_INT = 2; |
|
90 |
public static final int DT_DOUBLE = 3; |
|
91 |
||
92 |
||
93 |
boolean isIntPacked = false; |
|
94 |
int pixelType; |
|
95 |
int dataType; |
|
96 |
int width; |
|
97 |
int height; |
|
98 |
int nextRowOffset; |
|
99 |
int offset; |
|
100 |
||
101 |
Object dataArray; |
|
16899 | 102 |
private int dataArrayLength; /* in bytes */ |
103 |
||
104 |
private LCMSImageLayout(int np, int pixelType, int pixelSize) |
|
105 |
throws ImageLayoutException |
|
106 |
{ |
|
2 | 107 |
this.pixelType = pixelType; |
108 |
width = np; |
|
109 |
height = 1; |
|
16899 | 110 |
nextRowOffset = safeMult(pixelSize, np); |
2 | 111 |
offset = 0; |
112 |
} |
|
113 |
||
114 |
private LCMSImageLayout(int width, int height, int pixelType, |
|
16899 | 115 |
int pixelSize) |
116 |
throws ImageLayoutException |
|
117 |
{ |
|
2 | 118 |
this.pixelType = pixelType; |
119 |
this.width = width; |
|
120 |
this.height = height; |
|
16899 | 121 |
nextRowOffset = safeMult(pixelSize, width); |
2 | 122 |
offset = 0; |
123 |
} |
|
124 |
||
125 |
||
16899 | 126 |
public LCMSImageLayout(byte[] data, int np, int pixelType, int pixelSize) |
127 |
throws ImageLayoutException |
|
128 |
{ |
|
2 | 129 |
this(np, pixelType, pixelSize); |
130 |
dataType = DT_BYTE; |
|
131 |
dataArray = data; |
|
16899 | 132 |
dataArrayLength = data.length; |
133 |
||
134 |
verify(); |
|
2 | 135 |
} |
136 |
||
16899 | 137 |
public LCMSImageLayout(short[] data, int np, int pixelType, int pixelSize) |
138 |
throws ImageLayoutException |
|
139 |
{ |
|
2 | 140 |
this(np, pixelType, pixelSize); |
141 |
dataType = DT_SHORT; |
|
142 |
dataArray = data; |
|
16899 | 143 |
dataArrayLength = 2 * data.length; |
144 |
||
145 |
verify(); |
|
2 | 146 |
} |
147 |
||
16899 | 148 |
public LCMSImageLayout(int[] data, int np, int pixelType, int pixelSize) |
149 |
throws ImageLayoutException |
|
150 |
{ |
|
2 | 151 |
this(np, pixelType, pixelSize); |
152 |
dataType = DT_INT; |
|
153 |
dataArray = data; |
|
16899 | 154 |
dataArrayLength = 4 * data.length; |
155 |
||
156 |
verify(); |
|
2 | 157 |
} |
158 |
||
159 |
public LCMSImageLayout(double[] data, int np, int pixelType, int pixelSize) |
|
16899 | 160 |
throws ImageLayoutException |
2 | 161 |
{ |
162 |
this(np, pixelType, pixelSize); |
|
163 |
dataType = DT_DOUBLE; |
|
164 |
dataArray = data; |
|
16899 | 165 |
dataArrayLength = 8 * data.length; |
166 |
||
167 |
verify(); |
|
2 | 168 |
} |
169 |
||
16899 | 170 |
public LCMSImageLayout(BufferedImage image) throws ImageLayoutException { |
2 | 171 |
ShortComponentRaster shortRaster; |
172 |
IntegerComponentRaster intRaster; |
|
173 |
ByteComponentRaster byteRaster; |
|
174 |
switch (image.getType()) { |
|
175 |
case BufferedImage.TYPE_INT_RGB: |
|
176 |
pixelType = PT_ARGB_8; |
|
177 |
isIntPacked = true; |
|
178 |
break; |
|
179 |
case BufferedImage.TYPE_INT_ARGB: |
|
180 |
pixelType = PT_ARGB_8; |
|
181 |
isIntPacked = true; |
|
182 |
break; |
|
183 |
case BufferedImage.TYPE_INT_BGR: |
|
184 |
pixelType = PT_ABGR_8; |
|
185 |
isIntPacked = true; |
|
186 |
break; |
|
187 |
case BufferedImage.TYPE_3BYTE_BGR: |
|
188 |
pixelType = PT_BGR_8; |
|
189 |
break; |
|
190 |
case BufferedImage.TYPE_4BYTE_ABGR: |
|
191 |
pixelType = PT_ABGR_8; |
|
192 |
break; |
|
193 |
case BufferedImage.TYPE_BYTE_GRAY: |
|
194 |
pixelType = PT_GRAY_8; |
|
195 |
break; |
|
196 |
case BufferedImage.TYPE_USHORT_GRAY: |
|
197 |
pixelType = PT_GRAY_16; |
|
198 |
break; |
|
199 |
default: |
|
200 |
// TODO: Add support for some images having |
|
201 |
// SinglePixelPackedModel and ComponentSampleModel |
|
202 |
throw new IllegalArgumentException( |
|
203 |
"CMMImageLayout - bad image type passed to constructor"); |
|
204 |
} |
|
205 |
||
206 |
width = image.getWidth(); |
|
207 |
height = image.getHeight(); |
|
208 |
||
209 |
switch (image.getType()) { |
|
210 |
case BufferedImage.TYPE_INT_RGB: |
|
211 |
case BufferedImage.TYPE_INT_ARGB: |
|
212 |
case BufferedImage.TYPE_INT_BGR: |
|
213 |
intRaster = (IntegerComponentRaster)image.getRaster(); |
|
16899 | 214 |
|
215 |
nextRowOffset = safeMult(4, intRaster.getScanlineStride()); |
|
216 |
||
217 |
offset = safeMult(4, intRaster.getDataOffset(0)); |
|
218 |
||
2 | 219 |
dataArray = intRaster.getDataStorage(); |
16899 | 220 |
dataArrayLength = 4 * intRaster.getDataStorage().length; |
2 | 221 |
dataType = DT_INT; |
222 |
break; |
|
223 |
||
224 |
case BufferedImage.TYPE_3BYTE_BGR: |
|
225 |
case BufferedImage.TYPE_4BYTE_ABGR: |
|
226 |
byteRaster = (ByteComponentRaster)image.getRaster(); |
|
227 |
nextRowOffset = byteRaster.getScanlineStride(); |
|
14884
74d1acdb7ee4
7124245: [lcms] ColorConvertOp to color space CS_GRAY apparently converts orange to 244,244,0
bae
parents:
5506
diff
changeset
|
228 |
int firstBand = image.getSampleModel().getNumBands() - 1; |
74d1acdb7ee4
7124245: [lcms] ColorConvertOp to color space CS_GRAY apparently converts orange to 244,244,0
bae
parents:
5506
diff
changeset
|
229 |
offset = byteRaster.getDataOffset(firstBand); |
2 | 230 |
dataArray = byteRaster.getDataStorage(); |
16899 | 231 |
dataArrayLength = byteRaster.getDataStorage().length; |
2 | 232 |
dataType = DT_BYTE; |
233 |
break; |
|
234 |
||
235 |
case BufferedImage.TYPE_BYTE_GRAY: |
|
236 |
byteRaster = (ByteComponentRaster)image.getRaster(); |
|
237 |
nextRowOffset = byteRaster.getScanlineStride(); |
|
238 |
offset = byteRaster.getDataOffset(0); |
|
239 |
dataArray = byteRaster.getDataStorage(); |
|
16899 | 240 |
dataArrayLength = byteRaster.getDataStorage().length; |
2 | 241 |
dataType = DT_BYTE; |
242 |
break; |
|
243 |
||
244 |
case BufferedImage.TYPE_USHORT_GRAY: |
|
245 |
shortRaster = (ShortComponentRaster)image.getRaster(); |
|
16899 | 246 |
nextRowOffset = safeMult(2, shortRaster.getScanlineStride()); |
247 |
offset = safeMult(2, shortRaster.getDataOffset(0)); |
|
2 | 248 |
dataArray = shortRaster.getDataStorage(); |
16899 | 249 |
dataArrayLength = 2 * shortRaster.getDataStorage().length; |
2 | 250 |
dataType = DT_SHORT; |
251 |
break; |
|
252 |
} |
|
16899 | 253 |
verify(); |
2 | 254 |
} |
255 |
||
256 |
public static boolean isSupported(BufferedImage image) { |
|
257 |
switch (image.getType()) { |
|
258 |
case BufferedImage.TYPE_INT_RGB: |
|
259 |
case BufferedImage.TYPE_INT_ARGB: |
|
260 |
case BufferedImage.TYPE_INT_BGR: |
|
261 |
case BufferedImage.TYPE_3BYTE_BGR: |
|
262 |
case BufferedImage.TYPE_4BYTE_ABGR: |
|
263 |
case BufferedImage.TYPE_BYTE_GRAY: |
|
264 |
case BufferedImage.TYPE_USHORT_GRAY: |
|
265 |
return true; |
|
266 |
} |
|
267 |
return false; |
|
268 |
} |
|
16899 | 269 |
|
270 |
private void verify() throws ImageLayoutException { |
|
271 |
||
272 |
if (offset < 0 || offset >= dataArrayLength) { |
|
273 |
throw new ImageLayoutException("Invalid image layout"); |
|
274 |
} |
|
275 |
||
276 |
int lastPixelOffset = safeMult(nextRowOffset, (height - 1)); |
|
277 |
||
278 |
lastPixelOffset = safeAdd(lastPixelOffset, (width - 1)); |
|
279 |
||
280 |
int off = safeAdd(offset, lastPixelOffset); |
|
281 |
||
282 |
if (off < 0 || off >= dataArrayLength) { |
|
283 |
throw new ImageLayoutException("Invalid image layout"); |
|
284 |
} |
|
285 |
} |
|
286 |
||
287 |
static int safeAdd(int a, int b) throws ImageLayoutException { |
|
288 |
long res = a; |
|
289 |
res += b; |
|
290 |
if (res < Integer.MIN_VALUE || res > Integer.MAX_VALUE) { |
|
291 |
throw new ImageLayoutException("Invalid image layout"); |
|
292 |
} |
|
293 |
return (int)res; |
|
294 |
} |
|
295 |
||
296 |
static int safeMult(int a, int b) throws ImageLayoutException { |
|
297 |
long res = a; |
|
298 |
res *= b; |
|
299 |
if (res < Integer.MIN_VALUE || res > Integer.MAX_VALUE) { |
|
300 |
throw new ImageLayoutException("Invalid image layout"); |
|
301 |
} |
|
302 |
return (int)res; |
|
303 |
} |
|
304 |
||
305 |
public static class ImageLayoutException extends Exception { |
|
306 |
public ImageLayoutException(String message) { |
|
307 |
super(message); |
|
308 |
} |
|
309 |
} |
|
2 | 310 |
} |