Subversion Repositories Kolibri OS

Rev

Rev 8341 | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

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