Reputation: 602
Okay I have extracted RGB values from a file using the follwing code-:
int r = cBuffer[1024*0+32*(j/32)+(j%32)] & 0xFF;
int g = cBuffer[1024*1+32*(j/32)+(j%32)] & 0xFF;
int b = cBuffer[1024*2+32*(j/32)+(j%32)] & 0xFF;
The "anding" with 0xFF is done because I want to convert the byte value to an unsigned representation.
Now I form the actual pixel using the following code-:
int pixel = (r << 16) | (g << 8) | b;
Now the problem is when I print(using SOP(pixel)) the value of the pixel I get numbers like the following-:
Computed Pixel = -11119787
Computed Pixel = -13884637
Computed Pixel = -14081500
Computed Pixel = -12436417
I know the Pixel calculation is right since the images that I get after using these values are valid.
Now my questions are-:
Upvotes: 0
Views: 2133
Reputation: 10184
Why are there such arbitrary values of the pixel ? Shouldn't a pixel value range from 0 to 255 ?
Is there any way I can represent a pixel value between 0to 255 given the rgb values ? Would that even be a correct representation ?
Like I mentioned in the second point above, you can represent each of the RGB channels / bytes with a value between 0 and 255. Let's say you have read the first pixel (Left top corner of the image) in a byte array.
byte[] pixel = new byte[3];
/* code to read the pixel in this byte array */
int r = pixel[0] & 0xFF;
int g = pixel[1] & 0xFF;
int b = pixel[2] & 0xFF;
Note the "anding" of the channel byte with 0xFF (255). This operation makes the byte value to unsigned value.
Upvotes: 0
Reputation: 88717
Shouldn't a pixel value range from 0 to 255?
No and you should know that already since you're working with shifting bytes etc. 0-255 would be the value range for an unsigned byte and since pixel values typically consist of 3 8-bit (i.e. 3 byte) values the resulting int
will have a much higher value in absolute terms.
Is there any way I can represent a pixel value between 0to 255 given the rgb values ? Would that even be a correct representation ?
No, as said above a 24-bit pixel value can't be represented in the range 0 - 255. You'd typically represent each component (r,g,b) in that range, i.e. a pixel would consist of 3 values in the range 0-255.
There are representations that would allow you to map a pixel to 0-255 but you'd lose a lot of information since you'd either have to convert it to grayscale or "compress" the rgb values to 2 or 3-bit values which clearly can't hold as much information as 8-bit values.
Btw, I'm not sure your rgb-extraction routine is correct or at least it seems to be very complicated (did you check the values are correct?). Assuming cBuffer
is a byte array that represents uncompressed 32-bit color information you'd probably just get the rgb values like this:
//assuming the buffer to be in rgba format, i.e. 32-bit pixels with alpha information in the last byte
for( int i = 0; i < cBuffer.length; i+=4) {
int r = cBuffer[i];
int b = cBuffer[i + 1];
int g = cBuffer[i + 2];
int a = cBuffer[i + 3];
//do whatever you want with the pixel
}
Edit:
According to your comment you want to get the color value in the range [0.0,1.0]. In that case you'd get the integer color value and divide that to the maximum color value (white). Assuming you have 24-bit color (or 32-bit argb and ignore the alpha component) you'd do something like this:
int w = 255 << 16 | 255 << 8 | 255; //white = 0x00FFFFFF
int r = 255 << 16; //red = 0x00FF0000
int g = 255 << 8; //green = 0x0000FF00
int b = 255; //blue = 0x000000FF
System.out.format( "white > int: %8d (real: %1.5f )\n", w, (double)w/w);
System.out.format( "red > int: %8d (real: %1.5f )\n", r, (double)r/w);
System.out.format( "green > int: %8d (real: %1.5f )\n", g, (double)g/w);
System.out.format( "blue > int: %8d (real: %1.5f )\n", b, (double)b/w);
Output:
white > int: 16777215 (real: 1,00000 )
red > int: 16711680 (real: 0,99609 )
green > int: 65280 (real: 0,00389 )
blue > int: 255 (real: 0,00002 )
Upvotes: 2