1

i want to get RGB code for each pixel. As example i took pixel(0,0) (corner left). I load JPEG image and then store in Bitmap object in Java. For experiment i attach the image for example :

enter image description here

I take RGB with PHP and Java. Here the code :

Java (rgbImage is Bitmap object which loaded from JPEG file) :

int width = rgbImage.getWidth();
int height = rgbImage.getHeight();
for (int x = 0; x < rgbImage.getWidth(); x++){
 for (int y = 0; y < rgbImage.getHeight(); y++) {
  int pixel = rgbImage.getPixel(x, y);
    double red = Color.red(pixel);
    double green = Color.green(pixel);
    double blue = Color.blue(pixel);

    if(x == 0 && y == 0){
      System.out.println("red : "+red+" green : "+green+" blue : "+blue);
    }

PHP :

    $rgbImage = imagecreatefromjpeg("$path");
    $width = imagesx($rgbImage);
    $height = imagesy($rgbImage);

    for($x = 0 ; $x < $width ; $x++){
        for($y = 0 ; $y < $height ; $y++){
            $rgb = imagecolorat($rgbImage, $x, $y);
            $r = ($rgb >> 16) & 0xFF;
            $g = ($rgb >> 8) & 0xFF;
            $b = $rgb & 0xFF;

            if($x == 0 && $y == 0){
                echo("r : $r , g : $g, b : $b <br/>");
            }


        }
    }

And the result both of them are :

JAVA : I/System.out: red : 91.0 green : 91.0 blue : 101.0

PHP : r : 93 , g : 91, b : 102

The Main Question is :

Why with the same image, two methods above can give different result?

Mr. Mike
  • 433
  • 4
  • 16
  • Since you have a JPEG, might be something about how the API read the file, this is not a bitmap, so the values are not written for each pixel, but evaluated. Read this [post](http://stackoverflow.com/questions/17893814/java-create-and-read-rgb-pixel-value-different?rq=1) about something equivalent – AxelH Dec 28 '16 at 09:34
  • @axelh : i forget to tell. I load JPEG image and store inside Bitmap object. – Mr. Mike Dec 28 '16 at 09:38
  • @axelh : You mean the quality of JPEG bring back to Uncompressed (Bitmap) ? – Mr. Mike Dec 28 '16 at 09:41
  • @axelh : The file extension is JPEG and then load it by URL and then store to Bitmap object. so the resource is JPEG. – Mr. Mike Dec 28 '16 at 09:44
  • 3
    Everything is said. You used a JPEG so you have lost the precision, the bitmap will be build based on the JPEG format (basicly depending on pixel areas changment). So once you have a JPEG, you have lost the precise bitmap. That's is that simple. The difference will be small, so you can always accept some margin here – AxelH Dec 28 '16 at 09:47
  • @axelh : in my logic. Both of them take pixel from a same image and a same format (JPEG). But i don't understand why two methods above can get different result of RGB. Are both the methods have different technique to take RGB value? – Mr. Mike Dec 28 '16 at 09:48
  • Ok, read [this wiki article](https://en.wikipedia.org/wiki/JPEG#JPEG_compression). It explain that compression use some mathemical formula. This will result in floating values that will be rounded at some point, so depending on how PHP and Java devs have manage those rounding, there will be difference (this is a simplified explanation ;) ) – AxelH Dec 28 '16 at 09:51
  • @axelh : ok, thanks for your advices. :D – Mr. Mike Dec 28 '16 at 09:53

2 Answers2

2

BMP is different from JPEG.

JPEG is a compressed file, while BMP is a precise uncompressed one (mostly). When you create a JPEG in PHP, the image gets compressed and loses some information, and the difference is very hard to notice to a human eye. In Java, you use an original Bitmap, that doesn't use JPEG compression. Therefore, two resulting images may be different, like you have noticed.

Read here or here on differences between these and other formats.

Community
  • 1
  • 1
Yury Fedorov
  • 12,277
  • 6
  • 44
  • 63
  • I'm sorry but i forget to tell you this : I load JPEG image from URL and then store in Bitmap Object. So the quality like Bitmap or still like JPEG ? – Mr. Mike Dec 28 '16 at 09:40
2

The JPEG use a specific compression based on the discrete cosine transform (DCT).

This compression will use a mathematic formula to reduce the size of the information based on area of pixel. This will lead to floating values that will need to be rounded at some point. Of course, the opposite is true, to rebuild a bitmap from a JPEG, some math are needed, based on how this will be implemented will lead in differences because might round some values differently.

So basicly, you have a painting (bmp file), you want to reduce the detail of it (number of information = size of the file), for that, you can use water to dilute the paint, the painting is still pretty from a distance. But once you have done that, the details that you will see will depends on your brain that will imagine them. Each brain (algorythm to recreate a bitmap) will see a slighty different painting.

AxelH
  • 13,322
  • 2
  • 21
  • 50