Subversion Repositories Kolibri OS

Rev

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

  1. ;;================================================================================================;;
  2. ;;//// tga.asm //// (c) Nable, 2007-2008 /////////////////////////////////////////////////////////;;
  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. Hiview 1.2 by Mohammad A. REZAEI                                                          ;;
  22. ;;                                                                                                ;;
  23. ;;================================================================================================;;
  24.  
  25. include 'tga.inc'
  26.  
  27. ;;================================================================================================;;
  28. proc img.is.tga _data, _length ;//////////////////////////////////////////////////////////////////;;
  29. ;;------------------------------------------------------------------------------------------------;;
  30. ;? Determine if raw data could be decoded (is in Targa format)                                    ;;
  31. ;;------------------------------------------------------------------------------------------------;;
  32. ;> _data = raw data as read from file/stream                                                      ;;
  33. ;> _length = data length                                                                          ;;
  34. ;;------------------------------------------------------------------------------------------------;;
  35. ;< eax = false / true                                                                             ;;
  36. ;;================================================================================================;;
  37.         cmp     [_length], 18
  38.         jbe     .nope
  39.         mov     eax, [_data]
  40.         push ebx
  41.         mov     ebx,[eax+1] ;bl=cmatype,bh=subtype
  42.         cmp     bl,1            ;cmatype is in [0..1]
  43.         ja      .nope
  44.         cmp     bh,11           ;subtype is in [1..3] (non-rle) or in [9..11] (rle)
  45.         ja      .nope
  46.         cmp     bh,9
  47.         jae     .cont1
  48.         cmp     bh,3
  49.         ja      .nope
  50. .cont1:                         ;continue testing
  51.         mov     ebx,[eax+16] ;bl=bpp, bh=flags //image descriptor
  52.         test ebx,111b   ;bpp must be 8, 15, 16, 24 or 32
  53.         jnz     .maybe15
  54.         shr     bl,3
  55.         cmp     bl,4
  56.         ja      .nope
  57.         jmp     .cont2
  58. .maybe15:
  59.         cmp     bl,15
  60.         jne     .nope
  61. .cont2:                         ;continue testing
  62.         test bh,tga.flags.interlace_type ;deinterlacing is not supported yet
  63.         jnz     .nope
  64.         cmp     byte[eax+7],24  ;test palette bpp - only 24 and 32 are supported
  65.         je      .yep
  66.         cmp     byte[eax+7],32  ;test palette bpp - only 24 and 32 are supported
  67.         je      .yep
  68. .nope:
  69.         xor     eax, eax
  70.         pop ebx
  71.         ret
  72.  
  73. .yep:
  74.         xor     eax, eax
  75.         inc     eax
  76.         pop ebx
  77.         ret
  78. endp
  79.  
  80. ;;================================================================================================;;
  81. proc img.decode.tga _data, _length, _options ;////////////////////////////////////////////////////;;
  82. ;;------------------------------------------------------------------------------------------------;;
  83. ;? Decode data into image if it contains correctly formed raw data in Targa format                ;;
  84. ;;------------------------------------------------------------------------------------------------;;
  85. ;> _data = raw data as read from file/stream                                                      ;;
  86. ;> _length = data length                                                                          ;;
  87. ;;------------------------------------------------------------------------------------------------;;
  88. ;< eax = 0 (error) or pointer to image                                                            ;;
  89. ;;================================================================================================;;
  90. locals
  91.   IMGwidth              dd ?
  92.   IMGheight             dd ?
  93.   IMGbpp                dd ?
  94.   DupPixelCount dd ?
  95.   TgaBlockCount dd ?
  96. endl
  97.         pushad
  98.         cld                                             ;paranoia
  99.         and     [DupPixelCount],0       ;prepare variables
  100.         and     [TgaBlockCount],0       ;prepare variables
  101.         mov     eax,[_data]
  102.         movzx esi,byte[eax]
  103.         lea     esi,[esi+eax+18]        ;skip comment and header
  104.         mov     ebx,[eax+12]
  105.         movzx ecx,bx                   ;ecx=width
  106.         shr     ebx,16                          ;ebx=height
  107.         mov     [IMGwidth],ecx
  108.         mov     [IMGheight],ebx
  109.         movzx edx,byte[eax+16]
  110.         cmp     edx,16
  111.         jnz     @f
  112.         dec     edx                                     ;16bpp tga images are really 15bpp ARGB
  113. @@:
  114.         sub     edx, 16 - Image.bpp16   ; 15 -> Image.bpp15, 16 -> Image.bpp16
  115.         mov     [IMGbpp],edx
  116.         stdcall img.create,ecx,ebx,edx
  117.         mov     [esp+28],eax            ;save return value
  118.         test eax,eax                    ;failed to allocate?
  119.         jz      .locret                         ;then exit
  120.         cmp     edx,8
  121.         jne     .palette_parsed
  122.         mov edi,[eax+Image.Palette]
  123.         mov     ecx,[_data]
  124.         cmp     byte[ecx+2],3           ;we also have grayscale subtype
  125.         jz      .write_grayscale_palette ;that don't hold palette in file
  126.         cmp     byte[ecx+2],11
  127.         jz      .write_grayscale_palette
  128.         mov dh,[ecx+7]                  ;size of colormap entries in bits
  129.         movzx ecx,word[ecx+5]   ;number of colormap entries
  130.         cmp     dh,24
  131.         jz      .skip_24bpp_palette     ;test if colormap entries are 24bpp
  132.         rep     movsd                           ;else they are 32 bpp
  133.         jmp     .palette_parsed
  134. .write_grayscale_palette:
  135.         push eax
  136.         mov     ecx,0x100
  137.         xor     eax,eax
  138. @@:
  139.         stosd
  140.         add     eax,0x010101
  141.         loop @b
  142.         pop     eax
  143.         jmp     .palette_parsed
  144. .skip_24bpp_palette:
  145.         push eax
  146. @@:
  147.         lodsd
  148.         dec esi
  149.         and     eax,0xFFFFFF
  150. ;       bswap eax
  151. ;       shr     eax,8
  152.         stosd
  153.         loop @b
  154.         pop     eax
  155. .palette_parsed:
  156.         mov     edi,[eax+Image.Data]
  157.         imul ebx,[IMGwidth]             ;ebx=width*height
  158.  
  159.         mov     edx,[IMGbpp]
  160.         add     edx,7
  161.         shr     edx,3                           ;edx=bytes per pixel
  162.         mov     dh,dl                           ;dh=dl=bytes per pixel
  163.  
  164.         mov     eax,[_data]
  165.         cmp     byte[eax+2],9
  166.         jb      .not_an_rle
  167. .tga_read_rle_pixel:
  168.     cmp  [DupPixelCount],0      ;Duplicate previously read pixel?
  169.     jg   .duplicate_previously_read_pixel
  170.     dec  [TgaBlockCount]        ;Decrement pixels remaining in block
  171.     jns  .read_non_rle_pixel
  172.     xor  eax,eax
  173.     lodsb
  174.     test al,al                          ;Start of duplicate-pixel block?
  175.     jns  .2
  176.     and  al,0x7f
  177.     mov  [DupPixelCount],eax ;Number of duplications after this one
  178.     and  [TgaBlockCount],0      ;Then read new block header
  179.     jmp  .read_non_rle_pixel
  180. .2:
  181.     mov  dword[TgaBlockCount],eax
  182. .read_non_rle_pixel:
  183.     xor  eax,eax
  184.     mov  dl,dh
  185. @@:
  186.     shl  eax,8
  187.     lodsb
  188.     dec  dl
  189.     jnz  @b
  190.     cmp  dh,3
  191.     jne  .put_pixel
  192.     bswap eax
  193.     shr  eax,8
  194.         jmp     .put_pixel
  195. .duplicate_previously_read_pixel:
  196.     dec  [DupPixelCount]
  197. .put_pixel:
  198.         mov     dl,dh
  199.         push eax
  200. @@:
  201.     stosb
  202.     shr eax,8
  203.     dec dl
  204.     jnz @b
  205.         pop     eax
  206.         dec     ebx
  207.         jnz     .tga_read_rle_pixel
  208.         jmp     .locret
  209. .not_an_rle:
  210.         movzx edx,dl                    ;dh contains bpp too (for decoding needs)
  211.         imul edx,ebx
  212.         mov     ecx,edx
  213.         rep     movsb                           ;just copy the image
  214. .locret:       
  215.         popad
  216.         ret
  217. endp
  218.  
  219. ;;================================================================================================;;
  220. proc img.encode.tga _img, _p_length, _options ;///////////////////////////////////////////////////;;
  221. ;;------------------------------------------------------------------------------------------------;;
  222. ;? Encode image into raw data in Targa format                                                     ;;
  223. ;;------------------------------------------------------------------------------------------------;;
  224. ;> _img = pointer to image                                                                        ;;
  225. ;;------------------------------------------------------------------------------------------------;;
  226. ;< eax = 0 (error) or pointer to encoded data                                                     ;;
  227. ;< _p_length = encoded data length                                                                ;;
  228. ;;================================================================================================;;
  229.         xor     eax, eax
  230.         ret
  231. endp
  232.  
  233.  
  234. ;;================================================================================================;;
  235. ;;////////////////////////////////////////////////////////////////////////////////////////////////;;
  236. ;;================================================================================================;;
  237. ;! Below are private procs you should never call directly from your code                          ;;
  238. ;;================================================================================================;;
  239. ;;////////////////////////////////////////////////////////////////////////////////////////////////;;
  240. ;;================================================================================================;;
  241.  
  242. ;;================================================================================================;;
  243. ;;////////////////////////////////////////////////////////////////////////////////////////////////;;
  244. ;;================================================================================================;;
  245. ;! Below is private data you should never use directly from your code                             ;;
  246. ;;================================================================================================;;
  247. ;;////////////////////////////////////////////////////////////////////////////////////////////////;;
  248. ;;================================================================================================;;
  249.  
  250. ;