We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpSyntax Questions › Converting RGB 8-bit...
Page Index Toggle Pages: 1
Converting RGB 8-bit... (Read 770 times)
Converting RGB 8-bit...
Oct 7th, 2008, 7:17pm
 
I'm learning Processing in a class I'm taking, and for a project coming up, I wanted to create a pixelated image that I've drawn, with 8-bit nintendo-like coloring.  My professor said that it is possible, but to ask you all how to go about doing that.  So...how could I convert RGB to 8-bit color...or something close to it?
Re: Converting RGB 8-bit...
Reply #1 - Oct 8th, 2008, 11:57am
 
by '8-bit' do you mean 8-bit (256) colours total or a fixed 8-colour palette? *

you need to go through your image and map each pixel in turn to the nearest colour in the target palette. (but saving the result image out might still write an rgb image, i don't think there's a way to choose palette-based. that said, indexed bmp image format is easy enough)

it gets harder if you can choose the target palette yourself and if you start dithering. (i had to do something similar for work and ended calling imagemagick for both scaling / padding and quantisation)

there are newish java libraries that might help:
http://java.sun.com/products/java-media/jai/forDevelopers/jai-apidocs/javax/media/jai/operator/ColorQuantizerDescriptor.html

the magic google words are 'color quantization' (or, in english, 'colour quantisation')

http://en.wikipedia.org/wiki/Color_quantization

* http://en.wikipedia.org/wiki/List_of_8-bit_computer_hardware_palettes
Re: Converting RGB 8-bit...
Reply #2 - Oct 9th, 2008, 10:20pm
 
I meant the 256 8-bit color palatte.  Thanks for your quick reply.
Re: Converting RGB 8-bit...
Reply #3 - Oct 10th, 2008, 11:36am
 
then i'd take a 256 colour palette like this one

http://en.wikipedia.org/wiki/List_of_software_palettes#8-8-4_levels_RGB

and then for each pixel in your image find the value in the palette that's closest to it and just replace your pixel with that value.

'closest' in this case can just be the 3d euclidean distance from your r, g and b value to the one in the palette

distance = (R - r)^2 + (G - g)^2 + (B - b)^2

that's a lot of comparing (each of your pixels against the 256 values in the palette) but i'm sure there's some way of optimising it. (um, thinking about it, if you stick to the 216 websafe colours this mapping is trivial, just a bitmask)

(i'd be tempted to try this using an image editor first, see if the results are worth bothering with (and also as test output))
Page Index Toggle Pages: 1