1/*
2 * Copyright (c) 2007, 2016, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.  Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact 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.
24 */
25package sun.java2d.cmm.lcms;
26
27import java.awt.image.BufferedImage;
28import java.awt.image.ComponentColorModel;
29import java.awt.image.ComponentSampleModel;
30import java.awt.image.ColorModel;
31import java.awt.image.Raster;
32import java.awt.image.SampleModel;
33import sun.awt.image.ByteComponentRaster;
34import sun.awt.image.ShortComponentRaster;
35import sun.awt.image.IntegerComponentRaster;
36
37class LCMSImageLayout {
38
39    public static int BYTES_SH(int x) {
40        return x;
41    }
42
43    public static int EXTRA_SH(int x) {
44        return x << 7;
45    }
46
47    public static int CHANNELS_SH(int x) {
48        return x << 3;
49    }
50    public static final int SWAPFIRST = 1 << 14;
51    public static final int DOSWAP = 1 << 10;
52    public static final int PT_RGB_8 =
53            CHANNELS_SH(3) | BYTES_SH(1);
54    public static final int PT_GRAY_8 =
55            CHANNELS_SH(1) | BYTES_SH(1);
56    public static final int PT_GRAY_16 =
57            CHANNELS_SH(1) | BYTES_SH(2);
58    public static final int PT_RGBA_8 =
59            EXTRA_SH(1) | CHANNELS_SH(3) | BYTES_SH(1);
60    public static final int PT_ARGB_8 =
61            EXTRA_SH(1) | CHANNELS_SH(3) | BYTES_SH(1) | SWAPFIRST;
62    public static final int PT_BGR_8 =
63            DOSWAP | CHANNELS_SH(3) | BYTES_SH(1);
64    public static final int PT_ABGR_8 =
65            DOSWAP | EXTRA_SH(1) | CHANNELS_SH(3) | BYTES_SH(1);
66    public static final int PT_BGRA_8 = EXTRA_SH(1) | CHANNELS_SH(3)
67            | BYTES_SH(1) | DOSWAP | SWAPFIRST;
68    public static final int DT_BYTE = 0;
69    public static final int DT_SHORT = 1;
70    public static final int DT_INT = 2;
71    public static final int DT_DOUBLE = 3;
72    boolean isIntPacked = false;
73    int pixelType;
74    int dataType;
75    int width;
76    int height;
77    int nextRowOffset;
78    private int nextPixelOffset;
79    int offset;
80
81    /* This flag indicates whether the image can be processed
82     * at once by doTransfrom() native call. Otherwise, the
83     * image is processed scan by scan.
84     */
85    private boolean imageAtOnce = false;
86    Object dataArray;
87
88    private int dataArrayLength; /* in bytes */
89
90    private LCMSImageLayout(int np, int pixelType, int pixelSize)
91            throws ImageLayoutException
92    {
93        this.pixelType = pixelType;
94        width = np;
95        height = 1;
96        nextPixelOffset = pixelSize;
97        nextRowOffset = safeMult(pixelSize, np);
98        offset = 0;
99    }
100
101    private LCMSImageLayout(int width, int height, int pixelType,
102                            int pixelSize)
103            throws ImageLayoutException
104    {
105        this.pixelType = pixelType;
106        this.width = width;
107        this.height = height;
108        nextPixelOffset = pixelSize;
109        nextRowOffset = safeMult(pixelSize, width);
110        offset = 0;
111    }
112
113
114    public LCMSImageLayout(byte[] data, int np, int pixelType, int pixelSize)
115            throws ImageLayoutException
116    {
117        this(np, pixelType, pixelSize);
118        dataType = DT_BYTE;
119        dataArray = data;
120        dataArrayLength = data.length;
121
122        verify();
123    }
124
125    public LCMSImageLayout(short[] data, int np, int pixelType, int pixelSize)
126            throws ImageLayoutException
127    {
128        this(np, pixelType, pixelSize);
129        dataType = DT_SHORT;
130        dataArray = data;
131        dataArrayLength = 2 * data.length;
132
133        verify();
134    }
135
136    public LCMSImageLayout(int[] data, int np, int pixelType, int pixelSize)
137            throws ImageLayoutException
138    {
139        this(np, pixelType, pixelSize);
140        dataType = DT_INT;
141        dataArray = data;
142        dataArrayLength = 4 * data.length;
143
144        verify();
145    }
146
147    public LCMSImageLayout(double[] data, int np, int pixelType, int pixelSize)
148            throws ImageLayoutException
149    {
150        this(np, pixelType, pixelSize);
151        dataType = DT_DOUBLE;
152        dataArray = data;
153        dataArrayLength = 8 * data.length;
154
155        verify();
156    }
157
158    private LCMSImageLayout() {
159    }
160
161    /* This method creates a layout object for given image.
162     * Returns null if the image is not supported by current implementation.
163     */
164    public static LCMSImageLayout createImageLayout(BufferedImage image) throws ImageLayoutException {
165        LCMSImageLayout l = new LCMSImageLayout();
166
167        switch (image.getType()) {
168            case BufferedImage.TYPE_INT_RGB:
169                l.pixelType = PT_ARGB_8;
170                l.isIntPacked = true;
171                break;
172            case BufferedImage.TYPE_INT_ARGB:
173                l.pixelType = PT_ARGB_8;
174                l.isIntPacked = true;
175                break;
176            case BufferedImage.TYPE_INT_BGR:
177                l.pixelType = PT_ABGR_8;
178                l.isIntPacked = true;
179                break;
180            case BufferedImage.TYPE_3BYTE_BGR:
181                l.pixelType = PT_BGR_8;
182                break;
183            case BufferedImage.TYPE_4BYTE_ABGR:
184                l.pixelType = PT_ABGR_8;
185                break;
186            case BufferedImage.TYPE_BYTE_GRAY:
187                l.pixelType = PT_GRAY_8;
188                break;
189            case BufferedImage.TYPE_USHORT_GRAY:
190                l.pixelType = PT_GRAY_16;
191                break;
192            default:
193                /* ColorConvertOp creates component images as
194                 * default destination, so this kind of images
195                 * has to be supported.
196                 */
197                ColorModel cm = image.getColorModel();
198                if (cm instanceof ComponentColorModel) {
199                    ComponentColorModel ccm = (ComponentColorModel) cm;
200
201                    // verify whether the component size is fine
202                    int[] cs = ccm.getComponentSize();
203                    for (int s : cs) {
204                        if (s != 8) {
205                            return null;
206                        }
207                    }
208
209                    return createImageLayout(image.getRaster());
210
211                }
212                return null;
213        }
214
215        l.width = image.getWidth();
216        l.height = image.getHeight();
217
218        switch (image.getType()) {
219            case BufferedImage.TYPE_INT_RGB:
220            case BufferedImage.TYPE_INT_ARGB:
221            case BufferedImage.TYPE_INT_BGR:
222                do {
223                    IntegerComponentRaster intRaster = (IntegerComponentRaster)
224                            image.getRaster();
225                    l.nextRowOffset = safeMult(4, intRaster.getScanlineStride());
226                    l.nextPixelOffset = safeMult(4, intRaster.getPixelStride());
227                    l.offset = safeMult(4, intRaster.getDataOffset(0));
228                    l.dataArray = intRaster.getDataStorage();
229                    l.dataArrayLength = 4 * intRaster.getDataStorage().length;
230                    l.dataType = DT_INT;
231
232                    if (l.nextRowOffset == l.width * 4 * intRaster.getPixelStride()) {
233                        l.imageAtOnce = true;
234                    }
235                } while (false);
236                break;
237
238            case BufferedImage.TYPE_3BYTE_BGR:
239            case BufferedImage.TYPE_4BYTE_ABGR:
240                do {
241                    ByteComponentRaster byteRaster = (ByteComponentRaster)
242                            image.getRaster();
243                    l.nextRowOffset = byteRaster.getScanlineStride();
244                    l.nextPixelOffset = byteRaster.getPixelStride();
245
246                    int firstBand = image.getSampleModel().getNumBands() - 1;
247                    l.offset = byteRaster.getDataOffset(firstBand);
248                    l.dataArray = byteRaster.getDataStorage();
249                    l.dataArrayLength = byteRaster.getDataStorage().length;
250                    l.dataType = DT_BYTE;
251                    if (l.nextRowOffset == l.width * byteRaster.getPixelStride()) {
252                        l.imageAtOnce = true;
253                    }
254                } while (false);
255                break;
256
257            case BufferedImage.TYPE_BYTE_GRAY:
258                do {
259                    ByteComponentRaster byteRaster = (ByteComponentRaster)
260                            image.getRaster();
261                    l.nextRowOffset = byteRaster.getScanlineStride();
262                    l.nextPixelOffset = byteRaster.getPixelStride();
263
264                    l.dataArrayLength = byteRaster.getDataStorage().length;
265                    l.offset = byteRaster.getDataOffset(0);
266                    l.dataArray = byteRaster.getDataStorage();
267                    l.dataType = DT_BYTE;
268
269                    if (l.nextRowOffset == l.width * byteRaster.getPixelStride()) {
270                        l.imageAtOnce = true;
271                    }
272                } while (false);
273                break;
274
275            case BufferedImage.TYPE_USHORT_GRAY:
276                do {
277                    ShortComponentRaster shortRaster = (ShortComponentRaster)
278                            image.getRaster();
279                    l.nextRowOffset = safeMult(2, shortRaster.getScanlineStride());
280                    l.nextPixelOffset = safeMult(2, shortRaster.getPixelStride());
281
282                    l.offset = safeMult(2, shortRaster.getDataOffset(0));
283                    l.dataArray = shortRaster.getDataStorage();
284                    l.dataArrayLength = 2 * shortRaster.getDataStorage().length;
285                    l.dataType = DT_SHORT;
286
287                    if (l.nextRowOffset == l.width * 2 * shortRaster.getPixelStride()) {
288                        l.imageAtOnce = true;
289                    }
290                } while (false);
291                break;
292            default:
293                return null;
294        }
295        l.verify();
296        return l;
297    }
298
299    private static enum BandOrder {
300        DIRECT,
301        INVERTED,
302        ARBITRARY,
303        UNKNOWN;
304
305        public static BandOrder getBandOrder(int[] bandOffsets) {
306            BandOrder order = UNKNOWN;
307
308            int numBands = bandOffsets.length;
309
310            for (int i = 0; (order != ARBITRARY) && (i < bandOffsets.length); i++) {
311                switch (order) {
312                    case UNKNOWN:
313                        if (bandOffsets[i] == i) {
314                            order = DIRECT;
315                        } else if (bandOffsets[i] == (numBands - 1 - i)) {
316                            order = INVERTED;
317                        } else {
318                            order = ARBITRARY;
319                        }
320                        break;
321                    case DIRECT:
322                        if (bandOffsets[i] != i) {
323                            order = ARBITRARY;
324                        }
325                        break;
326                    case INVERTED:
327                        if (bandOffsets[i] != (numBands - 1 - i)) {
328                            order = ARBITRARY;
329                        }
330                        break;
331                }
332            }
333            return order;
334        }
335    }
336
337    private void verify() throws ImageLayoutException {
338
339        if (offset < 0 || offset >= dataArrayLength) {
340            throw new ImageLayoutException("Invalid image layout");
341        }
342
343        if (nextPixelOffset != getBytesPerPixel(pixelType)) {
344            throw new ImageLayoutException("Invalid image layout");
345        }
346
347        int lastScanOffset = safeMult(nextRowOffset, (height - 1));
348
349        int lastPixelOffset = safeMult(nextPixelOffset, (width -1 ));
350
351        lastPixelOffset = safeAdd(lastPixelOffset, lastScanOffset);
352
353        int off = safeAdd(offset, lastPixelOffset);
354
355        if (off < 0 || off >= dataArrayLength) {
356            throw new ImageLayoutException("Invalid image layout");
357        }
358    }
359
360    static int safeAdd(int a, int b) throws ImageLayoutException {
361        long res = a;
362        res += b;
363        if (res < Integer.MIN_VALUE || res > Integer.MAX_VALUE) {
364            throw new ImageLayoutException("Invalid image layout");
365        }
366        return (int)res;
367    }
368
369    static int safeMult(int a, int b) throws ImageLayoutException {
370        long res = a;
371        res *= b;
372        if (res < Integer.MIN_VALUE || res > Integer.MAX_VALUE) {
373            throw new ImageLayoutException("Invalid image layout");
374        }
375        return (int)res;
376    }
377
378    @SuppressWarnings("serial") // JDK-implementation class
379    public static class ImageLayoutException extends Exception {
380        public ImageLayoutException(String message) {
381            super(message);
382        }
383    }
384    public static LCMSImageLayout createImageLayout(Raster r) {
385        LCMSImageLayout l = new LCMSImageLayout();
386        if (r instanceof ByteComponentRaster &&
387                r.getSampleModel() instanceof ComponentSampleModel) {
388            ByteComponentRaster br = (ByteComponentRaster)r;
389
390            ComponentSampleModel csm = (ComponentSampleModel)r.getSampleModel();
391
392            l.pixelType = CHANNELS_SH(br.getNumBands()) | BYTES_SH(1);
393
394            int[] bandOffsets = csm.getBandOffsets();
395            BandOrder order = BandOrder.getBandOrder(bandOffsets);
396
397            int firstBand = 0;
398            switch (order) {
399                case INVERTED:
400                    l.pixelType |= DOSWAP;
401                    firstBand  = csm.getNumBands() - 1;
402                    break;
403                case DIRECT:
404                    // do nothing
405                    break;
406                default:
407                    // unable to create the image layout;
408                    return null;
409            }
410
411            l.nextRowOffset = br.getScanlineStride();
412            l.nextPixelOffset = br.getPixelStride();
413
414            l.offset = br.getDataOffset(firstBand);
415            l.dataArray = br.getDataStorage();
416            l.dataType = DT_BYTE;
417
418            l.width = br.getWidth();
419            l.height = br.getHeight();
420
421            if (l.nextRowOffset == l.width * br.getPixelStride()) {
422                l.imageAtOnce = true;
423            }
424            return l;
425        }
426        return null;
427    }
428
429    /**
430     * Derives number of bytes per pixel from the pixel format.
431     * Following bit fields are used here:
432     *  [0..2] - bytes per sample
433     *  [3..6] - number of color samples per pixel
434     *  [7..9] - number of non-color samples per pixel
435     *
436     * A complete description of the pixel format can be found
437     * here: lcms2.h, lines 651 - 667.
438     *
439     * @param pixelType pixel format in lcms2 notation.
440     * @return number of bytes per pixel for given pixel format.
441     */
442    private static int getBytesPerPixel(int pixelType) {
443        int bytesPerSample = (0x7 & pixelType);
444        int colorSamplesPerPixel = 0xF & (pixelType >> 3);
445        int extraSamplesPerPixel = 0x7 & (pixelType >> 7);
446
447        return bytesPerSample * (colorSamplesPerPixel + extraSamplesPerPixel);
448    }
449}
450