Subversion Repositories Kolibri OS

Rev

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

Rev Author Line No. Line
6733 IgorA 1
 
2
3
 
4
; Copyright (c) 1998-2002,2004,2006-2016 Glenn Randers-Pehrson
5
; (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger)
6
; (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.)
7
8
 
9
; For conditions of distribution and use, see the disclaimer
10
; and license in png.inc
11
12
 
13
14
 
15
include '../../../../../../fs/kfar/trunk/kfar_arc/crc.inc'
16
include '../../../../../../fs/kfar/trunk/zlib/deflate.inc'
17
include 'pngtokos.inc' ;integrate png to kos
18
19
 
20
include 'pnglibconf.inc'
21
include 'pngpriv.inc'
22
include 'png.inc'
23
include 'pngstruct.inc'
24
include 'pnginfo.inc'
25
include 'pngerror.asm'
26
include 'pngtrans.asm'
27
include 'pngget.asm'
28
include 'pngwrite.asm'
29
include 'pngmem.asm'
30
include 'pngset.asm'
31
include 'pngwutil.asm'
32
include 'pngwio.asm'
33
include 'pngwtran.asm'
34
35
 
36
;typedef png_libpng_version_1_6_25 Your_png_h_is_not_version_1_6_25;
37
38
 
39
; of the PNG file signature.  If the PNG data is embedded into another
40
; stream we can set num_bytes = 8 so that libpng will not attempt to read
41
; or write any of the magic bytes before it starts on the IHDR.
42
43
 
44
 
45
;void (png_structrp png_ptr, int num_bytes)
46
align 4
47
proc png_set_sig_bytes uses eax edi, png_ptr:dword, num_bytes:dword
48
	png_debug 1, 'in png_set_sig_bytes'
49
50
 
51
	cmp edi,0
52
	je .end_f ;if (..==0) return
53
54
 
55
	cmp eax,0
56
	jge @f
57
		xor eax,eax
58
	@@:
59
	cmp eax,8
60
	jle @f ;if (..>8)
61
		png_error edi, 'Too many bytes for PNG signature'
62
	@@:
63
	mov byte[edi+png_struct.sig_bytes],al
64
.end_f:
65
	ret
66
endp
67
68
 
69
; checking less than the full 8-byte signature so that those apps that
70
; already read the first few bytes of a file to determine the file type
71
; can simply check the remaining bytes for extra assurance.  Returns
72
; an integer less than, equal to, or greater than zero if sig is found,
73
; respectively, to be less than, to match, or be greater than the correct
74
; PNG signature (this is the same behavior as strcmp, memcmp, etc).
75
76
 
77
align 4
78
proc png_sig_cmp, sig:dword, start:dword, num_to_check:dword
79
;   byte png_signature[8] = {137, 80, 78, 71, 13, 10, 26, 10};
80
81
 
82
;      num_to_check = 8;
83
84
 
85
;      return (-1);
86
87
 
88
;      return (-1);
89
90
 
91
;      num_to_check = 8 - start;
92
93
 
94
	ret
95
endp
96
97
 
98
99
 
100
;voidpf (voidpf png_ptr, uInt items, uInt size)
101
align 4
102
proc png_zalloc uses edx ecx, png_ptr:dword, items:dword, size:dword
103
104
 
105
	jne @f
106
		xor eax,eax
107
		jmp .end_f ;if (..==0) return 0
108
	@@:
109
110
 
111
	not eax
112
	xor edx,edx
113
	mov ecx,[size]
114
	div ecx
115
	cmp [items],eax
116
	jl @f ;if (..>=..)
117
		png_warning [png_ptr], 'Potential overflow in png_zalloc()'
118
		xor eax,eax
119
		jmp .end_f
120
	@@:
121
122
 
123
	imul ecx,[items]
124
	stdcall png_malloc_warn, [png_ptr], ecx
125
.end_f:
126
	ret
127
endp
128
129
 
130
;void (voidpf png_ptr, voidpf ptr)
131
align 4
132
proc png_zfree, png_ptr:dword, p2ptr:dword
133
	stdcall png_free, [png_ptr], [p2ptr]
134
	ret
135
endp
136
137
 
138
; in case CRC is > 32 bits to leave the top bits 0.
139
140
 
141
align 4
142
proc png_reset_crc uses eax edi, png_ptr:dword
143
	; The cast is safe because the crc is a 32-bit value.
144
	mov edi,[png_ptr]
145
	stdcall [calc_crc32], 0, Z_NULL, 0
146
	mov dword[edi+png_struct.crc],eax
147
	ret
148
endp
149
150
 
151
; much data to this routine as the largest single buffer size.  We
152
; also check that this data will actually be used before going to the
153
; trouble of calculating it.
154
155
 
156
align 4
157
proc png_calculate_crc uses eax ebx edi, png_ptr:dword, ptr:dword, length:dword
158
locals
159
	need_crc dd 1
160
	safe_length dd ?
161
endl
162
	mov edi,[png_ptr]
163
	PNG_CHUNK_ANCILLARY [edi+png_struct.chunk_name]
164
	cmp eax,0 ;if (..!=0)
165
	je @f
166
		mov eax,[edi+png_struct.flags]
167
		and eax,PNG_FLAG_CRC_ANCILLARY_MASK
168
		cmp eax,PNG_FLAG_CRC_ANCILLARY_USE or PNG_FLAG_CRC_ANCILLARY_NOWARN
169
		jne .end0 ;if (..==..)
170
			mov dword[need_crc],0
171
		jmp .end0
172
	@@: ;else ;critical
173
		mov eax,[edi+png_struct.flags]
174
		and eax,PNG_FLAG_CRC_CRITICAL_IGNORE
175
		jz .end0 ;if (..!=0)
6779 IgorA 176
			mov dword[need_crc],0
6733 IgorA 177
	.end0:
178
179
 
180
	; systems it is a 64-bit value.  crc32, however, returns 32 bits so the
181
	; following cast is safe.  'uInt' may be no more than 16 bits, so it is
182
	; necessary to perform a loop here.
183
184
 
185
	je .end_f
186
	cmp dword[length],0
187
	jle .end_f ;if (..!=0 && ..>0)
188
		mov eax,[edi+png_struct.crc] ;Should never issue a warning
189
190
 
191
			mov ebx,[length]
192
			mov [safe_length],ebx
193
;#ifndef __COVERITY__
194
;         if (safe_length == 0)
195
;            safe_length = (uInt)-1 ;evil, but safe
196
;end if
197
			stdcall [calc_crc32], eax, [ptr], [safe_length]
198
199
 
200
			; target system has characteristics that will probably violate other
201
			; assumptions within the libpng code.
202
203
 
204
			add [ptr],ebx
205
			sub [length],ebx
206
			cmp dword[length],0
207
		jg .cycle0 ;while (..>0)
208
209
 
210
		mov [edi+png_struct.crc],eax
211
	.end_f:
212
	ret
213
endp
214
215
 
216
; functions that create a png_struct.
217
218
 
219
align 4
220
proc png_user_version_check, png_ptr:dword, user_png_ver:dword
221
	; Libpng versions 1.0.0 and later are binary compatible if the version
222
	; string matches through the second '.'; we must recompile any
223
	; applications that use any older library version.
224
225
 
226
;   {
227
;      int i = -1;
228
;      int found_dots = 0;
229
230
 
231
;      {
232
;         i++;
233
;         if (user_png_ver[i] != PNG_LIBPNG_VER_STRING[i])
234
;            png_ptr->flags |= PNG_FLAG_LIBRARY_MISMATCH;
235
;         if (user_png_ver[i] == '.')
236
;            found_dots++;
237
;      } while (found_dots < 2 && user_png_ver[i] != 0 &&
238
;            PNG_LIBPNG_VER_STRING[i] != 0);
239
;   }
240
241
 
242
;      png_ptr->flags |= PNG_FLAG_LIBRARY_MISMATCH;
243
244
 
245
;   {
246
if PNG_WARNINGS_SUPPORTED eq 1
247
;      size_t pos = 0;
248
;      char m[128];
249
250
 
251
;          "Application built with libpng-");
252
;      pos = png_safecat(m, (sizeof m), pos, user_png_ver);
253
;      pos = png_safecat(m, (sizeof m), pos, " but running with ");
254
;      pos = png_safecat(m, (sizeof m), pos, PNG_LIBPNG_VER_STRING);
255
256
 
257
end if
258
259
 
260
;      png_ptr->flags = 0;
261
end if
262
263
 
264
;   }
265
266
 
267
	xor eax,eax
268
	inc eax
269
.end_f:
270
	ret
271
endp
272
273
 
274
; contains the common initialization.
275
276
 
277
;    png_error_ptr error_fn, png_error_ptr warn_fn, voidp mem_ptr,
278
;    png_malloc_ptr malloc_fn, png_free_ptr free_fn)
279
align 4
280
proc png_create_png_struct uses ebx ecx edi esi, user_png_ver:dword, error_ptr:dword, error_fn:dword, warn_fn:dword, mem_ptr:dword, malloc_fn:dword, free_fn:dword
281
locals
282
if PNG_SETJMP_SUPPORTED eq 1
283
	create_jmp_buf dd ? ;jmp_buf
284
end if
285
	create_struct png_struct
286
endl
287
	; This temporary stack-allocated structure is used to provide a place to
288
	; build enough context to allow the user provided memory allocator (if any)
289
	; to be called.
290
291
 
292
	mov ecx,sizeof.png_struct
293
	mov edi,ebp
294
	sub edi,ecx
295
	mov ebx,edi
296
	rep stosb
297
298
 
299
if PNG_USER_LIMITS_SUPPORTED eq 1
300
	mov dword[ebx+png_struct.user_width_max], PNG_USER_WIDTH_MAX
301
	mov dword[ebx+png_struct.user_height_max], PNG_USER_HEIGHT_MAX
302
303
 
304
	mov dword[ebx+png_struct.user_chunk_cache_max], PNG_USER_CHUNK_CACHE_MAX
305
306
 
307
	; in png_struct regardless.
308
309
 
310
end if
311
312
 
313
	; to do them now even though error handling is not yet set up.
314
315
 
316
	stdcall png_set_mem_fn, ebx, [mem_ptr], [malloc_fn], [free_fn]
317
end if
318
319
 
320
	; this will result in a memory leak unless the error_fn does something
321
	; extremely sophisticated.  The design lacks merit but is implicit in the
322
	; API.
323
324
 
325
326
 
327
	stdcall setjmp,... ;create_jmp_buf
328
	cmp eax,0
329
	j... .end0 ;if (!setjmp(create_jmp_buf))
330
331
 
332
		; successfully completed this function.  This only works if we have
333
		; setjmp() support compiled in, but it is safe - this stuff should
334
		; never happen.
335
336
 
337
		mov dword[ebx+png_struct.jmp_buf_size],0 ;stack allocation
338
;         create_struct.longjmp_fn = longjmp;
339
end if
340
	; Call the general version checker (shared with read and write code):
341
342
 
343
		cmp eax,0
344
		je .end0 ;if (..!=0)
345
			stdcall png_malloc_warn, ebx, sizeof.png_struct
346
			;eax = png_ptr
347
			cmp eax,0
348
			je .end0 ;if (..!=0)
349
				; png_ptr->zstream holds a back-pointer to the png_struct, so
350
				; this can only be done now:
351
352
 
353
				mov [ebx+png_struct.zstream.zfree], png_zfree
354
				mov [ebx+png_struct.zstream.opaque], eax
355
356
 
357
				; Eliminate the local error handling:
358
				mov [ebx+png_struct.jmp_buf_ptr], 0
359
				mov [ebx+png_struct.jmp_buf_size], 0
360
				mov [ebx+png_struct.longjmp_fn], 0
361
end if
362
				mov ecx,sizeof.png_struct
363
				mov edi,eax
364
				mov esi,ebx
365
				rep movsb ;*png_ptr = create_struct
366
367
 
368
				jmp .end_f
369
	.end0:
370
371
 
372
	; simple failure to allocate the png_struct.
373
374
 
375
.end_f:
376
	ret
377
endp
378
379
 
380
;png_infop (png_structrp png_ptr)
381
align 4
382
proc png_create_info_struct uses ebx ecx edi, png_ptr:dword
383
	png_debug 1, 'in png_create_info_struct'
384
	;ebx - info_ptr dd ? ;png_inforp
385
386
 
387
	cmp edi,0
388
	jne @f ;if (..==0) return 0
389
		xor eax,eax
390
		jmp .end_f
391
	@@:
392
393
 
394
	; that this call always returns ok.  The application typically sets up the
395
	; error handling *after* creating the info_struct because this is the way it
396
	; has always been done in 'example.asm'.
397
398
 
399
	mov ebx,eax
400
401
 
402
	je @f
403
		mov edi,eax
404
		xor eax,eax
405
		mov ecx,sizeof.png_info_def
406
		rep stosb ;memset(...
407
	@@:
408
409
 
410
.end_f:
411
	ret
412
endp
413
414
 
415
; Normally, one would use either png_destroy_read_struct() or
416
; png_destroy_write_struct() to free an info struct, but this may be
417
; useful for some applications.  From libpng 1.6.0 this function is also used
418
; internally to implement the png_info release part of the 'struct' destroy
419
; APIs.  This ensures that all possible approaches free the same data (all of
420
; it).
421
422
 
423
align 4
424
proc png_destroy_info_struct uses eax ebx ecx edi, png_ptr:dword, info_ptr_ptr:dword
425
	png_debug 1, 'in png_destroy_info_struct'
426
427
 
428
	je .end_f ;if (..==0) return
429
430
 
431
	cmp edi,0 ;if (..!=0)
432
	je .end_f
433
		; Do this first in case of an error below; if the app implements its own
434
		; memory management this can lead to png_free calling png_error, which
435
		; will abort this routine and return control to the app error handler.
436
		; An infinite loop may result if it then tries to free the same info
437
		; ptr.
438
439
 
440
441
 
442
		mov ebx,edi
443
		xor eax,eax
444
		mov ecx,sizeof.png_info_def
445
		rep stosb
446
		stdcall png_free, [png_ptr], ebx
447
	.end_f:
448
	ret
449
endp
450
451
 
452
; and applications using it are urged to use png_create_info_struct()
453
; instead.  Use deprecated in 1.6.0, internal use removed (used internally it
454
; is just a memset).
455
456
 
457
; the user-memory mechanism and the user error handling/warning mechanisms in
458
; those cases where it does anything other than a memset.
459
460
 
461
align 4
462
proc png_info_init_3, ptr_ptr:dword, png_info_struct_size:dword
463
;   png_inforp info_ptr = *ptr_ptr;
464
465
 
466
467
 
468
;      return;
469
470
 
471
;   {
472
;      *ptr_ptr = NULL;
473
	; The following line is why this API should not be used:
474
;      free(info_ptr);
475
;      info_ptr = png_malloc_base(NULL, (sizeof *info_ptr));
476
;      if (info_ptr == NULL)
477
;         return;
478
;      *ptr_ptr = info_ptr;
479
;   }
480
481
 
482
;   memset(info_ptr, 0, (sizeof *info_ptr));
483
	ret
484
endp
485
486
 
487
;void (png_structrp png_ptr, png_inforp info_ptr, int freer, uint_32 mask)
488
align 4
489
proc png_data_freer uses edi esi, png_ptr:dword, info_ptr:dword, freer:dword, mask:dword
490
	png_debug 1, 'in png_data_freer'
491
492
 
493
	cmp edi,0
494
	je .end_f
495
	mov esi,[info_ptr]
496
	cmp esi,0
497
	je .end_f ;if (..==0 || ..==0) return
498
499
 
500
;      info_ptr->free_me |= mask;
501
502
 
503
;      info_ptr->free_me &= ~mask;
504
505
 
506
;      png_error(png_ptr, "Unknown freer parameter in png_data_freer");
507
.end_f
508
	ret
509
endp
510
511
 
512
align 4
513
proc png_free_data uses eax edi esi, png_ptr:dword, info_ptr:dword, mask:dword, num:dword
514
	png_debug 1, 'in png_free_data'
515
516
 
517
	cmp edi,0
518
	je .end_f
519
	mov esi,[info_ptr]
520
	cmp esi,0
521
	je .end_f ;if (..==0 || ..==0) return
522
523
 
524
	; Free text item num or (if num == -1) all text items
525
;   if (info_ptr->text != 0 &&
526
;       ((mask & PNG_FREE_TEXT) & info_ptr->free_me) != 0)
527
;   {
528
;      if (num != -1)
529
;      {
530
;         png_free(png_ptr, info_ptr->text[num].key);
531
;         info_ptr->text[num].key = NULL;
532
;      }
533
534
 
535
;      {
536
;         int i;
537
538
 
539
;            png_free(png_ptr, info_ptr->text[i].key);
540
541
 
542
;         info_ptr->text = NULL;
543
;         info_ptr->num_text = 0;
544
;      }
545
;   }
546
end if
547
548
 
549
	; Free any tRNS entry
550
	mov eax,[mask]
551
	and eax,PNG_FREE_TRNS
552
	and eax,[esi+png_info_def.free_me]
553
	jz @f ;if (..!=0)
6779 IgorA 554
		and dword[esi+png_info_def.valid], not PNG_INFO_tRNS
6733 IgorA 555
		stdcall png_free, edi, [esi+png_info_def.trans_alpha]
556
		mov dword[esi+png_info_def.trans_alpha],0
557
		mov word[esi+png_info_def.num_trans],0
558
	@@:
559
end if
560
561
 
562
	; Free any sCAL entry
563
	mov eax,[mask]
564
	and eax,PNG_FREE_SCAL
565
	and eax,[esi+png_info_def.free_me]
566
	jz @f ;if (..!=0)
6779 IgorA 567
		stdcall png_free, edi, [esi+png_info_def.scal_s_width]
6733 IgorA 568
		stdcall png_free, edi, [esi+png_info_def.scal_s_height]
569
		mov dword[esi+png_info_def.scal_s_width],0
570
		mov dword[esi+png_info_def.scal_s_height],0
571
		and dword[esi+png_info_def.valid], not PNG_INFO_sCAL
572
	@@:
573
end if
574
575
 
576
	; Free any pCAL entry
577
;   if (((mask & PNG_FREE_PCAL) & info_ptr->free_me) != 0)
578
;   {
579
;      png_free(png_ptr, info_ptr->pcal_purpose);
580
;      png_free(png_ptr, info_ptr->pcal_units);
581
;      info_ptr->pcal_purpose = NULL;
582
;      info_ptr->pcal_units = NULL;
583
584
 
585
;         {
586
;            int i;
587
588
 
589
;               png_free(png_ptr, info_ptr->pcal_params[i]);
590
;
591
;            png_free(png_ptr, info_ptr->pcal_params);
592
;            info_ptr->pcal_params = NULL;
593
;         }
594
;      info_ptr->valid &= ~PNG_INFO_pCAL;
595
;   }
596
end if
597
598
 
599
	; Free any profile entry
600
	mov eax,[mask]
601
	and eax,PNG_FREE_ICCP
602
	and eax,[esi+png_info_def.free_me]
603
	jz @f ;if (..!=0)
6779 IgorA 604
		stdcall png_free, edi, [esi+png_info_def.iccp_name]
6733 IgorA 605
		stdcall png_free, edi, [esi+png_info_def.iccp_profile]
606
		mov dword[esi+png_info_def.iccp_name],0
607
		mov dword[esi+png_info_def.iccp_profile],0
608
		and dword[esi+png_info_def.valid], not PNG_INFO_iCCP
609
	@@:
610
end if
611
612
 
613
	; Free a given sPLT entry, or (if num == -1) all sPLT entries
614
;   if (info_ptr->splt_palettes != 0 &&
615
;       ((mask & PNG_FREE_SPLT) & info_ptr->free_me) != 0)
616
;   {
617
;      if (num != -1)
618
;      {
619
;         png_free(png_ptr, info_ptr->splt_palettes[num].name);
620
;         png_free(png_ptr, info_ptr->splt_palettes[num].entries);
621
;         info_ptr->splt_palettes[num].name = NULL;
622
;         info_ptr->splt_palettes[num].entries = NULL;
623
;      }
624
625
 
626
;      {
627
;         int i;
628
629
 
630
;         {
631
;            png_free(png_ptr, info_ptr->splt_palettes[i].name);
632
;            png_free(png_ptr, info_ptr->splt_palettes[i].entries);
633
;         }
634
635
 
636
;         info_ptr->splt_palettes = NULL;
637
;         info_ptr->splt_palettes_num = 0;
638
;         info_ptr->valid &= ~PNG_INFO_sPLT;
639
;      }
640
;   }
641
end if
642
643
 
644
;   if (info_ptr->unknown_chunks != 0 &&
645
;       ((mask & PNG_FREE_UNKN) & info_ptr->free_me) != 0)
646
;   {
647
;      if (num != -1)
648
;      {
649
;          png_free(png_ptr, info_ptr->unknown_chunks[num].data);
650
;          info_ptr->unknown_chunks[num].data = NULL;
651
;      }
652
653
 
654
;      {
655
;         int i;
656
657
 
658
;            png_free(png_ptr, info_ptr->unknown_chunks[i].data);
659
660
 
661
;         info_ptr->unknown_chunks = NULL;
662
;         info_ptr->unknown_chunks_num = 0;
663
;      }
664
;   }
665
end if
666
667
 
668
	; Free any hIST entry
669
	mov eax,[mask]
670
	and eax,PNG_FREE_HIST
671
	and eax,[esi+png_info_def.free_me]
672
	jz @f ;if (..!=0)
6779 IgorA 673
		stdcall png_free, edi, [esi+png_info_def.hist]
6733 IgorA 674
		mov dword[esi+png_info_def.hist],0
675
		and dword[esi+png_info_def.valid], not PNG_INFO_hIST
676
	@@:
677
end if
678
679
 
680
	mov eax,[mask]
681
	and eax,PNG_FREE_PLTE
682
	and eax,[esi+png_info_def.free_me]
683
	jz @f ;if (..!=0)
6779 IgorA 684
		stdcall png_free, edi, [esi+png_info_def.palette]
6733 IgorA 685
		mov dword[esi+png_info_def.palette],0
686
		and dword[esi+png_info_def.valid],not PNG_INFO_PLTE
687
		mov dword[esi+png_info_def.num_palette],0
688
	@@:
689
690
 
691
	; Free any image bits attached to the info structure
692
;   if (((mask & PNG_FREE_ROWS) & info_ptr->free_me) != 0)
693
;   {
694
;      if (info_ptr->row_pointers != 0)
695
;      {
696
;         uint_32 row;
697
;         for (row = 0; row < info_ptr->height; row++)
698
;            png_free(png_ptr, info_ptr->row_pointers[row]);
699
700
 
701
;         info_ptr->row_pointers = NULL;
702
;      }
703
;      info_ptr->valid &= ~PNG_INFO_IDAT;
704
;   }
705
end if
706
707
 
708
;      mask &= ~PNG_FREE_MUL;
709
710
 
711
	not eax
712
	and [esi+png_info_def.free_me],eax
713
.end_f:
714
	ret
715
endp
716
717
 
718
; functions.  The application should free any memory associated with this
719
; pointer before png_write_destroy() or png_read_destroy() are called.
720
721
 
722
align 4
723
proc png_get_io_ptr, png_ptr:dword
724
	mov eax,[png_ptr]
725
	cmp eax,0
726
	je @f ;if (..==0) return 0
727
		mov eax,[eax+png_struct.io_ptr]
728
	@@:
729
	ret
730
endp
731
732
 
733
; Initialize the default input/output functions for the PNG file.  If you
734
; use your own read or write routines, you can call either png_set_read_fn()
735
; or png_set_write_fn() instead of png_init_io().  If you have defined
736
; PNG_NO_STDIO or otherwise disabled PNG_STDIO_SUPPORTED, you must use a
737
; function of your own because "FILE *" isn't necessarily available.
738
739
 
740
align 4
741
proc png_init_io uses eax edi, png_ptr:dword, fp:dword
742
	png_debug 1, 'in png_init_io'
743
744
 
745
	cmp edi,0
746
	je @f ;if (..==0) return
747
		mov eax,[fp]
748
		mov [edi+png_struct.io_ptr],eax
749
	@@:
750
	ret
751
endp
752
753
 
754
; defines a cast of a signed integer to an unsigned integer either to preserve
755
; the value, if it is positive, or to calculate:
756
757
 
758
759
 
760
; negative integral value is added the result will be an unsigned value
761
; correspnding to the 2's complement representation.
762
763
 
764
align 4
765
proc png_save_int_32, buf:dword, i:dword
766
	stdcall png_save_uint_32, [buf], [i]
767
	ret
768
endp
769
770
 
771
; Convert the supplied time into an RFC 1123 string suitable for use in
772
; a "Creation Time" or other text-based time string.
773
774
 
775
align 4
776
short_months db 'Jan',0, 'Feb',0, 'Mar',0, 'Apr',0, 'May',0, 'Jun',0,\
777
	'Jul',0, 'Aug',0, 'Sep',0, 'Oct',0, 'Nov',0, 'Dec',0
778
779
 
780
proc png_convert_to_rfc1123_buffer, out_29:dword, ptime:dword
781
	cmp dword[out_29],0
782
	jne @f
783
		xor eax,eax
784
		jmp .end_f ;if (..==0) return 0
785
	@@:
786
787
 
788
;       ptime->month == 0    ||  ptime->month > 12  ||
789
;       ptime->day   == 0    ||  ptime->day   > 31  ||
790
;       ptime->hour  > 23    ||  ptime->minute > 59 ||
791
;       ptime->second > 60)
792
;      return 0;
793
794
 
