[java] More graphics stuff

Started by
6 comments, last by Smoo 23 years, 7 months ago
I''m attempting to use the pixelGrabber for the first time and since I never really played with pictures other than in their original picture form, I''m at a loss as to what I''m doing. Now I have: pix[w * h]; PixelGrabber Grabber=new PixelGrabber(bkGround.getSource(), 0, 0, w, h, pix, 0, w); try { Grabber.grabPixels(); } catch (Exception e) {return;} but what I get in pix are numbers in the -13 million, etc... always in the negative high numbers. Now what I''m curious to know is how do I interpret the numbers to something I can work with since I have no clue how. I know the numbers are suppose to represent the pixels but if for instance I want to take the first pixel and attempt to turn in blacker (for a fade to black) how can I tell what color that pixel is? I guess what I''m asking is if I take this pixel color: -13434676 can someone interpret this to me as to where the RGB is in here and how to manipulate it to change it''s color? Thanks, Smoo
Advertisement
I don't know for sure but I think it goes something like this:

If the image a 32bit or 24bit image use this:

int pixel = pix[0]; // index of 0 would be the first pixel
// index of 3 would be 2nd pixel in 24bit mode

int alpha = (pixel >> 24) & 0xff; // ignore alpha if 24bit
int red = (pixel >> 16) & 0xff;
int green = (pixel >> 8) & 0xff;
int blue = (pixel ) & 0xff;

if the image is 16bit then I guess you would have to figure out how the Red,Green,Blue bits are packed in there and extract them( I haven't ever dealth with an image saved as 16bit, only video modes )

I'm not sure how 8bit images work in java.

Anyhow, once you have the red/green/blue bytes each one will be in the range of 0 - 255, as you decrease there values the pixel will become darker.

If you want to re-assemble the bytes back into an int do this:

int endPixel = 0;
endPixel = blue + ( green << 8 ) + ( red << 16 );

ao


Edited by - ao on September 13, 2000 3:22:06 AM
Play free Java games at: www.infinitepixels.com
I''m pretty sure java deals only with 32bit images. It will convert everything to 32bits when you make a new Image object. The reason you get highly negative numbers is because you are printing hex numbers as if they were regular ints. These hex numbers represent color and not actual numerical values. If a hex number begins with
0xFF
the resulting binary will be something like
11111111
and java has no unsinged ints. All the ints use the last (leftmost) bit to indicate whether something is positve (0) or negative (1). When dealing with the colors the Alpha (opacity) chanel is leftmost as ao indicated. So any of your colors that is highly visible - or not very invisible - is going to have 0xFF near it''s end (left). In summary, a color that looks like this
A R G B
FF FF FF FF
will be negative when interpreted as an int.
Java will deal with 24 bit images and it is usual to have no alpha channel when you are doing this, it also has objects to deal with indexed images but I don''t know how many bits this can deal with, I would assume that it is a minimum of 8.

Indexed bitmaps are particularly well-suited to using this framework of indexed colours, you can also specify a colour to be interpreted as transparent. I don''t know for certain but I would have thought that if you only want two different alpha values, 0 and 255 in your images it would be quicker to use this framework as opposed to using 32bit images assuming that there enough indices to go round.

In 16 bit images it is up to you how the bits are interpreted, and this is also true for all colour depths but usually the conventional RGBA interpretation is followed. When you create your image you just need to provide a colormodel which has the information on which masks to use when extracting each component. In 32/24 bit colour the masks would be 0x0000FF, 0x00FF, 0xFF for red, green, blue respectively. It is very customisable.

jimbob.
Oops, those colour masks should be 0xFF0000, 0x00FF00 and 0x0000FF for Red, Green, Blue respectively.

Jimbob.
hey how do you actually load up an image? I''m looking for something like:

bufferedImage house = new bufferedImage("C:\superFunGame\images\house.jpg");

I''ve been looking the in the java2D area and all I can find is something that uses URLs. Thanks

*
Well guys, you've been extremely helpful in deciphering this and it is exactly as Ao said except on mine there was no alpha channel. So now in theory i whipped up a basic fade image to black proggy... but for some reason when I do a repaint() it goes to another class and not my custom canvas class' update() method... boggle. oh well I'll check that another time.

Anyways, to answer anon's post on loading images I think what you want is basically

Toolkit t1;
Image imgPic;

t1 = Toolkit.getDefaultToolkit();
imgPic = t1.getImage("pic.jpg");

and of course, pic.jpg is in the current directory of this class
or just add forward slashes for sub directories.
You can also look into the MediaTracker if you want for loading pics.

Anyways, thanks a bunch guys, you've been great.
Smoo

Edited by - Smoo on September 13, 2000 11:43:22 PM
thanks I''ll look into that

This topic is closed to new replies.

Advertisement