MysticSheik
MysticSheik

Reputation: 53

Java RGB Manipulation with Bitmaps

This question may be a little confusing, but here we go!

So, I watched Notch coding a game, and he did something interesting. He made a bitmap class, where he stored each pixel in an array. (int array). Then I got the idea of toying around with bitmaps myself, and then I got the idea of manipulation/changing the R, G and B of the bitmap.

So, the way I tried to do it was like this:

    int rmod =  100;
    int gmod =  100;
    int bmod =  100;
    for (int i = 0; i < 64*64; i++) {
        int color = testBitmap.pixels[i];
        int r = color >> 16;
        int g = color >> 8;
        int b = color;

        r = (r/100)*rmod;
        g = (g/100)*gmod;
        b = (b/100)*bmod;

        testBitmap.pixels[i] =  r+b+g;
    }

So yeah, the idea is that rmod, gmod and bmod works like the % of R G B. So, when they are all at 100, the bitmap is displayed normally is the idea. And then, say, if you change rmod to 150, the R will be 50% "stronger".

Now, I know that I'm doing some things wrong here. I think that int r, g and b is correct. (even though I don't really understand what ">>" do. I know that it shifts bytes or something, but I don't really get it.) After getting the r g and b, I then get 1% of them, and multiply them with the percent declared in rmod, gmod and bmod. Then, I believe I'm doing something totally wrong at the line where I set the new RGB of the current pixel. (testBitmap.pixels[i] = r+g+b;)

Not really sure how to do this. If anyone could try to explain how the ">>" works and, why it works in this situation, and also how to be able to manipulate the RGB via rmod, gmod and bmod, I would be so happy!

It may be that I have provided too little info, so if you need to see the classes etc, I can post them here too!

Thank you!

Upvotes: 0

Views: 851

Answers (1)

Tetramputechture
Tetramputechture

Reputation: 2921

First of all, the operator >> shifts a bit pattern to the right, however many places. Right shifting is the equivalent of division by a power of two. For example, (all numbers in binary), 1000 >> 3 = 0001 (shift '1000' to the left 3 times). 1000 in binary is 8, therefore, 1000 >> 3 = 10002 / 23 = 1.

However, you want the binary left shift for your code <<, which is equal to multiplication by a power two. The reason for this is because, since you're dealing with RGB values (24 bits), you want to multiply the red by 216, the green by 28, and the blue by 20. That's 24 powers of 2, total. Thus, 24 bits. I'm not exactly sure why this is, I'm sure somebody else can explain it.

For your array testBitmap.pixels[], I assume that is the actual RGB data in the bitmap. This requires a bit of understanding of how the bitmap file format works, but basically, the pixels are stored in 'reverse' order; e.g. RGB data is stored (relative to the image pixels) left to right, and down to up, and in triplets e.g. if the first triplet in the file was '00 00 FF', then the bottom left pixel would be red.

So, you realize you need another array to actually store the pixels, since the testBitmap.pixels array holds the bytes (in triplets, in BGR form).

Now that we have that out of the way (if you still don't understand, there are many resources out there to understand bitmap file formats and how to read them), to convert each triplet into a single integer pixel, you need to first get the unsigned value of each byte by adding & 0xFF to the end of your r, g, and b assignments. So,

int g = testBitmap.pixels[i] << 16 & 0xFF;
int b = testBitmap.pixels[i+1] << 8 & 0xFF;
int r = testBitmap.pixels[i+2] & 0xFF;

To convert those values into a single integer pixel, use the bitwise OR operator between them, thus

int pixel = (r | g) | b;

Then, if you want to store those values in an image at position (x, y), use BufferedImage.setRGB(x, y, pixel). But be careful, since the way the bitmap file format stores pixels 'upside down'.

To edit those values, multiply by each value by a constant, but remember that the values can only be between 0 and 255.

Upvotes: 2

Related Questions