Subversion Repositories Kolibri OS

Rev

Rev 2684 | Rev 4229 | 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, _common, _specific ;////////////////////////////////////////////////////;;
  186. ;;------------------------------------------------------------------------------------------------;;
  187. ;? Encode image into raw data in pnm format                                                       ;;
  188. ;;------------------------------------------------------------------------------------------------;;
  189. ;> [_img]      = pointer to image                                                                 ;;
  190. ;> [_common]   = format independent options                                                       ;;
  191. ;> [_specific] = 0 / pointer to the structure of format specific options                          ;;
  192. ;;------------------------------------------------------------------------------------------------;;
  193. ;< eax = 0 / pointer to encoded data                                                              ;;
  194. ;< ecx = error code / the size of encoded data                                                    ;;
  195. ;;================================================================================================;;
  196. locals
  197.         encoded_file            rd 1
  198.         encoded_file_size       rd 1
  199.         encoded_data_size       rd 1
  200. endl
  201.         push    ebx
  202.  
  203.         mov     ebx, [_img]
  204.         mov     eax, [ebx + Image.Type]
  205.         cmp     eax, Image.bpp1
  206.         je      .pbm
  207.         cmp     eax, Image.bpp8g
  208.         je      .pgm
  209.         cmp     eax, Image.bpp24
  210.         je      .ppm
  211.         mov     ecx, LIBIMG_ERROR_BIT_DEPTH
  212.         jmp     .error
  213.   .pbm:
  214.         mov     ecx, [ebx + Image.Width]
  215.         add     ecx, 7
  216.         shr     ecx, 3
  217.         imul    ecx, [ebx + Image.Height]
  218.         mov     [encoded_data_size], ecx
  219.         add     ecx, (2 + 1) + (2 + pnm._.creator_comment.size + 1) + (5 + 1 + 5 + 1) + (3 + 1)
  220.         mov     [encoded_file_size], ecx
  221.         stdcall [mem.alloc], ecx
  222.         mov     [encoded_file], eax
  223.         test    eax, eax
  224.         jnz     @f
  225.         mov     ecx, LIBIMG_ERROR_OUT_OF_MEMORY
  226.         jmp     .error
  227.     @@:
  228.         mov     edi, eax
  229.         mov     ax, 'P4'
  230.         call    pnm._.write_header
  231.         mov     esi, [ebx + Image.Data]
  232.         mov     ecx, [encoded_data_size]
  233.         rep     movsb
  234.         mov     eax, [encoded_file]
  235.         mov     ecx, [encoded_file_size]
  236.         jmp     .quit
  237.        
  238.   .pgm:
  239.         mov     ecx, [ebx + Image.Width]
  240.         imul    ecx, [ebx + Image.Height]
  241.         mov     [encoded_data_size], ecx
  242.         add     ecx, (2 + 1) + (2 + pnm._.creator_comment.size + 1) + (5 + 1 + 5 + 1) + (3 + 1)
  243.         mov     [encoded_file_size], ecx
  244.         stdcall [mem.alloc], ecx
  245.         mov     [encoded_file], eax
  246.         test    eax, eax
  247.         jnz     @f
  248.         mov     ecx, LIBIMG_ERROR_OUT_OF_MEMORY
  249.         jmp     .error
  250.     @@:
  251.         mov     edi, eax
  252.         mov     ax, 'P5'
  253.         call    pnm._.write_header
  254.         mov     dword[edi], '255 '
  255.         add     edi, 3
  256.         mov     byte[edi], 0x0A
  257.         add     edi, 1
  258.         mov     esi, [ebx + Image.Data]
  259.         mov     ecx, [encoded_data_size]
  260.         rep     movsb
  261.         mov     eax, [encoded_file]
  262.         mov     ecx, [encoded_file_size]
  263.         jmp     .quit
  264.  
  265.   .ppm:
  266.         mov     ecx, [ebx + Image.Width]
  267.         imul    ecx, [ebx + Image.Height]
  268.         lea     ecx, [ecx*3]
  269.         mov     [encoded_data_size], ecx
  270.         add     ecx, (2 + 1) + (2 + pnm._.creator_comment.size + 1) + (5 + 1 + 5 + 1) + (3 + 1)
  271.         mov     [encoded_file_size], ecx
  272.         stdcall [mem.alloc], ecx
  273.         mov     [encoded_file], eax
  274.         test    eax, eax
  275.         jnz     @f
  276.         mov     ecx, LIBIMG_ERROR_OUT_OF_MEMORY
  277.         jmp     .error
  278.     @@:
  279.         mov     edi, eax
  280.         mov     ax, 'P6'
  281.         call    pnm._.write_header
  282.         mov     dword[edi], '255 '
  283.         add     edi, 3
  284.         mov     byte[edi], 0x0A
  285.         add     edi, 1
  286.         mov     esi, [ebx + Image.Data]
  287.         mov     ecx, [ebx + Image.Width]
  288.         imul    ecx, [ebx + Image.Height]
  289.     @@:
  290.         lodsb
  291.         mov     byte[edi+2], al
  292.         lodsb
  293.         mov     byte[edi+1], al
  294.         movsb
  295.         add     edi, 2
  296.         dec     ecx
  297.         jnz     @b
  298.         mov     eax, [encoded_file]
  299.         mov     ecx, [encoded_file_size]
  300.         jmp     .quit
  301.  
  302.   .error:
  303.         xor     eax, eax
  304.   .quit:
  305.         pop     ebx
  306.         ret
  307. endp
  308.  
  309.  
  310. ;;================================================================================================;;
  311. ;;////////////////////////////////////////////////////////////////////////////////////////////////;;
  312. ;;================================================================================================;;
  313. ;! Below are private procs you should never call directly from your code                          ;;
  314. ;;================================================================================================;;
  315. ;;////////////////////////////////////////////////////////////////////////////////////////////////;;
  316. ;;================================================================================================;;
  317. proc    pnm._.get_number
  318.         sub     eax, '0'
  319.         mov     ebx, eax
  320.     @@:
  321.         lodsb
  322.         cmp     al, '0'
  323.         jb      .quit
  324.         sub     eax, '0'
  325.         lea     eax, [ebx*8 + eax]
  326.         lea     ebx, [ebx*2 + eax]
  327. ;       imul    ebx, 10
  328. ;       add     ebx, eax
  329.         jmp     @b
  330.   .quit:
  331.         ret
  332. endp
  333.  
  334.  
  335. proc    pnm._.write_header
  336.         stosw
  337.         mov     byte[edi], 0x0A
  338.         add     edi, 1
  339.         mov     word[edi], '# '
  340.         add     edi, 2
  341.         mov     esi, pnm._.creator_comment
  342.         mov     ecx, pnm._.creator_comment.size
  343.         rep     movsb
  344.         mov     byte[edi], 0x0A
  345.         add     edi, 1
  346.  
  347.         push    edi
  348.         mov     al, ' '
  349.         mov     ecx, (5 + 1 + 5)
  350.         rep     stosb
  351.         pop     edi
  352.         push    edi
  353.         add     edi, 4
  354.         mov     eax, [ebx + Image.Width]
  355.         mov     ecx, 10
  356.   .write_width:
  357.         xor     edx, edx
  358.         div     cx
  359.         add     dl, '0'
  360.         mov     byte[edi], dl
  361.         dec     edi
  362.         test    ax, ax
  363.         jnz     .write_width
  364.         mov     eax, [ebx + Image.Height]
  365.         pop     edi
  366.         push    edi
  367.         add     edi, 10
  368.   .write_height:
  369.         xor     edx, edx
  370.         div     cx
  371.         add     dl, '0'
  372.         mov     byte[edi], dl
  373.         dec     edi
  374.         test    ax, ax
  375.         jnz     .write_height
  376.         pop     edi
  377.         add     edi, 11
  378.         mov     byte[edi], 0x0A
  379.         add     edi, 1
  380.         ret
  381. endp
  382. ;;================================================================================================;;
  383. ;;////////////////////////////////////////////////////////////////////////////////////////////////;;
  384. ;;================================================================================================;;
  385. ;! Below is private data you should never use directly from your code                             ;;
  386. ;;================================================================================================;;
  387. ;;////////////////////////////////////////////////////////////////////////////////////////////////;;
  388. ;;================================================================================================;;
  389.  
  390. sz pnm._.creator_comment ,'CREATOR: KolibriOS / libimg'
  391.  
  392.