Java-Gaming.org
Play Revenge of the Titans! The situation is critical. We need fancy commanders to defend Earth, the moon, Mars!
Featured games (78)
games approved by the League of Dukes
Games in Showcase (407)
games submitted by our members
Games in WIP (293)
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 1814 times)
0 Members and 1 Guest are viewing this topic.
Offline appel

JGO Ninja


Medals: 35
Projects: 5


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: 2


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!
Offline appel

JGO Ninja


Medals: 35
Projects: 5


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!
Try the Free Demo of Droid Assault
Online 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 Ninja


Medals: 35
Projects: 5


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/
Online Riven
« League of Dukes »

JGO Overlord


Medals: 438
Projects: 4


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
Projects: Revenge of the Titans, Titan Attacks, Droid Assault, and Ultratron
Offline appel

JGO Ninja


Medals: 35
Projects: 5


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.

Play Revenge of the Titans! The situation is critical. We need fancy commanders to defend Earth, the moon, Mars!
 
Browse for soundtracks for your game!

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

The first screenshot will be displayed as a thumbnail.

The invasion has landed! On Mars! And you're there to beat 'em!
cubemaster21 (97 views)
2013-05-17 21:29:12

alaslipknot (105 views)
2013-05-16 21:24:48

gouessej (135 views)
2013-05-16 00:53:38

gouessej (130 views)
2013-05-16 00:17:58

theagentd (141 views)
2013-05-15 15:01:13

theagentd (128 views)
2013-05-15 15:00:54

StreetDoggy (170 views)
2013-05-14 15:56:26

kutucuk (192 views)
2013-05-12 17:10:36

kutucuk (195 views)
2013-05-12 15:36:09

UnluckyDevil (201 views)
2013-05-12 05:09:57
Complex number cookbook
by Roquen
2013-04-24 12:47:31

2D Dynamic Lighting
by Oskuro
2013-04-17 16:46:12

2D Dynamic Lighting
by Oskuro
2013-04-17 16:45:57

2D Dynamic Lighting
by Oskuro
2013-04-17 16:23:20

Noise (bandpassed white)
by Roquen
2013-04-05 17:36:01

Noise (bandpassed white)
by Roquen
2013-04-03 16:17:38

Java Data structures
by Roquen
2013-03-29 13:21:12

Topic Request
by kutucuk
2013-03-22 21:42:01
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!
Page created in 0.129 seconds with 21 queries.