Subversion Repositories Kolibri OS

Rev

Rev 109 | Go to most recent revision | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 109 Rev 552
Line 1... Line 1...
1
; GIF LITE v2.0 by Willow
1
; GIF LITE v3.0 by Willow
2
; Written in pure assembler by Ivushkin Andrey aka Willow
2
; Written in pure assembler by Ivushkin Andrey aka Willow
-
 
3
; Modified by Diamond
3
;
4
;
4
; This include file will contain functions to handle GIF image format
5
; This include file will contain functions to handle GIF image format
5
;
6
;
6
; Created: August 15, 2004
7
; Created: August 15, 2004
7
; Last changed: September 9, 2004
8
; Last changed: June 24, 2007
Line -... Line 9...
-
 
9
 
-
 
10
; Requires kglobals.inc (iglobal/uglobal macro)
-
 
11
; (program must 'include "kglobals.inc"' and say 'IncludeUGlobal'
-
 
12
; somewhere in uninitialized data area).
-
 
13
 
8
 
14
; Configuration: [changed from program which includes this file]
-
 
15
; 1. The constant COLOR_ORDER: must be one of
-
 
16
;    PALETTE - for 8-bit image with palette (sysfunction 65)
9
; Change COLOR_ORDER in your program
17
;    MENUETOS - for MenuetOS and KolibriOS color order (sysfunction 7)
-
 
18
;    OTHER - for standard color order
-
 
19
; 2. Define constant GIF_SUPPORT_INTERLACED if you want to support interlaced
-
 
20
;    GIFs.
-
 
21
; 3. Single image mode vs multiple image mode:
-
 
22
;    if the program defines the variable 'gif_img_count' of type dword
-
 
23
;    somewhere, ReadGIF will enter multiple image mode: gif_img_count
-
 
24
;    will be initialized with image count, output format is GIF_list,
-
 
25
;    the function GetGIFinfo retrieves Nth image info. Otherwise, ReadGIF
-
 
26
;    uses single image mode: exit after end of first image, output is
Line 10... Line 27...
10
; if colors are displayed improperly
27
;    
11
 
28
 
12
if ~ (COLOR_ORDER in )
29
if ~ (COLOR_ORDER in )
13
; This message may not appear under MenuetOS, so watch...
30
; This message may not appear under MenuetOS, so watch...
Line -... Line 31...
-
 
31
  display 'Please define COLOR_ORDER: PALETTE, MENUETOS or OTHER',13,10
14
  display 'Please define COLOR_ORDER: MENUETOS or OTHER',13,10
32
end if
Line 15... Line 33...
15
end if
33
 
16
 
-
 
17
; virtual structure, used internally
34
if defined gif_img_count
18
 
35
; virtual structure, used internally
19
struc GIF_list
36
 
20
{
37
struct GIF_list
21
    .NextImg rd 1
38
    NextImg rd 1
22
    .Left   rw 1
-
 
23
    .Top    rw 1
-
 
24
    .Width  rw 1
-
 
25
    .Height rw 1
-
 
26
}
39
    Left   rw 1
27
 
40
    Top    rw 1
-
 
41
    Width  rw 1
-
 
42
    Height rw 1
-
 
43
    Delay  rd 1
28
struc GIF_info
44
    Displacement rd 1 ; 0 = not specified
29
{
45
                       ; 1 = do not dispose
-
 
46
                       ; 2 = restore to background color
30
    .Left   rw 1
47
                       ; 3 = restore to previous
Line -... Line 48...
-
 
48
if COLOR_ORDER eq PALETTE
31
    .Top    rw 1
49
    Image rd 1
-
 
50
end if
-
 
51
ends
-
 
52
 
-
 
53
struct GIF_info
-
 
54
    Left   rw 1
-
 
55
    Top    rw 1
-
 
56
    Width  rw 1
-
 
57
    Height rw 1
-
 
58
    Delay  rd 1
Line 32... Line 59...
32
    .Width  rw 1
59
    Displacement rd 1
33
    .Height rw 1
60
if COLOR_ORDER eq PALETTE
34
}
61
    Palette rd 1
35
 
62
end if
Line 54... Line 81...
54
    mov   esi,[esi]
81
    mov   esi,[esi]
55
    test  esi,esi
82
    test  esi,esi
56
    jz    .error
83
    jz    .error
57
    loop  .lp
84
    loop  .lp
58
  .eloop:
85
  .eloop:
59
    add   esi,4
