Search in sources :

Example 51 with ColorSpace

use of java.awt.color.ColorSpace in project jdk8u_jdk by JetBrains.

the class ImageTypeSpecifierTest method createInterleavedTest.

public static void createInterleavedTest() {
    ColorSpace cs = ColorSpace.getInstance(ColorSpace.CS_sRGB);
    int[] bandOffsets = { 0, 0, 0, 0 };
    int dataType = 0;
    boolean hasAlpha = true;
    boolean isAlphaPremultiplied = true;
    try {
        ImageTypeSpecifier.createInterleaved(null, bandOffsets, dataType, hasAlpha, isAlphaPremultiplied);
        fail("Failed to get IAE!");
    } catch (IllegalArgumentException e) {
    }
    try {
        ImageTypeSpecifier.createInterleaved(cs, null, dataType, hasAlpha, isAlphaPremultiplied);
        fail("Failed to get IAE!");
    } catch (IllegalArgumentException e) {
    }
    int[] bad_bandOffsets = { 0, 100, 1000 };
    try {
        ImageTypeSpecifier.createInterleaved(cs, bad_bandOffsets, dataType, hasAlpha, isAlphaPremultiplied);
        fail("Failed to get IAE!");
    } catch (IllegalArgumentException e) {
    }
    int[] bad_bandOffsets_1 = {};
    try {
        ImageTypeSpecifier.createInterleaved(cs, bad_bandOffsets_1, dataType, hasAlpha, isAlphaPremultiplied);
        fail("Failed to get IAE!");
    } catch (IllegalArgumentException e) {
    }
}
Also used : ColorSpace(java.awt.color.ColorSpace)

Example 52 with ColorSpace

use of java.awt.color.ColorSpace in project jdk8u_jdk by JetBrains.

the class ImageTypeSpecifierTest method createPackedTest.

public static void createPackedTest() {
    ColorSpace cs = ColorSpace.getInstance(ColorSpace.CS_sRGB);
    int rmask = 0x00ff0000;
    int gmask = 0x0000ff00;
    int bmask = 0x000000ff;
    int amask = 0xff000000;
    try {
        ImageTypeSpecifier.createPacked(null, rmask, gmask, bmask, amask, 0, false);
        fail("Failed to get IAE!");
    } catch (IllegalArgumentException e) {
    }
    ColorSpace cs1 = ColorSpace.getInstance(ColorSpace.CS_GRAY);
    try {
        ImageTypeSpecifier.createPacked(cs1, rmask, gmask, bmask, amask, 0, false);
        fail("Failed to get IAE!");
    } catch (IllegalArgumentException e) {
    }
    try {
        ImageTypeSpecifier.createPacked(cs, 0, 0, 0, 0, 0, false);
        fail("Failed to get IAE!");
    } catch (IllegalArgumentException e) {
    }
    try {
        ImageTypeSpecifier.createPacked(cs, rmask, gmask, bmask, amask, -1, false);
        fail("Failed to get IAE!");
    } catch (IllegalArgumentException e) {
    }
}
Also used : ColorSpace(java.awt.color.ColorSpace)

Example 53 with ColorSpace

use of java.awt.color.ColorSpace in project jdk8u_jdk by JetBrains.

the class LCMSTransform method colorConvert.

