Subversion Repositories Kolibri OS

Rev

Rev 1921 | Rev 2392 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
717 mikedld 1
;;================================================================================================;;
999 diamond 2
;;//// libimg.asm //// (c) mike.dld, 2007-2008, (c) diamond, 2009 ////////////////////////////////;;
717 mikedld 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 ;;
999 diamond 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.                                         ;;
717 mikedld 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  ;;
999 diamond 13
;; Lesser General Public License for more details.                                                ;;
717 mikedld 14
;;                                                                                                ;;
999 diamond 15
;; You should have received a copy of the GNU Lesser General Public License along with Libs-Dev.  ;;
16
;; If not, see .                                                    ;;
717 mikedld 17
;;                                                                                                ;;
18
;;================================================================================================;;
19
 
20
 
21
format MS COFF
22
 
23
public @EXPORT as 'EXPORTS'
24
 
25
include '../../../../struct.inc'
26
include '../../../../proc32.inc'
27
include '../../../../macros.inc'
999 diamond 28
purge section,mov,add,sub
717 mikedld 29
 
30
include 'libimg.inc'
31
 
32
section '.flat' code readable align 16
33
 
34
include 'bmp/bmp.asm'
35
include 'gif/gif.asm'
999 diamond 36
include 'jpeg/jpeg.asm'
1014 diamond 37
include 'png/png.asm'
1079 diamond 38
include 'tga/tga.asm'
39
include 'z80/z80.asm'
1102 diamond 40
include 'ico_cur/ico_cur.asm'
1569 dunkaist 41
include 'pcx/pcx.asm'
1921 dunkaist 42
include 'xcf/xcf.asm'
2388 dunkaist 43
include 'tiff/tiff.asm'
44
include 'pnm/pnm.asm'
717 mikedld 45
 
46
;;================================================================================================;;
47
proc lib_init ;///////////////////////////////////////////////////////////////////////////////////;;
48
;;------------------------------------------------------------------------------------------------;;
49
;? Library entry point (called after library load)                                                ;;
50
;;------------------------------------------------------------------------------------------------;;
51
;> eax = pointer to memory allocation routine                                                     ;;
52
;> ebx = pointer to memory freeing routine                                                        ;;
53
;> ecx = pointer to memory reallocation routine                                                   ;;
54
;> edx = pointer to library loading routine                                                       ;;
55
;;------------------------------------------------------------------------------------------------;;
56
;< eax = 1 (fail) / 0 (ok) (library initialization result)                                        ;;
57
;;================================================================================================;;
1569 dunkaist 58
    mov [mem.alloc], eax
59
    mov [mem.free], ebx
60
    mov [mem.realloc], ecx
61
    mov [dll.load], edx
717 mikedld 62
 
1569 dunkaist 63
    call    img.initialize.jpeg
999 diamond 64
 
1569 dunkaist 65
    xor eax, eax
66
    cpuid
67
    cmp ecx, 'ntel'
68
    jnz @f
69
    mov dword [img._.do_rgb.handlers + (Image.bpp15-1)*4], img._.do_rgb.bpp15.intel
70
    mov dword [img._.do_rgb.handlers + (Image.bpp16-1)*4], img._.do_rgb.bpp16.intel
1079 diamond 71
  @@:
72
 
1569 dunkaist 73
  .ok:  xor eax,eax
74
    ret
717 mikedld 75
endp
76
 
77
;;================================================================================================;;
78
proc img.is_img _data, _length ;//////////////////////////////////////////////////////////////////;;
79
;;------------------------------------------------------------------------------------------------;;
80
;? --- TBD ---                                                                                    ;;
81
;;------------------------------------------------------------------------------------------------;;
82
;> --- TBD ---                                                                                    ;;
83
;;------------------------------------------------------------------------------------------------;;
84
;< --- TBD ---                                                                                    ;;
85
;;================================================================================================;;
1569 dunkaist 86
    push    ebx
87
    mov ebx, img._.formats_table
717 mikedld 88
    @@: stdcall [ebx + FormatsTableEntry.Is], [_data], [_length]
1569 dunkaist 89
    or  eax, eax
90
    jnz @f
91
    add ebx, sizeof.FormatsTableEntry
92
    cmp dword[ebx], 0
93
    jnz @b
94
    xor eax, eax
95
    @@: pop ebx
96
    ret
717 mikedld 97
endp
98
 
99
;;================================================================================================;;
100
proc img.info _data, _length ;////////////////////////////////////////////////////////////////////;;
101
;;------------------------------------------------------------------------------------------------;;
102
;? --- TBD ---                                                                                    ;;
103
;;------------------------------------------------------------------------------------------------;;
104
;> --- TBD ---                                                                                    ;;
105
;;------------------------------------------------------------------------------------------------;;
106
;< --- TBD ---                                                                                    ;;
107
;;================================================================================================;;
1569 dunkaist 108
    xor eax, eax
109
    ret
717 mikedld 110
endp
111
 
112
;;================================================================================================;;
113
proc img.from_file _filename ;////////////////////////////////////////////////////////////////////;;
114
;;------------------------------------------------------------------------------------------------;;
115
;? --- TBD ---                                                                                    ;;
116
;;------------------------------------------------------------------------------------------------;;
117
;> --- TBD ---                                                                                    ;;
118
;;------------------------------------------------------------------------------------------------;;
119
;< eax = 0 / pointer to image                                                                     ;;
120
;;================================================================================================;;
1569 dunkaist 121
    xor eax, eax
122
    ret
717 mikedld 123
endp
124
 
125
;;================================================================================================;;
126
proc img.to_file _img, _filename ;////////////////////////////////////////////////////////////////;;
127
;;------------------------------------------------------------------------------------------------;;
128
;? --- TBD ---                                                                                    ;;
129
;;------------------------------------------------------------------------------------------------;;
130
;> --- TBD ---                                                                                    ;;
131
;;------------------------------------------------------------------------------------------------;;
132
;< eax = false / true                                                                             ;;
133
;;================================================================================================;;
1569 dunkaist 134
    xor eax, eax
135
    ret
717 mikedld 136
endp
137
 
138
;;================================================================================================;;
139
proc img.from_rgb _rgb_data ;/////////////////////////////////////////////////////////////////////;;
140
;;------------------------------------------------------------------------------------------------;;
141
;? --- TBD ---                                                                                    ;;
142
;;------------------------------------------------------------------------------------------------;;
143
;> --- TBD ---                                                                                    ;;
144
;;------------------------------------------------------------------------------------------------;;
145
;< eax = 0 / pointer to image                                                                     ;;
146
;;================================================================================================;;
1569 dunkaist 147
    xor eax, eax
148
    ret
717 mikedld 149
endp
150
 
151
;;================================================================================================;;
1001 diamond 152
proc img.to_rgb2 _img, _out ;/////////////////////////////////////////////////////////////////////;;
153
;;------------------------------------------------------------------------------------------------;;
154
;? --- TBD ---                                                                                    ;;
155
;;------------------------------------------------------------------------------------------------;;
156
;> --- TBD ---                                                                                    ;;
157
;;------------------------------------------------------------------------------------------------;;
158
;< --- TBD ---                                                                                    ;;
159
;;================================================================================================;;
1569 dunkaist 160
    push    esi edi
161
    mov esi, [_img]
162
    stdcall img._.validate, esi
163
    or  eax, eax
164
    jnz .ret
165
    mov edi, [_out]
166
    call    img._.do_rgb
1001 diamond 167
.ret:
1569 dunkaist 168
    pop edi esi
169
    ret
1001 diamond 170
endp
171
 
172
;;================================================================================================;;
717 mikedld 173
proc img.to_rgb _img ;////////////////////////////////////////////////////////////////////////////;;
174
;;------------------------------------------------------------------------------------------------;;
175
;? --- TBD ---                                                                                    ;;
176
;;------------------------------------------------------------------------------------------------;;
177
;> --- TBD ---                                                                                    ;;
178
;;------------------------------------------------------------------------------------------------;;
179
;< eax = 0 / pointer to rgb_data (array of [rgb] triplets)                                        ;;
180
;;================================================================================================;;
1569 dunkaist 181
    push    esi edi
182
    mov esi, [_img]
183
    stdcall img._.validate, esi
184
    or  eax, eax
185
    jnz .error
717 mikedld 186
 
1569 dunkaist 187
    mov esi, [_img]
188
    mov ecx, [esi + Image.Width]
189
    imul    ecx, [esi + Image.Height]
190
    lea eax, [ecx * 3 + 4 * 3]
191
    invoke  mem.alloc, eax
192
    or  eax, eax
193
    jz  .error
717 mikedld 194
 
1569 dunkaist 195
    mov edi, eax
196
    push    eax
197
    mov eax, [esi + Image.Width]
198
    stosd
199
    mov eax, [esi + Image.Height]
200
    stosd
201
    call    img._.do_rgb
202
    pop eax
203
    pop edi esi
204
    ret
1001 diamond 205
 
206
  .error:
1569 dunkaist 207
    xor eax, eax
208
    pop edi esi
209
    ret
1001 diamond 210
endp
211
 
212
;;================================================================================================;;
213
proc img._.do_rgb ;///////////////////////////////////////////////////////////////////////////////;;
214
;;------------------------------------------------------------------------------------------------;;
215
;? --- TBD ---                                                                                    ;;
216
;;------------------------------------------------------------------------------------------------;;
217
;> --- TBD ---                                                                                    ;;
218
;;------------------------------------------------------------------------------------------------;;
219
;< --- TBD ---                                                                                    ;;
220
;;================================================================================================;;
1569 dunkaist 221
    mov ecx, [esi + Image.Width]
222
    imul    ecx, [esi + Image.Height]
223
    mov eax, [esi + Image.Type]
224
    jmp dword [.handlers + (eax-1)*4]
1079 diamond 225
 
226
align 16
227
.bpp8:
228
; 8 BPP -> 24 BPP
1569 dunkaist 229
    push    ebx
230
    mov ebx, [esi + Image.Palette]
231
    mov esi, [esi + Image.Data]
232
    sub ecx, 1
233
    jz  .bpp8.last
1079 diamond 234
@@:
1569 dunkaist 235
    movzx   eax, byte [esi]
236
    add esi, 1
237
    mov eax, [ebx + eax*4]
238
    mov [edi], eax
239
    add edi, 3
240
    sub ecx, 1
241
    jnz @b
1152 diamond 242
.bpp8.last:
1569 dunkaist 243
    movzx   eax, byte [esi]
