home *** CD-ROM | disk | FTP | other *** search
/ PC World 2002 February / PCWorld_2002-02_cd.bin / Software / Vyzkuste / pdflib / pdflib-4.0.1.sit / pdflib-4.0.1 / png / pngtrans.c < prev   
Encoding:
C/C++ Source or Header  |  2001-07-04  |  18.9 KB  |  611 lines  |  [TEXT/CWIE]

  1.  
  2. /* pngtrans.c - transforms the data in a row (used by both readers and writers)
  3.  *
  4.  * libpng 1.0.8 - July 24, 2000
  5.  * For conditions of distribution and use, see copyright notice in png.h
  6.  * Copyright (c) 1998, 1999, 2000 Glenn Randers-Pehrson
  7.  * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger)
  8.  * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.)
  9.  */
  10.  
  11. /* $Id: pngtrans.c,v 1.2 2001/02/14 23:43:19 rjs Exp $ */
  12.  
  13. #define PNG_INTERNAL
  14. #include "png.h"
  15.  
  16. #if defined(PNG_READ_BGR_SUPPORTED) || defined(PNG_WRITE_BGR_SUPPORTED)
  17. /* turn on BGR-to-RGB mapping */
  18. void PNGAPI
  19. png_set_bgr(png_structp png_ptr)
  20. {
  21.    png_debug(1, "in png_set_bgr\n");
  22.    png_ptr->transformations |= PNG_BGR;
  23. }
  24. #endif
  25.  
  26. #if defined(PNG_READ_SWAP_SUPPORTED) || defined(PNG_WRITE_SWAP_SUPPORTED)
  27. /* turn on 16 bit byte swapping */
  28. void PNGAPI
  29. png_set_swap(png_structp png_ptr)
  30. {
  31.    png_debug(1, "in png_set_swap\n");
  32.    if (png_ptr->bit_depth == 16)
  33.       png_ptr->transformations |= PNG_SWAP_BYTES;
  34. }
  35. #endif
  36.  
  37. #if defined(PNG_READ_PACK_SUPPORTED) || defined(PNG_WRITE_PACK_SUPPORTED)
  38. /* turn on pixel packing */
  39. void PNGAPI
  40. png_set_packing(png_structp png_ptr)
  41. {
  42.    png_debug(1, "in png_set_packing\n");
  43.    if (png_ptr->bit_depth < 8)
  44.    {
  45.       png_ptr->transformations |= PNG_PACK;
  46.       png_ptr->usr_bit_depth = 8;
  47.    }
  48. }
  49. #endif
  50.  
  51. #if defined(PNG_READ_PACKSWAP_SUPPORTED)||defined(PNG_WRITE_PACKSWAP_SUPPORTED)
  52. /* turn on packed pixel swapping */
  53. void PNGAPI
  54. png_set_packswap(png_structp png_ptr)
  55. {
  56.    png_debug(1, "in png_set_packswap\n");
  57.    if (png_ptr->bit_depth < 8)
  58.       png_ptr->transformations |= PNG_PACKSWAP;
  59. }
  60. #endif
  61.  
  62. #if defined(PNG_READ_SHIFT_SUPPORTED) || defined(PNG_WRITE_SHIFT_SUPPORTED)
  63. void PNGAPI
  64. png_set_shift(png_structp png_ptr, png_color_8p true_bits)
  65. {
  66.    png_debug(1, "in png_set_shift\n");
  67.    png_ptr->transformations |= PNG_SHIFT;
  68.    png_ptr->shift = *true_bits;
  69. }
  70. #endif
  71.  
  72. #if defined(PNG_READ_INTERLACING_SUPPORTED) || \
  73.     defined(PNG_WRITE_INTERLACING_SUPPORTED)
  74. int PNGAPI
  75. png_set_interlace_handling(png_structp png_ptr)
  76. {
  77.    png_debug(1, "in png_set_interlace handling\n");
  78.    if (png_ptr->interlaced)
  79.    {
  80.       png_ptr->transformations |= PNG_INTERLACE;
  81.       return (7);
  82.    }
  83.  
  84.    return (1);
  85. }
  86. #endif
  87.  
  88. #if defined(PNG_READ_FILLER_SUPPORTED) || defined(PNG_WRITE_FILLER_SUPPORTED)
  89. /* Add a filler byte on read, or remove a filler or alpha byte on write.
  90.  * The filler type has changed in v0.95 to allow future 2-byte fillers
  91.  * for 48-bit input data, as well as to avoid problems with some compilers
  92.  * that don't like bytes as parameters.
  93.  */
  94. void PNGAPI
  95. png_set_filler(png_structp png_ptr, png_uint_32 filler, int filler_loc)
  96. {
  97.    png_debug(1, "in png_set_filler\n");
  98.    png_ptr->transformations |= PNG_FILLER;
  99.    png_ptr->filler = (png_byte)filler;
  100.    if (filler_loc == PNG_FILLER_AFTER)
  101.       png_ptr->flags |= PNG_FLAG_FILLER_AFTER;
  102.    else
  103.       png_ptr->flags &= ~PNG_FLAG_FILLER_AFTER;
  104.  
  105.    /* This should probably go in the "do_filler" routine.
  106.     * I attempted to do that in libpng-1.0.1a but that caused problems
  107.     * so I restored it in libpng-1.0.2a
  108.    */
  109.  
  110.    if (png_ptr->color_type == PNG_COLOR_TYPE_RGB)
  111.    {
  112.       png_ptr->usr_channels = 4;
  113.    }
  114.  
  115.    /* Also I added this in libpng-1.0.2a (what happens when we expand
  116.     * a less-than-8-bit grayscale to GA? */
  117.  
  118.    if (png_ptr->color_type == PNG_COLOR_TYPE_GRAY && png_ptr->bit_depth >= 8)
  119.    {
  120.       png_ptr->usr_channels = 2;
  121.    }
  122. }
  123. #endif
  124.  
  125. #if defined(PNG_READ_SWAP_ALPHA_SUPPORTED) || \
  126.     defined(PNG_WRITE_SWAP_ALPHA_SUPPORTED)
  127. void PNGAPI
  128. png_set_swap_alpha(png_structp png_ptr)
  129. {
  130.    png_debug(1, "in png_set_swap_alpha\n");
  131.    png_ptr->transformations |= PNG_SWAP_ALPHA;
  132. }
  133. #endif
  134.  
  135. #if defined(PNG_READ_INVERT_ALPHA_SUPPORTED) || \
  136.     defined(PNG_WRITE_INVERT_ALPHA_SUPPORTED)
  137. void PNGAPI
  138. png_set_invert_alpha(png_structp png_ptr)
  139. {
  140.    png_debug(1, "in png_set_invert_alpha\n");
  141.    png_ptr->transformations |= PNG_INVERT_ALPHA;
  142. }
  143. #endif
  144.  
  145. #if defined(PNG_READ_INVERT_SUPPORTED) || defined(PNG_WRITE_INVERT_SUPPORTED)
  146. void PNGAPI
  147. png_set_invert_mono(png_structp png_ptr)
  148. {
  149.    png_debug(1, "in png_set_invert_mono\n");
  150.    png_ptr->transformations |= PNG_INVERT_MONO;
  151. }
  152.  
  153. /* invert monochrome grayscale data */
  154. void /* PRIVATE */
  155. png_do_invert(png_row_infop row_info, png_bytep row)
  156. {
  157.    png_debug(1, "in png_do_invert\n");
  158.    if (row_info->bit_depth == 1 &&
  159. #if defined(PNG_USELESS_TESTS_SUPPORTED)
  160.        row != NULL && row_info != NULL &&
  161. #endif
  162.        row_info->color_type == PNG_COLOR_TYPE_GRAY)
  163.    {
  164.       png_bytep rp = row;
  165.       png_uint_32 i;
  166.       png_uint_32 istop = row_info->rowbytes;
  167.  
  168.       for (i = 0; i < istop; i++)
  169.       {
  170.          *rp = (png_byte)(~(*rp));
  171.          rp++;
  172.       }
  173.    }
  174. }
  175. #endif
  176.  
  177. #if defined(PNG_READ_SWAP_SUPPORTED) || defined(PNG_WRITE_SWAP_SUPPORTED)
  178. /* swaps byte order on 16 bit depth images */
  179. void /* PRIVATE */
  180. png_do_swap(png_row_infop row_info, png_bytep row)
  181. {
  182.    png_debug(1, "in png_do_swap\n");
  183.    if (
  184. #if defined(PNG_USELESS_TESTS_SUPPORTED)
  185.        row != NULL && row_info != NULL &&
  186. #endif
  187.        row_info->bit_depth == 16)
  188.    {
  189.       png_bytep rp = row;
  190.       png_uint_32 i;
  191.       png_uint_32 istop= row_info->width * row_info->channels;
  192.  
  193.       for (i = 0; i < istop; i++, rp += 2)
  194.       {
  195.          png_byte t = *rp;
  196.          *rp = *(rp + 1);
  197.          *(rp + 1) = t;
  198.       }
  199.    }
  200. }
  201. #endif
  202.  
  203. #if defined(PNG_READ_PACKSWAP_SUPPORTED)||defined(PNG_WRITE_PACKSWAP_SUPPORTED)
  204. static png_byte onebppswaptable[256] = {
  205.    0x00, 0x80, 0x40, 0xC0, 0x20, 0xA0, 0x60, 0xE0,
  206.    0x10, 0x90, 0x50, 0xD0, 0x30, 0xB0, 0x70, 0xF0,
  207.    0x08, 0x88, 0x48, 0xC8, 0x28, 0xA8, 0x68, 0xE8,
  208.    0x18, 0x98, 0x58, 0xD8, 0x38, 0xB8, 0x78, 0xF8,
  209.    0x04, 0x84, 0x44, 0xC4, 0x24, 0xA4, 0x64, 0xE4,
  210.    0x14, 0x94, 0x54, 0xD4, 0x34, 0xB4, 0x74, 0xF4,
  211.    0x0C, 0x8C, 0x4C, 0xCC, 0x2C, 0xAC, 0x6C, 0xEC,
  212.    0x1C, 0x9C, 0x5C, 0xDC, 0x3C, 0xBC, 0x7C, 0xFC,
  213.    0x02, 0x82, 0x42, 0xC2, 0x22, 0xA2, 0x62, 0xE2,
  214.    0x12, 0x92, 0x52, 0xD2, 0x32, 0xB2, 0x72, 0xF2,
  215.    0x0A, 0x8A, 0x4A, 0xCA, 0x2A, 0xAA, 0x6A, 0xEA,
  216.    0x1A, 0x9A, 0x5A, 0xDA, 0x3A, 0xBA, 0x7A, 0xFA,
  217.    0x06, 0x86, 0x46, 0xC6, 0x26, 0xA6, 0x66, 0xE6,
  218.    0x16, 0x96, 0x56, 0xD6, 0x36, 0xB6, 0x76, 0xF6,
  219.    0x0E, 0x8E, 0x4E, 0xCE, 0x2E, 0xAE, 0x6E, 0xEE,
  220.    0x1E, 0x9E, 0x5E, 0xDE, 0x3E, 0xBE, 0x7E, 0xFE,
  221.    0x01, 0x81, 0x41, 0xC1, 0x21, 0xA1, 0x61, 0xE1,
  222.    0x11, 0x91, 0x51, 0xD1, 0x31, 0xB1, 0x71, 0xF1,
  223.    0x09, 0x89, 0x49, 0xC9, 0x29, 0xA9, 0x69, 0xE9,
  224.    0x19, 0x99, 0x59, 0xD9, 0x39, 0xB9, 0x79, 0xF9,
  225.    0x05, 0x85, 0x45, 0xC5, 0x25, 0xA5, 0x65, 0xE5,
  226.    0x15, 0x95, 0x55, 0xD5, 0x35, 0xB5, 0x75, 0xF5,
  227.    0x0D, 0x8D, 0x4D, 0xCD, 0x2D, 0xAD, 0x6D, 0xED,
  228.    0x1D, 0x9D, 0x5D, 0xDD, 0x3D, 0xBD, 0x7D, 0xFD,
  229.    0x03, 0x83, 0x43, 0xC3, 0x23, 0xA3, 0x63, 0xE3,
  230.    0x13, 0x93, 0x53, 0xD3, 0x33, 0xB3, 0x73, 0xF3,
  231.    0x0B, 0x8B, 0x4B, 0xCB, 0x2B, 0xAB, 0x6B, 0xEB,
  232.    0x1B, 0x9B, 0x5B, 0xDB, 0x3B, 0xBB, 0x7B, 0xFB,
  233.    0x07, 0x87, 0x47, 0xC7, 0x27, 0xA7, 0x67, 0xE7,
  234.    0x17, 0x97, 0x57, 0xD7, 0x37, 0xB7, 0x77, 0xF7,
  235.    0x0F, 0x8F, 0x4F, 0xCF, 0x2F, 0xAF, 0x6F, 0xEF,
  236.    0x1F, 0x9F, 0x5F, 0xDF, 0x3F, 0xBF, 0x7F, 0xFF
  237. };
  238.  
  239. static png_byte twobppswaptable[256] = {
  240.    0x00, 0x40, 0x80, 0xC0, 0x10, 0x50, 0x90, 0xD0,
  241.    0x20, 0x60, 0xA0, 0xE0, 0x30, 0x70, 0xB0, 0xF0,
  242.    0x04, 0x44, 0x84, 0xC4, 0x14, 0x54, 0x94, 0xD4,
  243.    0x24, 0x64, 0xA4, 0xE4, 0x34, 0x74, 0xB4, 0xF4,
  244.    0x08, 0x48, 0x88, 0xC8, 0x18, 0x58, 0x98, 0xD8,
  245.    0x28, 0x68, 0xA8, 0xE8, 0x38, 0x78, 0xB8, 0xF8,
  246.    0x0C, 0x4C, 0x8C, 0xCC, 0x1C, 0x5C, 0x9C, 0xDC,
  247.    0x2C, 0x6C, 0xAC, 0xEC, 0x3C, 0x7C, 0xBC, 0xFC,
  248.    0x01, 0x41, 0x81, 0xC1, 0x11, 0x51, 0x91, 0xD1,
  249.    0x21, 0x61, 0xA1, 0xE1, 0x31, 0x71, 0xB1, 0xF1,
  250.    0x05, 0x45, 0x85, 0xC5, 0x15, 0x55, 0x95, 0xD5,
  251.    0x25, 0x65, 0xA5, 0xE5, 0x35, 0x75, 0xB5, 0xF5,
  252.    0x09, 0x49, 0x89, 0xC9, 0x19, 0x59, 0x99, 0xD9,
  253.    0x29, 0x69, 0xA9, 0xE9, 0x39, 0x79, 0xB9, 0xF9,
  254.    0x0D, 0x4D, 0x8D, 0xCD, 0x1D, 0x5D, 0x9D, 0xDD,
  255.    0x2D, 0x6D, 0xAD, 0xED, 0x3D, 0x7D, 0xBD, 0xFD,
  256.    0x02, 0x42, 0x82, 0xC2, 0x12, 0x52, 0x92, 0xD2,
  257.    0x22, 0x62, 0xA2, 0xE2, 0x32, 0x72, 0xB2, 0xF2,
  258.    0x06, 0x46, 0x86, 0xC6, 0x16, 0x56, 0x96, 0xD6,
  259.    0x26, 0x66, 0xA6, 0xE6, 0x36, 0x76, 0xB6, 0xF6,
  260.    0x0A, 0x4A, 0x8A, 0xCA, 0x1A, 0x5A, 0x9A, 0xDA,
  261.    0x2A, 0x6A, 0xAA, 0xEA, 0x3A, 0x7A, 0xBA, 0xFA,
  262.    0x0E, 0x4E, 0x8E, 0xCE, 0x1E, 0x5E, 0x9E, 0xDE,
  263.    0x2E, 0x6E, 0xAE, 0xEE, 0x3E, 0x7E, 0xBE, 0xFE,
  264.    0x03, 0x43, 0x83, 0xC3, 0x13, 0x53, 0x93, 0xD3,
  265.    0x23, 0x63, 0xA3, 0xE3, 0x33, 0x73, 0xB3, 0xF3,
  266.    0x07, 0x47, 0x87, 0xC7, 0x17, 0x57, 0x97, 0xD7,
  267.    0x27, 0x67, 0xA7, 0xE7, 0x37, 0x77, 0xB7, 0xF7,
  268.    0x0B, 0x4B, 0x8B, 0xCB, 0x1B, 0x5B, 0x9B, 0xDB,
  269.    0x2B, 0x6B, 0xAB, 0xEB, 0x3B, 0x7B, 0xBB, 0xFB,
  270.    0x0F, 0x4F, 0x8F, 0xCF, 0x1F, 0x5F, 0x9F, 0xDF,
  271.    0x2F, 0x6F, 0xAF, 0xEF, 0x3F, 0x7F, 0xBF, 0xFF
  272. };
  273.  
  274. static png_byte fourbppswaptable[256] = {
  275.    0x00, 0x10, 0x20, 0x30, 0x40, 0x50, 0x60, 0x70,
  276.    0x80, 0x90, 0xA0, 0xB0, 0xC0, 0xD0, 0xE0, 0xF0,
  277.    0x01, 0x11, 0x21, 0x31, 0x41, 0x51, 0x61, 0x71,
  278.    0x81, 0x91, 0xA1, 0xB1, 0xC1, 0xD1, 0xE1, 0xF1,
  279.    0x02, 0x12, 0x22, 0x32, 0x42, 0x52, 0x62, 0x72,
  280.    0x82, 0x92, 0xA2, 0xB2, 0xC2, 0xD2, 0xE2, 0xF2,
  281.    0x03, 0x13, 0x23, 0x33, 0x43, 0x53, 0x63, 0x73,
  282.    0x83, 0x93, 0xA3, 0xB3, 0xC3, 0xD3, 0xE3, 0xF3,
  283.    0x04, 0x14, 0x24, 0x34, 0x44, 0x54, 0x64, 0x74,
  284.    0x84, 0x94, 0xA4, 0xB4, 0xC4, 0xD4, 0xE4, 0xF4,
  285.    0x05, 0x15, 0x25, 0x35, 0x45, 0x55, 0x65, 0x75,
  286.    0x85, 0x95, 0xA5, 0xB5, 0xC5, 0xD5, 0xE5, 0xF5,
  287.    0x06, 0x16, 0x26, 0x36, 0x46, 0x56, 0x66, 0x76,
  288.    0x86, 0x96, 0xA6, 0xB6, 0xC6, 0xD6, 0xE6, 0xF6,
  289.    0x07, 0x17, 0x27, 0x37, 0x47, 0x57, 0x67, 0x77,
  290.    0x87, 0x97, 0xA7, 0xB7, 0xC7, 0xD7, 0xE7, 0xF7,
  291.    0x08, 0x18, 0x28, 0x38, 0x48, 0x58, 0x68, 0x78,
  292.    0x88, 0x98, 0xA8, 0xB8, 0xC8, 0xD8, 0xE8, 0xF8,
  293.    0x09, 0x19, 0x29, 0x39, 0x49, 0x59, 0x69, 0x79,
  294.    0x89, 0x99, 0xA9, 0xB9, 0xC9, 0xD9, 0xE9, 0xF9,
  295.    0x0A, 0x1A, 0x2A, 0x3A, 0x4A, 0x5A, 0x6A, 0x7A,
  296.    0x8A, 0x9A, 0xAA, 0xBA, 0xCA, 0xDA, 0xEA, 0xFA,
  297.    0x0B, 0x1B, 0x2B, 0x3B, 0x4B, 0x5B, 0x6B, 0x7B,
  298.    0x8B, 0x9B, 0xAB, 0xBB, 0xCB, 0xDB, 0xEB, 0xFB,
  299.    0x0C, 0x1C, 0x2C, 0x3C, 0x4C, 0x5C, 0x6C, 0x7C,
  300.    0x8C, 0x9C, 0xAC, 0xBC, 0xCC, 0xDC, 0xEC, 0xFC,
  301.    0x0D, 0x1D, 0x2D, 0x3D, 0x4D, 0x5D, 0x6D, 0x7D,
  302.    0x8D, 0x9D, 0xAD, 0xBD, 0xCD, 0xDD, 0xED, 0xFD,
  303.    0x0E, 0x1E, 0x2E, 0x3E, 0x4E, 0x5E, 0x6E, 0x7E,
  304.    0x8E, 0x9E, 0xAE, 0xBE, 0xCE, 0xDE, 0xEE, 0xFE,
  305.    0x0F, 0x1F, 0x2F, 0x3F, 0x4F, 0x5F, 0x6F, 0x7F,
  306.    0x8F, 0x9F, 0xAF, 0xBF, 0xCF, 0xDF, 0xEF, 0xFF
  307. };
  308.  
  309. /* swaps pixel packing order within bytes */
  310. void /* PRIVATE */
  311. png_do_packswap(png_row_infop row_info, png_bytep row)
  312. {
  313.    png_debug(1, "in png_do_packswap\n");
  314.    if (
  315. #if defined(PNG_USELESS_TESTS_SUPPORTED)
  316.        row != NULL && row_info != NULL &&
  317. #endif
  318.        row_info->bit_depth < 8)
  319.    {
  320.       png_bytep rp, end, table;
  321.  
  322.       end = row + row_info->rowbytes;
  323.  
  324.       if (row_info->bit_depth == 1)
  325.          table = onebppswaptable;
  326.       else if (row_info->bit_depth == 2)
  327.          table = twobppswaptable;
  328.       else if (row_info->bit_depth == 4)
  329.          table = fourbppswaptable;
  330.       else
  331.          return;
  332.  
  333.       for (rp = row; rp < end; rp++)
  334.          *rp = table[*rp];
  335.    }
  336. }
  337. #endif /* PNG_READ_PACKSWAP_SUPPORTED or PNG_WRITE_PACKSWAP_SUPPORTED */
  338.  
  339. #if defined(PNG_WRITE_FILLER_SUPPORTED) || \
  340.     defined(PNG_READ_STRIP_ALPHA_SUPPORTED)
  341. /* remove filler or alpha byte(s) */
  342. void /* PRIVATE */
  343. png_do_strip_filler(png_row_infop row_info, png_bytep row, png_uint_32 flags)
  344. {
  345.    png_debug(1, "in png_do_strip_filler\n");
  346. #if defined(PNG_USELESS_TESTS_SUPPORTED)
  347.    if (row != NULL && row_info != NULL)
  348. #endif
  349.    {
  350. /*
  351.       if (row_info->color_type == PNG_COLOR_TYPE_RGB ||
  352.           row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA)
  353. */
  354.       png_bytep sp=row;
  355.       png_bytep dp=row;
  356.       png_uint_32 row_width=row_info->width;
  357.       png_uint_32 i;
  358.  
  359.       if (row_info->channels == 4)
  360.       {
  361.          if (row_info->bit_depth == 8)
  362.          {
  363.             /* This converts from RGBX or RGBA to RGB */
  364.             if (flags & PNG_FLAG_FILLER_AFTER)
  365.             {
  366.                dp+=3; sp+=4;
  367.                for (i = 1; i < row_width; i++)
  368.                {
  369.                   *dp++ = *sp++;
  370.                   *dp++ = *sp++;
  371.                   *dp++ = *sp++;
  372.                   sp++;
  373.                }
  374.             }
  375.             /* This converts from XRGB or ARGB to RGB */
  376.             else
  377.             {
  378.                for (i = 0; i < row_width; i++)
  379.                {
  380.                   sp++;
  381.                   *dp++ = *sp++;
  382.                   *dp++ = *sp++;
  383.                   *dp++ = *sp++;
  384.                }
  385.             }
  386.             row_info->pixel_depth = 24;
  387.             row_info->rowbytes = row_width * 3;
  388.          }
  389.          else /* if (row_info->bit_depth == 16) */
  390.          {
  391.             if (flags & PNG_FLAG_FILLER_AFTER)
  392.             {
  393.                /* This converts from RRGGBBXX or RRGGBBAA to RRGGBB */
  394.                sp += 8; dp += 6;
  395.                for (i = 1; i < row_width; i++)
  396.                {
  397.                   /* This could be (although memcpy is probably slower):
  398.                   png_memcpy(dp, sp, 6);
  399.                   sp += 8;
  400.                   dp += 6;
  401.                   */
  402.                   *dp++ = *sp++;
  403.                   *dp++ = *sp++;
  404.                   *dp++ = *sp++;
  405.                   *dp++ = *sp++;
  406.                   *dp++ = *sp++;
  407.                   *dp++ = *sp++;
  408.                   sp += 2;
  409.                }
  410.             }
  411.             else
  412.             {
  413.                /* This converts from XXRRGGBB or AARRGGBB to RRGGBB */
  414.                for (i = 0; i < row_width; i++)
  415.                {
  416.                   /* This could be (although memcpy is probably slower):
  417.                   png_memcpy(dp, sp, 6);
  418.                   sp += 8;
  419.                   dp += 6;
  420.                   */
  421.                   sp+=2;
  422.                   *dp++ = *sp++;
  423.                   *dp++ = *sp++;
  424.                   *dp++ = *sp++;
  425.                   *dp++ = *sp++;
  426.                   *dp++ = *sp++;
  427.                   *dp++ = *sp++;
  428.                }
  429.             }
  430.             row_info->pixel_depth = 48;
  431.             row_info->rowbytes = row_width * 6;
  432.          }
  433.          row_info->channels = 3;
  434.          row_info->color_type &= ~PNG_COLOR_MASK_ALPHA;
  435.       }
  436. /*
  437.       else if (row_info->color_type == PNG_COLOR_TYPE_GRAY ||
  438.                row_info->color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
  439. */
  440.       else if (row_info->channels == 2)
  441.       {
  442.          if (row_info->bit_depth == 8)
  443.          {
  444.             /* This converts from GX or GA to G */
  445.             if (flags & PNG_FLAG_FILLER_AFTER)
  446.             {
  447.                for (i = 0; i < row_width; i++)
  448.                {
  449.                   *dp++ = *sp++;
  450.                   sp++;
  451.                }
  452.             }
  453.             /* This converts from XG or AG to G */
  454.             else
  455.             {
  456.                for (i = 0; i < row_width; i++)
  457.                {
  458.                   sp++;
  459.                   *dp++ = *sp++;
  460.                }
  461.             }
  462.             row_info->pixel_depth = 8;
  463.             row_info->rowbytes = row_width;
  464.          }
  465.          else /* if (row_info->bit_depth == 16) */
  466.          {
  467.             if (flags & PNG_FLAG_FILLER_AFTER)
  468.             {
  469.                /* This converts from GGXX or GGAA to GG */
  470.                sp += 4; dp += 2;
  471.                for (i = 1; i < row_width; i++)
  472.                {
  473.                   *dp++ = *sp++;
  474.                   *dp++ = *sp++;
  475.                   sp += 2;
  476.                }
  477.             }
  478.             else
  479.             {
  480.                /* This converts from XXGG or AAGG to GG */
  481.                for (i = 0; i < row_width; i++)
  482.                {
  483.                   sp += 2;
  484.                   *dp++ = *sp++;
  485.                   *dp++ = *sp++;
  486.                }
  487.             }
  488.             row_info->pixel_depth = 16;
  489.             row_info->rowbytes = row_width * 2;
  490.          }
  491.          row_info->channels = 1;
  492.          row_info->color_type &= ~PNG_COLOR_MASK_ALPHA;
  493.       }
  494.    }
  495. }
  496. #endif
  497.  
  498. #if defined(PNG_READ_BGR_SUPPORTED) || defined(PNG_WRITE_BGR_SUPPORTED)
  499. /* swaps red and blue bytes within a pixel */
  500. void /* PRIVATE */
  501. png_do_bgr(png_row_infop row_info, png_bytep row)
  502. {
  503.    png_debug(1, "in png_do_bgr\n");
  504.    if (
  505. #if defined(PNG_USELESS_TESTS_SUPPORTED)
  506.        row != NULL && row_info != NULL &&
  507. #endif
  508.        (row_info->color_type & PNG_COLOR_MASK_COLOR))
  509.    {
  510.       png_uint_32 row_width = row_info->width;
  511.       if (row_info->bit_depth == 8)
  512.       {
  513.          if (row_info->color_type == PNG_COLOR_TYPE_RGB)
  514.          {
  515.             png_bytep rp;
  516.             png_uint_32 i;
  517.  
  518.             for (i = 0, rp = row; i < row_width; i++, rp += 3)
  519.             {
  520.                png_byte save = *rp;
  521.                *rp = *(rp + 2);
  522.                *(rp + 2) = save;
  523.             }
  524.          }
  525.          else if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA)
  526.          {
  527.             png_bytep rp;
  528.             png_uint_32 i;
  529.  
  530.             for (i = 0, rp = row; i < row_width; i++, rp += 4)
  531.             {
  532.                png_byte save = *rp;
  533.                *rp = *(rp + 2);
  534.                *(rp + 2) = save;
  535.             }
  536.          }
  537.       }
  538.       else if (row_info->bit_depth == 16)
  539.       {
  540.          if (row_info->color_type == PNG_COLOR_TYPE_RGB)
  541.          {
  542.             png_bytep rp;
  543.             png_uint_32 i;
  544.  
  545.             for (i = 0, rp = row; i < row_width; i++, rp += 6)
  546.             {
  547.                png_byte save = *rp;
  548.                *rp = *(rp + 4);
  549.                *(rp + 4) = save;
  550.                save = *(rp + 1);
  551.                *(rp + 1) = *(rp + 5);
  552.                *(rp + 5) = save;
  553.             }
  554.          }
  555.          else if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA)
  556.          {
  557.             png_bytep rp;
  558.             png_uint_32 i;
  559.  
  560.             for (i = 0, rp = row; i < row_width; i++, rp += 8)
  561.             {
  562.                png_byte save = *rp;
  563.                *rp = *(rp + 4);
  564.                *(rp + 4) = save;
  565.                save = *(rp + 1);
  566.                *(rp + 1) = *(rp + 5);
  567.                *(rp + 5) = save;
  568.             }
  569.          }
  570.       }
  571.    }
  572. }
  573. #endif /* PNG_READ_BGR_SUPPORTED or PNG_WRITE_BGR_SUPPORTED */
  574.  
  575. #if defined(PNG_READ_USER_TRANSFORM_SUPPORTED) || \
  576.     defined(PNG_WRITE_USER_TRANSFORM_SUPPORTED) || \
  577.     defined(PNG_LEGACY_SUPPORTED)
  578. void PNGAPI
  579. png_set_user_transform_info(png_structp png_ptr, png_voidp
  580.    user_transform_ptr, int user_transform_depth, int user_transform_channels)
  581. {
  582.    png_debug(1, "in png_set_user_transform_info\n");
  583. #if defined(PNG_USER_TRANSFORM_PTR_SUPPORTED)
  584.    png_ptr->user_transform_ptr = user_transform_ptr;
  585.    png_ptr->user_transform_depth = (png_byte)user_transform_depth;
  586.    png_ptr->user_transform_channels = (png_byte)user_transform_channels;
  587. #else
  588.    if(user_transform_ptr || user_transform_depth || user_transform_channels)
  589.       png_warning(png_ptr,
  590.         "This version of libpng does not support user transform info");
  591. #endif
  592. }
  593. #endif
  594.  
  595. /* This function returns a pointer to the user_transform_ptr associated with
  596.  * the user transform functions.  The application should free any memory
  597.  * associated with this pointer before png_write_destroy and png_read_destroy
  598.  * are called.
  599.  */
  600. png_voidp PNGAPI
  601. png_get_user_transform_ptr(png_structp png_ptr)
  602. {
  603. #if defined(PNG_USER_TRANSFORM_PTR_SUPPORTED)
  604.    return ((png_voidp)png_ptr->user_transform_ptr);
  605. #else
  606.    if(png_ptr)
  607.      return (NULL);
  608.    return (NULL);
  609. #endif
  610. }
  611.