Subversion Repositories Kolibri OS

Rev

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

  1. ;;================================================================================================;;
  2. ;;//// xbm.asm //// (c) dunkaist, 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. ;;================================================================================================;;
  21. proc img.is.xbm _data, _length ;//////////////////////////////////////////////////////////////////;;
  22. ;;------------------------------------------------------------------------------------------------;;
  23. ;? Determine if raw data could be decoded (is in xbm format)                                      ;;
  24. ;;------------------------------------------------------------------------------------------------;;
  25. ;> _data = raw data as read from file/stream                                                      ;;
  26. ;> _length = data length                                                                          ;;
  27. ;;------------------------------------------------------------------------------------------------;;
  28. ;< eax = false / true                                                                             ;;
  29. ;;================================================================================================;;
  30.  
  31.         push    ebx ecx esi edi
  32.         xor     eax, eax
  33.  
  34.         mov     edi, [_data]
  35.         mov     ecx, 4          ; BOM
  36.         mov     al, '#'
  37.         repne   scasb
  38.         jne     .is_not_xbm
  39.         dec     edi
  40.         mov     esi, str_define
  41.         mov     ecx, str_define.size
  42.         repe    cmpsb
  43.         jne     .is_not_xbm
  44.         mov     esi, edi
  45.         call    xbm._.skip_whitespace
  46.         mov     edx, esi
  47.         call    xbm._.wait_whitespace
  48.         mov     ebx, esi
  49.         sub     ebx, edx
  50.         sub     ebx, 5
  51.         cmp     word[esi - 6], '_w'
  52.         jne     .is_not_xbm
  53.         cmp     dword[esi - 4], 'idth'
  54.         jne     .is_not_xbm
  55.         call    xbm._.skip_whitespace
  56.         call    xbm._.wait_whitespace
  57.         call    xbm._.skip_whitespace
  58.         mov     edi, str_define
  59.         mov     ecx, str_define.size
  60.         repe    cmpsb
  61.         jne     .is_not_xbm
  62.         call    xbm._.skip_whitespace
  63.         mov     edi, edx
  64.         mov     ecx, ebx
  65.         repe    cmpsb
  66.         jne     .is_not_xbm
  67.         cmp     dword[esi], 'heig'
  68.         jne     .is_not_xbm
  69.         cmp     word[esi + 4], 'ht'
  70.         jne     .is_not_xbm
  71.  
  72.   .is_xbm:
  73.         pop     edi esi ecx ebx
  74.         xor     eax, eax
  75.         inc     eax
  76.         ret
  77.   .is_not_xbm:
  78.         pop     edi esi ecx ebx
  79.         xor     eax, eax
  80.         ret
  81. endp
  82.  
  83.  
  84. ;;================================================================================================;;
  85. proc img.decode.xbm _data, _length, _options ;////////////////////////////////////////////////////;;
  86. ;;------------------------------------------------------------------------------------------------;;
  87. ;? Decode data into image if it contains correctly formed raw data in xbm format                  ;;
  88. ;;------------------------------------------------------------------------------------------------;;
  89. ;> _data = raw data as read from file/stream                                                      ;;
  90. ;> _length = data length                                                                          ;;
  91. ;;------------------------------------------------------------------------------------------------;;
  92. ;< eax = 0 (error) or pointer to image                                                            ;;
  93. ;;================================================================================================;;
  94. locals
  95.         width           rd 1
  96.         height          rd 1
  97.         counter         rd 1
  98.         retvalue        rd 1    ; 0 (error) or pointer to image
  99. endl
  100.  
  101.         pusha
  102.  
  103.         mov     esi, [_data]
  104.         add     esi, 5
  105.         call    xbm._.wait_whitespace
  106.         call    xbm._.skip_whitespace
  107.         call    xbm._.wait_whitespace
  108.         call    xbm._.skip_whitespace
  109.         call    xbm._.read_number
  110.         mov     [width], eax
  111.         call    xbm._.skip_whitespace
  112.         call    xbm._.wait_whitespace
  113.         call    xbm._.skip_whitespace
  114.         call    xbm._.wait_whitespace
  115.         call    xbm._.skip_whitespace
  116.         call    xbm._.read_number
  117.         mov     [height], eax
  118.         stdcall img.create, [width], [height], Image.bpp1
  119.         test    eax, eax
  120.         jz      .quit
  121.         mov     [retvalue], eax
  122.  
  123.         mov     edi, [_data]
  124.         mov     ecx, [_length]
  125.         mov     eax, '{'
  126.         repne   scasb
  127.         mov     esi, edi
  128.  
  129.         mov     ebx, [retvalue]
  130.         mov     eax, [ebx + Image.Palette]
  131.         mov     dword[eax], 0xffffffff
  132.         mov     dword[eax + 4], 0xff000000
  133.         mov     ecx, [ebx + Image.Width]
  134.         add     ecx, 7
  135.         shr     ecx, 3
  136.         imul    ecx, [ebx + Image.Height]
  137.         mov     [counter], ecx
  138.         mov     edi, [ebx + Image.Data]
  139.     @@:
  140.         call    xbm._.skip_whitespace
  141.         call    xbm._.read_number
  142.         mov     cl, al
  143.         and     eax, 0x0f
  144.         mov     al, byte[reverse_bits + eax]
  145.         shl     eax, 4
  146.         mov     edx, eax
  147.         mov     al, cl
  148.         shr     eax, 4
  149.         mov     al, byte[reverse_bits + eax]
  150.         or      eax, edx
  151.         mov     byte[edi], al
  152.         add     edi, 1
  153.         sub     [counter], 1
  154.         jnz     @b
  155.   .quit:
  156.         popa
  157.         mov     eax, [retvalue]
  158.         ret
  159. endp
  160.  
  161.  
  162. ;;================================================================================================;;
  163. proc img.encode.xbm _img, _common, _specific ;////////////////////////////////////////////////////;;
  164. ;;------------------------------------------------------------------------------------------------;;
  165. ;? Encode image into raw data in xbm format                                                       ;;
  166. ;;------------------------------------------------------------------------------------------------;;
  167. ;> [_img]      = pointer to image                                                                 ;;
  168. ;> [_common]   = format independent options                                                       ;;
  169. ;> [_specific] = 0 / pointer to the structure of format specific options                          ;;
  170. ;;------------------------------------------------------------------------------------------------;;
  171. ;< eax = 0 / pointer to encoded data                                                              ;;
  172. ;< ecx = error code / the size of encoded data                                                    ;;
  173. ;;================================================================================================;;
  174.         xor     eax, eax
  175.         ret
  176. endp
  177.  
  178.  
  179. ;;================================================================================================;;
  180. ;;////////////////////////////////////////////////////////////////////////////////////////////////;;
  181. ;;================================================================================================;;
  182. ;! Below are private procs you should never call directly from your code                          ;;
  183. ;;================================================================================================;;
  184. ;;////////////////////////////////////////////////////////////////////////////////////////////////;;
  185. ;;================================================================================================;;
  186. proc xbm._.skip_whitespace
  187.   .next_byte:
  188.         movzx   eax, byte[esi]
  189.         cmp     eax, 0x20
  190.         jne     @f
  191.         add     esi, 1
  192.         jmp     .next_byte
  193.     @@:
  194.         cmp     eax, 0x09
  195.         jne     @f
  196.         add     esi, 1
  197.         jmp     .next_byte
  198.     @@:
  199.         cmp     eax, 0x0a
  200.         jne     @f
  201.         add     esi, 1
  202.         jmp     .next_byte
  203.     @@:
  204.         ret
  205. endp
  206.  
  207.  
  208. proc xbm._.wait_whitespace
  209.     @@:
  210.         movzx   eax, byte[esi]
  211.         cmp     eax, 0x20
  212.         je      @f
  213.         cmp     eax, 0x09
  214.         je      @f
  215.         cmp     eax, 0x0a
  216.         je      @f
  217.         add     esi, 1
  218.         jmp     @b
  219.     @@:
  220.         ret
  221. endp
  222.  
  223.  
  224. proc xbm._.read_number
  225.         xor     eax, eax
  226.         mov     ecx, eax
  227.         mov     al, byte[esi]
  228.         add     esi, 1
  229.         sub     al, 0x30
  230.         test    al, al
  231.         jz     .octal_or_hex
  232.   .decimal:
  233.         mov     ebx, 10
  234.         imul    ecx, ebx
  235.         add     ecx, eax
  236.         mov     al, byte[esi]
  237.         add     esi, 1
  238.         sub     al, 0x30
  239.         cmp     al, 10
  240.         jb      .decimal
  241.         jmp     .done
  242.   .octal_or_hex:
  243.         mov     al, byte[esi]
  244.         add     esi, 1
  245.         sub     al, 0x30
  246.         cmp     al, 'x' - 0x30
  247.         je      .hex
  248.   .octal:
  249.         jmp     .done
  250.   .hex:
  251.         mov     al, byte[esi]
  252.         add     esi, 1
  253.         sub     al, 0x30
  254.         jc      .done
  255.         cmp     al, 9
  256.         jna     @f
  257.         sub     al, 0x30 - 9
  258.         jc      .done
  259.         cmp     al, 15
  260.         ja      .done
  261.     @@:
  262.         shl     ecx, 4
  263.         add     ecx, eax
  264.         jmp     .hex
  265.   .done:
  266.         mov     eax, ecx
  267.         ret
  268. endp
  269. ;;================================================================================================;;
  270. ;;////////////////////////////////////////////////////////////////////////////////////////////////;;
  271. ;;================================================================================================;;
  272. ;! Below is private data you should never use directly from your code                             ;;
  273. ;;================================================================================================;;
  274. ;;////////////////////////////////////////////////////////////////////////////////////////////////;;
  275. ;;================================================================================================;;
  276. reverse_bits    db 0x00, 0x08, 0x04, 0x0c, 0x02, 0x0a, 0x06, 0x0e, 0x01, 0x09, 0x05, 0x0d, 0x03, 0x0b, 0x07, 0x0f
  277. sz str_define   , '#define'
  278. sz str__width   , '_width'
  279. sz str__height  , '_height'
  280.  
  281.