What does yourBufferedImage.getColorModel() return?
Here's one example.
<http://helpdesk.objects.com.au/java/changing-the-colormodel-of-a-bufferedimage>
Bringing this back to the group:
In an email message PickleMan wrote:
<http://helpdesk.objects.com.au/java/changing-the-colormodel-of-a-buff...>
Thanks, but I still don't completely understand how ColorModels work.
I mean, I don't get how to specify the colors. If I wanted to have all
white pixels (255,255,255) be represented as say...green pixels
(0,255,0); how would I set up the color model?
Study the javadocs, most of the classes of interest are here:
<http://java.sun.com/javase/6/docs/api/java/awt/image/package-frame.html>,
here is a pointer to a short tutorial:
<http://docs.rinet.ru/JaTricks/ch12.htm>
From the example pointed to above:
Note changes:
private static ColorModel createColorModel(int n) {
// Create a simple color model with all values mapping to
// a single shade of gray
// nb. this could be improved by reusing the byte arrays
byte[] r = new byte[16];
byte[] g = new byte[16];
byte[] b = new byte[16];
for (int i = 0; i < r.length; i++) {
r[i] = (byte) n;
g[i] = (byte) 255;
b[i] = (byte) n;
}
return new IndexColorModel(4, 16, r, g, b);
}
> > PickleMan wrote:
> > Thanks, but I still don't completely understand how ColorModels
> > work. I mean, I don't get how to specify the colors. If I wanted to
> > have all white pixels (255,255,255) be represented as say...green
> > pixels (0,255,0); how would I set up the color model?
If the default RGB ColorModel is OK, one simple approach might be to
use an RGBImageFilter [1] as described in the work cited [2]. If not,
LookupOp [3], a BufferedImageOp, with a suitable LookupTable [4] might
do. There's an example here [5].
[1]<http://java.sun.com/javase/6/docs/api/java/awt/image/RGBImageFilter.html>
[2]<http://www.webbasedprogramming.com/Tricks-of-the-Java-Programming-Gurus/>
[3]<http://java.sun.com/javase/6/docs/api/java/awt/image/LookupOp.html>
[4]<http://java.sun.com/javase/6/docs/api/java/awt/image/LookupTable.html>
[5]<http://www.javaworld.com/javaworld/jw-09-1998/jw-09-media.html>
--
John B. Matthews
trashgod at gmail dot com
<http://sites.google.com/site/drjohnbmatthews>
>>> PickleMan wrote:
>
>>> Thanks, but I still don't completely understand how ColorModels
>>> work.
Huh. This one seems to be all over the web.
> [2]<http://www.webbasedprogramming.com/Tricks-of-the-Java-Programming-Gurus/>
I wish for a freely available, comprehensive tutorial on AWT images.
Here's one that covers SampleModels & Rasters.
<http://javaboutique.internet.com/tutorials/rasters/>
> John B. Matthews wrote:
>
> >>> PickleMan wrote:
> >
> >>> Thanks, but I still don't completely understand how ColorModels
> >>> work.
This prompts me to return to your question, "What does getColorModel()
return?" I sense that I missed some discussion.
> Huh. This one seems to be all over the web.
Indeed, I should be more careful about linking obviously unauthorized
copy of Vanderburg's 1996 work "Tricks of the Java Programming Gurus":
<http://www.amazon.com/Tricks-Programming-Gurus-Glenn-Vanderburg/dp/1575211025>
Interestingly, the link to a Moscow host that you provided had been
black-listed, and it was refused by my news host. I (rather carelessly)
Googled another.
> I wish for a freely available, comprehensive tutorial on AWT images.
> Here's one that covers SampleModels & Rasters.
>
> <http://javaboutique.internet.com/tutorials/rasters/>
I confess an enduring fascination. I had some fun with the JavaWorld
example, adding a revert button and replacing Hashtable with a Map:
Map<String,BufferedImageOp> mOps new TreeMap<String,BufferedImageOp>();
<http://www.javaworld.com/javaworld/jw-09-1998/media/ImageDicer.java>
> This prompts me to return to your question, "What does getColorModel()
> return?" I sense that I missed some discussion.
>
No, there was no prior discussion.
My question was intended as a prompt.
Shortly after posting,
It dawned on me that there is no way to mutate a
BufferedImage with a new ColorModel, hence my follow-up
with a pointer to an example.
Over the years I've had to take a lot of instruction
given in this manner, and while it is frustrating
sometimes when you're in a hurry to get answers, I've
learned a lot from being prompted to find my own answers.
> John B. Matthews wrote:
>
> > This prompts me to return to your question, "What does
> > getColorModel() return?" I sense that I missed some discussion.
>
> No, there was no prior discussion.
> My question was intended as a prompt.
>
> Shortly after posting,
> It dawned on me that there is no way to mutate a
> BufferedImage with a new ColorModel, hence my follow-up
> with a pointer to an example.
Ah, that makes sense. The BufferedImageOp, LookupOp, seems
closest to meeting the OP's needs. A the same time, I'm always
intrigued by the possibility of using ColorConvertOp, which
filters on ColorSpace, as seen in this pithy example:
<http://groups.google.com/group/comp.lang.java.programmer/msg/d4c436ab64d4ced4>
[...]
I'm going to take this opportunty to plug Dick Baldwin:
<http://www.dickbaldwin.com/toc.htm>
the 400 series - Processing Image Pixels
<http://www.dickbaldwin.com/tocadv.htm>
> Ah, that makes sense. The BufferedImageOp, LookupOp, seems
> closest to meeting the OP's needs.
What function are you using?
Assuming a bufferedImage and three short[256] initialized to 0..255,
r[255] = 255; g[255] = 153; b[255] = 0;
short[][] whiteOrange = new short[][]{r, g, b};
LookupOp whiteOrangeOp = new LookupOp(
new ShortLookupTable(0, whiteOrange), null));
then
whiteOrangeOp.filter(bufferedImage, null);
> How would you use a ColorConvertOp to change all white pixels in an
> image to green(orange) per the OR?
I don't know that I could. It wouldn't be a very useful ColorSpace!
> I'm going to take this opportunty to plug Dick Baldwin:
> <http://www.dickbaldwin.com/toc.htm>
> the 400 series - Processing Image Pixels
> <http://www.dickbaldwin.com/tocadv.htm>
Yep, I'm a fan.