86
    lodsd
60
    movsd
87
    movsd
61
    movsd
88
    movsd
-
 
89
    movsd
-
 
90
    movsd
-
 
91
if COLOR_ORDER eq PALETTE
-
 
92
    lodsd
-
 
93
    mov   [edi],esi
-
 
94
else
62
    mov   eax,esi
95
    mov   eax,esi
-
 
96
end if
63
  .error:
97
  .error:
64
    pop   edi ecx esi
98
    pop   edi ecx esi
65
    ret
99
    ret
Line -... Line 100...
-
 
100
 
-
 
101
end if
-
 
102
 
-
 
103
_null fix 0x1000
66
 
104
 
67
; ****************************************
105
; ****************************************
68
;   FUNCTION ReadGIF - unpacks GIF image
106
;   FUNCTION ReadGIF - unpacks GIF image
69
; ****************************************
107
; ****************************************
70
; in:
108
; in:
71
;   esi - pointer to GIF file in memory
109
;   esi - pointer to GIF file in memory
72
;   edi - pointer to output image list
-
 
Line 73... Line 110...
73
;   eax - pointer to work area (MIN 16 KB!)
110
;   edi - pointer to output image list
74
 
111
 
75
; out:
112
; out:
76
;   eax - 0, all OK;
113
;   eax - 0, all OK;
77
;   eax - 1, invalid signature;
114
;   eax - 1, invalid signature;
78
;   eax >=8, unsupported image attributes
-
 
Line 79... Line 115...
79
;
115
;   eax >=8, unsupported image attributes
80
;   ecx - number of images
116
;
81
 
-
 
82
ReadGIF:
117
 
83
    push esi edi
118
ReadGIF:
84
    mov  [.table_ptr],eax
119
    push esi edi
-
 
120
    mov  [.cur_info],edi
85
    mov  [.cur_info],edi
121
    xor  eax,eax
-
 
122
    mov  [.globalColor],eax
-
 
123
if defined gif_img_count
-
 
124
    mov  [gif_img_count],eax
86
    xor  eax,eax
125
    mov  [.anim_delay],eax
87
    mov  [.globalColor],eax
126
    mov  [.anim_disp],eax
88
    mov  [.img_count],eax
127
end if
89
    inc  eax
128
    inc  eax
90
    cmp  dword[esi],'GIF8'
-
 
91
    jne  .er            ; signature
129
    cmp  dword[esi],'GIF8'
92
    mov  ecx,[esi+0xa]
130
    jne  .ex            ; signature
93
    inc  eax
131
    mov  ecx,[esi+0xa]
94
    add  esi,0xd
132
    add  esi,0xd
95
    mov  edi,esi
133
    mov  edi,esi
96
    bt   ecx,7
134
    test cl,cl
97
    jnc  .nextblock
135
    jns  .nextblock
98
    mov  [.globalColor],esi
136
    mov  [.globalColor],esi
99
    call .Gif_skipmap
137
    call .Gif_skipmap
100
  .nextblock:
138
  .nextblock:
-
 
139
    cmp  byte[edi],0x21
101
    cmp  byte[edi],0x21
140
    jne  .noextblock
102
    jne  .noextblock
141
    inc  edi
-
 
142
if defined gif_img_count
-
 
143
    cmp  byte[edi],0xf9 ; Graphic Control Ext
-
 
144
    jne  .no_gc
-
 
145
    movzx eax,word [edi+3]
-
 
146
    mov  [.anim_delay],eax
-
 
147
    mov  al,[edi+2]
103
    inc  edi
148
    shr  al,2
104
    cmp  byte[edi],0xf9 ; Graphic Control Ext
149
    and  eax,7
105
    jne  .no_gc
150
    mov  [.anim_disp],eax
106
    add  edi,7
-
 
107
    jmp  .nextblock
151
    add  edi,7
108
  .no_gc:
152
    jmp  .nextblock
109
    cmp  byte[edi],0xfe ; Comment Ext
153
  .no_gc:
110
    jne  .no_comm
154
end if
111
    inc  edi
155
    inc  edi
112
  .block_skip:
156
  .block_skip:
113
    movzx eax,byte[edi]
157
    movzx eax,byte[edi]
114
    lea  edi,[edi+eax+1]
-
 
115
    cmp  byte[edi],0
158
    lea  edi,[edi+eax+1]
116
    jnz  .block_skip
-
 
117
    inc  edi
-
 
118
    jmp  .nextblock