795
;      size_t pos = 0;
796
;      char number_buf[5]; /* enough for a four-digit year */
797
798
 
799
;#     define APPEND_NUMBER(format, value)\
800
;         APPEND_STRING(PNG_FORMAT_NUMBER(number_buf, format, (value)))
801
;#     define APPEND(ch) if (pos < 28) out_29[pos++] = (ch)
802
803
 
804
;      APPEND(' ');
805
;      APPEND_STRING(short_months[(ptime->month - 1)]);
806
;      APPEND(' ');
807
;      APPEND_NUMBER(PNG_NUMBER_FORMAT_u, ptime->year);
808
;      APPEND(' ');
809
;      APPEND_NUMBER(PNG_NUMBER_FORMAT_02u, (unsigned)ptime->hour);
810
;      APPEND(':');
811
;      APPEND_NUMBER(PNG_NUMBER_FORMAT_02u, (unsigned)ptime->minute);
812
;      APPEND(':');
813
;      APPEND_NUMBER(PNG_NUMBER_FORMAT_02u, (unsigned)ptime->second);
814
;      APPEND_STRING(" +0000"); /* This reliably terminates the buffer */
815
816
 
817
;#     undef APPEND_NUMBER
818
;#     undef APPEND_STRING
819
;   }
820
821
 
822
	inc eax
823
.end_f:
824
	ret
825
endp
826
827
 
828
; To do: remove the following from libpng-1.7
829
; Original API that uses a private buffer in png_struct.
830
; Deprecated because it causes png_struct to carry a spurious temporary
831
; buffer (png_struct::time_buffer), better to have the caller pass this in.
832
833
 
834
align 4
835
proc png_convert_to_rfc1123, png_ptr:dword, ptime:dword
836
;   if (png_ptr != NULL)
837
;   {
838
	; The only failure above if png_ptr != NULL is from an invalid ptime
839
;      if (png_convert_to_rfc1123_buffer(png_ptr->time_buffer, ptime) == 0)
840
;         png_warning(png_ptr, "Ignoring invalid time value");
841
842
 
843
;         return png_ptr->time_buffer;
844
;   }
845
846
 
847
	ret
848
endp
849
;#    endif /* LIBPNG_VER < 10700 */
850
;#  endif /* TIME_RFC1123 */
851
852
 
853
854
 
855
align 4
856
proc png_get_copyright, png_ptr:dword
857
jmp .end_0
858
@@: db 'libpng version 1.6.25 - September 1, 2016',13,10,\
859
	'      Copyright (c) 1998-2002,2004,2006-2016 Glenn Randers-Pehrson',13,10,\
860
	'      Copyright (c) 1996-1997 Andreas Dilger',13,10,\
861
	'      Copyright (c) 1995-1996 Guy Eric Schalnat, Group 42, Inc.',0
862
.end_0:
863
	mov eax,@b
864
	ret
865
endp
866
867
 
868
; format 1.0.0 through 99.99.99zz.  To get the version of *.inc files
869
; used with your application, print out PNG_LIBPNG_VER_STRING, which
870
; is defined in png.inc.
871
; Note: now there is no difference between png_get_libpng_ver() and
872
; png_get_header_ver().  Due to the version_nn_nn_nn typedef guard,
873
; it is guaranteed that png.asm uses the correct version of png.inc.
874
875
 
876
align 4
877
proc png_get_libpng_ver, png_ptr:dword
878
	; Version of *.asm files used when building libpng
879
;   return png_get_header_ver(png_ptr);
880
	ret
881
endp
882
883
 
884
align 4
885
proc png_get_header_ver, png_ptr:dword
886
	; Version of *.inc files used when building libpng
887
;   return PNG_LIBPNG_VER_STRING;
888
	ret
889
endp
890
891
 
892
align 4
893
proc png_get_header_version, png_ptr:dword
894
	; Returns longer string containing both version and date
895
;if __STDC__
896
;   return PNG_HEADER_VERSION_STRING
897
;#  ifndef PNG_READ_SUPPORTED
898
;      " (NO READ SUPPORT)"
899
;#  endif
900
;      PNG_STRING_NEWLINE;
901
;#else
902
;   return PNG_HEADER_VERSION_STRING;
903
;end if
904
	ret
905
endp
906
907
 
908
; Build a grayscale palette.  Palette is assumed to be 1 << bit_depth
909
; large of png_color.  This lets grayscale images be treated as
910
; paletted.  Most useful for gamma correction and simplification
911
; of code.  This API is not used internally.
912
913
 
914
align 4
915
proc png_build_grayscale_palette, bit_depth:dword, palette:dword
916
;   int num_palette;
917
;   int color_inc;
918
;   int i;
919
;   int v;
920
921
 
922
923
 
924
;      return;
925
926
 
927
;   {
928
;      case 1:
929
;         num_palette = 2;
930
;         color_inc = 0xff;
931
;         break;
932
;
933
;      case 2:
934
;         num_palette = 4;
935
;         color_inc = 0x55;
936
;         break;
937
;
938
;      case 4:
939
;         num_palette = 16;
940
;         color_inc = 0x11;
941
;         break;
942
;
943
;      case 8:
944
;         num_palette = 256;
945
;         color_inc = 1;
946
;         break;
947
;
948
;      default:
949
;         num_palette = 0;
950
;         color_inc = 0;
951
;         break;
952
;   }
953
;
954
;   for (i = 0, v = 0; i < num_palette; i++, v += color_inc)
955
;   {
956
;      palette[i].red = (byte)(v & 0xff);
957
;      palette[i].green = (byte)(v & 0xff);
958
;      palette[i].blue = (byte)(v & 0xff);
959
;   }
960
	ret
961
endp
962
963
 
964
align 4
965
proc png_handle_as_unknown uses ecx edi esi, png_ptr:dword, chunk_name:dword
966
	; Check chunk_name and return "keep" value if it's on the list, else 0
967
;   bytep p, p_end;
968
969
 
970
	cmp edi,0
971
	je .end0
972
	cmp dword[chunk_name],0
973
	je .end0
974
	cmp dword[edi+png_struct.num_chunk_list],0
975
	je .end0
976
		jmp @f
977
	.end0: ;if (..==0 || ..==0 || ..==0)
978
		mov eax,PNG_HANDLE_CHUNK_AS_DEFAULT
979
		jmp .end_f
980
	@@:
981
982
 
983
;   p = p_end + png_ptr->num_chunk_list*5; /* beyond end */
984
985
 
986
	; code was always searched from the end of the list, this is no longer
987
	; necessary because the 'set' routine handles duplicate entries correcty.
988
989
 
990
;   {
991
;      p -= 5;
992
993
 
994
;         return p[4];
995
;   }
996
;   while (p > p_end);
997
998
 
999
	; be handled according to the value of png_ptr->unknown_default; this can be
1000
	; confusing because, as a result, there are two levels of defaulting for
1001
	; unknown chunks.
1002
1003
 
1004
.end_f:
1005
	ret
1006
endp
1007
1008
 
1009
align 4
1010
proc png_chunk_unknown_handling, png_ptr:dword, chunk_name:dword
1011
;   byte chunk_string[5];
1012
1013
 
1014
;   return png_handle_as_unknown(png_ptr, chunk_string);
1015
	ret
1016
endp
1017
1018
 
1019
;int (png_structrp png_ptr)
1020
align 4
1021
proc png_reset_zstream, png_ptr:dword
1022
	mov eax,[png_ptr]
1023
	cmp eax,0
1024
	jne @f ;if (..==0)
1025
		mov eax,Z_STREAM_ERROR
1026
		jmp .end_f
1027
	@@:
1028
	; WARNING: this resets the window bits to the maximum!
1029
	add eax,png_struct.zstream
1030
	stdcall inflateReset,eax
1031
.end_f:
1032
	ret
1033
endp
1034
1035
 
1036
;uint_32 png_access_version_number(void)
1037
align 4
1038
png_access_version_number:
1039
	; Version of *.asm files used when building libpng
1040
	mov eax,PNG_LIBPNG_VER
1041
	ret
1042
1043
 
1044
; Ensure that png_ptr->zstream.msg holds some appropriate error message string.
1045
; If it doesn't 'ret' is used to set it to something appropriate, even in cases
1046
; like Z_OK or Z_STREAM_END where the error code is apparently a success code.
1047
1048
 
1049
align 4
1050
proc png_zstream_error uses eax edi, png_ptr:dword, p2ret:dword
1051
	; Translate 'p2ret' into an appropriate error string, priority is given to the
1052
	; one in zstream if set.  This always returns a string, even in cases like
1053
	; Z_OK or Z_STREAM_END where the error code is a success code.
1054
1055
 
1056
	cmp dword[edi+png_struct.zstream.msg],0
1057
	jne .end_f ;if (..==0) switch (p2ret)
1058
		mov eax,[p2ret]
1059
;      default:
1060
		cmp eax,Z_OK
1061
		jne @f
1062
			cStr dword[edi+png_struct.zstream.msg],'unexpected zlib return code'
1063
			jmp .end_f
1064
		@@:
1065
		cmp eax,Z_STREAM_END
1066
		jne @f
1067
			; Normal exit
1068
			cStr dword[edi+png_struct.zstream.msg],'unexpected end of LZ stream'
1069
			jmp .end_f
1070
		@@:
1071
		cmp eax,Z_NEED_DICT
1072
		jne @f
1073
			; This means the deflate stream did not have a dictionary; this
1074
			; indicates a bogus PNG.
1075
1076
 
1077
			jmp .end_f
1078
		@@:
1079
		cmp eax,Z_ERRNO
1080
		jne @f
1081
			; gz APIs only: should not happen
1082
			cStr dword[edi+png_struct.zstream.msg],'zlib IO error'
1083
			jmp .end_f
1084
		@@:
1085
		cmp eax,Z_STREAM_ERROR
1086
		jne @f
1087
			; internal libpng error
1088
			cStr dword[edi+png_struct.zstream.msg],'bad parameters to zlib'
1089
			jmp .end_f
1090
		@@:
1091
		cmp eax,Z_DATA_ERROR
1092
		jne @f
1093
			cStr dword[edi+png_struct.zstream.msg],'damaged LZ stream'
1094
			jmp .end_f
1095
		@@:
1096
		cmp eax,Z_MEM_ERROR
1097
		jne @f
1098
			cStr dword[edi+png_struct.zstream.msg],'insufficient memory'
1099
			jmp .end_f
1100
		@@:
1101
		cmp eax,Z_BUF_ERROR
1102
		jne @f
1103
			; End of input or output; not a problem if the caller is doing
1104
			; incremental read or write.
1105
1106
 
1107
			jmp .end_f
1108
		@@:
1109
		cmp eax,Z_VERSION_ERROR
1110
		jne @f
1111
			cStr dword[edi+png_struct.zstream.msg],'unsupported zlib version'
1112
			jmp .end_f
1113
		@@:
1114
		cmp eax,PNG_UNEXPECTED_ZLIB_RETURN
1115
		jne .end_f
1116
			; Compile errors here mean that zlib now uses the value co-opted in
1117
			; pngpriv.inc for PNG_UNEXPECTED_ZLIB_RETURN; update the switch above
1118
			; and change pngpriv.inc.  Note that this message is "... return",
1119
			; whereas the default/Z_OK one is "... return code".
1120
1121
 
1122
;         break;
1123
.end_f:
1124
	ret
1125
endp
1126
1127
 
1128
; at libpng 1.5.5!
1129
1130
 
1131
 
1132
;if PNG_GAMMA_SUPPORTED /* always set if COLORSPACE */
1133
;int (png_structrp png_ptr,
1134
;    png_colorspacerp colorspace, png_fixed_point gAMA, int from)
1135
	; This is called to check a new gamma value against an existing one.  The
1136
	; routine returns false if the new gamma value should not be written.
1137
	;
1138
	; 'from' says where the new gamma value comes from:
1139
	;
1140
	;    0: the new gamma value is the libpng estimate for an ICC profile
1141
	;    1: the new gamma value comes from a gAMA chunk
1142
	;    2: the new gamma value comes from an sRGB chunk
1143
1144
 
1145
proc png_colorspace_check_gamma, png_ptr:dword, colorspace:dword, gAMA:dword, from:dword
1146
;   png_fixed_point gtest;
1147
;
1148
;   if ((colorspace->flags & PNG_COLORSPACE_HAVE_GAMMA) != 0 &&
1149
;       (png_muldiv(>est, colorspace->gamma, PNG_FP_1, gAMA) == 0  ||
1150
;      png_gamma_significant(gtest) != 0))
1151
;   {
1152
	; Either this is an sRGB image, in which case the calculated gamma
1153
	; approximation should match, or this is an image with a profile and the
1154
	; value libpng calculates for the gamma of the profile does not match the
1155
	; value recorded in the file.  The former, sRGB, case is an error, the
1156
	; latter is just a warning.
1157
1158
 
1159
;      {
1160
;         png_chunk_report(png_ptr, "gamma value does not match sRGB",
1161
;             PNG_CHUNK_ERROR);
1162
;         /* Do not overwrite an sRGB value */
1163
;         return from == 2;
1164
;      }
1165
1166
 
1167
;      {
1168
;         png_chunk_report(png_ptr, "gamma value does not match libpng estimate",
1169
;             PNG_CHUNK_WARNING);
1170
;         return from == 1;
1171
;      }
1172
;   }
1173
1174
 
1175
	ret
1176
endp
1177
1178
 
1179
align 4
1180
proc png_colorspace_set_gamma, png_ptr:dword, colorspace:dword, gAMA:dword
1181
	; Changed in libpng-1.5.4 to limit the values to ensure overflow can't
1182
	; occur.  Since the fixed point representation is asymetrical it is
1183
	; possible for 1/gamma to overflow the limit of 21474 and this means the
1184
	; gamma value must be at least 5/100000 and hence at most 20000.0.  For
1185
	; safety the limits here are a little narrower.  The values are 0.00016 to
1186
	; 6250.0, which are truly ridiculous gamma values (and will produce
1187
	; displays that are all black or all white.)
1188
1189
 
1190
	; handling code, which only required the value to be >0.
1191
1192
 
1193
1194
 
1195
;      errmsg = "gamma value out of range";
1196
1197
 
1198
	; Allow the application to set the gamma value more than once
1199
;   else if ((png_ptr->mode & PNG_IS_READ_STRUCT) != 0 &&
1200
;      (colorspace->flags & PNG_COLORSPACE_FROM_gAMA) != 0)
1201
;      errmsg = "duplicate";
1202
;#  endif
1203
1204
 
1205
;   else if ((colorspace->flags & PNG_COLORSPACE_INVALID) != 0)
1206
;      return;
1207
1208
 
1209
;   {
1210
;      if (png_colorspace_check_gamma(png_ptr, colorspace, gAMA,
1211
;          1/*from gAMA*/) != 0)
1212
;      {
1213
;         /* Store this gamma value. */
1214
;         colorspace->gamma = gAMA;
1215
;         colorspace->flags |=
1216
;            (PNG_COLORSPACE_HAVE_GAMMA | PNG_COLORSPACE_FROM_gAMA);
1217
;      }
1218
1219
 
1220
	; not updated however the colorspace is not invalidated.  This
1221
	; corresponds to the case where the existing gamma comes from an sRGB
1222
	; chunk or profile.  An error message has already been output.
1223
1224
 
1225
;   }
1226
1227
 
1228
;   colorspace->flags |= PNG_COLORSPACE_INVALID;
1229
;   png_chunk_report(png_ptr, errmsg, PNG_CHUNK_WRITE_ERROR);
1230
.end_f:
1231
	ret
1232
endp
1233
1234
 
1235
align 4
1236
proc png_colorspace_sync_info uses eax esi, png_ptr:dword, info_ptr:dword
1237
	mov esi,[info_ptr]
1238
	mov ax,[esi+png_info_def.colorspace.flags]
1239
	and ax,PNG_COLORSPACE_INVALID
1240
	cmp ax,0
1241
	je @f ;if (..!=0)
1242
		; Everything is invalid
1243
		and dword[esi+png_info_def.valid], not (PNG_INFO_gAMA or PNG_INFO_cHRM or PNG_INFO_sRGB or PNG_INFO_iCCP)
1244
1245
 
1246
		; Clean up the iCCP profile now if it won't be used.
1247
		stdcall png_free_data, [png_ptr], esi, PNG_FREE_ICCP, -1 ;not used
1248
end if
1249
		jmp .end0
1250
	@@: ;else
1251
if PNG_COLORSPACE_SUPPORTED eq 1
1252
		; Leave the INFO_iCCP flag set if the pngset.c code has already set
1253
		; it; this allows a PNG to contain a profile which matches sRGB and
1254
		; yet still have that profile retrievable by the application.
1255
1256
 
1257
		and ax,PNG_COLORSPACE_MATCHES_sRGB
1258
		cmp ax,0
1259
		je @f ;if (..!=0)
1260
			or dword[esi+png_info_def.valid], PNG_INFO_sRGB
1261
			jmp .end1
1262
		@@: ;else
1263
			and dword[esi+png_info_def.valid], not PNG_INFO_sRGB
1264
		.end1:
1265
		mov ax,[esi+png_info_def.colorspace.flags]
1266
		and ax,PNG_COLORSPACE_HAVE_ENDPOINTS
1267
		cmp ax,0
1268
		je @f ;if (..!=0)
1269
			or dword[esi+png_info_def.valid], PNG_INFO_cHRM
1270
			jmp .end2
1271
		@@: ;else
1272
			and dword[esi+png_info_def.valid], not PNG_INFO_cHRM
1273
		.end2:
1274
end if
1275
1276
 
1277
		and ax,PNG_COLORSPACE_HAVE_GAMMA
1278
		cmp ax,0
1279
		je @f ;if (..!=0)
1280
			or dword[esi+png_info_def.valid], PNG_INFO_gAMA
