0

I'm using the following code to load a grayscale JPG image, and query a single pixel's value from it:

Raster data;
data = ImageIO.read (file).getData();
int [] buf = new int[1];
data.getPixel(0, 0, buf);

The image I'm using for test purposes is this one:

testgradient.jpg

When I open this image in Photoshop, and extract the colour information from the top left pixel, it tells me it is pure black (RGB 0,0,0). As far as I can see, there is no colour profile attached to the image, so it should be loaded identically by ImageIO. However, the result of the above code is that buf[0] contains 1. As my intended application is sensitive to this change (I am processing map elevation data with 0 indicating sea level or lower) I am wondering if either

  1. I can rely on it always happening, and can rely on RGB 1,1,1 not also being decoded as value 1 (testing this, I get '3' out), or
  2. there is any way of preventing the change?

Preventing the change would clearly be preferable -- if black is coming out as 1, and having tested white and seeing it come out as 250, this must mean that some values in the middle are going to be compressed so multiple source values come out with the same output value, so I am clearly losing information here. I'd ideally want to avoid information loss (and, yes, I'm aware that information was lost in the conversion of the map data to JPG, but this is how it was supplied to me by the original data provider, so it is impossible for me to recover that data... I'd like to avoid losing any more of it, though).

Jules
  • 14,841
  • 9
  • 83
  • 130

1 Answers1

4

What's probably happening is that the JPEG does not have an embedded colorspace, and Photoshop's default colorspace is different from Java's default, resulting in slightly different pixel values. For example, Java could be assuming sRGB while Photoshop might be using AdobeRGB. You should verify that you're using the same colorspace in both Photoshop and Java.

EDIT:

I examined the image you included in your post with Photoshop, and found the following interesting info:

  1. The colorspace is "uncalibrated" <exif:ColorSpace>65535</exif:ColorSpace>
  2. The image is a CMYK TIFF file with 4 samples per pixel, not 3

Whatever is giving you (0,0,0) for the top-left corner and (255,255,255) for the bottom-right is applying some conversion function to the raw data, which is most definitely not pure black and pure white in this file.

Here's the entire XMP raw data set:

<?xpacket begin="" id="W5M0MpCehiHzreSzNTczkc9d"?>
<x:xmpmeta xmlns:x="adobe:ns:meta/" x:xmptk="Adobe XMP Core 5.0-c060 61.134777, 2010/02/12-17:32:00        ">
   <rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#">
      <rdf:Description rdf:about=""
            xmlns:xmpMM="http://ns.adobe.com/xap/1.0/mm/">
         <xmpMM:DocumentID>45A47BED0B7E76061DB20B529F19C17E</xmpMM:DocumentID>
         <xmpMM:InstanceID>45A47BED0B7E76061DB20B529F19C17E</xmpMM:InstanceID>
      </rdf:Description>
      <rdf:Description rdf:about=""
            xmlns:dc="http://purl.org/dc/elements/1.1/">
         <dc:format>image/jpeg</dc:format>
      </rdf:Description>
      <rdf:Description rdf:about=""
            xmlns:photoshop="http://ns.adobe.com/photoshop/1.0/">
         <photoshop:ColorMode>1</photoshop:ColorMode>
      </rdf:Description>
      <rdf:Description rdf:about=""
            xmlns:tiff="http://ns.adobe.com/tiff/1.0/">
         <tiff:ImageWidth>16</tiff:ImageWidth>
         <tiff:ImageLength>16</tiff:ImageLength>
         <tiff:BitsPerSample>
            <rdf:Seq>
               <rdf:li>8</rdf:li>
               <rdf:li>8</rdf:li>
               <rdf:li>8</rdf:li>
               <rdf:li>8</rdf:li>
            </rdf:Seq>
         </tiff:BitsPerSample>
         <tiff:PhotometricInterpretation>1</tiff:PhotometricInterpretation>
         <tiff:SamplesPerPixel>4</tiff:SamplesPerPixel>
         <tiff:XResolution>72/1</tiff:XResolution>
         <tiff:YResolution>72/1</tiff:YResolution>
         <tiff:ResolutionUnit>2</tiff:ResolutionUnit>
      </rdf:Description>
      <rdf:Description rdf:about=""
            xmlns:exif="http://ns.adobe.com/exif/1.0/">
         <exif:ExifVersion>0221</exif:ExifVersion>
         <exif:ColorSpace>65535</exif:ColorSpace>
         <exif:PixelXDimension>16</exif:PixelXDimension>
         <exif:PixelYDimension>16</exif:PixelYDimension>
      </rdf:Description>
      <rdf:Description rdf:about=""
            xmlns:xmp="http://ns.adobe.com/xap/1.0/">
         <xmp:CreateDate>2012-04-11T12:26:47-07:00</xmp:CreateDate>
         <xmp:ModifyDate>2012-04-11T12:26:48-07:00</xmp:ModifyDate>
         <xmp:MetadataDate>2012-04-11T12:26:48-07:00</xmp:MetadataDate>
      </rdf:Description>
   </rdf:RDF>
</x:xmpmeta>
Jim Garrison
  • 85,615
  • 20
  • 155
  • 190
  • That's really strange. When I open the image in photoshop it tells me that it's an 8-bit per pixel grayscale image, not CMYK. – Jules Apr 12 '12 at 10:31
  • ... and that it isn't performing any colour space management on it. – Jules Apr 12 '12 at 10:36
  • OK, assigning a colour profile of 'Grey Gamma 2.2', matching my default Photoshop workspace setting (from the "color management off" preset), seems to have fixed it. Thanks for pointing me in the right direction... I'd just assumed that "color management off" meant the colours photoshop told me were the colours stored in the file, but clearly that isn't the case. – Jules Apr 12 '12 at 11:04