Question

I am attempting to write a BufferedImage to png file using ImageIO, but every time I add an ICC_Profile to the IIOMetadata the resulting png image has no ICC_Profile embedded, and running ImageMagick identify on the resulting png file yields an error.

What I need is working code that adds the ICC_Profile. I admit that I am a bit of a novice with ImageIO. My thinking is that I just add the profile to the header and whatever is used to render the image should find the profile and display the image in the associated colorspace (if supported). Am I missing something else that I should be doing to this image?

Also - I would like to be clear that I want to use only ImageIO and not ColorConvertOp from JAI because JAI hasn't been developed in 7 years and the best way to do this is with standard JDK classes, in my opinion. Is this possible and/or has anybody else been able to achieve it?

My Code:

Iterator i = ImageIO.getImageWritersByFormatName("PNG");
    if (i.hasNext()) {
        ImageWriter imageWriter = (ImageWriter) i.next();
        ImageWriteParam param = imageWriter.getDefaultWriteParam();
        ImageTypeSpecifier its = new ImageTypeSpecifier(bi.getColorModel(), bi.getSampleModel());
        IIOMetadata iomd = imageWriter.getDefaultImageMetadata(its, param);
        String formatName = "javax_imageio_png_1.0";
        Node node = iomd.getAsTree(formatName);

        //add resolution
        IIOMetadataNode phys = new IIOMetadataNode("pHYs");
        phys.setAttribute("unitSpecifier", "unknown");
        phys.setAttribute("pixelsPerUnitXAxis", "300");
        phys.setAttribute("pixelsPerUnitYAxis", "300");

        //add ICC PROFILE
        IIOMetadataNode iccp = new IIOMetadataNode("iCCP");
        //ICC_Profile pro = Util.getAdobeRGBICCProfile();
        iccp.setUserObject(parameters.getOriginalICC());
        iccp.setAttribute("profileName", "AdobeRGB1998");
        iccp.setAttribute("compressionMethod", "deflate");

        node.appendChild(phys);
        node.appendChild(iccp);

        try {
            iomd.setFromTree(formatName, node);
        } catch (IIOInvalidTreeException e) {
            System.out.println("IIOInvalidTreeException Occurred setting resolution on PNG EXIF Header.");
            e.printStackTrace(System.out);
        }

        IIOImage iioimage = new IIOImage(bi, null, iomd);
        try {
            imageWriter.setOutput(new FileImageOutputStream(new File(fileName)));
            imageWriter.write(iioimage);
        } catch (IOException e) {
            System.out.println("Error Occurred setting resolution on PNG.");
            e.printStackTrace(System.out);
        }
    }

Image Magick identify output:

Image: /adobe.png
Format: PNG (Portable Network Graphics)
Class: DirectClass
Geometry: 399x508+0+0
Resolution: 300x300
Print size: 1.33x1.69333
Units: Undefined
Type: TrueColorAlpha
Endianess: Undefined
Colorspace: sRGB
Depth: 8-bit
Channel depth:
red: 8-bit
green: 8-bit
blue: 8-bit
alpha: 1-bit
Channel statistics:
Red:
  min: 0 (0)
  max: 255 (1)
  mean: 34.4178 (0.134972)
  standard deviation: 82.6482 (0.324111)
  kurtosis: 2.12664
  skewness: 2.01838
Green:
  min: 0 (0)
  max: 252 (0.988235)
  mean: 27.8842 (0.10935)
  standard deviation: 71.8833 (0.281895)
  kurtosis: 4.11874
  skewness: 2.41137
Blue:
  min: 0 (0)
  max: 254 (0.996078)
  mean: 28.1182 (0.110267)
  standard deviation: 73.4184 (0.287915)
  kurtosis: 4.25472
  skewness: 2.44667
Alpha:
  min: 0 (0)
  max: 255 (1)
  mean: 38.5321 (0.151106)
  standard deviation: 91.3288 (0.358152)
  kurtosis: 1.79587
  skewness: -1.9483
Image statistics:
Overall:
  min: 0 (0)
  max: 255 (1)
  mean: 76.722 (0.300871)
  standard deviation: 80.2016 (0.314516)
  kurtosis: 4.15993
  skewness: 2.43672