1281
			jmp .end0
1282
		@@: ;else
1283
			and dword[esi+png_info_def.valid], not PNG_INFO_gAMA
1284
	.end0:
1285
	ret
1286
endp
1287
1288
 
1289
align 4
1290
proc png_colorspace_sync uses ecx edi esi, png_ptr:dword, info_ptr:dword
1291
	mov edi,[info_ptr]
1292
	cmp edi,0
1293
	je @f ;if (..==0) ;reduce code size; check here not in the caller
1294
		mov ecx,sizeof.png_colorspace
1295
		mov esi,[png_ptr]
1296
		mov esi,[esi+png_struct.colorspace]
1297
		mov edi,[edi+png_info_def.colorspace]
1298
		rep movsb
1299
		stdcall png_colorspace_sync_info, [png_ptr], [info_ptr]
1300
	@@:
1301
	ret
1302
endp
1303
1304
 
1305
1306
 
1307
; Added at libpng-1.5.5 to support read and write of true CIEXYZ values for
1308
; cHRM, as opposed to using chromaticities.  These internal APIs return
1309
; non-zero on a parameter error.  The X, Y and Z values are required to be
1310
; positive and less than 1.0.
1311
1312
 
1313
align 4
1314
proc png_xy_from_XYZ, xy:dword, XYZ:dword
1315
;   int_32 d, dwhite, whiteX, whiteY;
1316
1317
 
1318
;   if (png_muldiv(&xy->redx, XYZ->red_X, PNG_FP_1, d) == 0)
1319
;      return 1;
1320
;   if (png_muldiv(&xy->redy, XYZ->red_Y, PNG_FP_1, d) == 0)
1321
;      return 1;
1322
;   dwhite = d;
1323
;   whiteX = XYZ->red_X;
1324
;   whiteY = XYZ->red_Y;
1325
1326
 
1327
;   if (png_muldiv(&xy->greenx, XYZ->green_X, PNG_FP_1, d) == 0)
1328
;      return 1;
1329
;   if (png_muldiv(&xy->greeny, XYZ->green_Y, PNG_FP_1, d) == 0)
1330
;      return 1;
1331
;   dwhite += d;
1332
;   whiteX += XYZ->green_X;
1333
;   whiteY += XYZ->green_Y;
1334
1335
 
1336
;   if (png_muldiv(&xy->bluex, XYZ->blue_X, PNG_FP_1, d) == 0)
1337
;      return 1;
1338
;   if (png_muldiv(&xy->bluey, XYZ->blue_Y, PNG_FP_1, d) == 0)
1339
;      return 1;
1340
;   dwhite += d;
1341
;   whiteX += XYZ->blue_X;
1342
;   whiteY += XYZ->blue_Y;
1343
1344
 
1345
	; thus:
1346
1347
 
1348
;      return 1;
1349
;   if (png_muldiv(&xy->whitey, whiteY, PNG_FP_1, dwhite) == 0)
1350
;      return 1;
1351
1352
 
1353
	ret
1354
endp
1355
1356
 
1357
align 4
1358
proc png_XYZ_from_xy, XYZ:dword, xy:dword
1359
;   png_fixed_point red_inverse, green_inverse, blue_scale;
1360
;   png_fixed_point left, right, denominator;
1361
1362
 
1363
	; have end points with 0 tristimulus values (these are impossible end
1364
	; points, but they are used to cover the possible colors).  We check
1365
	; xy->whitey against 5, not 0, to avoid a possible integer overflow.
1366
1367
 
1368
;   if (xy->redy   < 0 || xy->redy > PNG_FP_1-xy->redx) return 1;
1369
;   if (xy->greenx < 0 || xy->greenx > PNG_FP_1) return 1;
1370
;   if (xy->greeny < 0 || xy->greeny > PNG_FP_1-xy->greenx) return 1;
1371
;   if (xy->bluex  < 0 || xy->bluex > PNG_FP_1) return 1;
1372
;   if (xy->bluey  < 0 || xy->bluey > PNG_FP_1-xy->bluex) return 1;
1373
;   if (xy->whitex < 0 || xy->whitex > PNG_FP_1) return 1;
1374
;   if (xy->whitey < 5 || xy->whitey > PNG_FP_1-xy->whitex) return 1;
1375
1376
 
1377
	; value had 9 independent values (red,green,blue)x(X,Y,Z) however only 8
1378
	; derived values were recorded in the cHRM chunk;
1379
	; (red,green,blue,white)x(x,y).  This loses one degree of freedom and
1380
	; therefore an arbitrary ninth value has to be introduced to undo the
1381
	; original transformations.
1382
1383
 
1384
	; chromaticity values (c) have the property:
1385
1386
 
1387
	;   c = ---------
1388
	;       X + Y + Z
1389
1390
 
1391
	; three chromaticity values (x,y,z) for each end-point obey the
1392
	; relationship:
1393
1394
 
1395
1396
 
1397
	; value 1.0; call this the chromaticity plane.  Thus the chromaticity
1398
	; calculation has scaled each end-point so that it is on the x+y+z=1 plane
1399
	; and chromaticity is the intersection of the vector from the origin to the
1400
	; (X,Y,Z) value with the chromaticity plane.
1401
1402
 
1403
	; end-point scale factors, (red-scale, green-scale, blue-scale), but these
1404
	; were not recorded.  Instead we calculated the reference white (X,Y,Z) and
1405
	; recorded the chromaticity of this.  The reference white (X,Y,Z) would have
1406
	; given all three of the scale factors since:
1407
1408
 
1409
	;    white-C = red-C + green-C + blue-C
1410
	;            = red-c*red-scale + green-c*green-scale + blue-c*blue-scale
1411
1412
 
1413
	; factor:
1414
1415
 
1416
1417
 
1418
	; about white-scale:
1419
1420
 
1421
	;    Hence:  white-scale = 1/white-y
1422
	;    Or:     red-Y + green-Y + blue-Y = 1.0
1423
1424
 
1425
	; the nine values we want to calculate.  8 more equations come from the
1426
	; above routine as summarised at the top above (the chromaticity
1427
	; calculation):
1428
1429
 
1430
	;    Hence: (color-x - 1)*color-X + color.x*color-Y + color.x*color-Z = 0
1431
1432
 
1433
	; solved by Cramer's rule.  Cramer's rule requires calculating 10 9x9 matrix
1434
	; determinants, however this is not as bad as it seems because only 28 of
1435
	; the total of 90 terms in the various matrices are non-zero.  Nevertheless
1436
	; Cramer's rule is notoriously numerically unstable because the determinant
1437
	; calculation involves the difference of large, but similar, numbers.  It is
1438
	; difficult to be sure that the calculation is stable for real world values
1439
	; and it is certain that it becomes unstable where the end points are close
1440
	; together.
1441
1442
 
1443
	; understandable and totally obvious approach of calculating color-scale.
1444
1445
 
1446
	; (1/white-y), so we can immediately see that as white-y approaches 0 the
1447
	; accuracy inherent in the cHRM chunk drops off substantially.
1448
1449
 
1450
	; ------------------------------------------------------------
1451
1452
 
1453
	;    white-X = white-x * white-scale
1454
	;    white-Y = 1.0
1455
	;    white-Z = (1 - white-x - white-y) * white_scale
1456
1457
 
1458
	;            = red-c*red-scale + green-c*green-scale + blue-c*blue-scale
1459
1460
 
1461
	; all the coefficients are now known:
1462
1463
 
1464
	;       = white-x/white-y
1465
	;    red-y*red-scale + green-y*green-scale + blue-y*blue-scale = 1
1466
	;    red-z*red-scale + green-z*green-scale + blue-z*blue-scale
1467
	;       = (1 - white-x - white-y)/white-y
1468
1469
 
1470
	; three equations together to get an alternative third:
1471
1472
 
1473
1474
 
1475
	; 3x3 - far more tractible.  Unfortunately 3x3 determinants still involve
1476
	; multiplication of three coefficients so we can't guarantee to avoid
1477
	; overflow in the libpng fixed point representation.  Using Cramer's rule in
1478
	; floating point is probably a good choice here, but it's not an option for
1479
	; fixed point.  Instead proceed to simplify the first two equations by
1480
	; eliminating what is likely to be the largest value, blue-scale:
1481
1482
 
1483
1484
 
1485
1486
 
1487
	;                (white-x - blue-x)*white-scale
1488
1489
 
1490
	;                1 - blue-y*white-scale
1491
1492
 
1493
1494
 
1495
	;                (white-x - blue-x)*white-scale - (red-x - blue-x)*red-scale
1496
	;                -----------------------------------------------------------
1497
	;                                  green-x - blue-x
1498
1499
 
1500
	;                1 - blue-y*white-scale - (green-y - blue-y) * green-scale
1501
	;                ---------------------------------------------------------
1502
	;                                  red-y - blue-y
1503
1504
 
1505
1506
 
1507
	;          ( (green-x - blue-x) * (white-y - blue-y) -
1508
	;            (green-y - blue-y) * (white-x - blue-x) ) / white-y
1509
	; -------------------------------------------------------------------------
1510
	;  (green-x - blue-x)*(red-y - blue-y)-(green-y - blue-y)*(red-x - blue-x)
1511
1512
 
1513
	;          ( (red-y - blue-y) * (white-x - blue-x) -
1514
	;            (red-x - blue-x) * (white-y - blue-y) ) / white-y
1515
	; -------------------------------------------------------------------------
1516
	;  (green-x - blue-x)*(red-y - blue-y)-(green-y - blue-y)*(red-x - blue-x)
1517
1518
 
1519
	; The input values have 5 decimal digits of accuracy.  The values are all in
1520
	; the range 0 < value < 1, so simple products are in the same range but may
1521
	; need up to 10 decimal digits to preserve the original precision and avoid
1522
	; underflow.  Because we are using a 32-bit signed representation we cannot
1523
	; match this; the best is a little over 9 decimal digits, less than 10.
1524
1525
 
1526
	; signed representation.  Because the red-scale calculation above uses the
1527
	; difference between two products of values that must be in the range -1..+1
1528
	; it is sufficient to divide the product by 7; ceil(100,000/32767*2).  The
1529
	; factor is irrelevant in the calculation because it is applied to both
1530
	; numerator and denominator.
1531
1532
 
1533
	; chromaticities in the above equations tend to be small, for example for
1534
	; the sRGB chromaticities they are:
1535
1536
 
1537
	; green numerator:  -0.08788
1538
	; denominator:      -0.2241 (without white-y multiplication)
1539
1540
 
1541
	;  color space definitions are (to 15 decimal places):
1542
1543
 
1544
	;    0.212639005871510 0.715168678767756 0.072192315360734
1545
	;  Kodak ProPhoto
1546
	;    0.288071128229293 0.711843217810102 0.000085653960605
1547
	;  Adobe RGB
1548
	;    0.297344975250536 0.627363566255466 0.075291458493998
1549
	;  Adobe Wide Gamut RGB
1550
	;    0.258728243040113 0.724682314948566 0.016589442011321
1551
1552
 
1553
	; value of 2 indicates an internal error to the caller.
1554
1555
 
1556
;      return 2;
1557
;   if (png_muldiv(&right, xy->greeny-xy->bluey, xy->redx - xy->bluex, 7) == 0)
1558
;      return 2;
1559
;   denominator = left - right;
1560
1561
 
1562
;   if (png_muldiv(&left, xy->greenx-xy->bluex, xy->whitey-xy->bluey, 7) == 0)
1563
;      return 2;
1564
;   if (png_muldiv(&right, xy->greeny-xy->bluey, xy->whitex-xy->bluex, 7) == 0)
1565
;      return 2;
1566
1567
 
1568
	; chunk values.  This calculation actually returns the reciprocal of the
1569
	; scale value because this allows us to delay the multiplication of white-y
1570
	; into the denominator, which tends to produce a small number.
1571
1572
 
1573
;       red_inverse <= xy->whitey /* r+g+b scales = white scale */)
1574
;      return 1;
1575
1576
 
1577
;   if (png_muldiv(&left, xy->redy-xy->bluey, xy->whitex-xy->bluex, 7) == 0)
1578
;      return 2;
1579
;   if (png_muldiv(&right, xy->redx-xy->bluex, xy->whitey-xy->bluey, 7) == 0)
1580
;      return 2;
1581
;   if (png_muldiv(&green_inverse, xy->whitey, denominator, left-right) == 0 ||
1582
;       green_inverse <= xy->whitey)
1583
;      return 1;
1584
1585
 
1586
	; can still produce 0 for extreme cHRM values.
1587
1588
 
1589
;       png_reciprocal(green_inverse);
1590
;   if (blue_scale <= 0)
1591
;      return 1;
1592
1593
 
1594
 
1595
;   if (png_muldiv(&XYZ->red_X, xy->redx, PNG_FP_1, red_inverse) == 0)
1596
;      return 1;
1597
;   if (png_muldiv(&XYZ->red_Y, xy->redy, PNG_FP_1, red_inverse) == 0)
1598
;      return 1;
1599
;   if (png_muldiv(&XYZ->red_Z, PNG_FP_1 - xy->redx - xy->redy, PNG_FP_1,
1600
;       red_inverse) == 0)
1601
;      return 1;
1602
1603
 
1604
;      return 1;
1605
;   if (png_muldiv(&XYZ->green_Y, xy->greeny, PNG_FP_1, green_inverse) == 0)
1606
;      return 1;
1607
;   if (png_muldiv(&XYZ->green_Z, PNG_FP_1 - xy->greenx - xy->greeny, PNG_FP_1,
1608
;       green_inverse) == 0)
1609
;      return 1;
1610
1611
 
1612
;      return 1;
1613
;   if (png_muldiv(&XYZ->blue_Y, xy->bluey, blue_scale, PNG_FP_1) == 0)
1614
;      return 1;
1615
;   if (png_muldiv(&XYZ->blue_Z, PNG_FP_1 - xy->bluex - xy->bluey, blue_scale,
1616
;       PNG_FP_1) == 0)
1617
;      return 1;
1618
1619
 
1620
	ret
1621
endp
1622
1623
 
1624
align 4
1625
proc png_XYZ_normalize, XYZ:dword
1626
;   int_32 Y;
1627
1628
 
1629
;      XYZ->red_X < 0 || XYZ->green_X < 0 || XYZ->blue_X < 0 ||
1630
;      XYZ->red_Z < 0 || XYZ->green_Z < 0 || XYZ->blue_Z < 0)
1631
;      return 1;
1632
1633
 
1634
	; IMPLEMENTATION NOTE: ANSI requires signed overflow not to occur, therefore
1635
	; relying on addition of two positive values producing a negative one is not
1636
	; safe.
1637
1638
 
1639
;   if (0x7fffffff - Y < XYZ->green_X)
1640
;      return 1;
1641
;   Y += XYZ->green_Y;
1642
;   if (0x7fffffff - Y < XYZ->blue_X)
1643
;      return 1;
1644
;   Y += XYZ->blue_Y;
1645
1646
 
1647
;   {
1648
;      if (png_muldiv(&XYZ->red_X, XYZ->red_X, PNG_FP_1, Y) == 0)
1649
;         return 1;
1650
;      if (png_muldiv(&XYZ->red_Y, XYZ->red_Y, PNG_FP_1, Y) == 0)
1651
;         return 1;
1652
;      if (png_muldiv(&XYZ->red_Z, XYZ->red_Z, PNG_FP_1, Y) == 0)
1653
;         return 1;
1654
1655
 
1656
;         return 1;
1657
;      if (png_muldiv(&XYZ->green_Y, XYZ->green_Y, PNG_FP_1, Y) == 0)
1658
;         return 1;
1659
;      if (png_muldiv(&XYZ->green_Z, XYZ->green_Z, PNG_FP_1, Y) == 0)
1660
;         return 1;
1661
1662
 
1663
;         return 1;
1664
;      if (png_muldiv(&XYZ->blue_Y, XYZ->blue_Y, PNG_FP_1, Y) == 0)
1665
;         return 1;
1666
;      if (png_muldiv(&XYZ->blue_Z, XYZ->blue_Z, PNG_FP_1, Y) == 0)
1667
;         return 1;
1668
;   }
1669
1670
 
1671
	ret
1672
endp
1673
1674
 
1675
align 4
1676
proc png_colorspace_endpoints_match, xy1:dword, xy2:dword, delta:dword
1677
	; Allow an error of +/-0.01 (absolute value) on each chromaticity
1678
;   if (PNG_OUT_OF_RANGE(xy1->whitex, xy2->whitex,delta) ||
1679
;       PNG_OUT_OF_RANGE(xy1->whitey, xy2->whitey,delta) ||
1680
;       PNG_OUT_OF_RANGE(xy1->redx,   xy2->redx,  delta) ||
1681
;       PNG_OUT_OF_RANGE(xy1->redy,   xy2->redy,  delta) ||
1682
;       PNG_OUT_OF_RANGE(xy1->greenx, xy2->greenx,delta) ||
1683
;       PNG_OUT_OF_RANGE(xy1->greeny, xy2->greeny,delta) ||
1684
;       PNG_OUT_OF_RANGE(xy1->bluex,  xy2->bluex, delta) ||
1685
;       PNG_OUT_OF_RANGE(xy1->bluey,  xy2->bluey, delta))
1686
;      return 0;
1687
;   return 1;
1688
	ret
1689
endp
1690
1691
 
1692
; chunk chromaticities.  Earlier checks used to simply look for the overflow
1693
; condition (where the determinant of the matrix to solve for XYZ ends up zero
1694
; because the chromaticity values are not all distinct.)  Despite this it is
1695
; theoretically possible to produce chromaticities that are apparently valid
1696
; but that rapidly degrade to invalid, potentially crashing, sets because of
1697
; arithmetic inaccuracies when calculations are performed on them.  The new
1698
; check is to round-trip xy -> XYZ -> xy and then check that the result is
1699
; within a small percentage of the original.
1700
1701
 
1702
align 4
1703
proc png_colorspace_check_xy, XYZ:dword, xy:dword
1704
;   int result;
1705
;   png_xy xy_test;
1706
1707
 
1708
;   result = png_XYZ_from_xy(XYZ, xy);
1709
;   if (result != 0)
1710
;      return result;
1711
1712
 
1713
;   if (result != 0)
1714
;      return result;
1715
1716
 
1717
;       5/*actually, the math is pretty accurate*/) != 0)
1718
;      return 0;
1719
1720
 
1721
;   return 1;
1722
	ret
1723
endp
1724
1725
 
1726
; (another side-effect) and the xy chromaticities are returned.
1727
1728
 
1729
align 4
1730
proc png_colorspace_check_XYZ, xy:dword, XYZ:dword
1731
;   int result;
1732
;   png_XYZ XYZtemp;
1733
1734
 
1735
;   if (result != 0)
1736
;      return result;
1737
1738
 
1739
;   if (result != 0)
1740
;      return result;
1741
1742
 
1743
;   return png_colorspace_check_xy(&XYZtemp, xy);
1744
	ret
1745
endp
1746
1747
 
1748
;const png_xy sRGB_xy = /* From ITU-R BT.709-3 */
1749
;   /* color      x       y */
1750
;   /* red   */ 64000, 33000,
1751
;   /* green */ 30000, 60000,
1752
;   /* blue  */ 15000,  6000,
1753
;   /* white */ 31270, 32900
1754
1755
 
1756
;    png_colorspacerp colorspace, const png_xy *xy, const png_XYZ *XYZ,
1757
;    int preferred)
1758
align 4
1759
proc png_colorspace_set_xy_and_XYZ, png_ptr:dword, colorspace:dword, xy:dword, XYZ:dword, preferred:dword
1760
;   if ((colorspace->flags & PNG_COLORSPACE_INVALID) != 0)
1761
;      return 0;
1762
1763
 
1764
	; variations because of the normalization (or not) of the end point Y
1765
	; values.
1766
1767
 
1768
;       (colorspace->flags & PNG_COLORSPACE_HAVE_ENDPOINTS) != 0)
1769
;   {
1770
	; The end points must be reasonably close to any we already have.  The
1771
	; following allows an error of up to +/-.001
1772
1773
 
1774
;          100) == 0)
1775
;      {
1776
;         colorspace->flags |= PNG_COLORSPACE_INVALID;
1777
;         png_benign_error(png_ptr, "inconsistent chromaticities");
1778
;         return 0; /* failed */
1779
;      }
1780
1781
 
1782
;      if (preferred == 0)
1783
;         return 1; /* ok, but no change */
1784
;   }
1785
1786
 
1787
;   colorspace->end_points_XYZ = *XYZ;
1788
;   colorspace->flags |= PNG_COLORSPACE_HAVE_ENDPOINTS;
1789
1790
 
1791
	; on this test.
1792
1793
 
1794
;      colorspace->flags |= PNG_COLORSPACE_ENDPOINTS_MATCH_sRGB;
1795
;
1796
;   else
1797
;      colorspace->flags &= PNG_COLORSPACE_CANCEL(
1798
;         PNG_COLORSPACE_ENDPOINTS_MATCH_sRGB);
1799
1800
 
1801
	ret
1802
endp
1803
1804
 
1805
;    png_colorspacerp colorspace, const png_xy *xy, int preferred)
1806
align 4
1807
proc png_colorspace_set_chromaticities, png_ptr:dword, colorspace:dword, xy:dword, preferred:dword
1808
	; We must check the end points to ensure they are reasonable - in the past
1809
	; color management systems have crashed as a result of getting bogus
1810
	; colorant values, while this isn't the fault of libpng it is the
1811
	; responsibility of libpng because PNG carries the bomb and libpng is in a
1812
	; position to protect against it.
1813
1814
 
1815
1816
 
