Search in sources :

Example 6 with CMMException

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

the class CMSManager method getModule.

public static synchronized PCMM getModule() {
    if (cmmImpl != null) {
        return cmmImpl;
    }
    CMMServiceProvider spi = AccessController.doPrivileged(new PrivilegedAction<CMMServiceProvider>() {

        public CMMServiceProvider run() {
            String cmmClass = System.getProperty("sun.java2d.cmm", "sun.java2d.cmm.lcms.LcmsServiceProvider");
            ServiceLoader<CMMServiceProvider> cmmLoader = ServiceLoader.loadInstalled(CMMServiceProvider.class);
            CMMServiceProvider spi = null;
            for (CMMServiceProvider cmm : cmmLoader) {
                spi = cmm;
                if (cmm.getClass().getName().equals(cmmClass)) {
                    break;
                }
            }
            return spi;
        }
    });
    cmmImpl = spi.getColorManagementModule();
    if (cmmImpl == null) {
        throw new CMMException("Cannot initialize Color Management System." + "No CM module found");
    }
    GetPropertyAction gpa = new GetPropertyAction("sun.java2d.cmm.trace");
    String cmmTrace = (String) AccessController.doPrivileged(gpa);
    if (cmmTrace != null) {
        cmmImpl = new CMMTracer(cmmImpl);
    }
    return cmmImpl;
}
Also used : ServiceLoader(java.util.ServiceLoader) GetPropertyAction(sun.security.action.GetPropertyAction) CMMException(java.awt.color.CMMException)

Example 7 with CMMException

use of java.awt.color.CMMException 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 8 with CMMException

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

the class LCMSTransform method colorConvert.

public void colorConvert(Raster src, WritableRaster dst) {
    LCMSImageLayout srcIL, dstIL;
    dstIL = LCMSImageLayout.createImageLayout(dst);
    if (dstIL != null) {
        srcIL = LCMSImageLayout.createImageLayout(src);
        if (srcIL != null) {
            doTransform(srcIL, dstIL);
            return;
        }
    }
    // Can't pass src and dst directly to CMM, so process per scanline
    SampleModel srcSM = src.getSampleModel();
    SampleModel dstSM = dst.getSampleModel();
    int srcTransferType = src.getTransferType();
    int dstTransferType = dst.getTransferType();
    int w = src.getWidth();
    int h = src.getHeight();
    int srcNumBands = src.getNumBands();
    int dstNumBands = dst.getNumBands();
    int precision = 8;
    float maxNum = 255.0f;
    for (int i = 0; i < srcNumBands; i++) {
        if (srcSM.getSampleSize(i) > 8) {
            precision = 16;
            maxNum = 65535.0f;
        }
    }
    for (int i = 0; i < dstNumBands; i++) {
        if (dstSM.getSampleSize(i) > 8) {
            precision = 16;
            maxNum = 65535.0f;
        }
    }
    float[] srcScaleFactor = new float[srcNumBands];
    float[] dstScaleFactor = new float[dstNumBands];
    for (int i = 0; i < srcNumBands; i++) {
        if (srcTransferType == DataBuffer.TYPE_SHORT) {
            srcScaleFactor[i] = maxNum / 32767.0f;
        } else {
            srcScaleFactor[i] = maxNum / ((float) ((1 << srcSM.getSampleSize(i)) - 1));
        }
    }
    for (int i = 0; i < dstNumBands; i++) {
        if (dstTransferType == DataBuffer.TYPE_SHORT) {
            dstScaleFactor[i] = 32767.0f / maxNum;
        } else {
            dstScaleFactor[i] = ((float) ((1 << dstSM.getSampleSize(i)) - 1)) / maxNum;
        }
    }
    int ys = src.getMinY();
    int yd = dst.getMinY();
    int xs, xd;
    int sample;
    if (precision == 8) {
        byte[] srcLine = new byte[w * srcNumBands];
        byte[] dstLine = new byte[w * dstNumBands];
        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 rasters");
        }
        // process each scanline
        for (int y = 0; y < h; y++, ys++, yd++) {
            // get src scanline
            xs = src.getMinX();
            idx = 0;
            for (int x = 0; x < w; x++, xs++) {
                for (int i = 0; i < srcNumBands; i++) {
                    sample = src.getSample(xs, ys, i);
                    srcLine[idx++] = (byte) ((sample * srcScaleFactor[i]) + 0.5f);
                }
            }
            // color convert srcLine to dstLine
            doTransform(srcIL, dstIL);
            // store dst scanline
            xd = dst.getMinX();
            idx = 0;
            for (int x = 0; x < w; x++, xd++) {
                for (int i = 0; i < dstNumBands; i++) {
                    sample = (int) (((dstLine[idx++] & 0xff) * dstScaleFactor[i]) + 0.5f);
                    dst.setSample(xd, yd, i, sample);
                }
            }
        }
    } else {
        short[] srcLine = new short[w * srcNumBands];
        short[] dstLine = new short[w * dstNumBands];
        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 rasters");
        }
        // process each scanline
        for (int y = 0; y < h; y++, ys++, yd++) {
            // get src scanline
            xs = src.getMinX();
            idx = 0;
            for (int x = 0; x < w; x++, xs++) {
                for (int i = 0; i < srcNumBands; i++) {
                    sample = src.getSample(xs, ys, i);
                    srcLine[idx++] = (short) ((sample * srcScaleFactor[i]) + 0.5f);
                }
            }
            // color convert srcLine to dstLine
            doTransform(srcIL, dstIL);
            // store dst scanline
            xd = dst.getMinX();
            idx = 0;
            for (int x = 0; x < w; x++, xd++) {
                for (int i = 0; i < dstNumBands; i++) {
                    sample = (int) (((dstLine[idx++] & 0xffff) * dstScaleFactor[i]) + 0.5f);
                    dst.setSample(xd, yd, i, sample);
                }
            }
        }
    }
}
Also used : ComponentSampleModel(java.awt.image.ComponentSampleModel) SampleModel(java.awt.image.SampleModel) SinglePixelPackedSampleModel(java.awt.image.SinglePixelPackedSampleModel) ImageLayoutException(sun.java2d.cmm.lcms.LCMSImageLayout.ImageLayoutException) CMMException(java.awt.color.CMMException)

