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

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

Post by luis »

netmaestro wrote:I've plugged your NeuQuant.pb code into my 256Colors.dll for converting/saving images as .GIF and the results really are stunning.
If you just used the neuquant.pb please remember there is still the limitation about the image size you mentioned, it is fixed but only through the main include !

Have fun :)
"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 chose a different solution for the size. I was afraid of warping the colortable by introducing all those black border pixels, so I make a copy of the image, resize it raw to large enough while keeping the same aspect ratio, and feed that to the NeuQuant procedure. That way, the same colors in the same proportions are being analyzed by NeuQuant. Pixels from the original input image are used for the set-and-dither.
BERESHEIT
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 »

Yes, it's a better (probably) solution.

I initially thought to use for the border the most used color inside the original image, but then I saw it was really immaterial so I simply went for the black (edit: I should have used rgb(128,128,128) on a second thought because it's in the middle of the 3d color cube).

But your idea is very good.
"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 »

1.03 for download if someone interested.

Just to have peace of mind :o I modified the border-thing.

Now the pixels-buffer's color around the image is the most used color in the source image.

I added this proc

Code: Select all

; Find the n most used colors in the passed image
; The desired n number of colors is specified through the sizing of array iaMostused()
; The returned value is the number of colors copied in the array
; If the returned value is less than n, than the number of colors of the image was less than n
; and the remaining items of the array are filled with zeros
; The colors are sorted in descending order, from the most used (first item) to the less used (last item)
Declare.i	Find32BitImageMostUsedColors (hImage, Array iaMostUsed(1))
I use it to find the most (one, single) used color but can be useful to find the n most used too.

Ah, fixed a couple of "256" that should have been "255". Mixing Dims and actual sizes is always a pain in the... back.

See first post for dl.

Bye!
"Have you tried turning it off and on again ?"
A little PureBasic review
THCM
Enthusiast
Enthusiast
Posts: 276
Joined: Fri Apr 25, 2003 5:06 pm
Location: Gummersbach - Germany
Contact:

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

Post by THCM »

Great Work Luis!

Is it possible to reduce a true color image to less than 256 different colors (16, 32 etc.)? Also a nice function for game developers would be, if your converter would support loadable user defined color tables. For example colors 0-15 will be predefined by the user and colors 16-255 can be freely chosen by your quantizer and of course all 256 colors should then be used for the conversion. It would be useful to mix for example a gui and changing background pictures etc..
The Human Code Machine / Masters' Design Group
User avatar
luis
Addict
Addict
Posts: 3876
Joined: Wed Aug 31, 2005 11:09 pm
Location: Italy

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

Post by luis »

The original algorithm was specifically thought to reduce 24 bit images (or 15-16 bit too for that matter) to 256 colors. The target was 256 colors. It will not work well, even if modified, for less than 256 colors. It's possible force it but it's not the right algorithm for that.

So the short answer is no! To quantize for less than 256 color is better to use something different.

About the reserved, loadable palette, etc. I see what you mean but it's all beyond the purpose of this code and I believe it would be better to write something quite different for youR needs. Some code in here can be certainly useful for write that, but that's all.
"Have you tried turning it off and on again ?"
A little PureBasic review
User avatar
Dreamland Fantasy
Enthusiast
Enthusiast
Posts: 335
Joined: Fri Jun 11, 2004 9:35 pm
Location: Glasgow, UK
Contact:

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

Post by Dreamland Fantasy »

@THCM

I have some code that remaps an image to an 8 colour pre-defined palette. It shouldn't be too hard to adapt that to other quantities of colours.

If you are interested then I will dig it out.

Kind regards,

Francis
c4s
Addict
Addict
Posts: 1981
Joined: Thu Nov 01, 2007 5:37 pm
Location: Germany

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

Post by c4s »

I realized I didn't thank you although I really like your code - it's fantastic and works very well!

So thank you. :wink:
If any of you native English speakers have any suggestions for the above text, please let me know (via PM). Thanks!
User avatar
luis
Addict
Addict
Posts: 3876
Joined: Wed Aug 31, 2005 11:09 pm
Location: Italy

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

Post by luis »

c4s wrote:I realized I didn't thank you although I really like your code - it's fantastic and works very well!
Well, thank you so much for letting me know !
It's always nice to hear something is appreciated and maybe useful.

Hey, now this PB port is officialy linked from the original author of the algorithm !

See "Are people using NeuQuant?"
http://members.ozemail.com.au/~dekker/NEUQUANT.HTML

So my thanks to Mr. Dekker !

(it's another way to make people know about the great PB :wink: )
"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.03 (True Color to 8 bit) - Windows

Post by Mistrel »

Does not work on Windows XP 64-bit.

I tried both this and netmaestro's method in PureBasic 4.31 and 4.40.

For your code the output is a black bitmap with a small white square towards the bottom right.
User avatar
luis
Addict
Addict
Posts: 3876
Joined: Wed Aug 31, 2005 11:09 pm
Location: Italy

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

Post by luis »

It works under 64 bit (I mean compiled as 64 bit code) so the the problem is probably due to some quirk of XP64.

Maybe an API not supported by that OS or working differently ?

Sorry but I never had a 64 bit version of XP so I don't know how to check, you are probably in a better position to see where it fails :-)

If you can point me in the right direction I will be happy to update the code to make it work there also.
"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.03 (True Color to 8 bit) - Windows

Post by Mistrel »

I tested it with the 4.40 x64 compiler and it works fine. It just doesn't work compiled with the x86 compiler on Windows XP 64-bit. I'm guessing that this might be the case on any x64 platform (Vista/Win7).
User avatar
luis
Addict
Addict
Posts: 3876
Joined: Wed Aug 31, 2005 11:09 pm
Location: Italy

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

Post by luis »

Ah! So you previously tried the 32 bit executable under xp64 ?

AFAIK xp64 didn't have the same level of support for 32 bit programs as vista or windows 7, so a compatibility problem is possible. Good to know it works as expected with the 64 bit executable.

FYI the 32 bit executable compiled under win7/32 works fine under win7/64 bits too.
"Have you tried turning it off and on again ?"
A little PureBasic review
User avatar
Dreamland Fantasy
Enthusiast
Enthusiast
Posts: 335
Joined: Fri Jun 11, 2004 9:35 pm
Location: Glasgow, UK
Contact:

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

Post by Dreamland Fantasy »

The 32 bit executable also worked fine under Vista 64 bits.

Kind regards,

Francis
Fred
Administrator
Administrator
Posts: 16623
Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:

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

Post by Fred »

PureBasic v4.50+ uses the very same algo to dither the pictures when using saveimage() with 8 bit paramter.
Post Reply