Neuquant Quantize 1.05 (True Color to 8 bit) - Windows
Neuquant Quantize 1.05 (True Color to 8 bit) - Windows
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 ?
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 !
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 ?
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
A little PureBasic review
Re: Quantize 1.0 (True Color to 8 bit)
Impressive, and thanks for sharing
Edit: Just checked your website and looked at the code you've posted in the forums - nice! Glad I came across it all
Edit: Just checked your website and looked at the code you've posted in the forums - nice! Glad I came across it all
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
"When the facts change, I change my mind" - John Maynard Keynes
Re: Quantize 1.0 (True Color to 8 bit)
very very well.
thanks
thanks
- netmaestro
- PureBasic Bullfrog
- Posts: 8433
- Joined: Wed Jul 06, 2005 5:42 am
- Location: Fort Nelson, BC, Canada
Re: Quantize 1.0 (True Color to 8 bit) Windows
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
- Dreamland Fantasy
- Enthusiast
- Posts: 335
- Joined: Fri Jun 11, 2004 9:35 pm
- Location: Glasgow, UK
- Contact:
Re: Quantize 1.0 (True Color to 8 bit) Windows
Nice work there Luis!
Kind regards,
Francis
Kind regards,
Francis
Re: Quantize 1.0 (True Color to 8 bit) Windows
Thank you all
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...
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 )
Maybe I should add a fall back algorithm for quantization if the NeuQuant fails for extremely small images ?
Opinions ?
Yes, it's a size in bytes. if you look at the "NeuQuant.pb" include you'll see in the learning procedure this test: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?
Code: Select all
If iBufferLen < #NQ_minpicturebytes
ProcedureReturn 0
EndIf
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
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 )
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
A little PureBasic review
Re: Quantize 1.0 (True Color to 8 bit) Windows
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
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
A little PureBasic review
- netmaestro
- PureBasic Bullfrog
- Posts: 8433
- Joined: Wed Jul 06, 2005 5:42 am
- Location: Fort Nelson, BC, Canada
Re: Quantize 1.0 (True Color to 8 bit) Windows
My 32*32 image only had two colors, dunno if that makes a difference.
BERESHEIT
Re: Quantize 1.01 (True Color to 8 bit) - Windows
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)
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
A little PureBasic review
Re: Quantize 1.02 (True Color to 8 bit) - Windows
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.
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
A little PureBasic review
- netmaestro
- PureBasic Bullfrog
- Posts: 8433
- Joined: Wed Jul 06, 2005 5:42 am
- Location: Fort Nelson, BC, Canada
Re: Quantize 1.02 (True Color to 8 bit) - Windows
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
- flaith
- 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
i tried many different pictures and the results are amazing, thanks for sharing your code Luis
“Fear is a reaction. Courage is a decision.” - WC
-
- Enthusiast
- Posts: 767
- Joined: Sat Jan 24, 2004 6:56 pm
Re: Quantize 1.02 (True Color to 8 bit) - Windows
Luis,
you get my vote for using a picture of a P51 - anytime.
As to the rest:
you get my vote for using a picture of a P51 - anytime.
As to the rest:
Re: Quantize 1.02 (True Color to 8 bit) - Windows
P51, bf109, fw190, hurricane, mosquito, p38 lightning, spitfire, aermacchi mc202, etc. etc.dell_jockey wrote: you get my vote for using a picture of a P51 - anytime.
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
A little PureBasic review