Page 3 of 4
Re: Quantize 1.03 (True Color to 8 bit) - Windows
Posted: Mon Aug 02, 2010 3:30 pm
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!
Re: Quantize 1.03 (True Color to 8 bit) - Windows
Posted: Wed Aug 04, 2010 3:43 pm
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.
Re: Quantize 1.03 (True Color to 8 bit) - Windows
Posted: Wed Aug 04, 2010 3:54 pm
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...
Re: Quantize 1.03 (True Color to 8 bit) - Windows
Posted: Wed Aug 04, 2010 3:54 pm
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

Re: Quantize 1.03 (True Color to 8 bit) - Windows
Posted: Wed Aug 04, 2010 3:57 pm
by luis
Hi c4s !
About the -1 value for the constant ? No I didn't, thank you, I will try that

Re: Quantize 1.03 (True Color to 8 bit) - Windows
Posted: Wed Aug 04, 2010 4:00 pm
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
Re: Quantize 1.03 (True Color to 8 bit) - Windows
Posted: Wed Aug 04, 2010 4:10 pm
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!
Re: Quantize 1.03 (True Color to 8 bit) - Windows
Posted: Wed Aug 04, 2010 4:12 pm
by Fred
It wasn't that clear in source header, i will take a closer look, thanks.
Re: Quantize 1.03 (True Color to 8 bit) - Windows
Posted: Wed Aug 04, 2010 8:30 pm
by blueznl
It would be nice if a single page in the help file would list all required licenses, per command or command group.
Re: Quantize 1.03 (True Color to 8 bit) - Windows
Posted: Sun May 05, 2013 4:22 pm
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.

Re: Quantize 1.03 (True Color to 8 bit) - Windows
Posted: Sun May 05, 2013 6:50 pm
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().

Re: Quantize 1.03 (True Color to 8 bit) - Windows
Posted: Sun May 05, 2013 7:09 pm
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.

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.
Re: Quantize 1.03 (True Color to 8 bit) - Windows
Posted: Sun May 05, 2013 7:17 pm
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).
Re: Quantize 1.03 (True Color to 8 bit) - Windows
Posted: Sun May 05, 2013 7:43 pm
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
Re: Quantize 1.03 (True Color to 8 bit) - Windows
Posted: Sun May 05, 2013 7:53 pm
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.