Hi !
Featured games (84)
games approved by the League of Dukes
Games in Showcase (595)
Games in Android Showcase (169)
games submitted by our members
Games in WIP (647)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  PixelGrabber  (Read 2645 times)
0 Members and 1 Guest are viewing this topic.
Offline Kommi

Junior Devvie

All opinions will be lined up and shot!

« Posted 2002-12-18 18:40:32 »

I am having trouble understanding what the values of a pixel mean in an array and how to change them. Could someone please explain?

Edit: Also what is the opperation to set a certain pixel color's alpha to 0? I found this example

for(int ctr = 0; ctr < pixels.length; ctr++)  
  int alpha = (pixels[ctr] >> 24) & 0xff;
  if (alpha != 0)
    // set this pixels transparancy to 25%
    pixels[ctr] =  0x4F000000 | (pixels[ctr] & 0x00FFFFFF);

Can someone explain the
int alpha = (pixels[ctr] >> 24) & 0xff    line of code?

Also if I wish to search the array for all the red pixels what value do I look for?

Offline Harley Rana

Junior Devvie

Java games rock!

« Reply #1 - Posted 2002-12-19 03:45:12 »

Hey the technique is called bit shifting.

int alpha = (pixels[ctr] >> 24) & 0xff;                          
you are getting 24th to the 32nd bits of the int.

Check out

Finding all the red pixels is pretty easy.

int width = img.getWidth(null);
int height = img.getHeight(null);
int[] imageData = new int[width * height];
PixelGrabber pg = new PixelGrabber(img, 0, 0, width, height, imageData, 0, width);
       } catch (InterruptedException e) {            
     for(int i=0; i<width; i++)
         for(int j=0; j<height; j++)
               int pixel = imageData[i + (j * width)];
               int red  =  (pixel >> 16) & 0xff;
               int green  =  (pixel >> 8) & 0xff;
               int blue  =  (pixel ) & 0xff;
               if(red==255 && green == 0 && blue == 0){
                    // Found full red pixel.


Offline Kommi

Junior Devvie

All opinions will be lined up and shot!

« Reply #2 - Posted 2002-12-20 14:44:41 »

ok second question is what do I have to multiply by to set the alpha to 0?

// set this pixels transparancy to 25%  
    pixels[ctr] =  0x4F000000 | (pixels[ctr] & 0x00FFFFFF);  

tis sets them to 25%. How do you figure out by what you need to and the number? In other words how do you get 0x4F000000 and 0x00FFFFFF?

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline leknor

Junior Devvie


« Reply #3 - Posted 2002-12-20 15:25:06 »

In other words how do you get 0x4F000000 and 0x00FFFFFF?

Those are number in hex. In hex each digit represents 4 bits, so it takes 2 hex digits for one byte. In java an int is a 32 bit number (4 bytes). When people use an int to represent a color they typically use one byte per component. This works nicely because you can have Red, Green, Blue and a Apha fit nicely in an int. The order is usually ARGB. So, for the number 0x12345678 in hex "12" is the alpha component, "34" is the red, and so on.

To set the apha to 0 (completely transparent) you would set the alpha component to 0x00000000 in hex. The new line would look like:

pixels[ctr] =  0x00000000 | (pixels[ctr] & 0x00FFFFFF);

PS: by my math the 0x4F000000 would be 31% transparent, not 25% like previously claimed.
Offline leknor

Junior Devvie


« Reply #4 - Posted 2002-12-20 15:52:39 »

pixels[ctr] =  0x4F000000 | (pixels[ctr] & 0x00FFFFFF);

Let me explain what is going on.

We have a color: pixels[ctr] . The first thing that happens is the bitwise and operator: "&" is used to effectivly remove the alpha component. It is really setting it to zero by only keeping the RGB components.

So now we have that color without an alpha and we are combining an alpha of 0x4F with the bitwise or operator: "|".

Finally we put the new color back in to the array.

One more time: We took a color, removed the alpha, and combined that color with a new alpha.

I think you need to study up on bitwise operators. The following link should get you started but if you want a solid understanding you're gonna need to search around or get a book.
Offline Kommi

Junior Devvie

All opinions will be lined up and shot!

« Reply #5 - Posted 2002-12-21 12:51:26 »

Thanks to everyone fo their responses. I now understand and have implemented my program using the knowledge provided by this community. Now if only I can get some help with my full screen question Smiley

Pages: [1]
  ignore  |  Print  
You cannot reply to this message, because it is very, very old.

KaiHH (23 views)
2015-07-07 11:39:58

deepthought (55 views)
2015-06-30 15:39:44

deepthought (58 views)
2015-06-30 15:39:09

deepthought (67 views)
2015-06-30 15:36:52

Za\'Anzabar (45 views)
2015-06-29 05:44:54

TritonDreyja (51 views)
2015-06-24 17:10:40

CopyableCougar4 (53 views)
2015-06-23 00:34:45

BurntPizza (58 views)
2015-06-21 20:36:46

cookiecompiler (100 views)
2015-06-11 15:42:53

cookiecompiler (58 views)
2015-06-11 15:41:14
How Do I Expand My Game?
by bashfrog
2015-06-14 11:34:43

List of Learning Resources
by PocketCrafter7
2015-05-31 05:37:30

Intersection Methods
by Roquen
2015-05-29 08:19:33

List of Learning Resources
by SilverTiger
2015-05-05 10:20:32

How to: JGO Wiki
by Mac70
2015-02-17 20:56:16

2D Dynamic Lighting
by ThePixelPony
2015-01-01 20:25:42

How do I start Java Game Development?
by gouessej
2014-12-27 19:41:21

Resources for WIP games
by kpars
2014-12-18 10:26:14 is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!