244
    mov eax, [ebx + eax*4]
245
    mov [edi], ax
246
    shr eax, 16
247
    mov [edi+2], al
248
    pop ebx
249
    ret
717 mikedld 250
 
1079 diamond 251
; 15 BPP -> 24 BPP
252
.bpp15.intel:
1569 dunkaist 253
    push    ebx ebp
254
    sub ecx, 4
255
    jb  .bpp15.tail
1079 diamond 256
align 16
257
.bpp15.intel.loop:
258
repeat 2
1569 dunkaist 259
    mov ebx, [esi]
260
    mov al, [esi]
261
    mov ah, [esi+1]
262
    add esi, 4
263
    and al, 0x1F
264
    and ah, 0x1F shl 2
265
    mov ebp, ebx
266
    mov dl, al
267
    mov dh, ah
268
    shr al, 2
269
    shr ah, 4
270
    shl dl, 3
271
    shl dh, 1
272
    and ebp, 0x1F shl 5
273
    add al, dl
274
    add ah, dh
275
    shr ebp, 2
276
    mov [edi], al
277
    mov [edi+2], ah
278
    mov eax, ebx
279
    mov ebx, ebp
280
    shr eax, 16
281
    shr ebx, 5
282
    add ebx, ebp
283
    mov ebp, eax
284
    mov [edi+1], bl
285
    and eax, (0x1F) or (0x1F shl 10)
286
    and ebp, 0x1F shl 5
287
    lea edx, [eax+eax]
288
    shr al, 2
289
    mov ebx, ebp
290
    shr ah, 4
291
    shl dl, 2
292
    shr ebx, 2
293
    shr ebp, 7
294
    add al, dl
295
    add ah, dh
296
    mov [edi+3], al
297
    add ebx, ebp
298
    mov [edi+5], ah
299
    mov [edi+4], bl
300
    add edi, 6
1079 diamond 301
end repeat
1569 dunkaist 302
    sub ecx, 4
303
    jnb .bpp15.intel.loop
1079 diamond 304
.bpp15.tail:
1569 dunkaist 305
    add ecx, 4
306
    jz  .bpp15.done
1079 diamond 307
@@:
1569 dunkaist 308
    movzx   eax, word [esi]
309
    mov ebx, eax
310
    add esi, 2
311
    and eax, (0x1F) or (0x1F shl 10)
312
    and ebx, 0x1F shl 5
313
    lea edx, [eax+eax]
314
    shr al, 2
315
    mov ebp, ebx
316
    shr ebx, 2
317
    shr ah, 4
318
    shl dl, 2
319
    shr ebp, 7
320
    add eax, edx
321
    add ebx, ebp
322
    mov [edi], al
323
    mov [edi+1], bl
324
    mov [edi+2], ah
325
    add edi, 3
326
    sub ecx, 1
327
    jnz @b
1079 diamond 328
.bpp15.done:
1569 dunkaist 329
    pop ebp ebx
330
    ret
1079 diamond 331
 
332
.bpp15.amd:
1569 dunkaist 333
    push    ebx ebp
334
    sub ecx, 4
335
    jb  .bpp15.tail
1079 diamond 336
align 16
337
.bpp15.amd.loop:
338
repeat 4
339
if (% mod 2) = 1
1569 dunkaist 340
    mov eax, dword [esi]
341
    mov ebx, dword [esi]
1079 diamond 342
else
1569 dunkaist 343
    movzx   eax, word [esi]
344
    mov ebx, eax
1079 diamond 345
end if
1569 dunkaist 346
    add esi, 2
347
    and eax, (0x1F) or (0x1F shl 10)
348
    and ebx, 0x1F shl 5
349
    lea edx, [eax+eax]
350
    shr al, 2
351
    mov ebp, ebx
352
    shr ebx, 2
353
    shr ah, 4
354
    shl dl, 2
355
    shr ebp, 7
356
    add eax, edx
357
    add ebx, ebp
358
    mov [edi], al
359
    mov [edi+1], bl
360
    mov [edi+2], ah
361
    add edi, 3
1079 diamond 362
end repeat
1569 dunkaist 363
    sub ecx, 4
364
    jnb .bpp15.amd.loop
365
    jmp .bpp15.tail
1079 diamond 366
 
367
; 16 BPP -> 24 BPP
368
.bpp16.intel:
1569 dunkaist 369
    push    ebx ebp
370
    sub ecx, 4
371
    jb  .bpp16.tail
1079 diamond 372
align 16
373
.bpp16.intel.loop:
374
repeat 2
1569 dunkaist 375
    mov ebx, [esi]
376
    mov al, [esi]
377
    mov ah, [esi+1]
378
    add esi, 4
379
    and al, 0x1F
380
    and ah, 0x1F shl 3
381
    mov ebp, ebx
382
    mov dl, al
383
    mov dh, ah
384
    shr al, 2
385
    shr ah, 5
386
    shl dl, 3
387
    and ebp, 0x3F shl 5
388
    add al, dl
389
    add ah, dh
390
    shr ebp, 3
391
    mov [edi], al
392
    mov [edi+2], ah
393
    mov eax, ebx
394
    mov ebx, ebp
395
    shr eax, 16
396
    shr ebx, 6
397
    add ebx, ebp
398
    mov ebp, eax
399
    mov [edi+1], bl
400
    and eax, (0x1F) or (0x1F shl 11)
401
    and ebp, 0x3F shl 5
402
    mov edx, eax
403
    shr al, 2
404
    mov ebx, ebp
405
    shr ah, 5
406
    shl dl, 3
407
    shr ebx, 3
408
    shr ebp, 9
409
    add al, dl
410
    add ah, dh
411
    mov [edi+3], al
412
    add ebx, ebp
413
    mov [edi+5], ah
414
    mov [edi+4], bl
415
    add edi, 6
1079 diamond 416
end repeat
1569 dunkaist 417
    sub ecx, 4
418
    jnb .bpp16.intel.loop
1079 diamond 419
.bpp16.tail:
1569 dunkaist 420
    add ecx, 4
421
    jz  .bpp16.done
1079 diamond 422
@@:
1569 dunkaist 423
    movzx   eax, word [esi]
424
    mov ebx, eax
425
    add esi, 2
426
    and eax, (0x1F) or (0x1F shl 11)
427
    and ebx, 0x3F shl 5
428
    mov edx, eax
429
    shr al, 2
430
    mov ebp, ebx
431
    shr ebx, 3
432
    shr ah, 5
433
    shl dl, 3
434
    shr ebp, 9
435
    add eax, edx
436
    add ebx, ebp
437
    mov [edi], al
438
    mov [edi+1], bl
439
    mov [edi+2], ah
440
    add edi, 3
441
    sub ecx, 1
442
    jnz @b
1079 diamond 443
.bpp16.done:
1569 dunkaist 444
    pop ebp ebx
445
    ret
717 mikedld 446
 
1079 diamond 447
.bpp16.amd:
1569 dunkaist 448
    push    ebx ebp
449
    sub ecx, 4
450
    jb  .bpp16.tail
1079 diamond 451
align 16
452
.bpp16.amd.loop:
453
repeat 4
454
if (% mod 2) = 1
1569 dunkaist 455
    mov eax, dword [esi]
456
    mov ebx, dword [esi]
1079 diamond 457
else
1569 dunkaist 458
    movzx   eax, word [esi]
459
    mov ebx, eax
1079 diamond 460
end if
1569 dunkaist 461
    add esi, 2
462
    and eax, (0x1F) or (0x1F shl 11)
463
    and ebx, 0x3F shl 5
464
    mov edx, eax
465
    shr al, 2
466
    mov ebp, ebx
467
    shr ebx, 3
468
    shr ah, 5
469
    shl dl, 3
470
    shr ebp, 9
471
    add eax, edx
472
    add ebx, ebp
473
    mov [edi], al
474
    mov [edi+1], bl
475
    mov [edi+2], ah
476
    add edi, 3
1079 diamond 477
end repeat
1569 dunkaist 478
    sub ecx, 4
479
    jnb .bpp16.amd.loop
480
    jmp .bpp16.tail
1079 diamond 481
 
482
align 16
999 diamond 483
.bpp24:
484
; 24 BPP -> 24 BPP
1569 dunkaist 485
    lea ecx, [ecx*3 + 3]
486
    mov esi, [esi + Image.Data]
487
    shr ecx, 2
488
    rep movsd
489
    ret
999 diamond 490
 
1079 diamond 491
align 16
492
.bpp32:
493
; 32 BPP -> 24 BPP
1569 dunkaist 494
    mov esi, [esi + Image.Data]
1079 diamond 495
 
496
    @@:
1569 dunkaist 497
    mov eax, [esi]
498
    mov [edi], ax
499
    shr eax, 16
500
    mov [edi+2], al
501
    add esi, 4
502
    add edi, 3
503
    sub ecx, 1
504
    jnz @b
1079 diamond 505
 
506
    @@:
1569 dunkaist 507
    ret
1079 diamond 508
 
1593 dunkaist 509
align 16
510
.bpp1:
511
    push ebx edx
512
    mov ebx, esi
513
    mov esi, [ebx + Image.Data]
514
    mov ecx, [ebx + Image.Height]
515
  .bpp1.pre:
516
    mov edx, [ebx + Image.Width]
517
    mov eax, 7
518
  .bpp1.begin:
519
    push ecx esi
520
    xor cx, cx
521
    bt [esi], eax
522
    setc cl
523
    mov esi, [ebx + Image.Palette]
524
     jcxz @f
525
    add esi, 4
526
  @@:
527
    mov ecx, 3
528
    cld
529
    rep movsb
530
    pop esi ecx
531
    dec edx
532
     jz .bpp1.end_line
533
    dec eax
534
     jns .bpp1.begin
535
    mov eax, 7
536
    inc esi
537
     jmp .bpp1.begin
538
 
539
  .bpp1.end_line:
540
    dec ecx
541
     jz .bpp1.quit
542
    inc esi
543
     jmp .bpp1.pre
544
 
545
  .bpp1.quit:
546
    pop edx ebx
547
    ret
548
 
717 mikedld 549
endp
550
 
551
;;================================================================================================;;
1102 diamond 552
proc img.decode _data, _length, _options ;////////////////////////////////////////////////////////;;
717 mikedld 553
;;------------------------------------------------------------------------------------------------;;
554
;? --- TBD ---                                                                                    ;;
555
;;------------------------------------------------------------------------------------------------;;
556
;> --- TBD ---                                                                                    ;;
557
;;------------------------------------------------------------------------------------------------;;
558
;< eax = 0 / pointer to image                                                                     ;;
559
;;================================================================================================;;
1569 dunkaist 560
    push    ebx
