Java-Gaming.org Hi !
Featured games (81)
games approved by the League of Dukes
Games in Showcase (513)
Games in Android Showcase (119)
games submitted by our members
Games in WIP (576)
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  
  Image to Array of colors  (Read 1407 times)
0 Members and 1 Guest are viewing this topic.
Offline kidluff

Senior Newbie





« Posted 2006-01-11 17:51:17 »

How can I convert an image to an array of Colors (by pixels)?
Offline tom
« Reply #1 - Posted 2006-01-11 19:25:50 »

Create a java.awt.image.BufferedImage with the same dimensions as your image. Paint your image on the BufferedImage. Use BufferedImage.getRGB() to get the pixels.

Or use java.awt.image.PixelGrabber

Offline kidluff

Senior Newbie





« Reply #2 - Posted 2006-01-12 08:20:22 »

Oh how do I load an image from disk (such as TGA) onto a BufferedImage?

And how do I convert the int given by BufferedImage.getRGB(x,y,) to the RGB values?
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline tusaki

Junior Duke


Medals: 1


In a mad world only the mad are sane.


« Reply #3 - Posted 2006-01-12 09:47:32 »

Oh how do I load an image from disk (such as TGA) onto a BufferedImage?

1  
2  
3  
// although ImageIO.read pretends to returns an Image, in practice this is always a BufferedImage, so you can
// cast it to BufferedImage if you like...
Image image = ImageIO.read(new File("myFile.tga"));


And how do I convert the int given by BufferedImage.getRGB(x,y,) to the RGB values?

1  
2  
3  
4  
5  
6  
int color = myBufferedImage.getRGB(x,y);

// red, green, blue in the range 0-255
int red = (color >> 16) & 0xFF;
int green = (color >> 8) & 0xFF;
int blue = color & 0xFF;
Offline kidluff

Senior Newbie





« Reply #4 - Posted 2006-01-12 10:14:59 »

Cool thanks =P
Offline Riven
« League of Dukes »

JGO Overlord


Medals: 816
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #5 - Posted 2006-01-12 10:43:22 »

1  
2  
3  
// although ImageIO.read pretends to returns an Image, in practice this is always a BufferedImage, so you can
// cast it to BufferedImage if you like...
Image image = ImageIO.read(new File("myFile.tga"));

Check the javadocs Smiley

ImageIO returns a BufferedImage, not an Image. It's not even pretending Smiley

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Offline kidluff

Senior Newbie





« Reply #6 - Posted 2006-01-12 11:49:20 »

1  
2  
3  
// although ImageIO.read pretends to returns an Image, in practice this is always a BufferedImage, so you can
// cast it to BufferedImage if you like...
Image image = ImageIO.read(new File("myFile.tga"));

Check the javadocs Smiley

ImageIO returns a BufferedImage, not an Image. It's not even pretending Smiley

BufferedImage image = ImageIO.read(new File("pic.jpg"));
That works, but it only takes JPG and GIF files... is there something else to load TGA/BMP files?

Annd how do you convert RGB vaules back into the single RGB int (so the reverse)?
Offline tusaki

Junior Duke


Medals: 1


In a mad world only the mad are sane.


« Reply #7 - Posted 2006-01-12 16:02:13 »

1  
2  
3  
// although ImageIO.read pretends to returns an Image, in practice this is always a BufferedImage, so you can
// cast it to BufferedImage if you like...
Image image = ImageIO.read(new File("myFile.tga"));

Check the javadocs Smiley

ImageIO returns a BufferedImage, not an Image. It's not even pretending Smiley

You are right ofcourse. I looked something up from my own code and it was me who was casting to Image. lol.

BufferedImage image = ImageIO.read(new File("pic.jpg"));

That works, but it only takes JPG and GIF files... is there something else to load TGA/BMP files?

http://java.sun.com/products/java-media/jai/forDevelopers/jaifaq.html#fileformat for supported file formats. BMP seems to be supported. TGA is just uncompressed anyway...  but here is a plugin for ImageIO http://www.realityinteractive.com/software/oss/#ImageIOTGA

Annd how do you convert RGB vaules back into the single RGB int (so the reverse)?

1  
2  
// shift them back
int finalColor = (red << 16) | (green << 8) | blue;
Offline kidluff

Senior Newbie





« Reply #8 - Posted 2006-01-13 09:08:24 »

Wow thank you so much you guys are the greatest  Grin
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.

Longarmx (45 views)
2014-10-17 03:59:02

Norakomi (36 views)
2014-10-16 15:22:06

Norakomi (27 views)
2014-10-16 15:20:20

lcass (31 views)
2014-10-15 16:18:58

TehJavaDev (61 views)
2014-10-14 00:39:48

TehJavaDev (61 views)
2014-10-14 00:35:47

TehJavaDev (51 views)
2014-10-14 00:32:37

BurntPizza (69 views)
2014-10-11 23:24:42

BurntPizza (39 views)
2014-10-11 23:10:45

BurntPizza (81 views)
2014-10-11 22:30:10
Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06
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!