1817
;   {
1818
;      case 0: /* success */
1819
;         return png_colorspace_set_xy_and_XYZ(png_ptr, colorspace, xy, &XYZ,
1820
;             preferred);
1821
1822
 
1823
	; We can't invert the chromaticities so we can't produce value XYZ
1824
	; values.  Likely as not a color management system will fail too.
1825
1826
 
1827
;         png_benign_error(png_ptr, "invalid chromaticities");
1828
;         break;
1829
;
1830
;      default:
1831
	; libpng is broken; this should be a warning but if it happens we
1832
	; want error reports so for the moment it is an error.
1833
1834
 
1835
;         png_error(png_ptr, "internal error checking chromaticities");
1836
;   }
1837
1838
 
1839
.end_f:
1840
	ret
1841
endp
1842
1843
 
1844
;    png_colorspacerp colorspace, const png_XYZ *XYZ_in, int preferred)
1845
align 4
1846
proc png_colorspace_set_endpoints, png_ptr:dword, colorspace:dword, XYZ_in:dword, preferred:dword
1847
;   png_XYZ XYZ = *XYZ_in;
1848
;   png_xy xy;
1849
1850
 
1851
;   {
1852
;      case 0:
1853
;         return png_colorspace_set_xy_and_XYZ(png_ptr, colorspace, &xy, &XYZ,
1854
;             preferred);
1855
1856
 
1857
	; End points are invalid.
1858
;         colorspace->flags |= PNG_COLORSPACE_INVALID;
1859
;         png_benign_error(png_ptr, "invalid end points");
1860
;         break;
1861
1862
 
1863
;         colorspace->flags |= PNG_COLORSPACE_INVALID;
1864
;         png_error(png_ptr, "internal error checking chromaticities");
1865
;   }
1866
1867
 
1868
.end_f:
1869
	ret
1870
endp
1871
1872
 
1873
;char (uint_32 byte)
1874
align 4
1875
proc png_icc_tag_char, p1byte:dword
1876
	mov eax,[p1byte]
1877
	cmp al,32
1878
	jl @f
1879
	cmp al,126
1880
	jg @f ;if (..>=.. && ..<=..) return
1881
		mov al,'?'
1882
	@@:
1883
	and eax,0xff
1884
	ret
1885
endp
1886
1887
 
1888
align 4
1889
proc png_icc_tag_name uses eax edi, name:dword, tag:dword
1890
	mov edi,[name]
1891
	mov byte[edi],39
1892
	mov byte[edi+5],39
1893
	inc edi
1894
	mov eax,[tag]
1895
	shr eax,24
1896
	stdcall png_icc_tag_char,eax
1897
	stosb
1898
	mov eax,[tag]
1899
	shr eax,16
1900
	stdcall png_icc_tag_char,eax
1901
	stosb
1902
	mov eax,[tag]
1903
	shr eax,8
1904
	stdcall png_icc_tag_char,eax
1905
	stosb
1906
	stdcall png_icc_tag_char,[tag]
1907
	stosb
1908
	ret
1909
endp
1910
1911
 
1912
align 4
1913
proc is_ICC_signature_char, it:dword
1914
;   return it == 32 || (it >= 48 && it <= 57) || (it >= 65 && it <= 90) ||
1915
;      (it >= 97 && it <= 122);
1916
	ret
1917
endp
1918
1919
 
1920
align 4
1921
proc is_ICC_signature, it:dword
1922
;   return is_ICC_signature_char(it >> 24) /* checks all the top bits */ &&
1923
;      is_ICC_signature_char((it >> 16) & 0xff) &&
1924
;      is_ICC_signature_char((it >> 8) & 0xff) &&
1925
;      is_ICC_signature_char(it & 0xff);
1926
	ret
1927
endp
1928
1929
 
1930
;    charp name, png_alloc_size_t value, charp reason)
1931
align 4
1932
proc png_icc_profile_error, png_ptr:dword, colorspace:dword, name:dword, value:dword, reason:dword
1933
locals
1934
	pos dd ? ;size_t
1935
	message rb 196 ;char[] ;see below for calculation
1936
endl
1937
	mov eax,[colorspace]
1938
	cmp eax,0
1939
	je @f ;if (..!=0)
1940
		or word[eax+png_colorspace.flags], PNG_COLORSPACE_INVALID
1941
	@@:
1942
1943
 
1944
;   pos = png_safecat(message, pos+79, pos, name); /* Truncate to 79 chars */
1945
;   pos = png_safecat(message, (sizeof message), pos, "': "); /* +2 = 90 */
1946
;   if (is_ICC_signature(value) != 0)
1947
;   {
1948
	; So 'value' is at most 4 bytes and the following cast is safe
1949
;      png_icc_tag_name(message+pos, (uint_32)value);
1950
;      pos += 6; /* total +8; less than the else clause */
1951
;      message[pos++] = ':';
1952
;      message[pos++] = ' ';
1953
;   }
1954
if PNG_WARNINGS_SUPPORTED eq 1
1955
;   else
1956
;      {
1957
;         char number[PNG_NUMBER_BUFFER_SIZE]; /* +24 = 114*/
1958
1959
 
1960
;             png_format_number(number, number+(sizeof number),
1961
;             PNG_NUMBER_FORMAT_x, value));
1962
;         pos = png_safecat(message, (sizeof message), pos, "h: "); /*+2 = 116*/
1963
;      }
1964
end if
1965
	; The 'reason' is an arbitrary message, allow +79 maximum 195
1966
;   pos = png_safecat(message, (sizeof message), pos, reason);
1967
1968
 
1969
	; avoid writing invalid ICC profiles into PNG files (i.e., we handle them
1970
	; on read, with a warning, but on write unless the app turns off
1971
	; application errors the PNG won't be written.)
1972
1973
 
1974
;       (colorspace != NULL) ? PNG_CHUNK_ERROR : PNG_CHUNK_WRITE_ERROR);
1975
1976
 
1977
	ret
1978
endp
1979
1980
 
1981
;color      X      Y      Z
1982
sRGB_XYZ dd 41239, 21264,  1933,\ ;red
1983
	35758, 71517, 11919,\ ;green
1984
	18048,  7219, 95053  ;blue
1985
end if
1986
1987
 
1988
align 4
1989
proc png_colorspace_set_sRGB uses ebx ecx edi esi, png_ptr:dword, colorspace:dword, intent:dword
1990
	; sRGB sets known gamma, end points and (from the chunk) intent.
1991
	; IMPORTANT: these are not necessarily the values found in an ICC profile
1992
	; because ICC profiles store values adapted to a D50 environment; it is
1993
	; expected that the ICC profile mediaWhitePointTag will be D50; see the
1994
	; checks and code elsewhere to understand this better.
1995
1996
 
1997
	; coefficients of (6968,23435,2366), which are reduced (because they add up
1998
	; to 32769 not 32768) to (6968,23434,2366).  These are the values that
1999
	; libpng has traditionally used (and are the best values given the 15bit
2000
	; algorithm used by the rgb to gray code.)
2001
2002
 
2003
	mov ebx,[colorspace]
2004
	mov ax,[ebx+png_colorspace.flags]
2005
	and ax,PNG_COLORSPACE_INVALID
2006
	cmp ax,0
2007
	je @f ;if (..!=0)
2008
		xor eax,eax
2009
		jmp .end_f
2010
	@@:
2011
2012
 
2013
	; PNG file to have cHRM or gAMA chunks along with sRGB, but the values must
2014
	; be consistent with the correct values.  If, however, this function is
2015
	; called below because an iCCP chunk matches sRGB then it is quite
2016
	; conceivable that an older app recorded incorrect gAMA and cHRM because of
2017
	; an incorrect calculation based on the values in the profile - this does
2018
	; *not* invalidate the profile (though it still produces an error, which can
2019
	; be ignored.)
2020
2021
 
2022
	cmp dword[intent],0
2023
	jl @f
2024
	cmp dword[intent],PNG_sRGB_INTENT_LAST
2025
	jge @f
2026
		jmp .end0
2027
	@@: ;if (..<0 || ..>=..)
2028
		cStr ,'sRGB'
2029
		cStr ecx,'invalid sRGB rendering intent'
2030
		stdcall png_icc_profile_error, edi, ebx, eax, [intent], ecx
2031
		jmp .end_f
2032
	.end0:
2033
2034
 
2035
	and ax,PNG_COLORSPACE_HAVE_INTENT
2036
	cmp ax,0
2037
	je @f
2038
	movzx eax,word[ebx+png_colorspace.rendering_intent]
2039
	cmp eax,[intent]
2040
	je @f ;if (..!=0 && ..!=..)
2041
		cStr ,'sRGB'
2042
		cStr ecx,'inconsistent rendering intents'
2043
		stdcall png_icc_profile_error, edi, ebx, eax, [intent], ecx
2044
		jmp .end_f
2045
	@@:
2046
2047
 
2048
	and ax,PNG_COLORSPACE_FROM_sRGB
2049
	cmp ax,0
2050
	je @f ;if (..!=0)
2051
		png_benign_error edi, 'duplicate sRGB information ignored'
2052
		xor eax,eax
2053
		jmp .end_f
2054
	@@:
2055
2056
 
2057
	; warn but overwrite the value with the correct one.
2058
2059
 
2060
	and ax,PNG_COLORSPACE_HAVE_ENDPOINTS
2061
	cmp ax,0
2062
	je @f ;if (..!=0 &&
2063
;       !png_colorspace_endpoints_match(&sRGB_xy, &colorspace->end_points_xy,
2064
;       100))
2065
		cStr ,'cHRM chunk does not match sRGB'
2066
		stdcall png_chunk_report, edi, eax, PNG_CHUNK_ERROR
2067
	@@:
2068
2069
 
2070
	; returns true when the 'from' argument corresponds to sRGB (2).
2071
2072
 
2073
2074
 
2075
	mov eax,[intent]
2076
	mov [ebx+png_colorspace.rendering_intent],ax
2077
	or word[ebx+png_colorspace.flags], PNG_COLORSPACE_HAVE_INTENT
2078
2079
 
2080
;   colorspace->end_points_xy = sRGB_xy;
2081
;   colorspace->end_points_XYZ = sRGB_XYZ;
2082
	or word[ebx+png_colorspace.flags], (PNG_COLORSPACE_HAVE_ENDPOINTS or PNG_COLORSPACE_ENDPOINTS_MATCH_sRGB)
2083
2084
 
2085
	mov dword[ebx+png_colorspace.gamma], PNG_GAMMA_sRGB_INVERSE
2086
	or word[ebx+png_colorspace.flags], PNG_COLORSPACE_HAVE_GAMMA
2087
2088
 
2089
	or word[ebx+png_colorspace.flags], (PNG_COLORSPACE_MATCHES_sRGB or PNG_COLORSPACE_FROM_sRGB)
2090
2091
 
2092
	inc eax ;set
2093
.end_f:
2094
	ret
2095
endp
2096
2097
 
2098
; Encoded value of D50 as an ICC XYZNumber.  From the ICC 2010 spec the value
2099
; is XYZ(0.9642,1.0,0.8249), which scales to:
2100
2101
 
2102
2103
 
2104
	0x00, 0x00, 0xf6, 0xd6, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0xd3, 0x2d
2105
2106
 
2107
;(png_structrp png_ptr, png_colorspacerp colorspace, charp name, uint_32 profile_length)
2108
align 4
2109
proc icc_check_length, png_ptr:dword, colorspace:dword, name:dword, profile_length:dword
2110
	cmp dword[profile_length],132
2111
	jge @f ;if (..<..)
2112
;      return png_icc_profile_error(png_ptr, colorspace, name, profile_length,
2113
;          "too short");
2114
		jmp .end_f
2115
	@@:
2116
	xor eax,eax
2117
	inc eax
2118
.end_f:
2119
	ret
2120
endp
2121
2122
 
2123
;    charp name, uint_32 profile_length)
2124
align 4
2125
proc png_icc_check_length, png_ptr:dword, colorspace:dword, name:dword, profile_length:dword
2126
;   if (!icc_check_length(png_ptr, colorspace, name, profile_length))
2127
;      return 0;
2128
2129
 
2130
	; png_decompress_chunk, yet this happens after the attempt to
2131
	; png_malloc_base the required data.  We only need this on read; on write
2132
	; the caller supplies the profile buffer so libpng doesn't allocate it.  See
2133
	; the call to icc_check_length below (the write case).
2134
2135
 
2136
;      else if (png_ptr->user_chunk_malloc_max > 0 &&
2137
;               png_ptr->user_chunk_malloc_max < profile_length)
2138
;         return png_icc_profile_error(png_ptr, colorspace, name, profile_length,
2139
;             "exceeds application limits");
2140
elseif PNG_USER_CHUNK_MALLOC_MAX > 0
2141
;      else if (PNG_USER_CHUNK_MALLOC_MAX < profile_length)
2142
;         return png_icc_profile_error(png_ptr, colorspace, name, profile_length,
2143
;             "exceeds libpng limits");
2144
else ;!SET_USER_LIMITS
2145
	; This will get compiled out on all 32-bit and better systems.
2146
;      else if (PNG_SIZE_MAX < profile_length)
2147
;         return png_icc_profile_error(png_ptr, colorspace, name, profile_length,
2148
;             "exceeds system limits");
2149
end if ;!SET_USER_LIMITS
2150
	xor eax,eax
2151
	inc eax
2152
.end_f:
2153
	ret
2154
endp
2155
2156
 
2157
;    charp name, uint_32 profile_length,
2158
;    bytep profile/* first 132 bytes only */, int color_type)
2159
align 4
2160
proc png_icc_check_header, png_ptr:dword, colorspace:dword, name:dword, profile_length:dword, profile:dword, color_type:dword
2161
;   uint_32 temp;
2162
2163
 
2164
	; is used later to check the tag table, so even if the profile seems over
2165
	; long profile_length from the caller must be correct.  The caller can fix
2166
	; this up on read or write by just passing in the profile header length.
2167
2168
 
2169
;   if (temp != profile_length)
2170
;      return png_icc_profile_error(png_ptr, colorspace, name, temp,
2171
;          "length does not match profile");
2172
2173
 
2174
;   if (temp > 3 && (profile_length & 3))
2175
;      return png_icc_profile_error(png_ptr, colorspace, name, profile_length,
2176
;          "invalid length");
2177
2178
 
2179
;   if (temp > 357913930 || /* (2^32-4-132)/12: maximum possible tag count */
2180
;      profile_length < 132+12*temp) /* truncated tag table */
2181
;      return png_icc_profile_error(png_ptr, colorspace, name, temp,
2182
;          "tag count too large");
2183
2184
 
2185
	; 16 bits.
2186
2187
 
2188
;   if (temp >= 0xffff) /* The ICC limit */
2189
;      return png_icc_profile_error(png_ptr, colorspace, name, temp,
2190
;          "invalid rendering intent");
2191
2192
 
2193
	; versions.
2194
2195
 
2196
;      (void)png_icc_profile_error(png_ptr, NULL, name, temp,
2197
;          "intent outside defined range");
2198
2199
 
2200
	; been loaded; however, various header fields can be checked.  These checks
2201
	; are for values permitted by the PNG spec in an ICC profile; the PNG spec
2202
	; restricts the profiles that can be passed in an iCCP chunk (they must be
2203
	; appropriate to processing PNG data!)
2204
2205
 
2206
	; version number; however, the version number doesn't accomodate changes in
2207
	; the header fields (just the known tags and the interpretation of the
2208
	; data.)
2209
2210
 
2211
;   if (temp != 0x61637370)
2212
;      return png_icc_profile_error(png_ptr, colorspace, name, temp,
2213
;          "invalid signature");
2214
2215
 
2216
	; white point) are required to be D50,
2217
	; however the profile contains a record of the illuminant so perhaps ICC
2218
	; expects to be able to change this in the future (despite the rationale in
2219
	; the introduction for using a fixed PCS adopted white.)  Consequently the
2220
	; following is just a warning.
2221
2222
 
2223
;      (void)png_icc_profile_error(png_ptr, NULL, name, 0/*no tag value*/,
2224
;          "PCS illuminant is not D50");
2225
2226
 
2227
	; "If the iCCP chunk is present, the image samples conform to the colour
2228
	; space represented by the embedded ICC profile as defined by the
2229
	; International Color Consortium [ICC]. The colour space of the ICC profile
2230
	; shall be an RGB colour space for colour images (PNG colour types 2, 3, and
2231
	; 6), or a greyscale colour space for greyscale images (PNG colour types 0
2232
	; and 4)."
2233
2234
 
2235
	; conforms to the specification requirements.  Notice that an ICC 'gray'
2236
	; color-space profile contains the information to transform the monochrome
2237
	; data to XYZ or L*a*b (according to which PCS the profile uses) and this
2238
	; should be used in preference to the standard libpng K channel replication
2239
	; into R, G and B channels.
2240
2241
 
2242
	; handled.  However it it is clear that using an RGB profile in this context
2243
	; must be an error - there is no specification of what it means.  Thus it is
2244
	; almost certainly more correct to ignore the profile.
2245
2246
 
2247
;   switch (temp)
2248
;   {
2249
;      case 0x52474220: /* 'RGB ' */
2250
;         if ((color_type & PNG_COLOR_MASK_COLOR) == 0)
2251
;            return png_icc_profile_error(png_ptr, colorspace, name, temp,
2252
;                "RGB color space not permitted on grayscale PNG");
2253
;         break;
2254
2255
 
2256
;         if ((color_type & PNG_COLOR_MASK_COLOR) != 0)
2257
;            return png_icc_profile_error(png_ptr, colorspace, name, temp,
2258
;                "Gray color space not permitted on RGB PNG");
2259
;         break;
2260
2261
 
2262
;         return png_icc_profile_error(png_ptr, colorspace, name, temp,
2263
;             "invalid ICC profile color space");
2264
;   }
2265
2266
 
2267
	; application requirements; the spec provides no guidance, but it's pretty
2268
	; weird if the profile is not scanner ('scnr'), monitor ('mntr'), printer
2269
	; ('prtr') or 'spac' (for generic color spaces).  Issue a warning in these
2270
	; cases.  Issue an error for device link or abstract profiles - these don't
2271
	; contain the records necessary to transform the color-space to anything
2272
	; other than the target device (and not even that for an abstract profile).
2273
	; Profiles of these classes may not be embedded in images.
2274
2275
 
2276
;   switch (temp)
2277
;   {
2278
;      case 0x73636e72: /* 'scnr' */
2279
;      case 0x6d6e7472: /* 'mntr' */
2280
;      case 0x70727472: /* 'prtr' */
2281
;      case 0x73706163: /* 'spac' */
2282
;         /* All supported */
2283
;         break;
2284
2285
 
2286
;         /* May not be embedded in an image */
2287
;         return png_icc_profile_error(png_ptr, colorspace, name, temp,
2288
;             "invalid embedded Abstract ICC profile");
2289
2290
 
2291
;         /* DeviceLink profiles cannot be interpreted in a non-device specific
2292
	; fashion, if an app uses the AToB0Tag in the profile the results are
2293
	; undefined unless the result is sent to the intended device,
2294
	; therefore a DeviceLink profile should not be found embedded in a
2295
	; PNG.
2296
2297
 
2298
;             "unexpected DeviceLink ICC profile class");
2299
2300
 
2301
;         /* A NamedColor profile is also device specific, however it doesn't
2302
	; contain an AToB0 tag that is open to misinterpretation.  Almost
2303
	; certainly it will fail the tests below.
2304
2305
 
2306
;             "unexpected NamedColor ICC profile class");
2307
;         break;
2308
2309
 
2310
;         /* To allow for future enhancements to the profile accept unrecognized
2311
	; profile classes with a warning, these then hit the test below on the
2312
	; tag content to ensure they are backward compatible with one of the
2313
	; understood profiles.
2314
2315
 
2316
;             "unrecognized ICC profile class");
2317
;         break;
2318
;   }
2319
2320
 
2321
	; either in XYZ or Lab.
2322
2323
 
2324
;   switch (temp)
2325
;   {
2326
;      case 0x58595a20: /* 'XYZ ' */
2327
;      case 0x4c616220: /* 'Lab ' */
2328
;         break;
2329
2330
 
2331
;         return png_icc_profile_error(png_ptr, colorspace, name, temp,
2332
;             "unexpected ICC PCS encoding");
2333
;   }
2334
2335
 
2336
	ret
2337
endp
2338
2339
 
2340
;    charp name, uint_32 profile_length,
2341
;    bytep profile /* header plus whole tag table */)
2342
align 4
2343
proc png_icc_check_tag_table, png_ptr:dword, colorspace:dword, name:dword, profile_length:dword, profile:dword
2344
;   uint_32 tag_count = png_get_uint_32(profile+128);
2345
;   uint_32 itag;
2346
;   bytep tag = profile+132; /* The first tag */
2347
2348
 
2349
	; (temporarily in 'tags').
2350
2351
 
2352
;   {
2353
;      uint_32 tag_id = png_get_uint_32(tag+0);
2354
;      uint_32 tag_start = png_get_uint_32(tag+4); /* must be aligned */
2355
;      uint_32 tag_length = png_get_uint_32(tag+8);/* not padded */
2356
2357
 
2358
	; start might actually be anywhere if there is no data, but this would be
2359
	; a clear abuse of the intent of the standard so the start is checked for
2360
	; being in range.  All defined tag types have an 8 byte header - a 4 byte
2361
	; type signature then 0.
2362
2363
 
2364
;      {
2365
	; CNHP730S.icc shipped with Microsoft Windows 64 violates this, it is
2366
	; only a warning here because libpng does not care about the
2367
	; alignment.
2368
2369
 
2370
;             "ICC profile tag start not a multiple of 4");
2371
;      }
2372
2373
 
2374
	; profile.
2375
2376
 
2377
;         return png_icc_profile_error(png_ptr, colorspace, name, tag_id,
2378
;             "ICC profile tag outside profile");
2379
;   }
2380
	xor eax,eax
