Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (513)
Games in Android Showcase (120)
games submitted by our members
Games in WIP (577)
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  
  Manipulating BufferedImages with IndexColorModels  (Read 2023 times)
0 Members and 1 Guest are viewing this topic.
Offline BoBear2681

JGO Coder


Medals: 19



« Posted 2013-02-24 06:41:18 »

Hopefully it's just because it's so late, but for the life of me I can't figure this out.  I start with an 8-bit PNG image.  I can create a grayscale version of this image with Java2D like so:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
   private static final IndexColorModel createGreyscaleModel() {
      int SIZE = 256;
      byte[] r = new byte[SIZE];
      byte[] g = new byte[SIZE];
      byte[] b = new byte[SIZE];
      for (int i=0; i<SIZE; i++) {
         r[i] = g[i] = b[i] = (byte)i;
      }
      return new IndexColorModel(8, SIZE, r, g, b);
   }

   private static final BufferedImage createIndexedCopy(Image orig, IndexColorModel cm, int scale) {
      int w = orig.getWidth(null);
      int h = orig.getHeight(null);
      BufferedImage out = new BufferedImage(w*scale, h*scale, BufferedImage.TYPE_BYTE_INDEXED, cm);
      Graphics2D g2d = out.createGraphics();
//      g2d.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_SPEED);
//      g2d.setRenderingHint(RenderingHints.KEY_DITHERING, RenderingHints.VALUE_DITHER_DISABLE);
//      g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_OFF);
//      g2d.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_NEAREST_NEIGHBOR);
      g2d.drawImage(orig, 0, 0, w*scale, h*scale, null);
      g2d.dispose();
      return out;
   }

   // ...

   BufferedImage grayscaleImage = createIndexedCopy(otherImage, createGreyscaleModel(), 1);


The end result looks like what I'd expect.  So next, I thought I'd try to create a "greenscale" version of the image by creating the color model like so:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
   private static final IndexColorModel createGreenModel() {
      int SIZE = 256;
      byte[] r = new byte[SIZE];
      byte[] g = new byte[SIZE];
      byte[] b = new byte[SIZE];
      for (int i=0; i<SIZE; i++) {
         g[i] = (byte)i;
      }
      IndexColorModel model = new IndexColorModel(8, SIZE, r, g, b);
      return model;
   }


But the end result isn't what I want.  It looks as if some kind of interpolation was done, even though the image was never scaled.  This happens even when the original image is just a single color.  See the example below.  The original (blue) image has all pixels a single color, the grayscale version looks good, but the green version doesn't...



What am I missing?
Offline sproingie

JGO Kernel


Medals: 202



« Reply #1 - Posted 2013-02-24 16:44:59 »

Very hard to tell from the screenshot that the green is being dithered.  Why not try uncommenting line 18 and disabling dithering?
Offline philfrei
« Reply #2 - Posted 2013-02-24 20:21:12 »

I was reluctant to offer any ideas because I am baffled by IndexColorModel. Are there any tutorials on it that you recommend? I read the header in the JavaDocs and am scratching my head.

Is it possible the problem is arising due to how the signs of the bytes are being interpreted? Casting an int to a byte while running it up into "negative" territory throws a warning flag for me.

1  
2  
System.out.println((byte)128); // displays -128
System.out.println((byte)255); // displays -1

For example, is blue in the "high" position? If so, blue and grey (which includes blue) are possibly having their high bit being interpreted one way (correctly), while the high bit in red or green (when used in isolation) might be interpreted in another.

"It's after the end of the world! Don't you know that yet?"
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline sproingie

JGO Kernel


Medals: 202



« Reply #3 - Posted 2013-02-24 20:49:39 »

I can't figure out IndexedColorModel either, but all I saw was a commented-out hint to disable dithering and one of the swatches being dithered, and I just put the two together.  One thing to remember is that in smaller gamuts like 16 bit, green usually gets extra bits, because the eye is more sensitive to green.  So it might be interpolating some shades if you don't take that into account.

Offline BoBear2681

JGO Coder


Medals: 19



« Reply #4 - Posted 2013-02-24 23:40:32 »

Thanks guys.  Sorry about the super-small sample image; I was using the simplest test case possible, a single 16x16 tile, all one color, while trying to figure things out.

I had tried all those RenderingHints in the hope they would make a difference (as well as perhaps ward off issues when creating new images with scales > 1) but they seem to make no difference - the result is still the same, apparent dithering.

I found this old thread, in which Abuse gives an approach that does work.  If you start with one IndexColorModel'd Image, and create a new BufferedImage with a new ColorModel but the same Raster, things are much happier.  I'm still not sure why you get differing results when using a "grayscale" IndexColorModel vs. another IndexColorModel, when using a source image that has all pixels the same color, but whatever.
Offline philfrei
« Reply #5 - Posted 2013-02-25 03:43:21 »

Glad you found a work-around.

If it is not too much trouble, does the problem appear when you use Red in place of Green?

Interesting, that about green taking more bits sometimes.

"It's after the end of the world! Don't you know that yet?"
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 (52 views)
2014-10-17 03:59:02

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

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

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

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

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

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

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

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

BurntPizza (86 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!