561
    mov ebx, img._.formats_table
717 mikedld 562
    @@: stdcall [ebx + FormatsTableEntry.Is], [_data], [_length]
1569 dunkaist 563
    or  eax, eax
564
    jnz @f
565
    add ebx, sizeof.FormatsTableEntry
566
    cmp dword[ebx], eax ;0
567
    jnz @b
568
    pop ebx
569
    ret
570
    @@: mov eax, [ebx + FormatsTableEntry.Decode]
571
    pop ebx
572
    leave
573
    jmp eax
717 mikedld 574
endp
575
 
576
;;================================================================================================;;
1102 diamond 577
proc img.encode _img, _p_length, _options ;///////////////////////////////////////////////////////;;
717 mikedld 578
;;------------------------------------------------------------------------------------------------;;
579
;? --- TBD ---                                                                                    ;;
580
;;------------------------------------------------------------------------------------------------;;
581
;> --- TBD ---                                                                                    ;;
582
;;------------------------------------------------------------------------------------------------;;
583
;< eax = 0 / pointer to encoded data                                                              ;;
584
;< [_p_length] = data length                                                                      ;;
585
;;================================================================================================;;
1569 dunkaist 586
    xor eax, eax
587
    ret
717 mikedld 588
endp
589
 
590
;;================================================================================================;;
999 diamond 591
proc img.create _width, _height, _type ;//////////////////////////////////////////////////////////;;
717 mikedld 592
;;------------------------------------------------------------------------------------------------;;
593
;? --- TBD ---                                                                                    ;;
594
;;------------------------------------------------------------------------------------------------;;
595
;> --- TBD ---                                                                                    ;;
596
;;------------------------------------------------------------------------------------------------;;
597
;< eax = 0 / pointer to image                                                                     ;;
598
;;================================================================================================;;
1569 dunkaist 599
    push    ecx
717 mikedld 600
 
1569 dunkaist 601
    stdcall img._.new
602
    or  eax, eax
603
    jz  .error
717 mikedld 604
 
1569 dunkaist 605
    mov ecx, [_type]
606
    mov [eax + Image.Type], ecx
999 diamond 607
 
1569 dunkaist 608
    push    eax
717 mikedld 609
 
1569 dunkaist 610
    stdcall img._.resize_data, eax, [_width], [_height]
611
    or  eax, eax
612
    jz  .error.2
717 mikedld 613
 
1569 dunkaist 614
    pop eax
615
    jmp .ret
717 mikedld 616
 
617
  .error.2:
618
;       pop     eax
1569 dunkaist 619
    stdcall img._.delete; eax
620
    xor eax, eax
717 mikedld 621
 
622
  .error:
999 diamond 623
  .ret:
1569 dunkaist 624
    pop ecx
625
    ret
717 mikedld 626
endp
627
 
628
;;================================================================================================;;
1079 diamond 629
proc img.destroy.layer _img ;/////////////////////////////////////////////////////////////////////;;
630
;;------------------------------------------------------------------------------------------------;;
631
;? --- TBD ---                                                                                    ;;
632
;;------------------------------------------------------------------------------------------------;;
633
;> --- TBD ---                                                                                    ;;
634
;;------------------------------------------------------------------------------------------------;;
635
;< eax = false / true                                                                             ;;
636
;;================================================================================================;;
1569 dunkaist 637
    mov eax, [_img]
638
    mov edx, [eax + Image.Previous]
639
    test    edx, edx
640
    jz  @f
641
    push    [eax + Image.Next]
642
    pop [edx + Image.Next]
1079 diamond 643
@@:
1569 dunkaist 644
    mov edx, [eax + Image.Next]
645
    test    edx, edx
646
    jz  @f
647
    push    [eax + Image.Previous]
648
    pop [edx + Image.Previous]
1079 diamond 649
@@:
1569 dunkaist 650
    stdcall img._.delete, eax
651
    ret
1079 diamond 652
endp
653
 
654
;;================================================================================================;;
717 mikedld 655
proc img.destroy _img ;///////////////////////////////////////////////////////////////////////////;;
656
;;------------------------------------------------------------------------------------------------;;
657
;? --- TBD ---                                                                                    ;;
658
;;------------------------------------------------------------------------------------------------;;
659
;> --- TBD ---                                                                                    ;;
660
;;------------------------------------------------------------------------------------------------;;
661
;< eax = false / true                                                                             ;;
662
;;================================================================================================;;
1569 dunkaist 663
    push    1
664
    mov eax, [_img]
665
    mov eax, [eax + Image.Previous]
1079 diamond 666
.destroy_prev_loop:
1569 dunkaist 667
    test    eax, eax
668
    jz  .destroy_prev_done
669
    pushd   [eax + Image.Previous]
670
    stdcall img._.delete, eax
671
    test    eax, eax
672
    jnz @f
673
    mov byte [esp+4], 0
1079 diamond 674
@@:
1569 dunkaist 675
    pop eax
676
    jmp .destroy_prev_loop
1079 diamond 677
.destroy_prev_done:
1569 dunkaist 678
    mov eax, [_img]
1079 diamond 679
.destroy_next_loop:
1569 dunkaist 680
    pushd   [eax + Image.Next]
681
    stdcall img._.delete, eax
682
    test    eax, eax
683
    jnz @f
684
    mov byte [esp+4], 0
1079 diamond 685
@@:
1569 dunkaist 686
    pop eax
687
    test    eax, eax
688
    jnz .destroy_next_loop
689
    pop eax
690
    ret
717 mikedld 691
endp
692
 
693
;;================================================================================================;;
783 mikedld 694
proc img.count _img ;/////////////////////////////////////////////////////////////////////////////;;
717 mikedld 695
;;------------------------------------------------------------------------------------------------;;
783 mikedld 696
;? Get number of images in the list (e.g. in animated GIF file)                                   ;;
717 mikedld 697
;;------------------------------------------------------------------------------------------------;;
783 mikedld 698
;> _img = pointer to image                                                                        ;;
717 mikedld 699
;;------------------------------------------------------------------------------------------------;;
783 mikedld 700
;< eax = -1 (fail) / >0 (ok)                                                                      ;;
717 mikedld 701
;;================================================================================================;;
1569 dunkaist 702
    push    ecx edx
703
    mov edx, [_img]
704
    stdcall img._.validate, edx
705
    or  eax, eax
706
    jnz .error
717 mikedld 707
 
1569 dunkaist 708
    @@: mov eax, [edx + Image.Previous]
709
    or  eax, eax
710
    jz  @f
711
    mov edx, eax
712
    jmp @b
717 mikedld 713
 
1569 dunkaist 714
    @@: xor ecx, ecx
715
    @@: inc ecx
716
    mov eax, [edx + Image.Next]
717
    or  eax, eax
718
    jz  .exit
719
    mov edx, eax
720
    jmp @b
783 mikedld 721
 
722
  .exit:
1569 dunkaist 723
    mov eax, ecx
724
    pop edx ecx
725
    ret
783 mikedld 726
 
717 mikedld 727
  .error:
1569 dunkaist 728
    or  eax, -1
729
    pop edx ecx
730
    ret
717 mikedld 731
endp
732
 
783 mikedld 733
;;//// image processing //////////////////////////////////////////////////////////////////////////;;
734
 
717 mikedld 735
;;================================================================================================;;
736
proc img.lock_bits _img, _start_line, _end_line ;/////////////////////////////////////////////////;;
737
;;------------------------------------------------------------------------------------------------;;
738
;? --- TBD ---                                                                                    ;;
739
;;------------------------------------------------------------------------------------------------;;
740
;> --- TBD ---                                                                                    ;;
741
;;------------------------------------------------------------------------------------------------;;
742
;< eax = 0 / pointer to bits                                                                      ;;
743
;;================================================================================================;;
1569 dunkaist 744
    xor eax, eax
745
    ret
717 mikedld 746
endp
747
 
748
;;================================================================================================;;
749
proc img.unlock_bits _img, _lock ;////////////////////////////////////////////////////////////////;;
750
;;------------------------------------------------------------------------------------------------;;
751
;? --- TBD ---                                                                                    ;;
752
;;------------------------------------------------------------------------------------------------;;
753
;> --- TBD ---                                                                                    ;;
754
;;------------------------------------------------------------------------------------------------;;
755
;< eax = false / true                                                                             ;;
756
;;================================================================================================;;
1569 dunkaist 757
    xor eax, eax
758
    ret
717 mikedld 759
endp
760
 
761
;;================================================================================================;;
1079 diamond 762
proc img.flip.layer _img, _flip_kind ;////////////////////////////////////////////////////////////;;
717 mikedld 763
;;------------------------------------------------------------------------------------------------;;
1079 diamond 764
;? Flip image layer                                                                               ;;
717 mikedld 765
;;------------------------------------------------------------------------------------------------;;
783 mikedld 766
;> _img = pointer to image                                                                        ;;
767
;> _flip_kind = one of FLIP_* constants                                                           ;;
717 mikedld 768
;;------------------------------------------------------------------------------------------------;;
783 mikedld 769
;< eax = false / true                                                                             ;;
717 mikedld 770
;;================================================================================================;;
771
locals
772
  scanline_len dd ?
773
endl
774
 
1569 dunkaist 775
    push    ebx esi edi
776
    mov ebx, [_img]
777
    stdcall img._.validate, ebx
778
    or  eax, eax
779
    jnz .error
717 mikedld 780
 
1569 dunkaist 781
    mov ecx, [ebx + Image.Height]
782
    mov eax, [ebx + Image.Width]
783
    call    img._.get_scanline_len
784
    mov [scanline_len], eax
717 mikedld 785
 
1569 dunkaist 786
    test    [_flip_kind], FLIP_VERTICAL
787
    jz  .dont_flip_vert
717 mikedld 788
 
1569 dunkaist 789
    imul    eax, ecx
790
    sub eax, [scanline_len]
791
    shr ecx, 1
792
    mov esi, [ebx + Image.Data]
793
    lea edi, [esi + eax]
794
 
717 mikedld 795
  .next_line_vert:
1569 dunkaist 796
    push    ecx
717 mikedld 797
 
1569 dunkaist 798
    mov ecx, [scanline_len]
799
    push    ecx
800
    shr ecx, 2
801
    @@: mov eax, [esi]
802
    xchg    eax, [edi]
