mainwin 40 15
' G4 Compression calling libtiff3.dll, by Pablo Nieto (Editorial Ssifo)
' Matches the well-known CCITTFaxDecode      April 2007
' Also known as Huffman MMR
' Warning:  runnig this program a second time inside the same instance
' of Liberty Basic gives "Run-time error! ... abnormal program termination"
' Keep one mainwin running to avoid this.
' The problem is related to msvcrt.dll, called by libtiff3.dll:
' it keeps track of some pointers in memory.
' This program runs under W98. Others not tested
ceros$="": for i=0 to 23: ceros$=ceros$+chr$(0): next
OPEN "kernel32.dll" for dll as #kernel
OPEN "libtiff3.dll" for dll as #tiff
'    codec struct embedded in tiff struct for easier debugging
struct tiff,  unused0 as char[460], codec as char[8], _
              rowbytes as long, rowpixels as long, _
              unused1 as char[12], codecptr as long, _
              unused2 as char[8], streamstart as long, _
              bignum as long, curstreamptr as long, _
              rawcc as long, acumbits as long, pendbits as long, _
              unused3 as char[16], unusedwrite as long, _
              unusedseek as long, ptr2refline as long, _
              ptr2origin as long, ptr2stream as long, _
              xyz as char[4], _
     refline as char[24], origin as char[24], stream as char[24]
OPEN "memor.dll" for dll as #memor
CALLDLL #memor, "VarPtr", tiff as ptr, tifptr as ulong
   'proc VarPtr (in memor.dll)
   '    mov eax, [esp+4] ; dereference the given dword pointer
   '    ret 4            ; return the result
   ' Inaccessible functions in libtiff because they lack entry point
absol=hexdec("6fd8edf0")      'fax4encode ,3,0
    'absol=hexdec("6fd90120") 'fax3dencode2drow ref, 24
  calldll #memor,"Peek4",absol as ulong, resu as ulong
'proc Peek4   (in memor.dll)
'    mov     ecx,[esp+4] ; ecx := addr from
'    mov     eax,[ecx]   ; eax := DWORD
'    ret     4
code$=dechex$(resu)
' Check code contents in libtiff
  print "Code in libtiff3.dll : ";code$ ' 53565755 =OK reverso
'OPEN "libtiff3.dll" for dll as #tiff
if code$<>"53565755" then print "Version mismatch" : goto [fin]
tiff.codecptr.struct=tifptr+460
tiff.rowbytes.struct=3
tiff.rowpixels.struct=24
tiff.pendbits.struct=8
tiff.acumbits.struct=0
tiff.bignum.struct=hexdec("888888")
tiff.rawcc.struct=0
tiff.ptr2refline.struct=tifptr+564 'pointer to refline start
tiff.ptr2origin.struct=tifptr+564+24 '+refline length
tiff.ptr2stream.struct=tifptr+564+24+24 '++origin length
tiff.streamstart.struct=tifptr+564+24+24
tiff.curstreamptr.struct=tifptr+564+24+24 
tiff.stream.struct=space$(24)+chr$(0)+chr$(0)+chr$(0)
tiff.origin.struct="f0"+chr$(15)+"@q"
' Typical example in Internet for CCITTFax4Encode
' binary input: 0110 0110 0011 0000 0000 1111  row 1
'               0100 0000 0111 0000 1110 0000  row 2
print "ASCII equivalent of binary input: ";tiff.origin.struct

print "tiff-codec struct BEFORE encoding:" : gosub [codec]

tiff.refline.struct=ceros$ : todo=6 : ori=tifptr+588

gosub [getg4] ' keep the sub there, don't place here its contents

if resu=1 then print "Encoding OK"
print "tiff-codec struct AFTER encoding:" : gosub [codec]

