Subversion Repositories Kolibri OS

Rev

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

  1. ;;================================================================================================;;
  2. ;;//// convert.asm //// (c) dunkaist, 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. ;;================================================================================================;;
  21. proc img.convert _src, _dst, _dst_type, _flags, _param                                            ;;
  22. ;;------------------------------------------------------------------------------------------------;;
  23. ;? scale _image                                                                                   ;;
  24. ;;------------------------------------------------------------------------------------------------;;
  25. ;> [_src]      = pointer to source image                                                          ;;
  26. ;> [_flags]    = see libimg.inc                                                                   ;;
  27. ;> [_dst_type] = the Image.Type of converted image                                                ;;
  28. ;> [_dst]      = pointer to destination image, if any                                             ;;
  29. ;;------------------------------------------------------------------------------------------------;;
  30. ;< eax = 0 / pointer to converted image                                                           ;;
  31. ;< ecx = error code / undefined                                                                   ;;
  32. ;;================================================================================================;;
  33. locals
  34.         width   rd 1
  35.         height  rd 1
  36. endl
  37.         mov     ebx, [_src]
  38.         mov     eax, 1
  39.         mov     ecx, [_dst_type]
  40.         shl     eax, cl
  41.         mov     ecx, [ebx + Image.Type]
  42.         test    eax, [img.types_table + 4*ecx]
  43.         jnz     @f
  44.         mov     ecx, LIBIMG_ERROR_BIT_DEPTH
  45.         jmp     .error
  46.     @@:
  47.         mov     eax, [_dst]
  48.         test    eax, eax
  49.         jnz     @f
  50.         stdcall img.create, [ebx + Image.Width], [ebx + Image.Height], [_dst_type]
  51.         test    eax, eax
  52.         jz      .error
  53.         mov     [_dst], eax
  54.     @@:
  55.         mov     edi, [eax + Image.Data]
  56.         mov     esi, [ebx + Image.Data]
  57.         mov     eax, [ebx + Image.Type]
  58.         cmp     eax, Image.bpp8i
  59.         je      .bpp8i
  60.         cmp     eax, Image.bpp8g
  61.         je      .bpp8g
  62.         cmp     eax, Image.bpp24
  63.         je      .bpp24
  64.         cmp     eax, Image.bpp32
  65.         je      .bpp32
  66.         cmp     eax, Image.bpp15
  67.         je      .bpp15
  68.         cmp     eax, Image.bpp16
  69.         je      .bpp16
  70.         cmp     eax, Image.bpp1
  71.         je      .bpp1
  72.         cmp     eax, Image.bpp8a
  73.         je      .bpp8a
  74.         mov     ecx, LIBIMG_ERROR_BIT_DEPTH
  75.         jmp     .error
  76.  
  77.   .find_in_table_and_jump:
  78.         mov     ecx, [_dst_type]
  79.     @@:
  80.         mov     eax, [edx]
  81.         add     edx, 8
  82.         cmp     eax, ecx
  83.         jne     @b
  84.         jmp     dword[edx - 4]
  85.  
  86.  
  87.   .bpp8i:
  88.         mov     edx, img.convert.bpp8i.table
  89.         jmp     .find_in_table_and_jump
  90.   .bpp8i_to_bpp24:
  91.         mov     ecx, [ebx + Image.Width]
  92.         imul    ecx, [ebx + Image.Height]
  93.  
  94.         mov     ebx, [ebx + Image.Palette]
  95.         sub     ecx, 1
  96.         jz      .bpp8i.last
  97.     @@:
  98.         movzx   eax, byte[esi]
  99.         add     esi, 1
  100.         mov     eax, [ebx + eax*4]
  101.         mov     [edi], eax
  102.         add     edi, 3
  103.         sub     ecx, 1
  104.         jnz     @b
  105.   .bpp8i.last:
  106.         movzx   eax, byte[esi]
  107.         mov     eax, [ebx + eax*4]
  108.         mov     [edi], ax
  109.         shr     eax, 16
  110.         mov     [edi + 2], al
  111.         mov     eax, [_dst]
  112.         jmp     .quit
  113.  
  114.  
  115.  
  116.   .bpp8g:
  117.         mov     edx, img.convert.bpp8g.table
  118.         jmp     .find_in_table_and_jump
  119.   .bpp8g_to_bpp1:
  120.         mov     eax, [_dst]
  121.         mov     eax, [eax + Image.Palette]
  122.         mov     dword[eax], 0x00000000
  123.         mov     dword[eax + 4], 0x00ffffff
  124.         mov     edx, [ebx + Image.Height]
  125.   .bpp8g_to_bpp1.line:
  126.         mov     ax, 0x0800
  127.         mov     ecx, [ebx + Image.Width]
  128.   .bpp8g_to_bpp1.pixel:
  129.         shl     al, 1
  130.         cmp     byte[esi], 0x7f
  131.         cmc
  132.         adc     eax, 0
  133.         add     esi, 1
  134.         dec     ah
  135.         jnz     @f
  136.         mov     byte[edi], al
  137.         add     edi, 1
  138.         mov     ax, 0x0800
  139.     @@:
  140.         dec     ecx
  141.         jnz     .bpp8g_to_bpp1.pixel
  142.         cmp     ah, 8
  143.         je      @f
  144.         mov     cl, ah
  145.         shl     al, cl
  146.         mov     byte[edi], al
  147.         add     edi, 1
  148.     @@:
  149.         dec     edx
  150.         jnz     .bpp8g_to_bpp1.line
  151.         mov     eax, [_dst]
  152.         jmp     .quit
  153.  
  154.   .bpp8g_to_bpp24:
  155.         mov     ecx, [ebx + Image.Width]
  156.         imul    ecx, [ebx + Image.Height]
  157.     @@:
  158.         mov     al, byte[esi]
  159.         mov     byte[edi + 0], al
  160.         mov     byte[edi + 1], al
  161.         mov     byte[edi + 2], al
  162.         add     esi, 1
  163.         add     edi, 3
  164.         sub     ecx, 1
  165.         jnz     @b
  166.         mov     eax, [_dst]
  167.         jmp     .quit
  168.  
  169.   .bpp24:
  170.         mov     edx, img.convert.bpp24.table
  171.         jmp     .find_in_table_and_jump
  172.   .bpp24_to_bpp24:
  173.         mov     ecx, [ebx + Image.Width]
  174.         imul    ecx, [ebx + Image.Height]
  175.         lea     ecx, [ecx*3]
  176.         mov     edx, ecx
  177.         shr     ecx, 2
  178.         rep     movsd
  179.         mov     ecx, edx
  180.         and     ecx, 3
  181.         rep     movsb
  182.         mov     eax, [_dst]
  183.         jmp     .quit
  184.   .bpp24_to_bpp8g:
  185.         mov     ecx, [ebx + Image.Width]
  186.         imul    ecx, [ebx + Image.Height]
  187.     @@:
  188.         movzx   ebx, byte[esi + 0]
  189.         movzx   eax, byte[esi + 1]
  190.         add     ebx, eax
  191.         movzx   eax, byte[esi + 2]
  192.         add     eax, ebx
  193.         mov     ebx, 3
  194.         add     esi, 3
  195.         div     bl
  196.         mov     byte[edi], al
  197.         add     edi, 1
  198.         sub     ecx, 1
  199.         jnz     @b
  200.         mov     eax, [_dst]
  201.         jmp     .quit
  202.  
  203.  
  204.   .bpp32:
  205.         mov     edx, img.convert.bpp32.table
  206.         jmp     .find_in_table_and_jump
  207.   .bpp32_to_bpp24:
  208.         mov     ecx, [ebx + Image.Width]
  209.         imul    ecx, [ebx + Image.Height]
  210.     @@:
  211.         mov     eax, [esi]
  212.         mov     [edi], ax
  213.         shr     eax, 16
  214.         mov     [edi + 2], al
  215.         add     esi, 4
  216.         add     edi, 3
  217.         sub     ecx, 1
  218.         jnz     @b
  219.         mov     eax, [_dst]
  220.         jmp     .quit
  221.  
  222.  
  223.   .bpp15:
  224.         mov     edx, img.convert.bpp15.table
  225.         jmp     .find_in_table_and_jump
  226.   .bpp15_to_bpp24:
  227.         mov     ecx, [ebx + Image.Width]
  228.         imul    ecx, [ebx + Image.Height]
  229.  
  230.   .bpp15.intel: ; copypasted from do_rgb
  231.         push    ebx ebp
  232.         sub     ecx, 4
  233.         jb      .bpp15.tail
  234. align 16
  235.   .bpp15.intel.loop:
  236. repeat 2
  237.         mov     ebx, [esi]
  238.         mov     al, [esi]
  239.         mov     ah, [esi + 1]
  240.         add     esi, 4
  241.         and     al, 0x1F
  242.         and     ah, 0x1F shl 2
  243.         mov     ebp, ebx
  244.         mov     dl, al
  245.         mov     dh, ah
  246.         shr     al, 2
  247.         shr     ah, 4
  248.         shl     dl, 3
  249.         shl     dh, 1
  250.         and     ebp, 0x1F shl 5
  251.         add     al, dl
  252.         add     ah, dh
  253.         shr     ebp, 2
  254.         mov     [edi], al
  255.         mov     [edi + 2], ah
  256.         mov     eax, ebx
  257.         mov     ebx, ebp
  258.         shr     eax, 16
  259.         shr     ebx, 5
  260.         add     ebx, ebp
  261.         mov     ebp, eax
  262.         mov     [edi + 1], bl
  263.         and     eax, (0x1F) or (0x1F shl 10)
  264.         and     ebp, 0x1F shl 5
  265.         lea     edx, [eax + eax]
  266.         shr     al, 2
  267.         mov     ebx, ebp
  268.         shr     ah, 4
  269.         shl     dl, 2
  270.         shr     ebx, 2
  271.         shr     ebp, 7
  272.         add     al, dl
  273.         add     ah, dh
  274.         mov     [edi + 3], al
  275.         add     ebx, ebp
  276.         mov     [edi + 5], ah
  277.         mov     [edi + 4], bl
  278.         add     edi, 6
  279. end repeat
  280.         sub     ecx, 4
  281.         jnb     .bpp15.intel.loop
  282.   .bpp15.tail:
  283.         add     ecx, 4
  284.         jz      .bpp15.done
  285.     @@:
  286.         movzx   eax, word [esi]
  287.         mov     ebx, eax
  288.         add     esi, 2
  289.         and     eax, (0x1F) or (0x1F shl 10)
  290.         and     ebx, 0x1F shl 5
  291.         lea     edx, [eax + eax]
  292.         shr     al, 2
  293.         mov     ebp, ebx
  294.         shr     ebx, 2
  295.         shr     ah, 4
  296.         shl     dl, 2
  297.         shr     ebp, 7
  298.         add     eax, edx
  299.         add     ebx, ebp
  300.         mov     [edi], al
  301.         mov     [edi + 1], bl
  302.         mov     [edi + 2], ah
  303.         add     edi, 3
  304.         sub     ecx, 1
  305.         jnz     @b
  306.   .bpp15.done:
  307.         pop     ebp ebx
  308.         mov     eax, [_dst]
  309.         jmp     .quit
  310.  
  311.   .bpp15.amd:
  312.         push    ebx ebp
  313.         sub     ecx, 4
  314.         jb      .bpp15.tail
  315. align 16
  316.   .bpp15.amd.loop:
  317. repeat 4
  318. if (% mod 2) = 1
  319.         mov     eax, dword[esi]
  320.         mov     ebx, dword[esi]
  321. else
  322.         movzx   eax, word[esi]
  323.         mov     ebx, eax
  324. end if
  325.         add     esi, 2
  326.         and     eax, (0x1F) or (0x1F shl 10)
  327.         and     ebx, 0x1F shl 5
  328.         lea     edx, [eax + eax]
  329.         shr     al, 2
  330.         mov     ebp, ebx
  331.         shr     ebx, 2
  332.         shr     ah, 4
  333.         shl     dl, 2
  334.         shr     ebp, 7
  335.         add     eax, edx
  336.         add     ebx, ebp
  337.         mov     [edi], al
  338.         mov     [edi + 1], bl
  339.         mov     [edi + 2], ah
  340.         add     edi, 3
  341. end repeat
  342.         sub     ecx, 4
  343.         jnb     .bpp15.amd.loop
  344.         jmp     .bpp15.tail
  345.  
  346.  
  347.   .bpp16:
  348.         mov     edx, img.convert.bpp16.table
  349.         jmp     .find_in_table_and_jump
  350.   .bpp16_to_bpp24:
  351.         mov     ecx, [ebx + Image.Width]
  352.         imul    ecx, [ebx + Image.Height]
  353.   .bpp16.intel:
  354.         push    ebx ebp
  355.         sub     ecx, 4
  356.         jb      .bpp16.tail
  357. align 16
  358.   .bpp16.intel.loop:
  359. repeat 2
  360.         mov     ebx, [esi]
  361.         mov     al, [esi]
  362.         mov     ah, [esi + 1]
  363.         add     esi, 4
  364.         and     al, 0x1F
  365.         and     ah, 0x1F shl 3
  366.         mov     ebp, ebx
  367.         mov     dl, al
  368.         mov     dh, ah
  369.         shr     al, 2
  370.         shr     ah, 5
  371.         shl     dl, 3
  372.         and     ebp, 0x3F shl 5
  373.         add     al, dl
  374.         add     ah, dh
  375.         shr     ebp, 3
  376.         mov     [edi], al
  377.         mov     [edi + 2], ah
  378.         mov     eax, ebx
  379.         mov     ebx, ebp
  380.         shr     eax, 16
  381.         shr     ebx, 6
  382.         add     ebx, ebp
  383.         mov     ebp, eax
  384.         mov     [edi + 1], bl
  385.         and     eax, (0x1F) or (0x1F shl 11)
  386.         and     ebp, 0x3F shl 5
  387.         mov     edx, eax
  388.         shr     al, 2
  389.         mov     ebx, ebp
  390.         shr     ah, 5
  391.         shl     dl, 3
  392.         shr     ebx, 3
  393.         shr     ebp, 9
  394.         add     al, dl
  395.         add     ah, dh
  396.         mov     [edi + 3], al
  397.         add     ebx, ebp
  398.         mov     [edi + 5], ah
  399.         mov     [edi + 4], bl
  400.         add     edi, 6
  401. end repeat
  402.         sub     ecx, 4
  403.         jnb     .bpp16.intel.loop
  404.   .bpp16.tail:
  405.         add     ecx, 4
  406.         jz      .bpp16.done
  407.     @@:
  408.         movzx   eax, word[esi]
  409.         mov     ebx, eax
  410.         add     esi, 2
  411.         and     eax, (0x1F) or (0x1F shl 11)
  412.         and     ebx, 0x3F shl 5
  413.         mov     edx, eax
  414.         shr     al, 2
  415.         mov     ebp, ebx
  416.         shr     ebx, 3
  417.         shr     ah, 5
  418.         shl     dl, 3
  419.         shr     ebp, 9
  420.         add     eax, edx
  421.         add     ebx, ebp
  422.         mov     [edi], al
  423.         mov     [edi + 1], bl
  424.         mov     [edi + 2], ah
  425.         add     edi, 3
  426.         sub     ecx, 1
  427.         jnz     @b
  428.   .bpp16.done:
  429.         pop     ebp ebx
  430.         mov     eax, [_dst]
  431.         jmp     .quit
  432.  
  433.   .bpp16.amd:
  434.         push    ebx ebp
  435.         sub     ecx, 4
  436.         jb      .bpp16.tail
  437. align 16
  438.   .bpp16.amd.loop:
  439. repeat 4
  440. if (% mod 2) = 1
  441.         mov     eax, dword[esi]
  442.         mov     ebx, dword[esi]
  443. else
  444.         movzx   eax, word[esi]
  445.         mov     ebx, eax
  446. end if
  447.         add     esi, 2
  448.         and     eax, (0x1F) or (0x1F shl 11)
  449.         and     ebx, 0x3F shl 5
  450.         mov     edx, eax
  451.         shr     al, 2
  452.         mov     ebp, ebx
  453.         shr     ebx, 3
  454.         shr     ah, 5
  455.         shl     dl, 3
  456.         shr     ebp, 9
  457.         add     eax, edx
  458.         add     ebx, ebp
  459.         mov     [edi], al
  460.         mov     [edi + 1], bl
  461.         mov     [edi + 2], ah
  462.         add     edi, 3
  463. end repeat
  464.         sub     ecx, 4
  465.         jnb     .bpp16.amd.loop
  466.         jmp     .bpp16.tail
  467.  
  468.  
  469.   .bpp1:
  470.         mov     edx, img.convert.bpp1.table
  471.         jmp     .find_in_table_and_jump
  472.   .bpp1_to_bpp24:
  473.         push    [ebx + Image.Width]
  474.         pop     [width]
  475.         push    [ebx + Image.Height]
  476.         pop     [height]
  477.         mov     edx, [ebx + Image.Palette]
  478.   .bpp1_to_bpp24.line:
  479.         mov     ebx, [width]
  480.   .bpp1_to_bpp24.byte:
  481.         mov     ah, 8
  482.         mov     al, byte[esi]
  483.         add     esi, 1
  484.   .bpp1_to_bpp24.bit:
  485.         xor     ecx, ecx
  486.         shl     al, 1
  487.         adc     ecx, 0
  488.         mov     ecx, [edx + 4*ecx]
  489.         mov     word[edi], cx
  490.         shr     ecx, 8
  491.         mov     byte[edi + 2], ch
  492.         add     edi, 3
  493.         sub     ebx, 1
  494.         jnz     @f
  495.         sub     [height], 1
  496.         jnz     .bpp1_to_bpp24.line
  497.         jmp     .bpp1.done
  498.     @@:
  499.         sub     ah, 1
  500.         jnz     .bpp1_to_bpp24.bit
  501.         jmp     .bpp1_to_bpp24.byte
  502.   .bpp1.done:
  503.         mov     eax, [_dst]
  504.         jmp     .quit
  505.  
  506.  
  507.   .bpp8a:
  508.         mov     edx, img.convert.bpp8a.table
  509.         jmp     .find_in_table_and_jump
  510.   .bpp8a_to_bpp1:
  511.         mov     eax, [_dst]
  512.         mov     eax, [eax + Image.Palette]
  513.         mov     dword[eax], 0x00000000
  514.         mov     dword[eax + 4], 0x00ffffff
  515.         mov     edx, [ebx + Image.Height]
  516.   .bpp8a_to_bpp1.line:
  517.         mov     ax, 0x0800
  518.         mov     ecx, [ebx + Image.Width]
  519.   .bpp8a_to_bpp1.pixel:
  520.         shl     al, 1
  521.         cmp     byte[esi], 0x7f
  522.         cmc
  523.         adc     eax, 0
  524.         add     esi, 2
  525.         dec     ah
  526.         jnz     @f
  527.         mov     byte[edi], al
  528.         add     edi, 1
  529.         mov     ax, 0x0800
  530.     @@:
  531.         dec     ecx
  532.         jnz     .bpp8a_to_bpp1.pixel
  533.         cmp     ah, 8
  534.         je      @f
  535.         mov     cl, ah
  536.         shl     al, cl
  537.         mov     byte[edi], al
  538.         add     edi, 1
  539.     @@:
  540.         dec     edx
  541.         jnz     .bpp8a_to_bpp1.line
  542.         mov     eax, [_dst]
  543.         jmp     .quit
  544.  
  545.   .bpp8a_to_bpp24:
  546.         mov     ecx, [ebx + Image.Width]
  547.         imul    ecx, [ebx + Image.Height]
  548.     @@:
  549.         mov     al, byte[esi]
  550.         mov     byte[edi + 0], al
  551.         mov     byte[edi + 1], al
  552.         mov     byte[edi + 2], al
  553.         add     esi, 2
  554.         add     edi, 3
  555.         sub     ecx, 1
  556.         jnz     @b
  557.         mov     eax, [_dst]
  558.         jmp     .quit
  559.  
  560.  
  561.   .error:
  562.         xor     eax, eax
  563.   .quit:
  564.         ret
  565. endp
  566.  
  567.  
  568. img.convert.bpp8i.table:
  569.         dd Image.bpp24, img.convert.bpp8i_to_bpp24
  570. img.convert.bpp8g.table:
  571.         dd Image.bpp24, img.convert.bpp8g_to_bpp24
  572.         dd Image.bpp1,  img.convert.bpp8g_to_bpp1
  573. img.convert.bpp24.table:
  574.         dd Image.bpp24, img.convert.bpp24_to_bpp24
  575.         dd Image.bpp8g, img.convert.bpp24_to_bpp8g
  576. img.convert.bpp32.table:
  577.         dd Image.bpp24, img.convert.bpp32_to_bpp24
  578. img.convert.bpp15.table:
  579.         dd Image.bpp24, img.convert.bpp15_to_bpp24
  580. img.convert.bpp16.table:
  581.         dd Image.bpp24, img.convert.bpp16_to_bpp24
  582. img.convert.bpp1.table:
  583.         dd Image.bpp24, img.convert.bpp1_to_bpp24
  584. img.convert.bpp8a.table:
  585.         dd Image.bpp24, img.convert.bpp8a_to_bpp24
  586.