Subversion Repositories Kolibri OS

Rev

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

  1. ;;================================================================================================;;
  2. ;;//// pcx.asm //// (c) dunkaist, 2010,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 'pcx.inc'
  21. ;include '../../../../system/board/trunk/debug.inc'
  22.  
  23. ;;================================================================================================;;
  24. proc img.is.pcx _data, _length ;//////////////////////////////////////////////////////////////////;;
  25. ;;------------------------------------------------------------------------------------------------;;
  26. ;? Determine if raw data could be decoded (is in pcx format)                                      ;;
  27. ;;------------------------------------------------------------------------------------------------;;
  28. ;> _data = raw data as read from file/stream                                                      ;;
  29. ;> _length = data length                                                                          ;;
  30. ;;------------------------------------------------------------------------------------------------;;
  31. ;< eax = false / true                                                                             ;;
  32. ;;================================================================================================;;
  33.  
  34.         push    ecx edi
  35.         xor     eax, eax
  36.  
  37.         mov     edi, [_data]
  38.  
  39.         cmp     byte[edi + pcx_header.magic_number], 0x0A
  40.         jne     .is_not_pcx
  41.         cmp     byte[edi + pcx_header.version], 5
  42.         jne     .is_not_pcx
  43.         cmp     byte[edi + pcx_header.encoding], 1
  44.         jne     .is_not_pcx
  45.         cmp     byte[edi + pcx_header.reserved], 0
  46.         jne     .is_not_pcx
  47.  
  48.         add     edi, pcx_header.filler
  49.         xor     al, al
  50.         mov     ecx, 58
  51.         cld
  52.         repe    scasb
  53.         test    ecx, ecx
  54.         jnz     .is_not_pcx
  55.  
  56.   .is_pcx:
  57.         inc     eax
  58.  
  59.   .is_not_pcx:
  60.         pop     edi ecx
  61.         ret
  62. endp
  63.  
  64.  
  65. ;;================================================================================================;;
  66. proc img.decode.pcx _data, _length, _options ;////////////////////////////////////////////////////;;
  67. ;;------------------------------------------------------------------------------------------------;;
  68. ;? Decode data into image if it contains correctly formed raw data in pcx format                  ;;
  69. ;;------------------------------------------------------------------------------------------------;;
  70. ;> _data = raw data as read from file/stream                                                      ;;
  71. ;> _length = data length                                                                          ;;
  72. ;;------------------------------------------------------------------------------------------------;;
  73. ;< eax = 0 (error) or pointer to image                                                            ;;
  74. ;;================================================================================================;;
  75. locals
  76.         nplanes                 rd      1
  77.         xsize                   rd      1
  78.         ysize                   rd      1
  79.         bpl                     rd      1
  80.         total_bpl               rd      1
  81.         line_begin              rd      1
  82.         retvalue                rd      1               ; 0 (error) or pointer to image
  83. endl
  84.  
  85.         pusha
  86.  
  87.         mov     esi, [_data]
  88.         movzx   eax, byte[esi + pcx_header.nplanes]
  89.         mov     [nplanes], eax
  90.         movzx   ebx, word[esi + pcx_header.bpl]
  91.         mov     [bpl], ebx
  92.         imul    eax, ebx
  93.         mov     [total_bpl], eax
  94.  
  95.         movzx   eax, word[esi + pcx_header.xmax]
  96.         sub     ax, word[esi + pcx_header.xmin]
  97.         inc     eax
  98.         mov     [xsize], eax
  99.  
  100.         movzx   ebx, word[esi + pcx_header.ymax]
  101.         sub     bx, word[esi + pcx_header.ymin]
  102.         inc     ebx
  103.         mov     [ysize], ebx
  104.  
  105.         cmp     [esi + pcx_header.bpp], 1
  106.         jz      .monochrome
  107.         cmp     byte[esi + pcx_header.nplanes], 3
  108.         jnz     .indexed
  109.  
  110.  
  111.   .24bit:
  112.  
  113.         stdcall img.create, eax, ebx, Image.bpp24
  114.         mov     [retvalue], eax
  115.         test    eax, eax
  116.         jz      .quit
  117.  
  118.         mov     esi, [_data]
  119.         add     esi, 128                        ; skip header
  120.         mov     edi, [eax + Image.Data]
  121.         add     edi, 2
  122.         mov     [line_begin], edi
  123.  
  124.   .24bit.scanline:
  125.         mov     ebx, [total_bpl]
  126.   .24bit.color_line:
  127.         mov     edx, [bpl]
  128.   .24bit.next_byte:
  129.         call    pcx._.get_byte
  130.         sub     edx, ecx
  131.     @@:
  132.         mov     [edi], al
  133.         add     edi, [nplanes]
  134.         dec     ecx
  135.         jnz     @b
  136.  
  137.         test    edx, edx
  138.         jnz     .24bit.next_byte
  139.  
  140.   .24bit.end_color_line:
  141.         test    ebx, ebx
  142.         jz      .24bit.end_full_line
  143.         dec     [line_begin]
  144.         mov     edi, [line_begin]
  145.         jmp     .24bit.color_line
  146.  
  147.   .24bit.end_full_line:
  148.         dec     [ysize]
  149.         jz      .quit
  150.         add     edi, 2
  151.         bt      [xsize], 0
  152.         jnc     @f
  153.         sub     edi, 3
  154.     @@:
  155.         mov     [line_begin], edi
  156.         jmp     .24bit.scanline
  157.  
  158.  
  159.   .indexed:
  160.  
  161.         stdcall img.create, eax, ebx, Image.bpp8
  162.         mov     [retvalue], eax
  163.         test    eax, eax
  164.         jz      .quit
  165.  
  166.         mov     ebx, eax
  167.         mov     esi, [_data]
  168.         add     esi, [_length]
  169.         sub     esi, 768
  170.         mov     edi, [eax + Image.Palette]
  171.         mov     ecx, 256
  172.         xor     eax, eax
  173.     @@:
  174.         lodsw
  175.         xchg    al, ah
  176.         shl     eax, 8
  177.         lodsb
  178.         stosd
  179.         dec     ecx
  180.         jnz     @b
  181.  
  182.         mov     esi, [_data]
  183.         add     esi, 128
  184.         mov     edi, [ebx + Image.Data]
  185.  
  186.   .indexed.line:
  187.         mov     ebx, [total_bpl]
  188.   .indexed.next_byte:
  189.         call    pcx._.get_byte
  190.     @@:
  191.         stosb
  192.         dec     ecx
  193.         jnz     @b
  194.         test    ebx, ebx
  195.         jnz     .indexed.next_byte
  196.  
  197.         dec     [ysize]
  198.         jnz     .indexed.line
  199.         jmp     .quit
  200.  
  201.  
  202.   .monochrome:
  203.  
  204.         stdcall img.create, eax, ebx, Image.bpp1
  205.         mov     [retvalue], eax
  206.         test    eax, eax
  207.         jz      .quit
  208.  
  209.         mov     edi, [eax + Image.Palette]
  210.         mov     [edi], dword 0xff000000
  211.         mov     [edi + 4], dword 0xffffffff
  212.  
  213.         mov     esi, [_data]
  214.         add     esi, 128
  215.         mov     edi, [eax + Image.Data]
  216.  
  217.  
  218.   .monochrome.line:
  219.         mov     ebx, [total_bpl]
  220.   .monochrome.next_byte:
  221.         call    pcx._.get_byte
  222.     @@:
  223.         stosb
  224.         dec     ecx
  225.         jnz     @b
  226.         test    ebx, ebx
  227.         jnz     .monochrome.next_byte
  228.         dec     [ysize]
  229.         jnz     .monochrome.line
  230. ;       jmp     .quit
  231.  
  232.  
  233.   .quit:
  234.         popa
  235.         mov     eax, [retvalue]
  236.         ret
  237. endp
  238.  
  239.  
  240. ;;================================================================================================;;
  241. proc img.encode.pcx _img, _p_length, _options ;///////////////////////////////////////////////////;;
  242. ;;------------------------------------------------------------------------------------------------;;
  243. ;? Encode image into raw data in pcx format                                                     ;;
  244. ;;------------------------------------------------------------------------------------------------;;
  245. ;> _img = pointer to image                                                                        ;;
  246. ;;------------------------------------------------------------------------------------------------;;
  247. ;< eax = 0 (error) or pointer to encoded data                                                     ;;
  248. ;< _p_length = encoded data length                                                                ;;
  249. ;;================================================================================================;;
  250.         xor     eax, eax
  251.         ret
  252. endp
  253.  
  254.  
  255. ;;================================================================================================;;
  256. ;;////////////////////////////////////////////////////////////////////////////////////////////////;;
  257. ;;================================================================================================;;
  258. ;! Below are private procs you should never call directly from your code                          ;;
  259. ;;================================================================================================;;
  260. ;;////////////////////////////////////////////////////////////////////////////////////////////////;;
  261. ;;================================================================================================;;
  262. proc    pcx._.get_byte
  263.  
  264.         xor     ecx, ecx
  265.         lodsb
  266.         cmp     al, 0xC0
  267.         setb    cl
  268.         jb      .done
  269.         and     al, 0x3F
  270.         mov     cl, al
  271.         lodsb
  272.   .done:
  273.         sub     ebx, ecx
  274.         ret
  275. endp
  276.  
  277.  
  278. ;;================================================================================================;;
  279. ;;////////////////////////////////////////////////////////////////////////////////////////////////;;
  280. ;;================================================================================================;;
  281. ;! Below is private data you should never use directly from your code                             ;;
  282. ;;================================================================================================;;
  283. ;;////////////////////////////////////////////////////////////////////////////////////////////////;;
  284. ;;================================================================================================;;
  285.