-
Notifications
You must be signed in to change notification settings - Fork 3
/
Deflate.bas
69 lines (57 loc) · 2.92 KB
/
Deflate.bas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
'-----------------------------------------------------------------------------------------------------------------------
' _INFLATE$ compatible high-efficiency compression library for QB64-PE
' Copyright (c) 2024 Samuel Gomes
'-----------------------------------------------------------------------------------------------------------------------
$INCLUDEONCE
'$INCLUDE:'Deflate.bi'
'-----------------------------------------------------------------------------------------------------------------------
' TEST CODE
'-----------------------------------------------------------------------------------------------------------------------
'$CONSOLE:ONLY
'DIM a AS STRING: a = "The quick brown fox jumps over the lazy dog. "
'PRINT "Original string (a): "; a
'DIM i AS LONG: FOR i = 1 TO 15
' a = a + a
'NEXT
'PRINT "After concatenating it into itself several times, LEN(a) ="; LEN(a)
'DIM AS DOUBLE t: t = TIMER(.001)
'PRINT "_INFLATE + _DEFLATE:"
'DIM b AS STRING: b = _DEFLATE$(a)
'PRINT "After using _DEFLATE with default compression level to compress it, LEN(b) ="; LEN(b)
'PRINT USING "(compressed size is #.###% of the original)"; ((LEN(b) * 100) / LEN(a))
'DIM c AS STRING: c = _INFLATE$(b, LEN(a))
'PRINT "After using _INFLATE to decompress it, LEN(c) ="; LEN(c)
'IF a = c THEN PRINT "Passed!" ELSE PRINT "Failed!"
'PRINT USING "Time taken: ########.####"; TIMER(.001) - t
'PRINT
't = TIMER(.001)
'PRINT "DeflateZopfli + _DEFLATE:"
'b = DeflatePro(a, 0)
'PRINT "After using DeflateZopfli with default compression level to compress it, LEN(b) ="; LEN(b)
'PRINT USING "(compressed size is #.###% of the original)"; ((LEN(b) * 100) / LEN(a))
'c = _INFLATE$(b, LEN(a))
'PRINT "After using _INFLATE$ to decompress it, LEN(c) ="; LEN(c)
'IF a = c THEN PRINT "Passed!" ELSE PRINT "Failed!"
'PRINT USING "Time taken: ########.####"; TIMER(.001) - t
'PRINT
'END
'-----------------------------------------------------------------------------------------------------------------------
' This uses Zopfli to compress the buffer using the Deflate method
' The buffer can then be decompressed using QB64's INFLATE$ command
' compressionLevel can be 0 - 65535. 65535 is the highest compression level and 0 uses the library default setting
FUNCTION DeflatePro$ (inputBuffer AS STRING, compressionLevel AS _UNSIGNED INTEGER)
DIM AS _UNSIGNED _OFFSET outputPtr, outputSize
' Call the internal compression routine
__Zopfli_Compress compressionLevel, inputBuffer, LEN(inputBuffer), outputPtr, outputSize
IF outputPtr <> NULL THEN ' only if the compression succeeded
' Copy the compressed memory
IF outputSize > 0 THEN
' Allocate memory to copy the compressed buffer
DIM outputBuffer AS STRING: outputBuffer = STRING$(outputSize, NULL)
CopyMemory _OFFSET(outputBuffer), outputPtr, outputSize
END IF
' Free outputPtr
FreeMemory outputPtr
DeflatePro = outputBuffer
END IF
END FUNCTION