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.03 (True Color to 8 bit) - Windows

Post by luis »

Fred wrote:PureBasic v4.50+ uses the very same algo to dither the pictures when using saveimage() with 8 bit paramter.
Really ? But do you really mean dither or maybe quantize ?
I'm confused by your use of "dither" here.

Because this algorithm use two different type of dithering BTW (floyd steinberg and sierra). But they are somewhat marginal. The important thing is the type of quantization applied. Fred, are you telling me now PB use the same quantization's algorithm ? The Neuquant ?


EDIT: just tried a quick save of 32 bit image to 8 with pb, in any case the quantization is *very* good. About the flag #PB_Image_FloydSteinberg mentioned in the manual... the compiler don't recognize it...(PB 4.51 RC1).

So... DOH!
"Have you tried turning it off and on again ?"
A little PureBasic review
Fred
Administrator
Administrator
Posts: 16619
Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:

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

Post by Fred »

luis wrote:
Fred wrote:PureBasic v4.50+ uses the very same algo to dither the pictures when using saveimage() with 8 bit paramter.
Really ? But do you really mean dither or maybe quantize ?
I'm confused by your use of "dither" here.

Because this algorithm use two different type of dithering BTW (floyd steinberg and sierra). But they are somewhat marginal. The important thing is the type of quantization applied. Fred, are you telling me now PB use the same quantization's algorithm ? The Neuquant ?
Yep.
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 »

That's very good news and explain the quality of the images. Thanks.

But shouldn't this be mentioned in the docs ? For licensing reasons I mean. Like you did for PCRE for the regular expressions for example ?

Sorry if it's already there and I didn't see it...
Last edited by luis on Wed Aug 04, 2010 3:55 pm, edited 1 time in total.
"Have you tried turning it off and on again ?"
A little PureBasic review
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 »

luis wrote:EDIT: just tried a quick save of 32 bit image to 8 with pb, in any case the quantization is *very* good. About the flag #PB_Image_FloydSteinberg mentioned in the manual... the compiler don't recognize it...(PB 4.51 RC1).
Try using -1 as the flag as mentioned here: http://www.purebasic.fr/english/viewtop ... 64#p329864
The result looks pretty good except that for example Windows Image Viewer displays the result a little brighter...

Edit: I just realized that you seem to know this for yourself ;)
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 »

Hi c4s !

About the -1 value for the constant ? No I didn't, thank you, I will try that :)
"Have you tried turning it off and on again ?"
A little PureBasic review
Fred
Administrator
Administrator
Posts: 16619
Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:

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

Post by Fred »

luis wrote:That's very good news and explain the quality of the images. Thanks.

But shouldn't this be mentioned in the docs ? For licensing reasons I mean. Like you did for PCRE for the regular expressions for example ?

Sorry if it's already there and I didn't see it...
For what i can read from the license, there is no mention about publishing a note about it, did i miss something ?

The constant value is : 1<<8
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 »

@Fred

Thank for the constant :)

About the license, on the author's site I read this:
Do I need to pay a license fee to use NeuQuant?

No, but you must include the copyright notice at the top of the program in the source code and documentation of the program using NeuQuant. See the copyright notice at the top of the program for more details.
I imagine a mention in the docs should be in order. But you certainly know these things better than myself!
"Have you tried turning it off and on again ?"
A little PureBasic review
Fred
Administrator
Administrator
Posts: 16619
Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:

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

Post by Fred »

It wasn't that clear in source header, i will take a closer look, thanks.
User avatar
blueznl
PureBasic Expert
PureBasic Expert
Posts: 6161
Joined: Sat May 17, 2003 11:31 am
Contact:

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

Post by blueznl »

It would be nice if a single page in the help file would list all required licenses, per command or command group.
( PB6.00 LTS Win11 x64 Asrock AB350 Pro4 Ryzen 5 3600 32GB GTX1060 6GB)
( The path to enlightenment and the PureBasic Survival Guide right here... )
Little John
Addict
Addict
Posts: 4519
Joined: Thu Jun 07, 2007 3:25 pm
Location: Berlin, Germany

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

Post by Little John »

The results of this code are impressive. Luis, thank you very much for this code!
luis wrote: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
5) This code can create 8 bit images in memory, while PB can only do this conversion when saving an image to disk.
:-)
User avatar
J. Baker
Addict
Addict
Posts: 2178
Joined: Sun Apr 27, 2003 8:12 am
Location: USA
Contact:

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

