Subversion Repositories Kolibri OS

Rev

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, (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. ;; References:                                                                                    ;;
  21. ;;   1. Hiview 1.2 by Mohammad A. REZAEI                                                          ;;
  22. ;;   2. Truevision TGA FILE FORMAT SPECIFICATION Version 2.0                                      ;;
  23. ;;      Technical Manual Version 2.2 January, 1991                                                ;;
  24. ;;                                                                                                ;;
  25. ;;================================================================================================;;
  26.  
  27. include 'tga.inc'
  28.  
  29. ;;================================================================================================;;
  30. proc img.is.tga _data, _length ;//////////////////////////////////////////////////////////////////;;
  31. ;;------------------------------------------------------------------------------------------------;;
  32. ;? Determine if raw data could be decoded (is in Targa format)                                    ;;
  33. ;;------------------------------------------------------------------------------------------------;;
  34. ;> _data = raw data as read from file/stream                                                      ;;
  35. ;> _length = data length                                                                          ;;
  36. ;;------------------------------------------------------------------------------------------------;;
  37. ;< eax = false / true                                                                             ;;
  38. ;;================================================================================================;;
  39.         push    ebx
  40.         cmp     [_length], 18
  41.         jbe     .nope
  42.         mov     ebx, [_data]
  43.         mov     eax, dword[ebx + tga_header.colormap_type]
  44.         cmp     al, 1
  45.         ja      .nope
  46.         cmp     ah, 11
  47.         ja      .nope
  48.         cmp     ah, 9
  49.         jae     .cont1
  50.         cmp     ah, 3
  51.         ja      .nope
  52.   .cont1:
  53.         mov     eax, dword[ebx + tga_header.image_spec.depth]
  54.         test    eax, 111b       ; bpp must be 8, 15, 16, 24 or 32
  55.         jnz     .maybe15
  56.         shr     al, 3
  57.         cmp     al, 4
  58.         ja      .nope
  59.         jmp     .cont2
  60.   .maybe15:
  61.         cmp     al, 15
  62.         jne     .nope
  63.   .cont2:                       ; continue testing
  64.         movzx   eax, byte[ebx + tga_header.colormap_spec.entry_size]    ; palette bpp
  65.         cmp     eax, 0
  66.         je      .yep
  67.         cmp     eax, 16
  68.         je      .yep
  69.         cmp     eax, 24
  70.         je      .yep
  71.         cmp     eax, 32
  72.         je      .yep
  73.   .nope:
  74.         xor     eax, eax
  75.         pop     ebx
  76.         ret
  77.  
  78.   .yep:
  79.         xor     eax, eax
  80.         inc     eax
  81.         pop     ebx
  82.         ret
  83. endp
  84.  
  85. ;;================================================================================================;;
  86. proc img.decode.tga _data, _length, _options ;////////////////////////////////////////////////////;;
  87. ;;------------------------------------------------------------------------------------------------;;
  88. ;? Decode data into image if it contains correctly formed raw data in Targa format                ;;
  89. ;;------------------------------------------------------------------------------------------------;;
  90. ;> _data = raw data as read from file/stream                                                      ;;
  91. ;> _length = data length                                                                          ;;
  92. ;;------------------------------------------------------------------------------------------------;;
  93. ;< eax = 0 (error) or pointer to image                                                            ;;
  94. ;;================================================================================================;;
  95. locals
  96.         width           dd ?
  97.         height          dd ?
  98.         bytes_per_pixel dd ?
  99.         retvalue        dd ?
  100. endl
  101.         push    ebx esi edi
  102.         mov     ebx, [_data]
  103.         movzx   esi, byte[ebx]
  104.         lea     esi, [esi + ebx + sizeof.tga_header]    ; skip comment and header
  105.         mov     edx, dword[ebx + tga_header.image_spec.width]
  106.         movzx   ecx, dx                 ; ecx = width
  107.         shr     edx, 16                 ; edx = height
  108.         mov     [width], ecx
  109.         mov     [height], edx
  110.         movzx   eax, byte[ebx + tga_header.image_spec.depth]
  111.         add     eax, 7
  112.         shr     eax, 3
  113.         mov     [bytes_per_pixel], eax
  114.         movzx   eax, byte[ebx + tga_header.image_spec.depth]
  115.  
  116.         cmp     eax, 8
  117.         jne     @f
  118.         mov     eax, Image.bpp8i
  119.         jmp     .type_defined
  120.     @@:
  121.         cmp     eax, 15
  122.         jne     @f
  123.         mov     eax, Image.bpp15
  124.         jmp     .type_defined
  125.     @@:
  126.         cmp     eax, 16
  127.         jne     @f
  128.         mov     eax, Image.bpp15        ; 16bpp tga images are really 15bpp ARGB
  129.         jmp     .type_defined
  130.     @@:
  131.         cmp     eax, 24
  132.         jne     @f
  133.         mov     eax, Image.bpp24
  134.         jmp     .type_defined
  135.     @@:
  136.         cmp     eax, 32
  137.         jne     @f
  138.         mov     eax, Image.bpp32
  139.         jmp     .type_defined
  140.     @@:
  141.   .type_defined:
  142.         stdcall img.create, ecx, edx, eax
  143.         mov     [retvalue], eax
  144.         test    eax, eax                ; failed to allocate?
  145.         jz      .done                   ; then exit
  146.         mov     ebx, eax
  147.         cmp     dword[ebx + Image.Type], Image.bpp8i
  148.         jne     .palette_parsed
  149.         mov     edi, [ebx + Image.Palette]
  150.         mov     ecx, [_data]
  151.         cmp     byte[ecx + tga_header.image_type], 3    ; we also have grayscale subtype
  152.         jz      .write_grayscale_palette                ; that don't hold palette in file
  153.         cmp     byte[ecx + tga_header.image_type], 11
  154.         jz      .write_grayscale_palette
  155.         movzx   eax, byte[ecx + tga_header.colormap_spec.entry_size]    ; size of colormap entries in bits
  156.         movzx   ecx, word[ecx + tga_header.colormap_spec.colormap_length]       ; number of colormap entries
  157.         cmp     eax, 24
  158.         je      .24bpp_palette
  159.         cmp     eax, 16
  160.         je      .16bpp_palette
  161.         rep     movsd                   ; else they are 32 bpp
  162.         jmp     .palette_parsed
  163.   .write_grayscale_palette:
  164.         mov     ecx, 0x100
  165.         xor     eax, eax
  166.     @@:
  167.         stosd
  168.         add     eax, 0x010101
  169.         loop    @b
  170.         jmp     .palette_parsed
  171.   .16bpp_palette:                       ; FIXME: code copypasted from img.do_rgb, should use img.convert
  172.         push    ebx edx ebp
  173.     @@:
  174.         movzx   eax, word[esi]
  175.         mov     ebx, eax
  176.         add     esi, 2
  177.         and     eax, (0x1F) or (0x1F shl 10)
  178.         and     ebx, 0x1F shl 5
  179.         lea     edx, [eax + eax]
  180.         shr     al, 2
  181.         mov     ebp, ebx
  182.         shr     ebx, 2
  183.         shr     ah, 4
  184.         shl     dl, 2
  185.         shr     ebp, 7
  186.         add     eax, edx
  187.         add     ebx, ebp
  188.         mov     [edi], al
  189.         mov     [edi + 1], bl
  190.         mov     [edi + 2], ah
  191.         add     edi, 4
  192.         loop    @b
  193.         pop     ebp edx ebx
  194.         jmp     .palette_parsed
  195.  
  196.   .24bpp_palette:
  197.     @@:
  198.         lodsd
  199.         dec     esi
  200.         and     eax, 0xffffff
  201.         stosd
  202.         loop    @b
  203.   .palette_parsed:
  204.         mov     edi, [ebx + Image.Data]
  205.         mov     ebx, [width]
  206.         imul    ebx, [height]
  207.         mov     edx, [bytes_per_pixel]
  208.         mov     eax, [_data]
  209.         test    byte[eax + tga_header.image_type], 0x08
  210.         jz      .uncompressed
  211.   .next_rle_packet:
  212.         xor     eax, eax
  213.         lodsb
  214.         btr     ax, 7                   ; Run-length packet?
  215.         jnc     .raw_packet
  216.         add     eax, 1
  217.         sub     ebx, eax
  218.     @@:
  219.         mov     ecx, edx
  220.         rep     movsb
  221.         sub     esi, edx
  222.         sub     eax, 1
  223.         jnz     @b
  224.         add     esi, edx
  225.         test    ebx, ebx
  226.         jnz     .next_rle_packet
  227.         jmp     .done
  228.   .raw_packet:
  229.         mov     ecx, eax
  230.         add     ecx, 1
  231.         sub     ebx, ecx
  232.         imul    ecx, edx
  233.         rep     movsb
  234.         test    ebx, ebx
  235.         jnz     .next_rle_packet
  236.   .uncompressed:
  237.         imul    edx, ebx
  238.         mov     ecx, edx
  239.         rep     movsb
  240.   .done:
  241.         xor     ebx, ebx
  242.         mov     esi, [_data]
  243.         test    byte[esi + tga_header.image_spec.descriptor], TGA_START_TOP
  244.         jnz     @f
  245.         or      ebx, FLIP_VERTICAL
  246.     @@:
  247.         test    byte[esi + tga_header.image_spec.descriptor], TGA_START_RIGHT
  248.         jz      @f
  249.         or      ebx, FLIP_HORIZONTAL
  250.     @@:
  251.         test    ebx, ebx
  252.         jz      @f
  253.         stdcall img.flip, [retvalue], ebx
  254.     @@:
  255.         pop     edi esi ebx
  256.         mov     eax, [retvalue]
  257.         ret
  258. endp
  259.  
  260. ;;================================================================================================;;
  261. proc img.encode.tga _img, _p_length, _options ;///////////////////////////////////////////////////;;
  262. ;;------------------------------------------------------------------------------------------------;;
  263. ;? Encode image into raw data in Targa format                                                     ;;
  264. ;;------------------------------------------------------------------------------------------------;;
  265. ;> _img = pointer to image                                                                        ;;
  266. ;;------------------------------------------------------------------------------------------------;;
  267. ;< eax = 0 (error) or pointer to encoded data                                                     ;;
  268. ;< _p_length = encoded data length                                                                ;;
  269. ;;================================================================================================;;
  270.         xor     eax, eax
  271.         ret
  272. endp
  273.  
  274. ;;================================================================================================;;
  275. ;;////////////////////////////////////////////////////////////////////////////////////////////////;;
  276. ;;================================================================================================;;
  277. ;! Below are private procs you should never call directly from your code                          ;;
  278. ;;================================================================================================;;
  279. ;;////////////////////////////////////////////////////////////////////////////////////////////////;;
  280. ;;================================================================================================;;
  281.  
  282. ;;================================================================================================;;
  283. ;;////////////////////////////////////////////////////////////////////////////////////////////////;;
  284. ;;================================================================================================;;
  285. ;! Below is private data you should never use directly from your code                             ;;
  286. ;;================================================================================================;;
  287. ;;////////////////////////////////////////////////////////////////////////////////////////////////;;
  288. ;;================================================================================================;;
  289.