public void colorConvert(BufferedImage src, BufferedImage dst) {
    LCMSImageLayout srcIL, dstIL;
    try {
        if (!dst.getColorModel().hasAlpha()) {
            dstIL = LCMSImageLayout.createImageLayout(dst);
            if (dstIL != null) {
                srcIL = LCMSImageLayout.createImageLayout(src);
                if (srcIL != null) {
                    doTransform(srcIL, dstIL);
                    return;
                }
            }
        }
    } catch (ImageLayoutException e) {
        throw new CMMException("Unable to convert images");
    }
    Raster srcRas = src.getRaster();
    WritableRaster dstRas = dst.getRaster();
    ColorModel srcCM = src.getColorModel();
    ColorModel dstCM = dst.getColorModel();
    int w = src.getWidth();
    int h = src.getHeight();
    int srcNumComp = srcCM.getNumColorComponents();
    int dstNumComp = dstCM.getNumColorComponents();
    int precision = 8;
    float maxNum = 255.0f;
    for (int i = 0; i < srcNumComp; i++) {
        if (srcCM.getComponentSize(i) > 8) {
            precision = 16;
            maxNum = 65535.0f;
        }
    }
    for (int i = 0; i < dstNumComp; i++) {
        if (dstCM.getComponentSize(i) > 8) {
            precision = 16;
            maxNum = 65535.0f;
        }
    }
    float[] srcMinVal = new float[srcNumComp];
    float[] srcInvDiffMinMax = new float[srcNumComp];
    ColorSpace cs = srcCM.getColorSpace();
    for (int i = 0; i < srcNumComp; i++) {
        srcMinVal[i] = cs.getMinValue(i);
        srcInvDiffMinMax[i] = maxNum / (cs.getMaxValue(i) - srcMinVal[i]);
    }
    cs = dstCM.getColorSpace();
    float[] dstMinVal = new float[dstNumComp];
    float[] dstDiffMinMax = new float[dstNumComp];
    for (int i = 0; i < dstNumComp; i++) {
        dstMinVal[i] = cs.getMinValue(i);
        dstDiffMinMax[i] = (cs.getMaxValue(i) - dstMinVal[i]) / maxNum;
    }
    boolean dstHasAlpha = dstCM.hasAlpha();
    boolean needSrcAlpha = srcCM.hasAlpha() && dstHasAlpha;
    float[] dstColor;
    if (dstHasAlpha) {
        dstColor = new float[dstNumComp + 1];
    } else {
        dstColor = new float[dstNumComp];
    }
    if (precision == 8) {
        byte[] srcLine = new byte[w * srcNumComp];
        byte[] dstLine = new byte[w * dstNumComp];
        Object pixel;
        float[] color;
        float[] alpha = null;
        if (needSrcAlpha) {
            alpha = new float[w];
        }
        int idx;
        // TODO check for src npixels = dst npixels
        try {
            srcIL = new LCMSImageLayout(srcLine, srcLine.length / getNumInComponents(), LCMSImageLayout.CHANNELS_SH(getNumInComponents()) | LCMSImageLayout.BYTES_SH(1), getNumInComponents());
            dstIL = new LCMSImageLayout(dstLine, dstLine.length / getNumOutComponents(), LCMSImageLayout.CHANNELS_SH(getNumOutComponents()) | LCMSImageLayout.BYTES_SH(1), getNumOutComponents());
        } catch (ImageLayoutException e) {
            throw new CMMException("Unable to convert images");
        }
        // process each scanline
        for (int y = 0; y < h; y++) {
            // convert src scanline
            pixel = null;
            color = null;
            idx = 0;
            for (int x = 0; x < w; x++) {
                pixel = srcRas.getDataElements(x, y, pixel);
                color = srcCM.getNormalizedComponents(pixel, color, 0);
                for (int i = 0; i < srcNumComp; i++) {
                    srcLine[idx++] = (byte) ((color[i] - srcMinVal[i]) * srcInvDiffMinMax[i] + 0.5f);
                }
                if (needSrcAlpha) {
                    alpha[x] = color[srcNumComp];
                }
            }
            // color convert srcLine to dstLine
            doTransform(srcIL, dstIL);
            // convert dst scanline
            pixel = null;
            idx = 0;
            for (int x = 0; x < w; x++) {
                for (int i = 0; i < dstNumComp; i++) {
                    dstColor[i] = ((float) (dstLine[idx++] & 0xff)) * dstDiffMinMax[i] + dstMinVal[i];
                }
                if (needSrcAlpha) {
                    dstColor[dstNumComp] = alpha[x];
                } else if (dstHasAlpha) {
                    dstColor[dstNumComp] = 1.0f;
                }
                pixel = dstCM.getDataElements(dstColor, 0, pixel);
                dstRas.setDataElements(x, y, pixel);
            }
        }
    } else {
        short[] srcLine = new short[w * srcNumComp];
        short[] dstLine = new short[w * dstNumComp];
        Object pixel;
        float[] color;
        float[] alpha = null;
        if (needSrcAlpha) {
            alpha = new float[w];
        }
        int idx;
        try {
            srcIL = new LCMSImageLayout(srcLine, srcLine.length / getNumInComponents(), LCMSImageLayout.CHANNELS_SH(getNumInComponents()) | LCMSImageLayout.BYTES_SH(2), getNumInComponents() * 2);
            dstIL = new LCMSImageLayout(dstLine, dstLine.length / getNumOutComponents(), LCMSImageLayout.CHANNELS_SH(getNumOutComponents()) | LCMSImageLayout.BYTES_SH(2), getNumOutComponents() * 2);
        } catch (ImageLayoutException e) {
            throw new CMMException("Unable to convert images");
        }
        // process each scanline
        for (int y = 0; y < h; y++) {
            // convert src scanline
            pixel = null;
            color = null;
            idx = 0;
            for (int x = 0; x < w; x++) {
                pixel = srcRas.getDataElements(x, y, pixel);
                color = srcCM.getNormalizedComponents(pixel, color, 0);
                for (int i = 0; i < srcNumComp; i++) {
                    srcLine[idx++] = (short) ((color[i] - srcMinVal[i]) * srcInvDiffMinMax[i] + 0.5f);
                }
                if (needSrcAlpha) {
                    alpha[x] = color[srcNumComp];
                }
            }
            // color convert srcLine to dstLine
            doTransform(srcIL, dstIL);
            // convert dst scanline
            pixel = null;
            idx = 0;
            for (int x = 0; x < w; x++) {
                for (int i = 0; i < dstNumComp; i++) {
                    dstColor[i] = ((float) (dstLine[idx++] & 0xffff)) * dstDiffMinMax[i] + dstMinVal[i];
                }
                if (needSrcAlpha) {
                    dstColor[dstNumComp] = alpha[x];
                } else if (dstHasAlpha) {
                    dstColor[dstNumComp] = 1.0f;
                }
                pixel = dstCM.getDataElements(dstColor, 0, pixel);
                dstRas.setDataElements(x, y, pixel);
            }
        }
    }
}
Also used : ColorSpace(java.awt.color.ColorSpace) ImageLayoutException(sun.java2d.cmm.lcms.LCMSImageLayout.ImageLayoutException) Raster(java.awt.image.Raster) WritableRaster(java.awt.image.WritableRaster) CMMException(java.awt.color.CMMException) WritableRaster(java.awt.image.WritableRaster) DirectColorModel(java.awt.image.DirectColorModel) ComponentColorModel(java.awt.image.ComponentColorModel) ColorModel(java.awt.image.ColorModel)

