title: ImageIO reading slightly different RGB values than other methods
body: <p>I've found that I'm getting different RGB when using Java (&amp; actually <a href="http://www.getpaint.net/index.html">paint.NET</a>) than I am using ImageMagick, Gimp, Python, and Octave. The last 4 all agreeing with eachother and so I'm assuming to be correct.</p>
  
  <p>For these examples, I'm using this test image: <code>http://farm3.static.flickr.com/2811/9177301733_9836174725_o.jpg</code></p>
  
  <p>Testing pixel <code>x=4144 y=2768</code></p>
  
  <pre><code>               R    G    B
  Java        = (125, 107, 69)
  Paint.NET   = (125, 107, 69)
  ImageMagick = (128, 106, 67)
  Python      = (128, 106, 67)
  Octave      = (128, 106, 67)
  Gimp        = (128, 106, 67)
  </code></pre>
  
  <p>What gives?</p>
  
  <p>Here's a quick test using imagemagick:</p>
  
  <pre><code>convert image.jpg -crop 1x1+4144+2768 -depth 8 txt:
  </code></pre>
  
  <p>output:</p>
  
  <pre><code># ImageMagick pixel enumeration: 1,1,65535,srgb
  0,0: (32896,27242,17219)  #806A43  srgb(128,106,67)
  </code></pre>
  
  <p>Here's some java and python code that also demonstrates the problem:</p>
  
  <pre><code>import org.apache.commons.io.FileUtils;
  import org.junit.Test;
  
  import javax.imageio.ImageIO;
  import java.awt.image.BufferedImage;
  import java.io.File;
  import java.io.IOException;
  import java.net.URISyntaxException;
  import java.net.URL;
  
  public class ImageIOTest {
      @Test
      public void can_read_file() throws IOException, InterruptedException, URISyntaxException {
          File tempFile = File.createTempFile("image", "jpg");
          FileUtils.copyURLToFile(new URL("http://farm3.static.flickr.com/2811/9177301733_9836174725_o.jpg"), tempFile);
  
          BufferedImage image = ImageIO.read(tempFile);
  
          int javaRGB = image.getRGB(4144, 2768);
          int javaRed = (javaRGB &gt;&gt; 16) &amp; 0xFF;
          int javaGreen = (javaRGB &gt;&gt; 8) &amp; 0xFF;
          int javaBlue = (javaRGB &gt;&gt; 0) &amp; 0xFF;
          System.out.printf("rgb: (%d, %d, %d)", javaRed, javaGreen, javaBlue);
      }
  }
  </code></pre>
  
  <p>And here is the corresponding python script:</p>
  
  <pre><code>from PIL import Image
  import sys, urllib, cStringIO
  
  file = cStringIO.StringIO(urllib.urlopen("http://farm3.static.flickr.com/2811/9177301733_9836174725_o.jpg").read())
  
  im = Image.open(file)
  pix = im.load()
  print pix[4144, 2768]
  </code></pre>
  
  <p>I've tried using <a href="https://github.com/haraldk/TwelveMonkeys">this 12monkeys library</a> in the hope that that would fix it but no dice. Any other ideas how I can extract correct RGB values using java? Surely I'm not the first person to have this problem!</p>
  
  <h2>Update</h2>
  
  <p>I tried <code>getRaster().getSample()</code> but got the same invalid result: <code>System.out.println(raster.getSample(4144, 2768, 0)+","+ raster.getSample(4144, 2768, 1)+","+ raster.getSample(4144, 2768, 2));</code> output: <code>125,107,69</code></p>
  
  <h2>More info</h2>
  
  <p>Here is some output that shows what RGB values are decoded by three different tools for the first 9 (3x3 square of) pixels in the top left of the image. As you can see, Python and ImageMagick are in unison. Java sometimes matches. I've put an X where java disagrees...:</p>
  
  <pre><code>Tool          [x, y] = (R , G , B )
  ImageIO     : [0, 0] = (86, 90, 93)
  Python      : [0, 0] = (86, 90, 93)
  ImageMagick : [0, 0] = (86, 90, 93)
  
  ImageIO     : [1, 0] = (86, 90, 93)
  Python      : [1, 0] = (86, 90, 93)
  ImageMagick : [1, 0] = (86, 90, 93)
  
  ImageIO     : [2, 0] = (90, 91, 95) X
  Python      : [2, 0] = (88, 92, 95)
  ImageMagick : [2, 0] = (88, 92, 95)
  
  ImageIO     : [0, 1] = (85, 93, 95)
  Python      : [0, 1] = (85, 93, 95)
  ImageMagick : [0, 1] = (85, 93, 95)
  
  ImageIO     : [1, 1] = (85, 93, 95) X
  Python      : [1, 1] = (87, 92, 95)
  ImageMagick : [1, 1] = (87, 92, 95)
  
  ImageIO     : [2, 1] = (87, 92, 95)
  Python      : [2, 1] = (87, 92, 95)
  ImageMagick : [2, 1] = (87, 92, 95)
  
  ImageIO     : [0, 2] = (83, 93, 94)
  Python      : [0, 2] = (83, 93, 94)
  ImageMagick : [0, 2] = (83, 93, 94)
  
  ImageIO     : [1, 2] = (83, 93, 94) X
  Python      : [1, 2] = (84, 92, 94)
  ImageMagick : [1, 2] = (84, 92, 94)
  
  ImageIO     : [2, 2] = (83, 91, 93)
  Python      : [2, 2] = (83, 91, 93)
  ImageMagick : [2, 2] = (83, 91, 93)
  </code></pre>
  
  <p>Why is Java giving different values for some pixels? Alternatively, is there another (fast) way to generate correct values using native Java code?</p>
  
  <h2>UPDATE 2016-09-26:</h2>
  
  <p>I committed my code that demonstrates this problem and pushed it to github (<a href="https://github.com/mattburns/imageio-test">imageio-test</a>) so that I could easily test it out on different machines. It turns out that Java was consistent across both OSX and Ubuntu Linux, but it was Python, ImageMagick and Octave that were inconsistent. In other words, on the Linux box, all tools agree with each other, and therefore, I'm now thinking that java was right all along, and it's the other tools that are giving incorrect results on OSX! I still don't really understand why and I haven't got any concrete proof as to which values are the correct ones but I'm getting somewhere...</p>