2381
	inc eax ;success, maybe with warnings
2382
.end_f:
2383
	ret
2384
endp
2385
2386
 
2387
;#if PNG_sRGB_PROFILE_CHECKS >= 0
2388
; Information about the known ICC sRGB profiles
2389
struct png_sRGB_checks
2390
	adler dd ? ;uint_32
2391
	crc dd ?
2392
	length dd ?
2393
	md5 rd 4 ;uint_32[4]
2394
	have_md5 db ? ;byte
2395
	is_broken db ? ;byte
2396
	intent dw ? ;uint_16
2397
ends
2398
;#  define PNG_MD5(a,b,c,d) { a, b, c, d }, (a!=0)||(b!=0)||(c!=0)||(d!=0)
2399
;#  define PNG_ICC_CHECKSUM(adler, crc, md5, intent, broke, date, length, fname)\
2400
;      { adler, crc, length, md5, broke, intent },
2401
2402
 
2403
	; This data comes from contrib/tools/checksum-icc run on downloads of
2404
	; all four ICC sRGB profiles from www.color.org.
2405
2406
 
2407
;   PNG_ICC_CHECKSUM(0x0a3fd9f6, 0x3b8772b9,
2408
;       PNG_MD5(0x29f83dde, 0xaff255ae, 0x7842fae4, 0xca83390d), 0, 0,
2409
;       "2009/03/27 21:36:31", 3048, "sRGB_IEC61966-2-1_black_scaled.icc")
2410
2411
 
2412
;   PNG_ICC_CHECKSUM(0x4909e5e1, 0x427ebb21,
2413
;       PNG_MD5(0xc95bd637, 0xe95d8a3b, 0x0df38f99, 0xc1320389), 1, 0,
2414
;       "2009/03/27 21:37:45", 3052, "sRGB_IEC61966-2-1_no_black_scaling.icc")
2415
2416
 
2417
;       PNG_MD5(0xfc663378, 0x37e2886b, 0xfd72e983, 0x8228f1b8), 0, 0,
2418
;       "2009/08/10 17:28:01", 60988, "sRGB_v4_ICC_preference_displayclass.icc")
2419
2420
 
2421
;   PNG_ICC_CHECKSUM(0x209c35d2, 0xbbef7812,
2422
;       PNG_MD5(0x34562abf, 0x994ccd06, 0x6d2c5721, 0xd0d68c5d), 0, 0,
2423
;       "2007/07/25 00:05:37", 60960, "sRGB_v4_ICC_preference.icc")
2424
2425
 
2426
	; on the (empty) MD5 the other fields are used to attempt a match and
2427
	; a warning is produced.  The first two of these profiles have a 'cprt' tag
2428
	; which suggests that they were also made by Hewlett Packard.
2429
2430
 
2431
;       PNG_MD5(0x00000000, 0x00000000, 0x00000000, 0x00000000), 1, 0,
2432
;       "2004/07/21 18:57:42", 3024, "sRGB_IEC61966-2-1_noBPC.icc")
2433
2434
 
2435
	; match the D50 PCS illuminant in the header (it is in fact the D65 values,
2436
	; so the white point is recorded as the un-adapted value.)  The profiles
2437
	; below only differ in one byte - the intent - and are basically the same as
2438
	; the previous profile except for the mediaWhitePointTag error and a missing
2439
	; chromaticAdaptationTag.
2440
2441
 
2442
;       PNG_MD5(0x00000000, 0x00000000, 0x00000000, 0x00000000), 0, 1/*broken*/,
2443
;       "1998/02/09 06:49:00", 3144, "HP-Microsoft sRGB v2 perceptual")
2444
2445
 
2446
;       PNG_MD5(0x00000000, 0x00000000, 0x00000000, 0x00000000), 1, 1/*broken*/,
2447
;       "1998/02/09 06:49:00", 3144, "HP-Microsoft sRGB v2 media-relative")
2448
;
2449
2450
 
2451
align 4
2452
proc png_compare_ICC_profile_with_sRGB, png_ptr:dword, profile:dword, adler:dword
2453
	; The quick check is to verify just the MD5 signature and trust the
2454
	; rest of the data.  Because the profile has already been verified for
2455
	; correctness this is safe.  png_colorspace_set_sRGB will check the 'intent'
2456
	; field too, so if the profile has been edited with an intent not defined
2457
	; by sRGB (but maybe defined by a later ICC specification) the read of
2458
	; the profile will fail at that point.
2459
2460
 
2461
;   uint_32 intent = 0x10000; /* invalid */
2462
if PNG_sRGB_PROFILE_CHECKS > 1
2463
;   uLong crc = 0; /* the value for 0 length data */
2464
end if
2465
;   uint i;
2466
2467
 
2468
	; First see if PNG_SKIP_sRGB_CHECK_PROFILE has been set to "on"
2469
;   if (((png_ptr->options >> PNG_SKIP_sRGB_CHECK_PROFILE) & 3) ==
2470
;               PNG_OPTION_ON)
2471
;      return 0;
2472
end if
2473
2474
 
2475
;   {
2476
;      if (png_get_uint_32(profile+84) == png_sRGB_checks[i].md5[0] &&
2477
;         png_get_uint_32(profile+88) == png_sRGB_checks[i].md5[1] &&
2478
;         png_get_uint_32(profile+92) == png_sRGB_checks[i].md5[2] &&
2479
;         png_get_uint_32(profile+96) == png_sRGB_checks[i].md5[3])
2480
;      {
2481
	; This may be one of the old HP profiles without an MD5, in that
2482
	; case we can only use the length and Adler32 (note that these
2483
	; are not used by default if there is an MD5!)
2484
2485
 
2486
;            if (png_sRGB_checks[i].have_md5 != 0)
2487
;               return 1+png_sRGB_checks[i].is_broken;
2488
;#        endif
2489
2490
 
2491
;         if (length == 0)
2492
;         {
2493
;            length = png_get_uint_32(profile);
2494
;            intent = png_get_uint_32(profile+64);
2495
;         }
2496
2497
 
2498
;         if (length == (uint_32) png_sRGB_checks[i].length &&
2499
;            intent == (uint_32) png_sRGB_checks[i].intent)
2500
;         {
2501
	; Now calculate the adler32 if not done already.
2502
;            if (adler == 0)
2503
;            {
2504
;               adler = adler32(0, NULL, 0);
2505
;               adler = adler32(adler, profile, length);
2506
;            }
2507
2508
 
2509
;            {
2510
	; These basic checks suggest that the data has not been
2511
	; modified, but if the check level is more than 1 perform
2512
	; our own crc32 checksum on the data.
2513
2514
 
2515
;                  if (crc == 0)
2516
;                  {
2517
;                     crc = calc_crc32(0, NULL, 0);
2518
;                     crc = calc_crc32(crc, profile, length);
2519
;                  }
2520
2521
 
2522
2523
 
2524
;#              endif
2525
;               {
2526
;                  if (png_sRGB_checks[i].is_broken != 0)
2527
;                  {
2528
	; These profiles are known to have bad data that may cause
2529
	; problems if they are used, therefore attempt to
2530
	; discourage their use, skip the 'have_md5' warning below,
2531
	; which is made irrelevant by this error.
2532
2533
 
2534
;                         PNG_CHUNK_ERROR);
2535
;                  }
2536
2537
 
2538
	; the profile is perfectly valid, but it would be nice if
2539
	; people used the up-to-date ones.
2540
2541
 
2542
;                  {
2543
;                     png_chunk_report(png_ptr,
2544
;                         "out-of-date sRGB profile with no signature",
2545
;                         PNG_CHUNK_WARNING);
2546
;                  }
2547
2548
 
2549
;               }
2550
;            }
2551
2552
 
2553
	; The signature matched, but the profile had been changed in some
2554
	; way.  This probably indicates a data error or uninformed hacking.
2555
	; Fall through to "no match".
2556
2557
 
2558
;             "Not recognizing known sRGB profile that has been edited",
2559
;             PNG_CHUNK_WARNING);
2560
;         break;
2561
;# endif
2562
;         }
2563
;      }
2564
;   }
2565
2566
 
2567
	ret
2568
endp
2569
2570
 
2571
;    png_colorspacerp colorspace, bytep profile, uLong adler)
2572
align 4
2573
proc png_icc_set_sRGB uses eax, png_ptr:dword, colorspace:dword, profile:dword, adler:dword
2574
	; Is this profile one of the known ICC sRGB profiles?  If it is, just set
2575
	; the sRGB information.
2576
2577
 
2578
;      (void)png_colorspace_set_sRGB(png_ptr, colorspace,
2579
;         (int)/*already checked*/png_get_uint_32(profile+64));
2580
	ret
2581
endp
2582
;end if /* PNG_sRGB_PROFILE_CHECKS >= 0 */
2583
;end if /* sRGB */
2584
2585
 
2586
;    charp name, uint_32 profile_length, bytep profile,
2587
;    int color_type)
2588
align 4
2589
proc png_colorspace_set_ICC, png_ptr:dword, colorspace:dword, name:dword, profile_length:dword, profile:dword, color_type:dword
2590
;   if ((colorspace->flags & PNG_COLORSPACE_INVALID) != 0)
2591
;      return 0;
2592
2593
 
2594
;       png_icc_check_header(png_ptr, colorspace, name, profile_length, profile,
2595
;           color_type) != 0 &&
2596
;       png_icc_check_tag_table(png_ptr, colorspace, name, profile_length,
2597
;           profile) != 0)
2598
;   {
2599
;#     if defined(PNG_sRGB_SUPPORTED) && PNG_sRGB_PROFILE_CHECKS >= 0
2600
	; If no sRGB support, don't try storing sRGB information
2601
;         png_icc_set_sRGB(png_ptr, colorspace, profile, 0);
2602
;#     endif
2603
;      return 1;
2604
;   }
2605
2606
 
2607
	xor eax,eax
2608
.end_f:
2609
	ret
2610
endp
2611
;end if /* iCCP */
2612
2613
 
2614
align 4
2615
proc png_colorspace_set_rgb_coefficients, png_ptr:dword
2616
	; Set the rgb_to_gray coefficients from the colorspace.
2617
;   if (png_ptr->rgb_to_gray_coefficients_set == 0 &&
2618
;      (png_ptr->colorspace.flags & PNG_COLORSPACE_HAVE_ENDPOINTS) != 0)
2619
;   {
2620
	; png_set_background has not been called, get the coefficients from the Y
2621
	; values of the colorspace colorants.
2622
2623
 
2624
;      png_fixed_point g = png_ptr->colorspace.end_points_XYZ.green_Y;
2625
;      png_fixed_point b = png_ptr->colorspace.end_points_XYZ.blue_Y;
2626
;      png_fixed_point total = r+g+b;
2627
2628
 
2629
;         r >= 0 && png_muldiv(&r, r, 32768, total) && r >= 0 && r <= 32768 &&
2630
;         g >= 0 && png_muldiv(&g, g, 32768, total) && g >= 0 && g <= 32768 &&
2631
;         b >= 0 && png_muldiv(&b, b, 32768, total) && b >= 0 && b <= 32768 &&
2632
;         r+g+b <= 32769)
2633
;      {
2634
	; We allow 0 coefficients here.  r+g+b may be 32769 if two or
2635
	; all of the coefficients were rounded up.  Handle this by
2636
	; reducing the *largest* coefficient by 1; this matches the
2637
	; approach used for the default coefficients in pngrtran.c
2638
2639
 
2640
;
2641
;         if (r+g+b > 32768)
2642
;            add = -1;
2643
;         else if (r+g+b < 32768)
2644
;            add = 1;
2645
2646
 
2647
;         {
2648
;            if (g >= r && g >= b)
2649
;               g += add;
2650
;            else if (r >= g && r >= b)
2651
;               r += add;
2652
;            else
2653
;               b += add;
2654
;         }
2655
2656
 
2657
;         if (r+g+b != 32768)
2658
;            png_error(png_ptr,
2659
;                "internal error handling cHRM coefficients");
2660
2661
 
2662
;         {
2663
;            png_ptr->rgb_to_gray_red_coeff   = (uint_16)r;
2664
;            png_ptr->rgb_to_gray_green_coeff = (uint_16)g;
2665
;         }
2666
;      }
2667
2668
 
2669
	; it should never happen, but it is important that if it does, the
2670
	; bug is fixed.
2671
2672
 
2673
;         png_error(png_ptr, "internal error handling cHRM->XYZ");
2674
;   }
2675
	ret
2676
endp
2677
2678
 
2679
2680
 
2681
;    uint_32 width, uint_32 height, int bit_depth,
2682
;    int color_type, int interlace_type, int compression_type, int filter_type)
2683
align 4
2684
proc png_check_IHDR uses eax ebx edi, png_ptr:dword, width:dword, height:dword, bit_depth:dword, color_type:dword, interlace_type:dword, compression_type:dword, filter_type:dword
2685
	mov edi,[png_ptr]
2686
	xor ebx,ebx
2687
2688
 
2689
	cmp dword[width],0
2690
	jne @f ;if (..==0)
2691
		png_warning edi, 'Image width is zero in IHDR'
2692
		inc ebx
2693
	@@:
2694
2695
 
2696
	jle @f ;if (..>..)
2697
		png_warning edi, 'Invalid image width in IHDR'
2698
		inc ebx
2699
	@@:
2700
2701
 
2702
	;  1 - filter byte
2703
	;  8 - 8-byte RGBA pixels
2704
	;  1 - extra max_pixel_depth pad
2705
	mov eax,[width]
2706
	add eax,7
2707
	and eax,not 7
2708
	cmp eax,((PNG_SIZE_MAX -48 -1) / 8) -1
2709
	jle @f ;if (..>..)
2710
		; The size of the row must be within the limits of this architecture.
2711
		; Because the read code can perform arbitrary transformations the
2712
		; maximum size is checked here.  Because the code in png_read_start_row
2713
		; adds extra space "for safety's sake" in several places a conservative
2714
		; limit is used here.
2715
2716
 
2717
		; but the effect in the real world is minor and the changes are more
2718
		; extensive, therefore much more dangerous and much more difficult to
2719
		; write in a way that avoids compiler warnings.
2720
2721
 
2722
		inc ebx
2723
	@@:
2724
2725
 
2726
	mov eax,[edi+png_struct.user_width_max]
2727
	cmp dword[width],eax
2728
else
2729
	cmp dword[width],PNG_USER_WIDTH_MAX
2730
end if
2731
	jle @f ;if (..>..)
2732
		png_warning edi, 'Image width exceeds user limit in IHDR'
2733
		inc ebx
2734
	@@:
2735
2736
 
2737
	jne @f ;if (..==0)
2738
		png_warning edi, 'Image height is zero in IHDR'
2739
		inc ebx
2740
	@@:
2741
2742
 
2743
	jle @f ;if (..>..)
2744
		png_warning edi, 'Invalid image height in IHDR'
2745
		inc ebx
2746
	@@:
2747
2748
 
2749
	mov eax,[edi+png_struct.user_height_max]
2750
	cmp dword[height],eax
2751
else
2752
	cmp dword[height],PNG_USER_HEIGHT_MAX
2753
end if
2754
	jle @f ;if (..>..)
2755
		png_warning edi, 'Image height exceeds user limit in IHDR'
2756
		inc ebx
2757
	@@:
2758
2759
 
2760
	cmp dword[bit_depth],1
2761
	je @f
2762
	cmp dword[bit_depth],2
2763
	je @f
2764
	cmp dword[bit_depth],4
2765
	je @f
2766
	cmp dword[bit_depth],8
2767
	je @f
2768
	cmp dword[bit_depth],16
2769
	je @f ;if (..!=.. && ...)
2770
		png_warning edi, 'Invalid bit depth in IHDR'
2771
		inc ebx
2772
	@@:
2773
2774
 
2775
	jl @f
2776
	cmp dword[color_type],1
2777
	je @f
2778
	cmp dword[color_type],5
2779
	je @f
2780
	cmp dword[color_type],6
2781
	jg @f
2782
		jmp .end0
2783
	@@: ;if (..<0 || ..==1 || ..==5 || ..>6)
2784
		png_warning edi, 'Invalid color type in IHDR'
2785
		inc ebx
2786
	.end0:
2787
2788
 
2789
	jne @f
2790
	cmp dword[bit_depth],8
2791
	jg .beg1
2792
	@@:
2793
	cmp dword[color_type],PNG_COLOR_TYPE_RGB
2794
	je @f
2795
	cmp dword[color_type],PNG_COLOR_TYPE_GRAY_ALPHA
2796
	je @f
2797
	cmp dword[color_type],PNG_COLOR_TYPE_RGB_ALPHA
2798
	jne .end1
2799
	@@:
2800
	cmp dword[bit_depth],8
2801
	jge .end1
2802
	.beg1: ;if (((..==..) && ..>..) || ((..==.. || ..==.. || ..==..) && ..<..))
2803
		png_warning edi, 'Invalid color type/bit depth combination in IHDR'
2804
		inc ebx
2805
	.end1:
2806
2807
 
2808
	jl @f ;if (..>=..)
2809
		png_warning edi, 'Unknown interlace method in IHDR'
2810
		inc ebx
2811
	@@:
2812
2813
 
2814
	je @f ;if (..!=..)
2815
		png_warning edi, 'Unknown compression method in IHDR'
2816
		inc ebx
2817
	@@:
2818
2819
 
2820
	; Accept filter_method 64 (intrapixel differencing) only if
2821
	; 1. Libpng was compiled with PNG_MNG_FEATURES_SUPPORTED and
2822
	; 2. Libpng did not read a PNG signature (this filter_method is only
2823
	;    used in PNG datastreams that are embedded in MNG datastreams) and
2824
	; 3. The application called png_permit_mng_features with a mask that
2825
	;    included PNG_FLAG_MNG_FILTER_64 and
2826
	; 4. The filter_method is 64 and
2827
	; 5. The color_type is RGB or RGBA
2828
2829
 
2830
;       png_ptr->mng_features_permitted != 0)
2831
		png_warning edi, 'MNG features are not allowed in a PNG datastream'
2832
2833
 
2834
;   {
2835
;      if (!((png_ptr->mng_features_permitted & PNG_FLAG_MNG_FILTER_64) != 0 &&
2836
;          (filter_type == PNG_INTRAPIXEL_DIFFERENCING) &&
2837
;          ((png_ptr->mode & PNG_HAVE_PNG_SIGNATURE) == 0) &&
2838
;          (color_type == PNG_COLOR_TYPE_RGB ||
2839
;          color_type == PNG_COLOR_TYPE_RGB_ALPHA)))
2840
;      {
2841
		png_warning edi, 'Unknown filter method in IHDR'
2842
		inc ebx
2843
;      }
2844
2845
 
2846
;      {
2847
		png_warning edi, 'Invalid filter method in IHDR'
2848
		inc ebx
2849
;      }
2850
;   }
2851
2852
 
2853
	cmp dword[filter_type],PNG_FILTER_TYPE_BASE
2854
	je @f ;if (..!=..)
2855
		png_warning edi, 'Unknown filter method in IHDR'
2856
		inc ebx
2857
	@@:
2858
end if
2859
2860
 
2861
	je @f
2862
		png_error edi, 'Invalid IHDR data'
2863
	@@:
2864
	ret
2865
endp
2866
2867
 
2868
; ASCII to fp functions
2869
; Check an ASCII formated floating point value, see the more detailed
2870
; comments in pngpriv.inc
2871
2872
 
2873
;#define png_fp_add(state, flags) ((state) |= (flags))
2874
;#define png_fp_set(state, value) ((state) = (value) | ((state) & PNG_FP_STICKY))
2875
2876
 
2877
align 4
2878
proc png_check_fp_number, string:dword, size:dword, statep:dword, whereami:dword
2879
;   int state = *statep;
2880
;   png_size_t i = *whereami;
2881
2882
 
2883
;   {
2884
;      int type;
2885
	; First find the type of the next character
2886
;      switch (string[i])
2887
;      {
2888
;      case 43:  type = PNG_FP_SAW_SIGN;                   break;
2889
;      case 45:  type = PNG_FP_SAW_SIGN + PNG_FP_NEGATIVE; break;
2890
;      case 46:  type = PNG_FP_SAW_DOT;                    break;
2891
;      case 48:  type = PNG_FP_SAW_DIGIT;                  break;
2892
;      case 49: case 50: case 51: case 52:
2893
;      case 53: case 54: case 55: case 56:
2894
;      case 57:  type = PNG_FP_SAW_DIGIT + PNG_FP_NONZERO; break;
2895
;      case 69:
2896
;      case 101: type = PNG_FP_SAW_E;                      break;
2897
;      default:  goto PNG_FP_End;
2898
;      }
2899
2900
 
2901
	; state, the type is arranged to not overlap the
2902
	; bits of the PNG_FP_STATE.
2903
2904
 
2905
;      {
2906
;      case PNG_FP_INTEGER + PNG_FP_SAW_SIGN:
2907
;         if ((state & PNG_FP_SAW_ANY) != 0)
2908
;            goto PNG_FP_End; /* not a part of the number */
2909
2910
 
2911
;         break;
2912
2913
 
2914
	; Ok as trailer, ok as lead of fraction.
2915
;         if ((state & PNG_FP_SAW_DOT) != 0) /* two dots */
2916
;            goto PNG_FP_End;
2917
2918
 
2919
;            png_fp_add(state, type);
2920
2921
 
2922
;            png_fp_set(state, PNG_FP_FRACTION | type);
2923
2924
 
2925
2926
 
2927
;         if ((state & PNG_FP_SAW_DOT) != 0) /* delayed fraction */
2928
;            png_fp_set(state, PNG_FP_FRACTION | PNG_FP_SAW_DOT);
2929
2930
 
2931
2932
 
2933
2934
 
2935
;         if ((state & PNG_FP_SAW_DIGIT) == 0)
2936
;            goto PNG_FP_End;
2937
2938
 
2939
2940
 
2941
2942
 
2943
2944
 
2945
2946
 
2947
;         png_fp_add(state, type | PNG_FP_WAS_VALID);
2948
;         break;
2949
2950
 
2951
	; This is correct because the trailing '.' on an
2952
	; integer is handled above - so we can only get here
2953
	; with the sequence ".E" (with no preceding digits).
2954
2955
 
2956
;            goto PNG_FP_End;
2957
2958
 
2959
2960
 
2961
2962
 
2963
;         if ((state & PNG_FP_SAW_ANY) != 0)
2964
;            goto PNG_FP_End; /* not a part of the number */
2965
2966
 
2967
2968
 
2969
2970
 
2971
2972
 
2973
;         png_fp_add(state, PNG_FP_SAW_DIGIT | PNG_FP_WAS_VALID);
2974
2975
 
2976
2977
 
2978
2979
 
2980
;      }
2981
2982
 
2983
;      ++i;
2984
;   }
2985
;
2986
;PNG_FP_End:
2987
	; Here at the end, update the state and return the correct
