Search in sources :

Example 41 with ColorSpace

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

the class TestCompressionBI_BITFIELDS method compareImages.

protected void compareImages(BufferedImage src, BufferedImage dst) {
    ColorSpace srcCS = src.getColorModel().getColorSpace();
    ColorSpace dstCS = dst.getColorModel().getColorSpace();
    if (!srcCS.equals(dstCS) && srcCS.getType() == ColorSpace.TYPE_GRAY) {
        System.out.println("Workaround color difference with GRAY.");
        BufferedImage tmp = new BufferedImage(src.getWidth(), src.getHeight(), BufferedImage.TYPE_INT_RGB);
        Graphics g = tmp.createGraphics();
        g.drawImage(src, 0, 0, null);
        src = tmp;
    }
    int y = h / 2;
    for (int i = 0; i < colors.length; i++) {
        int x = dx * i + dx / 2;
        int srcRgb = src.getRGB(x, y);
        int dstRgb = dst.getRGB(x, y);
        if (srcRgb != dstRgb) {
            throw new RuntimeException("Test failed due to color difference: " + "src_pixel=" + Integer.toHexString(srcRgb) + "dst_pixel=" + Integer.toHexString(dstRgb));
        }
    }
}
Also used : Graphics(java.awt.Graphics) ColorSpace(java.awt.color.ColorSpace) BufferedImage(java.awt.image.BufferedImage)

Example 42 with ColorSpace

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

the class AlphaTest method main.

public static void main(String[] args) {
    ColorSpace cs = ColorSpace.getInstance(ColorSpace.CS_GRAY);
    ColorConvertOp op = new ColorConvertOp(cs, null);
    // create source image filled with an opaque color
    BufferedImage src = createSrc();
    int srcAlpha = getAlpha(src);
    System.out.printf("Src alpha: 0x%02x\n", srcAlpha);
    // create clear (transparent black) destination image
    BufferedImage dst = createDst();
    int dstAlpha = getAlpha(dst);
    System.out.printf("Dst alpha: 0x%02x\n", dstAlpha);
    dst = op.filter(src, dst);
    dstAlpha = getAlpha(dst);
    // we expect that destination image is opaque
    // i.e. alpha is transferred from source to
    // the destination
    System.out.printf("Result alpha: 0x%02x\n", dstAlpha);
    if (srcAlpha != dstAlpha) {
        throw new RuntimeException("Test failed!");
    }
    System.out.println("Test passed");
}
Also used : ColorConvertOp(java.awt.image.ColorConvertOp) ColorSpace(java.awt.color.ColorSpace) BufferedImage(java.awt.image.BufferedImage)

Example 43 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 44 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 45 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)

Aggregations

ColorSpace (java.awt.color.ColorSpace)74 BufferedImage (java.awt.image.BufferedImage)28 ColorModel (java.awt.image.ColorModel)28 ComponentColorModel (java.awt.image.ComponentColorModel)25 ICC_ColorSpace (java.awt.color.ICC_ColorSpace)22 SampleModel (java.awt.image.SampleModel)15 IndexColorModel (java.awt.image.IndexColorModel)13 WritableRaster (java.awt.image.WritableRaster)12 ImageTypeSpecifier (javax.imageio.ImageTypeSpecifier)11 ColorConvertOp (java.awt.image.ColorConvertOp)8 DataBuffer (java.awt.image.DataBuffer)8 DirectColorModel (java.awt.image.DirectColorModel)8 Point (java.awt.Point)7 DataBufferByte (java.awt.image.DataBufferByte)6 MultiPixelPackedSampleModel (java.awt.image.MultiPixelPackedSampleModel)6 IIOException (javax.imageio.IIOException)5 Rectangle (java.awt.Rectangle)4 ComponentSampleModel (java.awt.image.ComponentSampleModel)4 PixelInterleavedSampleModel (java.awt.image.PixelInterleavedSampleModel)4 SinglePixelPackedSampleModel (java.awt.image.SinglePixelPackedSampleModel)4