Search in sources :

Example 26 with ICC_ColorSpace

use of java.awt.color.ICC_ColorSpace in project acs-aem-commons by Adobe-Consulting-Services.

the class CMYKJPEGImageReader method createRGBAImageFromRGBA.

/**
 * Creates a buffered image from a raster in the RGBA color space, converting
 * the colors to RGB using the provided CMYK ICC_Profile.
 *
 * As seen from a comment made by 'phelps' at
 * http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4799903
 *
 * @param rgbaRaster A raster with (at least) 4 bands of samples.
 * @param rgbaProfile 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 createRGBAImageFromRGBA(Raster rgbaRaster, ICC_Profile rgbaProfile) {
    BufferedImage image;
    int w = rgbaRaster.getWidth();
    int h = rgbaRaster.getHeight();
    // ICC_Profile currently not supported
    rgbaProfile = null;
    if (rgbaProfile != null) {
        ColorSpace rgbaCS = new ICC_ColorSpace(rgbaProfile);
        image = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
        WritableRaster rgbRaster = image.getRaster();
        ColorSpace rgbCS = image.getColorModel().getColorSpace();
        ColorConvertOp cmykToRgb = new ColorConvertOp(rgbaCS, rgbCS, null);
        cmykToRgb.filter(rgbaRaster, rgbRaster);
    } else {
        int[] rgb = new int[w * h];
        int[] R = rgbaRaster.getSamples(0, 0, w, h, 0, (int[]) null);
        int[] G = rgbaRaster.getSamples(0, 0, w, h, 1, (int[]) null);
        int[] B = rgbaRaster.getSamples(0, 0, w, h, 2, (int[]) null);
        int[] A = rgbaRaster.getSamples(0, 0, w, h, 3, (int[]) null);
        for (int i = 0, imax = R.length; i < imax; i++) {
            rgb[i] = A[i] << 24 | R[i] << 16 | G[i] << 8 | B[i];
        }
        Raster rgbRaster = Raster.createPackedRaster(new DataBufferInt(rgb, rgb.length), w, h, w, new int[] { 0xff0000, 0xff00, 0xff, 0xff000000 }, null);
        ColorSpace cs = ColorSpace.getInstance(ColorSpace.CS_sRGB);
        ColorModel cm = new DirectColorModel(cs, 32, 0xff0000, 0xff00, 0xff, 0x0ff000000, false, DataBuffer.TYPE_INT);
        image = new BufferedImage(cm, (WritableRaster) rgbRaster, true, null);
    }
    return image;
}
Also used : ICC_ColorSpace(java.awt.color.ICC_ColorSpace) ColorSpace(java.awt.color.ColorSpace) ICC_ColorSpace(java.awt.color.ICC_ColorSpace)

Example 27 with ICC_ColorSpace

use of java.awt.color.ICC_ColorSpace in project pdfbox by apache.

the class PNGConverterTest method getImageWithProfileData.

public static BufferedImage getImageWithProfileData(BufferedImage sourceImage, ICC_Profile realProfile) {
    Hashtable<String, Object> properties = new Hashtable<>();
    String[] propertyNames = sourceImage.getPropertyNames();
    if (propertyNames != null) {
        for (String propertyName : propertyNames) {
            properties.put(propertyName, sourceImage.getProperty(propertyName));
        }
    }
    ComponentColorModel oldColorModel = (ComponentColorModel) sourceImage.getColorModel();
    boolean hasAlpha = oldColorModel.hasAlpha();
    int transparency = oldColorModel.getTransparency();
    boolean alphaPremultiplied = oldColorModel.isAlphaPremultiplied();
    WritableRaster raster = sourceImage.getRaster();
    int dataType = raster.getDataBuffer().getDataType();
    int[] componentSize = oldColorModel.getComponentSize();
    final ColorModel colorModel = new ComponentColorModel(new ICC_ColorSpace(realProfile), componentSize, hasAlpha, alphaPremultiplied, transparency, dataType);
    return new BufferedImage(colorModel, raster, sourceImage.isAlphaPremultiplied(), properties);
}
Also used : Hashtable(java.util.Hashtable) ComponentColorModel(java.awt.image.ComponentColorModel) BufferedImage(java.awt.image.BufferedImage) ICC_ColorSpace(java.awt.color.ICC_ColorSpace) WritableRaster(java.awt.image.WritableRaster) ComponentColorModel(java.awt.image.ComponentColorModel) ColorModel(java.awt.image.ColorModel)

Example 28 with ICC_ColorSpace

use of java.awt.color.ICC_ColorSpace in project pdfbox by apache.

the class LosslessFactoryTest method testCreateLosslessFromImageCMYK.

/**
 * Test lossless encoding of CMYK images
 */
@Test
void testCreateLosslessFromImageCMYK() throws IOException {
    PDDocument document = new PDDocument();
    BufferedImage image = ImageIO.read(this.getClass().getResourceAsStream("png.png"));
    final ColorSpace targetCS = new ICC_ColorSpace(ICC_Profile.getInstance(this.getClass().getResourceAsStream("/org/apache/pdfbox/resources/icc/ISOcoated_v2_300_bas.icc")));
    ColorConvertOp op = new ColorConvertOp(image.getColorModel().getColorSpace(), targetCS, null);
    BufferedImage imageCMYK = op.filter(image, null);
    PDImageXObject ximage = LosslessFactory.createFromImage(document, imageCMYK);
    validate(ximage, 8, imageCMYK.getWidth(), imageCMYK.getHeight(), "png", "ICCBased");
    doWritePDF(document, ximage, TESTRESULTSDIR, "cmyk.pdf");
// still slight difference of 1 color level
// checkIdent(imageCMYK, ximage.getImage());
}
Also used : ICC_ColorSpace(java.awt.color.ICC_ColorSpace) ColorConvertOp(java.awt.image.ColorConvertOp) ColorSpace(java.awt.color.ColorSpace) ICC_ColorSpace(java.awt.color.ICC_ColorSpace) PDDocument(org.apache.pdfbox.pdmodel.PDDocument) BufferedImage(java.awt.image.BufferedImage) Test(org.junit.jupiter.api.Test)

Aggregations

ICC_ColorSpace (java.awt.color.ICC_ColorSpace)28 ColorSpace (java.awt.color.ColorSpace)10 ICC_Profile (java.awt.color.ICC_Profile)10 WeakHashMap (java.util.WeakHashMap)8 ColorTransform (sun.java2d.cmm.ColorTransform)8 PCMM (sun.java2d.cmm.PCMM)8 BufferedImage (java.awt.image.BufferedImage)5 IndexColorModel (java.awt.image.IndexColorModel)4 IOException (java.io.IOException)4 CMMException (java.awt.color.CMMException)3 ColorConvertOp (java.awt.image.ColorConvertOp)3 ComponentColorModel (java.awt.image.ComponentColorModel)3 InputStream (java.io.InputStream)3 IIOException (javax.imageio.IIOException)3 TIFFField (it.geosolutions.imageio.plugins.tiff.TIFFField)2 Point (java.awt.Point)2 ColorModel (java.awt.image.ColorModel)2 ImageTypeSpecifier (javax.imageio.ImageTypeSpecifier)2 IIOMetadata (javax.imageio.metadata.IIOMetadata)2 PDDocument (org.apache.pdfbox.pdmodel.PDDocument)2