Subversion Repositories Kolibri OS

Rev

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