-
 
119
  .no_comm:
-
 
120
    cmp  byte[edi],0xff ; Application Ext
-
 
121
    jne  .nextblock
159
    test eax,eax
-
 
160
    jnz  .block_skip
122
    add  edi,13
161
    jmp  .nextblock
123
    jmp  .block_skip
162
  .noextblock:
-
 
163
    mov  al,8
124
  .noextblock:
164
    cmp  byte[edi],0x2c    ; image beginning
-
 
165
    jne  .ex
125
    cmp  byte[edi],0x2c    ; image beginning
166
if defined gif_img_count
126
    jne  .er
167
    inc  [gif_img_count]
-
 
168
end if
127
    inc  [.img_count]
169
    inc  edi
-
 
170
    mov  esi,[.cur_info]
128
    inc  edi
171
if defined gif_img_count
-
 
172
    add  esi,4
-
 
173
end if
-
 
174
    xchg esi,edi
-
 
175
if defined GIF_SUPPORT_INTERLACED
-
 
176
    movzx ecx,word[esi+4]
-
 
177
    mov  [.width],ecx
-
 
178
    movzx eax,word[esi+6]
-
 
179
    imul eax,ecx
-
 
180
if ~(COLOR_ORDER eq PALETTE)
-
 
181
    lea  eax,[eax*3]
-
 
182
end if
-
 
183
    mov  [.img_end],eax
-
 
184
    inc  eax
-
 
185
    mov  [.row_end],eax
-
 
186
    and  [.pass],0
-
 
187
    test byte[esi+8],40h
-
 
188
    jz   @f
-
 
189
if ~(COLOR_ORDER eq PALETTE)
-
 
190
    lea  ecx,[ecx*3]
-
 
191
end if
-
 
192
    mov  [.row_end],ecx
129
    mov  esi,[.cur_info]
193
@@:
130
    add  esi,4
194
end if
-
 
195
if defined gif_img_count
-
 
196
    movsd
-
 
197
    movsd
-
 
198
    mov  eax,[.anim_delay]
-
 
199
    stosd
-
 
200
    mov  eax,[.anim_disp]
-
 
201
    stosd
-
 
202
else
-
 
203
    movzx eax,word[esi+4]
-
 
204
    stosd
-
 
205
    movzx eax,word[esi+6]
131
    xchg esi,edi
206
    stosd
132
    movsd
207
    add   esi,8
133
    movsd
208
end if
134
    push edi
209
    push edi
135
    movzx ecx,word[esi]
210
    mov  ecx,[esi]
136
    inc  esi
211
    inc  esi
137
    bt   ecx,7
212
    test cl,cl
138
    jc   .uselocal
213
    js   .uselocal
139
    push [.globalColor]
214
    push [.globalColor]
140
    mov  edi,esi
215
    mov  edi,esi
Line 145... Line 220...
145
  .setPal:
220
  .setPal:
146
    movzx ecx,byte[edi]
221
    movzx ecx,byte[edi]
147
    inc  ecx
222
    inc  ecx
148
    mov  [.codesize],ecx
223
    mov  [.codesize],ecx
149
    dec  ecx
224
    dec  ecx
-
 
225
if ~(COLOR_ORDER eq PALETTE)
150
    pop  [.Palette]
226
    pop  [.Palette]
-
 
227
end if
151
    lea  esi,[edi+1]
228
    lea  esi,[edi+1]
152
    mov  edi,[.table_ptr]
229
    mov  edi,.gif_workarea
153
    xor  eax,eax
230
    xor  eax,eax
154
    cld
-
 
155
    lodsb               ; eax - block_count
231
    lodsb               ; eax - block_count
156
    add  eax,esi
232
    add  eax,esi
157
    mov  [.block_ofs],eax
233
    mov  [.block_ofs],eax
158
    mov  [.bit_count],8
234
    mov  [.bit_count],8
159
    mov  eax,1
235
    mov  eax,1
160
    shl  eax,cl
236
    shl  eax,cl
161
    mov  [.CC],eax
237
    mov  [.CC],eax
-
 
238
    mov  ecx,eax
162
    inc  eax
239
    inc  eax
163
    mov  [.EOI],eax
240
    mov  [.EOI],eax
164
    lea  ecx,[eax-1]
-
 
165
    mov  eax, _null shl 16
241
    mov  eax, _null shl 16
166
  .filltable:
242
  .filltable:
167
    stosd
243
    stosd
168
    inc  eax