stream$=tiff.stream.struct : coded=tiff.rawcc.struct
pend=tiff.pendbits.struct : stream$=left$(stream$,coded)
if pend<>8 then stream$=stream$+chr$(tiff.acumbits.struct) : coded=coded+1
print "ascii stream=";stream$
print "hex stream=";
for i=1 to coded : print dechex$(asc(mid$(stream$,i,1)));" "; : next
eol$=mid$("00 20 0200 40 0400 80 0801 00 1002 00 2004 00 4008 00 8000 10 01",pend*8-7,8)
acu=tiff.acumbits.struct : pend=tiff.pendbits.struct
print: print "      EOLs: ";eol$

[fin]
print "finished"
close #memor
close #kernel
close #tiff
end

[codec]
restore 100
for i=0 to 43
if i mod 4 = 0 then print : read expl$ : print expl$
desde=tifptr+i*4+460
CALLDLL #memor, "Peek4", desde as ulong, resu as ulong
re1$=dechex$(resu):le$=right$("0000000",8-len(re1$))
res$=le$+re1$
print res$;"   ";
next
print
return

100
data "                      rowbytes  rowpixels"
data "                                codecptr"
data "                    streamstart bignum"
data "streamend streamlen    acumbits pendbits"
data ""
data "                   ptr2refline ptr2origin"
data "ptr2stream            r-e-f-l-i-n-e"
data " 0-0-0"
data "origin"
data "                      s-t-r-e-a-m"
data ""
data ""
data ""

[getg4]
  calldll #memor,"CallAbs", absol as ulong, tifptr as ulong, _
             ori as ulong, todo as ulong, 0 as ulong, resu as ulong
return

'proc CallAbs  ; calls external binary sub ended with RET
'       pop eax   ; provided that the stack reverts unchanged
'       xchg eax,[esp] ; and register ebp must return unchanged
'       push eax    ; exchanges the values of the 2 latest in the stack
'       ret      (in memor.dll)

' PRINTOUT IN MAINWIN :
'code in libtiff3.dll : 53565755
'ASCII equivalent of binary input: f0@q
' if you remove this sub, the struct gets spurious garbage
'codec ascii=
'codecptr 14959196  E4425C
'rowb,px       3             24
'tiff-codec struct BEFORE encoding:
'
'                      rowbytes  rowpixels
'00000000   00000000   00000003   00000018
'                                codecptr
'00000000   00000000   00000000   00E4425C
'                    streamstart bignum
'00000000   00000000   00E442F4   00888888
'streamend streamlen    acumbits pendbits
'00E442F4   00000000   00000000   00000008
'
'00000000   00000000   00000000   00000000
'                   ptr2refline ptr2origin
'00000000   00000000   00E442C6   00E442DC
'ptr2stream            r-e-f-l-i-n-e
'00E442F4   00000000   00000000   00000000
' 0-0-0
'00000000   00000000   00000000   00000000
'origin
'400F3066   0000E071   00000000   00000000
'                      s-t-r-e-a-m
'00000000   00000000   20202020   20202020
'
'20202020   20202020   20202020   00202020
'Encoding OK
'tiff-codec struct AFTER encoding:
'
'                      rowbytes  rowpixels
'00000000   00000000   00000003   00000018
'                                codecptr
'00000000   00000000   00000000   00E4425C
'                    streamstart bignum
'00000000   00000000   00E442F4   00888888
'streamend streamlen    acumbits pendbits
'00E442FD   00000009   0000006E   00000001
'
'00000000   00000000   00000000   00000000
'                   ptr2refline ptr2origin
'00000000   00000000   00E442C6   00E442DC
'ptr2stream            r-e-f-l-i-n-e
'00E442F4   00000000   71400000   000000E0
' 0-0-0
'00000000   00000000   00000000   00000000
'origin
'400F3066   0000E071   00000000   00000000
'                      s-t-r-e-a-m
'00000000   00000000   19F3E523   0C53A19B
'
'202020E0   20202020   20202020   00202020
'ascii stream=#Sn
'hex stream=23 E5 F3 19 9B A1 53 C E0 6E
'      EOLs: 00 20 02
'finished

Pagina principal