Subversion Repositories Kolibri OS

Rev

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