244
    inc  eax
169
    loop .filltable
245
    loop .filltable
-
 
246
if COLOR_ORDER eq PALETTE
-
 
247
    pop  eax
-
 
248
    pop  edi
-
 
249
    push edi
-
 
250
    scasd
-
 
251
    push esi
-
 
252
    mov  esi,eax
-
 
253
    mov  ecx,[.CC]
-
 
254
@@:
-
 
255
    lodsd
-
 
256
    dec  esi
-
 
257
    bswap eax
-
 
258
    shr  eax,8
-
 
259
    stosd
-
 
260
    loop @b
-
 
261
    pop  esi
-
 
262
    pop  eax
-
 
263
    mov  [eax],edi
-
 
264
else
170
    pop  edi
265
    pop  edi
-
 
266
end if
-
 
267
if defined GIF_SUPPORT_INTERLACED
171
    mov  [.img_start],edi
268
    mov  [.img_start],edi
-
 
269
    add  [.img_end],edi
-
 
270
    add  [.row_end],edi
-
 
271
end if
172
  .reinit:
272
  .reinit:
173
    mov  edx,[.EOI]
273
    mov  edx,[.EOI]
174
    inc  edx
274
    inc  edx
175
    push [.codesize]
275
    push [.codesize]
176
    pop  [.compsize]
276
    pop  [.compsize]
Line 187... Line 287...
187
    je   .reinit
287
    je   .reinit
188
    cmp  eax,[.EOI]
288
    cmp  eax,[.EOI]
189
    je   .end
289
    je   .end
190
    call .Gif_output
290
    call .Gif_output
191
  .add:
291
  .add:
192
    push eax
-
 
193
    mov  eax,[.table_ptr]
-
 
194
    mov  [eax+edx*4],ebx
292
    mov  dword [.gif_workarea+edx*4],ebx
195
    pop  eax
-
 
196
    cmp  edx,0xFFF
293
    cmp  edx,0xFFF
197
    jae  .cycle
294
    jae  .cycle
198
    inc  edx
295
    inc  edx
199
    bsr  ebx,edx
296
    bsr  ebx,edx
200
    cmp  ebx,[.compsize]
297
    cmp  ebx,[.compsize]
Line 209... Line 306...
209
    push ebx
306
    push ebx
210
    movzx eax,bx
307
    movzx eax,bx
211
    call .Gif_output
308
    call .Gif_output
212
    pop  ebx eax
309
    pop  ebx eax
213
    jmp  .add
310
    jmp  .add
214
  .er:
-
 
215
    pop  edi
-
 
216
    jmp  .ex
-
 
217
  .end:
311
  .end:
-
 
312
if defined GIF_SUPPORT_INTERLACED
-
 
313
    mov  edi,[.img_end]
-
 
314
end if
-
 
315
if defined gif_img_count
218
    mov  eax,[.cur_info]
316
    mov  eax,[.cur_info]
219
    mov  [eax],edi
317
    mov  [eax],edi
220
    mov  [.cur_info],edi
318
    mov  [.cur_info],edi
221
    add  esi,2
319
    add  esi,2
222
    xchg esi,edi
320
    xchg esi,edi
Line 226... Line 324...
226
    inc  edi
324
    inc  edi
227
    jmp  .nxt
325
    jmp  .nxt
228
  .continue:
326
  .continue:
229
    cmp  byte[edi],0x3b
327
    cmp  byte[edi],0x3b
230
    jne  .nextblock
328
    jne  .nextblock
-
 
329
    xchg esi,edi
-
 
330
    and  dword [eax],0
-
 
331
end if
231
    xor  eax,eax
332
    xor  eax,eax
232
    stosd
-
 
233
    mov  ecx,[.img_count]
-
 
234
  .ex:
333
  .ex:
235
    pop  edi esi
334
    pop  edi esi
236
    ret
335
    ret
Line 237... Line 336...
237
 
336
 
Line 282... Line 381...
282
    xor  ecx,ecx
381
    xor  ecx,ecx
283
    ret
382
    ret
Line 284... Line 383...
284
 
383
 
285
.Gif_output:
384
.Gif_output:
286
    push esi eax edx
385
    push esi eax edx
287
    mov  edx,[.table_ptr]
386
    mov  edx,.gif_workarea
288
  .next:
387
  .next:
289
    push word[edx+eax*4]
388
    push word[edx+eax*4]
290
    mov  ax,word[edx+eax*4+2]