803
    mov [esi], eax
804
    add esi, 4
805
    add edi, 4
806
    sub ecx, 1
807
    jnz @b
808
    pop ecx
809
    and ecx, 3
810
    jz  .cont_line_vert
999 diamond 811
    @@:
1569 dunkaist 812
    mov al, [esi]
813
    xchg    al, [edi]
814
    mov [esi], al
815
    add esi, 1
816
    add edi, 1
817
    dec ecx
818
    jnz @b
999 diamond 819
    .cont_line_vert:
717 mikedld 820
 
1569 dunkaist 821
    pop ecx
822
    mov eax, [scanline_len]
823
    shl eax, 1
824
    sub edi, eax
825
    dec ecx
826
    jnz .next_line_vert
717 mikedld 827
 
828
  .dont_flip_vert:
829
 
1569 dunkaist 830
    test    [_flip_kind], FLIP_HORIZONTAL
831
    jz  .exit
717 mikedld 832
 
1569 dunkaist 833
    mov ecx, [ebx + Image.Height]
834
    mov eax, [ebx + Image.Type]
835
    mov esi, [ebx + Image.Data]
836
    mov edi, [scanline_len]
837
    add edi, esi
838
    jmp dword [.handlers_horz + (eax-1)*4]
717 mikedld 839
 
1079 diamond 840
.bpp32_horz:
1569 dunkaist 841
    sub edi, 4
999 diamond 842
 
783 mikedld 843
  .next_line_horz:
1569 dunkaist 844
    push    ecx esi edi
783 mikedld 845
 
1569 dunkaist 846
    mov ecx, [scanline_len]
847
    shr ecx, 3
848
    @@: mov eax, [esi]
849
    xchg    eax, [edi]
850
    mov [esi], eax
851
    add esi, 4
852
    add edi, -4
853
    sub ecx, 1
854
    jnz @b
783 mikedld 855
 
1569 dunkaist 856
    pop edi esi ecx
857
    add esi, [scanline_len]
858
    add edi, [scanline_len]
859
    dec ecx
860
    jnz .next_line_horz
861
    jmp .exit
783 mikedld 862
 
1079 diamond 863
.bpp1x_horz:
1569 dunkaist 864
    sub edi, 2
1079 diamond 865
  .next_line_horz1x:
1569 dunkaist 866
    push    ecx esi edi
1079 diamond 867
 
1569 dunkaist 868
    mov ecx, [ebx + Image.Width]
869
    @@: mov ax, [esi]
870
    mov dx, [edi]
871
    mov [edi], ax
872
    mov [esi], dx
873
    add esi, 2
874
    sub edi, 2
875
    sub ecx, 2
876
    ja  @b
1079 diamond 877
 
1569 dunkaist 878
    pop edi esi ecx
879
    add esi, [scanline_len]
880
    add edi, [scanline_len]
881
    dec ecx
882
    jnz .next_line_horz1x
883
    jmp .exit
1079 diamond 884
 
885
.bpp8_horz:
1569 dunkaist 886
    dec edi
999 diamond 887
  .next_line_horz8:
1569 dunkaist 888
    push    ecx esi edi
999 diamond 889
 
1569 dunkaist 890
    mov ecx, [scanline_len]
891
    shr ecx, 1
892
    @@: mov al, [esi]
893
    mov dl, [edi]
894
    mov [edi], al
895
    mov [esi], dl
896
    add esi, 1
897
    sub edi, 1
898
    sub ecx, 1
899
    jnz @b
999 diamond 900
 
1569 dunkaist 901
    pop edi esi ecx
902
    add esi, [scanline_len]
903
    add edi, [scanline_len]
904
    dec ecx
905
    jnz .next_line_horz8
906
    jmp .exit
999 diamond 907
 
1079 diamond 908
.bpp24_horz:
1569 dunkaist 909
    sub edi, 3
1079 diamond 910
  .next_line_horz24:
1569 dunkaist 911
    push    ecx esi edi
999 diamond 912
 
1569 dunkaist 913
    mov ecx, [ebx + Image.Width]
999 diamond 914
    @@:
1569 dunkaist 915
    mov al, [esi]
916
    mov dl, [edi]
917
    mov [edi], al
918
    mov [esi], dl
919
    mov al, [esi+1]
920
    mov dl, [edi+1]
921
    mov [edi+1], al
922
    mov [esi+1], dl
923
    mov al, [esi+2]
924
    mov dl, [edi+2]
925
    mov [edi+2], al
926
    mov [esi+2], dl
927
    add esi, 3
928
    sub edi, 3
929
    sub ecx, 2
930
    ja  @b
999 diamond 931
 
1569 dunkaist 932
    pop edi esi ecx
933
    add esi, [scanline_len]
934
    add edi, [scanline_len]
935
    dec ecx
936
    jnz .next_line_horz24
1593 dunkaist 937
    jmp .exit
999 diamond 938
 
1593 dunkaist 939
.bpp1_horz:
940
    push eax edx
941
    mov edi, [scanline_len]
942
    mov edx, [ebx+Image.Width]
943
    and edx,  0x07
944
    neg dl
945
    add dl, 8
946
    and dl, 0x07                                        ; clear if cl=8
947
.bpp1_horz.begin:
948
    push ebx ecx edx esi
949
    mov eax, 7
950
    add edi, esi
951
    sub edi, 1
952
    mov ebx, [ebx+Image.Width]
953
    shr ebx, 1
954
.bpp1_horz.flip_line:
955
    xor ecx, ecx
956
    bt  [esi], eax
957
    setc cl
958
    bt  [edi], edx
959
     jc .bpp1_horz.one
960
  .bpp1_horz.zero:
961
    btr [esi], eax
962
     jmp @f
963
  .bpp1_horz.one:
964
    bts [esi], eax
965
  @@:
966
     jecxz .bpp1_horz.reset
967
  .bpp1_horz.set:
968
    bts [edi], edx
969
     jmp @f
970
  .bpp1_horz.reset:
971
    btr [edi], edx
972
  @@:
973
    inc edx
974
    and edx, 0x07
975
     jnz @f
976
    dec edi
977
  @@:
978
    dec eax
979
     jns @f
980
    mov eax, 7
981
    inc esi
982
  @@:
983
    dec ebx
984
     jnz .bpp1_horz.flip_line
985
 
986
    pop esi edx ecx ebx
987
    add esi, [scanline_len]
988
    mov edi, [scanline_len]
989
    dec ecx
990
     jnz .bpp1_horz.begin
991
    pop edx eax
992
 
717 mikedld 993
  .exit:
1569 dunkaist 994
    xor eax, eax
995
    inc eax
996
    pop edi esi ebx
997
    ret
717 mikedld 998
 
999
  .error:
1569 dunkaist 1000
    xor eax, eax
1001
    pop edi esi ebx
1002
    ret
717 mikedld 1003
endp
1004
 
783 mikedld 1005
;;================================================================================================;;
1079 diamond 1006
proc img.flip _img, _flip_kind ;//////////////////////////////////////////////////////////////////;;
783 mikedld 1007
;;------------------------------------------------------------------------------------------------;;
1079 diamond 1008
;? Flip all layers of image                                                                       ;;
783 mikedld 1009
;;------------------------------------------------------------------------------------------------;;
1010
;> _img = pointer to image                                                                        ;;
1079 diamond 1011
;> _flip_kind = one of FLIP_* constants                                                           ;;
1012
;;------------------------------------------------------------------------------------------------;;
1013
;< eax = false / true                                                                             ;;
1014
;;================================================================================================;;
1569 dunkaist 1015
    push    1
1016
    mov ebx, [_img]
1079 diamond 1017
@@:
1569 dunkaist 1018
    mov eax, [ebx + Image.Previous]
1019
    test    eax, eax
1020
    jz  .loop
1021
    mov ebx, eax
1022
    jmp @b
1079 diamond 1023
.loop:
1569 dunkaist 1024
    stdcall img.flip.layer, ebx, [_flip_kind]
1025
    test    eax, eax
1026
    jnz @f
1027
    mov byte [esp], 0
1079 diamond 1028
@@:
1569 dunkaist 1029
    mov ebx, [ebx + Image.Next]
1030
    test    ebx, ebx
1031
    jnz .loop
1032
    pop eax
1033
    ret
1079 diamond 1034
endp
1035
 
1036
;;================================================================================================;;
1037
proc img.rotate.layer _img, _rotate_kind ;////////////////////////////////////////////////////////;;
1038
;;------------------------------------------------------------------------------------------------;;
1039
;? Rotate image layer                                                                             ;;
1040
;;------------------------------------------------------------------------------------------------;;
1041
;> _img = pointer to image                                                                        ;;
783 mikedld 1042
;> _rotate_kind = one of ROTATE_* constants                                                       ;;
1043
;;------------------------------------------------------------------------------------------------;;
1044
;< eax = false / true                                                                             ;;
1045
;;================================================================================================;;
1046
locals
1047
  scanline_len_old    dd ?
1048
  scanline_len_new    dd ?
1049
  scanline_pixels_new dd ?
1569 dunkaist 1050
  line_buffer         dd ?
1051
  pixels_ptr          dd ?
783 mikedld 1052
endl
717 mikedld 1053
 
1569 dunkaist 1054
    mov [line_buffer], 0
783 mikedld 1055
 
1569 dunkaist 1056
    push    ebx esi edi
1057
    mov ebx, [_img]
1058
    stdcall img._.validate, ebx
1059
    or  eax, eax
1060
    jnz .error
783 mikedld 1061
 
1569 dunkaist 1062
    cmp [_rotate_kind], ROTATE_90_CCW
1063
    je  .rotate_ccw_low
1064
    cmp [_rotate_kind], ROTATE_90_CW
1065
    je  .rotate_cw_low
1066
    cmp [_rotate_kind], ROTATE_180
1067
    je  .flip
1068
    jmp .exit
783 mikedld 1069
 
1070
  .rotate_ccw_low:
1569 dunkaist 1071
    mov eax, [ebx + Image.Height]
1072
    mov [scanline_pixels_new], eax
1073
    call    img._.get_scanline_len
1074
    mov [scanline_len_new], eax
783 mikedld 1075
 
1569 dunkaist 1076
    invoke  mem.alloc, eax
1077
    or  eax, eax
1078
    jz  .error
1079
    mov [line_buffer], eax
783 mikedld 1080
 
1569 dunkaist 1081
    mov eax, [ebx + Image.Width]
1082
    mov ecx, eax
1083
    call    img._.get_scanline_len
