Subversion Repositories Kolibri OS

Rev

Blame | Last modification | View Log | Download | RSS feed

  1. ;*****************************************************************************
  2. ; PNG to RAW convert plugin - for zSea image viewer
  3. ; Copyright (c) 2008-2011, Marat Zakiyanov aka Mario79, aka Mario
  4. ; All rights reserved.
  5. ;
  6. ; Redistribution and use in source and binary forms, with or without
  7. ; modification, are permitted provided that the following conditions are met:
  8. ;        * Redistributions of source code must retain the above copyright
  9. ;          notice, this list of conditions and the following disclaimer.
  10. ;        * Redistributions in binary form must reproduce the above copyright
  11. ;          notice, this list of conditions and the following disclaimer in the
  12. ;          documentation and/or other materials provided with the distribution.
  13. ;        * Neither the name of the <organization> nor the
  14. ;          names of its contributors may be used to endorse or promote products
  15. ;          derived from this software without specific prior written permission.
  16. ;
  17. ; THIS SOFTWARE IS PROVIDED BY Marat Zakiyanov ''AS IS'' AND ANY
  18. ; EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  19. ; WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  20. ; DISCLAIMED. IN NO EVENT SHALL <copyright holder> BE LIABLE FOR ANY
  21. ; DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  22. ; (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  23. ; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  24. ; ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  25. ; (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  26. ; SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  27. ;*****************************************************************************
  28.  
  29. format MS COFF
  30.  
  31. public EXPORTS
  32.  
  33. section '.flat' code readable align 16
  34.  
  35. ;include        'macros.inc'
  36. include '../../../../macros.inc'
  37. ;---------------------------------------------------------------------
  38. START:
  39.         pushad
  40.         mov     eax,dword [esp+36]
  41.         call    check_header_1
  42.         je      header_OK
  43. ;---------------------------------------------------------------------
  44. no_png_file:
  45.         xor  eax,eax
  46.         mov [raw_area],eax
  47.         inc  eax  ; data corrupt eax = 1
  48.         jmp  header_OK.ret
  49. ;---------------------------------------------------------------------
  50. check_header_1:
  51.         mov [pointer],eax
  52.         mov ebx,[eax]
  53.         mov [image_file],ebx   
  54.         cmp  [ebx],dword 0x474E5089  ; check main label p.1
  55.         jne  @f ;no_png_file
  56.         cmp  [ebx+4],dword 0x0A1A0A0D ; check main label p.2
  57.         ret
  58. @@:
  59.         add  esp,4
  60.         jmp  no_png_file
  61. ;---------------------------------------------------------------------
  62. header_OK:
  63.         mov ebx,[eax+12]  ; file size
  64.         mov [file_size],ebx
  65. ;---------------------------------------------------------------------
  66.         mov ebx,[eax+16]
  67.         mov [deflate_unpack],ebx
  68. ;---------------------------------------------------------------------
  69.         mov ebx,[pointer]
  70. ;---------------------------------------------------------------------
  71.         xor  eax,eax
  72. ;       mov  [IDAT.pointer],eax
  73. ;       mov  [IDAT.size],eax
  74.         mov  [deflate_start_offset],eax
  75.         mov  eax,[image_file]
  76.         mov  [next_Chunk],eax
  77.         call search_IHDR
  78.         call search_IDAT  
  79. ;       call search_IEND
  80. ;--------------------------------------------------------------------- 
  81.         mov  eax,[IHDR.pointer]
  82.         mov  ecx,[eax] ; get width PNG
  83.         call convert_NBO_to_PC  
  84.         mov [IHDR_data.Width],ecx       ; store width PNG
  85.         mov [Image_Width],ecx
  86.         mov  ecx,[eax+4] ; get height PNG
  87.         call convert_NBO_to_PC
  88.         mov [IHDR_data.Height],ecx  ; store height PNG
  89.         mov [Image_Height],ecx
  90.         mov ecx,[eax+9] ; Color type
  91.                                         ; Compression method
  92.                                                  ; Filter method
  93.                                                  ; Interlace method
  94.         mov [IHDR_data.Color_type],ecx
  95.         xor ecx,ecx
  96.         mov cl,[eax+8]  ; Bit depth
  97.         mov [IHDR_data.Bit_depth],cl
  98. ;---------------------------------------------------------------------         
  99.         mov al,[eax+9]  ; check Color type
  100.         cmp al,byte 0    ; Each pixel is a grayscale sample.
  101.         je  .resolution
  102.         cmp al,byte 2    ; Each pixel is an R,G,B triple.
  103.         je  .Bit_depth_2
  104.         cmp al,byte 3    ; Each pixel is a palette index
  105.                                                   ; a PLTE chunk must appear.
  106.         jne  .no_PLTE
  107.         call search_PLTE
  108.         jmp  .resolution
  109. .no_PLTE:
  110.         cmp al,byte 4    ; Each pixel is a grayscale sample,
  111.                                                   ; followed by an alpha sample.
  112.         je  .Bit_depth_4
  113.         cmp al,byte 6    ; Each pixel is an R,G,B triple,
  114.                                                   ; followed by an alpha sample.
  115.         jne  no_png_file
  116. ;--------------------------------------------------------------------- 
  117. .Bit_depth_6:  
  118.         shl  ecx,1
  119.  
  120. .Bit_depth_4:
  121.         shl  ecx,1
  122.         jmp  .resolution
  123.  
  124. .Bit_depth_2:
  125.         lea  ecx,[ecx*3]
  126.  
  127. .resolution:
  128. ;--------------------------------------------------------------------- 
  129.         cmp [IHDR_data.Compression_method], byte 0 ; check Compression method
  130.         jne  no_png_file
  131.  
  132.         cmp [IHDR_data.Filter_method], byte 0 ; check Filtering method
  133.         jne  no_png_file
  134.  
  135.         cmp [IHDR_data.Interlace_method], byte 0 ; No Interlaced
  136.         je      @f
  137.         cmp [IHDR_data.Interlace_method], byte 1 ; Interlaced
  138.         jne  no_png_file
  139. @@:
  140. ;---------------------------------------------------------------------  
  141.         mov [resolution],ecx
  142.         imul ecx,[IHDR_data.Width]
  143.         mov  edi,ecx
  144.         shr  ecx,3
  145.         test edi,7
  146.         jz  @f
  147.         inc  ecx
  148. @@:
  149.         imul ecx,[IHDR_data.Height]
  150.         cmp  [IHDR_data.Color_type],byte 3
  151.         jne  @f
  152.         mov  eax,[PLTE.size]
  153.         mov  ebx,3
  154.         xor  edx,edx
  155.         div  ebx
  156.         shl  eax,2     
  157.         add  ecx,eax ; PLTE table area
  158.         jmp  .RAW_header
  159. ;--------------------------------------
  160. @@:
  161.         cmp  [IHDR_data.Color_type],byte 0
  162.         je      @f
  163.         cmp  [IHDR_data.Color_type],byte 4
  164.         jne  .RAW_header
  165. @@:
  166.         push ecx
  167.         mov  eax,1
  168.         movzx ecx,byte [IHDR_data.Bit_depth]
  169.         cmp     ecx,16
  170.         jne  @f
  171.         shr  ecx,1
  172. @@:
  173.         shl eax,cl
  174.         shl eax,2
  175.         pop  ecx
  176.         add ecx,eax
  177. ;       mov ebx,[pointer]
  178. ;       mov [ebx+20],eax
  179. ;--------------------------------------
  180. .RAW_header:
  181.         add ecx,44 ; RAW header
  182. ;       mov ebx,[pointer]
  183. ;       mov [ebx+24],ecx
  184. ;       shl ecx,1
  185.         mcall 68, 12
  186.         cmp  eax,0
  187.         jne  @f
  188.         xor  eax,eax
  189.         mov [raw_area],eax  ; store pointer of image area
  190.         mov  eax,2      ; not enough memory
  191.         jmp     .ret
  192. @@:
  193.         mov  [raw_area],eax
  194. ;       mov ebx,[pointer]
  195. ;       mov [ebx+28],eax
  196. ;---------------------------------------------------------------------   
  197.         mov edi,eax
  198.         xor eax,eax
  199.         shr ecx,2
  200.         cld
  201.         rep stosd       ; clear memory
  202. ;---------------------------------------------------------------------
  203. ; Create RAW header
  204. ;---------------------------------------------------------------------
  205.         mov  eax,[raw_area]
  206.         mov  [eax],dword 'RAW '
  207. ;---------------------------------------------------------------------  
  208.         mov  ecx,[IHDR_data.Width] ; width BMP
  209.         mov  [eax+4],ecx  ; width RAW
  210. ;---------------------------------------------------------------------
  211.         mov  ecx,[IHDR_data.Height] ; high  BMP
  212.         mov  [eax+8],ecx  ;high  RAW
  213. ;---------------------------------------------------------------------
  214.         mov  ecx,[resolution] ; color resolution  BMP
  215.         mov  [eax+12],ecx  ;color resolution  RAW
  216. ;---------------------------------------------------------------------
  217.         xor  ecx,ecx
  218.         mov  cl,[IHDR_data.Bit_depth]
  219.         mov  [eax+16],cx  ;channel color resolution  RAW
  220. ;---------------------------------------------------------------------
  221.         mov  ecx,[IHDR_data.Color_type]
  222.         xor  ebx,ebx
  223.         inc  ebx
  224.         cmp  cl,byte 0   ; Each pixel is a grayscale sample.
  225.         je  .1
  226.         cmp  cl,byte 2   ; Each pixel is an R,G,B triple.
  227.         je  .Bit_depth_2_1
  228.         cmp  cl,byte 3   ; Each pixel is a palette index
  229.                                                   ; a PLTE chunk must appear.
  230.         je  .1
  231.         cmp  cl,byte 4   ; Each pixel is a grayscale sample,
  232.                                                   ; followed by an alpha sample.
  233.         je  .Bit_depth_4_1
  234.         cmp  cl,byte 6
  235.         jne  no_png_file
  236. .Bit_depth_6_1:
  237.         shl  ebx,1
  238. .Bit_depth_4_1:
  239.         shl  ebx,1
  240.         jmp  .1
  241. .Bit_depth_2_1:
  242.         lea  ebx,[ebx*3]
  243. .1:    
  244.         mov  [eax+18],bx  ; channels amount RAW
  245. ;---------------------------------------------------------------------
  246.         xor  edx,edx
  247.         cmp  cl,byte 3   ; Each pixel is a palette index
  248.                                                   ; a PLTE chunk must appear.
  249.         je  @f
  250.         cmp  cl,0
  251.         je      @f
  252.         cmp  cl,4
  253.         jne  .no_PLTE_1
  254. @@:
  255.         add  edx,44
  256.         mov  [eax+20],edx ; palette pointer (offset from file start)
  257. ;---------------------------------------------------------------------
  258.         cmp  cl,0
  259.         je      @f
  260.         cmp  cl,4
  261.         jne  .PLTE
  262. @@:
  263.         mov  ecx,256*4
  264.         jmp  .PLTE_1
  265. .PLTE:
  266.         push eax
  267.         mov  eax,[PLTE.size]
  268.         xor  edx,edx
  269.         mov  ebx,3
  270.         div  ebx
  271.         shl  eax,2
  272.         mov  ecx,eax
  273.         pop  eax
  274. .PLTE_1:
  275.         mov  [eax+24],ecx  ; palette area size
  276.         jmp  @f
  277. .no_PLTE_1:
  278.     xor  ecx,ecx
  279. @@:
  280. ;---------------------------------------------------------------------  
  281.         add  ecx,dword 44
  282.         mov  [eax+28],ecx  ; pixels pointer (offset from file start)
  283. ;       mov ebx,[pointer]
  284. ;       mov [ebx+44],ecx
  285. ;---------------------------------------------------------------------
  286.         mov      ecx, [IHDR_data.Width] ; width BMP
  287.         imul ecx,[resolution]
  288.         mov  edi,ecx
  289.         shr  ecx,3
  290.         test edi,7
  291.         jz  @f
  292.         inc  ecx
  293. @@:
  294.         imul ecx,[IHDR_data.Height]  ;  high  BMP
  295.         mov  [eax+32],ecx  ; pixels area size
  296. ;---------------------------------------------------------------------  
  297.         xor  ecx,ecx            ; Stub!!!
  298.         mov  [eax+36],ecx  ;Transparency pointer (offset from file start)
  299.         mov  [eax+40],ecx  ;Transparency area size
  300.  
  301. ;---------------------------------------------------------------------
  302. ; Finish create RAW header
  303. ;---------------------------------------------------------------------
  304.  
  305.         mov eax,[raw_area]
  306.         mov edi,[eax+20]  ; palette pointer (offset from file start)
  307.         add edi,eax
  308.        
  309.         mov esi,[PLTE.pointer]
  310.         mov eax,[PLTE.size]
  311.         xor edx,edx
  312.         mov ebx,3
  313.         div ebx
  314.         mov ecx,eax
  315.         mov eax,[IHDR_data.Color_type]
  316.         cmp  al,byte 0
  317.         je  .grayscale_palette
  318.         cmp  al,byte 4
  319.         je  .grayscale_palette
  320.         cmp  al,byte 3   ; Each pixel is a palette index
  321.                                                   ; a PLTE chunk must appear.
  322.         jne  .no_palette
  323. @@:
  324.         cld
  325.         lodsd
  326.         dec esi
  327.         and  eax,0xffffff
  328.  
  329.         mov bl,al
  330.         shr eax,8
  331.         xchg ah,bl
  332.         shl eax,8
  333.         mov al,bl
  334.        
  335.         cld
  336.         stosd
  337.         dec ecx
  338.         jnz @r
  339.         jmp .no_palette
  340. ;-------------------------------
  341. .grayscale_palette:
  342. ;       cmp  [IHDR_data.Bit_depth], dword 1
  343. ;       jne @f
  344. ;       xor eax,eax
  345. ;       cld
  346. ;       stosd
  347. ;       dec eax
  348. ;       and eax,0xffffff
  349. ;       cld
  350. ;       stosd
  351. ;       jmp  .no_palette
  352. @@:
  353.         mov eax,1
  354.         mov ecx,[resolution]
  355.         cmp ecx,8
  356.         jbe @f
  357.         mov ecx,8
  358. @@:
  359.         shl eax,cl
  360.         mov ecx,eax
  361.         mov eax,256
  362. ;       mov ebx,[resolution]
  363.         xor edx,edx
  364.         div ecx  ;ebx
  365.         mov edx,eax
  366.         cmp edx,0
  367.         jne @f
  368.         inc edx
  369. @@:
  370.         xor eax,eax
  371. ;       mov eax,0xffffff
  372. ;       mov ecx,256
  373.        
  374. @@:    
  375.         cld
  376.         stosd
  377. ;       inc ah
  378.         add ah,dl
  379. ;       inc al
  380.         add al,dl
  381.         shl eax,8
  382.         mov al,ah
  383.  
  384. ;       dec ah
  385. ;       dec al
  386. ;       shl eax,8
  387. ;       mov al,ah
  388.        
  389.         and eax,0xffffff
  390.         dec ecx
  391.         jnz @r
  392.  
  393.         sub edi,4      
  394.         xor eax,eax
  395.         dec eax
  396.         and eax,0xffffff
  397.         cld
  398.         stosd
  399. ;-------------------------------       
  400. .no_palette:
  401.  
  402. ;---------------------------------------------------------------------
  403.         mov ebx,[IDAT.pointer]
  404.         mov al,[ebx]
  405.         mov [IDAT_CMF],al  ; Compression Method and flags
  406.         mov ah,al
  407.         and al,1111b
  408.         mov [IDAT_ÑÌ],al  ; Compression method
  409.         shr ah,4
  410.         mov [IDAT_CINFO],ah  ; Compression info
  411.         mov al,[ebx+1]
  412.         mov [IDAT_FLG],al  ; FLaGs
  413.         mov ah,al
  414.         and al,11111b
  415.         mov [IDAT_FCHECK],al  
  416.         mov al,ah
  417.         and al,100000b
  418.         shr al,5
  419.         mov [IDAT_FDICT],al  ; Preset dictionary
  420.         shr ah,6
  421.         mov [IDAT_FLEVEL],ah  ; Compression level
  422.         mov  al,[IDAT_ÑÌ]
  423. ;       test al,1000b     ; Compression method = 8 ?
  424.         cmp  al,8
  425.         jnz  no_png_file
  426.         add     [IDAT.pointer],2
  427.         sub     [IDAT.size],2
  428. ;       xor  eax,eax
  429. ;       mov  ah,[IDAT_FLG]
  430. ;       mov  al,[IDAT_CMF]
  431. ;       imul  eax,31
  432. ;       shr     eax,16
  433. ;       cmp  [IDAT_FCHECK],al
  434. ;       jne  no_png_file  
  435.  
  436. ;        
  437. ;       cmp  [IDAT_FDICT],1
  438. ;       jne  .no_IDAT_FDICT
  439. ;       mov  ecx,[ebx+2]
  440. ;       jmp  .IDAT_DICT
  441. ;.no_IDAT_FDICT:
  442. ;       mov  eax,[IDAT.pointer]
  443. ;       add  eax,[IDAT.size]
  444. ;       mov  ecx,[eax]
  445. ;.IDAT_DICT:
  446. ;       call convert_NBO_to_PC
  447. ;       mov [IDAT_DICT],ecx
  448. ;               jmp  .ret_ok
  449.  
  450. ;       jmp  no_png_file
  451.  
  452. ;       mov ecx,[file_size]
  453. ;        mcall 68, 12
  454. ;       mov [IDAT_raw_area],eax  ; store pointer of image area
  455. ;---------------------------------------------------------------------
  456. .start_Deflate_unpack:
  457.         push    eax     ; âûäåëÿåì â ñòåêå ïåðåìåííóþ äëÿ *pUnpackedLength
  458.                         ; ïîñêîëüêó çíà÷åíèå íåâàæíî, êîðî÷å è áûñòðåå âñåãî
  459.                         ; ñäåëàòü ýòî îäíîáàéòîâûì push <ðåãèñòð>
  460.         push    esp     ; à âîò è ñàì óêàçàòåëü pUnpackedLength
  461.         push    esi     ; êàêîé-íèáóäü ïàðàìåòð
  462.         push    deflate_callback
  463.         call    [deflate_unpack]
  464.         pop     ecx     ; âûòàëêèâàåì UnpackedLength
  465. ; êàê è â ïåðâîì ñëó÷àå, eax = óêàçàòåëü íà ðàñïàêîâàííûå äàííûå, ecx = ðàçìåð
  466.         mov [unpacked_data],eax
  467.         mov esi,eax
  468. ;--------------------------------------------------------------------- 
  469.         mov eax,[raw_area]
  470.         mov edi,[eax+28]  ; pixels pointer (offset from file start)
  471.         add edi,eax
  472. ;       mov ecx,edx
  473.         sub ecx,[IHDR_data.Height]
  474.         mov [first_line],byte 1
  475.         cmp [IHDR_data.Color_type],byte 0
  476.         je  .PLTE_and_grayscale
  477.         cmp [IHDR_data.Color_type],byte 3
  478.         je  .PLTE_and_grayscale
  479.         cmp [IHDR_data.Color_type],byte 4
  480.         je  .PLTE_and_grayscale
  481. ;---------------------------------------------------------------------
  482. ; Processing filtering RGB;     type 2 and 6, sample 8b
  483. ;---------------------------------------------------------------------
  484.         mov eax,ecx
  485. ;       sub eax,[IHDR_data.Height]
  486.         xor edx,edx
  487.         mov ebx,3
  488.         cmp [IHDR_data.Color_type],byte 6
  489.         jne @f
  490.         inc ebx
  491. @@:
  492.         cmp [IHDR_data.Bit_depth],byte 16
  493.         jne @f
  494.         shl ebx,1
  495. @@:
  496.         div ebx
  497.         mov ecx,eax
  498.         cmp [IHDR_data.Bit_depth],byte 16
  499.         je  .filter_Bit_depth_16
  500. ;       jmp .ret_ok
  501. ;---------------------------------------------------------------------
  502.         cmp [IHDR_data.Interlace_method], byte 0 ; Interlaced
  503.         je  @f
  504.         call filtering_RGB_Interlaced
  505.         jmp .converting_MSB_to_LSB
  506. @@:
  507. ;---------------------------------------------------------------------
  508.         push edi
  509.         call filtering_RGB
  510.         pop  edi
  511. ;---------------------------------------------------------------------
  512. ; converting MSB to LSB
  513. ;  24b = 1B x 3 sample
  514. ;  32b = 1B x 4 sample
  515. .converting_MSB_to_LSB:
  516.         call .calculate_all_pixels
  517.         mov ebx,3
  518.         cmp [IHDR_data.Color_type],byte 6
  519.         jne @f
  520.         inc ebx
  521. @@:
  522.         sub edi,ebx
  523. @@:
  524.         add edi,ebx
  525.         mov al,[edi]
  526.         mov ah,[edi+2]
  527.         mov [edi+2],al
  528.         mov [edi],ah
  529.         dec ecx
  530.         jnz @r
  531.         jmp .ret_ok
  532. ;---------------------------------------------------------------------
  533. .calculate_all_pixels:
  534.         mov  ecx,[Image_Height]
  535.         imul ecx,[Image_Width]
  536.         ret
  537. ;---------------------------------------------------------------------
  538. ; Processing filtering RGB;     type 2 and 6, sample 16b
  539. ;---------------------------------------------------------------------
  540. .filter_Bit_depth_16:
  541.         cmp [IHDR_data.Interlace_method], byte 0 ; Interlaced
  542.         je  @f
  543.         call filtering_RGB_16_Interlaced
  544.         jmp .converting_MSB_to_LSB_16
  545. @@:
  546. ;---------------------------------------------------------------------
  547.         push edi
  548.         call filtering_RGB_16
  549.         pop  edi
  550. ;---------------------------------------------------------------------
  551. ; converting MSB to LSB 48 or 64b
  552. ;  48b = 2B x 3 sample
  553. ;  64b = 2B x 4 sample
  554. .converting_MSB_to_LSB_16:
  555.         call .calculate_all_pixels
  556.         mov ebx,3
  557.         cmp [IHDR_data.Color_type],byte 6
  558.         jne @f
  559.         inc ebx
  560. @@:
  561.         shl ebx,1
  562.        
  563.         sub edi,ebx
  564. @@:
  565.         add edi,ebx
  566.        
  567.         mov ax,[edi]
  568.         xchg al,ah
  569.         shl  eax,16
  570.         mov ax,[edi+4]
  571.         xchg al,ah
  572.         mov [edi],ax
  573.         shr  eax,16
  574.         mov [edi+4],ax
  575.        
  576.         mov ax,[edi+2]
  577.         xchg al,ah
  578.         mov [edi+2],ax
  579.  
  580. ;       mov ax,[edi+6]
  581. ;       xchg al,ah
  582. ;       mov [edi+6],al
  583.        
  584.         dec ecx
  585.         jnz @r
  586.         jmp .ret_ok
  587. ;---------------------------------------------------------------------
  588. ; Processing filtering Grayscale and RGB; type 2, 3 and 4, sample 8b
  589. ;--------------------------------------------------------------------- 
  590. .PLTE_and_grayscale:
  591.         cmp [IHDR_data.Color_type],byte 4
  592.         jne @f
  593.         shr  ecx,1
  594. @@:
  595.         cmp [IHDR_data.Bit_depth],byte 16
  596.         jne  @f
  597.         shr  ecx,1
  598.         jmp  .filter_grayscale_Bit_depth_16_1
  599. @@:
  600. ;---------------------------------------------------------------------
  601.         cmp [IHDR_data.Interlace_method], byte 0 ; Interlaced
  602.         je  @f
  603.         call filtering_grayscale_Interlaced
  604.         jmp .continue_1
  605. @@:
  606. ;---------------------------------------------------------------------
  607.         push edi
  608.         call filtering_grayscale
  609.         pop  edi
  610. ;---------------------------------------------------------------------
  611. .continue_1:
  612.         cmp [IHDR_data.Color_type],byte 4
  613.         jne  .ret_ok
  614. ;-------------------------------
  615. ; 8b or less     
  616.         mov  esi,edi
  617.         call .calculate_all_pixels
  618. .convert_transp_to_notransp:
  619.         cld
  620.         lodsw
  621.         stosb
  622.         dec ecx
  623.         jnz .convert_transp_to_notransp
  624.        
  625.         jmp  .ret_ok
  626. ;---------------------------------------------------------------------
  627. ; Processing filtering Grayscale and RGB; type 2, 3 and 4, sample 16b
  628. ;---------------------------------------------------------------------
  629. .filter_grayscale_Bit_depth_16_1:
  630.         cmp [IHDR_data.Interlace_method], byte 0 ; Interlaced
  631.         je  @f
  632.         call filtering_grayscale_16_Interlaced
  633.         jmp .continue_2
  634. @@:
  635. ;---------------------------------------------------------------------
  636.         push edi
  637.         call filtering_grayscale_16
  638.         pop  edi
  639. ;---------------------------------------------------------------------
  640. .continue_2:
  641.         cmp  [IHDR_data.Color_type],byte 4
  642.         jne  .ret_ok
  643. ;-------------------------------
  644. ; 16b
  645.         mov  esi,edi
  646.         call .calculate_all_pixels
  647. .convert_transp_to_notransp_1:
  648.         cld
  649.         lodsd
  650.         stosw
  651.         dec  ecx
  652.         jnz  .convert_transp_to_notransp_1
  653. ;---------------------------------------------------------------------         
  654. .ret_ok:
  655.         mov     ecx,[unpacked_data]
  656.         mcall 68, 13,
  657.         xor  eax,eax
  658. ;---------------------------------------------------------------------   
  659. .ret:
  660.         mov     ebx,[pointer]
  661.         mov     [ebx+8],eax  ; store return code
  662.         mov     eax,[raw_area]
  663.         mov     [ebx+4],eax  ; store RAW pointer
  664.         popad
  665.         ret     4
  666. ;---------------------------------------------------------------------
  667. include 'filter.inc'
  668. include 'proced.inc'
  669. include 'fl_call.inc'
  670. include 'interlac.inc'
  671. ;---------------------------------------------------------------------
  672. Check_Header:
  673.         pushad
  674.         mov     eax,dword [esp+36]
  675.         call    check_header_1
  676.         jne     no_png_file
  677.         popad
  678.         ret     4
  679. ;---------------------------------------------------------------------
  680. Associations:
  681.         dd Associations.end - Associations
  682.         db 'PNG',0
  683. .end:
  684.         db 0
  685. ;---------------------------------------------------------------------
  686. align 4
  687. EXPORTS:
  688.                 dd              szStart,        START
  689.                 dd              szVersion,      0x00010002
  690.                 dd              szCheck,        Check_Header
  691.                 dd              szAssoc,        Associations
  692.                 dd              0
  693.  
  694. szStart         db 'START',0
  695. szVersion       db 'version',0
  696. szCheck         db 'Check_Header',0
  697. szAssoc         db 'Associations',0
  698.  
  699. ;*****************************************************************************
  700. ; Chunks names
  701. ;*****************************************************************************
  702. ; Critical chunks
  703. IHDR_name: db 'IHDR' ; Image header
  704. PLTE_name: db 'PLTE' ; Palette
  705. IDAT_name: db 'IDAT' ; Image data
  706. IEND_name: db 'IEND' ; Image trailer
  707.  
  708. ; Ancillary chunks
  709. ;tRNS_name: db 'tRNS' ; Transparency
  710.  
  711. ;; Color space information
  712. ;gAMA_name: db 'gAMA' ; Image gamma
  713. ;cHRM_name: db 'cHRM' ; Primary chromaticities
  714. ;sRGB_name: db 'sRGB' ; Standard RGB color space
  715. ;iCCP_name: db 'iCCP' ; Embedded ICC profile
  716.  
  717. ;; Textual information
  718. ;tEXt_name: db 'tEXt' ; Textual data
  719. ;zTXt_name: db 'zTXt' ; Compressed textual data
  720. ;iTXt_name: db 'iTXt' ; International textual data
  721.  
  722. ;; Miscellaneous information
  723. ;bKGD_name: db 'bKGD' ; Background color
  724. ;pHYs_name: db 'pHYs' ; Physical pixel dimensions
  725. ;sBIT_name: db 'sBIT' ; Significant bits
  726. ;sPLT_name: db 'sPLT' ; Suggested palette
  727. ;hIST_name: db 'hIST' ; Palette histogram
  728. ;tIME_name: db 'tIME' ; Imagelast-modification time
  729. ;*****************************************************************************
  730. calculate_Interlaced_counters:
  731. .1:     dd calculate_Interlaced_1
  732. .2:     dd calculate_Interlaced_2
  733. .3:     dd calculate_Interlaced_3
  734. .4:     dd calculate_Interlaced_4
  735. .5:     dd calculate_Interlaced_5
  736. .6:     dd calculate_Interlaced_6
  737. .7:     dd calculate_Interlaced_7
  738.  
  739. deflate_start_offset dd 0
  740.  
  741. pointer                 dd 0
  742. image_file              dd 0
  743. file_size               dd 0
  744. raw_area                dd 0
  745. resolution              dd 0
  746.  
  747. Chunk_pointer   dd 0
  748. next_Chunk              dd 0
  749.  
  750. deflate_unpack  dd 0
  751.  
  752. unpacked_data   dd 0
  753. ;IDAT_raw_area  dd 0
  754. ;IDAT_raw_counter dd 0
  755.  
  756. Interlaced_area dd 0
  757.  
  758. Starting_Row:   dd 0
  759. Starting_Col:   dd 0
  760. Row_Increment:  dd 0
  761. Col_Increment:  dd 0
  762.  
  763. Interlaced_step dd 0
  764.  
  765.  
  766. counter_IDAT_Chunk dd 0
  767.  
  768. ;CRC32                   dd 0
  769. ;CRC32table:  rd 256
  770. ;Adler32                  dd 0
  771.  
  772. Image_Width dd 0
  773. Image_Height dd 0
  774.  
  775. IHDR_data:
  776. .Width                                  dd 0  ;+0
  777. .Height                                 dd 0  ;+4
  778. .Bit_depth:                             db 0  ;+8
  779. .Color_type:                    db 0  ;+9
  780. .Compression_method:    db 0  ;+10
  781. .Filter_method:                 db 0  ;+11
  782. .Interlace_method:              db 0  ;+12
  783.  
  784. IDAT_CMF                db 0              ; Compression Method and flags
  785.   IDAT_ÑÌ               db 0            ; Compression method
  786.   IDAT_CINFO    db 0            ; Compression info
  787. IDAT_FLG                db 0              ; FLaGs
  788.   IDAT_FCHECK   db 0    ;
  789.   IDAT_FDICT    db 0    ; Preset dictionary
  790.   IDAT_FLEVEL   db 0    ; Compression level
  791.                                                           ; "deflate" method (CM = 8) sets these flags:
  792.                                                           ; 0 - compressor used fastest algorithm
  793.                                                           ; 1 - compressor used fast algorithm
  794.                                                           ; 2 - compressor used default algorithm
  795.                                                           ; 3 - compressor used maximum compression, slowest algorithm  
  796. IDAT_DICT       dd 0  ; dictionary identifier = Adler-32 checksum
  797. BFINAL          db 0  ; set 1 only if this is the last block of the data set
  798. BTYPE           db 0  ; specifies how the data are compressed:
  799.                                  ; 00 - no compression
  800.                                  ; 01 - compressed with fixed Huffman codes
  801.                                  ; 10 - compressed with dynamic Huffman codes
  802.                                  ; 11 - reserved (error)
  803.                                  
  804. line_filter_type        dd 0 ; 0 None
  805.                                                          ; 1 Sub
  806.                                                          ; 2 Up
  807.                                                          ; 3 Averag
  808.                                                          ; 4 Paeth
  809. first_line      db 0
  810. first_pixel     db 0
  811.  
  812. previous_pixel_value:
  813.  rb 8
  814.  
  815. Paeth_filter:
  816. .a:     dw 0
  817. .b:     dw 0
  818. .c:     dw 0
  819. .p      dd 0
  820. .pa     dd 0
  821. .pb     dd 0
  822. .pc     dd 0
  823. ;*****************************************************************************
  824. ; Chunks pointer
  825. ;*****************************************************************************
  826. IHDR:
  827. .pointer        dd 0
  828. .size           dd 0
  829.  
  830. PLTE:
  831. .pointer        dd 0
  832. .size           dd 0
  833.  
  834. IDAT:
  835. .pointer        dd 0
  836. .size           dd 0
  837.  
  838. IEND:
  839. .pointer        dd 0
  840. .size           dd 0
  841.  
  842. ;tRNS:
  843. ;.pointer       dd 0
  844. ;.size          dd 0
  845.  
  846. ;gAMA:
  847. ;.pointer       dd 0
  848. ;.size          dd 0
  849.  
  850. ;cHRM:
  851. ;.pointer       dd 0
  852. ;.size          dd 0
  853.  
  854. ;sRGB:
  855. ;.pointer       dd 0
  856. ;.size          dd 0
  857.  
  858. ;iCCP:
  859. ;.pointer       dd 0
  860. ;.size          dd 0
  861.  
  862. ;tEXt:
  863. ;.pointer       dd 0
  864. ;.size          dd 0
  865.  
  866. ;zTXt:
  867. ;.pointer       dd 0
  868. ;.size          dd 0
  869.  
  870. ;iTXt:
  871. ;.pointer       dd 0
  872. ;.size          dd 0
  873.  
  874. ;bKGD:
  875. ;.pointer       dd 0
  876. ;.size          dd 0
  877.  
  878. ;pHYs:
  879. ;.pointer       dd 0
  880. ;.size          dd 0
  881.  
  882. ;sBIT:
  883. ;.pointer       dd 0
  884. ;.size          dd 0
  885.  
  886. ;sPLT:
  887. ;.pointer       dd 0
  888. ;.size          dd 0
  889.  
  890. ;hIST:
  891. ;.pointer       dd 0
  892. ;.size          dd 0
  893.  
  894. ;tIME:
  895. ;.pointer       dd 0
  896. ;.size          dd 0
  897.