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 :
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?
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.
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.