Java-Gaming.org    
Featured games (78)
games approved by the League of Dukes
Games in Showcase (429)
Games in Android Showcase (89)
games submitted by our members
Games in WIP (468)
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  
  Writing indexed PNG using Image IO  (Read 2442 times)
0 Members and 1 Guest are viewing this topic.
Offline appel

JGO Wizard


Medals: 49
Projects: 4


I always win!


« Posted 2011-09-22 13:25:46 »

I'm trying to save this image to disk in correct format but not able to find the correct way of doing it in Java2D and using ImageIO to write the image to disk.

If you have used photoshop you can use "save for web..." and save image as PNG24 or PNG8. I want to save it as PNG8 so that the output image looks like this:

and not like this:


I do not want translucency. I want translucent pixels to have a black matte.

Here's roughly what I am currently doing:

1  
2  
3  
4  
BufferedImage image= new BufferedImage(width, height, BufferedImage.BITMASK);
// do image editing.
// flush.
ImageIO.write(image, "PNG", targetImageFile);

This however saves the image as translucent, although BITMASK clearly states that transparency is either 1 or 0.

So, I tried to use BufferedImage.TYPE_BYTE_INDEXED, but the problem with that is that the background color ends up black and not transparent!

What's the right way to create and save an image as PNG8?

Check out the 4K competition @ www.java4k.com
Check out GAMADU (my own site) @ http://gamadu.com/
Offline Abuse

JGO Coder


Medals: 10


falling into the abyss of reality


« Reply #1 - Posted 2011-09-22 14:14:33 »

I'm trying to save this image to disk in correct format but not able to find the correct way of doing it in Java2D and using ImageIO to write the image to disk.

If you have used photoshop you can use "save for web..." and save image as PNG24 or PNG8. I want to save it as PNG8 so that the output image looks like this:

and not like this:


I do not want translucency. I want translucent pixels to have a black matte.

Here's roughly what I am currently doing:

1  
2  
3  
4  
BufferedImage image= new BufferedImage(width, height, BufferedImage.BITMASK);
// do image editing.
// flush.
ImageIO.write(image, "PNG", targetImageFile);

This however saves the image as translucent, although BITMASK clearly states that transparency is either 1 or 0.

So, I tried to use BufferedImage.TYPE_BYTE_INDEXED, but the problem with that is that the background color ends up black and not transparent!

What's the right way to create and save an image as PNG8?

BufferedImage.BITMASK won't work, as it's actually Transparency.BITMASK so is not an intended input for the BufferedImage constructor. (oh to retrofit the existing APIs with enums ><)

As for TYPE_BYTE_INDEXED; that's the correct image type - but if you are constructing it with the basic constructor (not supplying a ColorModel) you'll get one created for you that will be inappropriate (and no transparency colour).
As per the javadoc:
Quote
TYPE_BYTE_INDEXED
Represents an indexed byte image. When this type is used as the imageType argument to the BufferedImage constructor that takes an imageType argument but no ColorModel argument, an IndexColorModel is created with a 256-color 6/6/6 color cube palette with the rest of the colors from 216-255 populated by grayscale values in the default sRGB ColorSpace.

You'll need to construct a TYPE_BYTE_INDEXED and supply it an IndexColorModel with a palette appropriate for the image you are converting.

Don't ask me how you go about optimally reducing the number of colors in a 24bit png down to a 8bit palette - never had to do it myself Smiley

Make Elite IV:Dangerous happen! Pledge your backing at KICKSTARTER here! https://dl.dropbox.com/u/54785909/EliteIVsmaller.png
Offline appel

JGO Wizard


Medals: 49
Projects: 4


I always win!


« Reply #2 - Posted 2011-09-22 14:38:22 »

Ok, so I have to provide for the color palette myself.  Lips Sealed  Roll Eyes

Fine, I'll cook something up.

Thanks.

Check out the 4K competition @ www.java4k.com
Check out GAMADU (my own site) @ http://gamadu.com/
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Cero
« Reply #3 - Posted 2011-09-22 15:28:29 »

Quote
1  
BufferedImage image= new BufferedImage(width, height, BufferedImage.BITMASK); 

Yeah it's supposed to be Transparency.BITMASK
I made that mistake before too... it's just stupid

Offline appel

JGO Wizard


Medals: 49
Projects: 4


I always win!


« Reply #4 - Posted 2011-09-22 16:12:33 »

Ok, got it working, here's how it's done:


1  
2  
3  
4  
5  
6  
7  
final int[] colourMap = {   0x00000000, 0xff000000, 0xffffffff, 0xff353535, 0xff888888, 0xff969696, 0xff237fe9, 0xffff0000 };
IndexColorModel colorModel = new IndexColorModel(8, colourMap.length, colourMap, 0, true, 0, DataBuffer.TYPE_BYTE );
BufferedImage image = new BufferedImage(IMAGE_WIDTH, IMAGE_HEIGHT, BufferedImage.TYPE_BYTE_INDEXED, colorModel);

// Do whatever with your image
// ...
ImageIO.write(image, "PNG", imageFile);

Check out the 4K competition @ www.java4k.com
Check out GAMADU (my own site) @ http://gamadu.com/
Offline Riven
« League of Dukes »

JGO Overlord


Medals: 613
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #5 - Posted 2011-09-22 16:38:47 »

Offtopic: to prevent the syntax-highlighter from screwing up, start your multi-line comments with /** instead of /* Emo (javadoc style)

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

JGO Wizard


Medals: 49
Projects: 4


I always win!


« Reply #6 - Posted 2011-09-22 17:43:03 »

Offtopic: to prevent the syntax-highlighter from screwing up, start your multi-line comments with /** instead of /* Emo (javadoc style)

Cleaned it up.

Check out the 4K competition @ www.java4k.com
Check out GAMADU (my own site) @ http://gamadu.com/
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.

theagentd (6 views)
2014-04-24 23:00:44

xsi3rr4x (83 views)
2014-04-15 18:08:23

BurntPizza (75 views)
2014-04-15 03:46:01

UprightPath (86 views)
2014-04-14 17:39:50

UprightPath (69 views)
2014-04-14 17:35:47

Porlus (86 views)
2014-04-14 15:48:38

tom_mai78101 (109 views)
2014-04-10 04:04:31

BurntPizza (169 views)
2014-04-08 23:06:04

tom_mai78101 (265 views)
2014-04-05 13:34:39

trollwarrior1 (216 views)
2014-04-04 12:06:45
List of Learning Resources
by SHC
2014-04-18 03:17:39

List of Learning Resources
by Longarmx
2014-04-08 03:14:44

Good Examples
by matheus23
2014-04-05 13:51:37

Good Examples
by Grunnt
2014-04-03 15:48:46

Good Examples
by Grunnt
2014-04-03 15:48:37

Good Examples
by matheus23
2014-04-01 18:40:51

Good Examples
by matheus23
2014-04-01 18:40:34

Anonymous/Local/Inner class gotchas
by Roquen
2014-03-11 15:22:30
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!