1084
    mov [scanline_len_old], eax
783 mikedld 1085
 
1569 dunkaist 1086
    mov eax, [scanline_len_new]
1087
    imul    eax, ecx
1088
    add eax, [ebx + Image.Data]
1089
    mov [pixels_ptr], eax
783 mikedld 1090
 
1593 dunkaist 1091
    cmp [ebx + Image.Type], Image.bpp1
1092
    jz  .rotate_ccw1
1569 dunkaist 1093
    cmp [ebx + Image.Type], Image.bpp8
1094
    jz  .rotate_ccw8
1095
    cmp [ebx + Image.Type], Image.bpp24
1096
    jz  .rotate_ccw24
1097
    cmp [ebx + Image.Type], Image.bpp32
1098
    jz  .rotate_ccw32
999 diamond 1099
 
1079 diamond 1100
  .next_column_ccw_low1x:
1569 dunkaist 1101
    dec ecx
1102
    js  .exchange_dims
1103
    push    ecx
1079 diamond 1104
 
1569 dunkaist 1105
    mov edx, [scanline_len_old]
1106
    add [scanline_len_old], -2
1079 diamond 1107
 
1569 dunkaist 1108
    mov ecx, [scanline_pixels_new]
1109
    mov esi, [ebx + Image.Data]
1110
    mov edi, [line_buffer]
1111
    @@: mov ax, [esi]
1112
    mov [edi], ax
1113
    add esi, edx
1114
    add edi, 2
1115
    sub ecx, 1
1116
    jnz @b
1079 diamond 1117
 
1569 dunkaist 1118
    mov eax, [scanline_pixels_new]
1119
    mov edi, [ebx + Image.Data]
1120
    lea esi, [edi + 2]
1121
    mov edx, [scanline_len_old]
1122
    @@: mov ecx, edx
1123
    shr ecx, 2
1124
    rep movsd
1125
    mov ecx, edx
1126
    and ecx, 3
1127
    rep movsb
1128
    add esi, 1
1129
    sub eax, 1
1130
    jnz @b
1079 diamond 1131
 
1569 dunkaist 1132
    mov eax, [scanline_len_new]
1133
    sub [pixels_ptr], eax
1134
    mov ecx, [scanline_pixels_new]
1135
    mov esi, [line_buffer]
1136
    mov edi, [pixels_ptr]
1137
    mov edx, ecx
1138
    shr ecx, 2
1139
    rep movsd
1140
    mov ecx, edx
1141
    and ecx, 3
1142
    rep movsb
1079 diamond 1143
 
1569 dunkaist 1144
    pop ecx
1145
    jmp .next_column_ccw_low1x
1079 diamond 1146
 
1147
.rotate_ccw32:
783 mikedld 1148
  .next_column_ccw_low:
1569 dunkaist 1149
    dec ecx
1150
    js  .exchange_dims
1151
    push    ecx
783 mikedld 1152
 
1569 dunkaist 1153
    mov edx, [scanline_len_old]
1154
    add [scanline_len_old], -4
783 mikedld 1155
 
1569 dunkaist 1156
    mov ecx, [scanline_pixels_new]
1157
    mov esi, [ebx + Image.Data]
1158
    mov edi, [line_buffer]
1159
    @@: mov eax, [esi]
1160
    stosd
1161
    add esi, edx
1162
    dec ecx
1163
    jnz @b
783 mikedld 1164
 
1569 dunkaist 1165
    mov eax, [scanline_pixels_new]
1166
    mov edi, [ebx + Image.Data]
1167
    lea esi, [edi + 4]
1168
    mov edx, [scanline_len_old]
1169
    shr edx, 2
1170
    @@: mov ecx, edx
1171
    rep movsd
1172
    add esi, 4
1173
    dec eax
1174
    jnz @b
783 mikedld 1175
 
1569 dunkaist 1176
    mov eax, [scanline_len_new]
1177
    sub [pixels_ptr], eax
1178
    mov ecx, [scanline_pixels_new]
1179
    mov esi, [line_buffer]
1180
    mov edi, [pixels_ptr]
1181
    rep movsd
783 mikedld 1182
 
1569 dunkaist 1183
    pop ecx
1184
    jmp .next_column_ccw_low
783 mikedld 1185
 
999 diamond 1186
.rotate_ccw8:
1187
  .next_column_ccw_low8:
1569 dunkaist 1188
    dec ecx
1189
    js  .exchange_dims
1190
    push    ecx
999 diamond 1191
 
1569 dunkaist 1192
    mov edx, [scanline_len_old]
1193
    add [scanline_len_old], -1
999 diamond 1194
 
1569 dunkaist 1195
    mov ecx, [scanline_pixels_new]
1196
    mov esi, [ebx + Image.Data]
1197
    mov edi, [line_buffer]
1198
    @@: mov al, [esi]
1199
    mov [edi], al
1200
    add esi, edx
1201
    add edi, 1
1202
    sub ecx, 1
1203
    jnz @b
999 diamond 1204
 
1569 dunkaist 1205
    mov eax, [scanline_pixels_new]
1206
    mov edi, [ebx + Image.Data]
1207
    lea esi, [edi + 1]
1208
    mov edx, [scanline_len_old]
1209
    @@: mov ecx, edx
1210
    shr ecx, 2
1211
    rep movsd
1212
    mov ecx, edx
1213
    and ecx, 3
1214
    rep movsb
1215
    add esi, 1
1216
    sub eax, 1
1217
    jnz @b
999 diamond 1218
 
1569 dunkaist 1219
    mov eax, [scanline_len_new]
1220
    sub [pixels_ptr], eax
1221
    mov ecx, [scanline_pixels_new]
1222
    mov esi, [line_buffer]
1223
    mov edi, [pixels_ptr]
1224
    mov edx, ecx
1225
    shr ecx, 2
1226
    rep movsd
1227
    mov ecx, edx
1228
    and ecx, 3
1229
    rep movsb
999 diamond 1230
 
1569 dunkaist 1231
    pop ecx
1232
    jmp .next_column_ccw_low8
999 diamond 1233
 
1234
.rotate_ccw24:
1235
  .next_column_ccw_low24:
1569 dunkaist 1236
    dec ecx
1237
    js  .exchange_dims
1238
    push    ecx
999 diamond 1239
 
1569 dunkaist 1240
    mov edx, [scanline_len_old]
1241
    add [scanline_len_old], -3
999 diamond 1242
 
1569 dunkaist 1243
    mov ecx, [scanline_pixels_new]
1244
    mov esi, [ebx + Image.Data]
1245
    mov edi, [line_buffer]
1246
    @@: mov al, [esi]
1247
    mov [edi], al
1248
    mov al, [esi+1]
1249
    mov [edi+1], al
1250
    mov al, [esi+2]
1251
    mov [edi+2], al
1252
    add esi, edx
1253
    add edi, 3
1254
    sub ecx, 1
1255
    jnz @b
999 diamond 1256
 
1569 dunkaist 1257
    mov eax, [scanline_pixels_new]
1258
    mov edi, [ebx + Image.Data]
1259
    lea esi, [edi + 3]
1260
    mov edx, [scanline_len_old]
1261
    @@: mov ecx, edx
1262
    shr ecx, 2
1263
    rep movsd
1264
    mov ecx, edx
1265
    and ecx, 3
1266
    rep movsb
1267
    add esi, 3
1268
    sub eax, 1
1269
    jnz @b
999 diamond 1270
 
1569 dunkaist 1271
    mov eax, [scanline_len_new]
1272
    sub [pixels_ptr], eax
1273
    mov ecx, eax
1274
    mov esi, [line_buffer]
1275
    mov edi, [pixels_ptr]
1276
    shr ecx, 2
1277
    rep movsd
1278
    mov ecx, eax
1279
    and ecx, 3
1280
    rep movsb
999 diamond 1281
 
1569 dunkaist 1282
    pop ecx
1283
    jmp .next_column_ccw_low24
999 diamond 1284
 
1593 dunkaist 1285
.rotate_ccw1:
1286
    push ecx edx
1287
 
1288
    mov eax, [ebx+Image.Height]
1289
    add eax, 7
1290
    shr eax, 3
1291
    mul word[ebx+Image.Width]
1292
    shl eax, 16
1293
    shrd eax, edx, 16
1294
    push eax                                            ; save new data size
1295
 
1296
    invoke  mem.alloc, eax
1297
    or  eax, eax
1298
    jz  .error
1299
    push eax                                            ; save pointer to new data
1300
 
1301
    mov ecx, [ebx+Image.Width]
1302
    and ecx,  0x07
1303
    neg cl
1304
    add cl, 8
1305
    and cl, 0x07                                        ; clear if cl=8
1306
 
1307
    mov esi, eax
1308
    mov edi, [ebx+Image.Data]
1309
    mov eax, 7
1310
    mov edx, [scanline_len_old]
1311
    dec edx
1312
    add edi, edx
1313
 
1314
  .rotate_ccw1.begin:
1315
    bt  [edi], ecx
1316
     jc .rotate_ccw1.one
1317
  .rotate_ccw1.zero:
1318
    btr [esi], eax
1319
     jmp @f
1320
  .rotate_ccw1.one:
1321
    bts [esi], eax
1322
  @@:
1323
    add edi, [scanline_len_old]
1324
    dec [scanline_pixels_new]
1325
     jz .rotate_ccw1.end_of_line
1326
    dec eax
1327
     jns .rotate_ccw1.begin
1328
    mov eax, 7
1329
    inc esi
1330
     jmp .rotate_ccw1.begin
1331
 
1332
  .rotate_ccw1.end_of_line:
1333
    inc esi
1334
    mov eax, 7
1335
    mov edi, [ebx+Image.Height]
1336
    mov [scanline_pixels_new],  edi
1337
    inc ecx
1338
    and cl, 0x07
1339
     jz @f
1340
    mov edi, [ebx+Image.Data]
1341
    add edi, edx
1342
     jmp .rotate_ccw1.begin
1343
  @@:
1344
    dec edx
1345
     js .rotate_ccw1.quit
1346
    mov edi, [ebx+Image.Data]
1347
    add edi, edx
1348
     jmp .rotate_ccw1.begin
1349
 
1350
  .rotate_ccw1.quit:
1351
    pop eax                                             ; get pointer to new data
1352
    mov esi, eax
1353
    mov edi, [ebx + Image.Data]
1354
    pop ecx                                             ; get new data size
1355
    rep movsb
1356
 
1357
    invoke  mem.free, eax
