QB64 Phoenix Edition
A quick lesson on _DEFLATE and _INFLATE - Printable Version

+- QB64 Phoenix Edition (https://qb64phoenix.com/forum)
+-- Forum: Official Links (https://qb64phoenix.com/forum/forumdisplay.php?fid=16)
+--- Forum: Learning Resources and Archives (https://qb64phoenix.com/forum/forumdisplay.php?fid=13)
+--- Thread: A quick lesson on _DEFLATE and _INFLATE (/showthread.php?tid=441)



A quick lesson on _DEFLATE and _INFLATE - SMcNeill - 05-20-2022

Code: (Select All)
_CONTROLCHR OFF


_TITLE "Quick _DEFLATE Demo"
PRINT "First, let me give you a little story:"
PRINT
story$ = "The Boy Who Cried Wolf"
story$ = story$ + CHR$(13)
story$ = story$ + "A shepherd boy, who tended his flock not far from a village, used to amuse himself at times in crying out 'Wolf! Wolf!' Twice or thrice his trick succeeded; the whole village came running out to his assistance, when all the return they got was to be laughed at for their pains."
story$ = story$ + CHR$(13)
story$ = story$ + "At last one day the wolf came indeed. The boy cried out in earnest. His neighbors, supposing him to be at his old sport, paid no heed to his cries, and the wolf devoured the sheep. So the boy learned, when it was too late, that liars are not believed even when they tell the truth."
PRINT story$

PRINT
PRINT "Now, our story above is"; LEN(story$); "bytes long."
PRINT
COLOR 15
PRINT "But let's _DEFLATE it!"
deflated_story$ = _DEFLATE$(story$)
PRINT
PRINT deflated_story$
PRINT
PRINT "Doesn't look like much, now does it?  The only thing is, the deflated story is now only"; LEN(deflated_story$); "bytes long!"
SLEEP
CLS
PRINT "Now, in this case, the original was"; LEN(story$); "bytes"
PRINT "And the compressed version was"; LEN(deflated_story$); "bytes"
PRINT
PRINT "So there's not a ton of compression in this limited example, as our original data set was rather small to begin with.  But let's see what happens when we have a larger dataset to work with."
PRINT
FOR i = 1 TO 10
    story$ = story$ + CHR$(13) + story$
NEXT
PRINT "If you check the source code, you'll see that I've basically doubled the size of our story 10 times."
PRINT "It's now:"; LEN(story$); "bytes in size."
PRINT
COLOR 7
deflated_story$ = _DEFLATE$(story$)
PRINT "And now when I deflate this massive file, it reduces down to"; LEN(deflated_story$); "bytes in size!"
PRINT
PRINT
PRINT "From about 600,000 bytes being used in memory to about 4,000 bytes to store the same data.  That's less than 1/100th of the original size here!!"
PRINT
PRINT "So, if you wanted to send those 600,000 bytes across the internet, how would you rather send them?  600,00 bytes uncompressed, or 4,000 bytes compressed, and then let the end user uncompress them?? ;)"
SLEEP
CLS
COLOR 15
PRINT "Images take up a ton of space in memory.  A 1000x1000, 32-bit screen, uses 4,000,000 bytes of memory/storage."
PRINT "Yet, how many times have you ever downloaded a picture that's that large?"
PRINT
PRINT "Most images use _DEFLATE style compression on that image data, to store and transfer that image information."
PRINT "And then when you load it into memory, it uses _INFLATE to restore the image back to its original size and structure."
PRINT
PRINT "_DEFLATE compresses a string of data.  _INFLATE decompresses it."
PRINT "And that's about all there is to it.  ;)"


Note that I didn't try to wordwrap any of these lines or such, as this is a truly quick little demo, but I think it highlights and explains fairly well what _DEFLATE and _INFLATE do for us.  If anyone has any questions, feel free to ask them and I'll do my best to expand as wanted.  Wink