Java-Gaming.org Hi !
Featured games (90)
games approved by the League of Dukes
Games in Showcase (768)
Games in Android Showcase (230)
games submitted by our members
Games in WIP (854)
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  
  Grabbing palette index from a pixel  (Read 3258 times)
0 Members and 1 Guest are viewing this topic.
Offline downward_spiral

Senior Newbie





« Posted 2003-04-07 14:51:34 »

Hi,

I have a 16 colour indexed/paletted bitmap. After loading this into an object, I want to look at the image data and grab the palette index for a specific pixel.

How can I do this?  I tried using a BufferedImage, being too stupid to realise that getRGB actually returns a colour value instead of an index. D'oh.. I'm too used to retro programming Roll Eyes Grin

Besides loading the bitmap in manually and sifting through the data, is there a lazy way to do it?

Speed isn't important.
Offline Abuse

JGO Ninja


Medals: 70


falling into the abyss of reality


« Reply #1 - Posted 2003-04-07 19:43:35 »

hmm, if its an IndexColorModel bufferedImage, you should be able to do this...... (I havn't tried it, so it might not be exactly correct Smiley)

1  
2  
3  
4  
5  
BufferedImage icm = #your indexcolormodel image#;
int [] pixel = new int[1];
icm.getData().getDataElement(x,y,pixel);

System.out.println("pixel @ " + x +"," + y +" is set to index " + pixel[0]);
Offline downward_spiral

Senior Newbie





« Reply #2 - Posted 2003-04-08 14:42:44 »

Hi,

That done it, thanks. Was previously trying to access the DataBuffer instead of the Raster, and it was giving strange results.

Although, it's getPixel(x,y,pixel) instead of getDataElement() Smiley

Thanks, again.

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

Senior Newbie





« Reply #3 - Posted 2003-04-08 18:31:34 »

Hi,

Ok, ran into a new problem.

When using ImageIO to load into a BufferedImage directly, the above works fine.

I can't use this though, since I'm writing an applet. Have to use getImage() instead.

So, I created a BufferedImage with type TYPE_BYTE_INDEXED, and gave it a new IndexColorModel object. Then I drew the previously loaded Image onto it.

So far so good. However, using the above code to grab the palette index from a pixel just gives nonsensical values.

Here's the code:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
            //create a new indexcolormodel
            byte[] t = new byte[16];
            IndexColorModel icm = new IndexColorModel(4,16,t,t,t);

            //draw the loaded image onto a BufferedImage
            tempImage = new BufferedImage(w,h,BufferedImage.TYPE_BYTE_INDEXED,icm);
            tempImage.createGraphics().drawImage(tempImage2,0,0,null);

            //grab a pixel
            int [] pixel = new int[1];
            tempImage.getData().getPixel(0,0,pixel);

            System.out.println(pixel[0]);



On a bitmap where the first three horizontal pixels have the values 1,2,1  this code will return 15, 15, 15.

Any ideas?
Offline Abuse

JGO Ninja


Medals: 70


falling into the abyss of reality


« Reply #4 - Posted 2003-04-08 21:08:55 »

drawing an image with a different pallete onto an Image of type IndexColorModel will force some kind of dithering. (to make the src colors fit within the pallete of the dst image)

if your dst image's palette is full of zeros, all the pixels drawn will be dithered to the same arbitrary palette index.

hence your result :S
Offline downward_spiral

Senior Newbie





« Reply #5 - Posted 2003-04-08 21:25:45 »

Thanks for the reply again. That's what I thought Sad

By tweaking the RGB values of the temporary palette, I did notice that the function is trying to be smart by remapping the image, thus destroying the data I want  Angry

Are there any non idiot-proof methods for doing a direct data transfer from an Image to a BufferedImage?

Is getImage() the only way to load images in an applet?

If so, is there *any* other way to access the raw data of an Image?  

Such a simple thing, and yet so awkward.

*tears hear out*
Offline jbanes

JGO Coder


Projects: 1


"Java Games? Incredible! Mr. Incredible, that is!"


« Reply #6 - Posted 2003-04-09 13:06:13 »

Just out of curiousity, what are you trying to accomplish? There may be a better way than trying to read the pallette indexes.

Java Game Console Project
Last Journal Entry: 12/17/04
Offline mill

Junior Devvie




popcorn freak


« Reply #7 - Posted 2003-04-10 05:16:39 »

when i want to grab pixels i use java.awt.image.PixelGrabber

dunno if it does what you want, but look it up

Offline downward_spiral

Senior Newbie





« Reply #8 - Posted 2003-04-10 14:48:46 »

What I'm trying to do is use a bitmap as a very simple level-map, with each colour index representing a block type.

I know there's a lot of map systems out there, but this is for a Uni project where every bit of code should be my own, and also has to be fully tested by myself.

So, I guessed this would be a nice easy way to implement a level map, and also get a free map editor in the process (MS Paint  Grin ). It's worked for me really well on other platforms before, and it gives GIF compression on the map in the bargain.

I think I'll ditch the Applet and move to a JFrame, since the alternative idea of writing my own map format, loader and editor is just too much hassle for this   :-/
Offline jbanes

JGO Coder


Projects: 1


"Java Games? Incredible! Mr. Incredible, that is!"


« Reply #9 - Posted 2003-04-10 15:10:04 »

Errm... Maybe you have some secret reason I'm missing, but why don't you just use the color value as a block type? Grayscale images would work particularly well since the red, green, and blue values would be the same.

Java Game Console Project
Last Journal Entry: 12/17/04
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline downward_spiral

Senior Newbie





« Reply #10 - Posted 2003-04-10 22:07:20 »

I considered this, but the team members who are going to be working on this map will most likely not have a clue how this works.

It's easier to say "each colour represents a block type" than to say "colour #001001001 represents block 1, #002002002 represents block 2.. " etc.

I guess a palette could always be provided to make sure the correct colours are always used, but then it limits the software they use.

For the moment though, JFrames are the way forward  Grin
Offline jbanes

JGO Coder


Projects: 1


"Java Games? Incredible! Mr. Incredible, that is!"


« Reply #11 - Posted 2003-04-11 13:28:59 »

You can still use the pallette. Just use the actual color value instead of the pallette value.

Java Game Console Project
Last Journal Entry: 12/17/04
Pages: [1]
  ignore  |  Print  
 
 

 
EgonOlsen (1329 views)
2018-06-10 19:43:48

EgonOlsen (1206 views)
2018-06-10 19:43:44

EgonOlsen (931 views)
2018-06-10 19:43:20

DesertCoockie (1372 views)
2018-05-13 18:23:11

nelsongames (1111 views)
2018-04-24 18:15:36

nelsongames (1359 views)
2018-04-24 18:14:32

ivj94 (2094 views)
2018-03-24 14:47:39

ivj94 (1251 views)
2018-03-24 14:46:31

ivj94 (2250 views)
2018-03-24 14:43:53

Solater (804 views)
2018-03-17 05:04:08
Deployment and Packaging
by mudlee
2018-08-22 18:09:50

Java Gaming Resources
by gouessej
2018-08-22 08:19:41

Deployment and Packaging
by gouessej
2018-08-22 08:04:08

Deployment and Packaging
by gouessej
2018-08-22 08:03:45

Deployment and Packaging
by philfrei
2018-08-20 02:33:38

Deployment and Packaging
by philfrei
2018-08-20 02:29:55

Deployment and Packaging
by philfrei
2018-08-19 23:56:20

Deployment and Packaging
by philfrei
2018-08-19 23:54:46
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!