Subversion Repositories Kolibri OS

Rev

Rev 6797 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

  1.  
  2. ; pngtrans.asm - transforms the data in a row (used by both readers and writers)
  3.  
  4. ; Last changed in libpng 1.6.24 [August 4, 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.  
  14. if (PNG_READ_SUPPORTED eq 1) | (PNG_WRITE_SUPPORTED eq 1)
  15.  
  16. ; Turn on BGR-to-RGB mapping
  17. ;void (png_structrp png_ptr)
  18. align 4
  19. proc png_set_bgr uses edi, png_ptr:dword
  20.         png_debug 1, 'in png_set_bgr'
  21.  
  22.         mov edi,[png_ptr]
  23.         test edi,edi
  24.         jz @f ;if (..==0) return
  25.                 or dword[edi+png_struct.transformations], PNG_BGR
  26.         @@:
  27.         ret
  28. endp
  29.  
  30. ; Turn on 16-bit byte swapping
  31. ;void (png_structrp png_ptr)
  32. align 4
  33. proc png_set_swap uses edi, png_ptr:dword
  34.         png_debug 1, 'in png_set_swap'
  35.  
  36.         mov edi,[png_ptr]
  37.         test edi,edi
  38.         jz @f ;if (..==0) return
  39.  
  40.         cmp byte[edi+png_struct.bit_depth],16
  41.         jne @f ;if (..==..)
  42.                 or dword[edi+png_struct.transformations], PNG_SWAP_BYTES
  43.         @@:
  44.         ret
  45. endp
  46.  
  47. ; Turn on pixel packing
  48. ;void (png_structrp png_ptr)
  49. align 4
  50. proc png_set_packing uses edi, png_ptr:dword
  51.         png_debug 1, 'in png_set_packing'
  52.  
  53.         mov edi,[png_ptr]
  54.         test edi,edi
  55.         jz @f ;if (..==0) return
  56.  
  57.         cmp byte[edi+png_struct.bit_depth],8
  58.         jge @f ;if (..<..)
  59.                 or dword[edi+png_struct.transformations], PNG_PACK
  60. if PNG_WRITE_SUPPORTED eq 1
  61.                 mov byte[edi+png_struct.usr_bit_depth],8
  62. end if
  63.         @@:
  64.         ret
  65. endp
  66.  
  67. ; Turn on packed pixel swapping
  68. ;void (png_structrp png_ptr)
  69. align 4
  70. proc png_set_packswap uses edi, png_ptr:dword
  71.         png_debug 1, 'in png_set_packswap'
  72.  
  73.         mov edi,[png_ptr]
  74.         test edi,edi
  75.         jz @f ;if (..==0) return
  76.  
  77.         cmp byte[edi+png_struct.bit_depth],8
  78.         jge @f ;if (..<..)
  79.                 or dword[edi+png_struct.transformations], PNG_PACKSWAP
  80.         @@:
  81.         ret
  82. endp
  83.  
  84. ;void (png_structrp png_ptr, png_color_8p true_bits)
  85. align 4
  86. proc png_set_shift uses ecx edi, png_ptr:dword, true_bits:dword
  87.         png_debug 1, 'in png_set_shift'
  88.  
  89.         mov edi,[png_ptr]
  90.         test edi,edi
  91.         jz @f ;if (..==0) return
  92.  
  93.         or dword[edi+png_struct.transformations], PNG_SHIFT
  94.         mov ecx,sizeof.png_color_8
  95.         mov edi,[edi+png_struct.shift]
  96.         mov esi,[true_bits]
  97.         rep movsb
  98.         @@:
  99.         ret
  100. endp
  101.  
  102. ;int (png_structrp png_ptr)
  103. align 4
  104. proc png_set_interlace_handling uses edi, png_ptr:dword
  105.         png_debug 1, 'in png_set_interlace handling'
  106.  
  107.         mov edi,[png_ptr]
  108.         test edi,edi
  109.         jz @f
  110.         cmp byte[edi+png_struct.interlaced],0
  111.         je @f ;if(..!=0 && ..!=0)
  112.                 or dword[edi+png_struct.transformations], PNG_INTERLACE
  113.                 mov eax,7
  114.                 jmp .end_f
  115.         @@:
  116.  
  117.         xor eax,eax
  118.         inc eax
  119. .end_f:
  120.         ret
  121. endp
  122.  
  123. ; Add a filler byte on read, or remove a filler or alpha byte on write.
  124. ; The filler type has changed in v0.95 to allow future 2-byte fillers
  125. ; for 48-bit input data, as well as to avoid problems with some compilers
  126. ; that don't like bytes as parameters.
  127.  
  128. ;void (png_structrp png_ptr, uint_32 filler, int filler_loc)
  129. align 4
  130. proc png_set_filler uses eax edi, png_ptr:dword, filler:dword, filler_loc:dword
  131.         png_debug 1, 'in png_set_filler'
  132.  
  133.         mov edi,[png_ptr]
  134.         test edi,edi
  135.         jz .end_f ;if (..==0) return
  136.  
  137.         ; In libpng 1.6 it is possible to determine whether this is a read or write
  138.         ; operation and therefore to do more checking here for a valid call.
  139.  
  140.         mov eax,[edi+png_struct.mode]
  141.         and eax,PNG_IS_READ_STRUCT
  142.         jz @f ;if (..!=0)
  143. if PNG_READ_FILLER_SUPPORTED eq 1
  144.                 ; On read png_set_filler is always valid, regardless of the base PNG
  145.                 ; format, because other transformations can give a format where the
  146.                 ; filler code can execute (basically an 8 or 16-bit component RGB or G
  147.                 ; format.)
  148.  
  149.                 ; NOTE: usr_channels is not used by the read code!  (This has led to
  150.                 ; confusion in the past.)  The filler is only used in the read code.
  151.  
  152.                 mov eax,[filler]
  153.                 mov [edi+png_struct.filler],ax
  154.                 jmp .end0
  155. else
  156.                 cStr ,'png_set_filler not supported on read'
  157.                 stdcall png_app_error, edi, eax
  158.                 jmp .end_f
  159. end if
  160.         @@: ;else ;write
  161. if PNG_WRITE_FILLER_SUPPORTED eq 1
  162.         ; On write the usr_channels parameter must be set correctly at the
  163.         ; start to record the number of channels in the app-supplied data.
  164.  
  165. ;         switch (png_ptr->color_type)
  166. ;         {
  167. ;            case PNG_COLOR_TYPE_RGB:
  168. ;               png_ptr->usr_channels = 4;
  169. ;               break;
  170.  
  171. ;            case PNG_COLOR_TYPE_GRAY:
  172. ;               if (png_ptr->bit_depth >= 8)
  173. ;               {
  174. ;                  png_ptr->usr_channels = 2;
  175. ;                  break;
  176. ;               }
  177.  
  178. ;               else
  179. ;               {
  180.                         ; There simply isn't any code in libpng to strip out bits
  181.                         ; from bytes when the components are less than a byte in
  182.                         ; size!
  183.  
  184. ;                  png_app_error(png_ptr,
  185. ;                      "png_set_filler is invalid for"
  186. ;                      " low bit depth gray output");
  187. ;                  return;
  188. ;               }
  189.  
  190. ;            default:
  191. ;               png_app_error(png_ptr,
  192. ;                   "png_set_filler: inappropriate color type");
  193. ;               return;
  194. ;         }
  195. else
  196.                 cStr ,'png_set_filler not supported on write'
  197.                 stdcall png_app_error, edi, eax
  198.                 jmp .end_f
  199. end if
  200.         .end0:
  201.  
  202.         ; Here on success - libpng supports the operation, set the transformation
  203.         ; and the flag to say where the filler channel is.
  204.  
  205.         or dword[edi+png_struct.transformations],PNG_FILLER
  206.  
  207.         cmp dword[filler_loc],PNG_FILLER_AFTER
  208.         jne @f ;if (..==..)
  209.                 or dword[edi+png_struct.flags],PNG_FLAG_FILLER_AFTER
  210.                 jmp .end_f
  211.         @@: ;else
  212.                 and dword[edi+png_struct.flags],not PNG_FLAG_FILLER_AFTER
  213. .end_f:
  214.         ret
  215. endp
  216.  
  217. ; Added to libpng-1.2.7
  218. ;void (png_structrp png_ptr, uint_32 filler, int filler_loc)
  219. align 4
  220. proc png_set_add_alpha uses eax edi, png_ptr:dword, filler:dword, filler_loc:dword
  221.         png_debug 1, 'in png_set_add_alpha'
  222.  
  223.         mov edi,[png_ptr]
  224.         test edi,edi
  225.         jz .end_f ;if (..==0) return
  226.  
  227.         stdcall png_set_filler, edi, [filler], [filler_loc]
  228.         ; The above may fail to do anything.
  229.         mov eax,[edi+png_struct.transformations]
  230.         and eax,PNG_FILLER
  231.         jz .end_f ;if (..!=0)
  232.                 or dword[edi+png_struct.transformations],PNG_ADD_ALPHA
  233. .end_f:
  234.         ret
  235. endp
  236.  
  237. ;void (png_structrp png_ptr)
  238. align 4
  239. proc png_set_swap_alpha uses edi, png_ptr:dword
  240.         png_debug 1, 'in png_set_swap_alpha'
  241.  
  242.         mov edi,[png_ptr]
  243.         test edi,edi
  244.         jz .end_f ;if (..==0) return
  245.                 or dword[edi+png_struct.transformations], PNG_SWAP_ALPHA
  246. .end_f:
  247.         ret
  248. endp
  249.  
  250.  
  251. ;void (png_structrp png_ptr)
  252. align 4
  253. proc png_set_invert_alpha uses edi, png_ptr:dword
  254.         png_debug 1, 'in png_set_invert_alpha'
  255.  
  256.         mov edi,[png_ptr]
  257.         test edi,edi
  258.         jz .end_f ;if (..==0) return
  259.                 or dword[edi+png_struct.transformations], PNG_INVERT_ALPHA
  260. .end_f:
  261.         ret
  262. endp
  263.  
  264. ;void (png_structrp png_ptr)
  265. align 4
  266. proc png_set_invert_mono uses edi, png_ptr:dword
  267.         png_debug 1, 'in png_set_invert_mono'
  268.  
  269.         mov edi,[png_ptr]
  270.         test edi,edi
  271.         jz .end_f ;if (..==0) return
  272.                 or dword[edi+png_struct.transformations], PNG_INVERT_MONO
  273. .end_f:
  274.         ret
  275. endp
  276.  
  277. ; Invert monochrome grayscale data
  278. ;void (png_row_infop row_info, bytep row)
  279. align 4
  280. proc png_do_invert uses eax ebx ecx edx, row_info:dword, row:dword
  281.         ;ecx - i
  282.         ;eax - rp
  283.         ;edx - istop
  284.         png_debug 1, 'in png_do_invert'
  285.  
  286.         mov ebx,[row_info]
  287.         cmp byte[ebx+png_row_info.color_type],PNG_COLOR_TYPE_GRAY
  288.         jne .end0
  289.         mov eax,[row]
  290.         mov edx,[ebx+png_row_info.rowbytes]
  291.         xor ecx,ecx
  292.         jmp @f
  293. align 4
  294.         .cycle0:
  295.                 inc ecx
  296.         @@:
  297.                 cmp ecx,edx
  298.                 jae .end_f
  299.                 not byte[eax]
  300.                 inc eax
  301.                 jmp .cycle0
  302. .end0:
  303.         cmp byte[ebx+png_row_info.color_type],PNG_COLOR_TYPE_GRAY_ALPHA
  304.         jne .end1
  305.         cmp byte[ebx+png_row_info.bit_depth],8
  306.         jne .end1
  307.         mov eax,[row]
  308.         mov edx,[ebx+png_row_info.rowbytes]
  309.         xor ecx,ecx
  310.         jmp @f
  311. align 4
  312.         .cycle1:
  313.                 add ecx,2
  314.         @@:
  315.                 cmp ecx,edx
  316.                 jae .end_f
  317.                 not byte[eax]
  318.                 add eax,2
  319.                 jmp .cycle1
  320. .end1:
  321. if PNG_16BIT_SUPPORTED eq 1
  322.         cmp byte[ebx+png_row_info.color_type],PNG_COLOR_TYPE_GRAY_ALPHA
  323.         jne .end_f
  324.         cmp byte[ebx+png_row_info.bit_depth],16
  325.         jne .end_f
  326.         mov eax,[row]
  327.         mov edx,[ebx+png_row_info.rowbytes]
  328.         xor ecx,ecx
  329.         jmp @f
  330. align 4
  331.         .cycle2:
  332.                 add ecx,4
  333.         @@:
  334.                 cmp ecx,edx
  335.                 jae .end_f
  336.                 not word[eax]
  337.                 add eax,4
  338.                 jmp .cycle2
  339. end if
  340. .end_f:
  341.         ret
  342. endp
  343.  
  344. ; Swaps byte order on 16-bit depth images
  345. ;void (png_row_infop row_info, bytep row)
  346. align 4
  347. proc png_do_swap, row_info:dword, row:dword
  348.         png_debug 1, 'in png_do_swap'
  349.  
  350. ;   if (row_info->bit_depth == 16)
  351. ;   {
  352. ;      bytep rp = row;
  353. ;      uint_32 i;
  354. ;      uint_32 istop= row_info->width * row_info->channels;
  355.  
  356. ;      for (i = 0; i < istop; i++, rp += 2)
  357. ;      {
  358. if PNG_BUILTIN_BSWAP16_SUPPORTED eq 1
  359.         ; Feature added to libpng-1.6.11 for testing purposes, not
  360.         ; enabled by default.
  361.  
  362. ;         *(uint_16*)rp = __builtin_bswap16(*(uint_16*)rp);
  363. else
  364. ;         byte t = *rp;
  365. ;         *rp = *(rp + 1);
  366. ;         *(rp + 1) = t;
  367. end if
  368. ;      }
  369. ;   }
  370.         ret
  371. endp
  372.  
  373. if (PNG_READ_PACKSWAP_SUPPORTED eq 1) | (PNG_WRITE_PACKSWAP_SUPPORTED eq 1)
  374. align 4
  375. onebppswaptable db 0x00, 0x80, 0x40, 0xC0, 0x20, 0xA0, 0x60, 0xE0,\
  376.    0x10, 0x90, 0x50, 0xD0, 0x30, 0xB0, 0x70, 0xF0,\
  377.    0x08, 0x88, 0x48, 0xC8, 0x28, 0xA8, 0x68, 0xE8,\
  378.    0x18, 0x98, 0x58, 0xD8, 0x38, 0xB8, 0x78, 0xF8,\
  379.    0x04, 0x84, 0x44, 0xC4, 0x24, 0xA4, 0x64, 0xE4,\
  380.    0x14, 0x94, 0x54, 0xD4, 0x34, 0xB4, 0x74, 0xF4,\
  381.    0x0C, 0x8C, 0x4C, 0xCC, 0x2C, 0xAC, 0x6C, 0xEC,\
  382.    0x1C, 0x9C, 0x5C, 0xDC, 0x3C, 0xBC, 0x7C, 0xFC,\
  383.    0x02, 0x82, 0x42, 0xC2, 0x22, 0xA2, 0x62, 0xE2,\
  384.    0x12, 0x92, 0x52, 0xD2, 0x32, 0xB2, 0x72, 0xF2,\
  385.    0x0A, 0x8A, 0x4A, 0xCA, 0x2A, 0xAA, 0x6A, 0xEA,\
  386.    0x1A, 0x9A, 0x5A, 0xDA, 0x3A, 0xBA, 0x7A, 0xFA,\
  387.    0x06, 0x86, 0x46, 0xC6, 0x26, 0xA6, 0x66, 0xE6,\
  388.    0x16, 0x96, 0x56, 0xD6, 0x36, 0xB6, 0x76, 0xF6,\
  389.    0x0E, 0x8E, 0x4E, 0xCE, 0x2E, 0xAE, 0x6E, 0xEE,\
  390.    0x1E, 0x9E, 0x5E, 0xDE, 0x3E, 0xBE, 0x7E, 0xFE,\
  391.    0x01, 0x81, 0x41, 0xC1, 0x21, 0xA1, 0x61, 0xE1,\
  392.    0x11, 0x91, 0x51, 0xD1, 0x31, 0xB1, 0x71, 0xF1,\
  393.    0x09, 0x89, 0x49, 0xC9, 0x29, 0xA9, 0x69, 0xE9,\
  394.    0x19, 0x99, 0x59, 0xD9, 0x39, 0xB9, 0x79, 0xF9,\
  395.    0x05, 0x85, 0x45, 0xC5, 0x25, 0xA5, 0x65, 0xE5,\
  396.    0x15, 0x95, 0x55, 0xD5, 0x35, 0xB5, 0x75, 0xF5,\
  397.    0x0D, 0x8D, 0x4D, 0xCD, 0x2D, 0xAD, 0x6D, 0xED,\
  398.    0x1D, 0x9D, 0x5D, 0xDD, 0x3D, 0xBD, 0x7D, 0xFD,\
  399.    0x03, 0x83, 0x43, 0xC3, 0x23, 0xA3, 0x63, 0xE3,\
  400.    0x13, 0x93, 0x53, 0xD3, 0x33, 0xB3, 0x73, 0xF3,\
  401.    0x0B, 0x8B, 0x4B, 0xCB, 0x2B, 0xAB, 0x6B, 0xEB,\
  402.    0x1B, 0x9B, 0x5B, 0xDB, 0x3B, 0xBB, 0x7B, 0xFB,\
  403.    0x07, 0x87, 0x47, 0xC7, 0x27, 0xA7, 0x67, 0xE7,\
  404.    0x17, 0x97, 0x57, 0xD7, 0x37, 0xB7, 0x77, 0xF7,\
  405.    0x0F, 0x8F, 0x4F, 0xCF, 0x2F, 0xAF, 0x6F, 0xEF,\
  406.    0x1F, 0x9F, 0x5F, 0xDF, 0x3F, 0xBF, 0x7F, 0xFF
  407.  
  408. align 4
  409. twobppswaptable db 0x00, 0x40, 0x80, 0xC0, 0x10, 0x50, 0x90, 0xD0,\
  410.    0x20, 0x60, 0xA0, 0xE0, 0x30, 0x70, 0xB0, 0xF0,\
  411.    0x04, 0x44, 0x84, 0xC4, 0x14, 0x54, 0x94, 0xD4,\
  412.    0x24, 0x64, 0xA4, 0xE4, 0x34, 0x74, 0xB4, 0xF4,\
  413.    0x08, 0x48, 0x88, 0xC8, 0x18, 0x58, 0x98, 0xD8,\
  414.    0x28, 0x68, 0xA8, 0xE8, 0x38, 0x78, 0xB8, 0xF8,\
  415.    0x0C, 0x4C, 0x8C, 0xCC, 0x1C, 0x5C, 0x9C, 0xDC,\
  416.    0x2C, 0x6C, 0xAC, 0xEC, 0x3C, 0x7C, 0xBC, 0xFC,\
  417.    0x01, 0x41, 0x81, 0xC1, 0x11, 0x51, 0x91, 0xD1,\
  418.    0x21, 0x61, 0xA1, 0xE1, 0x31, 0x71, 0xB1, 0xF1,\
  419.    0x05, 0x45, 0x85, 0xC5, 0x15, 0x55, 0x95, 0xD5,\
  420.    0x25, 0x65, 0xA5, 0xE5, 0x35, 0x75, 0xB5, 0xF5,\
  421.    0x09, 0x49, 0x89, 0xC9, 0x19, 0x59, 0x99, 0xD9,\
  422.    0x29, 0x69, 0xA9, 0xE9, 0x39, 0x79, 0xB9, 0xF9,\
  423.    0x0D, 0x4D, 0x8D, 0xCD, 0x1D, 0x5D, 0x9D, 0xDD,\
  424.    0x2D, 0x6D, 0xAD, 0xED, 0x3D, 0x7D, 0xBD, 0xFD,\
  425.    0x02, 0x42, 0x82, 0xC2, 0x12, 0x52, 0x92, 0xD2,\
  426.    0x22, 0x62, 0xA2, 0xE2, 0x32, 0x72, 0xB2, 0xF2,\
  427.    0x06, 0x46, 0x86, 0xC6, 0x16, 0x56, 0x96, 0xD6,\
  428.    0x26, 0x66, 0xA6, 0xE6, 0x36, 0x76, 0xB6, 0xF6,\
  429.    0x0A, 0x4A, 0x8A, 0xCA, 0x1A, 0x5A, 0x9A, 0xDA,\
  430.    0x2A, 0x6A, 0xAA, 0xEA, 0x3A, 0x7A, 0xBA, 0xFA,\
  431.    0x0E, 0x4E, 0x8E, 0xCE, 0x1E, 0x5E, 0x9E, 0xDE,\
  432.    0x2E, 0x6E, 0xAE, 0xEE, 0x3E, 0x7E, 0xBE, 0xFE,\
  433.    0x03, 0x43, 0x83, 0xC3, 0x13, 0x53, 0x93, 0xD3,\
  434.    0x23, 0x63, 0xA3, 0xE3, 0x33, 0x73, 0xB3, 0xF3,\
  435.    0x07, 0x47, 0x87, 0xC7, 0x17, 0x57, 0x97, 0xD7,\
  436.    0x27, 0x67, 0xA7, 0xE7, 0x37, 0x77, 0xB7, 0xF7,\
  437.    0x0B, 0x4B, 0x8B, 0xCB, 0x1B, 0x5B, 0x9B, 0xDB,\
  438.    0x2B, 0x6B, 0xAB, 0xEB, 0x3B, 0x7B, 0xBB, 0xFB,\
  439.    0x0F, 0x4F, 0x8F, 0xCF, 0x1F, 0x5F, 0x9F, 0xDF,\
  440.    0x2F, 0x6F, 0xAF, 0xEF, 0x3F, 0x7F, 0xBF, 0xFF
  441.  
  442. align 4
  443. fourbppswaptable db 0x00, 0x10, 0x20, 0x30, 0x40, 0x50, 0x60, 0x70,\
  444.    0x80, 0x90, 0xA0, 0xB0, 0xC0, 0xD0, 0xE0, 0xF0,\
  445.    0x01, 0x11, 0x21, 0x31, 0x41, 0x51, 0x61, 0x71,\
  446.    0x81, 0x91, 0xA1, 0xB1, 0xC1, 0xD1, 0xE1, 0xF1,\
  447.    0x02, 0x12, 0x22, 0x32, 0x42, 0x52, 0x62, 0x72,\
  448.    0x82, 0x92, 0xA2, 0xB2, 0xC2, 0xD2, 0xE2, 0xF2,\
  449.    0x03, 0x13, 0x23, 0x33, 0x43, 0x53, 0x63, 0x73,\
  450.    0x83, 0x93, 0xA3, 0xB3, 0xC3, 0xD3, 0xE3, 0xF3,\
  451.    0x04, 0x14, 0x24, 0x34, 0x44, 0x54, 0x64, 0x74,\
  452.    0x84, 0x94, 0xA4, 0xB4, 0xC4, 0xD4, 0xE4, 0xF4,\
  453.    0x05, 0x15, 0x25, 0x35, 0x45, 0x55, 0x65, 0x75,\
  454.    0x85, 0x95, 0xA5, 0xB5, 0xC5, 0xD5, 0xE5, 0xF5,\
  455.    0x06, 0x16, 0x26, 0x36, 0x46, 0x56, 0x66, 0x76,\
  456.    0x86, 0x96, 0xA6, 0xB6, 0xC6, 0xD6, 0xE6, 0xF6,\
  457.    0x07, 0x17, 0x27, 0x37, 0x47, 0x57, 0x67, 0x77,\
  458.    0x87, 0x97, 0xA7, 0xB7, 0xC7, 0xD7, 0xE7, 0xF7,\
  459.    0x08, 0x18, 0x28, 0x38, 0x48, 0x58, 0x68, 0x78,\
  460.    0x88, 0x98, 0xA8, 0xB8, 0xC8, 0xD8, 0xE8, 0xF8,\
  461.    0x09, 0x19, 0x29, 0x39, 0x49, 0x59, 0x69, 0x79,\
  462.    0x89, 0x99, 0xA9, 0xB9, 0xC9, 0xD9, 0xE9, 0xF9,\
  463.    0x0A, 0x1A, 0x2A, 0x3A, 0x4A, 0x5A, 0x6A, 0x7A,\
  464.    0x8A, 0x9A, 0xAA, 0xBA, 0xCA, 0xDA, 0xEA, 0xFA,\
  465.    0x0B, 0x1B, 0x2B, 0x3B, 0x4B, 0x5B, 0x6B, 0x7B,\
  466.    0x8B, 0x9B, 0xAB, 0xBB, 0xCB, 0xDB, 0xEB, 0xFB,\
  467.    0x0C, 0x1C, 0x2C, 0x3C, 0x4C, 0x5C, 0x6C, 0x7C,\
  468.    0x8C, 0x9C, 0xAC, 0xBC, 0xCC, 0xDC, 0xEC, 0xFC,\
  469.    0x0D, 0x1D, 0x2D, 0x3D, 0x4D, 0x5D, 0x6D, 0x7D,\
  470.    0x8D, 0x9D, 0xAD, 0xBD, 0xCD, 0xDD, 0xED, 0xFD,\
  471.    0x0E, 0x1E, 0x2E, 0x3E, 0x4E, 0x5E, 0x6E, 0x7E,\
  472.    0x8E, 0x9E, 0xAE, 0xBE, 0xCE, 0xDE, 0xEE, 0xFE,\
  473.    0x0F, 0x1F, 0x2F, 0x3F, 0x4F, 0x5F, 0x6F, 0x7F,\
  474.    0x8F, 0x9F, 0xAF, 0xBF, 0xCF, 0xDF, 0xEF, 0xFF
  475. end if ;PACKSWAP || WRITE_PACKSWAP
  476.  
  477. ; Swaps pixel packing order within bytes
  478. ;void (png_row_infop row_info, bytep row)
  479. align 4
  480. proc png_do_packswap uses eax edx edi esi, row_info:dword, row:dword
  481.         png_debug 1, 'in png_do_packswap'
  482.  
  483.         mov eax,[row_info]
  484.         cmp byte[eax+png_row_info.bit_depth],8
  485.         jge .end_f ;if (..<..)
  486.                 ;edi = rp
  487.                 ;esi = table
  488.  
  489.                 mov edx,[eax+png_row_info.rowbytes]
  490.                 mov edi,[row]
  491.                 add edx,edi
  492.  
  493.                 cmp byte[eax+png_row_info.bit_depth],1
  494.                 jne @f ;if (..==..)
  495.                         mov esi,onebppswaptable
  496.                         jmp .cycle0
  497.                 @@:
  498.                 cmp byte[eax+png_row_info.bit_depth],2
  499.                 jne @f ;else if (..==..)
  500.                         mov esi,twobppswaptable
  501.                         jmp .cycle0
  502.                 @@:
  503.                 cmp byte[eax+png_row_info.bit_depth],4
  504.                 jne .end_f ;else if (..==..)
  505.                         mov esi,fourbppswaptable
  506. align 4
  507.                 .cycle0: ;for (..=..;..<..;..)
  508.                         cmp edi,edx
  509.                         jge .end_f
  510.                         movzx eax,byte[edi]
  511.                         mov al,byte[esi+eax]
  512.                         stosb ;*rp = table[*rp]
  513.                         jmp .cycle0
  514. .end_f:
  515.         ret
  516. endp
  517.  
  518. ; Remove a channel - this used to be 'png_do_strip_filler' but it used a
  519. ; somewhat weird combination of flags to determine what to do.  All the calls
  520. ; to png_do_strip_filler are changed in 1.5.2 to call this instead with the
  521. ; correct arguments.
  522.  
  523. ; The routine isn't general - the channel must be the channel at the start or
  524. ; end (not in the middle) of each pixel.
  525.  
  526. ;void (png_row_infop row_info, bytep row, int at_start)
  527. align 4
  528. proc png_do_strip_channel, row_info:dword, row:dword, at_start:dword
  529. ;   bytep sp = row; /* source pointer */
  530. ;   bytep dp = row; /* destination pointer */
  531. ;   bytep ep = row + row_info->rowbytes; /* One beyond end of row */
  532.  
  533.         ; At the start sp will point to the first byte to copy and dp to where
  534.         ; it is copied to.  ep always points just beyond the end of the row, so
  535.         ; the loop simply copies (channels-1) channels until sp reaches ep.
  536.  
  537.         ; at_start:        0 -- convert AG, XG, ARGB, XRGB, AAGG, XXGG, etc.
  538.         ;            nonzero -- convert GA, GX, RGBA, RGBX, GGAA, RRGGBBXX, etc.
  539.  
  540.  
  541.         ; GA, GX, XG cases
  542. ;   if (row_info->channels == 2)
  543. ;   {
  544. ;      if (row_info->bit_depth == 8)
  545. ;      {
  546. ;         if (at_start != 0) /* Skip initial filler */
  547. ;            ++sp;
  548. ;         else          /* Skip initial channel and, for sp, the filler */
  549. ;            sp += 2, ++dp;
  550.  
  551.         ; For a 1 pixel wide image there is nothing to do
  552. ;         while (sp < ep)
  553. ;            *dp++ = *sp, sp += 2;
  554.  
  555. ;         row_info->pixel_depth = 8;
  556. ;      }
  557.  
  558. ;      else if (row_info->bit_depth == 16)
  559. ;      {
  560. ;         if (at_start != 0) /* Skip initial filler */
  561. ;            sp += 2;
  562. ;         else          /* Skip initial channel and, for sp, the filler */
  563. ;            sp += 4, dp += 2;
  564.  
  565. ;         while (sp < ep)
  566. ;            *dp++ = *sp++, *dp++ = *sp, sp += 3;
  567.  
  568. ;         row_info->pixel_depth = 16;
  569. ;      }
  570.  
  571. ;      else
  572. ;         return; /* bad bit depth */
  573.  
  574. ;      row_info->channels = 1;
  575.  
  576.         ; Finally fix the color type if it records an alpha channel
  577. ;      if (row_info->color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
  578. ;         row_info->color_type = PNG_COLOR_TYPE_GRAY;
  579. ;   }
  580.  
  581.         ; RGBA, RGBX, XRGB cases
  582. ;   else if (row_info->channels == 4)
  583. ;   {
  584. ;      if (row_info->bit_depth == 8)
  585. ;      {
  586. ;         if (at_start != 0) /* Skip initial filler */
  587. ;            ++sp;
  588. ;         else          /* Skip initial channels and, for sp, the filler */
  589. ;            sp += 4, dp += 3;
  590.  
  591.         ; Note that the loop adds 3 to dp and 4 to sp each time.
  592. ;         while (sp < ep)
  593. ;            *dp++ = *sp++, *dp++ = *sp++, *dp++ = *sp, sp += 2;
  594.  
  595. ;         row_info->pixel_depth = 24;
  596. ;      }
  597.  
  598. ;      else if (row_info->bit_depth == 16)
  599. ;      {
  600. ;         if (at_start != 0) /* Skip initial filler */
  601. ;            sp += 2;
  602. ;         else          /* Skip initial channels and, for sp, the filler */
  603. ;            sp += 8, dp += 6;
  604.  
  605. ;         while (sp < ep)
  606. ;         {
  607. ;            /* Copy 6 bytes, skip 2 */
  608. ;            *dp++ = *sp++, *dp++ = *sp++;
  609. ;            *dp++ = *sp++, *dp++ = *sp++;
  610. ;            *dp++ = *sp++, *dp++ = *sp, sp += 3;
  611. ;         }
  612.  
  613. ;         row_info->pixel_depth = 48;
  614. ;      }
  615.  
  616. ;      else
  617. ;         return; /* bad bit depth */
  618.  
  619. ;      row_info->channels = 3;
  620.  
  621.         ; Finally fix the color type if it records an alpha channel
  622. ;      if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA)
  623. ;         row_info->color_type = PNG_COLOR_TYPE_RGB;
  624. ;   }
  625.  
  626. ;   else
  627. ;      return; /* The filler channel has gone already */
  628.  
  629.         ; Fix the rowbytes value.
  630. ;   row_info->rowbytes = dp-row;
  631.         ret
  632. endp
  633.  
  634. ; Swaps red and blue bytes within a pixel
  635. ;void (png_row_infop row_info, bytep row)
  636. align 4
  637. proc png_do_bgr, row_info:dword, row:dword
  638.         png_debug 1, 'in png_do_bgr'
  639.         ;ebx - rp
  640.         ;ecx - i
  641.         ;esi - row_width
  642. pushad
  643.         mov edi,[row_info]
  644.         movzx eax,byte[edi+png_row_info.color_type]
  645.         and eax,PNG_COLOR_MASK_COLOR
  646.         je .end_f
  647.         mov esi,[edi+png_row_info.width]
  648.         cmp byte[edi+png_row_info.bit_depth],8
  649.         jne .end0
  650.         cmp byte[edi+png_row_info.color_type],PNG_COLOR_TYPE_RGB
  651.         jne .end1
  652.         xor ecx,ecx
  653.         mov ebx,[row]
  654.         jmp @f
  655. align 4
  656.         .cycle0:
  657.                 inc ecx
  658.                 add ebx,3
  659.         @@:
  660.                 cmp ecx,esi
  661.                 jae .end_f
  662.                 mov dl,[ebx]
  663.                 mov al,[ebx+2]
  664.                 mov [ebx],al
  665.                 mov [ebx+2],dl
  666.                 jmp .cycle0
  667. .end1:
  668.         cmp byte[edi+png_row_info.color_type],PNG_COLOR_TYPE_RGB_ALPHA
  669.         jne .end_f
  670.         xor ecx,ecx
  671.         mov ebx,[row]
  672.         jmp @f
  673. align 4
  674.         .cycle1:
  675.                 inc ecx
  676.                 add ebx,4
  677.         @@:
  678.                 cmp ecx,esi
  679.                 jae .end_f
  680.                 mov dl,[ebx]
  681.                 mov al,[ebx+2]
  682.                 mov [ebx],al
  683.                 mov [ebx+2],dl
  684.                 jmp .cycle1
  685. .end0:
  686. if PNG_16BIT_SUPPORTED eq 1
  687.         cmp byte[edi+png_row_info.bit_depth],16
  688.         jne .end_f
  689.         cmp byte[edi+png_row_info.color_type],PNG_COLOR_TYPE_RGB
  690.         jne .end2
  691.         xor ecx,ecx
  692.         mov ebx,[row]
  693.         jmp @f
  694. align 4
  695.         .cycle2:
  696.                 inc ecx
  697.                 add ebx,6
  698.         @@:
  699.                 cmp ecx,esi
  700.                 jae .end_f
  701.                 mov dx,[ebx]
  702.                 mov ax,[ebx+4]
  703.                 mov [ebx],ax
  704.                 mov [ebx+4],dx
  705.                 jmp .cycle2
  706. .end2:
  707.         cmp byte[edi+png_row_info.color_type],PNG_COLOR_TYPE_RGB_ALPHA
  708.         jne .end_f
  709.         xor ecx,ecx
  710.         mov ebx,[row]
  711.         jmp @f
  712. align 4
  713.         .cycle3:
  714.                 inc ecx
  715.                 add ebx,8
  716.         @@:
  717.                 cmp ecx,esi
  718.                 jae .end_f
  719.                 mov dx,[ebx]
  720.                 mov ax,[ebx+4]
  721.                 mov [ebx],ax
  722.                 mov [ebx+4],dx
  723.                 jmp .cycle3
  724. end if
  725. .end_f:
  726. popad
  727.         ret
  728. endp
  729.  
  730. ; Added at libpng-1.5.10
  731. ;void (png_structrp png_ptr, png_row_infop row_info)
  732. align 4
  733. proc png_do_check_palette_indexes, png_ptr:dword, row_info:dword
  734. ;   if (png_ptr->num_palette < (1 << row_info->bit_depth) &&
  735. ;      png_ptr->num_palette > 0) /* num_palette can be 0 in MNG files */
  736. ;   {
  737.         ; Calculations moved outside switch in an attempt to stop different
  738.         ; compiler warnings.  'padding' is in *bits* within the last byte, it is
  739.         ; an 'int' because pixel_depth becomes an 'int' in the expression below,
  740.         ; and this calculation is used because it avoids warnings that other
  741.         ; forms produced on either GCC or MSVC.
  742.  
  743. ;      int padding = (-row_info->pixel_depth * row_info->width) & 7;
  744. ;      bytep rp = png_ptr->row_buf + row_info->rowbytes;
  745.  
  746. ;      switch (row_info->bit_depth)
  747. ;      {
  748. ;         case 1:
  749. ;         {
  750. ;            /* in this case, all bytes must be 0 so we don't need
  751.         ; to unpack the pixels except for the rightmost one.
  752.  
  753. ;            for (; rp > png_ptr->row_buf; rp--)
  754. ;            {
  755. ;              if ((*rp >> padding) != 0)
  756. ;                 png_ptr->num_palette_max = 1;
  757. ;              padding = 0;
  758. ;            }
  759.  
  760. ;            break;
  761. ;         }
  762.  
  763. ;         case 2:
  764. ;         {
  765. ;            for (; rp > png_ptr->row_buf; rp--)
  766. ;            {
  767. ;              int i = ((*rp >> padding) & 0x03);
  768.  
  769. ;              if (i > png_ptr->num_palette_max)
  770. ;                 png_ptr->num_palette_max = i;
  771.  
  772. ;              i = (((*rp >> padding) >> 2) & 0x03);
  773.  
  774. ;              if (i > png_ptr->num_palette_max)
  775. ;                 png_ptr->num_palette_max = i;
  776.  
  777. ;              i = (((*rp >> padding) >> 4) & 0x03);
  778.  
  779. ;              if (i > png_ptr->num_palette_max)
  780. ;                 png_ptr->num_palette_max = i;
  781.  
  782. ;              i = (((*rp >> padding) >> 6) & 0x03);
  783.  
  784. ;              if (i > png_ptr->num_palette_max)
  785. ;                 png_ptr->num_palette_max = i;
  786.  
  787. ;              padding = 0;
  788. ;            }
  789.  
  790. ;            break;
  791. ;         }
  792.  
  793. ;         case 4:
  794. ;         {
  795. ;            for (; rp > png_ptr->row_buf; rp--)
  796. ;            {
  797. ;              int i = ((*rp >> padding) & 0x0f);
  798.  
  799. ;              if (i > png_ptr->num_palette_max)
  800. ;                 png_ptr->num_palette_max = i;
  801.  
  802. ;              i = (((*rp >> padding) >> 4) & 0x0f);
  803.  
  804. ;              if (i > png_ptr->num_palette_max)
  805. ;                 png_ptr->num_palette_max = i;
  806.  
  807. ;              padding = 0;
  808. ;            }
  809.  
  810. ;            break;
  811. ;         }
  812.  
  813. ;         case 8:
  814. ;         {
  815. ;            for (; rp > png_ptr->row_buf; rp--)
  816. ;            {
  817. ;               if (*rp > png_ptr->num_palette_max)
  818. ;                  png_ptr->num_palette_max = (int) *rp;
  819. ;            }
  820.  
  821. ;            break;
  822. ;         }
  823.  
  824. ;         default:
  825. ;            break;
  826. ;      }
  827. ;   }
  828.         ret
  829. endp
  830.  
  831. ;void (png_structrp png_ptr, voidp user_transform_ptr, int user_transform_depth, int user_transform_channels)
  832. align 4
  833. proc png_set_user_transform_info uses eax edi, png_ptr:dword, user_transform_ptr:dword, user_transform_depth:dword, user_transform_channels:dword
  834.         png_debug 1, 'in png_set_user_transform_info'
  835.  
  836.         mov edi,[png_ptr]
  837.         test edi,edi
  838.         jz .end_f
  839.  
  840. if PNG_READ_USER_TRANSFORM_SUPPORTED eq 1
  841.         mov eax,[edi+png_struct.mode]
  842.         and eax,PNG_IS_READ_STRUCT
  843.         jz @f
  844.         mov eax,[edi+png_struct.flags]
  845.         and eax,PNG_FLAG_ROW_INIT
  846.         jz @f ;if (..!=0 && ..!=0)
  847.                 cStr ,'info change after png_start_read_image or png_read_update_info'
  848.                 stdcall png_app_error, edi, eax
  849.                 jmp .end_f
  850.         @@:
  851. end if
  852.  
  853.         mov eax,[user_transform_ptr]
  854.         mov [edi+png_struct.user_transform_ptr],eax
  855.         mov eax,[user_transform_depth]
  856.         mov [edi+png_struct.user_transform_depth],al
  857.         mov eax,[user_transform_channels]
  858.         mov [edi+png_struct.user_transform_channels],al
  859. .end_f:
  860.         ret
  861. endp
  862.  
  863. ; This function returns a pointer to the user_transform_ptr associated with
  864. ; the user transform functions.  The application should free any memory
  865. ; associated with this pointer before png_write_destroy and png_read_destroy
  866. ; are called.
  867.  
  868. ;voidp (png_structrp png_ptr)
  869. align 4
  870. proc png_get_user_transform_ptr, png_ptr:dword
  871.         mov eax,[png_ptr]
  872.         cmp eax,0
  873.         je @f
  874.                 mov eax,[eax+png_struct.user_transform_ptr]
  875.         @@:
  876.         ret
  877. endp
  878.  
  879. ;uint_32 (png_structrp png_ptr)
  880. align 4
  881. proc png_get_current_row_number, png_ptr:dword
  882.         ; See the comments in png.inc - this is the sub-image row when reading an
  883.         ; interlaced image.
  884.  
  885.         mov eax,[png_ptr]
  886.         cmp eax,0
  887.         je @f ;if (..!=0)
  888.                 mov eax,[eax+png_struct.row_number]
  889.                 jmp .end_f
  890.         @@:
  891.         mov eax,PNG_UINT_32_MAX ;help the app not to fail silently
  892. .end_f:
  893.         ret
  894. endp
  895.  
  896. ;byte (png_structrp png_ptr)
  897. align 4
  898. proc png_get_current_pass_number, png_ptr:dword
  899.         mov eax,[png_ptr]
  900.         cmp eax,0
  901.         je @f ;if (..!=0)
  902.                 mov eax,[eax+png_struct.pass]
  903.                 jmp .end_f
  904.         @@:
  905.         mov eax,8 ;invalid
  906. .end_f:
  907.         ret
  908. endp
  909. end if
  910.