1358
 
1359
    pop edx ecx
1360
     jmp .exchange_dims
1361
 
783 mikedld 1362
  .rotate_cw_low:
1569 dunkaist 1363
    mov eax, [ebx + Image.Height]
1364
    mov [scanline_pixels_new], eax
1365
    call    img._.get_scanline_len
1366
    mov [scanline_len_new], eax
783 mikedld 1367
 
1569 dunkaist 1368
    invoke  mem.alloc, eax
1369
    or  eax, eax
1370
    jz  .error
1371
    mov [line_buffer], eax
783 mikedld 1372
 
1569 dunkaist 1373
    mov eax, [ebx + Image.Width]
1374
    mov ecx, eax
1375
    call    img._.get_scanline_len
1376
    mov [scanline_len_old], eax
783 mikedld 1377
 
1569 dunkaist 1378
    mov eax, [scanline_len_new]
1379
    imul    eax, ecx
1380
    add eax, [ebx + Image.Data]
1381
    mov [pixels_ptr], eax
783 mikedld 1382
 
1593 dunkaist 1383
    cmp [ebx + Image.Type], Image.bpp1
1384
    jz  .rotate_cw1
1569 dunkaist 1385
    cmp [ebx + Image.Type], Image.bpp8
1386
    jz  .rotate_cw8
1387
    cmp [ebx + Image.Type], Image.bpp24
1388
    jz  .rotate_cw24
1389
    cmp [ebx + Image.Type], Image.bpp32
1390
    jz  .rotate_cw32
999 diamond 1391
 
1079 diamond 1392
  .next_column_cw_low1x:
1569 dunkaist 1393
    dec ecx
1394
    js  .exchange_dims
1395
    push    ecx
1079 diamond 1396
 
1569 dunkaist 1397
    mov edx, [scanline_len_old]
1398
    add [scanline_len_old], -2
1079 diamond 1399
 
1569 dunkaist 1400
    mov ecx, [scanline_pixels_new]
1401
    mov esi, [pixels_ptr]
1402
    add esi, -2
1403
    mov edi, [line_buffer]
1404
    @@: mov ax, [esi]
1405
    mov [edi], ax
1406
    sub esi, edx
1407
    add edi, 2
1408
    sub ecx, 1
1409
    jnz @b
1079 diamond 1410
 
1569 dunkaist 1411
    mov eax, [scanline_pixels_new]
1412
    dec eax
1413
    mov edi, [ebx + Image.Data]
1414
    add edi, [scanline_len_old]
1415
    lea esi, [edi + 2]
1416
    mov edx, [scanline_len_old]
1417
    @@: mov ecx, edx
1418
    shr ecx, 2
1419
    rep movsd
1420
    mov ecx, edx
1421
    and ecx, 3
1422
    rep movsb
1423
    add esi, 3
1424
    sub eax, 1
1425
    jnz @b
1079 diamond 1426
 
1569 dunkaist 1427
    mov eax, [scanline_len_new]
1428
    sub [pixels_ptr], eax
1429
    mov ecx, eax
1430
    mov esi, [line_buffer]
1431
    mov edi, [pixels_ptr]
1432
    shr ecx, 2
1433
    rep movsd
1434
    mov ecx, eax
1435
    and ecx, 3
1436
    rep movsb
1079 diamond 1437
 
1569 dunkaist 1438
    pop ecx
1439
    jmp .next_column_cw_low1x
1079 diamond 1440
 
1441
.rotate_cw32:
783 mikedld 1442
  .next_column_cw_low:
1569 dunkaist 1443
    dec ecx
1444
    js  .exchange_dims
1445
    push    ecx
783 mikedld 1446
 
1569 dunkaist 1447
    mov edx, [scanline_len_old]
1448
    add [scanline_len_old], -4
783 mikedld 1449
 
1569 dunkaist 1450
    mov ecx, [scanline_pixels_new]
1451
    mov esi, [pixels_ptr]
1452
    add esi, -4
1453
    mov edi, [line_buffer]
1454
    @@: mov eax, [esi]
1455
    stosd
1456
    sub esi, edx
1457
    dec ecx
1458
    jnz @b
783 mikedld 1459
 
1569 dunkaist 1460
    mov eax, [scanline_pixels_new]
1461
    dec eax
1462
    mov edi, [ebx + Image.Data]
1463
    add edi, [scanline_len_old]
1464
    lea esi, [edi + 4]
1465
    mov edx, [scanline_len_old]
1466
    shr edx, 2
1467
    @@: mov ecx, edx
1468
    rep movsd
1469
    add esi, 4
1470
    dec eax
1471
    jnz @b
783 mikedld 1472
 
1569 dunkaist 1473
    mov eax, [scanline_len_new]
1474
    sub [pixels_ptr], eax
1475
    mov ecx, [scanline_pixels_new]
1476
    mov esi, [line_buffer]
1477
    mov edi, [pixels_ptr]
1478
    rep movsd
783 mikedld 1479
 
1569 dunkaist 1480
    pop ecx
1481
    jmp .next_column_cw_low
783 mikedld 1482
 
999 diamond 1483
.rotate_cw8:
1484
  .next_column_cw_low8:
1569 dunkaist 1485
    dec ecx
1486
    js  .exchange_dims
1487
    push    ecx
999 diamond 1488
 
1569 dunkaist 1489
    mov edx, [scanline_len_old]
1490
    add [scanline_len_old], -1
999 diamond 1491
 
1569 dunkaist 1492
    mov ecx, [scanline_pixels_new]
1493
    mov esi, [pixels_ptr]
1494
    add esi, -1
1495
    mov edi, [line_buffer]
1496
    @@: mov al, [esi]
1497
    mov [edi], al
1498
    sub esi, edx
1499
    add edi, 1
1500
    sub ecx, 1
1501
    jnz @b
999 diamond 1502
 
1569 dunkaist 1503
    mov eax, [scanline_pixels_new]
1504
    dec eax
1505
    mov edi, [ebx + Image.Data]
1506
    add edi, [scanline_len_old]
1507
    lea esi, [edi + 1]
1508
    mov edx, [scanline_len_old]
1509
    @@: mov ecx, edx
1510
    shr ecx, 2
1511
    rep movsd
1512
    mov ecx, edx
1513
    and ecx, 3
1514
    rep movsb
1515
    add esi, 1
1516
    sub eax, 1
1517
    jnz @b
999 diamond 1518
 
1569 dunkaist 1519
    mov eax, [scanline_len_new]
1520
    sub [pixels_ptr], eax
1521
    mov ecx, eax
1522
    mov esi, [line_buffer]
1523
    mov edi, [pixels_ptr]
1524
    shr ecx, 2
1525
    rep movsd
1526
    mov ecx, eax
1527
    and ecx, 3
1528
    rep movsb
999 diamond 1529
 
1569 dunkaist 1530
    pop ecx
1531
    jmp .next_column_cw_low8
999 diamond 1532
 
1533
.rotate_cw24:
1534
  .next_column_cw_low24:
1569 dunkaist 1535
    dec ecx
1536
    js  .exchange_dims
1537
    push    ecx
999 diamond 1538
 
1569 dunkaist 1539
    mov edx, [scanline_len_old]
1540
    add [scanline_len_old], -3
999 diamond 1541
 
1569 dunkaist 1542
    mov ecx, [scanline_pixels_new]
1543
    mov esi, [pixels_ptr]
1544
    add esi, -3
1545
    mov edi, [line_buffer]
1546
    @@: mov al, [esi]
1547
    mov [edi], al
1548
    mov al, [esi+1]
1549
    mov [edi+1], al
1550
    mov al, [esi+2]
1551
    mov [edi+2], al
1552
    sub esi, edx
1553
    add edi, 3
1554
    sub ecx, 1
1555
    jnz @b
999 diamond 1556
 
1569 dunkaist 1557
    mov eax, [scanline_pixels_new]
1558
    dec eax
1559
    mov edi, [ebx + Image.Data]
1560
    add edi, [scanline_len_old]
1561
    lea esi, [edi + 3]
1562
    mov edx, [scanline_len_old]
1563
    @@: mov ecx, edx
1564
    shr ecx, 2
1565
    rep movsd
1566
    mov ecx, edx
1567
    and ecx, 3
1568
    rep movsb
1569
    add esi, 3
1570
    sub eax, 1
1571
    jnz @b
999 diamond 1572
 
1569 dunkaist 1573
    mov eax, [scanline_len_new]
1574
    sub [pixels_ptr], eax
1575
    mov ecx, eax
1576
    mov esi, [line_buffer]
1577
    mov edi, [pixels_ptr]
1578
    shr ecx, 2
1579
    rep movsd
1580
    mov ecx, eax
1581
    and ecx, 3
1582
    rep movsb
999 diamond 1583
 
1569 dunkaist 1584
    pop ecx
1585
    jmp .next_column_cw_low24
999 diamond 1586
 
1593 dunkaist 1587
.rotate_cw1:
1588
    push ecx edx
1589
 
1590
    mov eax, [ebx+Image.Height]
1591
    add eax, 7
1592
    shr eax, 3
1593
    mul word[ebx+Image.Width]
1594
    shl eax, 16
1595
    shrd eax, edx, 16
1596
 
1597
    push eax                                            ; save new data size
1598
 
1599
    invoke  mem.alloc, eax
1600
    or  eax, eax
1601
    jz  .error
1602
    push eax                                            ; save pointer to new data
1603
 
1604
    mov ecx, 7
1605
 
1606
    mov edx, [ebx+Image.Width]
1607
    mov [pixels_ptr],   edx                             ; we don't use pixels_ptr as it do other procedures, we save there [ebx+Image.Width]
1608
    mov esi, eax
1609
    mov edi, [ebx+Image.Data]
1610
    mov eax, [ebx+Image.Height]
1611
    dec eax
1612
    mul [scanline_len_old]
1613
    add edi, eax
1614
    mov eax, 7
1615
    mov edx, 0
1616
 
1617
  .rotate_cw1.begin:
1618
    bt  [edi], ecx
1619
     jc .rotate_cw1.one
1620
  .rotate_cw1.zero:
1621
    btr [esi], eax
1622
     jmp @f
1623
  .rotate_cw1.one:
1624
    bts [esi], eax
1625
  @@:
1626
    sub edi, [scanline_len_old]
1627
    dec [scanline_pixels_new]
1628
     jz .rotate_cw1.end_of_line
1629
    dec eax
1630
     jns .rotate_cw1.begin
1631
    mov eax, 7
