It is currently Fri Aug 29, 2014 7:20 pm

All times are UTC + 1 hour




Post new topic Reply to topic  [ 48 posts ]  Go to page 1, 2, 3, 4  Next
Author Message
 Post subject: Neuquant Quantize 1.05 (True Color to 8 bit) - Windows
PostPosted: Wed Oct 21, 2009 11:49 pm 
Offline
Addict
Addict
User avatar

Joined: Wed Aug 31, 2005 11:09 pm
Posts: 3232
Location: Italy
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

You can download this version (1.05) from --> here
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 -> viewtopic.php?p=434633#p434633

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

_________________
[ Home ] [ My PC ] [ New to PB ? ]


Last edited by luis on Fri Mar 07, 2014 6:16 pm, edited 24 times in total.

Top
 Profile  
 
 Post subject: Re: Quantize 1.0 (True Color to 8 bit)
PostPosted: Thu Oct 22, 2009 1:28 am 
Offline
Enthusiast
Enthusiast

Joined: Thu Feb 16, 2006 1:41 am
Posts: 169
Location: New Zealand
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

_________________
WinXP Pro-SP3, PB4, slow CPU, ancient video card, fuzzy monitor
"When the facts change, I change my mind" - John Maynard Keynes


Top
 Profile  
 
 Post subject: Re: Quantize 1.0 (True Color to 8 bit)
PostPosted: Thu Oct 22, 2009 7:03 am 
Offline
User
User
User avatar

Joined: Thu Feb 26, 2004 5:42 pm
Posts: 86
Location: 28:58E 41:01N
very very well.
thanks


Top
 Profile  
 
 Post subject: Re: Quantize 1.0 (True Color to 8 bit) Windows
PostPosted: Thu Oct 22, 2009 11:42 am 
Offline
PureBasic Bullfrog
PureBasic Bullfrog
User avatar

Joined: Wed Jul 06, 2005 5:42 am
Posts: 7079
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.

_________________
Veni, vidi, vici.


Top
 Profile  
 
 Post subject: Re: Quantize 1.0 (True Color to 8 bit) Windows
PostPosted: Thu Oct 22, 2009 12:03 pm 
Offline
Enthusiast
Enthusiast
User avatar

Joined: Fri Jun 11, 2004 9:35 pm
Posts: 220
Location: Glasgow, UK
Nice work there Luis! :D

Kind regards,

Francis


Top
 Profile  
 
 Post subject: Re: Quantize 1.0 (True Color to 8 bit) Windows
PostPosted: Thu Oct 22, 2009 12:15 pm 
Offline
Addict
Addict
User avatar

Joined: Wed Aug 31, 2005 11:09 pm
Posts: 3232
Location: Italy
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:
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:
#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 ?

_________________
[ Home ] [ My PC ] [ New to PB ? ]


Top
 Profile  
 
 Post subject: Re: Quantize 1.0 (True Color to 8 bit) Windows
PostPosted: Thu Oct 22, 2009 1:03 pm 
Offline
Addict
Addict
User avatar

Joined: Sat Jun 30, 2007 8:04 pm
Posts: 2733
For images that are too small can the borders be expanded to fake a larger image?

_________________
Image


Top
 Profile  
 
 Post subject: Re: Quantize 1.0 (True Color to 8 bit) Windows
PostPosted: Thu Oct 22, 2009 1:06 pm 
Offline
Addict
Addict
User avatar

Joined: Wed Aug 31, 2005 11:09 pm
Posts: 3232
Location: Italy
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.

_________________
[ Home ] [ My PC ] [ New to PB ? ]


Top
 Profile  
 
 Post subject: Re: Quantize 1.0 (True Color to 8 bit) Windows
PostPosted: Thu Oct 22, 2009 1:57 pm 
Offline
PureBasic Bullfrog
PureBasic Bullfrog
User avatar

Joined: Wed Jul 06, 2005 5:42 am
Posts: 7079
My 32*32 image only had two colors, dunno if that makes a difference.

_________________
Veni, vidi, vici.


Top
 Profile  
 
 Post subject: Re: Quantize 1.01 (True Color to 8 bit) - Windows
PostPosted: Thu Oct 22, 2009 11:43 pm 
Offline
Addict
Addict
User avatar

Joined: Wed Aug 31, 2005 11:09 pm
Posts: 3232
Location: Italy
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)

_________________
[ Home ] [ My PC ] [ New to PB ? ]


Top
 Profile  
 
 Post subject: Re: Quantize 1.02 (True Color to 8 bit) - Windows
PostPosted: Sat Oct 24, 2009 7:27 pm 
Offline
Addict
Addict
User avatar

Joined: Wed Aug 31, 2005 11:09 pm
Posts: 3232
Location: Italy
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.

_________________
[ Home ] [ My PC ] [ New to PB ? ]


Top
 Profile  
 
 Post subject: Re: Quantize 1.02 (True Color to 8 bit) - Windows
PostPosted: Mon Oct 26, 2009 4:34 am 
Offline
PureBasic Bullfrog
PureBasic Bullfrog
User avatar

Joined: Wed Jul 06, 2005 5:42 am
Posts: 7079
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.

_________________
Veni, vidi, vici.


Top
 Profile  
 
 Post subject: Re: Quantize 1.02 (True Color to 8 bit) - Windows
PostPosted: Mon Oct 26, 2009 12:57 pm 
Offline
Enthusiast
Enthusiast
User avatar

Joined: Mon Apr 25, 2005 9:28 pm
Posts: 677
Location: $300:20 58 FC 60 - Vietnam
: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


Top
 Profile  
 
 Post subject: Re: Quantize 1.02 (True Color to 8 bit) - Windows
PostPosted: Mon Oct 26, 2009 2:51 pm 
Offline
Enthusiast
Enthusiast

Joined: Sat Jan 24, 2004 6:56 pm
Posts: 767
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


Top
 Profile  
 
 Post subject: Re: Quantize 1.02 (True Color to 8 bit) - Windows
PostPosted: Mon Oct 26, 2009 10:58 pm 
Offline
Addict
Addict
User avatar

Joined: Wed Aug 31, 2005 11:09 pm
Posts: 3232
Location: Italy
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.

_________________
[ Home ] [ My PC ] [ New to PB ? ]


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 48 posts ]  Go to page 1, 2, 3, 4  Next

All times are UTC + 1 hour


Who is online

Users browsing this forum: No registered users and 3 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
Jump to:  

 


Powered by phpBB © 2008 phpBB Group
subSilver+ theme by Canver Software, sponsor Sanal Modifiye