Subversion Repositories Kolibri OS

Rev

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

  1. ;*****************************************************************************
  2. ; GIF to RAW1 convert plugin - for zSea image viewer
  3. ; Copyright (c) 2009, Evgeny Grechnikov aka Diamond
  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 Evgeny Grechnikov ''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. ; Based on gif_lite.inc (c) Ivuskin Andrey aka Willow and Diamond 2004-2007
  29. ;*****************************************************************************
  30. ; Some small changes (c) 2011 Marat Zakiyanov aka Mario79, aka Mario
  31. ;*****************************************************************************
  32.  
  33. format MS COFF
  34.  
  35. public EXPORTS
  36.  
  37. section '.flat' code readable align 16
  38.  
  39. START:
  40.         pushad
  41.         mov     eax,dword [esp+36]
  42.         mov     esi, [eax]      ; esi -> GIF data
  43.         mov     ebp, [eax+12]   ; ebp = file size
  44.         xor     ebx, ebx        ; ebx -> list of images, not allocated yet
  45.         call    check_header_1
  46.         jz      ReadGIF
  47. ReadGIF.end:
  48. ; general exit from the function
  49.         xor     eax, eax
  50.         cmp     ebx, eax
  51.         jz      .bad
  52.         cmp     dword [ebx+4], eax
  53.         jnz     ReadGIF.ret
  54.         mov     ecx, ebx
  55.         push    68
  56.         pop     eax
  57.         push    13
  58.         pop     ebx
  59.         int     40h
  60.         xor     ebx, ebx
  61. .bad:
  62.         inc     eax     ; bad image
  63. ReadGIF.ret:
  64.         mov     ecx, [esp+28]
  65.         mov     [ecx+4], ebx    ; save RAW data ptr
  66.         mov     [ecx+8], eax    ; save result
  67.         popad
  68.         ret     4
  69. ReadGIF.animated.ret:
  70.         mov     ebx, [ReadGIF.gifList]
  71.         jmp     ReadGIF.ret
  72. _null fix 0x1000
  73. ReadGIF:
  74. ; allocate one page for list of images
  75.         mov     ecx, 0x1000
  76.         push    68
  77.         pop     eax
  78.         push    12
  79.         pop     ebx
  80.         int     40h
  81.         xchg    eax, ebx
  82.         test    ebx, ebx
  83.         jnz     @f
  84.         mov     al, 2   ; no memory
  85.         jmp     .ret
  86. @@:
  87.     mov  dword[ebx],'RAW1'
  88.     xor  eax,eax
  89.     mov  [.globalColor],eax
  90.     mov  [.globalColorSize],eax
  91.     mov  [.curImageIndex],eax
  92.     sub  ebp,0xd
  93.     jb   .end
  94.     movzx eax,word[esi+6]
  95.     mov  [ebx+8],eax
  96.     movzx eax,word[esi+8]
  97.     mov  [ebx+12],eax
  98.     mov  cl,[esi+0xa]
  99.     add  esi,0xd
  100.     test cl,cl
  101.     jns  .nextblock
  102.     mov  [.globalColor],esi
  103.     push ebx
  104.     call .Gif_skipmap
  105.     mov  [.globalColorSize],ebx
  106.     pop  ebx
  107.     jb   .end
  108.   .nextblock:
  109.     dec  ebp
  110.     js   .end
  111.     cmp  byte[esi],0x21
  112.     jne  .noextblock
  113.     inc  esi
  114.     cmp  byte[esi],0xf9 ; Graphic Control Ext
  115.     jne  .no_gc
  116.     sub  ebp,7
  117.     jc   .end
  118.     mov  ecx,[ebx+4]
  119.     shl  ecx,4
  120.     add  ecx,ebx
  121.     mov  eax,[esi+3]
  122.     mov  [ecx+16+12],ax
  123. ;    test byte[esi+2],1
  124. ;    setnz byte[ecx+16+14]
  125.         mov al,[esi+2]
  126.         mov [ecx+16+14], al
  127.        
  128.     mov  al,[esi+5]
  129.     mov  [ecx+16+15],al
  130.     add  esi,7
  131.     jmp  .nextblock
  132.   .no_gc:
  133.     inc  esi
  134.         xor eax,eax
  135.   .block_skip:
  136.     dec  ebp
  137.     js   .end
  138.     lodsb
  139.     add  esi,eax
  140.     sub  ebp,eax
  141.     jc   .end2
  142.     test eax,eax
  143.     jnz  .block_skip
  144.     jmp  .nextblock
  145.   .noextblock:
  146.     cmp  byte[esi],0x2c    ; image beginning
  147.     jne  .end
  148.     inc  esi
  149.     sub  ebp,11
  150.     jc   .end2
  151.     movzx ecx,word[esi+4]  ; ecx = width
  152.     jecxz .end2
  153.     mov  [.width],ecx
  154.     movzx eax,word[esi+6]  ; eax = height
  155.     test eax,eax
  156.     jz   .end2
  157.     push eax ecx
  158.     imul ecx,eax
  159.     cmp  ecx,4000000h
  160.     jb   @f
  161.     pop  ecx eax
  162. .end2:
  163.     jmp  .end
  164. @@:
  165.     push ebx
  166.     push ecx
  167.     add  ecx,44+256*4
  168.     push 68
  169.     pop  eax
  170.     push 12
  171.     pop  ebx
  172.     int  0x40
  173.     pop  ecx
  174.     pop  ebx
  175.     test eax,eax
  176.     jnz  @f
  177.     pop  ecx ecx
  178.     jmp  .end2
  179. @@:
  180.     xchg eax,edi
  181.     inc  dword[ebx+4]
  182.     mov  [edi+32],ecx      ; size of pixels area
  183.     mov  byte[edi+20],44   ; pointer to palette
  184.     mov  byte[edi+24+1],4  ; size of palette=256*4
  185.     mov  dword[edi+28],44+256*4 ; pointer to RAW data
  186.     pop  ecx eax
  187.     mov  dword[edi], 'RAW ' ; signature
  188.     mov  dword[edi+4],ecx  ; width
  189.     mov  dword[edi+8],eax  ; height
  190.     mov  byte[edi+12],8    ; total pixel size
  191.     mov  byte[edi+16],8    ; 8 bits per component
  192.     mov  byte[edi+18],1    ; number of components
  193.     mov  eax,[ebx+4]
  194.     shl  eax,4
  195.     add  eax,ebx
  196.     mov  [eax],edi
  197.     movzx ecx,word[esi]
  198.     mov  [eax+4],ecx
  199.     movzx ecx,word[esi+2]
  200.     mov  [eax+8],ecx
  201.     mov  eax,[edi+32]
  202.     mov  [.img_end],eax
  203.     inc  eax
  204.     mov  [.row_end],eax
  205.     and  [.pass],0
  206.     test byte[esi+8],40h
  207.     jz   @f
  208.     mov  ecx,[edi+4]
  209.     mov  [.row_end],ecx
  210. @@:
  211.     mov  cl,[esi+8]
  212.     add  esi,9
  213.     add  edi,44
  214.     push edi
  215.     test cl,cl
  216.     js   .uselocal
  217.     push esi
  218.     mov  esi,[.globalColor]
  219.     mov  ecx,[.globalColorSize]
  220.     call .swap_palette
  221.     pop  esi
  222.     jmp  .setPal
  223.   .uselocal:
  224.     push ebx
  225.     call .Gif_skipmap
  226.     jnc  @f
  227.     pop  ebx
  228.     pop  edi
  229.     jmp  .end
  230. @@:
  231.     sub  esi,ebx
  232.     mov  ecx,ebx
  233.     pop  ebx
  234.     call .swap_palette
  235.   .setPal:
  236.     movzx ecx,byte[esi]
  237.     inc  ecx
  238.     mov  [.codesize],ecx
  239.     dec  ecx
  240.     inc  esi
  241.     mov  edi,.gif_workarea
  242.     xor  eax,eax
  243.     lodsb               ; eax - block_count
  244.     add  eax,esi
  245.     mov  [.block_ofs],eax
  246.     mov  [.bit_count],8
  247.     mov  eax,1
  248.     shl  eax,cl
  249.     mov  [.CC],eax
  250.     mov  ecx,eax
  251.     inc  eax
  252.     mov  [.EOI],eax
  253.     mov  eax, _null shl 16
  254.   .filltable:
  255.     stosd
  256.     inc  eax
  257.     loop .filltable
  258.     pop  edi
  259.     add  edi,256*4
  260.     mov  [.img_start],edi
  261.     add  [.img_end],edi
  262.     add  [.row_end],edi
  263.     mov  [.ebx],ebx
  264.   .reinit:
  265.     mov  edx,[.EOI]
  266.     inc  edx
  267.     push [.codesize]
  268.     pop  [.compsize]
  269.     call .Gif_get_sym
  270.     cmp  eax,[.CC]
  271.     je   .reinit
  272.     call .Gif_output
  273.   .cycle:
  274.     movzx ebx,ax
  275.     call .Gif_get_sym
  276.     cmp  eax,edx
  277.     jae  .notintable
  278.     cmp  eax,[.CC]
  279.     je   .reinit
  280.     cmp  eax,[.EOI]
  281.     je   .unpend
  282.     call .Gif_output
  283.   .add:
  284.     mov  dword [.gif_workarea+edx*4],ebx
  285.     cmp  edx,0xFFF
  286.     jae  .cycle
  287.     inc  edx
  288.     bsr  ebx,edx
  289.     cmp  ebx,[.compsize]
  290.     jne  .noinc
  291.     inc  [.compsize]
  292.   .noinc:
  293.     jmp  .cycle
  294.   .notintable:
  295.     push eax
  296.     mov  eax,ebx
  297.     call .Gif_output
  298.     push ebx
  299.     movzx eax,bx
  300.     call .Gif_output
  301.     pop  ebx eax
  302.     jmp  .add
  303. .unpend:
  304.     mov  ebx,[.ebx]
  305.     add  ebp,esi
  306.     mov  esi,[.block_ofs]
  307.     sub  ebp,esi
  308.     jc   .end2
  309.     xor  eax,eax
  310. @@:
  311.     dec  ebp
  312.     js   .end2
  313.     lodsb
  314.     test eax,eax
  315.     jz   @f
  316.     sub  ebp,eax
  317.     jc   .end2
  318.     add  esi,eax
  319.     jmp  @b
  320. @@:
  321.     test ebp,ebp
  322.     jz   .end2
  323.     cmp  byte[esi],0x3b
  324.     jz   .end2
  325. ; next image
  326.     mov  ecx,[ebx+4]
  327.     cmp  cl,0xFF
  328.     jnz  .noresize
  329.     mov  edx,ebx
  330.     inc  ecx
  331.     inc  ecx
  332.     shl  ecx,4
  333.     push 68
  334.     pop  eax
  335.     push 20
  336.     pop  ebx
  337.     int  40h
  338.     test eax,eax
  339.     jnz  @f
  340.     mov  ebx,edx
  341.     jmp  .end2
  342.  @@:
  343.     xchg ebx,eax
  344.  .noresize:
  345.     jmp  .nextblock
  346.  
  347. .Gif_skipmap:
  348. ; in: ecx - image descriptor, esi - pointer to colormap
  349. ; out: edi - pointer to area after colormap
  350.  
  351.     and  ecx,111b       ; color map size
  352.     mov  ebx,3*2
  353.     shl  ebx,cl
  354.     add  esi,ebx
  355.     sub  ebp,ebx
  356.     ret
  357.  
  358. .Gif_get_sym:
  359.     mov  ecx,[.compsize]
  360.     push ecx
  361.     xor  eax,eax
  362.   .shift:
  363.     ror  byte[esi],1
  364.     rcr  eax,1
  365.     dec  [.bit_count]
  366.     jnz  .loop1
  367.     inc  esi
  368.     cmp  esi,[.block_ofs]
  369.     jb   .noblock
  370.     push eax
  371.     xor  eax,eax
  372.     dec  ebp
  373.     js   .dataend
  374.     lodsb
  375.     test eax,eax
  376.     jnz  .nextbl
  377.     mov  eax,[.EOI]
  378.     sub  esi,2
  379.     add  esp,8
  380.     jmp  .exx
  381.   .nextbl:
  382.     add  eax,esi
  383.     mov  [.block_ofs],eax
  384.     pop  eax
  385.   .noblock:
  386.     mov  [.bit_count],8
  387.   .loop1:
  388.     loop .shift
  389.     pop  ecx
  390.     rol  eax,cl
  391.   .exx:
  392.     xor  ecx,ecx
  393.     ret
  394.  
  395. .dataend:
  396.         pop     eax eax
  397.         mov     ebx, [.ebx]
  398.         jmp     .end2
  399.  
  400. .Gif_output:
  401.     push esi eax edx
  402.     mov  edx,.gif_workarea
  403.   .next:
  404.     push word[edx+eax*4]
  405.     mov  ax,word[edx+eax*4+2]
  406.     inc  ecx
  407.     cmp  ax,_null
  408.     jnz  .next
  409.     shl  ebx,16
  410.     mov  bx,[esp]
  411.   .loop2:
  412.     pop  ax
  413.  
  414.         stosb
  415.  
  416.     cmp  edi,[.row_end]
  417.     jb   .norowend
  418.     mov  eax,[.width]
  419.     push eax
  420.     sub  edi,eax
  421.     add  eax,eax
  422.     cmp  [.pass],3
  423.     jz   @f
  424.     add  eax,eax
  425.     cmp  [.pass],2
  426.     jz   @f
  427.     add  eax,eax
  428. @@:
  429.     add  edi,eax
  430.     pop  eax
  431.     cmp  edi,[.img_end]
  432.     jb   .nextrow
  433.     mov  edi,[.img_start]
  434.     inc  [.pass]
  435.     add  edi,eax
  436.     cmp  [.pass],3
  437.     jz   @f
  438.     add  edi,eax
  439.     cmp  [.pass],2
  440.     jz   @f
  441.     add  edi,eax
  442.     add  edi,eax
  443. @@:
  444. .nextrow:
  445.     add  eax,edi
  446.     mov  [.row_end],eax
  447.     xor  eax,eax
  448. .norowend:
  449.  
  450.     loop .loop2
  451.     pop  edx eax esi
  452.     ret
  453.  
  454. .swap_palette:
  455.     xor  eax,eax
  456. @@:
  457.     lodsb
  458.     mov  ah,al
  459.     lodsb
  460.     shl  eax,8
  461.     lodsb
  462.     stosd
  463.     sub  ecx,3
  464.     jnz  @b
  465.     ret
  466. ;---------------------------------------------------------------------
  467. check_header_1:
  468.         and     dword [eax+8], 0
  469.         cmp     dword [eax+12], 6
  470.         jb      .err
  471.         push    eax
  472.         mov     eax, [eax]
  473.         cmp     dword [eax], 'GIF8'
  474.         jnz     .errpop
  475.         cmp     byte [eax+5], 'a'
  476.         jnz     .errpop
  477.         cmp     byte [eax+4], '7'
  478.         jz      @f
  479.         cmp     byte [eax+4], '9'
  480.         jnz     .errpop
  481. @@:
  482.         pop     eax
  483.         ret
  484. .errpop:
  485.         pop     eax
  486. .err:
  487.         inc     dword [eax+8]
  488.         ret
  489. ;---------------------------------------------------------------------
  490. check_header:
  491.         pushad
  492.         mov     eax,dword [esp+36]
  493.         call    check_header_1
  494.         popad
  495.         ret     4
  496. ;---------------------------------------------------------------------
  497. Associations:
  498. dd  Associations.end - Associations
  499. db 'GIF',0
  500. .end:
  501. db 0
  502. ;---------------------------------------------------------------------
  503. align 4
  504. EXPORTS:
  505.         dd szStart, START
  506.         dd szVersion, 0x00010002
  507.         dd szCheck, check_header
  508.         dd szAssoc, Associations
  509.         dd 0
  510.  
  511. szStart         db 'START',0
  512. szVersion       db 'version',0
  513. szCheck         db 'Check_Header',0
  514. szAssoc         db 'Associations',0
  515.  
  516. section '.data' data readable writable align 16
  517.     ReadGIF.globalColor rd 1
  518.     ReadGIF.globalColorSize rd 1
  519.     ReadGIF.cur_info rd 1        ; image table pointer
  520.     ReadGIF.codesize rd 1
  521.     ReadGIF.compsize rd 1
  522.     ReadGIF.bit_count rd 1
  523.     ReadGIF.CC rd 1
  524.     ReadGIF.EOI rd 1
  525.     ReadGIF.block_ofs rd 1
  526.     ReadGIF.row_end rd 1
  527.     ReadGIF.img_end rd 1
  528.     ReadGIF.img_start rd 1
  529.     ReadGIF.pass rd 1
  530.     ReadGIF.width rd 1
  531.     ReadGIF.ebx rd 1
  532.     ReadGIF.gifList rd 1
  533.     ReadGIF.curImageIndex rd 1
  534.     ReadGIF.gif_workarea rb 16*1024
  535.