1632
    inc esi
1633
     jmp .rotate_cw1.begin
1634
 
1635
  .rotate_cw1.end_of_line:
1636
    dec [pixels_ptr]
1637
     jz .rotate_cw1.quit
1638
    inc esi
1639
    mov eax, [ebx+Image.Height]
1640
    mov [scanline_pixels_new],   eax
1641
    mov eax, 7
1642
    dec ecx
1643
     js @f
1644
    mov edi, [ebx+Image.Height]
1645
    dec edi
1646
    imul edi, [scanline_len_old]
1647
    add edi, [ebx+Image.Data]
1648
    add edi, edx
1649
     jmp .rotate_cw1.begin
1650
  @@:
1651
    mov ecx, 7
1652
    inc edx
1653
    cmp edx, [scanline_len_old]
1654
     je .rotate_cw1.quit
1655
    mov edi, [ebx+Image.Height]
1656
    dec edi
1657
    imul edi, [scanline_len_old]
1658
    add edi, [ebx+Image.Data]
1659
    add edi, edx
1660
     jmp .rotate_cw1.begin
1661
 
1662
  .rotate_cw1.quit:
1663
    pop eax                                             ; get pointer to new data
1664
    mov esi, eax
1665
    mov edi, [ebx + Image.Data]
1666
    pop ecx                                             ; get new data size
1667
    rep movsb
1668
 
1669
    invoke  mem.free, eax
1670
 
1671
    pop edx ecx
1672
     jmp .exchange_dims
1673
 
783 mikedld 1674
  .flip:
1569 dunkaist 1675
    jmp .exit
783 mikedld 1676
 
1677
  .exchange_dims:
1569 dunkaist 1678
    push    [ebx + Image.Width] [ebx + Image.Height]
1679
    pop [ebx + Image.Width] [ebx + Image.Height]
783 mikedld 1680
 
1681
  .exit:
1569 dunkaist 1682
    invoke  mem.free, [line_buffer]
1683
    xor eax, eax
1684
    inc eax
1685
    pop edi esi ebx
1686
    ret
783 mikedld 1687
 
1688
  .error:
1569 dunkaist 1689
    invoke  mem.free, [line_buffer]
1690
    xor eax, eax
1691
    pop edi esi ebx
1692
    ret
783 mikedld 1693
endp
1694
 
1079 diamond 1695
;;================================================================================================;;
1696
proc img.rotate _img, _rotate_kind ;//////////////////////////////////////////////////////////////;;
1697
;;------------------------------------------------------------------------------------------------;;
1698
;? Rotate all layers of image                                                                     ;;
1699
;;------------------------------------------------------------------------------------------------;;
1700
;> _img = pointer to image                                                                        ;;
1701
;> _rotate_kind = one of ROTATE_* constants                                                       ;;
1702
;;------------------------------------------------------------------------------------------------;;
1703
;< eax = false / true                                                                             ;;
1704
;;================================================================================================;;
1569 dunkaist 1705
    push    1
1706
    mov ebx, [_img]
1079 diamond 1707
@@:
1569 dunkaist 1708
    mov eax, [ebx + Image.Previous]
1709
    test    eax, eax
1710
    jz  .loop
1711
    mov ebx, eax
1712
    jmp @b
1079 diamond 1713
.loop:
1569 dunkaist 1714
    stdcall img.rotate.layer, ebx, [_rotate_kind]
1715
    test    eax, eax
1716
    jnz @f
1717
    mov byte [esp], 0
1079 diamond 1718
@@:
1569 dunkaist 1719
    mov ebx, [ebx + Image.Next]
1720
    test    ebx, ebx
1721
    jnz .loop
1722
    pop eax
1723
    ret
1079 diamond 1724
endp
783 mikedld 1725
 
717 mikedld 1726
;;================================================================================================;;
1079 diamond 1727
proc img.draw _img, _x, _y, _width, _height, _xpos, _ypos ;///////////////////////////////////////;;
1728
;;------------------------------------------------------------------------------------------------;;
1729
;? Draw image in the window                                                                       ;;
1730
;;------------------------------------------------------------------------------------------------;;
1731
;> _img = pointer to image                                                                        ;;
1732
;>_x = x-coordinate in the window                                                                 ;;
1733
;>_y = y-coordinate in the window                                                                 ;;
1734
;>_width = maximum width to draw                                                                  ;;
1735
;>_height = maximum height to draw                                                                ;;
1736
;>_xpos = offset in image by x-axis                                                               ;;
1737
;>_ypos = offset in image by y-axis                                                               ;;
1738
;;------------------------------------------------------------------------------------------------;;
1739
;< no return value                                                                                ;;
1740
;;================================================================================================;;
1569 dunkaist 1741
    push    ebx esi edi
1742
    mov ebx, [_img]
1743
    stdcall img._.validate, ebx
1744
    test    eax, eax
1745
    jnz .done
1746
    mov ecx, [ebx + Image.Width]
1747
    sub ecx, [_xpos]
1748
    jbe .done
1749
    cmp ecx, [_width]
1750
    jb  @f
1751
    mov ecx, [_width]
1079 diamond 1752
@@:
1569 dunkaist 1753
    mov edx, [ebx + Image.Height]
1754
    sub edx, [_ypos]
1755
    jbe .done
1756
    cmp edx, [_height]
1757
    jb  @f
1758
    mov edx, [_height]
1079 diamond 1759
@@:
1569 dunkaist 1760
    mov eax, [ebx + Image.Width]
1761
    sub eax, ecx
1762
    call    img._.get_scanline_len
1763
    shl ecx, 16
1764
    add ecx, edx
1765
    push    eax
1766
    mov eax, [ebx + Image.Width]
1767
    imul    eax, [_ypos]
1768
    add eax, [_xpos]
1769
    call    img._.get_scanline_len
1770
    add eax, [ebx + Image.Data]
1771
    mov edx, [_x - 2]
1772
    mov dx, word [_y]
1773
    mov esi, [ebx + Image.Type]
1774
    mov esi, [type2bpp + (esi-1)*4]
1775
    mov edi, [ebx + Image.Palette]
1776
    xchg    eax, ebx
1777
    pop eax
1778
    push    ebp
1779
    push    65
1780
    pop ebp
1781
    xchg    eax, ebp
1782
    int 40h
1783
    pop ebp
1079 diamond 1784
.done:
1569 dunkaist 1785
    pop edi esi ebx
1786
    ret
1079 diamond 1787
endp
1788
 
1789
;;================================================================================================;;
717 mikedld 1790
;;////////////////////////////////////////////////////////////////////////////////////////////////;;
1791
;;================================================================================================;;
1792
;! Below are private procs you should never call directly from your code                          ;;
1793
;;================================================================================================;;
1794
;;////////////////////////////////////////////////////////////////////////////////////////////////;;
1795
;;================================================================================================;;
1796
 
1797
 
1798
;;================================================================================================;;
1799
proc img._.validate, _img ;///////////////////////////////////////////////////////////////////////;;
1800
;;------------------------------------------------------------------------------------------------;;
1801
;? --- TBD ---                                                                                    ;;
1802
;;------------------------------------------------------------------------------------------------;;
1803
;> --- TBD ---                                                                                    ;;
1804
;;------------------------------------------------------------------------------------------------;;
1805
;< --- TBD ---                                                                                    ;;
1806
;;================================================================================================;;
1569 dunkaist 1807
    xor eax, eax
1808
    ret
717 mikedld 1809
endp
1810
 
1811
;;================================================================================================;;
1812
proc img._.new ;//////////////////////////////////////////////////////////////////////////////////;;
1813
;;------------------------------------------------------------------------------------------------;;
1814
;? --- TBD ---                                                                                    ;;
1815
;;------------------------------------------------------------------------------------------------;;
1816
;> --- TBD ---                                                                                    ;;
1817
;;------------------------------------------------------------------------------------------------;;
1818
;< eax = 0 / pointer to image                                                                     ;;
1819
;;================================================================================================;;
1569 dunkaist 1820
    invoke  mem.alloc, sizeof.Image
1821
    test    eax, eax
1822
    jz  @f
1823
    push    ecx
1824
    xor ecx, ecx
1825
    mov [eax + Image.Data], ecx
1826
    mov [eax + Image.Type], ecx
1827
    mov [eax + Image.Flags], ecx
1828
    mov [eax + Image.Extended], ecx
1829
    mov [eax + Image.Previous], ecx
1830
    mov [eax + Image.Next], ecx
1831
    pop ecx
999 diamond 1832
@@:
1569 dunkaist 1833
    ret
717 mikedld 1834
endp
1835
 
1836
;;================================================================================================;;
1837
proc img._.delete _img ;//////////////////////////////////////////////////////////////////////////;;
1838
;;------------------------------------------------------------------------------------------------;;
1839
;? --- TBD ---                                                                                    ;;
1840
;;------------------------------------------------------------------------------------------------;;
1841
;> --- TBD ---                                                                                    ;;
1842
;;------------------------------------------------------------------------------------------------;;
1843
;< eax = false / true                                                                             ;;
1844
;;================================================================================================;;
1569 dunkaist 1845
    push    edx
1846
    mov edx, [_img]
1847
    cmp [edx + Image.Data], 0
1848
    je  @f
1849
    invoke  mem.free, [edx + Image.Data]
1850
    @@: cmp [edx + Image.Extended], 0
1851
    je  @f
1852
    invoke  mem.free, [edx + Image.Extended]
1853
    @@: invoke  mem.free, edx
1854
    pop edx
1855
    ret
717 mikedld 1856
endp
1857
 
783 mikedld 1858
;;================================================================================================;;
1859
proc img._.resize_data _img, _width, _height ;////////////////////////////////////////////////////;;
1860
;;------------------------------------------------------------------------------------------------;;
1861
;? --- TBD ---                                                                                    ;;
1862
;;------------------------------------------------------------------------------------------------;;
1863
;> --- TBD ---                                                                                    ;;
1864
;;------------------------------------------------------------------------------------------------;;
1865
;< --- TBD ---                                                                                    ;;
1866
;;================================================================================================;;
1569 dunkaist 1867
    push    ebx esi
1868
    mov ebx, [_img]
1869
    mov eax, [_height]
999 diamond 1870
; our memory is limited, [_width]*[_height] must not overflow
1871
; image with width or height greater than 65535 is most likely bogus
1569 dunkaist 1872
    cmp word [_width+2], 0
1873
    jnz .error
1874
    cmp word [_height+2], 0
1875
    jnz .error
