compresstest.bas 3.34 KB
Newer Older
kollo's avatar
kollo committed
1 2 3 4 5 6 7 8 9
' Test of the data compression routines       Oct 26  2005
' built in X11Basic by Markus Hoffmann
'
' Demonstration on how to use COMPRESS$(), UNCOMPRESS$()
'

' First we nedd a big file to be compressed

' bigfile$="/home/hoffmann/PI-Bonn/projekte/doktorarbeit/main.ps"
10
bigfile$=PARAM$(1)   ! This is this program itself
kollo's avatar
kollo committed
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

OPEN "I",#1,bigfile$
t$=INPUT$(#1,MIN(LOF(#1),1000000))   ! Max 1 Mbyte
CLOSE
PRINT bigfile$;" ";LEN(t$);" Bytes.",
FLUSH
t=TIMER
g$=COMPRESS$(t$)
PRINT STR$(TIMER-t,3,3);" sec. compressed down to ";LEN(g$);" bytes."
' show the first 32 Bytes of the compressed data
MEMDUMP VARPTR(g$),32
' now lets uncompress it again
n$=UNCOMPRESS$(g$)
PRINT "uncompressed it again to ";LEN(n$);" bytes."
' show the first 32 Bytes of the uncompressed data
MEMDUMP VARPTR(n$),64
PRINT
ff$=rle$(t$)
PRINT "after runlengh encoding: ";LEN(ff$);" bytes."
' show the first 32 Bytes of the compressed data
MEMDUMP VARPTR(ff$),64
' And now demystify the compression algorithm
ff$=bwte$(ff$)
PRINT "after burrow wheeler transformation: ";LEN(ff$);" bytes."
' show the first 32 Bytes of the compressed data
MEMDUMP VARPTR(ff$),64
ff$=mtfe$(ff$)
PRINT "after move-to-front encoding: ";LEN(ff$);" bytes."
' show the first 32 Bytes of the compressed data
MEMDUMP VARPTR(ff$),64
ff$=rle$(ff$)
PRINT "after another runlengh encoding: ";LEN(ff$);" bytes."
' show the first 32 Bytes of the compressed data
MEMDUMP VARPTR(ff$),64

ff$=arie$(ff$)
PRINT "after arithmetric encoding: ";LEN(ff$);" bytes."
' show the first 32 Bytes of the compressed data
MEMDUMP VARPTR(ff$),64
PRINT
' Now we can try to compress graphics:
'
CLEARW
COLOR COLOR_RGB(1,1,1)
PCIRCLE 100,100,100
COLOR COLOR_RGB(1,0,0)
TEXT 20,100,"This is a Test"
SHOWPAGE
GET 0,0,200,200,t$
60
PRINT "We got a portion of the screen: ";LEN(t$);" bytes."
kollo's avatar
kollo committed
61 62 63 64
PUT 300,200,t$
SHOWPAGE
PRINT "compressing takes a while..."
f$=compress$(t$)
65
PRINT "Compressed it to ";LEN(f$);" bytes. Thats only ";round(1000*len(f$)/len(t$))/10;"%."
kollo's avatar
kollo committed
66 67 68 69
' We can do also without arithmetric coding
ff$=rle$(mtfe$(bwte$(rle$(t$))))
' or without the burrow weeler trafo
fff$=arie$(mtfe$(rle$(t$)))
70 71 72 73
PRINT " original len:           ";LEN(t$);" Bytes."
PRINT "           compressed to ";LEN(f$);" Bytes."
PRINT "2. method: compressed to ";LEN(ff$);" Bytes."
PRINT "3. method: compressed to ";LEN(fff$);" Bytes."
kollo's avatar
kollo committed
74 75
PRINT "uncompressing is much faster..."
g$=UNCOMPRESS$(f$)
76
PRINT "Identical? (-1=Yes, 0=no) : ";g$=t$
kollo's avatar
kollo committed
77
' This is the proof:
78 79 80 81 82
PUT 200,200,g$
FOR i=0 TO 100 STEP 10
  PUT 0+i,300+i,g$
  SHOWPAGE
NEXT i
kollo's avatar
kollo committed
83 84 85 86

' Now some words concerning the burrow weeler transfor;

a$="The quick brown fox .... and so on ...################a"
87 88
PRINT a$,LEN(a$)
PRINT bwte$(a$),LEN(bwte$(a$))
kollo's avatar
kollo committed
89

90 91
PAUSE 10
QUIT
kollo's avatar
kollo committed
92 93
ttt:

94
FOR j=1 TO 100
kollo's avatar
kollo committed
95
  ll=j*1000
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
  PRINT "Build...",ll,
  FLUSH
  IF j=1
    t$=SPACE$(ll*4)
    FOR i=0 TO ll
      LPOKE varptr(t$)+4*i,i
    NEXT i
    hh$=t$
  ELSE
    t$=t$+hh$
  ENDIF
  PRINT len(t$),
  PRINT "COMPRESS... ",
  FLUSH
  t=timer
  g$=compress$(t$)
  PRINT str$(timer-t,3,3);" Sekunden. ",LEN(g$),
  PRINT "UNCOMPRESS... ",
  FLUSH
  t=timer
  t$=uncompress$(g$)
  PRINT str$(timer-t,3,3);" Sekunden. ",LEN(t$)
kollo's avatar
kollo committed
118

119 120
NEXT j
QUIT
kollo's avatar
kollo committed
121 122

a$="Hallo ich gehe noch nicht nach Hause ################a"
123 124 125
PRINT len(a$)
PRINT bwte$(a$)
PRINT bwtd$(bwte$(a$))
kollo's avatar
kollo committed
126 127

b$=compress$(a$)
128
PRINT len(b$)
kollo's avatar
kollo committed
129
c$=uncompress$(b$)
130 131 132 133 134 135 136
PRINT len(c$)
PRINT c$
FOR i=0 TO LEN(b$)-1
  PRINT peek(VARPTR(b$)+i);" ";
NEXT i
PRINT
PRINT "-----"
kollo's avatar
kollo committed
137