Subversion Repositories Kolibri OS

Rev

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

  1. ;;================================================================================================;;
  2. ;;//// xcf.asm //// (c) dunkaist, 2011 ///////////////////////////////////////////////////////////;;
  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. ;; References:                                                                                    ;;
  21. ;;   1. "SPECIFICATION OF THE XCF FILE FORMAT"                                                    ;;
  22. ;;      by Henning Makholm                                                                        ;;
  23. ;;      http://svn.gnome.org/viewvc/gimp/trunk/devel-docs/xcf.txt?view=markup                     ;;
  24. ;;   2. "Layer Modes"                                                                             ;;
  25. ;;      from docs.gimp.org                                                                        ;;
  26. ;;      http://docs.gimp.org/en/gimp-concepts-layer-modes.html                                    ;;
  27. ;;                                                                                                ;;
  28. ;;================================================================================================;;
  29. include 'xcf.inc'
  30. ;include '../../../../system/board/trunk/debug.inc'
  31.  
  32. COMPOSITE_MODE      equ     MMX
  33.  
  34. MAX_LAYERS          equ     255
  35.  
  36. DEBUG_STANDARD      equ     TRUE
  37. DEBUG_FBOUNDS       equ     FALSE
  38.  
  39. ;;================================================================================================;;
  40. proc img.is.xcf _data, _length ;//////////////////////////////////////////////////////////////////;;
  41. ;;------------------------------------------------------------------------------------------------;;
  42. ;? Determine if raw data could be decoded (is in xcf format)                                      ;;
  43. ;;------------------------------------------------------------------------------------------------;;
  44. ;> _data = raw data as read from file/stream                                                      ;;
  45. ;> _length = data length                                                                          ;;
  46. ;;------------------------------------------------------------------------------------------------;;
  47. ;< eax = false / true                                                                             ;;
  48. ;;================================================================================================;;
  49.  
  50.     push    edi
  51.     xor     eax,    eax
  52.  
  53.     mov     edi,    [_data]
  54.  
  55.     cmp     dword[edi+xcf_header.magic_string], 'gimp'
  56.      jne    .is_not_xcf
  57.     cmp     dword[edi+xcf_header.magic_string+4],   ' xcf'
  58.      jne    .is_not_xcf
  59.  
  60.     cmp     [edi+xcf_header.version],   'file'
  61.      je     @f
  62.     cmp     [edi+xcf_header.version],   'v001'
  63.      je     @f
  64.     cmp     [edi+xcf_header.version],   'v002'
  65.      je     @f
  66.      jmp    .is_not_xcf
  67. @@:
  68.  
  69.     cmp     byte[edi+xcf_header.reserved],  0
  70.      jne    .is_not_xcf
  71.  
  72. .is_xcf:
  73.     inc     eax
  74.  
  75. .is_not_xcf:
  76.     pop     edi
  77.     ret
  78.  
  79. endp
  80.  
  81. ;;================================================================================================;;
  82. proc img.decode.xcf _data, _length, _options ;////////////////////////////////////////////////////;;
  83. ;;------------------------------------------------------------------------------------------------;;
  84. ;? Decode data into image if it contains correctly formed raw data in xcf format                  ;;
  85. ;;------------------------------------------------------------------------------------------------;;
  86. ;> _data = raw data as read from file/stream                                                      ;;
  87. ;> _length = data length                                                                          ;;
  88. ;;------------------------------------------------------------------------------------------------;;
  89. ;< eax = 0 (error) or pointer to image                                                            ;;
  90. ;;================================================================================================;;
  91. locals
  92. count               rd      1
  93. retvalue            rd      1                       ; 0 (error) or pointer to image
  94. endl
  95.  
  96.     push    ebx esi edi
  97.  
  98.     mov     esi,    [_data]
  99.     add     esi,    xcf_header.width
  100.    
  101.     lodsd
  102.     bswap   eax
  103.     mov     ebx,    eax
  104.     lodsd
  105.     bswap   eax
  106.     mov     edx,    eax
  107.    
  108.     lodsd
  109.     bswap   eax
  110.     test    eax,    eax
  111.      jz     .process_rgb
  112.     dec     eax
  113.      jz     .process_grayscale
  114.     dec     eax
  115.      jz     .process_indexed
  116.      jmp    .error
  117.  
  118.  
  119. .process_rgb:
  120.    
  121.     stdcall img.create, ebx, edx, Image.bpp32
  122.     mov     [retvalue], eax
  123.     test    eax,    eax
  124.      jz     .error
  125.  
  126.     mov     ebx,    eax
  127.  
  128.     mov     edx,    XCF_BASETYPE_RGB
  129.  
  130.      jmp    .common_process
  131.  
  132. .process_grayscale:
  133.  
  134.     stdcall img.create, ebx, edx, Image.bpp8
  135.     mov     [retvalue], eax
  136.     test    eax,    eax
  137.      jz     .error
  138.  
  139.     mov     ebx,    eax
  140.  
  141.     mov     eax,    [ebx + Image.Width]
  142.     imul    [ebx + Image.Height]
  143.     shl     eax,    1
  144.     mov     [ebx+Image.Palette],    eax
  145.     add     eax,    256*4
  146.     invoke  mem.realloc, [ebx + Image.Data], eax
  147.     mov     [ebx + Image.Data], eax
  148.     add     [ebx + Image.Palette],  eax
  149.  
  150.     mov     edi,    [ebx+Image.Palette]
  151.     mov     eax,    0xff000000
  152. @@: stosd
  153.     add     eax,    0x00010101
  154.      jnc    @b
  155.  
  156.     mov     edx,    XCF_BASETYPE_GRAY
  157.    
  158.      jmp    .common_process
  159.  
  160.  
  161. .process_indexed:
  162.  
  163.     stdcall img.create, ebx, edx, Image.bpp8
  164.     mov     [retvalue], eax
  165.     test    eax,    eax
  166.      jz     .error
  167.  
  168.     mov     ebx,    eax
  169.  
  170.     mov     eax,    [ebx + Image.Width]
  171.     imul    [ebx + Image.Height]
  172.     shl     eax,    1
  173.     mov     [ebx+Image.Palette],    eax
  174.     add     eax,    256*4
  175.     invoke  mem.realloc, [ebx + Image.Data], eax
  176.     mov     [ebx + Image.Data], eax
  177.     add     [ebx + Image.Palette],  eax
  178.  
  179.     mov     edx,    XCF_BASETYPE_INDEXED
  180. ;     jmp    .common_process
  181.  
  182. .common_process:
  183.  
  184.         invoke  mem.alloc,  sizeof.xcf_ext
  185.         or      eax,    eax
  186.          jz  .error
  187.         mov     [ebx+Image.Extended],   eax
  188.         mov     [eax+xcf_ext.opacity],  0xffffffff
  189.         mov     [eax+xcf_ext.type], edx
  190.  
  191.         stdcall parse_properties, ebx
  192.  
  193.         mov     edi,    esi
  194.         mov     eax,    0
  195.         mov     ecx,    MAX_LAYERS
  196.         mov     [count],    MAX_LAYERS-1
  197.         repne   scasd
  198.         sub     [count],    ecx
  199.         mov     esi,    edi
  200.         mov     ecx,    0
  201.  
  202. .still: sub     esi,    8
  203.         lodsd
  204.         bswap   eax
  205.  
  206.         push    ecx
  207.         stdcall decode_layer, eax, [_data]
  208.         pop     ecx
  209.         test    eax,    eax
  210.          jz     @f
  211.         push    ecx
  212.         stdcall merge_down, eax, [retvalue], ecx
  213.         pop     ecx
  214.         add     ecx,    1
  215. @@:     dec     [count]
  216.          jnz    .still
  217. ;        jmp    .quit
  218.  
  219.  
  220.         cmp     [ebx+Image.Type],   Image.bpp8
  221.          jne    .quit
  222.         stdcall pack_8a, ebx
  223.          jmp .quit
  224.  
  225. .error: mov     [retvalue], 0
  226. .quit:  pop     edi esi ebx
  227.         mov     eax,    [retvalue]
  228.         ret
  229. endp
  230.  
  231.  
  232.  
  233. ;;================================================================================================;;
  234. proc img.encode.xcf _img, _p_length, _options ;///////////////////////////////////////////////////;;
  235. ;;------------------------------------------------------------------------------------------------;;
  236. ;? Encode image into raw data in xcf format                                                       ;;
  237. ;;------------------------------------------------------------------------------------------------;;
  238. ;> _img = pointer to image                                                                        ;;
  239. ;;------------------------------------------------------------------------------------------------;;
  240. ;< eax = 0 (error) or pointer to encoded data                                                     ;;
  241. ;< _p_length = encoded data length                                                                ;;
  242. ;;================================================================================================;;
  243.     xor eax, eax
  244.     ret
  245. endp
  246.  
  247.  
  248. ;;================================================================================================;;
  249. ;;////////////////////////////////////////////////////////////////////////////////////////////////;;
  250. ;;================================================================================================;;
  251. ;! Below are private procs you should never call directly from your code                          ;;
  252. ;;================================================================================================;;
  253. ;;////////////////////////////////////////////////////////////////////////////////////////////////;;
  254. ;;================================================================================================;;
  255. proc parse_properties _img
  256.  
  257.         mov     ebx,    [_img]
  258. .begin:
  259.     lodsd
  260.     bswap   eax
  261.  
  262.     mov     ecx,    (xcf_prop_table_end-xcf_prop_table_begin)/8
  263.     mov     edi,    xcf_prop_table_begin
  264.  
  265. .still:
  266.     cmp     eax,    [edi]
  267.      jne    @f
  268.      jmp    dword[edi+4]
  269. @@:
  270.     add     edi,    8
  271.     dec     ecx
  272.      jnz    .still
  273.     lodsd                       ; skip
  274.     bswap   eax                 ; uninteresting
  275.     add     esi,    eax         ; property
  276.      jmp    .begin
  277.  
  278. parse_prop_00:  ; PROP_END
  279.         lodsd
  280.         ret
  281.        
  282. parse_prop_01:  ; PROP_COLORMAP
  283.         lodsd
  284.         mov     ecx,    [ebx+Image.Extended]
  285.         cmp     [ecx+xcf_ext.type], XCF_BASETYPE_INDEXED
  286.          je     @f
  287.         bswap   eax
  288.         add     esi,    eax
  289.         jmp    parse_properties.begin
  290. @@:
  291.         lodsd
  292.         bswap   eax
  293.         mov     ecx,    eax
  294.         mov     edi,    [ebx+Image.Palette]
  295.  
  296. @@:     lodsd
  297.         sub     esi,    1
  298.         bswap   eax
  299.         shr     eax,    8
  300.         or      eax,    0xff000000
  301.         stosd
  302.         dec     ecx
  303.          jnz    @b
  304.          jmp    parse_properties.begin
  305.  
  306. parse_prop_06:  ; PROP_OPACITY
  307.         lodsd
  308.         lodsd
  309.         bswap   eax
  310.         mov     ecx,    [ebx+Image.Extended]
  311.         mov     [ecx+xcf_ext.opacity],  eax
  312.          jmp    parse_properties.begin
  313.  
  314. parse_prop_07:  ; PROP_MODE
  315.         lodsd
  316.         lodsd
  317.         bswap   eax
  318.         mov     ecx,    [ebx+Image.Extended]
  319.         mov     [ecx+xcf_ext.layer_mode],   eax
  320.          jmp    parse_properties.begin
  321.  
  322. parse_prop_08:  ; PROP_VISIBLE
  323.         lodsd
  324.         lodsd
  325.         bswap   eax
  326.         mov     ecx,    [ebx+Image.Extended]
  327.         mov     [ecx+xcf_ext.visible],  eax
  328.          jmp    parse_properties.begin
  329.  
  330. parse_prop_11:  ; PROP_APPLY_MASK
  331.         lodsd
  332.         lodsd
  333.         bswap   eax
  334.         mov     ecx,    [ebx+Image.Extended]
  335.         mov     [ecx+xcf_ext.apply_mask],   eax
  336.          jmp    parse_properties.begin
  337.  
  338. parse_prop_15:  ; PROP_OFFSETS
  339.         lodsd
  340.         lodsd
  341.         mov     ecx,    [ebx+Image.Extended]
  342.         bswap   eax
  343.         mov     [ecx+xcf_ext.offset_x], eax
  344.         lodsd
  345.         bswap   eax
  346.         mov     [ecx+xcf_ext.offset_y], eax
  347.          jmp    parse_properties.begin
  348. endp
  349.  
  350.  
  351. proc decode_channel _channel_begin, _data
  352. locals
  353. channel_width       rd  1
  354. channel_height      rd  1
  355. planes_todo         rd  1
  356. total_bpl           rd  1
  357. endl
  358.  
  359.         push    ebx esi edi
  360.         mov     esi,    [_channel_begin]
  361.         add     esi,    [_data]
  362.         lodsd
  363.         bswap   eax
  364.         mov     [channel_width],  eax
  365.         mov     [total_bpl],    eax
  366.         lodsd
  367.         bswap   eax
  368.         mov     [channel_height], eax
  369.         lodsd
  370.         bswap   eax
  371.         add     esi,    eax
  372.  
  373.         stdcall img.create, [channel_width], [channel_height], Image.bpp8
  374.         mov     ebx,    eax
  375.         test    ebx,    ebx
  376.          jz     .quit
  377.         invoke  mem.alloc,  sizeof.xcf_ext
  378.         or      eax,    eax
  379.          jz  .error
  380.         mov     [ebx+Image.Extended],   eax
  381.  
  382.         stdcall parse_properties, ebx
  383.  
  384.         lodsd
  385.         bswap   eax
  386.         mov     esi,    eax
  387.         add     esi,    [_data]
  388.         lodsd
  389.         lodsd
  390.         lodsd
  391.         bswap   eax
  392.         mov     [planes_todo],  eax
  393.         lodsd
  394.         bswap   eax
  395.         mov     esi,    eax
  396.         add     esi,    [_data]
  397.         lodsd
  398.         lodsd
  399.  
  400.         mov     edi,    [ebx+Image.Data]
  401.         mov     ecx,    0
  402. @@:     lodsd
  403.         test    eax,    eax
  404.          jz     .quit
  405.         bswap   eax
  406.         add     eax,    [_data]
  407.         stdcall decode_tile, eax, [channel_width], [channel_height], [total_bpl], [planes_todo], 1
  408.         add     ecx,    1
  409.          jmp    @b
  410.          
  411. .error: stdcall img.destroy, ebx
  412.         mov     ebx,    0
  413. .quit:  mov     eax,    ebx
  414.         pop     edi esi ebx
  415.         ret
  416. endp
  417.  
  418.  
  419. proc decode_layer _layer_begin, _data
  420. locals
  421. layer_width     rd  1
  422. layer_height    rd  1
  423. planes_todo     rd  1
  424. total_bpl       rd  1
  425. color_step      rd  1
  426. endl
  427.  
  428.         push    ebx esi edi
  429.         mov     esi,    [_layer_begin]
  430.         add     esi,    [_data]
  431.         lodsd
  432.         bswap   eax
  433.         mov     [layer_width],  eax
  434.         mov     [total_bpl],    eax
  435.         shl     [total_bpl],    1
  436.         lodsd
  437.         bswap   eax
  438.         mov     [layer_height], eax
  439.         lodsd
  440.         bswap   eax
  441.         mov     edx,    Image.bpp16
  442.         mov     [color_step],   1
  443.         cmp     eax,    2
  444.          jge    @f
  445.         mov     [color_step],   3
  446.         mov     edx,    Image.bpp32
  447.         shl     [total_bpl],    1
  448. @@:     stdcall img.create, [layer_width], [layer_height], edx
  449.         mov     ebx,    eax
  450.         test    ebx,    ebx
  451.          jz     .quit
  452.         invoke  mem.alloc,  sizeof.xcf_ext
  453.         or      eax,    eax
  454.          jz     .error
  455.         mov     [ebx+Image.Extended],   eax
  456.  
  457.         lodsd
  458.         bswap   eax
  459.         add     esi,    eax
  460.         stdcall parse_properties, ebx
  461.         mov     edx,    [ebx+Image.Extended]
  462.         or      [edx+xcf_ext.visible],  0
  463.          jz     .unvisible
  464.  
  465.         lodsd
  466.         bswap   eax
  467.         push    esi
  468.         mov     esi,    eax
  469.         add     esi,    [_data]
  470.         lodsd
  471.         lodsd
  472.         lodsd
  473.         bswap   eax
  474.         mov     [planes_todo],  eax
  475. ;        mov     ecx,    [ebx+Image.Extended]
  476. ;        mov     [ecx+xcf_ext.planes],   eax
  477.         lodsd
  478.         bswap   eax
  479.         mov     esi,    eax
  480.         add     esi,    [_data]
  481.         lodsd
  482.         lodsd
  483.  
  484.         mov     edi,    [ebx+Image.Data]
  485.         mov     ecx,    0
  486. @@:     lodsd
  487.         test    eax,    eax
  488.          jz     @f
  489.         bswap   eax
  490.         add     eax,    [_data]
  491.         stdcall decode_tile, eax, [layer_width], [layer_height], [total_bpl], [planes_todo], 0
  492.         add     ecx,    1
  493.          jmp    @b
  494. @@:
  495.  
  496.        stdcall apply_opacity, ebx, [color_step]
  497.  
  498.         pop     esi
  499.         lodsd
  500.         bswap   eax
  501.         test    eax,    eax
  502.          jz     .quit
  503.          
  504.         stdcall decode_channel, eax, [_data]
  505.         test    eax,    eax
  506.          jz     .error
  507.  
  508.         mov     edx,    [ebx+Image.Extended]
  509.         cmp     [edx+xcf_ext.apply_mask],   0
  510.          je     .quit
  511.  
  512.         stdcall apply_alpha_mask, ebx, eax, [color_step]
  513.          jmp    .quit
  514.  
  515. .unvisible:
  516. .error: stdcall img.destroy, ebx
  517.         mov     ebx,    0
  518. .quit:  mov     eax,    ebx
  519.         pop     edi esi ebx
  520.         ret
  521. endp
  522.  
  523.  
  524. proc decode_tile _tile_data, _width, _height, _total_bpl, _bytes_pp, _is_channel
  525. locals
  526. tile_x              rd  1
  527. tile_y              rd  1
  528. tile_width          rd  1
  529. tile_height         rd  1
  530. planes_todo         rd  1
  531. color_step          rd  1
  532. endl
  533.  
  534.         push    ebx ecx edx esi edi
  535.         pushd   [_bytes_pp]
  536.         popd    [planes_todo]
  537.        
  538.         cmp     [_is_channel],  1
  539.          je     @f
  540.         test    [_bytes_pp],    0x01
  541.          jz     @f
  542.         add     [_bytes_pp],    1
  543. @@:
  544.         mov     ebx,    [_bytes_pp]
  545.         sub     ebx,    1
  546.         mov     [color_step],   ebx
  547.  
  548.         mov     esi,    [_tile_data]
  549.         mov     eax,    ecx
  550.         mov     ebx,    [_width]
  551.         dec     ebx
  552.         shr     ebx,    6
  553.         inc     ebx
  554.         mov     edx,    0
  555.         div     bx
  556.         mov [tile_x],   edx
  557.         mov [tile_y],   eax
  558.  
  559.         mov     [tile_width],   64
  560.         mov     ebx,    [_width]
  561.         test    ebx,    0x0000003F
  562.          jz @f
  563.         dec     ebx
  564.         shr     ebx,    6
  565.         cmp     ebx,    [tile_x]
  566.          jne    @f
  567.         mov     ebx,    [_width]
  568.         and     ebx,    0x0000003F
  569.         mov     [tile_width],   ebx
  570. @@:
  571.  
  572.         mov     [tile_height],   64
  573.         mov     ebx,    [_height]
  574.         test    ebx,    0x0000003F
  575.          jz @f
  576.         dec     ebx
  577.         shr     ebx,    6
  578.         cmp     ebx,    [tile_y]
  579.          jne    @f
  580.         mov     ebx,    [_height]
  581.         and     ebx,    0x0000003F
  582.         mov     [tile_height],   ebx
  583. @@:
  584.  
  585.  
  586.         mov     eax,    [_total_bpl]
  587.         shl     eax,    6
  588.         mul     [tile_y]
  589.         add     edi,    eax
  590.  
  591.         mov     eax,    [tile_x]
  592.         shl     eax,    6
  593.         imul    eax,    [_bytes_pp]
  594.         add     edi,    eax
  595.  
  596.         cmp     [_is_channel],  1
  597.          jne    @f
  598.         stdcall decode_color, [tile_width], [tile_height], [color_step], [_total_bpl], [_bytes_pp]
  599.          jmp    .quit
  600. @@:
  601.         mov     eax,    [planes_todo]
  602.         dec     eax
  603.          jz     .p1
  604.         dec     eax
  605.          jz     .p2
  606.         dec     eax
  607.          jz     .p3
  608.          jmp    .p4
  609. .p1:
  610.         stdcall decode_color, [tile_width], [tile_height], [color_step], [_total_bpl], [_bytes_pp]
  611.         add     edi,    1
  612.         stdcall fill_color, [tile_width], [tile_height], [_total_bpl], [_bytes_pp], [color_step]
  613.          jmp    .quit
  614. .p2:
  615.         stdcall decode_color, [tile_width], [tile_height], [color_step], [_total_bpl], [_bytes_pp]
  616.         add     edi,    1
  617.         stdcall decode_color, [tile_width], [tile_height], [color_step], [_total_bpl], [_bytes_pp]
  618.          jmp    .quit
  619. .p3:
  620.         add     edi,    2
  621.         stdcall decode_color, [tile_width], [tile_height], [color_step], [_total_bpl], [_bytes_pp]
  622.         sub     edi,    1
  623.         stdcall decode_color, [tile_width], [tile_height], [color_step], [_total_bpl], [_bytes_pp]
  624.         sub     edi,    1
  625.         stdcall decode_color, [tile_width], [tile_height], [color_step], [_total_bpl], [_bytes_pp]
  626.         add     edi,    3
  627.         stdcall fill_color, [tile_width], [tile_height], [_total_bpl], [_bytes_pp], [color_step]
  628.          jmp    .quit
  629. .p4:
  630.         add     edi,    2
  631.         stdcall decode_color, [tile_width], [tile_height], [color_step], [_total_bpl], [_bytes_pp]
  632.         sub     edi,    1
  633.         stdcall decode_color, [tile_width], [tile_height], [color_step], [_total_bpl], [_bytes_pp]
  634.         sub     edi,    1
  635.         stdcall decode_color, [tile_width], [tile_height], [color_step], [_total_bpl], [_bytes_pp]
  636.         add     edi,    3
  637.         stdcall decode_color, [tile_width], [tile_height], [color_step], [_total_bpl], [_bytes_pp]
  638. ;         jmp    .quit
  639.  
  640. .quit:  pop     edi esi edx ecx ebx
  641.         ret
  642. endp
  643.  
  644.  
  645. proc fill_color _tile_width, _tile_height, _total_bpl, _bytes_pp, _color_step
  646.         push    ebx
  647.         mov     edx,    [_color_step]
  648.         mov     ebx,    [_total_bpl]
  649.         mov     eax,    [_bytes_pp]
  650.         mul     byte[_tile_width]
  651.         sub     ebx,    eax
  652.        
  653.         mov     ch,     byte[_tile_height]
  654.         mov     al,     0xff
  655. .still: mov     cl,     byte[_tile_width]
  656. @@:     stosb
  657.         add     edi,    edx
  658.         dec     cl
  659.          jnz    @b
  660.         add     edi,    ebx
  661.         dec     ch
  662.          jnz    .still
  663.         pop     ebx
  664.         ret
  665. endp
  666.  
  667.  
  668. proc decode_color _tile_width, _tile_height, _color_step, _total_bpl, _bytes_pp
  669. locals
  670. level_width     rd  1
  671. level_height    rd  1
  672. line_step       rd  1       ; [_total_bpl]-[_tile_width]*[_bytes_pp]
  673. endl
  674.  
  675.         push    edi
  676.  
  677.         mov     ebx,    [_total_bpl]
  678.         movzx   eax,    byte[_bytes_pp]
  679.         mul     byte[_tile_width]
  680.         sub     ebx,    eax
  681.         mov     [line_step],    ebx
  682.         mov     ebx,    [_tile_height]
  683.         mov     edx,    [_tile_width]
  684.  
  685. .decode:lodsb
  686.         cmp     al,     127
  687.          je     .long_identical
  688.          jb     .short_identical
  689.         test    al,     0x7f
  690.          jz     .long_different
  691.          jmp    .short_different
  692.  
  693. .short_identical:
  694.         movzx   ecx,    al
  695.         add     ecx,    1
  696.         lodsb
  697.          jmp    .step1
  698. .long_identical:
  699.         mov     ecx,    0
  700.         lodsw
  701.         mov     cx,     ax
  702.         xchg    cl,     ch
  703.         lodsb
  704. .step1: cmp     cx,     dx
  705.          je     .step2
  706.          jl     .step3
  707.         xchg    cx,     dx
  708.         sub     dx,     cx
  709.         sub     bx,     1
  710. @@:     stosb
  711.         add     edi,    [_color_step]
  712.         loop    @b
  713.         mov     cx,     dx
  714.         mov     edx,    [_tile_width]
  715.         add     edi,    [line_step]
  716.          jmp    .step1
  717.  
  718. .step2:
  719. @@:     stosb
  720.         add     edi,    [_color_step]
  721.         loop    @b
  722.         mov     edx,    [_tile_width]
  723.         add     edi,    [line_step]
  724.         dec     bx
  725.          jz     .quit
  726.          jmp    .decode
  727. .step3: sub     dx,     cx
  728. @@:     stosb
  729.         add     edi,    [_color_step]
  730.         loop    @b
  731.          jmp    .decode
  732.  
  733.  
  734. .short_different:
  735.         movzx   ecx,    al
  736.         neg     cx
  737.         add     cx,     256
  738.          jmp    .step4
  739. .long_different:
  740.         mov     ecx,    0
  741.         lodsb
  742.         mov     ch,     al
  743.         lodsb
  744.         mov     cl,     al
  745. .step4: cmp     cx,     dx
  746.          je     .step5
  747.          jl     .step6
  748.         xchg    cx,     dx
  749.         sub     dx,     cx
  750.         sub     bx,     1
  751. @@:     movsb
  752.         add     edi,    [_color_step]
  753.         loop    @b
  754.         mov     cx,     dx
  755.         mov     edx,    [_tile_width]
  756.         add     edi,    [line_step]
  757.          jmp    .step4
  758.  
  759. .step5:
  760. @@:     movsb
  761.         add     edi,    [_color_step]
  762.         loop    @b
  763.         mov     edx,    [_tile_width]
  764.         add     edi,    [line_step]
  765.         dec     bx
  766.          jz     .quit
  767.          jmp    .decode
  768.  
  769. .step6: sub     dx,     cx
  770. @@:     movsb
  771.         add     edi,    [_color_step]
  772.         loop    @b
  773.          jmp    .decode
  774.  
  775. .quit:  pop     edi
  776.         ret
  777. endp
  778.  
  779.  
  780. proc merge_down _img, _bottom, _layer_number
  781. locals
  782. copy_width          rd  1
  783. copy_height         rd  1
  784. img_x1              rd  1
  785. img_y1              rd  1
  786. bottom_x1           rd  1
  787. bottom_y1           rd  1
  788. img_total_bpl       rd  1
  789. bottom_total_bpl    rd  1
  790. img_length          rd  1
  791. bottom_length       rd  1
  792. endl
  793.         push    ebx esi edi
  794.  
  795.         mov     ebx,    [_bottom]
  796.         mov     edx,    [_img]
  797.  
  798.         mov     [img_x1],  0
  799.         push    [edx+Image.Width]
  800.         pop     [img_length]
  801.  
  802.         mov     [bottom_x1],   0
  803.         mov     ecx,    [ebx+Image.Width]
  804.         mov     [bottom_length],   ecx        
  805.  
  806.         mov     eax,    [edx+Image.Extended]
  807.         movsx   eax,    word[eax+xcf_ext.offset_x]
  808.         cmp     eax,    0
  809.          jg     .greater_x
  810.          jl     .lesser_x
  811.          mov     [copy_width],  ecx
  812.          jmp    .done_x
  813. .greater_x:
  814.         add     [bottom_x1],   eax
  815.         sub     [bottom_length],   eax
  816.          jns    .label_x
  817.         mov     [copy_width],   0
  818.          jmp    .done_x
  819. .lesser_x:
  820.         sub     [img_x1],  eax
  821.         add     [img_length],  eax
  822.          jns    .label_x
  823.         mov     [copy_width],   0
  824.          jmp    .done_x
  825. .label_x:
  826.         mov     ecx,    [img_length]
  827.         cmp     ecx,    [bottom_length]
  828.          jng    @f
  829.         mov     ecx,    [bottom_length]
  830. @@:
  831.         mov     [copy_width],   ecx
  832. .done_x:
  833.  
  834.  
  835.         mov     [img_y1],  0
  836.         push    [edx+Image.Height]
  837.         pop     [img_length]
  838.  
  839.         mov     [bottom_y1],   0
  840.         mov     ecx,    [ebx+Image.Height]
  841.         mov     [bottom_length],   ecx        
  842.  
  843.         mov     eax,    [edx+Image.Extended]
  844.         movsx   eax,    word[eax+xcf_ext.offset_y]
  845.         cmp     eax,    0
  846.          jg     .greater_y
  847.          jl     .lesser_y
  848.          mov     [copy_height], ecx
  849.          jmp    .done_y
  850. .greater_y:
  851.         add     [bottom_y1],   eax
  852.         sub     [bottom_length],   eax
  853.          jns    .label_y
  854.         mov     [copy_height],  0
  855.          jmp    .done_y
  856. .lesser_y:
  857.         sub     [img_y1],  eax
  858.         add     [img_length],  eax
  859.          jns    .label_y
  860.         mov     [copy_height],  0
  861.          jmp    .done_y
  862. .label_y:
  863.         mov     ecx,    [img_length]
  864.         cmp     ecx,    [bottom_length]
  865.          jng    @f
  866.         mov     ecx,    [bottom_length]
  867. @@:
  868.         mov     [copy_height],  ecx
  869. .done_y:
  870.  
  871.         mov     esi,    [edx+Image.Data]
  872.         mov     edi,    [ebx+Image.Data]
  873.  
  874.         mov     eax,    [edx+Image.Width]
  875.         imul    eax,    [img_y1]
  876.         add     eax,    [img_x1]
  877.         shl     eax,    1
  878.         cmp     [edx+Image.Width],  Image.bpp16
  879.          je     @f
  880.         shl     eax,    1
  881. @@:     add     esi,    eax
  882.  
  883.         mov     eax,    [ebx+Image.Width]
  884.         imul    eax,    [bottom_y1]
  885.         add     eax,    [bottom_x1]
  886.         shl     eax,    1
  887.         cmp     [ebx+Image.Width],  Image.bpp8
  888.          je     @f
  889.         shl     eax,    1
  890. @@:     add     edi,    eax
  891.  
  892.  
  893.         mov     eax,    [edx+Image.Width]
  894.         sub     eax,    [copy_width]
  895.         shl     eax,    1
  896.         cmp     [edx+Image.Width],  Image.bpp16
  897.          je     @f
  898.         shl     eax,    1
  899. @@:     mov     [img_total_bpl],   eax
  900.  
  901.         mov     eax,    [ebx+Image.Width]
  902.         sub     eax,    [copy_width]
  903.         shl     eax,    1
  904.         cmp     [ebx+Image.Width],  Image.bpp8
  905.          je     @f
  906.         shl     eax,    1
  907. @@:     mov     [bottom_total_bpl],    eax
  908.  
  909.         cmp     [_layer_number],    0
  910.          jne    .not_first
  911.         mov     ecx,    [copy_width]
  912.         imul    ecx,    [copy_height]
  913.         cmp     [ebx+Image.Type],   Image.bpp8
  914.          je     .bpp8
  915. .bpp32: rep     movsd
  916.          jmp    .done
  917. .bpp8:  rep     movsw
  918.          jmp    .done
  919. .not_first:
  920.  
  921.     push edi
  922.         mov     ecx,    [edx+Image.Extended]
  923.         mov     eax,    [ecx+xcf_ext.layer_mode]
  924.  
  925.         mov     ecx,    [ebx+Image.Extended]
  926.         mov     ecx,    [ecx+xcf_ext.type]
  927.        
  928.         cmp     ecx,    XCF_BASETYPE_RGB
  929.          jne    @f
  930.         mov     edx,    4
  931.          jmp    .type_defined
  932. @@:
  933.         cmp     ecx,    XCF_BASETYPE_GRAY
  934.          jne    @f
  935.         mov     edx,    8
  936.          jmp    .type_defined
  937. @@:
  938.         mov     edx,    12
  939. .type_defined:
  940.         mov     ecx,    (composite_table_end-composite_table_begin)/8
  941.         mov     edi,    composite_table_begin
  942.  
  943. .still:
  944.         cmp     eax,    [edi]
  945.          jne    @f
  946.         add     edi,    edx
  947.         mov     edx,    [edi]
  948.          jmp    .composite_found
  949. @@:
  950.         add     edi,    16
  951.         dec     ecx
  952.          jnz    .still
  953.  
  954. .composite_found:
  955.     pop edi
  956.  
  957.         mov     ecx,    [ebx+Image.Extended]
  958.         cmp     [ecx+xcf_ext.type], XCF_BASETYPE_INDEXED
  959.          jne    @f
  960.         stdcall edx, [copy_width], [copy_height], [bottom_total_bpl], [img_total_bpl]
  961.          jmp    .done
  962. @@:
  963.         cmp     eax,    1
  964.          ja     @f
  965.         stdcall edx, [copy_width], [copy_height], [bottom_total_bpl], [img_total_bpl]
  966.          jmp    .done
  967. @@:
  968.  
  969.  
  970.         cmp     [ebx+Image.Type],   Image.bpp8
  971.          jne    @f
  972.         stdcall merge_8a, [copy_width], [copy_height], [img_total_bpl], [bottom_total_bpl]
  973.          jmp    .done
  974. @@:     stdcall merge_32, [copy_width], [copy_height], [img_total_bpl], [bottom_total_bpl]
  975. ;        jmp    .done
  976. .done:
  977.         stdcall img.destroy, [_img]
  978.         pop     edi esi ebx
  979.         ret
  980. endp
  981.  
  982.  
  983. proc pack_8a _img
  984.         mov     ebx,    [_img]
  985.         mov     esi,    [ebx+Image.Data]
  986.         mov     edi,    [ebx+Image.Data]
  987.         mov     ecx,    [ebx+Image.Width]
  988.         mov     edx,    [ebx+Image.Height]
  989.         imul    ecx,    edx
  990. @@:     lodsw
  991.         stosb
  992.         dec     ecx
  993.          jnz @b
  994.         ret
  995. endp
  996.  
  997.  
  998. proc apply_opacity _img, _color_step
  999.  
  1000.         push    ebx
  1001.        
  1002.         mov     edx,    [ebx+Image.Extended]
  1003.         mov     edx,    [edx+xcf_ext.opacity]
  1004.         cmp     dl,     0xff
  1005.          je     .quit
  1006.  
  1007.         mov     ecx,    [ebx+Image.Width]
  1008.         imul    ecx,    [ebx+Image.Height]
  1009.         mov     esi,    [ebx+Image.Data]
  1010.         mov     ebx,    [_color_step]
  1011.         add     esi,    ebx
  1012.         mov     edi,    esi
  1013. @@:     lodsb
  1014.         mul     dl
  1015.         shr     ax,     8
  1016.         stosb
  1017.         add     esi,    ebx
  1018.         add     edi,    ebx
  1019.         dec     ecx
  1020.          jnz    @b
  1021.          
  1022. .quit:  pop     ebx
  1023.         ret
  1024. endp
  1025.  
  1026.  
  1027. proc apply_alpha_mask _img, _mask, _color_step
  1028.  
  1029.         push    ebx
  1030.  
  1031.         mov     ebx,    [_img]
  1032.         mov     esi,    [_mask]
  1033.         mov     esi,    [esi+Image.Data]
  1034.         mov     edi,    [ebx+Image.Data]
  1035.         mov     ecx,    [ebx+Image.Width]
  1036.         imul    ecx,    [ebx+Image.Height]
  1037.         mov     ebx,    [_color_step]
  1038.         add     edi,    ebx
  1039. @@:
  1040.         lodsb
  1041.         mul     byte[edi]
  1042.         shr     ax,     8
  1043.         stosb
  1044.         add     edi,    ebx
  1045.         dec     ecx
  1046.          jnz    @b
  1047.  
  1048.         stdcall img.destroy, [_mask]
  1049.         pop     ebx
  1050.         ret
  1051. endp
  1052.  
  1053.  
  1054. proc pixel_rgb2hsv
  1055. locals
  1056. vsha        rd  1
  1057. max         rd  1
  1058. min         rd  1
  1059. med         rd  1
  1060. endl
  1061.        push    ebx ecx edx
  1062.  
  1063.         mov     [vsha], eax
  1064.         movzx   eax,    byte[vsha]      ; eax = al = blue
  1065.         movzx   ecx,    byte[vsha+1]    ; ecx = cl = green
  1066.         movzx   edx,    byte[vsha+2]    ; edx = dl = red
  1067.  
  1068.         cmp     al,     cl
  1069.          jne    @f
  1070.         cmp     al,     dl
  1071.          jne    @f
  1072.         ror     eax,    8
  1073.         mov     ax,     0
  1074.         rol     eax,    8
  1075.          jmp    .quit
  1076.  
  1077. @@:     cmp     dl,     cl
  1078.          ja     @f
  1079.         cmp     dl,     al
  1080.          ja     @f
  1081.         mov     byte[min],  dl
  1082.          jmp    .min_found
  1083. @@:     cmp     cl,     al
  1084.          ja     @f
  1085.         cmp     cl,     dl
  1086.          ja     @f
  1087.         mov     byte[min],  cl
  1088.          jmp    .min_found
  1089. @@:     mov     byte[min],  al
  1090. ;         jmp    .min_found
  1091. .min_found:
  1092.  
  1093.         cmp     dl,     cl
  1094.          jb     @f
  1095.         cmp     dl,     al
  1096.          jb     @f
  1097.         mov     byte[max],  dl
  1098.         sub     cx,     ax
  1099.         mov     dx,     cx
  1100.         mov     cx,     0
  1101.          jmp    .max_found
  1102. @@:     cmp     cl,     al
  1103.          jb     @f
  1104.         cmp     cl,     dl
  1105.          jb     @f
  1106.         mov     byte[max],  cl
  1107.         sub     ax,     dx
  1108.         mov     dx,     ax
  1109.         mov     cx,     85
  1110.          jmp    .max_found
  1111. @@:     mov     byte[max],  al
  1112.         sub     dx,     cx
  1113.         mov     cx,     171
  1114. ;         jmp   .max_found
  1115. .max_found:
  1116.  
  1117.         mov     al,     byte[max]
  1118.         sub     al,     byte[min]
  1119.         mov     byte[med],  al
  1120.  
  1121.  
  1122.         imul    dx,     43
  1123.         movsx   eax,    dx
  1124.         ror     eax,    16
  1125.         mov     dx,     ax
  1126.         rol     eax,    16
  1127.         mov     byte[med+1],    0
  1128.         idiv    word[med]
  1129.         add     al,     cl
  1130.         mov     byte[vsha+2],   al
  1131.        
  1132.         mov     al,     byte[max]
  1133.         mov     byte[vsha],   al
  1134.  
  1135.         mov     byte[vsha+1],   0
  1136.         test    al,     al
  1137.          jz     @f
  1138.         mov     byte[vsha+1],   0xff
  1139.         cmp     al,     byte[med]
  1140.          je     @f
  1141.         mov     al,     byte[med]
  1142.         shl     ax,     8
  1143.         div     byte[max]
  1144.         mov     byte[vsha+1],   al
  1145. @@:
  1146.         mov     eax,    [vsha]
  1147.  
  1148. .quit:  pop     edx ecx ebx
  1149.         ret
  1150. endp
  1151.  
  1152.  
  1153. proc pixel_hsv2rgb
  1154. locals
  1155. vsha        rd  1
  1156. f           rb  1
  1157. c           rb  1
  1158. x           rb  1
  1159. endl
  1160.  
  1161.        push    ebx ecx edx
  1162.  
  1163.  
  1164.         mov     [vsha], eax
  1165.         mov     bl,     byte[vsha+1]
  1166.         mul     bl
  1167.         mov     byte[c],    ah
  1168.        
  1169.         movzx   eax,    byte[vsha+2]
  1170.         cmp     eax,    43
  1171.          ja     @f
  1172.         lea     eax,    [eax*3]
  1173.         shl     eax,    1
  1174.         mov     ebx,    eax
  1175.         shr     ebx,    7
  1176.         sub     eax,    ebx
  1177.         shr     ebx,    1
  1178.         sub     eax,    ebx
  1179.          jmp    .ok
  1180.  
  1181. @@:     cmp     eax,    86
  1182.          ja     @f
  1183.         sub     eax,    44
  1184.         lea     eax,    [eax*3]
  1185.         shl     eax,    1
  1186.         neg     al
  1187.         add     al,     0xff
  1188.          jmp    .ok
  1189.  
  1190. @@:     cmp     eax,    129
  1191.          ja     @f
  1192.         sub     eax,    87
  1193.         lea     eax,    [eax*3]
  1194.         shl     eax,    1
  1195.          jmp    .ok
  1196.  
  1197. @@:     cmp     eax,    171
  1198.          ja     @f
  1199.         sub     eax,    130
  1200.         lea     eax,    [eax*3]
  1201.         shl     eax,    1
  1202.         neg     al
  1203.         add     al,     0xff
  1204.          jmp    .ok
  1205.  
  1206. @@:     cmp     eax,    214
  1207.          ja     @f
  1208.         sub     eax,    172
  1209.         lea     eax,    [eax*3]
  1210.         shl     eax,    1
  1211.          jmp    .ok
  1212. @@:
  1213.         sub     eax,    215
  1214.         lea     eax,    [eax*3]
  1215.         shl     eax,    1
  1216.         neg     al
  1217.         add     al,     0xff
  1218. ;        jmp    .ok
  1219. .ok:
  1220.          
  1221.         neg     al
  1222.         add     al,     0xff
  1223.         neg     al
  1224.         add     al,     0xff
  1225. ;        shr     ax,     8
  1226.         mul     byte[c]
  1227.         mov     byte[x],    ah
  1228.  
  1229.  
  1230.  
  1231.         mov     al,     byte[vsha+2]
  1232.         cmp     al,     43
  1233.          jae    @f
  1234.         mov     eax,    [vsha]
  1235.         shr     eax,    8
  1236.         mov     ah,     byte[c]
  1237.         shl     eax,    8
  1238.         mov     ah,     byte[x]
  1239.         mov     al,     0
  1240.          jmp    .done
  1241.  
  1242. @@:     cmp     al,     86
  1243.          jae    @f
  1244.         mov     eax,    [vsha]
  1245.         shr     eax,    8
  1246.         mov     ah,     byte[x]
  1247.         shl     eax,    8
  1248.         mov     ah,     byte[c]
  1249.         mov     al,     0
  1250.          jmp    .done
  1251.          
  1252. @@:     cmp     al,     129
  1253.          jae    @f
  1254.         mov     eax,    [vsha]
  1255.         shr     eax,    8
  1256.         mov     ah,     0
  1257.         shl     eax,    8
  1258.         mov     ah,     byte[c]
  1259.         mov     al,     byte[x]
  1260.          jmp    .done
  1261.  
  1262. @@:     cmp     al,     171
  1263.          jae    @f
  1264.         mov     eax,    [vsha]
  1265.         shr     eax,    8
  1266.         mov     ah,     0
  1267.         shl     eax,    8
  1268.         mov     ah,     byte[x]
  1269.         mov     al,     byte[c]
  1270.          jmp    .done
  1271.  
  1272. @@:     cmp     al,     214
  1273.          jae    @f
  1274.         mov     eax,    [vsha]
  1275.         shr     eax,    8
  1276.         mov     ah,     byte[x]
  1277.         shl     eax,    8
  1278.         mov     ah,     0
  1279.         mov     al,     byte[c]
  1280.          jmp    .done
  1281.  
  1282. @@:     mov     eax,    [vsha]
  1283.         shr     eax,    8
  1284.         mov     ah,     byte[c]
  1285.         shl     eax,    8
  1286.         mov     ah,     0
  1287.         mov     al,     byte[x]
  1288. ;        jmp    .done
  1289.  
  1290. .done:
  1291.         mov     bl,     byte[vsha]
  1292.         sub     bl,     byte[c]
  1293.         ror     eax,    8
  1294.         add     ah,     bl
  1295.         rol     eax,    8
  1296.         add     ah,     bl
  1297.         add     al,     bl
  1298.  
  1299. .quit: pop     edx ecx ebx
  1300.         ret
  1301. endp
  1302.  
  1303.  
  1304. proc pixel_rgb2hsl
  1305. ; http://www.asmcommunity.net/board/index.php?topic=7425
  1306. ; iblis: "I don't know what X-Filez is, but yes you may use it however you wish.  That's why I made this post, to share."
  1307. ; so pixel_rgb2hsl procedure is based on code by Greg Hoyer (iblis). thanks!
  1308. ;--------------------------------------------------------------;
  1309. ; By Greg Hoyer aka "Iblis"                                    ;
  1310. ;                                                              ;
  1311. ; RGB2HSL converts a COLORREF  oriented dword filled with 8bit ;
  1312. ; Red/Green/Blue  values  (00ggbbrr) to  a similarly  oriented ;
  1313. ; dword filled with Hue/Saturation/Luminance values (00llsshh) ;
  1314. ; This procedure  returns the  full range,  from 0-255.   This ;
  1315. ; offers slightly more  precision over Windows' "color picker" ;
  1316. ; common dialog, which displays HSL values ranging from 0-240. ;
  1317. ;                                                              ;
  1318. ; It is important to note  that true HSL  values are  normally ;
  1319. ; represented as  floating point  fractions from  0.0 to  1.0. ;
  1320. ; As such, this  algorithm cannot  be used to  do the precise, ;
  1321. ; consistent conversions  that may  be required  by heavy-duty ;
  1322. ; graphics  applications.  To  get the  decimal  fraction  for ;
  1323. ; the  returned values,  convert  the Hue,  Saturation, and/or ;
  1324. ; Luminance values to floating  point, and then divide by 255. ;
  1325. ;--------------------------------------------------------------;
  1326. locals
  1327. bgra        rd  1
  1328. endl
  1329.        push    ebx esi edi
  1330.  
  1331.         mov     [bgra], eax
  1332.  
  1333.         movzx   esi,    byte[bgra+0]
  1334.         movzx   edi,    byte[bgra+1]
  1335.         movzx   ebx,    byte[bgra+2]
  1336.         mov     cl,     -1
  1337.         cmp     esi,    edi
  1338.          ja     .cmp1
  1339.         xchg    esi,    edi
  1340.         neg     cl
  1341.         shl     cl,     1
  1342. .cmp1:  cmp     edi,    ebx
  1343.          jb     .cmp2
  1344.         xchg    edi,    ebx
  1345.         neg     cl
  1346. .cmp2:  cmp     esi,    ebx
  1347.          ja     .cmp3
  1348.         xchg    esi,    ebx
  1349.         not     cl
  1350. .cmp3:  neg     ebx
  1351.         add     ebx,    esi
  1352.         mov     eax,    edi
  1353.         add     edi,    esi
  1354.          jz     .done
  1355.         sub     esi,    eax
  1356.          jz     .done
  1357.         mov     eax,    esi
  1358.         shl     eax,    8
  1359.         sub     eax,    esi
  1360.        push edi
  1361.         cmp     edi,    0xff
  1362.          jbe    .csat
  1363.         neg     edi
  1364.         add     edi,    510
  1365. .csat:  xor     edx,    edx
  1366.         div     edi
  1367.        pop      edi
  1368.         shr     edi,    1
  1369.         shl     eax,    8
  1370.         or      edi,    eax
  1371.         add     cl,     3
  1372.          jnc    .noneg
  1373.         neg     ebx
  1374. .noneg: shl     cl,     2
  1375.         mov     eax,    0x13135db9
  1376.         shr     eax,    cl
  1377.         and     eax,    7
  1378.         mul     esi
  1379.         add     eax,    ebx
  1380.         mov     ebx,    eax
  1381.         shl     eax,    8
  1382.         sub     eax,    ebx
  1383.         mov     ebx,    esi
  1384.         shl     esi,    1
  1385.         lea     ebx,    [ebx*4+esi]
  1386.         xor     edx,    edx
  1387.         div     ebx
  1388.         shl     eax,    16
  1389.         or      eax,    edi
  1390. .done:  bswap   eax
  1391.         shr     eax,    8
  1392.  
  1393.         mov     bl,     byte[bgra+3]
  1394.         bswap   eax
  1395.         mov     al,     bl
  1396.         ror     eax,    8
  1397.  
  1398.  
  1399.        pop     edi esi ebx
  1400.         ret
  1401. endp
  1402.  
  1403.  
  1404. proc pixel_hsl2rgb
  1405. ; http://www.asmcommunity.net/board/index.php?topic=7425
  1406. ; iblis: "I don't know what X-Filez is, but yes you may use it however you wish.  That's why I made this post, to share."
  1407. ; so pixel_hsl2rgb procedure is based on code by Greg Hoyer (iblis). thanks!
  1408. ;--------------------------------------------------------------;
  1409. ; By Greg Hoyer aka "Iblis"                                    ;
  1410. ;                                                              ;
  1411. ; HSL2RGB  does  the  opposite  of  RGB2HSL.   It  converts  a ;
  1412. ; Hue/Saturation/Luminance  (00llsshh)  dword  back  into  its ;
  1413. ; corresponding  RGB  COLORREF  (00bbggrr).  This  function is ;
  1414. ; intented  to be  used exclusively  with RGB2HSL  (see above) ;
  1415. ;                                                              ;
  1416. ; If  you're  using  this for  your own  custom  color-chooser ;
  1417. ; dialog, remember that the  values are in the range of 0-255. ;
  1418. ; If you MUST emulate the Windows'  color-chooser, convert HSL ;
  1419. ; values this way before you display them:                     ;
  1420. ;                                                              ;
  1421. ; display_value = ( x * 240 ) / 255                            ;
  1422. ;                                                              ;
  1423. ; ...where x represents any one of the HSL values.             ;
  1424. ;--------------------------------------------------------------;
  1425. locals
  1426. lsha        rd  1
  1427. endl
  1428.  
  1429.        push    ebx esi edi
  1430.        
  1431.         mov     [lsha], eax
  1432.  
  1433.         movzx   ebx,    byte[lsha+0]
  1434.         lea     esi,    [ebx*2]
  1435.         movzx   edi,    byte[lsha+1]
  1436.         xor     eax,    eax
  1437.         mov     cl,     1
  1438.         cmp     bl,     0x7f
  1439.          ja     .lcase
  1440.         dec     al
  1441.         xor     ecx,    ecx
  1442. .lcase: add     eax,    edi
  1443.         mul     ebx
  1444.         or      ecx,    ecx
  1445.          jz     .scase
  1446.         neg     eax
  1447.         mov     ecx,    ebx
  1448.         add     ecx,    edi
  1449.         mov     edx,    ecx
  1450.         shl     ecx,    8
  1451.         sub     ecx,    edx
  1452.         add     eax,    ecx
  1453. .scase: xor     edx,    edx
  1454.         xor     ecx,    ecx
  1455.         dec     cl
  1456.         mov     edi,    ecx
  1457.         div     ecx
  1458.          jz     .done
  1459.         mov     ecx,    eax
  1460.         sub     esi,    eax
  1461.         movzx   eax,    byte[lsha+2]
  1462.         mov     ebx,    eax
  1463.         shl     eax,    1
  1464.         lea     eax,    [ebx*4+eax]
  1465.         xor     edx,    edx
  1466.         div     edi
  1467.         mov     ebx,    eax
  1468.         mov     eax,    ecx
  1469.         sub     eax,    esi
  1470.         mul     edx
  1471.        push ebx
  1472.         mov     ebx,    ecx
  1473.         shl     ebx,    8
  1474.         sub     ebx,    ecx
  1475.         sub     ebx,    eax
  1476.         xchg    eax,    ebx
  1477.         xor     edx,    edx
  1478.         div     edi
  1479.         shl     eax,    24
  1480.         or      ecx,    eax
  1481.         mov     eax,    esi
  1482.         shl     eax,    8
  1483.         sub     eax,    esi
  1484.         shl     esi,    16
  1485.         or      ecx,    esi
  1486.         add     eax,    ebx
  1487.         xor     edx,    edx
  1488.         div     edi
  1489.         mov     ch,     al
  1490.         mov     eax,    ecx
  1491.        pop      ecx
  1492.         cmp     cl,     6
  1493.          jz     .done
  1494.         or      ecx,    ecx
  1495.          jz     .done
  1496.         bswap   eax
  1497.         rol     eax,    8
  1498.         xchg    ah,     al
  1499.         dec     ecx
  1500.          jz     .done
  1501.         ror     eax,    8
  1502.         xchg    ah,     al
  1503.         dec     ecx
  1504.          jz     .done
  1505.         rol     eax,    8
  1506.         xchg    ah,     al
  1507.         dec     ecx
  1508.          jz     .done
  1509.         bswap   eax
  1510.         rol     eax,    8
  1511.         xchg    ah,     al
  1512.         dec     ecx
  1513.          jz     .done
  1514.         ror     eax,    8
  1515.         xchg    ah,     al
  1516. .done:  and     eax,    0x00ffffff
  1517.  
  1518.         mov     bl,     byte[lsha+3]
  1519.         bswap   eax
  1520.         mov     al,     bl
  1521.         ror     eax,    8
  1522.  
  1523.         pop     edi esi ebx
  1524.         ret
  1525. endp
  1526.  
  1527.  
  1528. match =MMX,COMPOSITE_MODE{include 'composite_mmx.inc'}
  1529. ;;================================================================================================;;
  1530. ;;////////////////////////////////////////////////////////////////////////////////////////////////;;
  1531. ;;================================================================================================;;
  1532. ;! Below is private data you should never use directly from your code                             ;;
  1533. ;;================================================================================================;;
  1534. ;;////////////////////////////////////////////////////////////////////////////////////////////////;;
  1535. ;;================================================================================================;;
  1536. xcf_prop_table_begin:
  1537. dd 00, parse_prop_00
  1538. dd 01, parse_prop_01
  1539. dd 06, parse_prop_06
  1540. dd 07, parse_prop_07
  1541. dd 08, parse_prop_08
  1542. dd 11, parse_prop_11
  1543. dd 15, parse_prop_15
  1544. xcf_prop_table_end:
  1545.  
  1546. composite_table_begin:
  1547. .p00 dd 00, composite_rgb_00, composite_gray_00, composite_indexed_00   ; Normal
  1548. .p01 dd 01, composite_rgb_01, composite_gray_01, composite_gray_01      ; Dissolve      : random dithering to discrete alpha
  1549. ;.p02 dd 02, composite_rgb_02, 0, composite_indexed_02                  ; Behind        : not selectable in the GIMP UI. not implemented
  1550. .p03 dd 03, composite_rgb_03, composite_rgb_03, composite_indexed_00    ; Multiply
  1551. .p04 dd 04, composite_rgb_04, composite_rgb_04, composite_indexed_00    ; Screen
  1552. .p05 dd 05, composite_rgb_05, composite_rgb_05, composite_indexed_00    ; Overlay
  1553. .p06 dd 06, composite_rgb_06, composite_rgb_06, composite_indexed_00    ; Difference
  1554. .p07 dd 07, composite_rgb_07, composite_rgb_07, composite_indexed_00    ; Addition
  1555. .p08 dd 08, composite_rgb_08, composite_rgb_08, composite_indexed_00    ; Subtract
  1556. .p09 dd 09, composite_rgb_09, composite_rgb_09, composite_indexed_00    ; Darken Only
  1557. .p10 dd 10, composite_rgb_10, composite_rgb_10, composite_indexed_00    ; Lighten Only
  1558. .p11 dd 11, composite_rgb_11, composite_gray_00, composite_indexed_00   ; Hue (H of HSV)
  1559. .p12 dd 12, composite_rgb_12, composite_gray_00, composite_indexed_00   ; Saturation (S of HSV)
  1560. .p13 dd 13, composite_rgb_13, composite_gray_00, composite_indexed_00   ; Color (H and S of HSL)
  1561. .p14 dd 14, composite_rgb_14, composite_gray_00, composite_indexed_00   ; Value (V of HSV)
  1562. .p15 dd 15, composite_rgb_15, composite_rgb_15, composite_indexed_00    ; Divide
  1563. .p16 dd 16, composite_rgb_16, composite_rgb_16, composite_indexed_00    ; Dodge
  1564. .p17 dd 17, composite_rgb_17, composite_rgb_17, composite_indexed_00    ; Burn
  1565. .p18 dd 18, composite_rgb_18, composite_rgb_18, composite_indexed_00    ; Hard Light
  1566. .p19 dd 19, composite_rgb_05, composite_rgb_05, composite_indexed_00    ; Soft Light    : XCF version >= 2 only ('soft light' == 'overlay')
  1567. .p20 dd 20, composite_rgb_20, composite_rgb_20, composite_indexed_00    ; Grain Extract : XCF version >= 2 only
  1568. .p21 dd 21, composite_rgb_21, composite_rgb_21, composite_indexed_00    ; Grain Merge   : XCF version >= 2 only
  1569. composite_table_end:
  1570.  
  1571. random_a        dd  1103515245
  1572. random_b        dd  777
  1573. random_c        dd  12345