1876
    imul    eax, [_width]
1877
    test    eax, eax
1878
    jz  .error
999 diamond 1879
; do not allow images which require too many memory
1569 dunkaist 1880
    cmp eax, 4000000h
1881
    jae .error
1593 dunkaist 1882
    cmp [ebx + Image.Type], Image.bpp1
1883
    jz  .bpp1
1569 dunkaist 1884
    cmp [ebx + Image.Type], Image.bpp8
1885
    jz  .bpp8
1886
    cmp [ebx + Image.Type], Image.bpp24
1887
    jz  .bpp24
999 diamond 1888
.bpp32:
1569 dunkaist 1889
    shl eax, 2
1890
    jmp @f
999 diamond 1891
.bpp24:
1569 dunkaist 1892
    lea eax, [eax*3]
1893
    jmp @f
999 diamond 1894
.bpp8:
1569 dunkaist 1895
    add eax, 256*4  ; for palette
1593 dunkaist 1896
    jmp @f
1897
.bpp1:
1898
    mov eax, [_width]
1899
    add eax, 7
1900
    shr eax, 3
1901
    mul word[_height]
1902
    shl eax, 16
1903
    mov ax,  dx
1904
    ror eax, 16
1905
 
1906
    push ebx
1907
    mov ebx, eax
1908
 
1909
    mov eax, [_height]
1910
    add eax, 7
1911
    shr eax, 3
1912
    mul word[_width]
1913
    shl eax, 16
1914
    mov ax,  dx
1915
    ror eax, 16
1916
 
1917
    cmp eax, ebx
1918
     jge .bpp1.skip
1919
    mov eax, ebx
1920
  .bpp1.skip:
1921
    pop ebx
1922
 
1923
    add eax, 2*4    ; for palette
999 diamond 1924
@@:
1569 dunkaist 1925
    mov esi, eax
1926
    invoke  mem.realloc, [ebx + Image.Data], eax
1927
    or  eax, eax
1928
    jz  .error
717 mikedld 1929
 
1569 dunkaist 1930
    mov [ebx + Image.Data], eax
1931
    push    [_width]
1932
    pop [ebx + Image.Width]
1933
    push    [_height]
1934
    pop [ebx + Image.Height]
1935
    cmp [ebx + Image.Type], Image.bpp8
1593 dunkaist 1936
    jnz @f
1569 dunkaist 1937
    lea esi, [eax + esi - 256*4]
1938
    mov [ebx + Image.Palette], esi
1939
    jmp .ret
1593 dunkaist 1940
@@:
1941
    cmp [ebx + Image.Type], Image.bpp1
1942
    jnz .ret
1943
    lea esi, [eax + esi - 2*4]
1944
    mov [ebx + Image.Palette], esi
1945
    jmp .ret
783 mikedld 1946
 
1947
  .error:
1569 dunkaist 1948
    xor eax, eax
999 diamond 1949
  .ret:
1569 dunkaist 1950
    pop esi ebx
1951
    ret
783 mikedld 1952
endp
1953
 
999 diamond 1954
;;================================================================================================;;
1955
img._.get_scanline_len: ;/////////////////////////////////////////////////////////////////////////;;
1956
;;------------------------------------------------------------------------------------------------;;
1957
;? --- TBD ---                                                                                    ;;
1958
;;------------------------------------------------------------------------------------------------;;
1959
;> --- TBD ---                                                                                    ;;
1960
;;------------------------------------------------------------------------------------------------;;
1961
;< --- TBD ---                                                                                    ;;
1962
;;================================================================================================;;
1593 dunkaist 1963
    cmp [ebx + Image.Type], Image.bpp1
1964
    jz  .bpp1.1
1569 dunkaist 1965
    cmp [ebx + Image.Type], Image.bpp8
1966
    jz  .bpp8.1
1967
    cmp [ebx + Image.Type], Image.bpp24
1968
    jz  .bpp24.1
1969
    add eax, eax
1970
    cmp [ebx + Image.Type], Image.bpp32
1593 dunkaist 1971
    jnz .quit
1569 dunkaist 1972
    add eax, eax
1593 dunkaist 1973
    jmp .quit
999 diamond 1974
.bpp24.1:
1569 dunkaist 1975
    lea eax, [eax*3]
1593 dunkaist 1976
    jmp .quit
1977
.bpp1.1:
1978
    add eax, 7
1979
    shr eax, 3
999 diamond 1980
.bpp8.1:
1593 dunkaist 1981
.quit:
1569 dunkaist 1982
    ret
783 mikedld 1983
 
999 diamond 1984
 
717 mikedld 1985
;;================================================================================================;;
1986
;;////////////////////////////////////////////////////////////////////////////////////////////////;;
1987
;;================================================================================================;;
1988
;! Below is private data you should never use directly from your code                             ;;
1989
;;================================================================================================;;
1990
;;////////////////////////////////////////////////////////////////////////////////////////////////;;
1991
;;================================================================================================;;
1992
 
1079 diamond 1993
align 4
717 mikedld 1994
img._.formats_table:
1995
  .bmp dd img.is.bmp, img.decode.bmp, img.encode.bmp
1102 diamond 1996
  .ico dd img.is.ico, img.decode.ico_cur, img.encode.ico
1997
  .cur dd img.is.cur, img.decode.ico_cur, img.encode.cur
717 mikedld 1998
  .gif dd img.is.gif, img.decode.gif, img.encode.gif
1014 diamond 1999
  .png dd img.is.png, img.decode.png, img.encode.png
999 diamond 2000
  .jpg dd img.is.jpg, img.decode.jpg, img.encode.jpg
1079 diamond 2001
  .tga dd img.is.tga, img.decode.tga, img.encode.tga
1569 dunkaist 2002
  .pcx dd img.is.pcx, img.decode.pcx, img.encode.pcx
1921 dunkaist 2003
  .xcf dd img.is.xcf, img.decode.xcf, img.encode.xcf
2388 dunkaist 2004
  .tiff dd img.is.tiff, img.decode.tiff, img.encode.tiff
2005
  .pnm dd img.is.pnm, img.decode.pnm, img.encode.pnm
1079 diamond 2006
  .z80 dd img.is.z80, img.decode.z80, img.encode.z80 ;this must be the last entry as there are no
2007
  ;signatures in z80 screens at all
717 mikedld 2008
       dd 0
2009
 
1079 diamond 2010
align 4
1593 dunkaist 2011
type2bpp    dd  8, 24, 32, 15, 16, 1
1079 diamond 2012
img._.do_rgb.handlers:
1569 dunkaist 2013
    dd  img._.do_rgb.bpp8
2014
    dd  img._.do_rgb.bpp24
2015
    dd  img._.do_rgb.bpp32
2016
    dd  img._.do_rgb.bpp15.amd  ; can be overwritten in lib_init
2017
    dd  img._.do_rgb.bpp16.amd  ; can be overwritten in lib_init
1593 dunkaist 2018
    dd  img._.do_rgb.bpp1
717 mikedld 2019
 
1079 diamond 2020
img.flip.layer.handlers_horz:
1569 dunkaist 2021
    dd  img.flip.layer.bpp8_horz
2022
    dd  img.flip.layer.bpp24_horz
2023
    dd  img.flip.layer.bpp32_horz
2024
    dd  img.flip.layer.bpp1x_horz
2025
    dd  img.flip.layer.bpp1x_horz
1593 dunkaist 2026
    dd  img.flip.layer.bpp1_horz
1079 diamond 2027
 
717 mikedld 2028
;;================================================================================================;;
2029
;;////////////////////////////////////////////////////////////////////////////////////////////////;;
2030
;;================================================================================================;;
2031
;! Exported functions section                                                                     ;;
2032
;;================================================================================================;;
2033
;;////////////////////////////////////////////////////////////////////////////////////////////////;;
2034
;;================================================================================================;;
2035
 
2036
 
999 diamond 2037
align 4
717 mikedld 2038
@EXPORT:
2039
 
1569 dunkaist 2040
export                        \
2041
    lib_init    , 'lib_init'        , \
2042
    0x00050007  , 'version'     , \
2043
    img.is_img  , 'img_is_img'      , \
2044
    img.info    , 'img_info'        , \
2045
    img.from_file   , 'img_from_file'   , \
2046
    img.to_file , 'img_to_file'     , \
2047
    img.from_rgb    , 'img_from_rgb'    , \
2048
    img.to_rgb  , 'img_to_rgb'      , \
2049
    img.to_rgb2 , 'img_to_rgb2'     , \
2050
    img.decode  , 'img_decode'      , \
2051
    img.encode  , 'img_encode'      , \
2052
    img.create  , 'img_create'      , \
2053
    img.destroy , 'img_destroy'     , \
2054
    img.destroy.layer, 'img_destroy_layer', \
2055
    img.count   , 'img_count'       , \
2056
    img.lock_bits   , 'img_lock_bits'   , \
2057
    img.unlock_bits , 'img_unlock_bits' , \
2058
    img.flip    , 'img_flip'        , \
2059
    img.flip.layer  , 'img_flip_layer'  , \
2060
    img.rotate  , 'img_rotate'      , \
2061
    img.rotate.layer, 'img_rotate_layer', \
2062
    img.draw        , 'img_draw'
999 diamond 2063
 
1014 diamond 2064
; import from deflate unpacker
2065
; is initialized only when PNG loading is requested
2066
align 4
2067
@IMPORT:
2068
 
1102 diamond 2069
library archiver, 'archiver.obj'
1569 dunkaist 2070
import  archiver, \
2071
    deflate_unpack2, 'deflate_unpack2'
1014 diamond 2072
 
1079 diamond 2073
align 4
1014 diamond 2074
; mutex for unpacker loading
1569 dunkaist 2075
deflate_loader_mutex    dd  0
1014 diamond 2076
 
1079 diamond 2077
; default palette for GIF - b&w
2078
gif_default_palette:
1569 dunkaist 2079
    db  0, 0, 0
2080
    db  0xFF, 0xFF, 0xFF
1079 diamond 2081
 
999 diamond 2082
section '.data' data readable writable align 16
2083
; uninitialized data - global constant tables
1079 diamond 2084
mem.alloc   dd ?
2085
mem.free    dd ?
2086
mem.realloc dd ?
2087
dll.load    dd ?
999 diamond 2088
 
2089
; data for YCbCr -> RGB translation
1569 dunkaist 2090
color_table_1       rd  256
2091
color_table_2       rd  256
2092
color_table_3       rd  256
2093
color_table_4       rd  256