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