Example 54 with ColorSpace

use of java.awt.color.ColorSpace in project jdk8u_jdk by JetBrains.

the class BMPImageReader method readHeader.

/**
     * Process the image header.
     *
     * @exception IllegalStateException if source stream is not set.
     *
     * @exception IOException if image stream is corrupted.
     *
     * @exception IllegalArgumentException if the image stream does not contain
     *             a BMP image, or if a sample model instance to describe the
     *             image can not be created.
     */
protected void readHeader() throws IOException, IllegalArgumentException {
    if (gotHeader)
        return;
    if (iis == null) {
        throw new IllegalStateException("Input source not set!");
    }
    int profileData = 0, profileSize = 0;
    this.metadata = new BMPMetadata();
    iis.mark();
    // read and check the magic marker
    byte[] marker = new byte[2];
    iis.read(marker);
    if (marker[0] != 0x42 || marker[1] != 0x4d)
        throw new IllegalArgumentException(I18N.getString("BMPImageReader1"));
    // Read file size
    bitmapFileSize = iis.readUnsignedInt();
    // skip the two reserved fields
    iis.skipBytes(4);
    // Offset to the bitmap from the beginning
    bitmapOffset = iis.readUnsignedInt();
    // End File Header
    // Start BitmapCoreHeader
    long size = iis.readUnsignedInt();
    if (size == 12) {
        width = iis.readShort();
        height = iis.readShort();
    } else {
        width = iis.readInt();
        height = iis.readInt();
    }
    metadata.width = width;
    metadata.height = height;
    int planes = iis.readUnsignedShort();
    bitsPerPixel = iis.readUnsignedShort();
    //metadata.colorPlane = planes;
    metadata.bitsPerPixel = (short) bitsPerPixel;
    // As BMP always has 3 rgb bands, except for Version 5,
    // which is bgra
    numBands = 3;
    if (size == 12) {
        // Windows 2.x and OS/2 1.x
        metadata.bmpVersion = VERSION_2;
        // Classify the image type
        if (bitsPerPixel == 1) {
            imageType = VERSION_2_1_BIT;
        } else if (bitsPerPixel == 4) {
            imageType = VERSION_2_4_BIT;
        } else if (bitsPerPixel == 8) {
            imageType = VERSION_2_8_BIT;
        } else if (bitsPerPixel == 24) {
            imageType = VERSION_2_24_BIT;
        }
        // Read in the palette
        int numberOfEntries = (int) ((bitmapOffset - 14 - size) / 3);
        int sizeOfPalette = numberOfEntries * 3;
        palette = new byte[sizeOfPalette];
        iis.readFully(palette, 0, sizeOfPalette);
        metadata.palette = palette;
        metadata.paletteSize = numberOfEntries;
    } else {
        compression = iis.readUnsignedInt();
        imageSize = iis.readUnsignedInt();
        long xPelsPerMeter = iis.readInt();
        long yPelsPerMeter = iis.readInt();
        long colorsUsed = iis.readUnsignedInt();
        long colorsImportant = iis.readUnsignedInt();
        metadata.compression = (int) compression;
        metadata.xPixelsPerMeter = (int) xPelsPerMeter;
        metadata.yPixelsPerMeter = (int) yPelsPerMeter;
        metadata.colorsUsed = (int) colorsUsed;
        metadata.colorsImportant = (int) colorsImportant;
        if (size == 40) {
            // Windows 3.x and Windows NT
            switch((int) compression) {
                case BI_JPEG:
                case BI_PNG:
                    metadata.bmpVersion = VERSION_3;
                    imageType = VERSION_3_XP_EMBEDDED;
                    break;
                // No compression
                case BI_RGB:
                // 8-bit RLE compression
                case BI_RLE8:
                case // 4-bit RLE compression
                BI_RLE4:
                    // Read in the palette
                    if (bitmapOffset < (size + 14)) {
                        throw new IIOException(I18N.getString("BMPImageReader7"));
                    }
                    int numberOfEntries = (int) ((bitmapOffset - 14 - size) / 4);
                    int sizeOfPalette = numberOfEntries * 4;
                    palette = new byte[sizeOfPalette];
                    iis.readFully(palette, 0, sizeOfPalette);
                    metadata.palette = palette;
                    metadata.paletteSize = numberOfEntries;
                    if (bitsPerPixel == 1) {
                        imageType = VERSION_3_1_BIT;
                    } else if (bitsPerPixel == 4) {
                        imageType = VERSION_3_4_BIT;
                    } else if (bitsPerPixel == 8) {
                        imageType = VERSION_3_8_BIT;
                    } else if (bitsPerPixel == 24) {
                        imageType = VERSION_3_24_BIT;
                    } else if (bitsPerPixel == 16) {
                        imageType = VERSION_3_NT_16_BIT;
                        redMask = 0x7C00;
                        greenMask = 0x3E0;
                        // 0x1F;
                        blueMask = (1 << 5) - 1;
                        metadata.redMask = redMask;
                        metadata.greenMask = greenMask;
                        metadata.blueMask = blueMask;
                    } else if (bitsPerPixel == 32) {
                        imageType = VERSION_3_NT_32_BIT;
                        redMask = 0x00FF0000;
                        greenMask = 0x0000FF00;
                        blueMask = 0x000000FF;
                        metadata.redMask = redMask;
                        metadata.greenMask = greenMask;
                        metadata.blueMask = blueMask;
                    }
                    metadata.bmpVersion = VERSION_3;
                    break;
                case BI_BITFIELDS:
                    if (bitsPerPixel == 16) {
                        imageType = VERSION_3_NT_16_BIT;
                    } else if (bitsPerPixel == 32) {
                        imageType = VERSION_3_NT_32_BIT;
                    }
                    // BitsField encoding
                    redMask = (int) iis.readUnsignedInt();
                    greenMask = (int) iis.readUnsignedInt();
                    blueMask = (int) iis.readUnsignedInt();
                    metadata.redMask = redMask;
                    metadata.greenMask = greenMask;
                    metadata.blueMask = blueMask;
                    if (colorsUsed != 0) {
                        // there is a palette
                        sizeOfPalette = (int) colorsUsed * 4;
                        palette = new byte[sizeOfPalette];
                        iis.readFully(palette, 0, sizeOfPalette);
                        metadata.palette = palette;
                        metadata.paletteSize = (int) colorsUsed;
                    }
                    metadata.bmpVersion = VERSION_3_NT;
                    break;
                default:
                    throw new IIOException(I18N.getString("BMPImageReader2"));
            }
        } else if (size == 108 || size == 124) {
            // Windows 4.x BMP
            if (size == 108)
                metadata.bmpVersion = VERSION_4;
            else if (size == 124)
                metadata.bmpVersion = VERSION_5;
            // rgb masks, valid only if comp is BI_BITFIELDS
            redMask = (int) iis.readUnsignedInt();
            greenMask = (int) iis.readUnsignedInt();
            blueMask = (int) iis.readUnsignedInt();
            // Only supported for 32bpp BI_RGB argb
            alphaMask = (int) iis.readUnsignedInt();
            long csType = iis.readUnsignedInt();
            int redX = iis.readInt();
            int redY = iis.readInt();
            int redZ = iis.readInt();
            int greenX = iis.readInt();
            int greenY = iis.readInt();
            int greenZ = iis.readInt();
            int blueX = iis.readInt();
            int blueY = iis.readInt();
            int blueZ = iis.readInt();
            long gammaRed = iis.readUnsignedInt();
            long gammaGreen = iis.readUnsignedInt();
            long gammaBlue = iis.readUnsignedInt();
            if (size == 124) {
                metadata.intent = iis.readInt();
                profileData = iis.readInt();
                profileSize = iis.readInt();
                iis.skipBytes(4);
            }
            metadata.colorSpace = (int) csType;
            if (csType == LCS_CALIBRATED_RGB) {
                // All the new fields are valid only for this case
                metadata.redX = redX;
                metadata.redY = redY;
                metadata.redZ = redZ;
                metadata.greenX = greenX;
                metadata.greenY = greenY;
                metadata.greenZ = greenZ;
                metadata.blueX = blueX;
                metadata.blueY = blueY;
                metadata.blueZ = blueZ;
                metadata.gammaRed = (int) gammaRed;
                metadata.gammaGreen = (int) gammaGreen;
                metadata.gammaBlue = (int) gammaBlue;
            }
            // Read in the palette
            int numberOfEntries = (int) ((bitmapOffset - 14 - size) / 4);
            int sizeOfPalette = numberOfEntries * 4;
            palette = new byte[sizeOfPalette];
            iis.readFully(palette, 0, sizeOfPalette);
            metadata.palette = palette;
            metadata.paletteSize = numberOfEntries;
            switch((int) compression) {
                case BI_JPEG:
                case BI_PNG:
                    if (size == 108) {
                        imageType = VERSION_4_XP_EMBEDDED;
                    } else if (size == 124) {
                        imageType = VERSION_5_XP_EMBEDDED;
                    }
                    break;
                default:
                    if (bitsPerPixel == 1) {
                        imageType = VERSION_4_1_BIT;
                    } else if (bitsPerPixel == 4) {
                        imageType = VERSION_4_4_BIT;
                    } else if (bitsPerPixel == 8) {
                        imageType = VERSION_4_8_BIT;
                    } else if (bitsPerPixel == 16) {
                        imageType = VERSION_4_16_BIT;
                        if ((int) compression == BI_RGB) {
                            redMask = 0x7C00;
                            greenMask = 0x3E0;
                            blueMask = 0x1F;
                        }
                    } else if (bitsPerPixel == 24) {
                        imageType = VERSION_4_24_BIT;
                    } else if (bitsPerPixel == 32) {
                        imageType = VERSION_4_32_BIT;
                        if ((int) compression == BI_RGB) {
                            redMask = 0x00FF0000;
                            greenMask = 0x0000FF00;
                            blueMask = 0x000000FF;
                        }
                    }
                    metadata.redMask = redMask;
                    metadata.greenMask = greenMask;
                    metadata.blueMask = blueMask;
                    metadata.alphaMask = alphaMask;
            }
        } else {
            throw new IIOException(I18N.getString("BMPImageReader3"));
        }
    }
    if (height > 0) {
        // bottom up image
        isBottomUp = true;
    } else {
        // top down image
        isBottomUp = false;
        height = Math.abs(height);
    }
    // Reset Image Layout so there's only one tile.
    //Define the color space
    ColorSpace colorSpace = ColorSpace.getInstance(ColorSpace.CS_sRGB);
    if (metadata.colorSpace == PROFILE_LINKED || metadata.colorSpace == PROFILE_EMBEDDED) {
        iis.mark();
        iis.skipBytes(profileData - size);
        byte[] profile = new byte[profileSize];
        iis.readFully(profile, 0, profileSize);
        iis.reset();
        try {
            if (metadata.colorSpace == PROFILE_LINKED && isLinkedProfileAllowed() && !isUncOrDevicePath(profile)) {
                String path = new String(profile, "windows-1252");
                colorSpace = new ICC_ColorSpace(ICC_Profile.getInstance(path));
            } else {
                colorSpace = new ICC_ColorSpace(ICC_Profile.getInstance(profile));
            }
        } catch (Exception e) {
            colorSpace = ColorSpace.getInstance(ColorSpace.CS_sRGB);
        }
    }
    if (bitsPerPixel == 0 || compression == BI_JPEG || compression == BI_PNG) {
        // the colorModel and sampleModel will be initialzed
        // by the  reader of embedded image
        colorModel = null;
        sampleModel = null;
    } else if (bitsPerPixel == 1 || bitsPerPixel == 4 || bitsPerPixel == 8) {
        // When number of bitsPerPixel is <= 8, we use IndexColorModel.
        numBands = 1;
        if (bitsPerPixel == 8) {
            int[] bandOffsets = new int[numBands];
            for (int i = 0; i < numBands; i++) {
                bandOffsets[i] = numBands - 1 - i;
            }
            sampleModel = new PixelInterleavedSampleModel(DataBuffer.TYPE_BYTE, width, height, numBands, numBands * width, bandOffsets);
        } else {
            // 1 and 4 bit pixels can be stored in a packed format.
            sampleModel = new MultiPixelPackedSampleModel(DataBuffer.TYPE_BYTE, width, height, bitsPerPixel);
        }
        // Create IndexColorModel from the palette.
        byte[] r, g, b;
        if (imageType == VERSION_2_1_BIT || imageType == VERSION_2_4_BIT || imageType == VERSION_2_8_BIT) {
            size = palette.length / 3;
            if (size > 256) {
                size = 256;
            }
            int off;
            r = new byte[(int) size];
            g = new byte[(int) size];
            b = new byte[(int) size];
            for (int i = 0; i < (int) size; i++) {
                off = 3 * i;
                b[i] = palette[off];
                g[i] = palette[off + 1];
                r[i] = palette[off + 2];
            }
        } else {
            size = palette.length / 4;
            if (size > 256) {
                size = 256;
            }
            int off;
            r = new byte[(int) size];
            g = new byte[(int) size];
            b = new byte[(int) size];
            for (int i = 0; i < size; i++) {
                off = 4 * i;
                b[i] = palette[off];
                g[i] = palette[off + 1];
                r[i] = palette[off + 2];
            }
        }
        if (ImageUtil.isIndicesForGrayscale(r, g, b))
            colorModel = ImageUtil.createColorModel(null, sampleModel);
        else
            colorModel = new IndexColorModel(bitsPerPixel, (int) size, r, g, b);
    } else if (bitsPerPixel == 16) {
        numBands = 3;
        sampleModel = new SinglePixelPackedSampleModel(DataBuffer.TYPE_USHORT, width, height, new int[] { redMask, greenMask, blueMask });
        colorModel = new DirectColorModel(colorSpace, 16, redMask, greenMask, blueMask, 0, false, DataBuffer.TYPE_USHORT);
    } else if (bitsPerPixel == 32) {
        numBands = alphaMask == 0 ? 3 : 4;
        // The number of bands in the SampleModel is determined by
        // the length of the mask array passed in.
        int[] bitMasks = numBands == 3 ? new int[] { redMask, greenMask, blueMask } : new int[] { redMask, greenMask, blueMask, alphaMask };
        sampleModel = new SinglePixelPackedSampleModel(DataBuffer.TYPE_INT, width, height, bitMasks);
        colorModel = new DirectColorModel(colorSpace, 32, redMask, greenMask, blueMask, alphaMask, false, DataBuffer.TYPE_INT);
    } else {
        numBands = 3;
        // Create SampleModel
        int[] bandOffsets = new int[numBands];
        for (int i = 0; i < numBands; i++) {
            bandOffsets[i] = numBands - 1 - i;
        }
        sampleModel = new PixelInterleavedSampleModel(DataBuffer.TYPE_BYTE, width, height, numBands, numBands * width, bandOffsets);
        colorModel = ImageUtil.createColorModel(colorSpace, sampleModel);
    }
    originalSampleModel = sampleModel;
    originalColorModel = colorModel;
    // Reset to the start of bitmap; then jump to the
    //start of image data
    iis.reset();
    iis.skipBytes(bitmapOffset);
    gotHeader = true;
}
Also used : PixelInterleavedSampleModel(java.awt.image.PixelInterleavedSampleModel) ColorSpace(java.awt.color.ColorSpace) ICC_ColorSpace(java.awt.color.ICC_ColorSpace) SinglePixelPackedSampleModel(java.awt.image.SinglePixelPackedSampleModel) MultiPixelPackedSampleModel(java.awt.image.MultiPixelPackedSampleModel) IIOException(javax.imageio.IIOException) Point(java.awt.Point) IIOException(javax.imageio.IIOException) ICC_ColorSpace(java.awt.color.ICC_ColorSpace) DirectColorModel(java.awt.image.DirectColorModel) IndexColorModel(java.awt.image.IndexColorModel)

