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

Share your advanced PureBasic knowledge/code with the community.
User avatar
luis
Addict
Addict
Posts: 3876
Joined: Wed Aug 31, 2005 11:09 pm
Location: Italy

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

Post 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 !
Last edited by luis on Mon Oct 23, 2017 1:02 pm, edited 27 times in total.
"Have you tried turning it off and on again ?"
A little PureBasic review
Amundo
Enthusiast
Enthusiast
Posts: 191
Joined: Thu Feb 16, 2006 1:41 am
Location: New Zealand

Re: Quantize 1.0 (True Color to 8 bit)

Post 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
Win8.1, PB5.x, okayish CPU, onboard video card, fuzzy monitor (or is that my eyesight?)
"When the facts change, I change my mind" - John Maynard Keynes
User avatar
HAnil
User
User
Posts: 87
Joined: Thu Feb 26, 2004 5:42 pm
Location: 28:58E 41:01N

Re: Quantize 1.0 (True Color to 8 bit)

Post by HAnil »

very very well.
thanks
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8425
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

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

Post 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.
BERESHEIT
User avatar
Dreamland Fantasy
Enthusiast
Enthusiast
Posts: 334
Joined: Fri Jun 11, 2004 9:35 pm
Location: Glasgow, UK
Contact:

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

Post by Dreamland Fantasy »

Nice work there Luis! :D

Kind regards,

Francis
User avatar
luis
Addict
Addict
Posts: 3876
Joined: Wed Aug 31, 2005 11:09 pm
Location: Italy

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

Post 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 ?
"Have you tried turning it off and on again ?"
A little PureBasic review
Mistrel
Addict
Addict
Posts: 3415
Joined: Sat Jun 30, 2007 8:04 pm

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

Post by Mistrel »

For images that are too small can the borders be expanded to fake a larger image?
User avatar
luis
Addict
Addict
Posts: 3876
Joined: Wed Aug 31, 2005 11:09 pm
Location: Italy

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

Post 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.
"Have you tried turning it off and on again ?"
A little PureBasic review
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8425
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

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

Post by netmaestro »

My 32*32 image only had two colors, dunno if that makes a difference.
BERESHEIT
User avatar
luis
Addict
Addict
Posts: 3876
Joined: Wed Aug 31, 2005 11:09 pm
Location: Italy

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

Post 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)
"Have you tried turning it off and on again ?"
A little PureBasic review
User avatar
luis
Addict
Addict
Posts: 3876
Joined: Wed Aug 31, 2005 11:09 pm
Location: Italy

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

Post 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.
"Have you tried turning it off and on again ?"
A little PureBasic review
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8425
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

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

Post 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.
BERESHEIT
User avatar
flaith
Enthusiast
Enthusiast
Posts: 704
Joined: Mon Apr 25, 2005 9:28 pm
Location: $300:20 58 FC 60 - Rennes
Contact:

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

Post by flaith »

:shock: i tried many different pictures and the results are amazing, thanks for sharing your code Luis :D
“Fear is a reaction. Courage is a decision.” - WC
dell_jockey
Enthusiast
Enthusiast
Posts: 767
Joined: Sat Jan 24, 2004 6:56 pm

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

Post by dell_jockey »

Luis,

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

As to the rest: :mrgreen:
cheers,
dell_jockey
________
http://blog.forex-trading-ideas.com
User avatar
luis
Addict
Addict
Posts: 3876
Joined: Wed Aug 31, 2005 11:09 pm
Location: Italy

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

Post 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.
"Have you tried turning it off and on again ?"
A little PureBasic review
Post Reply