Example 9 with CMMException

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

the class InvalidRenderIntentTest method main.

public static void main(String[] args) {
    ICC_Profile pSRGB = ICC_Profile.getInstance(CS_sRGB);
    byte[] raw_data = pSRGB.getData();
    setRenderingIntent(0x1000000, raw_data);
    ICC_Profile p = ICC_Profile.getInstance(raw_data);
    ICC_ColorSpace cs = new ICC_ColorSpace(p);
    // perfrom test color conversion
    ColorConvertOp op = new ColorConvertOp(cs, ColorSpace.getInstance(CS_sRGB), null);
    BufferedImage src = new BufferedImage(1, 1, TYPE_3BYTE_BGR);
    BufferedImage dst = new BufferedImage(1, 1, TYPE_3BYTE_BGR);
    try {
        op.filter(src.getRaster(), dst.getRaster());
    } catch (CMMException e) {
        throw new RuntimeException("Test failed.", e);
    }
    System.out.println("Test passed.");
}
Also used : ICC_ColorSpace(java.awt.color.ICC_ColorSpace) ColorConvertOp(java.awt.image.ColorConvertOp) ICC_Profile(java.awt.color.ICC_Profile) CMMException(java.awt.color.CMMException) BufferedImage(java.awt.image.BufferedImage)

Aggregations

CMMException (java.awt.color.CMMException)9 ImageLayoutException (sun.java2d.cmm.lcms.LCMSImageLayout.ImageLayoutException)5 ICC_ColorSpace (java.awt.color.ICC_ColorSpace)2 ICC_Profile (java.awt.color.ICC_Profile)2 BufferedImage (java.awt.image.BufferedImage)2 ComponentSampleModel (java.awt.image.ComponentSampleModel)2 SampleModel (java.awt.image.SampleModel)2 SinglePixelPackedSampleModel (java.awt.image.SinglePixelPackedSampleModel)2 ColorSpace (java.awt.color.ColorSpace)1 ColorConvertOp (java.awt.image.ColorConvertOp)1 ColorModel (java.awt.image.ColorModel)1 ComponentColorModel (java.awt.image.ComponentColorModel)1 DirectColorModel (java.awt.image.DirectColorModel)1 Raster (java.awt.image.Raster)1 WritableRaster (java.awt.image.WritableRaster)1 IOException (java.io.IOException)1 ServiceLoader (java.util.ServiceLoader)1 GetPropertyAction (sun.security.action.GetPropertyAction)1