389
    mov  ax,word[edx+eax*4+2]
291
    inc  ecx
390
    inc  ecx
Line 294... Line 393...
294
    shl  ebx,16
393
    shl  ebx,16
295
    mov  bx,[esp]
394
    mov  bx,[esp]
296
  .loop2:
395
  .loop2:
297
    pop  ax
396
    pop  ax
Line -... Line 397...
-
 
397
 
-
 
398
    if COLOR_ORDER eq PALETTE
-
 
399
        stosb
298
 
400
    else
299
    lea  esi,[eax+eax*2]
401
        lea  esi,[eax+eax*2]
Line 300... Line 402...
300
    add  esi,[.Palette]
402
        add  esi,[.Palette]
301
 
403
 
302
    if COLOR_ORDER eq MENUETOS
404
    if COLOR_ORDER eq MENUETOS
303
        mov  esi,[esi]
405
        mov  esi,[esi]
304
        bswap esi
406
        bswap esi
305
        shr  esi,8
407
        shr  esi,8
306
        mov  [edi],esi
408
        mov  [edi],esi
307
        add  edi,3
-
 
308
    else
409
        add  edi,3
-
 
410
    else
-
 
411
        movsb
-
 
412
        movsb
-
 
413
        movsb
-
 
414
    end if
-
 
415
    end if
-
 
416
 
-
 
417
if defined GIF_SUPPORT_INTERLACED
-
 
418
    cmp  edi,[.row_end]
-
 
419
    jb   .norowend
-
 
420
    mov  eax,[.width]
-
 
421
if ~(COLOR_ORDER eq PALETTE)
-
 
422
    lea  eax,[eax*3]
-
 
423
end if
-
 
424
    push eax
-
 
425
    sub  edi,eax
-
 
426
    add  eax,eax
-
 
427
    cmp  [.pass],3
-
 
428
    jz   @f
-
 
429
    add  eax,eax
-
 
430
    cmp  [.pass],2
-
 
431
    jz   @f
-
 
432
    add  eax,eax
-
 
433
@@:
-
 
434
    add  edi,eax
-
 
435
    pop  eax
-
 
436
    cmp  edi,[.img_end]
-
 
437
    jb   .nextrow
-
 
438
    mov  edi,[.img_start]
-
 
439
    inc  [.pass]
-
 
440
    add  edi,eax
-
 
441
    cmp  [.pass],3
-
 
442
    jz   @f
-
 
443
    add  edi,eax
-
 
444
    cmp  [.pass],2
-
 
445
    jz   @f
-
 
446
    add  edi,eax
-
 
447
    add  edi,eax
-
 
448
@@:
-
 
449
.nextrow:
-
 
450
    add  eax,edi
-
 
451
    mov  [.row_end],eax
309
        movsw
452
    xor  eax,eax
Line 310... Line 453...
310
        movsb
453
.norowend:
311
    end if
454
end if
312
 
455
 
Line 313... Line 456...
313
    loop .loop2
456
    loop .loop2
-
 
457
    pop  edx eax esi
314
    pop  edx eax esi
458
    ret
315
    ret
459
 
316
 
-
 
317
    .globalColor rd 1
460
uglobal
318
    .img_count rd 1
461
align 4
319
    .cur_info rd 1        ; image table pointer
462
    ReadGIF.globalColor rd 1
320
    .img_start rd 1
463
    ReadGIF.cur_info rd 1        ; image table pointer
321
    .codesize rd 1
464
    ReadGIF.codesize rd 1
-
 
465
    ReadGIF.compsize rd 1
322
    .compsize rd 1
466
    ReadGIF.bit_count rd 1
-
 
467
    ReadGIF.CC rd 1
323
    .bit_count rd 1
468
    ReadGIF.EOI rd 1
-
 
469
if ~(COLOR_ORDER eq PALETTE)
-
 
470
    ReadGIF.Palette rd 1
-
 
471
end if
-
 
472
    ReadGIF.block_ofs rd 1
324
    .CC rd 1
473
if defined GIF_SUPPORT_INTERLACED
-
 
474
    ReadGIF.row_end rd 1
-
 
475
    ReadGIF.img_end rd 1
-
 
476
    ReadGIF.img_start rd 1
-
 
477
    ReadGIF.pass rd 1
-
 
478
    ReadGIF.width rd 1
-
 
479
end if
-
 
480
if defined gif_img_count
-
 
481
    ReadGIF.anim_delay rd 1