2

I can read in an image and write to an image and the image looks perfect. So thats ok.

I think I am having difficulties with my bit shifting. Because when I test print the first 3 RGB int values, and then test print the 3 int values after, they don't have the same values? But the I run the program again, the first print values are the same as the first print values before?

OK, below is the code when I bit shift the values out into red, green and blue arrays

int g=0;
for(int row=0; row<h; row++)
{
    for(int col=0; col<w; col++)
    {
        redPixels[row][col] = ((RGBarray[g]>>16)&0xff);
        greenPixels[row][col] = ((RGBarray[g]>>8)&0xff);
        bluePixels[row][col] = (RGBarray[g]&0xff);
        g++;
     }
}

Below is the code to bit shift the values back into integers...

for(int row=0; row<h; row++)
{
    for(int col=0; col<w; col++)
    {
        int rgb = (redPixels[row][col] & 0xff) << 16 | (greenPixels[row][col] & 0xff) << 8 |(bluePixels[row][col] & 0xff);
            bufferedImage.setRGB(col, row, rgb);
     }
}

first 3 int values before bit shifting into array: -16573912 -16573912 -16508119

first 3 int values after bit shifting back into integers: 203304 203304 269097

But keep in mind, when I run the program again, the exact same values are shown (in this case the above same output). And also that an accurate image is created. I am just unsure about the values not being the same before and after bit shifting?

Hope this is clear? Thanks

1 Answer 1

3

Your RGB values are 24 bits (3*8), but a java int is 32 bits. if you look at the hexidecimal values, the difference should be more obvious:

-16573912 = 0xFF031A28
   203304 = 0x00031A28
Sign up to request clarification or add additional context in comments.

5 Comments

The extra 0xff at the start of the original numbers is commonly referred to the alpha value of the color, and in this case the 0xff means fully opaque
So do I need to bit shift the left most 8 bits into an alpha array, then vice versa back into an rgb int to get same values? Thanks
@DouglasGrealis You could store the alpha value additionally, and just shift it in the same way as you currently are (just shift 24). Or, if you know the alpha is always 0xff, you can just always shift 0xff left into your result. As it stands, you leave the alpha at 0x00 which means "fully transparent"
@DouglasGrealis - it also depends what color model you are using (i.e. does it use alpha, something like TYPE_INT_ARGB).
Ah ok. Yes I am actually using TYPE_INT_RGB. But thanks for your help. Just needed to clarify

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.