2988
	; return code.
2989
2990
 
2991
;   *whereami = i;
2992
2993
 
2994
	ret
2995
endp
2996
2997
 
2998
 
2999
;int (charp string, png_size_t size)
3000
align 4
3001
proc png_check_fp_string, string:dword, size:dword
3002
;   int        state=0;
3003
;   png_size_t char_index=0;
3004
;
3005
;   if (png_check_fp_number(string, size, &state, &char_index) != 0 &&
3006
;      (char_index == size || string[char_index] == 0))
3007
;      return state /* must be non-zero - see above */;
3008
3009
 
3010
	ret
3011
endp
3012
;end if /* pCAL || sCAL */
3013
3014
 
3015
;#  ifdef PNG_FLOATING_POINT_SUPPORTED
3016
; Utility used below - a simple accurate power of ten from an integral
3017
; exponent.
3018
3019
 
3020
align 4
3021
proc png_pow10, power:dword
3022
;   int recip = 0;
3023
;   double d = 1;
3024
3025
 
3026
	; 10 is exact whereas .1 is inexact in base 2
3027
3028
 
3029
;   {
3030
;      if (power < DBL_MIN_10_EXP) return 0;
3031
;      recip = 1, power = -power;
3032
;   }
3033
3034
 
3035
;   {
3036
	; Decompose power bitwise.
3037
;      double mult = 10;
3038
;      do
3039
;      {
3040
;         if (power & 1) d *= mult;
3041
;         mult *= mult;
3042
;         power >>= 1;
3043
;      }
3044
;      while (power > 0);
3045
3046
 
3047
;   }
3048
	; else power is 0 and d is 1
3049
3050
 
3051
	ret
3052
endp
3053
3054
 
3055
; precision.
3056
3057
 
3058
;    double fp, uint precision)
3059
align 4
3060
proc png_ascii_from_fp, png_ptr:dword, ascii:dword, size:dword, fp:dword, precision:dword
3061
	; We use standard functions from math.h, but not printf because
3062
	; that would require stdio.  The caller must supply a buffer of
3063
	; sufficient size or we will png_error.  The tests on size and
3064
	; the space in ascii[] consumed are indicated below.
3065
3066
 
3067
;      precision = DBL_DIG;
3068
3069
 
3070
;   if (precision > DBL_DIG+1)
3071
;      precision = DBL_DIG+1;
3072
3073
 
3074
;   if (size >= precision+5) /* See the requirements below. */
3075
;   {
3076
;      if (fp < 0)
3077
;      {
3078
;         fp = -fp;
3079
;         *ascii++ = 45; /* '-'  PLUS 1 TOTAL 1 */
3080
;         --size;
3081
;      }
3082
3083
 
3084
;      {
3085
;         int exp_b10;   /* A base 10 exponent */
3086
;         double base;   /* 10^exp_b10 */
3087
3088
 
3089
	; the calculation below rounds down when converting
3090
	; from base 2 to base 10 (multiply by log10(2) -
3091
	; 0.3010, but 77/256 is 0.3008, so exp_b10 needs to
3092
	; be increased.  Note that the arithmetic shift
3093
	; performs a floor() unlike C arithmetic - using a
3094
	; C multiply would break the following for negative
3095
	; exponents.
3096
3097
 
3098
3099
 
3100
3101
 
3102
;         base = png_pow10(exp_b10); /* May underflow */
3103
3104
 
3105
;         {
3106
;            /* And this may overflow. */
3107
;            double test = png_pow10(exp_b10+1);
3108
3109
 
3110
;               ++exp_b10, base = test;
3111
3112
 
3113
;               break;
3114
;         }
3115
3116
 
3117
	; range [.1,1) and exp_b10 is both the exponent and the digit
3118
	; *before* which the decimal point should be inserted
3119
	; (starting with 0 for the first digit).  Note that this
3120
	; works even if 10^exp_b10 is out of range because of the
3121
	; test on DBL_MAX above.
3122
3123
 
3124
;         while (fp >= 1) fp /= 10, ++exp_b10;
3125
3126
 
3127
	; less than .1, this is ok because the code below can
3128
	; handle the leading zeros this generates, so no attempt
3129
	; is made to correct that here.
3130
3131
 
3132
;            uint czero, clead, cdigits;
3133
;            char exponent[10];
3134
3135
 
3136
	; the number compared to using E-n.
3137
3138
 
3139
;            {
3140
;               czero = -exp_b10; /* PLUS 2 digits: TOTAL 3 */
3141
;               exp_b10 = 0;      /* Dot added below before first output. */
3142
;            }
3143
;            else
3144
;               czero = 0;    /* No zeros to add */
3145
3146
 
3147
	; inserting a '.' before a digit if the exponent is 0.
3148
3149
 
3150
;            cdigits = 0;   /* Count of digits in list. */
3151
3152
 
3153
;            {
3154
;               double d;
3155
3156
 
3157
	; Use modf here, not floor and subtract, so that
3158
	; the separation is done in one step.  At the end
3159
	; of the loop don't break the number into parts so
3160
	; that the final digit is rounded.
3161
3162
 
3163
;                  fp = modf(fp, &d);
3164
3165
 
3166
;               {
3167
;                  d = floor(fp + .5);
3168
3169
 
3170
;                  {
3171
;                     /* Rounding up to 10, handle that here. */
3172
;                     if (czero > 0)
3173
;                     {
3174
;                        --czero, d = 1;
3175
;                        if (cdigits == 0) --clead;
3176
;                     }
3177
;                     else
3178
;                     {
3179
;                        while (cdigits > 0 && d > 9)
3180
;                        {
3181
;                           int ch = *--ascii;
3182
3183
 
3184
;                              ++exp_b10;
3185
3186
 
3187
;                           {
3188
;                              ch = *--ascii, ++size;
3189
;                              /* Advance exp_b10 to '1', so that the
3190
;                               * decimal point happens after the
3191
;                               * previous digit.
3192
3193
 
3194
;                           }
3195
3196
 
3197
;                           d = ch - 47;  /* I.e. 1+(ch-48) */
3198
;                        }
3199
3200
 
3201
;                         * exponent but take into account the leading
3202
;                         * decimal point.
3203
3204
 
3205
;                        {
3206
;                           if (exp_b10 == (-1))
3207
;                           {
3208
		; Leading decimal point (plus zeros?), if
3209
		; we lose the decimal point here it must
3210
		; be reentered below.
3211
3212
 
3213
3214
 
3215
;                                 ++size, exp_b10 = 1;
3216
3217
 
3218
;                               * still ok at (-1)
3219
3220
 
3221
;                           else
3222
;                              ++exp_b10;
3223
3224
 
3225
;                           d = 1;
3226
;                        }
3227
;                     }
3228
;                  }
3229
;                  fp = 0; /* Guarantees termination below. */
3230
;               }
3231
3232
 
3233
;               {
3234
;                  ++czero;
3235
;                  if (cdigits == 0) ++clead;
3236
;               }
3237
;               else
3238
;               {
3239
;                  /* Included embedded zeros in the digit count. */
3240
;                  cdigits += czero - clead;
3241
;                  clead = 0;
3242
3243
 
3244
;                  {
3245
		; exp_b10 == (-1) means we just output the decimal
3246
		; place - after the DP don't adjust 'exp_b10' any
3247
		; more!
3248
3249
 
3250
;                     {
3251
;                        if (exp_b10 == 0) *ascii++ = 46, --size;
3252
;                        /* PLUS 1: TOTAL 4 */
3253
;                        --exp_b10;
3254
;                     }
3255
;                     *ascii++ = 48, --czero;
3256
;                  }
3257
3258
 
3259
;                  {
3260
;                     if (exp_b10 == 0)
3261
;                        *ascii++ = 46, --size; /* counted above */
3262
3263
 
3264
;                  }
3265
;                  *ascii++ = (char)(48 + (int)d), ++cdigits;
3266
;               }
3267
;            }
3268
;            while (cdigits+czero < precision+clead && fp > DBL_MIN);
3269
3270
 
3271
3272
 
3273
	; done and just need to terminate the string.  At
3274
	; this point exp_b10==(-1) is effectively if flag - it got
3275
	; to '-1' because of the decrement after outputting
3276
	; the decimal point above (the exponent required is
3277
	; *not* -1!)
3278
3279
 
3280
;            {
3281
		; The following only happens if we didn't output the
3282
		; leading zeros above for negative exponent, so this
3283
		; doesn't add to the digit requirement.  Note that the
3284
		; two zeros here can only be output if the two leading
3285
		; zeros were *not* output, so this doesn't increase
3286
		; the output count.
3287
3288
 
3289
3290
 
3291
3292
 
3293
		; 5+precision - see check at the start.
3294
3295
 
3296
;            }
3297
3298
 
3299
	; the digits we output but did not count.  The total
3300
	; digit output here so far is at most 1+precision - no
3301
	; decimal point and no leading or trailing zeros have
3302
	; been output.
3303
3304
 
3305
;
3306
;            *ascii++ = 69, --size;    /* 'E': PLUS 1 TOTAL 2+precision */
3307
3308
 
3309
	; the signed arithmetic on exp_b10 and permits GCC at least to do
3310
	; better optimization.
3311
3312
 
3313
;               uint uexp_b10;
3314
3315
 
3316
;               {
3317
;                  *ascii++ = 45, --size; /* '-': PLUS 1 TOTAL 3+precision */
3318
;                  uexp_b10 = -exp_b10;
3319
;               }
3320
3321
 
3322
;                  uexp_b10 = exp_b10;
3323
3324
 
3325
3326
 
3327
;               {
3328
;                  exponent[cdigits++] = (char)(48 + uexp_b10 % 10);
3329
;                  uexp_b10 /= 10;
3330
;               }
3331
;            }
3332
3333
 
3334
	; this need not be considered above.
3335
3336
 
3337
;            {
3338
;               while (cdigits > 0) *ascii++ = exponent[--cdigits];
3339
3340
 
3341
3342
 
3343
;            }
3344
;         }
3345
;      }
3346
;      else if (!(fp >= DBL_MIN))
3347
;      {
3348
;         *ascii++ = 48; /* '0' */
3349
;         *ascii = 0;
3350
;         return;
3351
;      }
3352
;      else
3353
;      {
3354
;         *ascii++ = 105; /* 'i' */
3355
;         *ascii++ = 110; /* 'n' */
3356
;         *ascii++ = 102; /* 'f' */
3357
;         *ascii = 0;
3358
;         return;
3359
;      }
3360
;   }
3361
3362
 
3363
;   png_error(png_ptr, "ASCII conversion buffer too small");
3364
	ret
3365
endp
3366
3367
 
3368
3369
 
3370
3371
 
3372
align 4
3373
proc png_ascii_from_fixed, png_ptr:dword, ascii:dword, size:dword, fp:dword
3374
	; Require space for 10 decimal digits, a decimal point, a minus sign and a
3375
	; trailing \0, 13 characters:
3376
3377
 
3378
	jle .end0 ;if (..>..)
3379
;      uint_32 num;
3380
3381
 
3382
;      if (fp < 0)
3383
;         *ascii++ = 45, num = -fp;
3384
;      else
3385
;         num = fp;
3386
3387
 
3388
;      {
3389
;         uint ndigits = 0, first = 16 /* flag value */;
3390
;         char digits[10];
3391
3392
 
3393
;         {
3394
	; Split the low digit off num:
3395
;            uint tmp = num/10;
3396
;            num -= tmp*10;
3397
;            digits[ndigits++] = (char)(48 + num);
3398
	; Record the first non-zero digit, note that this is a number
3399
	; starting at 1, it's not actually the array index.
3400
3401
 
3402
;               first = ndigits;
3403
;            num = tmp;
3404
;         }
3405
3406
 
3407
;         {
3408
;            while (ndigits > 5) *ascii++ = digits[--ndigits];
3409
	; The remaining digits are fractional digits, ndigits is '5' or
3410
	; smaller at this point.  It is certainly not zero.  Check for a
3411
	; non-zero fractional digit:
3412
3413
 
3414
;            {
3415
;               uint i;
3416
;               *ascii++ = 46; /* decimal point */
3417
	; ndigits may be <5 for small numbers, output leading zeros
3418
	; then ndigits digits to first:
3419
3420
 
3421
;               while (ndigits < i) *ascii++ = 48, --i;
3422
;               while (ndigits >= first) *ascii++ = digits[--ndigits];
3423
	; Don't output the trailing zeros!
3424
;            }
3425
;         }
3426
;         else
3427
;            *ascii++ = 48;
3428
3429
 
3430
;         *ascii = 0;
3431
;         return;
3432
;      }
3433
	.end0:
3434
3435
 
3436
	png_error [png_ptr], 'ASCII conversion buffer too small'
3437
	ret
3438
endp
3439
;end if /* SCAL */
3440
3441
 
3442
align 4
3443
proc png_fixed, png_ptr:dword, fp:dword, text:dword
3444
;   double r = floor(100000 * fp + .5);
3445
3446
 
3447
;      png_fixed_error(png_ptr, text);
3448
3449
 
3450
	ret
3451
endp
3452
3453
 
3454
; This API takes signed arguments and rounds the result to the nearest
3455
; integer (or, for a fixed point number - the standard argument - to
3456
; the nearest .00001).  Overflow and divide by zero are signalled in
3457
; the result, a boolean - true on success, false on overflow.
3458
3459
 
3460
align 4
3461
proc png_muldiv, res:dword, a:dword, p3times:dword, divisor:dword
3462
	; Return a * times / divisor, rounded.
3463
;   if (divisor != 0)
3464
;   {
3465
;      if (a == 0 || p3times == 0)
3466
;      {
3467
;         *res = 0;
3468
;         return 1;
3469
;      }
3470
;      else
3471
;      {
3472
if PNG_FLOATING_ARITHMETIC_SUPPORTED eq 1
3473
;         double r = a;
3474
;         r *= p3times;
3475
;         r /= divisor;
3476
;         r = floor(r+.5);
3477
3478
 
3479
;         if (r <= 2147483647. && r >= -2147483648.)
3480
;         {
3481
;            *res = (png_fixed_point)r;
3482
;            return 1;
3483
;         }
3484
else
3485
;         int negative = 0;
3486
;         uint_32 A, T, D;
3487
;         uint_32 s16, s32, s00;
3488
3489
 
3490
;            negative = 1, A = -a;
3491
;         else
3492
;            A = a;
3493
3494
 
3495
;            negative = !negative, T = -p3times;
3496
;         else
3497
;            T = p3times;
3498
3499
 
3500
;            negative = !negative, D = -divisor;
3501
;         else
3502
;            D = divisor;
3503
3504
 
3505
	; have 31 bits each, however the result may be 32 bits.
3506
3507
 
3508
;                           (A & 0xffff) * (T >> 16);
3509
	; Can't overflow because the a*times bit is only 30
3510
	; bits at most.
3511
3512
 
3513
;         s00 = (A & 0xffff) * (T & 0xffff);
3514
3515
 
3516
;         s00 += s16;
3517
3518
 
3519
;            ++s32; /* carry */
3520
3521
 
3522
;         {
3523
	; s32.s00 is now the 64-bit product, do a standard
3524
	; division, we know that s32 < D, so the maximum
3525
	; required shift is 31.
3526
3527
 
3528
;            png_fixed_point result = 0; /* NOTE: signed */
3529
3530
 
3531
;            {
3532
;               uint_32 d32, d00;
3533
3534
 
3535
;                  d32 = D >> (32-bitshift), d00 = D << bitshift;
3536
3537
 
3538
;                  d32 = 0, d00 = D;
3539
3540
 
3541
;               {
3542
;                  if (s00 < d00) --s32; /* carry */
3543
;                  s32 -= d32, s00 -= d00, result += 1<
3544
;               }
3545
3546
 
3547
;                  if (s32 == d32 && s00 >= d00)
3548
;                     s32 = 0, s00 -= d00, result += 1<
3549
;            }
3550
3551
 
3552
;            if (s00 >= (D >> 1))
3553
;               ++result;
3554
3555
 
3556
;               result = -result;
3557
3558
 
3559
;            if ((negative != 0 && result <= 0) ||
3560
;                (negative == 0 && result >= 0))
3561
;            {
3562
;               *res = result;
3563
;               return 1;
3564
;            }
3565
;         }
3566
end if
3567
;      }
3568
;   }
3569
3570
 
3571
	ret
3572
endp
3573
3574
 
3575
; result.
3576
3577
 
3578
;    int_32 divisor)
3579
align 4
3580
proc png_muldiv_warn, png_ptr:dword, a:dword, p3times:dword, divisor:dword
3581
;   png_fixed_point result;
3582
3583
 
3584
;      return result;
3585
3586
 
3587
	xor eax,eax
3588
	ret
3589
endp
3590
3591
 
3592
;png_fixed_point (png_fixed_point a)
3593
align 4
3594
proc png_reciprocal, a:dword
3595
if PNG_FLOATING_ARITHMETIC_SUPPORTED eq 1
3596
;   double r = floor(1E10/a+.5);
3597
3598
 
3599
;      return (png_fixed_point)r;
3600
else
3601
;   png_fixed_point res;
3602
3603
 
3604
;      return res;
3605
end if
3606
3607
 
3608
	ret
3609
endp
3610
3611
 
3612
; it is worth doing gamma correction.
3613
3614
 
3615
align 4
3616
proc png_gamma_significant, gamma_val:dword
3617
;   return gamma_val < PNG_FP_1 - PNG_GAMMA_THRESHOLD_FIXED ||
3618
;       gamma_val > PNG_FP_1 + PNG_GAMMA_THRESHOLD_FIXED;
3619
	ret
3620
endp
3621
3622
 
3623
; A local convenience routine.
3624
;png_fixed_point (png_fixed_point a, png_fixed_point b)
3625
align 4
3626
proc png_product2, a:dword, b:dword
3627
	; The required result is 1/a * 1/b; the following preserves accuracy.
3628
if PNG_FLOATING_ARITHMETIC_SUPPORTED eq 1
3629
;   double r = a * 1E-5;
3630
;   r *= b;
3631
;   r = floor(r+.5);
3632
3633
 
3634
;      return (png_fixed_point)r;
3635
else
3636
;   png_fixed_point res;
3637
3638
 
3639
;      return res;
3640
end if
3641
3642
 
3643
	ret
3644
endp
3645
3646
 
3647
;png_fixed_point (png_fixed_point a, png_fixed_point b)
3648
align 4
3649
proc png_reciprocal2, a:dword, b:dword
3650
	; The required result is 1/a * 1/b; the following preserves accuracy.
3651
if PNG_FLOATING_ARITHMETIC_SUPPORTED eq 1
3652
;   if (a != 0 && b != 0)
3653
;   {
3654
;      double r = 1E15/a;
3655
;      r /= b;
3656
;      r = floor(r+.5);
3657
;
3658
;      if (r <= 2147483647. && r >= -2147483648.)
3659
;         return (png_fixed_point)r;
3660
;   }
3661
else
3662
	; This may overflow because the range of png_fixed_point isn't symmetric,
3663
	; but this API is only used for the product of file and screen gamma so it
3664
	; doesn't matter that the smallest number it can produce is 1/21474, not
3665
	; 1/100000
3666
3667
 
3668
3669
 
3670
;      return png_reciprocal(res);
3671
end if
3672
3673
 
3674
	ret
3675
endp
3676
;end if /* READ_GAMMA */
3677
3678
 
3679
;#ifndef PNG_FLOATING_ARITHMETIC_SUPPORTED
3680
; Fixed point gamma.
3681
3682
 
3683
; contrib/tools/intgamma.sh
3684
3685
 
3686
; fixed point arithmetic.  This code has sufficient precision for either 8-bit
3687
; or 16-bit sample values.
3688
3689
 
3690
; precision floating point arithmetic would work fine.
3691
3692
 
3693
;   This is a table of -log(value/255)/log(2) for 'value' in the range 128 to
3694
;   255, so it's the base 2 logarithm of a normalized 8-bit floating point
3695
;   mantissa.  The numbers are 32-bit fractions.
3696
3697
 