Example 55 with ColorSpace

use of java.awt.color.ColorSpace in project frostwire by frostwire.

the class CMYKJPEGImageReader method createRGBImageFromInvertedYCCK.

/**
 * Creates a buffered image from a raster in the inverted YCCK color space,
 * converting the colors to RGB using the provided CMYK ICC_Profile.
 *
 * @param ycckRaster A raster with (at least) 4 bands of samples.
 * @param cmykProfile An ICC_Profile for conversion from the CMYK color space
 * to the RGB color space. If this parameter is null, a default profile is used.
 * @return a BufferedImage in the RGB color space.
 */
public static BufferedImage createRGBImageFromInvertedYCCK(Raster ycckRaster, ICC_Profile cmykProfile) {
    BufferedImage image;
    if (cmykProfile != null) {
        ycckRaster = convertInvertedYCCKToCMYK(ycckRaster);
        image = createRGBImageFromCMYK(ycckRaster, cmykProfile);
    } else {
        int w = ycckRaster.getWidth(), h = ycckRaster.getHeight();
        int[] rgb = new int[w * h];
        // PixelInterleavedSampleModel pix;
        // if (Adobe_APP14 and transform==2) then YCCK else CMYK
        int[] Y = ycckRaster.getSamples(0, 0, w, h, 0, (int[]) null);
        int[] Cb = ycckRaster.getSamples(0, 0, w, h, 1, (int[]) null);
        int[] Cr = ycckRaster.getSamples(0, 0, w, h, 2, (int[]) null);
        int[] K = ycckRaster.getSamples(0, 0, w, h, 3, (int[]) null);
        float vr, vg, vb;
        for (int i = 0, imax = Y.length; i < imax; i++) {
            float k = 255 - K[i], y = 255 - Y[i], cb = 255 - Cb[i], cr = 255 - Cr[i];
            vr = y + 1.402f * (cr - 128) - k;
            vg = y - 0.34414f * (cb - 128) - 0.71414f * (cr - 128) - k;
            vb = y + 1.772f * (cb - 128) - k;
            rgb[i] = (0xff & (vr < 0.0f ? 0 : vr > 255.0f ? 0xff : (int) (vr + 0.5f))) << 16 | (0xff & (vg < 0.0f ? 0 : vg > 255.0f ? 0xff : (int) (vg + 0.5f))) << 8 | (0xff & (vb < 0.0f ? 0 : vb > 255.0f ? 0xff : (int) (vb + 0.5f)));
        }
        Raster rgbRaster = Raster.createPackedRaster(new DataBufferInt(rgb, rgb.length), w, h, w, new int[] { 0xff0000, 0xff00, 0xff }, null);
        ColorSpace cs = ColorSpace.getInstance(ColorSpace.CS_sRGB);
        ColorModel cm = new DirectColorModel(cs, 24, 0xff0000, 0xff00, 0xff, 0x0, false, DataBuffer.TYPE_INT);
        image = new BufferedImage(cm, (WritableRaster) rgbRaster, true, null);
    }
    return image;
}
Also used : ColorSpace(java.awt.color.ColorSpace) ICC_ColorSpace(java.awt.color.ICC_ColorSpace)

Aggregations

ColorSpace (java.awt.color.ColorSpace)86 ColorModel (java.awt.image.ColorModel)33 BufferedImage (java.awt.image.BufferedImage)30 ComponentColorModel (java.awt.image.ComponentColorModel)29 ICC_ColorSpace (java.awt.color.ICC_ColorSpace)24 IndexColorModel (java.awt.image.IndexColorModel)17 SampleModel (java.awt.image.SampleModel)17 WritableRaster (java.awt.image.WritableRaster)13 ImageTypeSpecifier (javax.imageio.ImageTypeSpecifier)11 ColorConvertOp (java.awt.image.ColorConvertOp)10 DirectColorModel (java.awt.image.DirectColorModel)10 Point (java.awt.Point)8 DataBuffer (java.awt.image.DataBuffer)8 MultiPixelPackedSampleModel (java.awt.image.MultiPixelPackedSampleModel)8 DataBufferByte (java.awt.image.DataBufferByte)6 SinglePixelPackedSampleModel (java.awt.image.SinglePixelPackedSampleModel)6 IIOException (javax.imageio.IIOException)5 Test (org.junit.Test)5 Rectangle (java.awt.Rectangle)4 ComponentSampleModel (java.awt.image.ComponentSampleModel)4