Subversion Repositories Kolibri OS

Rev

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

  1. ;;================================================================================================;;
  2. ;;//// pnm.asm //// (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. include 'pnm.inc'
  21.  
  22. ;;================================================================================================;;
  23. proc img.is.pnm _data, _length ;//////////////////////////////////////////////////////////////////;;
  24. ;;------------------------------------------------------------------------------------------------;;
  25. ;? Determine if raw data could be decoded (is in pnm format)                                      ;;
  26. ;;------------------------------------------------------------------------------------------------;;
  27. ;> _data = raw data as read from file/stream                                                      ;;
  28. ;> _length = data length                                                                          ;;
  29. ;;------------------------------------------------------------------------------------------------;;
  30. ;< eax = false / true                                                                             ;;
  31. ;;================================================================================================;;
  32.  
  33.         xor     eax, eax
  34.  
  35.         mov     ecx, [_data]
  36.         mov     cx, word[ecx]
  37.         xchg    cl, ch
  38.         cmp     cx, '1P'
  39.         jb      .is_not_pnm
  40.         cmp     cx, '6P'
  41.         ja      .is_not_pnm
  42.  
  43.   .is_pnm:
  44.         inc     eax
  45.   .is_not_pnm:
  46.         ret
  47.  
  48. endp
  49.  
  50. ;;================================================================================================;;
  51. proc img.decode.pnm _data, _length, _options ;////////////////////////////////////////////////////;;
  52. ;;------------------------------------------------------------------------------------------------;;
  53. ;? Decode data into image if it contains correctly formed raw data in pnm format                  ;;
  54. ;;------------------------------------------------------------------------------------------------;;
  55. ;> _data = raw data as read from file/stream                                                      ;;
  56. ;> _length = data length                                                                          ;;
  57. ;;------------------------------------------------------------------------------------------------;;
  58. ;< eax = 0 (error) or pointer to image                                                            ;;
  59. ;;================================================================================================;;
  60. locals
  61.         width                   rd      1
  62.         height                  rd      1
  63.         pnm_type                rd      1
  64.         data_type               rd      1       ; raw or ascii
  65.         maxval                  rd      1
  66.         retvalue                rd      1
  67. endl
  68.  
  69.         pusha
  70.  
  71.         mov     esi, [_data]
  72.         lodsw
  73.         cmp     ax, 'P1'
  74.         jne     @f
  75.         mov     [pnm_type], PNM_PBM
  76.         mov     [data_type], PNM_ASCII
  77.         jmp     .parse_header
  78.     @@:
  79.         cmp     ax, 'P2'
  80.         jne     @f
  81.         mov     [pnm_type], PNM_PGM
  82.         mov     [data_type], PNM_ASCII
  83.         jmp     .parse_header
  84.     @@:
  85.         cmp     ax, 'P3'
  86.         jne     @f
  87.         mov     [pnm_type], PNM_PPM
  88.         mov     [data_type], PNM_ASCII
  89.         jmp     .parse_header
  90.     @@:
  91.         cmp     ax, 'P4'
  92.         jne     @f
  93.         mov     [pnm_type], PNM_PBM
  94.         mov     [data_type], PNM_RAW
  95.         jmp     .parse_header
  96.     @@:
  97.         cmp     ax, 'P5'
  98.         jne     @f
  99.         mov     [pnm_type], PNM_PGM
  100.         mov     [data_type], PNM_RAW
  101.         jmp     .parse_header
  102.     @@:
  103.         cmp     ax, 'P6'
  104.         jne     @f
  105.         mov     [pnm_type], PNM_PPM
  106.         mov     [data_type], PNM_RAW
  107.         jmp     .parse_header
  108.     @@:
  109.  
  110.   .parse_header:
  111.         xor     eax, eax
  112.         mov     [width], eax
  113.         mov     [height], eax
  114.         mov     [maxval], eax
  115.  
  116.   .next_char:
  117.         lodsb
  118.         cmp     al, '#'
  119.         jb      .next_char
  120.         ja      .read_number
  121.   .comment:
  122.         mov     edi, esi
  123.         mov     al, 0x0A
  124.         mov     ecx, edi
  125.         sub     ecx, [_data]
  126.         neg     ecx
  127.         add     ecx, [_length]
  128.         repne   scasb
  129.         mov     esi, edi
  130.         jmp     .next_char
  131.  
  132.   .read_number:
  133.         sub     eax, 0x30
  134.         mov     ebx, eax
  135.     @@:
  136.         lodsb
  137.         cmp     al, '0'
  138.         jb      .number_done
  139.         sub     eax, 0x30
  140.         imul    ebx, 10
  141.         add     ebx, eax
  142.         jmp     @b
  143.  
  144.   .number_done:
  145.         cmp     [width], 0
  146.         jne     @f
  147.         mov     [width], ebx
  148.         jmp     .next_char
  149.     @@:
  150.         cmp     [height], 0
  151.         jne     @f
  152.         mov     [height], ebx
  153.         cmp     [pnm_type], PNM_PBM
  154.         je      .header_parsed
  155.         jmp     .next_char
  156.     @@:
  157.         mov     [maxval], ebx
  158.  
  159.   .header_parsed:
  160.  
  161.         mov     eax, [pnm_type]
  162.         cmp     eax, PNM_PBM
  163.         je      .pbm
  164.         cmp     eax, PNM_PGM
  165.         je      .pgm
  166.         cmp     eax, PNM_PPM
  167.         je      .ppm
  168.         jmp     .quit
  169.  
  170.  
  171. include 'pbm.asm'
  172. include 'pgm.asm'
  173. include 'ppm.asm'
  174.  
  175.   .quit:
  176.         popa
  177.         mov     eax, [retvalue]
  178.         ret
  179.  
  180. endp
  181.  
  182.  
  183.  
  184. ;;================================================================================================;;
  185. proc img.encode.pnm _img, _p_length, _options ;///////////////////////////////////////////////////;;
  186. ;;------------------------------------------------------------------------------------------------;;
  187. ;? Encode image into raw data in pnm format                                                       ;;
  188. ;;------------------------------------------------------------------------------------------------;;
  189. ;> _img = pointer to image                                                                        ;;
  190. ;;------------------------------------------------------------------------------------------------;;
  191. ;< eax = 0 (error) or pointer to encoded data                                                     ;;
  192. ;< _p_length = encoded data length                                                                ;;
  193. ;;================================================================================================;;
  194.         xor     eax, eax
  195.         ret
  196. endp
  197.  
  198.  
  199. ;;================================================================================================;;
  200. ;;////////////////////////////////////////////////////////////////////////////////////////////////;;
  201. ;;================================================================================================;;
  202. ;! Below are private procs you should never call directly from your code                          ;;
  203. ;;================================================================================================;;
  204. ;;////////////////////////////////////////////////////////////////////////////////////////////////;;
  205. ;;================================================================================================;;
  206. proc    pnm._.get_number
  207.         sub     eax, '0'
  208.         mov     ebx, eax
  209.     @@:
  210.         lodsb
  211.         cmp     al, '0'
  212.         jb      .quit
  213.         sub     eax, '0'
  214.         lea     eax, [ebx*8 + eax]
  215.         lea     ebx, [ebx*2 + eax]
  216. ;       imul    ebx, 10
  217. ;       add     ebx, eax
  218.         jmp     @b
  219.   .quit:
  220.         ret
  221. endp
  222.  
  223. ;;================================================================================================;;
  224. ;;////////////////////////////////////////////////////////////////////////////////////////////////;;
  225. ;;================================================================================================;;
  226. ;! Below is private data you should never use directly from your code                             ;;
  227. ;;================================================================================================;;
  228. ;;////////////////////////////////////////////////////////////////////////////////////////////////;;
  229. ;;================================================================================================;;
  230.  
  231.