Subversion Repositories Kolibri OS

Rev

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