Post by J. Baker »

Little John wrote:The results of this code are impressive. Luis, thank you very much for this code!
luis wrote: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
5) This code can create 8 bit images in memory, while PB can only do this conversion when saving an image to disk.
:-)
EncodeImage(). ;)
www.posemotion.com

PureBasic Tools for OS X: PureMonitor, plist Tool, Data Maker & App Chef

Mac: 10.13.6 / 1.4GHz Core 2 Duo / 2GB DDR3 / Nvidia 320M
PC: Win 7 / AMD 64 4000+ / 3GB DDR / Nvidia 720GT


Even the vine knows it surroundings but the man with eyes does not.
Little John
Addict
Addict
Posts: 4519
Joined: Thu Jun 07, 2007 3:25 pm
Location: Berlin, Germany

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

Post by Little John »

J. Baker wrote:
Little John wrote:5) This code can create 8 bit images in memory, while PB can only do this conversion when saving an image to disk.
:-)
EncodeImage(). ;)
Oops. :oops:
Thanks for the correction.

Anyway, I previously didn't express myself precisely.
I'd like to convert any image to 8 bit in memory, and then read the color of each pixel of the image.

After converting an image to 8 bit with EncodeImage(), the image is in memory in PNG format, right? So I think in order to read the color of each pixel, I'd have to learn the details of the PNG format.

As far as I understood, with Luis' code the 8 bit image is in memory in BMP format. And I believe it's easier the read the colors of the pixels then. Is that right? 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 »

EncodeImage() store the image in memory in the format specified, but you can just do a CatchImage from the returned pointer to have the raw image data (decoded) in memory, you can then access it through DrawingBuffer()

Instead, I'm not sure how with EncodeImage() you are supposed to encode a 24/32 bit image to 8 bit (the conditions discussed here).
"Have you tried turning it off and on again ?"
A little PureBasic review
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4636
Joined: Sun Apr 12, 2009 6:27 am

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

Post by RASHAD »

Hi luis
He can encode a 24/32 bit image to 1,4 or 8 bit
But only valid for PNG format

Code: Select all

UsePNGImageDecoder()
UsePNGImageEncoder()

LoadImage(0,"g:\girl2.png")
*Buffer = EncodeImage(0,#PB_ImagePlugin_PNG,#PB_Image_FloydSteinberg,4)
CatchImage(1,*Buffer)
OpenWindow(0,0,0,ImageWidth(0),ImageHeight(0),"",#PB_Window_SystemMenu|#PB_Window_ScreenCentered)
ImageGadget(0,0,0,ImageWidth(0),ImageHeight(0),ImageID(1))
StartDrawing(ImageOutput(1))
  For x = 1 To 50 ;ImageWidth(0) - 1
     For y = 1  To 50 ;ImageHeight(0) - 1
        Debug Point(x,y)
     Next
  Next
StopDrawing()

SaveImage(1,"e:\test.bmp")
Repeat
  Select WaitWindowEvent()
      
       Case #PB_Event_CloseWindow
            Quit = 1
            
  EndSelect 

Until Quit = 1
End
Last edited by RASHAD on Sun May 05, 2013 8:05 pm, edited 1 time in total.
Egypt my love
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 »

@rashad

Thanks.

I was doing some experiments in the meantime, I tried to encode in memory to 8 bit with png but then catching it back was 24 bit again and that's why I was puzzled.

Then I checked the doc and saw loadimage/catchimage always promote back to 24/32 bit, something I had forgot.

So encodeimage can actually encode to 8 bit in memory, if specified, but when catched back it reverts to 24.
Anyway the quantization at that point has been already made, so the colors are correct (<= 256) even if not palettized anymore.
So you can work around all this stuff and make it work. Only thing it's not a 8 bit image anymore.

@little john

This should mean you can use encodeimage to quantize from 24 to 8 bits, and have a 8 bit png in memory.
If you want to access to the data, you have to catch it back and you will have a 24 bit image in memory with only 256 colors.
Do what you have to do to the image through drawingbuffer(), and then save the image to file as an 8 bit BMP or PNG (if that's what you want).

Or use this code if it makes it easier, don't know.
Last edited by luis on Sun May 05, 2013 8:07 pm, edited 1 time in total.
"Have you tried turning it off and on again ?"
A little PureBasic review
Post Reply