Subversion Repositories Kolibri OS

Rev

Rev 2397 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

  1. ;;================================================================================================;;
  2. ;;//// tiff.asm //// (c) dunkaist, 2011-2012 /////////////////////////////////////////////////////;;
  3. ;;================================================================================================;;
  4. ;;                                                                                                ;;
  5. ;; This file is part of Common development libraries (Libs-Dev).                                  ;;
  6. ;;                                                                                                ;;
  7. ;; Libs-Dev is free software: you can redistribute it and/or modify it under the terms of the GNU ;;
  8. ;; Lesser General Public License as published by the Free Software Foundation, either version 2.1 ;;
  9. ;; of the License, or (at your option) any later version.                                         ;;
  10. ;;                                                                                                ;;
  11. ;; Libs-Dev is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without  ;;
  12. ;; even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU  ;;
  13. ;; Lesser General Public License for more details.                                                ;;
  14. ;;                                                                                                ;;
  15. ;; You should have received a copy of the GNU Lesser General Public License along with Libs-Dev.  ;;
  16. ;; If not, see <http://www.gnu.org/licenses/>.                                                    ;;
  17. ;;                                                                                                ;;
  18. ;;================================================================================================;;
  19.  
  20. include 'tiff.inc'
  21. ;include '../../../../../system/board/trunk/debug.inc'
  22.  
  23. ;;================================================================================================;;
  24. proc img.is.tiff _data, _length ;/////////////////////////////////////////////////////////////////;;
  25. ;;------------------------------------------------------------------------------------------------;;
  26. ;? Determine if raw data could be decoded (is in tiff format)                                     ;;
  27. ;;------------------------------------------------------------------------------------------------;;
  28. ;> _data = raw data as read from file/stream                                                      ;;
  29. ;> _length = data length                                                                          ;;
  30. ;;------------------------------------------------------------------------------------------------;;
  31. ;< eax = false / true                                                                             ;;
  32. ;;================================================================================================;;
  33.  
  34.         push    esi
  35.  
  36.         mov     esi, [_data]
  37.         lodsw
  38.         cmp     ax, word 'II'
  39.         je      .little_endian
  40.         cmp     ax, word 'MM'
  41.         je      .big_endian
  42.         jmp     .is_not_tiff
  43.  
  44.   .little_endian:
  45.         lodsw
  46.         cmp     ax, 0x002A
  47.         je      .is_tiff
  48.         jmp     .is_not_tiff
  49.  
  50.   .big_endian:
  51.         lodsw
  52.         cmp     ax, 0x2A00
  53.         je      .is_tiff
  54.  
  55.   .is_not_tiff:
  56.         pop     esi
  57.         xor     eax, eax
  58.         ret
  59.  
  60.   .is_tiff:
  61.         pop     esi
  62.         xor     eax, eax
  63.         inc     eax
  64.         ret
  65. endp
  66.  
  67. ;;================================================================================================;;
  68. proc img.decode.tiff _data, _length, _options ;///////////////////////////////////////////////////;;
  69. ;;------------------------------------------------------------------------------------------------;;
  70. ;? Decode data into image if it contains correctly formed raw data in tiff format                 ;;
  71. ;;------------------------------------------------------------------------------------------------;;
  72. ;> _data = raw data as read from file/stream                                                      ;;
  73. ;> _length = data length                                                                          ;;
  74. ;;------------------------------------------------------------------------------------------------;;
  75. ;< eax = 0 (error) or pointer to image                                                            ;;
  76. ;;================================================================================================;;
  77. locals
  78.         _endianness             rd 1            ; 0 stands for LE, otherwise BE
  79.         retvalue                rd 1            ; 0 (error) or pointer to image
  80. endl
  81.  
  82.         push    ebx edx esi edi
  83.  
  84.         mov     esi, [_data]
  85.         lodsw
  86.         mov     [_endianness], 0
  87.         cmp     ax, word 'II'
  88.         seta    byte[_endianness]
  89.  
  90.         lodsw_
  91.         lodsd_
  92.     @@:
  93.         stdcall tiff._.parse_IFD, [_data], eax, [_endianness]
  94.         mov     ebx, eax
  95.         mov     [retvalue], eax
  96.         lodsd_
  97.         test    eax, eax
  98. ;       jnz     @b
  99.  
  100.  
  101.   .quit:
  102.         mov     eax, [retvalue]
  103.         pop     edi esi edx ebx
  104.         ret
  105. endp
  106.  
  107.  
  108. ;;================================================================================================;;
  109. proc img.encode.tiff _img, _p_length, _options ;//////////////////////////////////////////////////;;
  110. ;;------------------------------------------------------------------------------------------------;;
  111. ;? Encode image into raw data in tiff format                                                      ;;
  112. ;;------------------------------------------------------------------------------------------------;;
  113. ;> _img = pointer to image                                                                        ;;
  114. ;;------------------------------------------------------------------------------------------------;;
  115. ;< eax = 0 (error) or pointer to encoded data                                                     ;;
  116. ;< _p_length = encoded data length                                                                ;;
  117. ;;================================================================================================;;
  118.         xor     eax, eax
  119.         ret
  120. endp
  121.  
  122.  
  123. ;;================================================================================================;;
  124. ;;////////////////////////////////////////////////////////////////////////////////////////////////;;
  125. ;;================================================================================================;;
  126. ;! Below are private procs you should never call directly from your code                          ;;
  127. ;;================================================================================================;;
  128. ;;////////////////////////////////////////////////////////////////////////////////////////////////;;
  129. ;;================================================================================================;;
  130. proc tiff._.parse_IFD _data, _IFD, _endianness
  131. locals
  132.         extended                rd      1
  133.         retvalue                rd      1
  134.         decompress              rd      1
  135. endl
  136.         push    ebx edx edi
  137.         mov     [retvalue], 0
  138.  
  139.         invoke  mem.alloc, sizeof.tiff_extra
  140.         test    eax, eax
  141.         jz      .quit
  142.         mov     [extended], eax
  143.         mov     ebx, eax
  144.         mov     edi, eax
  145.         mov     ecx, sizeof.tiff_extra/4
  146.         xor     eax, eax
  147.         rep     stosd
  148.  
  149.         mov     esi, [_IFD]
  150.         add     esi, [_data]
  151.         lodsw_
  152.         movzx   ecx, ax
  153.     @@:
  154.         push    ecx
  155.         stdcall tiff._.parse_IFDE, [_data], [_endianness]
  156.         pop     ecx
  157.         dec     ecx
  158.         jnz     @b
  159.  
  160.         call    tiff._.define_image_type
  161.  
  162.         stdcall img.create, [ebx + tiff_extra.image_width], [ebx + tiff_extra.image_height], eax
  163.         test    eax, eax
  164.         jz      .quit
  165.         mov     [retvalue], eax
  166.         mov     edx, eax
  167.         mov     [edx + Image.Extended], ebx
  168.  
  169.         cmp     [ebx+tiff_extra.compression], TIFF.COMPRESSION.UNCOMPRESSED
  170.         jne     @f
  171.         mov     [decompress], tiff._.decompress.uncompressed
  172.         jmp     .decompressor_defined
  173.     @@:
  174.         cmp     [ebx + tiff_extra.compression], TIFF.COMPRESSION.PACKBITS
  175.         jne     @f
  176.         mov     [decompress], tiff._.decompress.packbits
  177.         jmp     .decompressor_defined
  178.     @@:
  179.         mov     [decompress], tiff._.decompress.ccitt1d
  180.         jmp     .decompressor_defined
  181.         jmp     .quit
  182.   .decompressor_defined:
  183.  
  184.         push    esi             ; fixme!!
  185.  
  186.         mov     ecx, [edx + Image.Type]
  187.         dec     ecx
  188.         jz      .bpp8
  189.         dec     ecx
  190.         jz      .bpp24
  191.         dec     ecx
  192.         jz      .bpp32
  193.         dec     ecx
  194.         dec     ecx             ; tiff doesn't handle 15bpp images
  195.         jz      .bpp16
  196.         dec     ecx
  197.         jz      .bpp1
  198.         dec     ecx
  199.         jz      .bpp4
  200. ;error report!!
  201.  
  202.   .bpp1:
  203.   .bpp1.palette:
  204.         mov     edi, [edx+Image.Palette]
  205.         cmp     [ebx + tiff_extra.photometric], TIFF.PHOTOMETRIC.BLACK_IS_ZERO
  206.         jne     .bpp1.white_is_zero
  207.   .bpp1.black_is_zero:
  208.         mov     [edi], dword 0x00000000
  209.         mov     [edi + 4], dword 0x00ffffff
  210.         jmp     .common
  211.   .bpp1.white_is_zero:
  212.         mov     [edi], dword 0x00ffffff
  213.         mov     [edi + 4], dword 0x00000000
  214.         jmp     .common
  215.  
  216.   .bpp4:
  217.         jmp     .common
  218.  
  219.   .bpp8:
  220.         cmp     [ebx + tiff_extra.palette], 0
  221.         je      .bpp8.grayscale
  222.  
  223.         mov     esi, [ebx + tiff_extra.palette]
  224.         mov     ah, 2
  225.   .bpp8.channel:
  226.         mov     edi, eax
  227.         and     edi, 0x0000ff00
  228.         shr     edi, 8
  229.         add     edi, [edx + Image.Palette]
  230.         mov     ecx, 256
  231.     @@:
  232.         lodsb
  233.         stosb
  234.         lodsb
  235.         add     edi, 3
  236.         dec     ecx
  237.         jnz     @b
  238.         dec     ah
  239.         jns     .bpp8.channel
  240.         jmp     .common
  241.   .bpp8.grayscale:
  242.         mov     edi, [edx + Image.Palette]
  243.         mov     eax, 0xff000000
  244.     @@:
  245.         stosd
  246.         add     eax, 0x00010101
  247.         jnc     @b
  248.         jmp     .common
  249.  
  250.   .bpp16:
  251.         jmp     .common
  252.  
  253.   .bpp24:
  254.         jmp     .common
  255.  
  256.   .bpp32:
  257.         jmp     .common
  258.  
  259.  
  260.   .common:
  261.         mov     edi, [edx+Image.Data]
  262.         mov     esi, [ebx+tiff_extra.strip_offsets]
  263.         mov     edx, [ebx+tiff_extra.strip_byte_counts]
  264.  
  265.  
  266.         cmp     [ebx + tiff_extra.strip_offsets_length], TIFF.IFDE_TYPE_LENGTH.SHORT
  267.         jne     .l_x
  268.         cmp     [ebx + tiff_extra.strip_byte_counts_length], TIFF.IFDE_TYPE_LENGTH.SHORT
  269.         jne     .s_l
  270.         jmp     .s_s
  271.   .l_x: cmp     [ebx + tiff_extra.strip_byte_counts_length], TIFF.IFDE_TYPE_LENGTH.SHORT
  272.         jne     .l_l
  273.         jmp     .l_s
  274.  
  275.   .s_s:
  276.         xor     eax, eax
  277.         lodsw_
  278.         push    esi
  279.         mov     esi, eax
  280.         add     esi, [_data]
  281.         xor     ecx, ecx
  282.         mov     cx, word[edx]
  283.         test    [_endianness], 1
  284.         jz      @f
  285.         xchg    cl, ch
  286.     @@:
  287.         add     edx, 2
  288.         stdcall [decompress], [retvalue]
  289.         pop     esi
  290.         dec     [ebx + tiff_extra.offsets_number]
  291.         jnz     .s_s
  292.         jmp     .decoded
  293.  
  294.   .s_l:
  295.         xor     eax, eax
  296.         lodsw_
  297.         push    esi
  298.         mov     esi, eax
  299.         add     esi, [_data]
  300.         mov     ecx, [edx]
  301.         test    [_endianness], 1
  302.         jz      @f
  303.         bswap   ecx
  304.     @@:
  305.         add     edx, 4
  306.         stdcall [decompress], [retvalue]
  307.         pop     esi
  308.         dec     [ebx + tiff_extra.offsets_number]
  309.         jnz     .s_l
  310.         jmp     .decoded
  311.  
  312.   .l_s:
  313.         lodsd_
  314.         push    esi
  315.         mov     esi, eax
  316.         add     esi, [_data]
  317.         xor     ecx, ecx
  318.         mov     cx, word[edx]
  319.         test    [_endianness], 1
  320.         jz      @f
  321.         xchg    cl, ch
  322.     @@:
  323.         add     edx, 2
  324.         stdcall [decompress], [retvalue]
  325.         pop     esi
  326.         dec     [ebx + tiff_extra.offsets_number]
  327.         jnz     .l_s
  328.         jmp     .decoded
  329.  
  330.   .l_l:
  331.         lodsd_
  332.         push    esi
  333.         mov     esi, eax
  334.         add     esi, [_data]
  335.         mov     ecx, [edx]
  336.         test    [_endianness], 1
  337.         jz      @f
  338.         bswap   ecx
  339.     @@:
  340.         add     edx, 4
  341.         stdcall [decompress], [retvalue]
  342.         pop     esi
  343.         dec     [ebx + tiff_extra.offsets_number]
  344.         jnz     .l_l
  345.         jmp     .decoded
  346.  
  347.  
  348.   .decoded:
  349.         cmp     [ebx + tiff_extra.samples_per_pixel], 3
  350.         jne     .pop_quit
  351.         mov     eax, [retvalue]
  352.         mov     esi, [eax + Image.Data]
  353.         mov     edi, [eax + Image.Data]
  354.         mov     ecx, [eax + Image.Width]
  355.         imul    ecx, [eax + Image.Height]
  356.     @@:
  357.         lodsw
  358.         movsb
  359.         mov     byte[esi - 1], al
  360.         add     edi, 2
  361.         dec     ecx
  362.         jnz     @b
  363.  
  364.  
  365.   .pop_quit:
  366.         pop     esi
  367.   .quit:
  368.         pop     edi edx ebx
  369.         mov     eax, [retvalue]
  370.         ret
  371. endp
  372.  
  373. proc tiff._.parse_IFDE _data, _endianness
  374.  
  375.         push    ebx edx edi
  376.  
  377.         lodsw_
  378.         mov     edx, tiff.IFDE_tag_table.begin
  379.         mov     ecx, (tiff.IFDE_tag_table.end-tiff.IFDE_tag_table.begin)/8
  380.   .tag:
  381.         cmp     ax, word[edx]
  382.         jne     @f
  383.         lodsw_
  384.         jmp     dword[edx + 4]
  385.     @@:
  386.         add     edx, 8
  387.         dec     ecx
  388.         jnz     .tag
  389.   .tag_default:                         ; unknown/unsupported/uninteresting/unimportant
  390.         lodsw
  391.         lodsd
  392.         lodsd
  393.         jmp     .quit                   ; just skip it
  394.  
  395.   .tag_100:
  396.         cmp     ax, TIFF.IFDE_TYPE.SHORT
  397.         jne     @f
  398.         lodsd
  399.         xor     eax, eax
  400.         lodsw_
  401.         mov     [ebx + tiff_extra.image_width], eax
  402.         lodsw
  403.         jmp     .quit
  404.     @@:
  405.         cmp     ax, TIFF.IFDE_TYPE.LONG
  406.         jne     @f
  407.         lodsd
  408.         lodsd_
  409.         mov     [ebx + tiff_extra.image_width], eax
  410.         jmp     .quit
  411.     @@:
  412.         jmp     .quit
  413.  
  414.   .tag_101:
  415.         cmp     ax, TIFF.IFDE_TYPE.SHORT
  416.         jne     @f
  417.         lodsd
  418.         xor     eax, eax
  419.         lodsw_
  420.         mov     [ebx + tiff_extra.image_height], eax
  421.         lodsw
  422.         jmp     .quit
  423.     @@:
  424.         cmp     ax, TIFF.IFDE_TYPE.LONG
  425.         jne     @f
  426.         lodsd
  427.         lodsd_
  428.         mov     [ebx + tiff_extra.image_height], eax
  429.         jmp     .quit
  430.     @@:
  431.         jmp     .quit
  432.  
  433.   .tag_102:
  434.         lodsd_
  435.         imul    eax, TIFF.IFDE_TYPE_LENGTH.SHORT
  436.         cmp     eax, 4
  437.         ja      @f
  438.         xor     eax, eax
  439.         lodsw_
  440.         mov     [ebx + tiff_extra.bits_per_sample], eax
  441.         lodsw
  442.         jmp     .quit
  443.     @@:
  444.         lodsd_
  445.         add     eax, [_data]
  446.         push    esi
  447.         mov     esi, eax
  448.         xor     eax, eax
  449.         lodsw_
  450.         pop     esi
  451.         mov     [ebx + tiff_extra.bits_per_sample], eax
  452.         jmp     .quit
  453.  
  454.   .tag_103:
  455.         cmp     ax, TIFF.IFDE_TYPE.SHORT
  456.         jne     @f
  457.         lodsd
  458.         xor     eax, eax
  459.         lodsw_
  460.         mov     [ebx + tiff_extra.compression], eax
  461.         lodsw
  462.         jmp     .quit
  463.     @@:
  464.         jmp     .quit
  465.  
  466.   .tag_106:
  467.         cmp     ax, TIFF.IFDE_TYPE.SHORT
  468.         jne     @f
  469.         lodsd
  470.         xor     eax, eax
  471.         lodsw_
  472.         mov     [ebx + tiff_extra.photometric], eax
  473.         lodsw
  474.         jmp     .quit
  475.     @@:
  476.  
  477.         jmp     .quit
  478.  
  479.   .tag_111:
  480.         cmp     ax, TIFF.IFDE_TYPE.SHORT
  481.         jne     @f
  482.         mov     [ebx + tiff_extra.strip_offsets_length], TIFF.IFDE_TYPE_LENGTH.SHORT
  483.         jmp     .tag_111.common
  484.     @@:
  485.         mov     [ebx + tiff_extra.strip_offsets_length], TIFF.IFDE_TYPE_LENGTH.LONG
  486.   .tag_111.common:
  487.         lodsd_
  488.         mov     [ebx + tiff_extra.offsets_number], eax
  489.         imul    eax, [ebx+tiff_extra.strip_offsets_length]
  490.         cmp     eax, 4
  491.         ja      @f
  492.         mov     [ebx + tiff_extra.strip_offsets], esi
  493.         lodsd
  494.         jmp     .quit
  495.     @@:
  496.         lodsd_
  497.         add     eax, [_data]
  498.         mov     [ebx + tiff_extra.strip_offsets], eax
  499.         jmp     .quit
  500.  
  501.   .tag_115:
  502.         lodsd_
  503.         imul    eax, TIFF.IFDE_TYPE_LENGTH.SHORT
  504.         cmp     eax, 4
  505.         ja      @f
  506.         xor     eax, eax
  507.         lodsw_
  508.         mov     [ebx + tiff_extra.samples_per_pixel], eax
  509.         lodsw
  510.         jmp     .quit
  511.     @@:
  512.         lodsd_
  513.         add     eax, [_data]
  514.         movzx   eax, word[eax]
  515.         jmp     .quit
  516.  
  517.   .tag_116:
  518.         cmp     ax, TIFF.IFDE_TYPE.SHORT
  519.         jne     @f
  520.         lodsd
  521.         xor     eax, eax
  522.         lodsw_
  523.         mov     [ebx + tiff_extra.rows_per_strip], eax
  524.         lodsw
  525.         jmp     .quit
  526.     @@:
  527.         lodsd
  528.         lodsd_
  529.         mov     [ebx + tiff_extra.rows_per_strip], eax
  530.         jmp     .quit
  531.  
  532.   .tag_117:
  533.         cmp     ax, TIFF.IFDE_TYPE.SHORT
  534.         jne     @f
  535.         mov     [ebx + tiff_extra.strip_byte_counts_length], TIFF.IFDE_TYPE_LENGTH.SHORT
  536.         jmp     .tag_117.common
  537.     @@:
  538.         mov     [ebx + tiff_extra.strip_byte_counts_length], TIFF.IFDE_TYPE_LENGTH.LONG
  539.   .tag_117.common:
  540.         lodsd_
  541.         imul    eax, [ebx + tiff_extra.strip_byte_counts_length]
  542.         cmp     eax, 4
  543.         ja      @f
  544.         mov     [ebx + tiff_extra.strip_byte_counts], esi
  545.         lodsd
  546.         jmp     .quit
  547.     @@:
  548.         lodsd_
  549.         add     eax, [_data]
  550.         mov     [ebx + tiff_extra.strip_byte_counts], eax
  551.         jmp     .quit
  552.  
  553.   .tag_140:
  554.         lodsd
  555.         lodsd_
  556.         add     eax, [_data]
  557.         mov     [ebx + tiff_extra.palette], eax
  558.         jmp     .quit
  559.  
  560.   .quit:
  561.         pop     edi edx ebx
  562.         ret
  563. endp
  564.  
  565.  
  566. proc tiff._.define_image_type
  567.  
  568.         xor     eax, eax
  569.  
  570.         cmp     [ebx + tiff_extra.bits_per_sample], 1
  571.         jg      .not_bilevel
  572.         mov     eax, Image.bpp1
  573.         jmp     .quit
  574.   .not_bilevel:
  575.         cmp     [ebx + tiff_extra.palette], 0
  576.         je      .without_palette
  577.         cmp     [ebx + tiff_extra.bits_per_sample], 4
  578.         jne     @f
  579.         mov     eax, Image.bpp4
  580.         jmp     .quit
  581.     @@:
  582.         cmp     [ebx + tiff_extra.bits_per_sample], 8
  583.         jne     @f
  584.         mov     eax, Image.bpp8
  585.         jmp     .quit
  586.     @@:
  587.         jmp     .quit
  588.   .without_palette:
  589.         cmp     [ebx + tiff_extra.samples_per_pixel], 1
  590.         jg      .not_grayscale
  591.         cmp     [ebx + tiff_extra.bits_per_sample], 4
  592.         jne     @f
  593.         mov     eax, Image.bpp4
  594.         jmp     .quit
  595.     @@:
  596.         cmp     [ebx + tiff_extra.bits_per_sample], 8
  597.         jne     @f
  598.         mov     eax, Image.bpp8
  599.         jmp     .quit
  600.   .not_grayscale:
  601.         cmp     [ebx + tiff_extra.samples_per_pixel], 3
  602.         jne     @f
  603.         mov     eax, Image.bpp24
  604.         jmp     .quit
  605.     @@:
  606.         jmp     .quit
  607.   .quit:
  608.         ret
  609. endp
  610.  
  611.  
  612. proc tiff._.decompress.uncompressed _image
  613.  
  614.         rep     movsb
  615.         ret
  616. endp
  617.  
  618.  
  619. proc tiff._.decompress.packbits _image
  620.  
  621.         push    ebx ecx edx esi
  622.  
  623.         mov     edx, ecx
  624.  
  625.   .decode:
  626.         lodsb
  627.         dec     edx
  628.         cmp     al, 0xff
  629.         jbe     .different
  630.         cmp     al, 0x80
  631.         jne     .identical
  632.         test    edx, edx
  633.         jz      .quit
  634.         jmp     .decode
  635.  
  636.   .identical:
  637.         neg     al
  638.         inc     al
  639.         movzx   ecx, al
  640.         dec     edx
  641.         lodsb
  642.         rep     stosb
  643.         test    edx, edx
  644.         jnz     .decode
  645.         jmp     .quit
  646.  
  647.   .different:
  648.         movzx   ecx, al
  649.         inc     ecx
  650.         sub     edx, ecx
  651.         rep     movsb
  652.         test    edx, edx
  653.         jnz     .decode
  654.  
  655.   .quit:
  656.         pop     esi edx ecx ebx
  657.         ret
  658. endp
  659.  
  660.  
  661. proc    tiff._.decompress.ccitt1d _image
  662. locals
  663.         current_tree            rd      1
  664.         old_tree                rd      1
  665.         width                   rd      1
  666.         height                  rd      1
  667.         width_left              rd      1
  668.         is_makeup               rd      1
  669. endl
  670.         push    ebx ecx edx esi
  671.         mov     [is_makeup], 0
  672.  
  673.         mov     ebx, [_image]
  674.         push    [ebx + Image.Height]
  675.         pop     [height]
  676.         push    [ebx + Image.Width]
  677.         pop     [width]
  678.  
  679.         mov     edx, esi
  680.   .next_scanline:
  681.         push    [width]
  682.         pop     [width_left]
  683.         dec     [height]
  684.         js      .error
  685.         mov     [current_tree], tiff._.huffman_tree_white.begin
  686.         mov     [old_tree], tiff._.huffman_tree_black.begin
  687.         mov     ebx, 0
  688.         mov     ecx, 8
  689.   .next_run:
  690.         mov     esi, [current_tree]
  691.   .branch:
  692.         lodsd
  693.         btr     eax, 31
  694.         jnc     .not_a_leaf
  695.         cmp     eax, 63
  696.         seta    byte[is_makeup]
  697.         ja      @f
  698.         push    [current_tree]
  699.         push    [old_tree]
  700.         pop     [current_tree]
  701.         pop     [old_tree]
  702.     @@:
  703.         stdcall tiff._.write_run, [width_left], [current_tree]
  704.         mov     [width_left], eax
  705.         test    byte[is_makeup], 0x01
  706.         jnz     .next_run
  707.         test    eax, eax
  708.         jnz     .next_run
  709.         jmp     .next_scanline
  710.   .not_a_leaf:
  711.         test    bh, bh
  712.         jnz     @f
  713.         mov     bl, byte[edx]
  714.         inc     edx
  715.         mov     bh, 8
  716.     @@:
  717.         test    al, 0x02
  718.         jz      .not_a_corner
  719.         dec     bh
  720.         sal     bl, 1
  721.         lahf
  722.         and     ah, 0x03
  723.         cmp     al, ah
  724.         jne     .error
  725.         mov     esi, [esi]
  726.         jmp     .branch
  727.   .not_a_corner:
  728.         lodsd
  729.         dec     bh
  730.         sal     bl, 1
  731.         jc      .branch
  732.         mov     esi, eax
  733.         jmp     .branch
  734.   .error:
  735.   .quit:
  736.         pop     esi edx ecx ebx
  737.         ret
  738. endp
  739.  
  740.  
  741. proc    tiff._.write_run _width_left, _current_tree
  742.  
  743.         push    ebx
  744.  
  745.         test    eax, eax
  746.         jz      .done
  747.         sub     [_width_left], eax
  748.         js      .error
  749.         cmp     esi, tiff._.huffman_tree_black.begin
  750.         seta    bh
  751.  
  752.         cmp     ecx, eax
  753.         ja      .one_byte
  754.   .many_bytes:
  755.         mov     bl, [edi]
  756.     @@:
  757.         shl     bl, 1
  758.         or      bl, bh
  759.         dec     eax
  760.         dec     ecx
  761.         jnz     @b
  762.         mov     [edi], bl
  763.         inc     edi
  764.         mov     ecx, eax
  765.         and     eax, 0x07
  766.         shr     ecx, 3
  767.  
  768.         push    eax
  769.         xor     eax, eax
  770.         test    bh, bh
  771.         jz      @f
  772.         dec     al
  773.     @@:
  774.         rep     stosb
  775.         pop     eax
  776.  
  777.         mov     ecx, 8
  778.         test    eax, eax
  779.         jz      .done
  780.  
  781.   .one_byte:
  782.         mov     bl, [edi]
  783.     @@:
  784.         shl     bl, 1
  785.         or      bl, bh
  786.         dec     ecx
  787.         dec     eax
  788.         jnz     @b
  789.         mov     byte[edi], bl
  790.  
  791.         cmp     [_width_left], 0
  792.         jne     .done
  793.         mov     bl, [edi]
  794.         shl     bl, cl
  795.         mov     byte[edi], bl
  796.         inc     edi
  797.   .done:
  798.         mov     eax, [_width_left]
  799.         jmp     .quit
  800.   .error:
  801.   .quit:
  802.         pop     ebx
  803.         ret
  804. endp
  805.  
  806.  
  807. proc    tiff._.get_word _endianness
  808.  
  809.         lodsw
  810.         test    [_endianness], 1
  811.         jnz     @f
  812.         ret
  813.     @@:
  814.         xchg    al, ah
  815.         ret
  816. endp
  817.  
  818.  
  819. proc    tiff._.get_dword _endianness
  820.  
  821.         lodsd
  822.         test    [_endianness], 1
  823.         jnz     @f
  824.         ret
  825.     @@:
  826.         bswap   eax
  827.         ret
  828.  
  829.         ret
  830. endp
  831.  
  832.  
  833. ;;================================================================================================;;
  834. ;;////////////////////////////////////////////////////////////////////////////////////////////////;;
  835. ;;================================================================================================;;
  836. ;! Below is private data you should never use directly from your code                             ;;
  837. ;;================================================================================================;;
  838. ;;////////////////////////////////////////////////////////////////////////////////////////////////;;
  839. ;;================================================================================================;;
  840. tiff.IFDE_tag_table.begin:
  841.   .tag_100:             dd      0x0100, tiff._.parse_IFDE.tag_100               ; image width
  842.   .tag_101:             dd      0x0101, tiff._.parse_IFDE.tag_101               ; image height (this is called 'length' in spec)
  843.   .tag_102:             dd      0x0102, tiff._.parse_IFDE.tag_102               ; bits per sample
  844.   .tag_103:             dd      0x0103, tiff._.parse_IFDE.tag_103               ; compression
  845.   .tag_106:             dd      0x0106, tiff._.parse_IFDE.tag_106               ; photometric interpretation
  846.   .tag_111:             dd      0x0111, tiff._.parse_IFDE.tag_111               ; strip offsets
  847.   .tag_115:             dd      0x0115, tiff._.parse_IFDE.tag_115               ; samples per pixel
  848.   .tag_116:             dd      0x0116, tiff._.parse_IFDE.tag_116               ; rows per strip
  849.   .tag_117:             dd      0x0117, tiff._.parse_IFDE.tag_117               ; strip byte counts
  850.   .tag_140:             dd      0x0140, tiff._.parse_IFDE.tag_140               ; color map
  851. tiff.IFDE_tag_table.end:
  852.  
  853. include 'huffman.asm'           ; huffman trees for ccitt1d compression method
  854.