Page 1 of 4

Neuquant Quantize 1.05 (True Color to 8 bit) - Windows

Posted: Wed Oct 21, 2009 11:49 pm
by luis
This is an include to quantize true-color images to 256 colors (8 bit).

The code use the NeuQuant C algorithm by Anthony Dekker and is based on Kohonen neural networks for optimal colour quantization, I also added Floyd-Steinberg and Sierra filters (for optional dithering) to the original implementation. Quality was the first priority, speed came second.

The NeuQuant include translated to PB should be cross-platform, usage from any OS is explained in the include, you only have to provide the image data in the right format.

The rest of the code (the one using the port) is Windows x86/x64 only.

The code is modular enough to permit you to add more dithering algorithms AND more quantization algorithms easily if you like, just following the same method I used. The dithering algorithms are separated in dedicated macros.

The zip contains a simple program and the source images showed in the picture below, good for testing.

Thanks to Netmaestro for the code to save the 8 bit bmp to file.

What do you think ? :wink:


Image


Download [... from someone who got a copy at the time].
Updated for PB 5.21


EDIT: IMPORTANT

Now PB uses this same algorithm to quantize images from 24 to 8 bits, with an optional Floid-Steinberg dithering.
So you can probably use its native implementation directly and forget about this !

The differences, for what I can see, are:

1) with this source you can specify different levels of quality

2) you have two type of dithering and you can easily add more for yourself using macros following the examples in Macros.pbi

3) this is pb code, you can modify it

4) the native implementation is cross platform

Bye!


EDIT 2: ASM SSE2 VERSION !

Wilbert has done a version in ASM using SSE2 instructions (so the CPU need to support that) and it's at least 3 time faster than this !

See it here -> http://www.purebasic.fr/english/viewtop ... 33#p434633

So now you have a lot of different options: the PB implementation, this one with the PB source, and one written is ASM !

Re: Quantize 1.0 (True Color to 8 bit)

Posted: Thu Oct 22, 2009 1:28 am
by Amundo
Impressive, and thanks for sharing :D

Edit: Just checked your website and looked at the code you've posted in the forums - nice! Glad I came across it all :o

Re: Quantize 1.0 (True Color to 8 bit)

Posted: Thu Oct 22, 2009 7:03 am
by HAnil
very very well.
thanks

Re: Quantize 1.0 (True Color to 8 bit) Windows

Posted: Thu Oct 22, 2009 11:42 am
by netmaestro
The results are really excellent, nice job! I just have one question, is there a minimum bitmap size that the quantize routine will handle? Because if I pass an image 32*32 I get no result. I changed my size to 64*32 and it worked then.

Re: Quantize 1.0 (True Color to 8 bit) Windows

Posted: Thu Oct 22, 2009 12:03 pm
by Dreamland Fantasy
Nice work there Luis! :D

Kind regards,

Francis

Re: Quantize 1.0 (True Color to 8 bit) Windows

Posted: Thu Oct 22, 2009 12:15 pm
by luis
Thank you all :)
netmaestro wrote:The results are really excellent, nice job! I just have one question, is there a minimum bitmap size that the quantize routine will handle?
Yes, it's a size in bytes. if you look at the "NeuQuant.pb" include you'll see in the learning procedure this test:

Code: Select all

If iBufferLen < #NQ_minpicturebytes
    ProcedureReturn 0
EndIf
This is because the routine is jumping up and down scanning the input image using some predefined steps based on prime numbers, if the image is too small the algorithm cannot work...

Code: Select all

#NQ_prime1  = 499
#NQ_prime2  = 491
#NQ_prime3 = 487
#NQ_prime4 = 503

#NQ_minpicturebytes     = 3 * #NQ_prime4 ; minimum size for input image 
And there is another test just below for other conditions... but we are talking of really microbic images :)

BTW: 32x32 should still work.... (?)

EDIT: forgot it depends on quality settings too, there are some variables... in short if the image is really small AND quality settings are low, the "quantization" try to operate on a larger area and it doesn't work. With a small image (32x32 for example) using higher quality settings the algorithm resolution is higher, the chunk of area processed is smaller and it works.

Just tried a 32x32 image with low settings and high settings.

Anyway yes, with really small images the quantization can fail (the tests were originally missing in the C implementation, I added them 8) )

Maybe I should add a fall back algorithm for quantization if the NeuQuant fails for extremely small images ?

Opinions ?

Re: Quantize 1.0 (True Color to 8 bit) Windows

Posted: Thu Oct 22, 2009 1:03 pm
by Mistrel
For images that are too small can the borders be expanded to fake a larger image?

Re: Quantize 1.0 (True Color to 8 bit) Windows

Posted: Thu Oct 22, 2009 1:06 pm
by luis
Mistrel wrote:For images that are too small can the borders be expanded to fake a larger image?

I suppose it could be done. I'll give it a try maybe this evening. Thanks for the suggestion.

Re: Quantize 1.0 (True Color to 8 bit) Windows

Posted: Thu Oct 22, 2009 1:57 pm
by netmaestro
My 32*32 image only had two colors, dunno if that makes a difference.

Re: Quantize 1.01 (True Color to 8 bit) - Windows

Posted: Thu Oct 22, 2009 11:43 pm
by luis
OK, I implemented a wrapper of pixels around the images to be processed, this way the NeuQuant alg. should always find enough data and be happy.

As a slight bonus, using the buffer mentioned above the macros responsible for the dithering can skip the boundary checks because there is not the risk to go "outside" the image data anymore.

Netmaestro: no, the number of color shouldn't be important. Probably was a combination of size and quality settings as I wrote above (?)


This should remove the lower size limitation. Please let me know if it seem to works for you too.


(updated the first post to 1.02)

Re: Quantize 1.02 (True Color to 8 bit) - Windows

Posted: Sat Oct 24, 2009 7:27 pm
by luis
I had time to do some more testing by myself.

Seems working ok, I'm happy with the results so I'll consider this final and I'll move on to something else.

I softened the Sierra dithering in 1.02.

My final suggestions are:

Try without dithering, keep in mind dithering is a filter and the original image is modified by it, so you should use only quantization is possible.

For grayscale images, if you want to use dithering I think Sierra is better.

For color images with some color banding Sierra is probably better, in any other case Floyd-Steinberg should be better.

Bye Bye.

Re: Quantize 1.02 (True Color to 8 bit) - Windows

Posted: Mon Oct 26, 2009 4:34 am
by netmaestro
I've plugged your NeuQuant.pb code into my 256Colors.dll for converting/saving images as .GIF and the results really are stunning. Thanks again for sharing this, it's very useful. I only hope that someday I'll understand how it works, but for now I know nothing about neural networks beyond the fact that you can do some cool things with them.

Re: Quantize 1.02 (True Color to 8 bit) - Windows

Posted: Mon Oct 26, 2009 12:57 pm
by flaith
:shock: i tried many different pictures and the results are amazing, thanks for sharing your code Luis :D

Re: Quantize 1.02 (True Color to 8 bit) - Windows

Posted: Mon Oct 26, 2009 2:51 pm
by dell_jockey
Luis,

you get my vote for using a picture of a P51 - anytime.

As to the rest: :mrgreen:

Re: Quantize 1.02 (True Color to 8 bit) - Windows

Posted: Mon Oct 26, 2009 10:58 pm
by luis
dell_jockey wrote: you get my vote for using a picture of a P51 - anytime.
P51, bf109, fw190, hurricane, mosquito, p38 lightning, spitfire, aermacchi mc202, etc. etc.

I like them all, it was an incredible period for airplanes' history.