Subversion Repositories Kolibri OS

Rev

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

  1.  
  2.  
  3. ; GIF LITE v2.0 by Willow
  4. ; Written in pure assembler by Ivushkin Andrey aka Willow
  5. ;
  6. ; This include file will contain functions to handle GIF image format
  7. ;
  8. ; Created: August 15, 2004
  9. ; Last changed: September 9, 2004
  10.  
  11. ; Change COLOR_ORDER in your program
  12. ; if colors are displayed improperly
  13.  
  14. COLOR_ORDER equ MENUETOS
  15. if ~ (COLOR_ORDER in <MENUETOS,OTHER>)
  16. ; This message may not appear under MenuetOS, so watch...
  17.   display 'Please define COLOR_ORDER: MENUETOS or OTHER',13,10
  18. end if
  19.  
  20. ; virtual structure, used internally
  21.  
  22. struc GIF_list
  23. {
  24.     .NextImg rd 1
  25.     .Left   rw 1
  26.     .Top    rw 1
  27.     .Width  rw 1
  28.     .Height rw 1
  29. }
  30.  
  31. struc GIF_info
  32. {
  33.     .Left   rw 1
  34.     .Top    rw 1
  35.     .Width  rw 1
  36.     .Height rw 1
  37. }
  38.  
  39. _null fix 0x1000
  40.  
  41. ; ****************************************
  42. ;   FUNCTION GetGIFinfo - retrieve Nth image info
  43. ; ****************************************
  44. ; in:
  45. ;   esi - pointer to image list header
  46. ;   ecx - image_index (0...img_count-1)
  47. ;   edi - pointer to GIF_info structure to be filled
  48.  
  49. ; out:
  50. ;   eax - pointer to RAW data, or 0, if error
  51.  
  52. GetGIFinfo:
  53.     push  esi ecx edi
  54.     xor   eax,eax
  55.     jecxz .eloop
  56.   .lp:
  57.     mov   esi,[esi]
  58.     test  esi,esi
  59.     jz    .error
  60.     loop  .lp
  61.   .eloop:
  62.     add   esi,4
  63.     movsd
  64.     movsd
  65.     mov   eax,esi
  66.   .error:
  67.     pop   edi ecx esi
  68.     ret
  69.  
  70. ; ****************************************
  71. ;   FUNCTION ReadGIF - unpacks GIF image
  72. ; ****************************************
  73. ; in:
  74. ;   esi - pointer to GIF file in memory
  75. ;   edi - pointer to output image list
  76. ;   eax - pointer to work area (MIN 16 KB!)
  77.  
  78. ; out:
  79. ;   eax - 0, all OK;
  80. ;   eax - 1, invalid signature;
  81. ;   eax >=8, unsupported image attributes
  82. ;
  83. ;   ecx - number of images
  84.  
  85. ReadGIF:
  86.     push esi edi
  87.     mov  [.table_ptr],eax
  88.     mov  [.cur_info],edi
  89.     xor  eax,eax
  90.     mov  [.globalColor],eax
  91.     mov  [.img_count],eax
  92.     inc  eax
  93.     cmp  dword[esi],'GIF8'
  94.     jne  .er            ; signature
  95.     mov  ecx,[esi+0xa]
  96.     inc  eax
  97.     add  esi,0xd
  98.     mov  edi,esi
  99.     bt   ecx,7
  100.     jnc  .nextblock
  101.     mov  [.globalColor],esi
  102.     call .Gif_skipmap
  103.   .nextblock:
  104.     cmp  byte[edi],0x21
  105.     jne  .noextblock
  106.     inc  edi
  107.     cmp  byte[edi],0xf9 ; Graphic Control Ext
  108.     jne  .no_gc
  109.     add  edi,7
  110.     jmp  .nextblock
  111.   .no_gc:
  112.     cmp  byte[edi],0xfe ; Comment Ext
  113.     jne  .no_comm
  114.     inc  edi
  115.   .block_skip:
  116.     movzx eax,byte[edi]
  117.     lea  edi,[edi+eax+1]
  118.     cmp  byte[edi],0
  119.     jnz  .block_skip
  120.     inc  edi
  121.     jmp  .nextblock
  122.   .no_comm:
  123.     cmp  byte[edi],0xff ; Application Ext
  124.     jne  .nextblock
  125.     add  edi,13
  126.     jmp  .block_skip
  127.   .noextblock:
  128.     cmp  byte[edi],0x2c    ; image beginning
  129.     jne  .er
  130.     inc  [.img_count]
  131.     inc  edi
  132.     mov  esi,[.cur_info]
  133.     add  esi,4
  134.     xchg esi,edi
  135.     movsd
  136.     movsd
  137.     push edi
  138.     movzx ecx,word[esi]
  139.     inc  esi
  140.     bt   ecx,7
  141.     jc   .uselocal
  142.     push [.globalColor]
  143.     mov  edi,esi
  144.     jmp  .setPal
  145.   .uselocal:
  146.     call .Gif_skipmap
  147.     push esi
  148.   .setPal:
  149.     movzx ecx,byte[edi]
  150.     inc  ecx
  151.     mov  [.codesize],ecx
  152.     dec  ecx
  153.     pop  [.Palette]
  154.     lea  esi,[edi+1]
  155.     mov  edi,[.table_ptr]
  156.     xor  eax,eax
  157.     cld
  158.     lodsb               ; eax - block_count
  159.     add  eax,esi
  160.     mov  [.block_ofs],eax
  161.     mov  [.bit_count],8
  162.     mov  eax,1
  163.     shl  eax,cl
  164.     mov  [.CC],eax
  165.     inc  eax
  166.     mov  [.EOI],eax
  167.     lea  ecx,[eax-1]
  168.     mov  eax, _null shl 16
  169.   .filltable:
  170.     stosd
  171.     inc  eax
  172.     loop .filltable
  173.     pop  edi
  174.     mov  [.img_start],edi
  175.   .reinit:
  176.     mov  edx,[.EOI]
  177.     inc  edx
  178.     push [.codesize]
  179.     pop  [.compsize]
  180.     call .Gif_get_sym
  181.     cmp  eax,[.CC]
  182.     je   .reinit
  183.     call .Gif_output
  184.   .cycle:
  185.     movzx ebx,ax
  186.     call .Gif_get_sym
  187.     cmp  eax,edx
  188.     jae  .notintable
  189.     cmp  eax,[.CC]
  190.     je   .reinit
  191.     cmp  eax,[.EOI]
  192.     je   .end
  193.     call .Gif_output
  194.   .add:
  195.     push eax
  196.     mov  eax,[.table_ptr]
  197.     mov  [eax+edx*4],ebx
  198.     pop  eax
  199.     cmp  edx,0xFFF
  200.     jae  .cycle
  201.     inc  edx
  202.     bsr  ebx,edx
  203.     cmp  ebx,[.compsize]
  204.     jne  .noinc
  205.     inc  [.compsize]
  206.   .noinc:
  207.     jmp  .cycle
  208.   .notintable:
  209.     push eax
  210.     mov  eax,ebx
  211.     call .Gif_output
  212.     push ebx
  213.     movzx eax,bx
  214.     call .Gif_output
  215.     pop  ebx eax
  216.     jmp  .add
  217.   .er:
  218.     pop  edi
  219.     jmp  .ex
  220.   .end:
  221.     mov  eax,[.cur_info]
  222.     mov  [eax],edi
  223.     mov  [.cur_info],edi
  224.     add  esi,2
  225.     xchg esi,edi
  226.   .nxt:
  227.     cmp  byte[edi],0
  228.     jnz  .continue
  229.     inc  edi
  230.     jmp  .nxt
  231.   .continue:
  232.     cmp  byte[edi],0x3b
  233.     jne  .nextblock
  234.     xor  eax,eax
  235.     stosd
  236.     mov  ecx,[.img_count]
  237.   .ex:
  238.     pop  edi esi
  239.     ret
  240.  
  241. .Gif_skipmap:
  242. ; in: ecx - image descriptor, esi - pointer to colormap
  243. ; out: edi - pointer to area after colormap
  244.  
  245.     and  ecx,111b
  246.     inc  ecx            ; color map size
  247.     mov  ebx,1
  248.     shl  ebx,cl
  249.     lea  ebx,[ebx*2+ebx]
  250.     lea  edi,[esi+ebx]
  251.     ret
  252.  
  253. .Gif_get_sym:
  254.     mov  ecx,[.compsize]
  255.     push ecx
  256.     xor  eax,eax
  257.   .shift:
  258.     ror  byte[esi],1
  259.     rcr  eax,1
  260.     dec  [.bit_count]
  261.     jnz  .loop1
  262.     inc  esi
  263.     cmp  esi,[.block_ofs]
  264.     jb   .noblock
  265.     push eax
  266.     xor  eax,eax
  267.     lodsb
  268.     test eax,eax
  269.     jnz  .nextbl
  270.     mov  eax,[.EOI]
  271.     sub  esi,2
  272.     add  esp,8
  273.     jmp  .exx
  274.   .nextbl:
  275.     add  eax,esi
  276.     mov  [.block_ofs],eax
  277.     pop  eax
  278.   .noblock:
  279.     mov  [.bit_count],8
  280.   .loop1:
  281.     loop .shift
  282.     pop  ecx
  283.     rol  eax,cl
  284.   .exx:
  285.     xor  ecx,ecx
  286.     ret
  287.  
  288. .Gif_output:
  289.     push esi eax edx
  290.     mov  edx,[.table_ptr]
  291.   .next:
  292.     push word[edx+eax*4]
  293.     mov  ax,word[edx+eax*4+2]
  294.     inc  ecx
  295.     cmp  ax,_null
  296.     jnz  .next
  297.     shl  ebx,16
  298.     mov  bx,[esp]
  299.   .loop2:
  300.     pop  ax
  301.  
  302.     lea  esi,[eax+eax*2]
  303.     add  esi,[.Palette]
  304.  
  305.     if COLOR_ORDER eq MENUETOS
  306.         mov  esi,[esi]
  307.         bswap esi
  308.         shr  esi,8
  309.         mov  [edi],esi
  310.         add  edi,3
  311.     else
  312.         movsw
  313.         movsb
  314.     end if
  315.  
  316.     loop .loop2
  317.     pop  edx eax esi
  318.     ret
  319.  
  320.     .globalColor rd 1
  321.     .img_count rd 1
  322.     .cur_info rd 1        ; image table pointer
  323.     .img_start rd 1
  324.     .codesize rd 1
  325.     .compsize rd 1
  326.     .bit_count rd 1
  327.     .CC rd 1
  328.     .EOI rd 1
  329.     .Palette rd 1
  330.     .block_ofs rd 1
  331.     .table_ptr rd 1
  332.