Color Reduction Algorithm

Does anyone have an algorithm to reduce a 32bit color value to a 8bit (256) or a 4bit (16) and/or a 2bit (4 color) value
I don’t know if it can reasonable be done based on just the COLOR value, or if it needs to analyze surrounding color pixels in the image…

But basically I need to take a PICTURE object and quickly reduce the color count while retaining the “best” looking result.
I was using QTExporter to do this for me. but since that is not an option any more I need to be able to replace that functionality

This will reduce to 2-256 colors with a quality parameter where higher quality takes more time. The output picture I believe is 24bit using the reduced colors, but the color table is supplied if you want to scan into a different picture.
http://home.comcast.net/~trochoid/code/NeuroQuant4d.zip

GraphicsMagick and ImageMagick has a few things for color reduction built inside. You can use via command line, declares or plugins…

Thanks Will… it seems to do what I want… albeit it very slow compared to the QTExporter I was using…

Again Mac or Windows?

It can be done in pure Xojo code, but if you want it done fast, I’d recommend looking at CoreImage.

[quote=71762:@Sam Rowlands]Again Mac or Windows?

It can be done in pure Xojo code, but if you want it done fast, I’d recommend looking at CoreImage.[/quote]

This would be just of OSX… have any sample code to support CI?

Do you want to write declares for CoreImage or use a plugin implementing all of CoreImage classes with over 100 filters?

I can’t check this from my iPhone, but see if MacOSLib has what you need as some CoreImage stuff has been implemented, if memory serves.

Well you know my positon on plugins… I’d prefer the minimal amount of code (such as declares)…
really don’t want to link a huge library such as MacOSLib… even though I realize the compiler supposedly only links what is necessary

Without declares or plugins, you are limited to the methods that Xojo provides. Have you looked to see if Transform might do the job for you?

I use Transform quite a lot in this application… it is great for inverting colors, changing brightness, contrast…
but to reduce a colormap would require building a transform map which is 99% of the work

Unless you have an idea in that regard that I’m not seeing :slight_smile:

And Declares would be fine… just do not wish Plugins for reasons I have stated before. At least with a Declare I can research what it is how it works, and have a possiblty of replacing it in the future should Apple deprecate the current one

Oh, I misread your post, my bad.

So see if MacOSLib has something you can use, then copy the code you need out of it. Or bite the bullet and include the folder. If memory serves, its mere presence adds very little to your final app.

Where can I find the latest MacOSLib?

Always at:

http://github.com/macoslib/macoslib

You can either branch it to your own GitHub repo, or just download as Zip.

Ok… I am assuming that because you recommended MacOSLib that you thought such a feature example was in there… but if it is I am failing to recognize it as such. I saw some stuff that looked promising, but it was Carbon code… seems the level of comment/explanation is a bit on the short side…

Very short.

As I said, I’m working off my iPhone today and couldn’t look to see if there is something in there that will help.

I haven’t tried this yet… but I currently see no reason why it would not work

To reduce to 256 colors… Save as GIF format…reload picture… result would be a 256< color image

opps… I was thinking for 16 colors… all I had to do was take the 256 from the GIF and and with &HF0
BUT that is the color INDEX… not the color itself :frowning:

But that gets me 1/2 way there… I just need 32Bit, 8Bit and 4bit colors

GrayScale is easy… THAT I have handled :slight_smile:

Doesn’t the picture class do the job correctly? You can create a new picture with a given depth (0, 1, 2, 4, 8, 16 or 32 pixels) and then copy the source image to it. Haven’t tried myself, so I am unsure about the quality …

Are you sure?
Most depths below 32 seem to fail for me on Mac these days. I gave up and made everything 32bit to shut it up, (even though in many cases for my needs, I only needed 2 bit images… sharp masks being a good case in point)

Oh, you’re right, sorry! Cocoa always uses 32 bit no matter what you select. Ignore my post!