3698
;png_8bit_l2[128] =
3699
;   4270715492U, 4222494797U, 4174646467U, 4127164793U, 4080044201U, 4033279239U,
3700
;   3986864580U, 3940795015U, 3895065449U, 3849670902U, 3804606499U, 3759867474U,
3701
;   3715449162U, 3671346997U, 3627556511U, 3584073329U, 3540893168U, 3498011834U,
3702
;   3455425220U, 3413129301U, 3371120137U, 3329393864U, 3287946700U, 3246774933U,
3703
;   3205874930U, 3165243125U, 3124876025U, 3084770202U, 3044922296U, 3005329011U,
3704
;   2965987113U, 2926893432U, 2888044853U, 2849438323U, 2811070844U, 2772939474U,
3705
;   2735041326U, 2697373562U, 2659933400U, 2622718104U, 2585724991U, 2548951424U,
3706
;   2512394810U, 2476052606U, 2439922311U, 2404001468U, 2368287663U, 2332778523U,
3707
;   2297471715U, 2262364947U, 2227455964U, 2192742551U, 2158222529U, 2123893754U,
3708
;   2089754119U, 2055801552U, 2022034013U, 1988449497U, 1955046031U, 1921821672U,
3709
;   1888774511U, 1855902668U, 1823204291U, 1790677560U, 1758320682U, 1726131893U,
3710
;   1694109454U, 1662251657U, 1630556815U, 1599023271U, 1567649391U, 1536433567U,
3711
;   1505374214U, 1474469770U, 1443718700U, 1413119487U, 1382670639U, 1352370686U,
3712
;   1322218179U, 1292211689U, 1262349810U, 1232631153U, 1203054352U, 1173618059U,
3713
;   1144320946U, 1115161701U, 1086139034U, 1057251672U, 1028498358U, 999877854U,
3714
;   971388940U, 943030410U, 914801076U, 886699767U, 858725327U, 830876614U,
3715
;   803152505U, 775551890U, 748073672U, 720716771U, 693480120U, 666362667U,
3716
;   639363374U, 612481215U, 585715177U, 559064263U, 532527486U, 506103872U,
3717
;   479792461U, 453592303U, 427502463U, 401522014U, 375650043U, 349885648U,
3718
;   324227938U, 298676034U, 273229066U, 247886176U, 222646516U, 197509248U,
3719
;   172473545U, 147538590U, 122703574U, 97967701U, 73330182U, 48790236U,
3720
;   24347096U, 0U
3721
3722
 
3723
	; The following are the values for 16-bit tables - these work fine for the
3724
	; 8-bit conversions but produce very slightly larger errors in the 16-bit
3725
	; log (about 1.2 as opposed to 0.7 absolute error in the final value).  To
3726
	; use these all the shifts below must be adjusted appropriately.
3727
3728
 
3729
;   57371, 56693, 56020, 55352, 54689, 54030, 53375, 52726, 52080, 51439, 50803,
3730
;   50170, 49542, 48918, 48298, 47682, 47070, 46462, 45858, 45257, 44661, 44068,
3731
;   43479, 42894, 42312, 41733, 41159, 40587, 40020, 39455, 38894, 38336, 37782,
3732
;   37230, 36682, 36137, 35595, 35057, 34521, 33988, 33459, 32932, 32408, 31887,
3733
;   31369, 30854, 30341, 29832, 29325, 28820, 28319, 27820, 27324, 26830, 26339,
3734
;   25850, 25364, 24880, 24399, 23920, 23444, 22970, 22499, 22029, 21562, 21098,
3735
;   20636, 20175, 19718, 19262, 18808, 18357, 17908, 17461, 17016, 16573, 16132,
3736
;   15694, 15257, 14822, 14390, 13959, 13530, 13103, 12678, 12255, 11834, 11415,
3737
;   10997, 10582, 10168, 9756, 9346, 8937, 8531, 8126, 7723, 7321, 6921, 6523,
3738
;   6127, 5732, 5339, 4947, 4557, 4169, 3782, 3397, 3014, 2632, 2251, 1872, 1495,
3739
;   1119, 744, 372
3740
end if
3741
3742
 
3743
align 4
3744
proc png_log8bit, x:dword
3745
;   uint lg2 = 0;
3746
	; Each time 'x' is multiplied by 2, 1 must be subtracted off the final log,
3747
	; because the log is actually negate that means adding 1.  The final
3748
	; returned value thus has the range 0 (for 255 input) to 7.994 (for 1
3749
	; input), return -1 for the overflow (log 0) case, - so the result is
3750
	; always at most 19 bits.
3751
3752
 
3753
;      return -1;
3754
3755
 
3756
;      lg2  = 4, x <<= 4;
3757
3758
 
3759
;      lg2 += 2, x <<= 2;
3760
3761
 
3762
;      lg2 += 1, x <<= 1;
3763
3764
 
3765
;   return (int_32)((lg2 << 16) + ((png_8bit_l2[x-128]+32768)>>16));
3766
	ret
3767
endp
3768
3769
 
3770
; for 16-bit images we use the most significant 8 bits of the 16-bit value to
3771
; get an approximation then multiply the approximation by a correction factor
3772
; determined by the remaining up to 8 bits.  This requires an additional step
3773
; in the 16-bit case.
3774
3775
 
3776
3777
 
3778
;          = v' * f
3779
3780
 
3781
; to 255 and v'' is in the range 0 to 255 f will be in the range 256 to less
3782
; than 258.  The final factor also needs to correct for the fact that our 8-bit
3783
; value is scaled by 255, whereas the 16-bit values must be scaled by 65535.
3784
3785
 
3786
; scaling by 65536 to match the above table:
3787
3788
 
3789
3790
 
3791
; interpolation between the two end values 256/257 (result -368.61) and 258/257
3792
; (result 367.179).  The values used below are scaled by a further 64 to give
3793
; 16-bit precision in the interpolation:
3794
3795
 
3796
; Zero  (257):      0
3797
; End   (258):  23499
3798
3799
 
3800
align 4
3801
proc png_log16bit, x:dword
3802
;   uint lg2 = 0;
3803
3804
 
3805
;   if ((x &= 0xffff) == 0)
3806
;      return -1;
3807
3808
 
3809
;      lg2  = 8, x <<= 8;
3810
3811
 
3812
;      lg2 += 4, x <<= 4;
3813
3814
 
3815
;      lg2 += 2, x <<= 2;
3816
3817
 
3818
;      lg2 += 1, x <<= 1;
3819
3820
 
3821
	; value.
3822
3823
 
3824
;   lg2 += (png_8bit_l2[(x>>8)-128]+8) >> 4;
3825
3826
 
3827
	; 8 bits.  Do this with maximum precision.
3828
3829
 
3830
3831
 
3832
	; the value at 1<<16 (ignoring this) will be 0 or 1; this gives us exactly
3833
	; 16 bits to interpolate to get the low bits of the result.  Round the
3834
	; answer.  Note that the end point values are scaled by 64 to retain overall
3835
	; precision and that 'lg2' is current scaled by an extra 12 bits, so adjust
3836
	; the overall scaling by 6-12.  Round at every step.
3837
3838
 
3839
3840
 
3841
;      lg2 += ((23591U * (65536U-x)) + (1U << (16+6-12-1))) >> (16+6-12);
3842
3843
 
3844
;      lg2 -= ((23499U * (x-65536U)) + (1U << (16+6-12-1))) >> (16+6-12);
3845
3846
 
3847
;   return (int_32)((lg2 + 2048) >> 12);
3848
	ret
3849
endp
3850
3851
 
3852
; logarithmic value and returning a 16 or 8-bit number as appropriate.  In
3853
; each case only the low 16 bits are relevant - the fraction - since the
3854
; integer bits (the top 4) simply determine a shift.
3855
3856
 
3857
; requires perhaps spurious accuracy in the decoding of the logarithm to
3858
; distinguish log2(65535/65534.5) - 10^-5 or 17 bits.  There is little chance
3859
; of getting this accuracy in practice.
3860
3861
 
3862
; frational part of the logarithm by using an accurate 32-bit value from the
3863
; top four fractional bits then multiplying in the remaining bits.
3864
3865
 
3866
align 4
3867
png_32bit_exp dd 4294967295, 4112874773, 3938502376, 3771522796, 3611622603, 3458501653,\
3868
	3311872529, 3171459999, 3037000500, 2908241642, 2784941738, 2666869345,\
3869
	2553802834, 2445529972, 2341847524, 2242560872
3870
3871
 
3872
;#if 0
3873
;for (i=11;i>=0;--i){ print i, " ", (1 - e(-(2^i)/65536*l(2))) * 2^(32-i), "\n"}
3874
;   11 44937.64284865548751208448
3875
;   10 45180.98734845585101160448
3876
;    9 45303.31936980687359311872
3877
;    8 45364.65110595323018870784
3878
;    7 45395.35850361789624614912
3879
;    6 45410.72259715102037508096
3880
;    5 45418.40724413220722311168
3881
;    4 45422.25021786898173001728
3882
;    3 45424.17186732298419044352
3883
;    2 45425.13273269940811464704
3884
;    1 45425.61317555035558641664
3885
;    0 45425.85339951654943850496
3886
;end if
3887
3888
 
3889
align 4
3890
proc png_exp, x:dword
3891
;   if (x > 0 && x <= 0xfffff) /* Else overflow or zero (underflow) */
3892
;   {
3893
	; Obtain a 4-bit approximation
3894
;      uint_32 e = png_32bit_exp[(x >> 12) & 0x0f];
3895
3896
 
3897
	; multiplying by a number less than 1 if the bit is set.  The multiplier
3898
	; is determined by the above table and the shift. Notice that the values
3899
	; converge on 45426 and this is used to allow linear interpolation of the
3900
	; low bits.
3901
3902
 
3903
;         e -= (((e >> 16) * 44938U) +  16U) >> 5;
3904
3905
 
3906
;         e -= (((e >> 16) * 45181U) +  32U) >> 6;
3907
3908
 
3909
;         e -= (((e >> 16) * 45303U) +  64U) >> 7;
3910
3911
 
3912
;         e -= (((e >> 16) * 45365U) + 128U) >> 8;
3913
3914
 
3915
;         e -= (((e >> 16) * 45395U) + 256U) >> 9;
3916
3917
 
3918
;         e -= (((e >> 16) * 45410U) + 512U) >> 10;
3919
3920
 
3921
;      e -= (((e >> 16) * 355U * (x & 0x3fU)) + 256U) >> 9;
3922
3923
 
3924
;      e >>= x >> 16;
3925
;      return e;
3926
;   }
3927
3928
 
3929
;   if (x <= 0)
3930
;      return png_32bit_exp[0];
3931
3932
 
3933
;   return 0;
3934
	ret
3935
endp
3936
3937
 
3938
align 4
3939
proc png_exp8bit, lg2:dword
3940
	; Get a 32-bit value:
3941
;   uint_32 x = png_exp(lg2);
3942
3943
 
3944
	; second, rounding, step can't overflow because of the first, subtraction,
3945
	; step.
3946
3947
 
3948
;   return (byte)(((x + 0x7fffffU) >> 24) & 0xff);
3949
	ret
3950
endp
3951
3952
 
3953
align 4
3954
proc png_exp16bit, lg2:dword
3955
	; Get a 32-bit value:
3956
;   uint_32 x = png_exp(lg2);
3957
3958
 
3959
;   x -= x >> 16;
3960
;   return (uint_16)((x + 32767U) >> 16);
3961
	ret
3962
endp
3963
;end if /* FLOATING_ARITHMETIC */
3964
3965
 
3966
align 4
3967
proc png_gamma_8bit_correct, value:dword, gamma_val:dword
3968
;   if (value > 0 && value < 255)
3969
;   {
3970
if PNG_FLOATING_ARITHMETIC_SUPPORTED eq 1
3971
	; 'value' is unsigned, ANSI-C90 requires the compiler to correctly
3972
	; convert this to a floating point value.  This includes values that
3973
	; would overflow if 'value' were to be converted to 'int'.
3974
3975
 
3976
	; on some (ARM) but not all (x86) platforms, possibly because of
3977
	; hardware FP limitations.  (E.g. if the hardware conversion always
3978
	; assumes the integer register contains a signed value.)  This results
3979
	; in ANSI-C undefined behavior for large values.
3980
3981
 
3982
	; conformant and therefore compile spurious extra code for the large
3983
	; values.
3984
3985
 
3986
	; won't be faster than an int to float one.  Therefore this code
3987
	; assumes responsibility for the undefined behavior, which it knows
3988
	; can't happen because of the check above.
3989
3990
 
3991
	; 16-bit platforms, it is assigned a value which might be out of
3992
	; range for an (int); that would result in undefined behavior in the
3993
	; caller if the *argument* ('value') were to be declared (int).
3994
3995
 
3996
;         return (byte)r;
3997
else
3998
;         int_32 lg2 = png_log8bit(value);
3999
;         png_fixed_point res;
4000
4001
 
4002
;            return png_exp8bit(res);
4003
4004
 
4005
;         value = 0;
4006
end if
4007
;   }
4008
4009
 
4010
	ret
4011
endp
4012
4013
 
4014
align 4
4015
proc png_gamma_16bit_correct, value:dword, gamma_val:dword
4016
;   if (value > 0 && value < 65535)
4017
;   {
4018
if PNG_FLOATING_ARITHMETIC_SUPPORTED eq 1
4019
	; The same (uint)->(double) constraints apply here as above,
4020
	; however in this case the (uint) to (int) conversion can
4021
	; overflow on an ANSI-C90 compliant system so the cast needs to ensure
4022
	; that this is not possible.
4023
4024
 
4025
;          gamma_val*.00001)+.5);
4026
;      return (uint_16)r;
4027
else
4028
;      int_32 lg2 = png_log16bit(value);
4029
;      png_fixed_point res;
4030
4031
 
4032
;         return png_exp16bit(res);
4033
4034
 
4035
;      value = 0;
4036
end if
4037
;   }
4038
4039
 
4040
	ret
4041
endp
4042
4043
 
4044
; png_struct, interpreting values as 8-bit or 16-bit.  While the result
4045
; is nominally a 16-bit value if bit depth is 8 then the result is
4046
; 8-bit (as are the arguments.)
4047
4048
 
4049
align 4
4050
proc png_gamma_correct, png_ptr:dword, value:dword, gamma_val:dword
4051
;   if (png_ptr->bit_depth == 8)
4052
;      return png_gamma_8bit_correct(value, gamma_val);
4053
;
4054
if PNG_16BIT_SUPPORTED eq 1
4055
;   else
4056
;      return png_gamma_16bit_correct(value, gamma_val);
4057
else
4058
	; should not reach this
4059
	xor eax,eax
4060
end if ;16BIT
4061
.end_f:
4062
	ret
4063
endp
4064
4065
 
4066
; Internal function to build a single 16-bit table - the table consists of
4067
; 'num' 256 entry subtables, where 'num' is determined by 'shift' - the amount
4068
; to shift the input values right (or 16-number_of_signifiant_bits).
4069
4070
 
4071
; png_error (i.e. if one of the mallocs below fails) - i.e. the *table argument
4072
; should be somewhere that will be cleaned.
4073
4074
 
4075
align 4
4076
proc png_build_16bit_table, png_ptr:dword, ptable:dword, shift:dword, gamma_val:dword
4077
	; Various values derived from 'shift':
4078
;    uint num = 1U << (8U - shift);
4079
if PNG_FLOATING_ARITHMETIC_SUPPORTED eq 1
4080
	; CSE the division and work round wacky GCC warnings (see the comments
4081
	; in png_gamma_8bit_correct for where these come from.)
4082
4083
 
4084
end if
4085
;    uint max = (1U << (16U - shift))-1U;
4086
;    uint max_by_2 = 1U << (15U-shift);
4087
;   uint i;
4088
4089
 
4090
;       (uint_16pp)png_calloc(png_ptr, num * (sizeof (uint_16p)));
4091
4092
 
4093
;   {
4094
;      uint_16p sub_table = table[i] =
4095
;          (uint_16p)png_malloc(png_ptr, 256 * (sizeof (uint_16)));
4096
4097
 
4098
	; the 16-bit tables even if the others don't hit it.
4099
4100
 
4101
;      {
4102
	; The old code would overflow at the end and this would cause the
4103
	; 'pow' function to return a result >1, resulting in an
4104
	; arithmetic error.  This code follows the spec exactly; ig is
4105
	; the recovered input sample, it always has 8-16 bits.
4106
4107
 
4108
	; bits (unsigned) so long as max <= 32767.
4109
4110
 
4111
;         for (j = 0; j < 256; j++)
4112
;         {
4113
;            uint_32 ig = (j << (8-shift)) + i;
4114
if PNG_FLOATING_ARITHMETIC_SUPPORTED eq 1
4115
		; Inline the 'max' scaling operation:
4116
		; See png_gamma_8bit_correct for why the cast to (int) is
4117
		; required here.
4118
4119
 
4120
;               sub_table[j] = (uint_16)d;
4121
else
4122
;               if (shift != 0)
4123
;                  ig = (ig * 65535U + max_by_2)/max;
4124
;
4125
;               sub_table[j] = png_gamma_16bit_correct(ig, gamma_val);
4126
end if
4127
;         }
4128
;      }
4129
;      else
4130
;      {
4131
		; We must still build a table, but do it the fast way.
4132
;         uint j;
4133
;
4134
;         for (j = 0; j < 256; j++)
4135
;         {
4136
;            uint_32 ig = (j << (8-shift)) + i;
4137
;
4138
;            if (shift != 0)
4139
;               ig = (ig * 65535U + max_by_2)/max;
4140
;
4141
;            sub_table[j] = (uint_16)ig;
4142
;         }
4143
;      }
4144
;   }
4145
	ret
4146
endp
4147
4148
 
4149
; required.
4150
4151
 
4152
align 4
4153
proc png_build_16to8_table, png_ptr:dword, ptable:dword, shift:dword, gamma_val:dword
4154
;   uint num = 1U << (8U - shift);
4155
;   uint max = (1U << (16U - shift))-1U;
4156
;   uint i;
4157
;   uint_32 last;
4158
4159
 
4160
;       (uint_16pp)png_calloc(png_ptr, num * (sizeof (uint_16p)));
4161
4162
 
4163
	; bits of the input 16-bit value used to select a table.  Each table is
4164
	; itself indexed by the high 8 bits of the value.
4165
4166
 
4167
;      table[i] = (uint_16p)png_malloc(png_ptr,
4168
;          256 * (sizeof (uint_16)));
4169
4170
 
4171
	; pow(out,g) is an *input* value.  'last' is the last input value set.
4172
	;
4173
	; In the loop 'i' is used to find output values.  Since the output is
4174
	; 8-bit there are only 256 possible values.  The tables are set up to
4175
	; select the closest possible output value for each input by finding
4176
	; the input value at the boundary between each pair of output values
4177
	; and filling the table up to that boundary with the lower output
4178
	; value.
4179
4180
 
4181
	; values the code below uses a 16-bit value in i; the values start at
4182
	; 128.5 (for 0.5) and step by 257, for a total of 254 values (the last
4183
	; entries are filled with 255).  Start i at 128 and fill all 'last'
4184
	; table entries <= 'max'
4185
4186
 
4187
;   for (i = 0; i < 255; ++i) /* 8-bit output value */
4188
;   {
4189
	; Find the corresponding maximum input value
4190
;      uint_16 out = (uint_16)(i * 257U); /* 16-bit output value */
4191
4192
 
4193
;      uint_32 bound = png_gamma_16bit_correct(out+128U, gamma_val);
4194
4195
 
4196
;      bound = (bound * max + 32768U)/65535U + 1U;
4197
;
4198
;      while (last < bound)
4199
;      {
4200
;         table[last & (0xffU >> shift)][last >> (8U - shift)] = out;
4201
;         last++;
4202
;      }
4203
;   }
4204
4205
 
4206
;   while (last < (num << 8))
4207
;   {
4208
;      table[last & (0xff >> shift)][last >> (8U - shift)] = 65535U;
4209
;      last++;
4210
;   }
4211
	ret
4212
endp
4213
;end if /* 16BIT */
4214
4215
 
4216
; typically much faster).  Note that libpng currently does no sBIT processing
4217
; (apparently contrary to the spec) so a 256-entry table is always generated.
4218
4219
 
4220
align 4
4221
proc png_build_8bit_table, png_ptr:dword, ptable:dword, gamma_val:dword
4222
;   uint i;
4223
;   bytep table = *ptable = (bytep)png_malloc(png_ptr, 256);
4224
4225
 
4226
;      for (i=0; i<256; i++)
4227
;         table[i] = png_gamma_8bit_correct(i, gamma_val);
4228
4229
 
4230
;      for (i=0; i<256; ++i)
4231
;         table[i] = (byte)(i & 0xff);
4232
	ret
4233
endp
4234
4235
 
4236
; tables.
4237
4238
 
4239
align 4
4240
proc png_destroy_gamma_table, png_ptr:dword
4241
;   png_free(png_ptr, png_ptr->gamma_table);
4242
;   png_ptr->gamma_table = NULL;
4243
4244
 
4245
;   if (png_ptr->gamma_16_table != NULL)
4246
;   {
4247
;      int i;
4248
;      int istop = (1 << (8 - png_ptr->gamma_shift));
4249
;      for (i = 0; i < istop; i++)
4250
;      {
4251
;         png_free(png_ptr, png_ptr->gamma_16_table[i]);
4252
;      }
4253
;   png_free(png_ptr, png_ptr->gamma_16_table);
4254
;   png_ptr->gamma_16_table = NULL;
4255
;   }
4256
end if ;16BIT
4257
4258
 
4259
;   defined(PNG_READ_ALPHA_MODE_SUPPORTED) || \
4260
;   defined(PNG_READ_RGB_TO_GRAY_SUPPORTED)
4261
;   png_free(png_ptr, png_ptr->gamma_from_1);
4262
;   png_ptr->gamma_from_1 = NULL;
4263
;   png_free(png_ptr, png_ptr->gamma_to_1);
4264
;   png_ptr->gamma_to_1 = NULL;
4265
4266
 
