Hi !
Featured games (91)
games approved by the League of Dukes
Games in Showcase (757)
Games in Android Showcase (229)
games submitted by our members
Games in WIP (844)
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  
  IndexColorModel  (Read 2886 times)
0 Members and 1 Guest are viewing this topic.
Offline userek

Junior Devvie

Projects: 1

« Posted 2009-01-18 14:01:18 »

I have a gif image that i load with ImageIO. It has 3 colors + transparent background(saved with gimp, indexed mode, 3 colors). I want to be able to use several different paletes for this image, infact i want to create another bufferedimage, with new palete and draw the old one on it.
int[] cmap = { 0xff2e00a4, 0xff2401d6, 0xff6c4eee, 0x00000000};
IndexColorModel icm = new IndexColorModel(8, cmap.length, cmap, 0,true,3,DataBuffer.TYPE_BYTE);

BufferedImage image = new BufferedImage(20,25,BufferedImage.TYPE_BYTE_INDEXED,icm);
Graphics2D g2d = (Graphics2D)image.getGraphics();
g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.CLEAR, 0.0f));
g2d.fillRect(0, 0, 20, 25);

The problem is that when i draw the original image on it, only one color is visible, ant the two others seem to be transparent.

The code:
IndexColorModel cm = (IndexColorModel)originalImage.getColorModel();      

Prints 4,3.
Any ideas?
Offline Abuse

JGO Ninja

Medals: 64

falling into the abyss of reality

« Reply #1 - Posted 2009-01-18 17:40:11 »

It seems as though you are trying to perform a palette swap?

If so, drawing the image from the 1st image onto the 2nd is not the correct approach.
When drawing onto an Image with an IndexColorModel it isn't just a case of copying the source raster onto the destination raster, the source colors must be mapped onto the closest color in the destination palette.

Obviously if the palettes of your 2 BufferedImages are dissimilar this process will not result in the outcome you want. (The mapping process can also be very slow)

What you should do is construct the 2nd BufferedImage using the Raster of your 1st BufferedImage (bi1.getRaster()), but providing a different (new) ColorModel.
The constructor you will be wanting is:
BufferedImage(ColorModel cm, WritableRaster raster, boolean isRasterPremultiplied, Hashtable<?,?> properties) 

I'm unsure if a copy of the provided raster is taken, you might find that the 2 images end up sharing the same Raster... which might or might not be desirable, depending upon what you intend to do with the images.
You can ofcourse clone the Raster if sharing is not desirable.
Offline userek

Junior Devvie

Projects: 1

« Reply #2 - Posted 2009-01-18 17:58:10 »

Thank you! The problem is solved now.
Pages: [1]
  ignore  |  Print  

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

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

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

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

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

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

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

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

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

Solater (159 views)
2018-03-17 05:04:08
Java Gaming Resources
by philfrei
2017-12-05 19:38:37

Java Gaming Resources
by philfrei
2017-12-05 19:37:39

Java Gaming Resources
by philfrei
2017-12-05 19:36:10

Java Gaming Resources
by philfrei
2017-12-05 19:33:10

List of Learning Resources
by elect
2017-03-13 14:05:44

List of Learning Resources
by elect
2017-03-13 14:04:45

SF/X Libraries
by philfrei
2017-03-02 08:45:19

SF/X Libraries
by philfrei
2017-03-02 08:44:05 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!