Subversion Repositories Kolibri OS

Rev

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