home *** CD-ROM | disk | FTP | other *** search
/ Der Mediaplex Sampler - Die 6 von Plex / 6_v_plex.zip / 6_v_plex / DISK5 / WIN_12 / IDEPIX2.ZIP / TIFFACS.ZIP / COMPRESS.ASM < prev    next >
Assembly Source File  |  1987-09-30  |  12KB  |  552 lines

  1. name    COMPRESS
  2.  
  3. ; This module made compatible to work with Microsoft C
  4. ; 1D Modified Huffman code compression technique
  5.  
  6. page    66,132
  7.  
  8. ;
  9. ;******************************************************************************
  10. ;* COMPRESS - Compress raw image data                          *
  11. ;*                                          *
  12. ;*    Input:    AH    Residual bit count                      *
  13. ;*        AL    Residual data byte                      *
  14. ;*        ES:DI    Address of source image data                  *
  15. ;*        ES:SI    Address to store compressed image data              *
  16. ;*        CX    Length of raw image data                  *
  17. ;*        DX    Maximum number of bits/scan line              *
  18. ;*                                          *
  19. ;*    Output: AH    Residual bit count                      *
  20. ;*        AL    Residual data byte                      *
  21. ;*        BX    If set, negative compression resulted              *
  22. ;*                                          *
  23. ;******************************************************************************
  24. ;
  25.  
  26. _compress_TEXT     SEGMENT BYTE PUBLIC 'CODE'
  27.     ASSUME    CS:_compress_TEXT,DS:_compress_TEXT
  28.  
  29.         PUBLIC    _COMPRESS
  30.  
  31. SRC_PTR DW    ?
  32. DST_PTR DW    ?
  33. DST_LEN DW    ?
  34. COMP_DS DW    ?        ; 9/12 VJ
  35.  
  36. _COMPRESS    PROC
  37.         PUSH    DS
  38.         mov    cs:comp_ds,ds    ; 9/12 vj
  39.         PUSH    CS
  40.         POP    DS
  41.         MOV    cs:DST_LEN,0
  42.         MOV    cs:SRC_PTR,DI
  43.         MOV    cs:DST_PTR,SI
  44.         JCXZ    COMPRESS7
  45.         MOV    BX,CX
  46.         SHL    BX,1
  47.         SHL    BX,1
  48.         SHL    BX,1        ; total pixel count
  49. COMPRESS1:    CALL    WHITE_RUN
  50.         SUB    BX,SI
  51.         CMP    SI,64
  52.         JL    COMPRESS2
  53.         PUSH    SI
  54.         SUB    SI,64
  55.         AND    SI,0FFC0H
  56.         SHR    SI,1
  57.         SHR    SI,1
  58.         SHR    SI,1
  59.         SHR    SI,1
  60.         ADD    SI,offset(WHITE_MC)
  61.         CALL    OUTPATTERN
  62.         JC    COMPRESS10
  63.         POP    SI
  64.         AND    SI,003FH
  65. COMPRESS2:    SHL    SI,1
  66.         SHL    SI,1
  67.         ADD    SI,offset(WHITE_TC)
  68.         CALL    OUTPATTERN
  69.         JC    COMPRESS10
  70.         CMP    BX,0
  71.         JZ    COMPRESS7
  72.         CALL    BLACK_RUN
  73.         SUB    BX,SI
  74.         CMP    SI,64
  75.         JL    COMPRESS3
  76.         PUSH    SI
  77.         SUB    SI,64
  78.         AND    SI,0FFC0H
  79.         SHR    SI,1
  80.         SHR    SI,1
  81.         SHR    SI,1
  82.         SHR    SI,1
  83.         ADD    SI,offset(BLACK_MC)
  84.         CALL    OUTPATTERN
  85.         JC    COMPRESS10
  86.         POP    SI
  87.         AND    SI,003FH
  88. COMPRESS3:    SHL    SI,1
  89.         SHL    SI,1
  90.         ADD    SI,offset(BLACK_TC)
  91.         CALL    OUTPATTERN
  92.         JC    COMPRESS10
  93.         CMP    BX,0
  94.         JNZ    COMPRESS1
  95. ;COMPRESS7:     CMP     DX,WORD PTR [EOL+0]
  96. ;         JL     COMPRESS10
  97. COMPRESS7:
  98. COMPRESS9:                   ; LEA     SI,EOL
  99.                        ; CALL    OUTPATTERN
  100.         POP    DS
  101.         MOV    CX,cs:DST_LEN
  102.         MOV    SI,cs:DST_PTR
  103.         MOV    BX,0
  104.         RET
  105. COMPRESS10:    POP    DS
  106.         MOV    CX,cs:DST_LEN
  107.         MOV    SI,cs:DST_PTR
  108.         MOV    BX,1
  109.         RET
  110. _COMPRESS    ENDP
  111.  
  112. page
  113. ;***************************************************************************
  114. ;
  115. OUTPATTERN    PROC
  116.         PUSH    CX
  117.         MOV    CX,word ptr[SI]     ; Pattern length
  118.         JCXZ    OUTPATTERN3
  119.         MOV    SI,word ptr[SI+2]    ; Pattern
  120. OUTPATTERN1:    SHL    SI,1
  121.         RCL    AL,1
  122.         INC    AH
  123.         CMP    AH,8
  124.         JL    OUTPATTERN2
  125.         PUSH    DI
  126.         MOV    DI,cs:DST_PTR
  127.  
  128. ;new addition 9/12 vj
  129.  
  130.         push    es
  131.         push    cx
  132.         mov    cx,cs:comp_ds
  133.         mov    es,cx
  134. ;end
  135.         MOV    ES:[DI],AL
  136.  
  137. ;new 9/12 vj
  138.         pop    cx
  139.         pop    es
  140. ;end
  141.         INC    cs:DST_PTR
  142.         INC    cs:DST_LEN
  143.         POP    DI
  144.         MOV    AX,0
  145. OUTPATTERN2:    DEC    DX
  146. ;        JZ    OUTPATTERN4
  147.         LOOP    OUTPATTERN1
  148. OUTPATTERN3:    POP    CX
  149.         CLC
  150.         RET
  151. OUTPATTERN4:    POP    CX
  152.         STC
  153.         RET
  154. OUTPATTERN    ENDP
  155.  
  156. page
  157. ;***************************************************************************
  158. ;
  159. WHITE_RUN    PROC
  160.         PUSH    AX            ; residue count/byte
  161.         PUSH    BX            ; output bit count
  162.         PUSH    DX            ; minimum bit output length
  163.         MOV    SI,0            ; run length
  164.         AND    BX,0007H
  165.         JZ    WHITE_RUN2
  166.         PUSH    CX
  167.         MOV    AL,ES:[DI]
  168.         MOV    CX,8
  169.         SUB    CX,BX
  170.         SHL    AL,CL
  171.         POP    CX
  172.         JNZ    WHITE_RUN1
  173.         ADD    SI,BX
  174.         INC    DI
  175.         DEC    CX
  176.         JMP    short WHITE_RUN2
  177. WHITE_RUN1:    SHL    AL,1
  178.         JC    WHITE_RUN6
  179.         INC    SI
  180.         JMP    short WHITE_RUN1
  181. WHITE_RUN2:    JCXZ    WHITE_RUN6
  182.         MOV    BX,DI
  183.         MOV    AL,00H
  184.         CLD
  185.         REPE SCASB        ; scan for non-white
  186.         JZ    WHITE_RUN5    ; jump if no black found
  187.         DEC    DI
  188.         INC    CX
  189.         MOV    AL,byte ptr(ES:[DI])
  190. WHITE_RUN4:    SHL    AL,1
  191.         JC    WHITE_RUN5
  192.         INC    SI
  193.         JMP    short WHITE_RUN4
  194. WHITE_RUN5:    SUB    BX,DI
  195.         NEG    BX
  196.         SHL    BX,1
  197.         SHL    BX,1
  198.         SHL    BX,1
  199.         ADD    SI,BX
  200. WHITE_RUN6:    POP    DX
  201.         POP    BX
  202.         POP    AX
  203.         RET
  204. WHITE_RUN    ENDP
  205.  
  206. page
  207. ;***************************************************************************
  208. ;
  209. BLACK_RUN    PROC
  210.         PUSH    AX            ; residue count/byte
  211.         PUSH    BX            ; output bit count
  212.         PUSH    DX            ; minimum bit output length
  213.         MOV    SI,0            ; run length
  214.         AND    BX,0007H
  215.         JZ    BLACK_RUN3
  216.         PUSH    CX
  217.         MOV    AL,byte ptr(ES:[DI])
  218.         MOV    CX,8
  219.         SUB    CX,BX
  220.         SHL    AL,CL
  221.         POP    CX
  222. BLACK_RUN1:    SHL    AL,1
  223.         JNC    BLACK_RUN2
  224.         INC    SI
  225.         JMP    short BLACK_RUN1
  226. BLACK_RUN2:    CMP    SI,BX
  227.         JNZ    BLACK_RUN6
  228.         INC    DI
  229.         DEC    CX
  230. BLACK_RUN3:    JCXZ    BLACK_RUN6
  231.         MOV    BX,DI
  232.         MOV    AL,0FFH
  233.         CLD
  234.         REPE SCASB        ; scan for non-BLACK
  235.         JZ    BLACK_RUN5    ; jump if no black found
  236.         DEC    DI
  237.         INC    CX
  238.         MOV    AL,byte ptr(ES:[DI])
  239. BLACK_RUN4:    SHL    AL,1
  240.         JNC    BLACK_RUN5
  241.         INC    SI
  242.         JMP    short BLACK_RUN4
  243. BLACK_RUN5:    SUB    BX,DI
  244.         NEG    BX
  245.         SHL    BX,1
  246.         SHL    BX,1
  247.         SHL    BX,1
  248.         ADD    SI,BX
  249. BLACK_RUN6:    POP    DX
  250.         POP    BX
  251.         POP    AX
  252.         RET
  253. BLACK_RUN    ENDP
  254.  
  255. page
  256. ;***************************************************************************
  257. ; Compression Tables
  258. ;
  259. TC        MACRO    runlength,codelength,pattern
  260.         IF (codelength EQ 0)
  261.         DW    codelength,0000000000000000B
  262.         ENDIF
  263.         IF (codelength EQ 1)
  264.         DW    codelength,pattern&000000000000000B
  265.         ENDIF
  266.         IF (codelength EQ 2)
  267.         DW    codelength,pattern&00000000000000B
  268.         ENDIF
  269.         IF (codelength EQ 3)
  270.         DW    codelength,pattern&0000000000000B
  271.         ENDIF
  272.         IF (codelength EQ 4)
  273.         DW    codelength,pattern&000000000000B
  274.         ENDIF
  275.         IF (codelength EQ 5)
  276.         DW    codelength,pattern&00000000000B
  277.         ENDIF
  278.         IF (codelength EQ 6)
  279.         DW    codelength,pattern&0000000000B
  280.         ENDIF
  281.         IF (codelength EQ 7)
  282.         DW    codelength,pattern&000000000B
  283.         ENDIF
  284.         IF (codelength EQ 8)
  285.         DW    codelength,pattern&00000000B
  286.         ENDIF
  287.         IF (codelength EQ 9)
  288.         DW    codelength,pattern&0000000B
  289.         ENDIF
  290.         IF (codelength EQ 10)
  291.         DW    codelength,pattern&000000B
  292.         ENDIF
  293.         IF (codelength EQ 11)
  294.         DW    codelength,pattern&00000B
  295.         ENDIF
  296.         IF (codelength EQ 12)
  297.         DW    codelength,pattern&0000B
  298.         ENDIF
  299.         IF (codelength EQ 13)
  300.         DW    codelength,pattern&000B
  301.         ENDIF
  302.         IF (codelength EQ 14)
  303.         DW    codelength,pattern&00B
  304.         ENDIF
  305.         IF (codelength EQ 15)
  306.         DW    codelength,pattern&0B
  307.         ENDIF
  308.         IF (codelength EQ 16)
  309.         DW    codelength,pattern&B
  310.         ENDIF
  311.         ENDM
  312.  
  313. page
  314. ;***************************************************************************
  315. ;
  316. WHITE_TC    LABEL    WORD        ; Termination codes for white runs
  317.         TC     0, 8,00110101
  318.         TC     1, 6,000111
  319.         TC     2, 4,0111
  320.         TC     3, 4,1000
  321.         TC     4, 4,1011
  322.         TC     5, 4,1100
  323.         TC     6, 4,1110
  324.         TC     7, 4,1111
  325.         TC     8, 5,10011
  326.         TC     9, 5,10100
  327.         TC    10, 5,00111
  328.         TC    11, 5,01000
  329.         TC    12, 6,001000
  330.         TC    13, 6,000011
  331.         TC    14, 6,110100
  332.         TC    15, 6,110101
  333.         TC    16, 6,101010
  334.         TC    17, 6,101011
  335.         TC    18, 7,0100111
  336.         TC    19, 7,0001100
  337.         TC    20, 7,0001000
  338.         TC    21, 7,0010111
  339.         TC    22, 7,0000011
  340.         TC    23, 7,0000100
  341.         TC    24, 7,0101000
  342.         TC    25, 7,0101011
  343.         TC    26, 7,0010011
  344.         TC    27, 7,0100100
  345.         TC    28, 7,0011000
  346.         TC    29, 8,00000010
  347.         TC    30, 8,00000011
  348.         TC    31, 8,00011010
  349.         TC    32, 8,00011011
  350.         TC    33, 8,00010010
  351.         TC    34, 8,00010011
  352.         TC    35, 8,00010100
  353.         TC    36, 8,00010101
  354.         TC    37, 8,00010110
  355.         TC    38, 8,00010111
  356.         TC    39, 8,00101000
  357.         TC    40, 8,00101001
  358.         TC    41, 8,00101010
  359.         TC    42, 8,00101011
  360.         TC    43, 8,00101100
  361.         TC    44, 8,00101101
  362.         TC    45, 8,00000100
  363.         TC    46, 8,00000101
  364.         TC    47, 8,00001010
  365.         TC    48, 8,00001011
  366.         TC    49, 8,01010010
  367.         TC    50, 8,01010011
  368.         TC    51, 8,01010100
  369.         TC    52, 8,01010101
  370.         TC    53, 8,00100100
  371.         TC    54, 8,00100101
  372.         TC    55, 8,01011000
  373.         TC    56, 8,01011001
  374.         TC    57, 8,01011010
  375.         TC    58, 8,01011011
  376.         TC    59, 8,01001010
  377.         TC    60, 8,01001011
  378.         TC    61, 8,00110010
  379.         TC    62, 8,00110011
  380.         TC    63, 8,00110100
  381.  
  382. page
  383. ;***************************************************************************
  384. ;
  385. WHITE_MC    LABEL    WORD        ; Make-up codes for white runs
  386.         TC      64, 5,11011
  387.         TC     128, 5,10010
  388.         TC     192, 6,010111
  389.         TC     256, 7,0110111
  390.         TC     320, 8,00110110
  391.         TC     384, 8,00110111
  392.         TC     448, 8,01100100
  393.         TC     512, 8,01100101
  394.         TC     576, 8,01101000
  395.         TC     640, 8,01100111
  396.         TC     704, 9,011001100
  397.         TC     768, 9,011001101
  398.         TC     832, 9,011010010
  399.         TC     896, 9,011010011
  400.         TC     960, 9,011010100
  401.         TC    1024, 9,011010101
  402.         TC    1088, 9,011010110
  403.         TC    1152, 9,011010111
  404.         TC    1216, 9,011011000
  405.         TC    1280, 9,011011001
  406.         TC    1344, 9,011011010
  407.         TC    1408, 9,011011011
  408.         TC    1472, 9,010011000
  409.         TC    1536, 9,010011001
  410.         TC    1600, 9,010011010
  411.         TC    1664, 6,011000
  412.         TC    1728, 9,010011011
  413.         TC    1792,11,00000001000
  414.         TC    1856,11,00000001100
  415.         TC    1920,11,00000001101
  416.         TC    1984,12,000000010010
  417.         TC    2048,12,000000010011
  418.         TC    2112,12,000000010100
  419.         TC    2176,12,000000010101
  420.         TC    2240,12,000000010110
  421.         TC    2304,12,000000010111
  422.         TC    2368,12,000000011100
  423.         TC    2432,12,000000011101
  424.         TC    2496,12,000000011110
  425.         TC    2560,12,000000011111
  426.  
  427. page
  428. ;***************************************************************************
  429. ;
  430. BLACK_TC    LABEL    WORD        ; termination codes for black runs
  431.         TC     0,10,0000110111
  432.         TC     1, 3,010
  433.         TC     2, 2,11
  434.         TC     3, 2,10
  435.         TC     4, 3,011
  436.         TC     5, 4,0011
  437.         TC     6, 4,0010
  438.         TC     7, 5,00011
  439.         TC     8, 6,000101
  440.         TC     9, 6,000100
  441.         TC    10, 7,0000100
  442.         TC    11, 7,0000101
  443.         TC    12, 7,0000111
  444.         TC    13, 8,00000100
  445.         TC    14, 8,00000111
  446.         TC    15, 9,000011000
  447.         TC    16,10,0000010111
  448.         TC    17,10,0000011000
  449.         TC    18,10,0000001000
  450.         TC    19,11,00001100111
  451.         TC    20,11,00001101000
  452.         TC    21,11,00001101100
  453.         TC    22,11,00000110111
  454.         TC    23,11,00000101000
  455.         TC    24,11,00000010111
  456.         TC    25,11,00000011000
  457.         TC    26,12,000011001010
  458.         TC    27,12,000011001011
  459.         TC    28,12,000011001100
  460.         TC    29,12,000011001101
  461.         TC    30,12,000001101000
  462.         TC    31,12,000001101001
  463.         TC    32,12,000001101010
  464.         TC    33,12,000001101011
  465.         TC    34,12,000011010010
  466.         TC    35,12,000011010011
  467.         TC    36,12,000011010100
  468.         TC    37,12,000011010101
  469.         TC    38,12,000011010110
  470.         TC    39,12,000011010111
  471.         TC    40,12,000001101100
  472.         TC    41,12,000001101101
  473.         TC    42,12,000011011010
  474.         TC    43,12,000011011011
  475.         TC    44,12,000001010100
  476.         TC    45,12,000001010101
  477.         TC    46,12,000001010110
  478.         TC    47,12,000001010111
  479.         TC    48,12,000001100100
  480.         TC    49,12,000001100101
  481.         TC    50,12,000001010010
  482.         TC    51,12,000001010011
  483.         TC    52,12,000000100100
  484.         TC    53,12,000000110111
  485.         TC    54,12,000000111000
  486.         TC    55,12,000000100111
  487.         TC    56,12,000000101000
  488.         TC    57,12,000001011000
  489.         TC    58,12,000001011001
  490.         TC    59,12,000000101011
  491.         TC    60,12,000000101100
  492.         TC    61,12,000001011010
  493.         TC    62,12,000001100110
  494.         TC    63,12,000001100111
  495.  
  496. page
  497. ;***************************************************************************
  498. ;
  499. BLACK_MC    LABEL    WORD        ; Make-up codes for black runs
  500.         TC      64,10,0000001111
  501.         TC     128,12,000011001000
  502.         TC     192,12,000011001001
  503.         TC     256,12,000001011011
  504.         TC     320,12,000000110011
  505.         TC     384,12,000000110100
  506.         TC     448,12,000000110101
  507.         TC     512,13,0000001101100
  508.         TC     576,13,0000001101101
  509.         TC     640,13,0000001001010
  510.         TC     704,13,0000001001011
  511.         TC     768,13,0000001001100
  512.         TC     832,13,0000001001101
  513.         TC     896,13,0000001110010
  514.         TC     960,13,0000001110011
  515.         TC    1024,13,0000001110100
  516.         TC    1088,13,0000001110101
  517.         TC    1152,13,0000001110110
  518.         TC    1216,13,0000001110111
  519.         TC    1280,13,0000001010010
  520.         TC    1344,13,0000001010011
  521.         TC    1408,13,0000001010100
  522.         TC    1472,13,0000001010101
  523.         TC    1536,13,0000001011010
  524.         TC    1600,13,0000001011011
  525.         TC    1664,13,0000001100100
  526.         TC    1728,13,0000001100101
  527.         TC    1792,11,00000001000
  528.         TC    1856,11,00000001100
  529.         TC    1920,11,00000001101
  530.         TC    1984,12,000000010010
  531.         TC    2048,12,000000010011
  532.         TC    2112,12,000000010100
  533.         TC    2176,12,000000010101
  534.         TC    2240,12,000000010110
  535.         TC    2304,12,000000010111
  536.         TC    2368,12,000000011100
  537.         TC    2432,12,000000011101
  538.         TC    2496,12,000000011110
  539.         TC    2560,12,000000011111
  540.  
  541. page
  542. ;***************************************************************************
  543. ;
  544. EOL        LABEL    WORD
  545.         TC    EOL,12,000000000001
  546.  
  547. FILL        LABEL    WORD
  548.         TC    FILL,1,0
  549.  
  550. _compress_TEXT         ENDS
  551.         END
  552.