001: /*
002: * Copyright 2007 Sun Microsystems, Inc. All Rights Reserved.
003: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004: *
005: * This code is free software; you can redistribute it and/or modify it
006: * under the terms of the GNU General Public License version 2 only, as
007: * published by the Free Software Foundation. Sun designates this
008: * particular file as subject to the "Classpath" exception as provided
009: * by Sun in the LICENSE file that accompanied this code.
010: *
011: * This code is distributed in the hope that it will be useful, but WITHOUT
012: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014: * version 2 for more details (a copy is included in the LICENSE file that
015: * accompanied this code).
016: *
017: * You should have received a copy of the GNU General Public License version
018: * 2 along with this work; if not, write to the Free Software Foundation,
019: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020: *
021: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022: * CA 95054 USA or visit www.sun.com if you need additional information or
023: * have any questions.
024: */
025:
026: package sun.java2d.cmm.lcms;
027:
028: import java.awt.Graphics2D;
029: import java.awt.image.BufferedImage;
030: import java.awt.image.ComponentColorModel;
031: import java.awt.image.Raster;
032: import java.awt.image.WritableRaster;
033: import java.awt.image.SinglePixelPackedSampleModel;
034: import java.awt.image.ComponentSampleModel;
035: import java.awt.image.DataBuffer;
036: import java.awt.image.DataBufferByte;
037: import java.awt.image.DataBufferUShort;
038: import java.awt.image.DataBufferInt;
039: import java.awt.image.ColorModel;
040: import sun.awt.image.ByteComponentRaster;
041: import sun.awt.image.ShortComponentRaster;
042: import sun.awt.image.IntegerComponentRaster;
043:
044: class LCMSImageLayout {
045:
046: public static int BYTES_SH(int x) {
047: return x;
048: }
049:
050: public static int EXTRA_SH(int x) {
051: return x << 7;
052: }
053:
054: public static int CHANNELS_SH(int x) {
055: return x << 3;
056: }
057:
058: public static final int SWAPFIRST = 1 << 14;
059:
060: public static final int DOSWAP = 1 << 10;
061:
062: public static final int PT_RGB_8 = CHANNELS_SH(3) | BYTES_SH(1);
063:
064: public static final int PT_GRAY_8 = CHANNELS_SH(1) | BYTES_SH(1);
065:
066: public static final int PT_GRAY_16 = CHANNELS_SH(1) | BYTES_SH(2);
067:
068: public static final int PT_RGBA_8 = EXTRA_SH(1) | CHANNELS_SH(3)
069: | BYTES_SH(1);
070:
071: public static final int PT_ARGB_8 = EXTRA_SH(1) | CHANNELS_SH(3)
072: | BYTES_SH(1) | SWAPFIRST;
073:
074: public static final int PT_BGR_8 = DOSWAP | CHANNELS_SH(3)
075: | BYTES_SH(1);
076:
077: public static final int PT_ABGR_8 = DOSWAP | EXTRA_SH(1)
078: | CHANNELS_SH(3) | BYTES_SH(1);
079:
080: public static final int PT_BGRA_8 = EXTRA_SH(1) | CHANNELS_SH(3)
081: | BYTES_SH(1) | DOSWAP | SWAPFIRST;
082:
083: public static final int DT_BYTE = 0;
084: public static final int DT_SHORT = 1;
085: public static final int DT_INT = 2;
086: public static final int DT_DOUBLE = 3;
087:
088: boolean isIntPacked = false;
089: int pixelType;
090: int dataType;
091: int width;
092: int height;
093: int nextRowOffset;
094: int offset;
095:
096: Object dataArray;
097:
098: private LCMSImageLayout(int np, int pixelType, int pixelSize) {
099: this .pixelType = pixelType;
100: width = np;
101: height = 1;
102: nextRowOffset = np * pixelSize;
103: offset = 0;
104: }
105:
106: private LCMSImageLayout(int width, int height, int pixelType,
107: int pixelSize) {
108: this .pixelType = pixelType;
109: this .width = width;
110: this .height = height;
111: nextRowOffset = width * pixelSize;
112: offset = 0;
113: }
114:
115: public LCMSImageLayout(byte[] data, int np, int pixelType,
116: int pixelSize) {
117: this (np, pixelType, pixelSize);
118: dataType = DT_BYTE;
119: dataArray = data;
120: }
121:
122: public LCMSImageLayout(short[] data, int np, int pixelType,
123: int pixelSize) {
124: this (np, pixelType, pixelSize);
125: dataType = DT_SHORT;
126: dataArray = data;
127: }
128:
129: public LCMSImageLayout(int[] data, int np, int pixelType,
130: int pixelSize) {
131: this (np, pixelType, pixelSize);
132: dataType = DT_INT;
133: dataArray = data;
134: }
135:
136: public LCMSImageLayout(double[] data, int np, int pixelType,
137: int pixelSize) {
138: this (np, pixelType, pixelSize);
139: dataType = DT_DOUBLE;
140: dataArray = data;
141: }
142:
143: public LCMSImageLayout(BufferedImage image) {
144: ShortComponentRaster shortRaster;
145: IntegerComponentRaster intRaster;
146: ByteComponentRaster byteRaster;
147: switch (image.getType()) {
148: case BufferedImage.TYPE_INT_RGB:
149: pixelType = PT_ARGB_8;
150: isIntPacked = true;
151: break;
152: case BufferedImage.TYPE_INT_ARGB:
153: pixelType = PT_ARGB_8;
154: isIntPacked = true;
155: break;
156: case BufferedImage.TYPE_INT_BGR:
157: pixelType = PT_ABGR_8;
158: isIntPacked = true;
159: break;
160: case BufferedImage.TYPE_3BYTE_BGR:
161: pixelType = PT_BGR_8;
162: break;
163: case BufferedImage.TYPE_4BYTE_ABGR:
164: pixelType = PT_ABGR_8;
165: break;
166: case BufferedImage.TYPE_BYTE_GRAY:
167: pixelType = PT_GRAY_8;
168: break;
169: case BufferedImage.TYPE_USHORT_GRAY:
170: pixelType = PT_GRAY_16;
171: break;
172: default:
173: // TODO: Add support for some images having
174: // SinglePixelPackedModel and ComponentSampleModel
175: throw new IllegalArgumentException(
176: "CMMImageLayout - bad image type passed to constructor");
177: }
178:
179: width = image.getWidth();
180: height = image.getHeight();
181:
182: switch (image.getType()) {
183: case BufferedImage.TYPE_INT_RGB:
184: case BufferedImage.TYPE_INT_ARGB:
185: case BufferedImage.TYPE_INT_BGR:
186: intRaster = (IntegerComponentRaster) image.getRaster();
187: nextRowOffset = intRaster.getScanlineStride() * 4;
188: offset = intRaster.getDataOffset(0) * 4;
189: dataArray = intRaster.getDataStorage();
190: dataType = DT_INT;
191: break;
192:
193: case BufferedImage.TYPE_3BYTE_BGR:
194: case BufferedImage.TYPE_4BYTE_ABGR:
195: byteRaster = (ByteComponentRaster) image.getRaster();
196: nextRowOffset = byteRaster.getScanlineStride();
197: offset = byteRaster.getDataOffset(0);
198: dataArray = byteRaster.getDataStorage();
199: dataType = DT_BYTE;
200: break;
201:
202: case BufferedImage.TYPE_BYTE_GRAY:
203: byteRaster = (ByteComponentRaster) image.getRaster();
204: nextRowOffset = byteRaster.getScanlineStride();
205: offset = byteRaster.getDataOffset(0);
206: dataArray = byteRaster.getDataStorage();
207: dataType = DT_BYTE;
208: break;
209:
210: case BufferedImage.TYPE_USHORT_GRAY:
211: shortRaster = (ShortComponentRaster) image.getRaster();
212: nextRowOffset = shortRaster.getScanlineStride() * 2;
213: offset = shortRaster.getDataOffset(0) * 2;
214: dataArray = shortRaster.getDataStorage();
215: dataType = DT_SHORT;
216: break;
217: }
218: }
219:
220: public static boolean isSupported(BufferedImage image) {
221: switch (image.getType()) {
222: case BufferedImage.TYPE_INT_RGB:
223: case BufferedImage.TYPE_INT_ARGB:
224: case BufferedImage.TYPE_INT_BGR:
225: case BufferedImage.TYPE_3BYTE_BGR:
226: case BufferedImage.TYPE_4BYTE_ABGR:
227: case BufferedImage.TYPE_BYTE_GRAY:
228: case BufferedImage.TYPE_USHORT_GRAY:
229: return true;
230: }
231: return false;
232: }
233: }
|