Java-Gaming.org    
Featured games (79)
games approved by the League of Dukes
Games in Showcase (477)
Games in Android Showcase (109)
games submitted by our members
Games in WIP (536)
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 2158 times)
0 Members and 1 Guest are viewing this topic.
Offline Kommi

Junior Member




All opinions will be lined up and shot!


« Posted 2002-12-18 19: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?

Kommi
Offline Harley Rana

Junior Member




Java games rock!


« Reply #1 - Posted 2002-12-19 04: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 http://www.javage.net/cgi-bin/showArt.cgi?index=7

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);
       try
       {
           pg.grabPixels();
           
       } 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 Member




All opinions will be lined up and shot!


« Reply #2 - Posted 2002-12-20 15: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?

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

Junior Member




ROCK!!!


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

Quote
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 Member




ROCK!!!


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

1  
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. http://java.sun.com/docs/books/tutorial/java/nutsandbolts/bitwise.html
Offline Kommi

Junior Member




All opinions will be lined up and shot!


« Reply #5 - Posted 2002-12-21 13: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

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

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

CogWheelz (18 views)
2014-07-30 21:08:39

Riven (25 views)
2014-07-29 18:09:19

Riven (15 views)
2014-07-29 18:08:52

Dwinin (13 views)
2014-07-29 10:59:34

E.R. Fleming (33 views)
2014-07-29 03:07:13

E.R. Fleming (12 views)
2014-07-29 03:06:25

pw (43 views)
2014-07-24 01:59:36

Riven (43 views)
2014-07-23 21:16:32

Riven (30 views)
2014-07-23 21:07:15

Riven (31 views)
2014-07-23 20:56:16
List of Learning Resources
by SilverTiger
2014-07-31 18:29:50

List of Learning Resources
by SilverTiger
2014-07-31 18:26:06

List of Learning Resources
by SilverTiger
2014-07-31 13:54:12

HotSpot Options
by dleskov
2014-07-08 03:59:08

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:58:24

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:47:22

How do I start Java Game Development?
by ra4king
2014-05-17 11:13:37

HotSpot Options
by Roquen
2014-05-15 09:59:54
java-gaming.org 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‑gaming.org
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!