Codesnippets > Code Snippets
Compressing a file/array by saving as PNG
Wampus:
Its possible to use the compression method used when saving PNG files to compress pretty much anything. Attached to this post is some code which does just that. Works nicely for large files. For smaller files the advantage diminishes since saving array data as a PNG file will add ~800 bytes of data.
The functions:-
LoadDataSprite( filename$ )
Loads a file into the array datasprite%[]
SaveDataSprite( filename$ )
Saves a file from the data contained in the array datasprite%[]
DeflateDataSprite (filename$ )
Compresses and saves the data in array datasprite%[]
InflateDataSprite ( filename$ )
Loads and uncompresses a file and puts the data into datasprite%[]
ClearDataSprite()
Clears the array datasprite%[] and size register
[attachment deleted by admin]
Wampus:
n/p if I could find a way to get GLBasic to turn off saving superfluous palette info when saving PNG files then that reduces every file by 780 bytes. I know there is an option in pnglib to turn off palette saving for RGB files so maybe there is a way to override default GLBasic using that.
Moru:
Wow, great idea, thanks!
Kitty Hello:
Blimey, is this an awesome idea.
I'll try to cut the palette out. Do you know the parameter off your mind?
Wampus:
Cool! Without the palette files would be real small. :)
I'm not sure how to cut the palette out completely but I have a couple of guesses. The official documentation for libpng says the palette (PLTE chunk) is optional for truecolor images with alpha but doesn't say what parameter to change when writing a file! In the source code I found a PNG_NO_READ_OPT_PLTE defined to prevent reading unnecessary optional PLTE data. I couldn't find the equivalent for turning off writing the optional palette.
In the source code it may be that color_type parameter being set to PNG_COLOR_TYPE_RGB_ALPHA is all that's needed. It is probably more complicated than that.
If the PLTE chunk can't be switched off in some way, maybe it can be reduced. Currently GLBASIC saves the full 256 optional palette. If the number of palette entries could be reduced to 0, 1 or 2 the PLTE chunk might still appear in file but only take up a few bytes instead of 780. There is a num_palette parameter I believe.
Navigation
[0] Message Index
[#] Next page
Go to full version