Alpha: none   #00000000
Rendering intent: Perceptual
Gamma: 0.454545
Chromaticity:
red primary: (0.64,0.33)
green primary: (0.3,0.6)
blue primary: (0.15,0.06)
white point: (0.3127,0.329)
Background color: white
Border color: srgba(223,223,223,1)
Matte color: grey74
Transparent color: none
Interlace: None
Intensity: Undefined
Compose: Over
Page geometry: 399x508+0+0
Dispose: Undefined
Iterations: 0
Compression: Zip
Orientation: Undefined
Properties:
date:create: 2013-12-30T10:23:05-06:00
date:modify: 2013-12-30T10:23:05-06:00
png:IHDR.bit-depth-orig: 8
png:IHDR.bit_depth: 8
png:IHDR.color-type-orig: 6
png:IHDR.color_type: 6 (RGBA)
png:IHDR.interlace_method: 0 (Not interlaced)
png:IHDR.width,height: 399, 508
png:pHYs: x_res=300, y_res=300, units=0
png:sRGB: intent=0 (Perceptual Intent)
signature: f03e01954623f646c9b594d0b2234ca444bb5c8282e923c8af520e08753fed0d
Artifacts:
filename: /adobe.png
verbose: true
Tainted: False
Filesize: 68.3KB
Number pixels: 203K
Pixels per second: 20.27MB
User time: 0.000u
Elapsed time: 0:01.009
Version: ImageMagick 6.8.6-6 2013-10-12 Q16 http://www.imagemagick.org
identify: iCCP: Data error in compressed datastream `/adobe.png' @ warning/png.c   /MagickPNGWarningHandler/1830.
identify: Profile size field missing from iCCP chunk `/adobe.png' @ warning/png.c/MagickPNGWarningHandler/1830.

Any help would be much appreciated. Thank You.

Was it helpful?

Solution

The reason I was asking, is I don't think your ICC profile is a deflate compressed byte[]. You state "This is not the problem", but I really think it is. :-)

If I don't apply deflate compression to the ICC profile, I get a similar warning from ExifTool as you get from identify.

Warning                         : Error inflating iCCP
ICC Profile                     : (Binary data 526 bytes, use -b option to extract)

If I properly apply deflate compression to the ICC profile, the ICC profile is correctly identified as "Adobe RGB 1998".

Profile CMM Type                : ADBE
Profile Version                 : 2.1.0
Profile Class                   : Display Device Profile
Color Space Data                : RGB
[...]
CMM Flags                       : Not Embedded, Independent
Rendering Intent                : Perceptual
Connection Space Illuminant     : 0.9642 1 0.82491
Profile Creator                 : ADBE
Profile ID                      : 0
Profile Copyright               : Copyright 2000 Adobe Systems Incorporated
Profile Description             : Adobe RGB (1998)

The following code works for me:

public class PNGICCProfileIssue {
    public static void main(String[] args) throws IOException {
        File input = new File(args[0]);
        BufferedImage image = ImageIO.read(input);

        Iterator<ImageWriter> writers = ImageIO.getImageWritersByFormatName("PNG");
        if (!writers.hasNext()) {
            return;
        }

        ImageWriter writer = writers.next();
        writer.setOutput(ImageIO.createImageOutputStream(new File(input.getParent(), input.getName().replace('.', '_') + "_icc.png")));

        IIOMetadata imageMetadata = writer.getDefaultImageMetadata(ImageTypeSpecifier.createFromRenderedImage(image), writer.getDefaultWriteParam());

        //add ICC PROFILE
        IIOMetadataNode iccp = new IIOMetadataNode("iCCP");
        ICC_ColorSpace colorSpace = (ICC_ColorSpace) ColorSpaces.getColorSpace(ColorSpaces.CS_ADOBE_RGB_1998);
        iccp.setUserObject(getAsDeflatedBytes(colorSpace));
        iccp.setAttribute("profileName", "AdobeRGB1998");
        iccp.setAttribute("compressionMethod", "deflate");

        Node nativeTree = imageMetadata.getAsTree(imageMetadata.getNativeMetadataFormatName());
        nativeTree.appendChild(iccp);
        imageMetadata.mergeTree(imageMetadata.getNativeMetadataFormatName(), nativeTree);

        writer.write(new IIOImage(image, null, imageMetadata));
    }

    private static byte[] getAsDeflatedBytes(ICC_ColorSpace colorSpace) throws IOException {
        byte[] data = colorSpace.getProfile().getData();

        ByteArrayOutputStream deflated = new ByteArrayOutputStream();
        DeflaterOutputStream deflater = new DeflaterOutputStream(deflated);
        deflater.write(data);
        deflater.flush();
        deflater.close();

        return deflated.toByteArray();
    }

I think this requirement should have been documented in the metadata documentation, as I have to give you it is kind of confusing..

Good luck!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top