Results 1 to 15 of 15
- 08-03-2014, 02:09 PM #1
Translating greyscale image to colour
Current situation:
I have a greyscale image with alpha. I also have a colour. I want to change that greyscale image to the colour specified, with black remaining black and white being the specified colour, and the different shades of grey between becoming the shade of the specified colour.
I've got a couple of different thoughts about how to solve it myself, but I rather hope there's an easier way.
Solution one would be to use a BufferedImage and just use getRGB/setRGB excessively. This, however, will be slow...
The second solution would be to use the above and cache the result. Given that there can be up to 10 different colour schemes, and up to approx. 2000 different images, this can, in the end, become excessively memory-hogging (although the general case would likely be 3 colours and perhaps a hundred or two images). It would also mean I'd have to loop through all images whenever the user changes the colour.
Is there a magical third solution that can translate a greyscale image to colour on-the-fly that's fast enough to run hundreds of thousands of times per second?
No code available since this is just in the thought process right now.
- 08-03-2014, 05:10 PM #2
Senior Member
- Join Date
- Jan 2013
- Location
- Northern Virginia, United States
- Posts
- 6,226
- Rep Power
- 13
Re: Translating greyscale image to colour
I believe it would depend on the processing power and image size. But I am unaware of such a capability.
I checked the documentation for the ColorConvertOP and it says this:
"This class performs a pixel-by-pixel color conversion of the data in the source image. The resulting color
values are scaled to the precision of the destination image. Color conversion can be specified via an array of
ColorSpace objects or an array of ICC_Profile objects."
So even the JDK does it pixel by pixel (but they probably work directly on the image raster).
Regards,
JimThe JavaTM Tutorials | SSCCE | Java Naming Conventions
Poor planning on your part does not constitute an emergency on my part
- 08-03-2014, 05:58 PM #3
Re: Translating greyscale image to colour
Hmm. So little hope of finding a memory-effective solution then... unless working on the image raster speeds the process up by a factor of ten or twenty or so. Managed to display 400 images updated at 30fps by doing the getRGB/setRGB (with an array of 256*3 to make me not having to recalculate the new colours all the time), which isn't nearly what I need. Buffering the drawn image made me able to put down ~100.000 images though, which is more than enough. Too bad on the memory though, but eh, win some lose some. If it all goes pear-shaped I can always revert back to the slow method. Thanks for the input though!
- 08-03-2014, 06:13 PM #4
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 14,422
- Blog Entries
- 7
- Rep Power
- 28
Re: Translating greyscale image to colour
Maybe an IndexedColorModel can be of help: the original grey values would be the index for the color model, while the value entries would make up the resulting color. If this works (and I think it can), the fun is that you can change the color by simply changing the LUT.
kind regards,
JosBuild a wall around Donald Trump; I'll pay for it.
- 08-03-2014, 06:58 PM #5
Re: Translating greyscale image to colour
After reading up on it and wrapping my head around it... perhaps. It seems promising at least! Would need a fair bit of recoding, since it seems I'd have to create an indexed BufferedImage to use it with (or at least index the existing greyscale images). That is, unless I misunderstood something (which is always likely to happen when you're delving into the unknown). Definitely worth a quick test code though.
- 08-03-2014, 07:13 PM #6
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 14,422
- Blog Entries
- 7
- Rep Power
- 28
Re: Translating greyscale image to colour
My first (naive) approach would be to load the grey scale image somewhere, prepare a BufferedImage with an IndexedColorModel and draw the first image into the second one ...
kind regards,
JosBuild a wall around Donald Trump; I'll pay for it.
- 08-03-2014, 07:29 PM #7
Re: Translating greyscale image to colour
Yeah, that's my thinking too. Right now I'm trying to figure out how to change the color model though... I can get the color model through getColorModel, but there's no setColorModel in BufferedImage. Likewise, I can get the data of the color model by using its functions, but there's no way to change the data in the color model that I can find. Unless, by using drawImage it actually uses the index of the BufferedImage and not the color it translates to (which, frankly, would be rather odd). But I haven't gotten to the code phase yet... Silly need to eat.
- 08-03-2014, 07:35 PM #8
Senior Member
- Join Date
- Jan 2013
- Location
- Northern Virginia, United States
- Posts
- 6,226
- Rep Power
- 13
Re: Translating greyscale image to colour
You set the color model when you create the target buffered image. It's a constructor argument.
Regards,
JimThe JavaTM Tutorials | SSCCE | Java Naming Conventions
Poor planning on your part does not constitute an emergency on my part
- 08-03-2014, 07:41 PM #9
Re: Translating greyscale image to colour
Yeah, that part I've found. But once I've got it in there, and the image is indexed and everything, how do I make it change colors if I can't actually change the underlying color model that translates the indexes to colors? As I said, I haven't started coding with it yet, so I might stumble across something when I do that, but right now I'm not sure how it's supposed to be done.
- 08-03-2014, 08:23 PM #10
Re: Translating greyscale image to colour
Managed to make it change colours by utilising getData in BufferedImage (which returns a Raster) and convertToIntDiscrete in IndexColorModel (which uses a Raster to create a BufferedImage). It was slightly quicker than my old method (by approx. 10%), but it wasn't fast enough. I might be able to use the IndexColorModel to lower the memory footprint though, if it ever becomes an issue, so the time spent certainly wasn't a complete loss!
- 08-03-2014, 08:29 PM #11
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 14,422
- Blog Entries
- 7
- Rep Power
- 28
Re: Translating greyscale image to colour
Hrmph, I gave it a second read, but it seems that the IndexColorModel isn't a real LUT, i.e. it seems that the color mapping can't be changed after it it set. That's a lousy implementation if you'd ask me ...
kind regards,
JosBuild a wall around Donald Trump; I'll pay for it.
- 08-03-2014, 09:46 PM #12
Senior Member
- Join Date
- Jan 2013
- Location
- Northern Virginia, United States
- Posts
- 6,226
- Rep Power
- 13
Re: Translating greyscale image to colour
I tried the first approach suggested by Jos. Use a graphics context to write into another buffered image. It worked fine. I couldn't get the
raster method to work because I didn't notice the convertToIntDiscrete method. That will be useful. Too bad that you can't simply update
the map and then reapply it. What would really be nice if there was some way to share specific raster locations with colormap entries. Like a
many to one mapping which would simply take effect instantly.
Regards,
JimThe JavaTM Tutorials | SSCCE | Java Naming Conventions
Poor planning on your part does not constitute an emergency on my part
- 08-03-2014, 09:59 PM #13
Re: Translating greyscale image to colour
Yeah, sharing Raster between several images would be pretty awesome. As would being able to change the IndexColorModel in the BufferedImage. As it stands though, I doubt no matter what is tried the speed simply won't be where I need it to be, so for now, I'll just stick to caching generated images. Hopefully it won't be too bad; with any luck, I'll only need 200-400 different images in any one setting, even though there are a potential ~20.000 combinations. With any luck it'll work out.
- 08-03-2014, 10:13 PM #14
Senior Member
- Join Date
- Jan 2013
- Location
- Northern Virginia, United States
- Posts
- 6,226
- Rep Power
- 13
Re: Translating greyscale image to colour
If you are going to be caching images and have memory concerns, take a look at WeakReference and SoftReference classes. They might
be helpful.
Regards,
JimThe JavaTM Tutorials | SSCCE | Java Naming Conventions
Poor planning on your part does not constitute an emergency on my part
- 08-03-2014, 11:11 PM #15
Similar Threads
-
Problem drawing an image in greyscale
By supremegrandruler in forum New To JavaReplies: 10Last Post: 07-16-2014, 10:26 PM -
AffineTransform: can I scale without translating?
By gib65 in forum AWT / SwingReplies: 1Last Post: 11-13-2010, 11:16 PM -
BufferedImage - greyscale - color information
By AndreB in forum Advanced JavaReplies: 8Last Post: 03-04-2010, 11:09 AM -
Error in converting colour image to gray scale
By LankanSniper in forum Java 2DReplies: 9Last Post: 12-14-2009, 05:30 PM -
Translating an XML with the DOM ParserAPI
By gangsterooseven in forum Advanced JavaReplies: 1Last Post: 11-16-2009, 12:19 PM
Bookmarks