4267
;   if (png_ptr->gamma_16_from_1 != NULL)
4268
;   {
4269
;      int i;
4270
;      int istop = (1 << (8 - png_ptr->gamma_shift));
4271
;      for (i = 0; i < istop; i++)
4272
;      {
4273
;         png_free(png_ptr, png_ptr->gamma_16_from_1[i]);
4274
;      }
4275
;   png_free(png_ptr, png_ptr->gamma_16_from_1);
4276
;   png_ptr->gamma_16_from_1 = NULL;
4277
;   }
4278
;   if (png_ptr->gamma_16_to_1 != NULL)
4279
;   {
4280
;      int i;
4281
;      int istop = (1 << (8 - png_ptr->gamma_shift));
4282
;      for (i = 0; i < istop; i++)
4283
;      {
4284
;         png_free(png_ptr, png_ptr->gamma_16_to_1[i]);
4285
;      }
4286
;   png_free(png_ptr, png_ptr->gamma_16_to_1);
4287
;   png_ptr->gamma_16_to_1 = NULL;
4288
;   }
4289
end if ;16BIT
4290
;end if /* READ_BACKGROUND || READ_ALPHA_MODE || RGB_TO_GRAY */
4291
	ret
4292
endp
4293
4294
 
4295
; tables, we don't make a full table if we are reducing to 8-bit in
4296
; the future.  Note also how the gamma_16 tables are segmented so that
4297
; we don't need to allocate > 64K chunks for a full 16-bit table.
4298
4299
 
4300
align 4
4301
proc png_build_gamma_table, png_ptr:dword, bit_depth:dword
4302
	png_debug 1, 'in png_build_gamma_table'
4303
4304
 
4305
	; png_read_update_info. The warning is because building the gamma tables
4306
	; multiple times is a performance hit - it's harmless but the ability to
4307
	; call png_read_update_info() multiple times is new in 1.5.6 so it seems
4308
	; sensible to warn if the app introduces such a hit.
4309
4310
 
4311
;   {
4312
;      png_warning(png_ptr, "gamma table being rebuilt");
4313
;      png_destroy_gamma_table(png_ptr);
4314
;   }
4315
4316
 
4317
;   {
4318
;      png_build_8bit_table(png_ptr, &png_ptr->gamma_table,
4319
;          png_ptr->screen_gamma > 0 ?
4320
;          png_reciprocal2(png_ptr->colorspace.gamma,
4321
;          png_ptr->screen_gamma) : PNG_FP_1);
4322
;
4323
if (PNG_READ_BACKGROUND_SUPPORTED eq 1) | (PNG_READ_ALPHA_MODE_SUPPORTED eq 1) | (PNG_READ_RGB_TO_GRAY_SUPPORTED eq 1)
4324
;      if ((png_ptr->transformations & (PNG_COMPOSE | PNG_RGB_TO_GRAY)) != 0)
4325
;      {
4326
;         png_build_8bit_table(png_ptr, &png_ptr->gamma_to_1,
4327
;             png_reciprocal(png_ptr->colorspace.gamma));
4328
;
4329
;         png_build_8bit_table(png_ptr, &png_ptr->gamma_from_1,
4330
;             png_ptr->screen_gamma > 0 ?
4331
;             png_reciprocal(png_ptr->screen_gamma) :
4332
;             png_ptr->colorspace.gamma/* Probably doing rgb_to_gray */);
4333
;      }
4334
end if ;READ_BACKGROUND || READ_ALPHA_MODE || RGB_TO_GRAY
4335
;   }
4336
if PNG_16BIT_SUPPORTED eq 1
4337
;   else
4338
;   {
4339
;      byte shift, sig_bit;
4340
;
4341
;      if ((png_ptr->color_type & PNG_COLOR_MASK_COLOR) != 0)
4342
;      {
4343
;         sig_bit = png_ptr->sig_bit.red;
4344
;
4345
;         if (png_ptr->sig_bit.green > sig_bit)
4346
;            sig_bit = png_ptr->sig_bit.green;
4347
;
4348
;         if (png_ptr->sig_bit.blue > sig_bit)
4349
;            sig_bit = png_ptr->sig_bit.blue;
4350
;      }
4351
;      else
4352
;         sig_bit = png_ptr->sig_bit.gray;
4353
4354
 
4355
4356
 
4357
4358
 
4359
	; pow(iv, gamma).
4360
4361
 
4362
	; is selected by the (8-gamma_shift) most significant of the low 8 bits
4363
	; of the color value then indexed by the upper 8 bits:
4364
	;
4365
	;   table[low bits][high 8 bits]
4366
4367
 
4368
4369
 
4370
4371
 
4372
 
4373
;         /* shift == insignificant bits */
4374
;         shift = (byte)((16U - sig_bit) & 0xff);
4375
4376
 
4377
;         shift = 0; /* keep all 16 bits */
4378
4379
 
4380
;      {
4381
	; PNG_MAX_GAMMA_8 is the number of bits to keep - effectively
4382
	; the significant bits in the *input* when the output will
4383
	; eventually be 8 bits.  By default it is 11.
4384
4385
 
4386
;            shift = (16U - PNG_MAX_GAMMA_8);
4387
;      }
4388
4389
 
4390
;         shift = 8U; /* Guarantees at least one table! */
4391
4392
 
4393
4394
 
4395
	; PNG_COMPOSE).  This effectively smashed the background calculation for
4396
	; 16-bit output because the 8-bit table assumes the result will be
4397
	; reduced to 8 bits.
4398
4399
 
4400
;          png_build_16to8_table(png_ptr, &png_ptr->gamma_16_table, shift,
4401
;          png_ptr->screen_gamma > 0 ? png_product2(png_ptr->colorspace.gamma,
4402
;          png_ptr->screen_gamma) : PNG_FP_1);
4403
;
4404
;      else
4405
;          png_build_16bit_table(png_ptr, &png_ptr->gamma_16_table, shift,
4406
;          png_ptr->screen_gamma > 0 ? png_reciprocal2(png_ptr->colorspace.gamma,
4407
;          png_ptr->screen_gamma) : PNG_FP_1);
4408
;
4409
if (PNG_READ_BACKGROUND_SUPPORTED eq 1) | (PNG_READ_ALPHA_MODE_SUPPORTED eq 1) | (PNG_READ_RGB_TO_GRAY_SUPPORTED eq 1)
4410
;      if ((png_ptr->transformations & (PNG_COMPOSE | PNG_RGB_TO_GRAY)) != 0)
4411
;      {
4412
;         png_build_16bit_table(png_ptr, &png_ptr->gamma_16_to_1, shift,
4413
;             png_reciprocal(png_ptr->colorspace.gamma));
4414
4415
 
4416
	; the lookup on this table still uses gamma_shift, so it can't be.
4417
	; TODO: fix this.
4418
4419
 
4420
;             png_ptr->screen_gamma > 0 ? png_reciprocal(png_ptr->screen_gamma) :
4421
;             png_ptr->colorspace.gamma/* Probably doing rgb_to_gray */);
4422
;      }
4423
end if ;READ_BACKGROUND || READ_ALPHA_MODE || RGB_TO_GRAY
4424
;   }
4425
end if ;16BIT
4426
	ret
4427
endp
4428
;end if /* READ_GAMMA */
4429
4430
 
4431
;int (png_structrp png_ptr, int option, int onoff)
4432
align 4
4433
proc png_set_option uses ecx, png_ptr:dword, option:dword, onoff:dword
4434
	mov eax,[png_ptr]
4435
	cmp eax,0
4436
	je @f
4437
	mov ecx,[option]
4438
	cmp ecx,0
4439
	jl @f
4440
	cmp ecx,PNG_OPTION_NEXT
4441
	jge @f
4442
	bt ecx,0 ;cmp (ecx & 1), 0
4443
	jc @f ;if (..!=0 && ..>=0 && ..<.. && ..==0)
4444
;      int mask = 3 << option;
4445
;      int setting = (2 + (onoff != 0)) << option;
4446
;      int current = png_ptr->options;
4447
4448
 
4449
4450
 
4451
		jmp .end_f
4452
	@@:
4453
	mov eax,PNG_OPTION_INVALID
4454
.end_f:
4455
	ret
4456
endp
4457
4458
 
4459
if (PNG_SIMPLIFIED_READ_SUPPORTED eq 1) | (PNG_SIMPLIFIED_WRITE_SUPPORTED eq 1)
4460
; sRGB conversion tables; these are machine generated with the code in
4461
; contrib/tools/makesRGB.c.  The actual sRGB transfer curve defined in the
4462
; specification (see the article at http://en.wikipedia.org/wiki/SRGB)
4463
; is used, not the gamma=1/2.2 approximation use elsewhere in libpng.
4464
; The sRGB to linear table is exact (to the nearest 16-bit linear fraction).
4465
; The inverse (linear to sRGB) table has accuracies as follows:
4466
4467
 
4468
;    error: -0.515566 - 0.625971, 79441 (0.475369%) of readings inexact
4469
4470
 
4471
;    error: -0.513727 - 0.607759, 308 (0.469978%) of readings inexact
4472
4473
 
4474
4475
 
4476
 
4477
; The convert-to-sRGB table is only currently required for read.
4478
align 4
4479
png_sRGB_table dw 0,20,40,60,80,99,119,139,\
4480
	159,179,199,219,241,264,288,313,\
4481
	340,367,396,427,458,491,526,562,\
4482
	599,637,677,718,761,805,851,898,\
4483
	947,997,1048,1101,1156,1212,1270,1330,\
4484
	1391,1453,1517,1583,1651,1720,1790,1863,\
4485
	1937,2013,2090,2170,2250,2333,2418,2504,\
4486
	2592,2681,2773,2866,2961,3058,3157,3258,\
4487
	3360,3464,3570,3678,3788,3900,4014,4129,\
4488
	4247,4366,4488,4611,4736,4864,4993,5124,\
4489
	5257,5392,5530,5669,5810,5953,6099,6246,\
4490
	6395,6547,6700,6856,7014,7174,7335,7500,\
4491
	7666,7834,8004,8177,8352,8528,8708,8889,\
4492
	9072,9258,9445,9635,9828,10022,10219,10417,\
4493
	10619,10822,11028,11235,11446,11658,11873,12090,\
4494
	12309,12530,12754,12980,13209,13440,13673,13909,\
4495
	14146,14387,14629,14874,15122,15371,15623,15878,\
4496
	16135,16394,16656,16920,17187,17456,17727,18001,\
4497
	18277,18556,18837,19121,19407,19696,19987,20281,\
4498
	20577,20876,21177,21481,21787,22096,22407,22721,\
4499
	23038,23357,23678,24002,24329,24658,24990,25325,\
4500
	25662,26001,26344,26688,27036,27386,27739,28094,\
4501
	28452,28813,29176,29542,29911,30282,30656,31033,\
4502
	31412,31794,32179,32567,32957,33350,33745,34143,\
4503
	34544,34948,35355,35764,36176,36591,37008,37429,\
4504
	37852,38278,38706,39138,39572,40009,40449,40891,\
4505
	41337,41785,42236,42690,43147,43606,44069,44534,\
4506
	45002,45473,45947,46423,46903,47385,47871,48359,\
4507
	48850,49344,49841,50341,50844,51349,51858,52369,\
4508
	52884,53401,53921,54445,54971,55500,56032,56567,\
4509
	57105,57646,58190,58737,59287,59840,60396,60955,\
4510
	61517,62082,62650,63221,63795,64372,64952,65535
4511
end if ;SIMPLIFIED_READ
4512
4513
 
4514
; only the simplified versions.)
4515
align 4
4516
png_sRGB_base dw 128,1782,3383,4644,5675,6564,7357,8074,\
4517
	8732,9346,9921,10463,10977,11466,11935,12384,\
4518
	12816,13233,13634,14024,14402,14769,15125,15473,\
4519
	15812,16142,16466,16781,17090,17393,17690,17981,\
4520
	18266,18546,18822,19093,19359,19621,19879,20133,\
4521
	20383,20630,20873,21113,21349,21583,21813,22041,\
4522
	22265,22487,22707,22923,23138,23350,23559,23767,\
4523
	23972,24175,24376,24575,24772,24967,25160,25352,\
4524
	25542,25730,25916,26101,26284,26465,26645,26823,\
4525
	27000,27176,27350,27523,27695,27865,28034,28201,\
4526
	28368,28533,28697,28860,29021,29182,29341,29500,\
4527
	29657,29813,29969,30123,30276,30429,30580,30730,\
4528
	30880,31028,31176,31323,31469,31614,31758,31902,\
4529
	32045,32186,32327,32468,32607,32746,32884,33021,\
4530
	33158,33294,33429,33564,33697,33831,33963,34095,\
4531
	34226,34357,34486,34616,34744,34873,35000,35127,\
4532
	35253,35379,35504,35629,35753,35876,35999,36122,\
4533
	36244,36365,36486,36606,36726,36845,36964,37083,\
4534
	37201,37318,37435,37551,37668,37783,37898,38013,\
4535
	38127,38241,38354,38467,38580,38692,38803,38915,\
4536
	39026,39136,39246,39356,39465,39574,39682,39790,\
4537
	39898,40005,40112,40219,40325,40431,40537,40642,\
4538
	40747,40851,40955,41059,41163,41266,41369,41471,\
4539
	41573,41675,41777,41878,41979,42079,42179,42279,\
4540
	42379,42478,42577,42676,42775,42873,42971,43068,\
4541
	43165,43262,43359,43456,43552,43648,43743,43839,\
4542
	43934,44028,44123,44217,44311,44405,44499,44592,\
4543
	44685,44778,44870,44962,45054,45146,45238,45329,\
4544
	45420,45511,45601,45692,45782,45872,45961,46051,\
4545
	46140,46229,46318,46406,46494,46583,46670,46758,\
4546
	46846,46933,47020,47107,47193,47280,47366,47452,\
4547
	47538,47623,47709,47794,47879,47964,48048,48133,\
4548
	48217,48301,48385,48468,48552,48635,48718,48801,\
4549
	48884,48966,49048,49131,49213,49294,49376,49458,\
4550
	49539,49620,49701,49782,49862,49943,50023,50103,\
4551
	50183,50263,50342,50422,50501,50580,50659,50738,\
4552
	50816,50895,50973,51051,51129,51207,51285,51362,\
4553
	51439,51517,51594,51671,51747,51824,51900,51977,\
4554
	52053,52129,52205,52280,52356,52432,52507,52582,\
4555
	52657,52732,52807,52881,52956,53030,53104,53178,\
4556
	53252,53326,53400,53473,53546,53620,53693,53766,\
4557
	53839,53911,53984,54056,54129,54201,54273,54345,\
4558
	54417,54489,54560,54632,54703,54774,54845,54916,\
4559
	54987,55058,55129,55199,55269,55340,55410,55480,\
4560
	55550,55620,55689,55759,55828,55898,55967,56036,\
4561
	56105,56174,56243,56311,56380,56448,56517,56585,\
4562
	56653,56721,56789,56857,56924,56992,57059,57127,\
4563
	57194,57261,57328,57395,57462,57529,57595,57662,\
4564
	57728,57795,57861,57927,57993,58059,58125,58191,\
4565
	58256,58322,58387,58453,58518,58583,58648,58713,\
4566
	58778,58843,58908,58972,59037,59101,59165,59230,\
4567
	59294,59358,59422,59486,59549,59613,59677,59740,\
4568
	59804,59867,59930,59993,60056,60119,60182,60245,\
4569
	60308,60370,60433,60495,60558,60620,60682,60744,\
4570
	60806,60868,60930,60992,61054,61115,61177,61238,\
4571
	61300,61361,61422,61483,61544,61605,61666,61727,\
4572
	61788,61848,61909,61969,62030,62090,62150,62211,\
4573
	62271,62331,62391,62450,62510,62570,62630,62689,\
4574
	62749,62808,62867,62927,62986,63045,63104,63163,\
4575
	63222,63281,63340,63398,63457,63515,63574,63632,\
4576
	63691,63749,63807,63865,63923,63981,64039,64097,\
4577
	64155,64212,64270,64328,64385,64443,64500,64557,\
4578
	64614,64672,64729,64786,64843,64900,64956,65013,\
4579
	65070,65126,65183,65239,65296,65352,65409,65465
4580
align 4
4581
png_sRGB_delta db 207,201,158,129,113,100,90,82,77,72,68,64,61,59,56,54,\
4582
	52,50,49,47,46,45,43,42,41,40,39,39,38,37,36,36,\
4583
	35,34,34,33,33,32,32,31,31,30,30,30,29,29,28,28,\
4584
	28,27,27,27,27,26,26,26,25,25,25,25,24,24,24,24,\
4585
	23,23,23,23,23,22,22,22,22,22,22,21,21,21,21,21,\
4586
	21,20,20,20,20,20,20,20,20,19,19,19,19,19,19,19,\
4587
	19,18,18,18,18,18,18,18,18,18,18,17,17,17,17,17,\
4588
	17,17,17,17,17,17,16,16,16,16,16,16,16,16,16,16,\
4589
	16,16,16,16,15,15,15,15,15,15,15,15,15,15,15,15,\
4590
	15,15,15,15,14,14,14,14,14,14,14,14,14,14,14,14,\
4591
	14,14,14,14,14,14,14,13,13,13,13,13,13,13,13,13,\
4592
	13,13,13,13,13,13,13,13,13,13,13,13,13,13,12,12,\
4593
	12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,\
4594
	12,12,12,12,12,12,12,12,12,12,12,12,11,11,11,11,\
4595
	11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,\
4596
	11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,\
4597
	11,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,\
4598
	10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,\
4599
	10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,\
4600
	10,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,\
4601
	9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,\
4602
	9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,\
4603
	9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,\
4604
	9,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,\
4605
	8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,\
4606
	8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,\
4607
	8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,\
4608
	8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,\
4609
	8,8,8,8,8,8,8,8,8,7,7,7,7,7,7,7,\
4610
	7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,\
4611
	7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,\
4612
	7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7
4613
4614
 
4615
4616
 
4617
;int (voidp argument)
4618
align 4
4619
proc png_image_free_function uses ebx ecx edi esi, argument:dword
4620
locals
4621
;   png_imagep image = argument;
4622
;   png_controlp cp = image->opaque;
4623
	c png_control
4624
endl
4625
	; Double check that we have a png_ptr - it should be impossible to get here
4626
	; without one.
4627
4628
 
4629
	mov esi,[ebx+png_image.opaque] ;esi = cp
4630
	cmp dword[esi+png_control.png_ptr],0
4631
	jne @f ;if (..==0)
4632
		xor eax,eax
4633
		jmp .end_f
4634
	@@:
4635
4636
 
4637
if PNG_STDIO_SUPPORTED eq 1
4638
;      if (cp->owned_file != 0)
4639
;      {
4640
;         FILE *fp = cp->png_ptr->io_ptr;
4641
;         cp->owned_file = 0;
4642
4643
 
4644
;         if (fp != NULL)
4645
;         {
4646
;            cp->png_ptr->io_ptr = NULL;
4647
;            (void)fclose(fp);
4648
;         }
4649
;      }
4650
end if
4651
4652
 
4653
	; safely freed.  Notice that a png_error here stops the remainder of the
4654
	; cleanup, but this is probably fine because that would indicate bad memory
4655
	; problems anyway.
4656
4657
 
4658
	mov edi,ebp
4659
	sub edi,ecx ;edi = &c
4660
	rep movsb
4661
	sub edi,sizeof.png_control
4662
	sub esi,sizeof.png_control
4663
	mov dword[ebx+png_image.opaque],edi
4664
	stdcall png_free, [edi+png_control.png_ptr], esi
4665
4666
 
4667
;   if (c.for_write != 0)
4668
;   {
4669
if PNG_SIMPLIFIED_WRITE_SUPPORTED eq 1
4670
;         png_destroy_write_struct(&c.png_ptr, &c.info_ptr);
4671
else
4672
;         png_error(c.png_ptr, "simplified write not supported");
4673
end if
4674
		jmp .end2
4675
	.end1: ;else
4676
if PNG_SIMPLIFIED_READ_SUPPORTED eq 1
4677
;         png_destroy_read_struct(&c.png_ptr, &c.info_ptr, NULL);
4678
else
4679
;         png_error(c.png_ptr, "simplified read not supported");
4680
end if
4681
	.end2:
4682
4683
 
4684
	xor eax,eax
4685
	inc eax
4686
.end_f:
4687
	ret
4688
endp
4689
4690
 
4691
align 4
4692
proc png_image_free uses eax ebx, image:dword
4693
	; Safely call the real function, but only if doing so is safe at this point
4694
	; (if not inside an error handling context).  Otherwise assume
4695
	; png_safe_execute will call this API after the return.
4696
4697
 
4698
	cmp ebx,0
4699
	je @f
4700
	cmp dword[ebx+png_image.opaque],0
4701
	je @f
4702
	mov eax,[ebx+png_image.opaque]
4703
	cmp dword[eax+png_control.error_buf],0
4704
	jne @f ;if (..!=0 && ..!=0 && ..==0)
4705
		; Ignore errors here:
4706
		stdcall png_safe_execute, ebx, png_image_free_function, ebx
4707
		mov dword[ebx+png_image.opaque],0
4708
	@@:
4709
	ret
4710
endp
4711
4712
 
4713
align 4
4714
proc png_image_error uses ebx, image:dword, error_message:dword
4715
	; Utility to log an error.
4716
	mov ebx,[image]
4717
	mov eax,ebx
4718
	add eax,png_image.message
4719
	stdcall png_safecat, eax, sizeof.png_image.message, 0, [error_message]
4720
	or dword[ebx+png_image.warning_or_error], PNG_IMAGE_ERROR
4721
	stdcall png_image_free, ebx
4722
	xor eax,eax
4723
	ret
4724
endp
4725
4726
 
4727
>