pngrtran.c
上传用户:szled88
上传日期:2015-04-09
资源大小:43957k
文件大小:145k
源码类别:

对话框与窗口

开发平台:

Visual C++

  1.             png_bytep dp = sp  + (png_size_t)row_width * 2;
  2.             for (i = 0; i < row_width; i++)
  3.             {
  4.                *(dp--) = *(sp--);
  5.                *(dp--) = *sp;
  6.                *(dp--) = *sp;
  7.                *(dp--) = *(sp--);
  8.             }
  9.          }
  10.          else
  11.          {
  12.             png_bytep sp = row + (png_size_t)row_width * 4 - 1;
  13.             png_bytep dp = sp  + (png_size_t)row_width * 4;
  14.             for (i = 0; i < row_width; i++)
  15.             {
  16.                *(dp--) = *(sp--);
  17.                *(dp--) = *(sp--);
  18.                *(dp--) = *sp;
  19.                *(dp--) = *(sp - 1);
  20.                *(dp--) = *sp;
  21.                *(dp--) = *(sp - 1);
  22.                *(dp--) = *(sp--);
  23.                *(dp--) = *(sp--);
  24.             }
  25.          }
  26.       }
  27.       row_info->channels += (png_byte)2;
  28.       row_info->color_type |= PNG_COLOR_MASK_COLOR;
  29.       row_info->pixel_depth = (png_byte)(row_info->channels *
  30.          row_info->bit_depth);
  31.       row_info->rowbytes = PNG_ROWBYTES(row_info->pixel_depth,row_width);
  32.    }
  33. }
  34. #endif
  35. #if defined(PNG_READ_RGB_TO_GRAY_SUPPORTED)
  36. /* reduce RGB files to grayscale, with or without alpha
  37.  * using the equation given in Poynton's ColorFAQ at
  38.  * <http://www.inforamp.net/~poynton/>
  39.  * Copyright (c) 1998-01-04 Charles Poynton poynton at inforamp.net
  40.  *
  41.  *     Y = 0.212671 * R + 0.715160 * G + 0.072169 * B
  42.  *
  43.  *  We approximate this with
  44.  *
  45.  *     Y = 0.21268 * R    + 0.7151 * G    + 0.07217 * B
  46.  *
  47.  *  which can be expressed with integers as
  48.  *
  49.  *     Y = (6969 * R + 23434 * G + 2365 * B)/32768
  50.  *
  51.  *  The calculation is to be done in a linear colorspace.
  52.  *
  53.  *  Other integer coefficents can be used via png_set_rgb_to_gray().
  54.  */
  55. int /* PRIVATE */
  56. png_do_rgb_to_gray(png_structp png_ptr, png_row_infop row_info, png_bytep row)
  57. {
  58.    png_uint_32 i;
  59.    png_uint_32 row_width = row_info->width;
  60.    int rgb_error = 0;
  61.    png_debug(1, "in png_do_rgb_to_grayn");
  62.    if (
  63. #if defined(PNG_USELESS_TESTS_SUPPORTED)
  64.        row != NULL && row_info != NULL &&
  65. #endif
  66.       (row_info->color_type & PNG_COLOR_MASK_COLOR))
  67.    {
  68.       png_uint_32 rc = png_ptr->rgb_to_gray_red_coeff;
  69.       png_uint_32 gc = png_ptr->rgb_to_gray_green_coeff;
  70.       png_uint_32 bc = png_ptr->rgb_to_gray_blue_coeff;
  71.       if (row_info->color_type == PNG_COLOR_TYPE_RGB)
  72.       {
  73.          if (row_info->bit_depth == 8)
  74.          {
  75. #if defined(PNG_READ_GAMMA_SUPPORTED) || defined(PNG_READ_BACKGROUND_SUPPORTED)
  76.             if (png_ptr->gamma_from_1 != NULL && png_ptr->gamma_to_1 != NULL)
  77.             {
  78.                png_bytep sp = row;
  79.                png_bytep dp = row;
  80.                for (i = 0; i < row_width; i++)
  81.                {
  82.                   png_byte red   = png_ptr->gamma_to_1[*(sp++)];
  83.                   png_byte green = png_ptr->gamma_to_1[*(sp++)];
  84.                   png_byte blue  = png_ptr->gamma_to_1[*(sp++)];
  85.                   if(red != green || red != blue)
  86.                   {
  87.                      rgb_error |= 1;
  88.                      *(dp++) = png_ptr->gamma_from_1[
  89.                        (rc*red+gc*green+bc*blue)>>15];
  90.                   }
  91.                   else
  92.                      *(dp++) = *(sp-1);
  93.                }
  94.             }
  95.             else
  96. #endif
  97.             {
  98.                png_bytep sp = row;
  99.                png_bytep dp = row;
  100.                for (i = 0; i < row_width; i++)
  101.                {
  102.                   png_byte red   = *(sp++);
  103.                   png_byte green = *(sp++);
  104.                   png_byte blue  = *(sp++);
  105.                   if(red != green || red != blue)
  106.                   {
  107.                      rgb_error |= 1;
  108.                      *(dp++) = (png_byte)((rc*red+gc*green+bc*blue)>>15);
  109.                   }
  110.                   else
  111.                      *(dp++) = *(sp-1);
  112.                }
  113.             }
  114.          }
  115.          else /* RGB bit_depth == 16 */
  116.          {
  117. #if defined(PNG_READ_GAMMA_SUPPORTED) || defined(PNG_READ_BACKGROUND_SUPPORTED)
  118.             if (png_ptr->gamma_16_to_1 != NULL &&
  119.                 png_ptr->gamma_16_from_1 != NULL)
  120.             {
  121.                png_bytep sp = row;
  122.                png_bytep dp = row;
  123.                for (i = 0; i < row_width; i++)
  124.                {
  125.                   png_uint_16 red, green, blue, w;
  126.                   red   = (png_uint_16)(((*(sp))<<8) | *(sp+1)); sp+=2;
  127.                   green = (png_uint_16)(((*(sp))<<8) | *(sp+1)); sp+=2;
  128.                   blue  = (png_uint_16)(((*(sp))<<8) | *(sp+1)); sp+=2;
  129.                   if(red == green && red == blue)
  130.                      w = red;
  131.                   else
  132.                   {
  133.                      png_uint_16 red_1   = png_ptr->gamma_16_to_1[(red&0xff) >>
  134.                                   png_ptr->gamma_shift][red>>8];
  135.                      png_uint_16 green_1 = png_ptr->gamma_16_to_1[(green&0xff) >>
  136.                                   png_ptr->gamma_shift][green>>8];
  137.                      png_uint_16 blue_1  = png_ptr->gamma_16_to_1[(blue&0xff) >>
  138.                                   png_ptr->gamma_shift][blue>>8];
  139.                      png_uint_16 gray16  = (png_uint_16)((rc*red_1 + gc*green_1
  140.                                   + bc*blue_1)>>15);
  141.                      w = png_ptr->gamma_16_from_1[(gray16&0xff) >>
  142.                          png_ptr->gamma_shift][gray16 >> 8];
  143.                      rgb_error |= 1;
  144.                   }
  145.                   *(dp++) = (png_byte)((w>>8) & 0xff);
  146.                   *(dp++) = (png_byte)(w & 0xff);
  147.                }
  148.             }
  149.             else
  150. #endif
  151.             {
  152.                png_bytep sp = row;
  153.                png_bytep dp = row;
  154.                for (i = 0; i < row_width; i++)
  155.                {
  156.                   png_uint_16 red, green, blue, gray16;
  157.                   red   = (png_uint_16)(((*(sp))<<8) | *(sp+1)); sp+=2;
  158.                   green = (png_uint_16)(((*(sp))<<8) | *(sp+1)); sp+=2;
  159.                   blue  = (png_uint_16)(((*(sp))<<8) | *(sp+1)); sp+=2;
  160.                   if(red != green || red != blue)
  161.                      rgb_error |= 1;
  162.                   gray16  = (png_uint_16)((rc*red + gc*green + bc*blue)>>15);
  163.                   *(dp++) = (png_byte)((gray16>>8) & 0xff);
  164.                   *(dp++) = (png_byte)(gray16 & 0xff);
  165.                }
  166.             }
  167.          }
  168.       }
  169.       if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA)
  170.       {
  171.          if (row_info->bit_depth == 8)
  172.          {
  173. #if defined(PNG_READ_GAMMA_SUPPORTED) || defined(PNG_READ_BACKGROUND_SUPPORTED)
  174.             if (png_ptr->gamma_from_1 != NULL && png_ptr->gamma_to_1 != NULL)
  175.             {
  176.                png_bytep sp = row;
  177.                png_bytep dp = row;
  178.                for (i = 0; i < row_width; i++)
  179.                {
  180.                   png_byte red   = png_ptr->gamma_to_1[*(sp++)];
  181.                   png_byte green = png_ptr->gamma_to_1[*(sp++)];
  182.                   png_byte blue  = png_ptr->gamma_to_1[*(sp++)];
  183.                   if(red != green || red != blue)
  184.                      rgb_error |= 1;
  185.                   *(dp++) =  png_ptr->gamma_from_1
  186.                              [(rc*red + gc*green + bc*blue)>>15];
  187.                   *(dp++) = *(sp++);  /* alpha */
  188.                }
  189.             }
  190.             else
  191. #endif
  192.             {
  193.                png_bytep sp = row;
  194.                png_bytep dp = row;
  195.                for (i = 0; i < row_width; i++)
  196.                {
  197.                   png_byte red   = *(sp++);
  198.                   png_byte green = *(sp++);
  199.                   png_byte blue  = *(sp++);
  200.                   if(red != green || red != blue)
  201.                      rgb_error |= 1;
  202.                   *(dp++) =  (png_byte)((rc*red + gc*green + bc*blue)>>15);
  203.                   *(dp++) = *(sp++);  /* alpha */
  204.                }
  205.             }
  206.          }
  207.          else /* RGBA bit_depth == 16 */
  208.          {
  209. #if defined(PNG_READ_GAMMA_SUPPORTED) || defined(PNG_READ_BACKGROUND_SUPPORTED)
  210.             if (png_ptr->gamma_16_to_1 != NULL &&
  211.                 png_ptr->gamma_16_from_1 != NULL)
  212.             {
  213.                png_bytep sp = row;
  214.                png_bytep dp = row;
  215.                for (i = 0; i < row_width; i++)
  216.                {
  217.                   png_uint_16 red, green, blue, w;
  218.                   red   = (png_uint_16)(((*(sp))<<8) | *(sp+1)); sp+=2;
  219.                   green = (png_uint_16)(((*(sp))<<8) | *(sp+1)); sp+=2;
  220.                   blue  = (png_uint_16)(((*(sp))<<8) | *(sp+1)); sp+=2;
  221.                   if(red == green && red == blue)
  222.                      w = red;
  223.                   else
  224.                   {
  225.                      png_uint_16 red_1   = png_ptr->gamma_16_to_1[(red&0xff) >>
  226.                                   png_ptr->gamma_shift][red>>8];
  227.                      png_uint_16 green_1 = png_ptr->gamma_16_to_1[(green&0xff) >>
  228.                                   png_ptr->gamma_shift][green>>8];
  229.                      png_uint_16 blue_1  = png_ptr->gamma_16_to_1[(blue&0xff) >>
  230.                                   png_ptr->gamma_shift][blue>>8];
  231.                      png_uint_16 gray16  = (png_uint_16)((rc * red_1
  232.                                   + gc * green_1 + bc * blue_1)>>15);
  233.                      w = png_ptr->gamma_16_from_1[(gray16&0xff) >>
  234.                          png_ptr->gamma_shift][gray16 >> 8];
  235.                      rgb_error |= 1;
  236.                   }
  237.                   *(dp++) = (png_byte)((w>>8) & 0xff);
  238.                   *(dp++) = (png_byte)(w & 0xff);
  239.                   *(dp++) = *(sp++);  /* alpha */
  240.                   *(dp++) = *(sp++);
  241.                }
  242.             }
  243.             else
  244. #endif
  245.             {
  246.                png_bytep sp = row;
  247.                png_bytep dp = row;
  248.                for (i = 0; i < row_width; i++)
  249.                {
  250.                   png_uint_16 red, green, blue, gray16;
  251.                   red   = (png_uint_16)((*(sp)<<8) | *(sp+1)); sp+=2;
  252.                   green = (png_uint_16)((*(sp)<<8) | *(sp+1)); sp+=2;
  253.                   blue  = (png_uint_16)((*(sp)<<8) | *(sp+1)); sp+=2;
  254.                   if(red != green || red != blue)
  255.                      rgb_error |= 1;
  256.                   gray16  = (png_uint_16)((rc*red + gc*green + bc*blue)>>15);
  257.                   *(dp++) = (png_byte)((gray16>>8) & 0xff);
  258.                   *(dp++) = (png_byte)(gray16 & 0xff);
  259.                   *(dp++) = *(sp++);  /* alpha */
  260.                   *(dp++) = *(sp++);
  261.                }
  262.             }
  263.          }
  264.       }
  265.    row_info->channels -= (png_byte)2;
  266.       row_info->color_type &= ~PNG_COLOR_MASK_COLOR;
  267.       row_info->pixel_depth = (png_byte)(row_info->channels *
  268.          row_info->bit_depth);
  269.       row_info->rowbytes = PNG_ROWBYTES(row_info->pixel_depth,row_width);
  270.    }
  271.    return rgb_error;
  272. }
  273. #endif
  274. /* Build a grayscale palette.  Palette is assumed to be 1 << bit_depth
  275.  * large of png_color.  This lets grayscale images be treated as
  276.  * paletted.  Most useful for gamma correction and simplification
  277.  * of code.
  278.  */
  279. void PNGAPI
  280. png_build_grayscale_palette(int bit_depth, png_colorp palette)
  281. {
  282.    int num_palette;
  283.    int color_inc;
  284.    int i;
  285.    int v;
  286.    png_debug(1, "in png_do_build_grayscale_paletten");
  287.    if (palette == NULL)
  288.       return;
  289.    switch (bit_depth)
  290.    {
  291.       case 1:
  292.          num_palette = 2;
  293.          color_inc = 0xff;
  294.          break;
  295.       case 2:
  296.          num_palette = 4;
  297.          color_inc = 0x55;
  298.          break;
  299.       case 4:
  300.          num_palette = 16;
  301.          color_inc = 0x11;
  302.          break;
  303.       case 8:
  304.          num_palette = 256;
  305.          color_inc = 1;
  306.          break;
  307.       default:
  308.          num_palette = 0;
  309.          color_inc = 0;
  310.          break;
  311.    }
  312.    for (i = 0, v = 0; i < num_palette; i++, v += color_inc)
  313.    {
  314.       palette[i].red = (png_byte)v;
  315.       palette[i].green = (png_byte)v;
  316.       palette[i].blue = (png_byte)v;
  317.    }
  318. }
  319. /* This function is currently unused.  Do we really need it? */
  320. #if defined(PNG_READ_DITHER_SUPPORTED) && defined(PNG_CORRECT_PALETTE_SUPPORTED)
  321. void /* PRIVATE */
  322. png_correct_palette(png_structp png_ptr, png_colorp palette,
  323.    int num_palette)
  324. {
  325.    png_debug(1, "in png_correct_paletten");
  326. #if defined(PNG_READ_BACKGROUND_SUPPORTED) && 
  327.     defined(PNG_READ_GAMMA_SUPPORTED) && defined(PNG_FLOATING_POINT_SUPPORTED)
  328.    if (png_ptr->transformations & (PNG_GAMMA | PNG_BACKGROUND))
  329.    {
  330.       png_color back, back_1;
  331.       if (png_ptr->background_gamma_type == PNG_BACKGROUND_GAMMA_FILE)
  332.       {
  333.          back.red = png_ptr->gamma_table[png_ptr->background.red];
  334.          back.green = png_ptr->gamma_table[png_ptr->background.green];
  335.          back.blue = png_ptr->gamma_table[png_ptr->background.blue];
  336.          back_1.red = png_ptr->gamma_to_1[png_ptr->background.red];
  337.          back_1.green = png_ptr->gamma_to_1[png_ptr->background.green];
  338.          back_1.blue = png_ptr->gamma_to_1[png_ptr->background.blue];
  339.       }
  340.       else
  341.       {
  342.          double g;
  343.          g = 1.0 / (png_ptr->background_gamma * png_ptr->screen_gamma);
  344.          if (png_ptr->background_gamma_type == PNG_BACKGROUND_GAMMA_SCREEN ||
  345.              fabs(g - 1.0) < PNG_GAMMA_THRESHOLD)
  346.          {
  347.             back.red = png_ptr->background.red;
  348.             back.green = png_ptr->background.green;
  349.             back.blue = png_ptr->background.blue;
  350.          }
  351.          else
  352.          {
  353.             back.red =
  354.                (png_byte)(pow((double)png_ptr->background.red/255, g) *
  355.                 255.0 + 0.5);
  356.             back.green =
  357.                (png_byte)(pow((double)png_ptr->background.green/255, g) *
  358.                 255.0 + 0.5);
  359.             back.blue =
  360.                (png_byte)(pow((double)png_ptr->background.blue/255, g) *
  361.                 255.0 + 0.5);
  362.          }
  363.          g = 1.0 / png_ptr->background_gamma;
  364.          back_1.red =
  365.             (png_byte)(pow((double)png_ptr->background.red/255, g) *
  366.              255.0 + 0.5);
  367.          back_1.green =
  368.             (png_byte)(pow((double)png_ptr->background.green/255, g) *
  369.              255.0 + 0.5);
  370.          back_1.blue =
  371.             (png_byte)(pow((double)png_ptr->background.blue/255, g) *
  372.              255.0 + 0.5);
  373.       }
  374.       if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
  375.       {
  376.          png_uint_32 i;
  377.          for (i = 0; i < (png_uint_32)num_palette; i++)
  378.          {
  379.             if (i < png_ptr->num_trans && png_ptr->trans[i] == 0)
  380.             {
  381.                palette[i] = back;
  382.             }
  383.             else if (i < png_ptr->num_trans && png_ptr->trans[i] != 0xff)
  384.             {
  385.                png_byte v, w;
  386.                v = png_ptr->gamma_to_1[png_ptr->palette[i].red];
  387.                png_composite(w, v, png_ptr->trans[i], back_1.red);
  388.                palette[i].red = png_ptr->gamma_from_1[w];
  389.                v = png_ptr->gamma_to_1[png_ptr->palette[i].green];
  390.                png_composite(w, v, png_ptr->trans[i], back_1.green);
  391.                palette[i].green = png_ptr->gamma_from_1[w];
  392.                v = png_ptr->gamma_to_1[png_ptr->palette[i].blue];
  393.                png_composite(w, v, png_ptr->trans[i], back_1.blue);
  394.                palette[i].blue = png_ptr->gamma_from_1[w];
  395.             }
  396.             else
  397.             {
  398.                palette[i].red = png_ptr->gamma_table[palette[i].red];
  399.                palette[i].green = png_ptr->gamma_table[palette[i].green];
  400.                palette[i].blue = png_ptr->gamma_table[palette[i].blue];
  401.             }
  402.          }
  403.       }
  404.       else
  405.       {
  406.          int i;
  407.          for (i = 0; i < num_palette; i++)
  408.          {
  409.             if (palette[i].red == (png_byte)png_ptr->trans_values.gray)
  410.             {
  411.                palette[i] = back;
  412.             }
  413.             else
  414.             {
  415.                palette[i].red = png_ptr->gamma_table[palette[i].red];
  416.                palette[i].green = png_ptr->gamma_table[palette[i].green];
  417.                palette[i].blue = png_ptr->gamma_table[palette[i].blue];
  418.             }
  419.          }
  420.       }
  421.    }
  422.    else
  423. #endif
  424. #if defined(PNG_READ_GAMMA_SUPPORTED)
  425.    if (png_ptr->transformations & PNG_GAMMA)
  426.    {
  427.       int i;
  428.       for (i = 0; i < num_palette; i++)
  429.       {
  430.          palette[i].red = png_ptr->gamma_table[palette[i].red];
  431.          palette[i].green = png_ptr->gamma_table[palette[i].green];
  432.          palette[i].blue = png_ptr->gamma_table[palette[i].blue];
  433.       }
  434.    }
  435. #if defined(PNG_READ_BACKGROUND_SUPPORTED)
  436.    else
  437. #endif
  438. #endif
  439. #if defined(PNG_READ_BACKGROUND_SUPPORTED)
  440.    if (png_ptr->transformations & PNG_BACKGROUND)
  441.    {
  442.       if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
  443.       {
  444.          png_color back;
  445.          back.red   = (png_byte)png_ptr->background.red;
  446.          back.green = (png_byte)png_ptr->background.green;
  447.          back.blue  = (png_byte)png_ptr->background.blue;
  448.          for (i = 0; i < (int)png_ptr->num_trans; i++)
  449.          {
  450.             if (png_ptr->trans[i] == 0)
  451.             {
  452.                palette[i].red = back.red;
  453.                palette[i].green = back.green;
  454.                palette[i].blue = back.blue;
  455.             }
  456.             else if (png_ptr->trans[i] != 0xff)
  457.             {
  458.                png_composite(palette[i].red, png_ptr->palette[i].red,
  459.                   png_ptr->trans[i], back.red);
  460.                png_composite(palette[i].green, png_ptr->palette[i].green,
  461.                   png_ptr->trans[i], back.green);
  462.                png_composite(palette[i].blue, png_ptr->palette[i].blue,
  463.                   png_ptr->trans[i], back.blue);
  464.             }
  465.          }
  466.       }
  467.       else /* assume grayscale palette (what else could it be?) */
  468.       {
  469.          int i;
  470.          for (i = 0; i < num_palette; i++)
  471.          {
  472.             if (i == (png_byte)png_ptr->trans_values.gray)
  473.             {
  474.                palette[i].red = (png_byte)png_ptr->background.red;
  475.                palette[i].green = (png_byte)png_ptr->background.green;
  476.                palette[i].blue = (png_byte)png_ptr->background.blue;
  477.             }
  478.          }
  479.       }
  480.    }
  481. #endif
  482. }
  483. #endif
  484. #if defined(PNG_READ_BACKGROUND_SUPPORTED)
  485. /* Replace any alpha or transparency with the supplied background color.
  486.  * "background" is already in the screen gamma, while "background_1" is
  487.  * at a gamma of 1.0.  Paletted files have already been taken care of.
  488.  */
  489. void /* PRIVATE */
  490. png_do_background(png_row_infop row_info, png_bytep row,
  491.    png_color_16p trans_values, png_color_16p background
  492. #if defined(PNG_READ_GAMMA_SUPPORTED)
  493.    , png_color_16p background_1,
  494.    png_bytep gamma_table, png_bytep gamma_from_1, png_bytep gamma_to_1,
  495.    png_uint_16pp gamma_16, png_uint_16pp gamma_16_from_1,
  496.    png_uint_16pp gamma_16_to_1, int gamma_shift
  497. #endif
  498.    )
  499. {
  500.    png_bytep sp, dp;
  501.    png_uint_32 i;
  502.    png_uint_32 row_width=row_info->width;
  503.    int shift;
  504.    png_debug(1, "in png_do_backgroundn");
  505.    if (background != NULL &&
  506. #if defined(PNG_USELESS_TESTS_SUPPORTED)
  507.        row != NULL && row_info != NULL &&
  508. #endif
  509.       (!(row_info->color_type & PNG_COLOR_MASK_ALPHA) ||
  510.       (row_info->color_type != PNG_COLOR_TYPE_PALETTE && trans_values)))
  511.    {
  512.       switch (row_info->color_type)
  513.       {
  514.          case PNG_COLOR_TYPE_GRAY:
  515.          {
  516.             switch (row_info->bit_depth)
  517.             {
  518.                case 1:
  519.                {
  520.                   sp = row;
  521.                   shift = 7;
  522.                   for (i = 0; i < row_width; i++)
  523.                   {
  524.                      if ((png_uint_16)((*sp >> shift) & 0x01)
  525.                         == trans_values->gray)
  526.                      {
  527.                         *sp &= (png_byte)((0x7f7f >> (7 - shift)) & 0xff);
  528.                         *sp |= (png_byte)(background->gray << shift);
  529.                      }
  530.                      if (!shift)
  531.                      {
  532.                         shift = 7;
  533.                         sp++;
  534.                      }
  535.                      else
  536.                         shift--;
  537.                   }
  538.                   break;
  539.                }
  540.                case 2:
  541.                {
  542. #if defined(PNG_READ_GAMMA_SUPPORTED)
  543.                   if (gamma_table != NULL)
  544.                   {
  545.                      sp = row;
  546.                      shift = 6;
  547.                      for (i = 0; i < row_width; i++)
  548.                      {
  549.                         if ((png_uint_16)((*sp >> shift) & 0x03)
  550.                             == trans_values->gray)
  551.                         {
  552.                            *sp &= (png_byte)((0x3f3f >> (6 - shift)) & 0xff);
  553.                            *sp |= (png_byte)(background->gray << shift);
  554.                         }
  555.                         else
  556.                         {
  557.                            png_byte p = (png_byte)((*sp >> shift) & 0x03);
  558.                            png_byte g = (png_byte)((gamma_table [p | (p << 2) |
  559.                                (p << 4) | (p << 6)] >> 6) & 0x03);
  560.                            *sp &= (png_byte)((0x3f3f >> (6 - shift)) & 0xff);
  561.                            *sp |= (png_byte)(g << shift);
  562.                         }
  563.                         if (!shift)
  564.                         {
  565.                            shift = 6;
  566.                            sp++;
  567.                         }
  568.                         else
  569.                            shift -= 2;
  570.                      }
  571.                   }
  572.                   else
  573. #endif
  574.                   {
  575.                      sp = row;
  576.                      shift = 6;
  577.                      for (i = 0; i < row_width; i++)
  578.                      {
  579.                         if ((png_uint_16)((*sp >> shift) & 0x03)
  580.                             == trans_values->gray)
  581.                         {
  582.                            *sp &= (png_byte)((0x3f3f >> (6 - shift)) & 0xff);
  583.                            *sp |= (png_byte)(background->gray << shift);
  584.                         }
  585.                         if (!shift)
  586.                         {
  587.                            shift = 6;
  588.                            sp++;
  589.                         }
  590.                         else
  591.                            shift -= 2;
  592.                      }
  593.                   }
  594.                   break;
  595.                }
  596.                case 4:
  597.                {
  598. #if defined(PNG_READ_GAMMA_SUPPORTED)
  599.                   if (gamma_table != NULL)
  600.                   {
  601.                      sp = row;
  602.                      shift = 4;
  603.                      for (i = 0; i < row_width; i++)
  604.                      {
  605.                         if ((png_uint_16)((*sp >> shift) & 0x0f)
  606.                             == trans_values->gray)
  607.                         {
  608.                            *sp &= (png_byte)((0xf0f >> (4 - shift)) & 0xff);
  609.                            *sp |= (png_byte)(background->gray << shift);
  610.                         }
  611.                         else
  612.                         {
  613.                            png_byte p = (png_byte)((*sp >> shift) & 0x0f);
  614.                            png_byte g = (png_byte)((gamma_table[p |
  615.                              (p << 4)] >> 4) & 0x0f);
  616.                            *sp &= (png_byte)((0xf0f >> (4 - shift)) & 0xff);
  617.                            *sp |= (png_byte)(g << shift);
  618.                         }
  619.                         if (!shift)
  620.                         {
  621.                            shift = 4;
  622.                            sp++;
  623.                         }
  624.                         else
  625.                            shift -= 4;
  626.                      }
  627.                   }
  628.                   else
  629. #endif
  630.                   {
  631.                      sp = row;
  632.                      shift = 4;
  633.                      for (i = 0; i < row_width; i++)
  634.                      {
  635.                         if ((png_uint_16)((*sp >> shift) & 0x0f)
  636.                             == trans_values->gray)
  637.                         {
  638.                            *sp &= (png_byte)((0xf0f >> (4 - shift)) & 0xff);
  639.                            *sp |= (png_byte)(background->gray << shift);
  640.                         }
  641.                         if (!shift)
  642.                         {
  643.                            shift = 4;
  644.                            sp++;
  645.                         }
  646.                         else
  647.                            shift -= 4;
  648.                      }
  649.                   }
  650.                   break;
  651.                }
  652.                case 8:
  653.                {
  654. #if defined(PNG_READ_GAMMA_SUPPORTED)
  655.                   if (gamma_table != NULL)
  656.                   {
  657.                      sp = row;
  658.                      for (i = 0; i < row_width; i++, sp++)
  659.                      {
  660.                         if (*sp == trans_values->gray)
  661.                         {
  662.                            *sp = (png_byte)background->gray;
  663.                         }
  664.                         else
  665.                         {
  666.                            *sp = gamma_table[*sp];
  667.                         }
  668.                      }
  669.                   }
  670.                   else
  671. #endif
  672.                   {
  673.                      sp = row;
  674.                      for (i = 0; i < row_width; i++, sp++)
  675.                      {
  676.                         if (*sp == trans_values->gray)
  677.                         {
  678.                            *sp = (png_byte)background->gray;
  679.                         }
  680.                      }
  681.                   }
  682.                   break;
  683.                }
  684.                case 16:
  685.                {
  686. #if defined(PNG_READ_GAMMA_SUPPORTED)
  687.                   if (gamma_16 != NULL)
  688.                   {
  689.                      sp = row;
  690.                      for (i = 0; i < row_width; i++, sp += 2)
  691.                      {
  692.                         png_uint_16 v;
  693.                         v = (png_uint_16)(((*sp) << 8) + *(sp + 1));
  694.                         if (v == trans_values->gray)
  695.                         {
  696.                            /* background is already in screen gamma */
  697.                            *sp = (png_byte)((background->gray >> 8) & 0xff);
  698.                            *(sp + 1) = (png_byte)(background->gray & 0xff);
  699.                         }
  700.                         else
  701.                         {
  702.                            v = gamma_16[*(sp + 1) >> gamma_shift][*sp];
  703.                            *sp = (png_byte)((v >> 8) & 0xff);
  704.                            *(sp + 1) = (png_byte)(v & 0xff);
  705.                         }
  706.                      }
  707.                   }
  708.                   else
  709. #endif
  710.                   {
  711.                      sp = row;
  712.                      for (i = 0; i < row_width; i++, sp += 2)
  713.                      {
  714.                         png_uint_16 v;
  715.                         v = (png_uint_16)(((*sp) << 8) + *(sp + 1));
  716.                         if (v == trans_values->gray)
  717.                         {
  718.                            *sp = (png_byte)((background->gray >> 8) & 0xff);
  719.                            *(sp + 1) = (png_byte)(background->gray & 0xff);
  720.                         }
  721.                      }
  722.                   }
  723.                   break;
  724.                }
  725.             }
  726.             break;
  727.          }
  728.          case PNG_COLOR_TYPE_RGB:
  729.          {
  730.             if (row_info->bit_depth == 8)
  731.             {
  732. #if defined(PNG_READ_GAMMA_SUPPORTED)
  733.                if (gamma_table != NULL)
  734.                {
  735.                   sp = row;
  736.                   for (i = 0; i < row_width; i++, sp += 3)
  737.                   {
  738.                      if (*sp == trans_values->red &&
  739.                         *(sp + 1) == trans_values->green &&
  740.                         *(sp + 2) == trans_values->blue)
  741.                      {
  742.                         *sp = (png_byte)background->red;
  743.                         *(sp + 1) = (png_byte)background->green;
  744.                         *(sp + 2) = (png_byte)background->blue;
  745.                      }
  746.                      else
  747.                      {
  748.                         *sp = gamma_table[*sp];
  749.                         *(sp + 1) = gamma_table[*(sp + 1)];
  750.                         *(sp + 2) = gamma_table[*(sp + 2)];
  751.                      }
  752.                   }
  753.                }
  754.                else
  755. #endif
  756.                {
  757.                   sp = row;
  758.                   for (i = 0; i < row_width; i++, sp += 3)
  759.                   {
  760.                      if (*sp == trans_values->red &&
  761.                         *(sp + 1) == trans_values->green &&
  762.                         *(sp + 2) == trans_values->blue)
  763.                      {
  764.                         *sp = (png_byte)background->red;
  765.                         *(sp + 1) = (png_byte)background->green;
  766.                         *(sp + 2) = (png_byte)background->blue;
  767.                      }
  768.                   }
  769.                }
  770.             }
  771.             else /* if (row_info->bit_depth == 16) */
  772.             {
  773. #if defined(PNG_READ_GAMMA_SUPPORTED)
  774.                if (gamma_16 != NULL)
  775.                {
  776.                   sp = row;
  777.                   for (i = 0; i < row_width; i++, sp += 6)
  778.                   {
  779.                      png_uint_16 r = (png_uint_16)(((*sp) << 8) + *(sp + 1));
  780.                      png_uint_16 g = (png_uint_16)(((*(sp+2)) << 8) + *(sp+3));
  781.                      png_uint_16 b = (png_uint_16)(((*(sp+4)) << 8) + *(sp+5));
  782.                      if (r == trans_values->red && g == trans_values->green &&
  783.                         b == trans_values->blue)
  784.                      {
  785.                         /* background is already in screen gamma */
  786.                         *sp = (png_byte)((background->red >> 8) & 0xff);
  787.                         *(sp + 1) = (png_byte)(background->red & 0xff);
  788.                         *(sp + 2) = (png_byte)((background->green >> 8) & 0xff);
  789.                         *(sp + 3) = (png_byte)(background->green & 0xff);
  790.                         *(sp + 4) = (png_byte)((background->blue >> 8) & 0xff);
  791.                         *(sp + 5) = (png_byte)(background->blue & 0xff);
  792.                      }
  793.                      else
  794.                      {
  795.                         png_uint_16 v = gamma_16[*(sp + 1) >> gamma_shift][*sp];
  796.                         *sp = (png_byte)((v >> 8) & 0xff);
  797.                         *(sp + 1) = (png_byte)(v & 0xff);
  798.                         v = gamma_16[*(sp + 3) >> gamma_shift][*(sp + 2)];
  799.                         *(sp + 2) = (png_byte)((v >> 8) & 0xff);
  800.                         *(sp + 3) = (png_byte)(v & 0xff);
  801.                         v = gamma_16[*(sp + 5) >> gamma_shift][*(sp + 4)];
  802.                         *(sp + 4) = (png_byte)((v >> 8) & 0xff);
  803.                         *(sp + 5) = (png_byte)(v & 0xff);
  804.                      }
  805.                   }
  806.                }
  807.                else
  808. #endif
  809.                {
  810.                   sp = row;
  811.                   for (i = 0; i < row_width; i++, sp += 6)
  812.                   {
  813.                      png_uint_16 r = (png_uint_16)(((*sp) << 8) + *(sp+1));
  814.                      png_uint_16 g = (png_uint_16)(((*(sp+2)) << 8) + *(sp+3));
  815.                      png_uint_16 b = (png_uint_16)(((*(sp+4)) << 8) + *(sp+5));
  816.                      if (r == trans_values->red && g == trans_values->green &&
  817.                         b == trans_values->blue)
  818.                      {
  819.                         *sp = (png_byte)((background->red >> 8) & 0xff);
  820.                         *(sp + 1) = (png_byte)(background->red & 0xff);
  821.                         *(sp + 2) = (png_byte)((background->green >> 8) & 0xff);
  822.                         *(sp + 3) = (png_byte)(background->green & 0xff);
  823.                         *(sp + 4) = (png_byte)((background->blue >> 8) & 0xff);
  824.                         *(sp + 5) = (png_byte)(background->blue & 0xff);
  825.                      }
  826.                   }
  827.                }
  828.             }
  829.             break;
  830.          }
  831.          case PNG_COLOR_TYPE_GRAY_ALPHA:
  832.          {
  833.             if (row_info->bit_depth == 8)
  834.             {
  835. #if defined(PNG_READ_GAMMA_SUPPORTED)
  836.                if (gamma_to_1 != NULL && gamma_from_1 != NULL &&
  837.                    gamma_table != NULL)
  838.                {
  839.                   sp = row;
  840.                   dp = row;
  841.                   for (i = 0; i < row_width; i++, sp += 2, dp++)
  842.                   {
  843.                      png_uint_16 a = *(sp + 1);
  844.                      if (a == 0xff)
  845.                      {
  846.                         *dp = gamma_table[*sp];
  847.                      }
  848.                      else if (a == 0)
  849.                      {
  850.                         /* background is already in screen gamma */
  851.                         *dp = (png_byte)background->gray;
  852.                      }
  853.                      else
  854.                      {
  855.                         png_byte v, w;
  856.                         v = gamma_to_1[*sp];
  857.                         png_composite(w, v, a, background_1->gray);
  858.                         *dp = gamma_from_1[w];
  859.                      }
  860.                   }
  861.                }
  862.                else
  863. #endif
  864.                {
  865.                   sp = row;
  866.                   dp = row;
  867.                   for (i = 0; i < row_width; i++, sp += 2, dp++)
  868.                   {
  869.                      png_byte a = *(sp + 1);
  870.                      if (a == 0xff)
  871.                      {
  872.                         *dp = *sp;
  873.                      }
  874. #if defined(PNG_READ_GAMMA_SUPPORTED)
  875.                      else if (a == 0)
  876.                      {
  877.                         *dp = (png_byte)background->gray;
  878.                      }
  879.                      else
  880.                      {
  881.                         png_composite(*dp, *sp, a, background_1->gray);
  882.                      }
  883. #else
  884.                      *dp = (png_byte)background->gray;
  885. #endif
  886.                   }
  887.                }
  888.             }
  889.             else /* if (png_ptr->bit_depth == 16) */
  890.             {
  891. #if defined(PNG_READ_GAMMA_SUPPORTED)
  892.                if (gamma_16 != NULL && gamma_16_from_1 != NULL &&
  893.                    gamma_16_to_1 != NULL)
  894.                {
  895.                   sp = row;
  896.                   dp = row;
  897.                   for (i = 0; i < row_width; i++, sp += 4, dp += 2)
  898.                   {
  899.                      png_uint_16 a = (png_uint_16)(((*(sp+2)) << 8) + *(sp+3));
  900.                      if (a == (png_uint_16)0xffff)
  901.                      {
  902.                         png_uint_16 v;
  903.                         v = gamma_16[*(sp + 1) >> gamma_shift][*sp];
  904.                         *dp = (png_byte)((v >> 8) & 0xff);
  905.                         *(dp + 1) = (png_byte)(v & 0xff);
  906.                      }
  907. #if defined(PNG_READ_GAMMA_SUPPORTED)
  908.                      else if (a == 0)
  909. #else
  910.                      else
  911. #endif
  912.                      {
  913.                         /* background is already in screen gamma */
  914.                         *dp = (png_byte)((background->gray >> 8) & 0xff);
  915.                         *(dp + 1) = (png_byte)(background->gray & 0xff);
  916.                      }
  917. #if defined(PNG_READ_GAMMA_SUPPORTED)
  918.                      else
  919.                      {
  920.                         png_uint_16 g, v, w;
  921.                         g = gamma_16_to_1[*(sp + 1) >> gamma_shift][*sp];
  922.                         png_composite_16(v, g, a, background_1->gray);
  923.                         w = gamma_16_from_1[(v&0xff) >> gamma_shift][v >> 8];
  924.                         *dp = (png_byte)((w >> 8) & 0xff);
  925.                         *(dp + 1) = (png_byte)(w & 0xff);
  926.                      }
  927. #endif
  928.                   }
  929.                }
  930.                else
  931. #endif
  932.                {
  933.                   sp = row;
  934.                   dp = row;
  935.                   for (i = 0; i < row_width; i++, sp += 4, dp += 2)
  936.                   {
  937.                      png_uint_16 a = (png_uint_16)(((*(sp+2)) << 8) + *(sp+3));
  938.                      if (a == (png_uint_16)0xffff)
  939.                      {
  940.                         png_memcpy(dp, sp, 2);
  941.                      }
  942. #if defined(PNG_READ_GAMMA_SUPPORTED)
  943.                      else if (a == 0)
  944. #else
  945.                      else
  946. #endif
  947.                      {
  948.                         *dp = (png_byte)((background->gray >> 8) & 0xff);
  949.                         *(dp + 1) = (png_byte)(background->gray & 0xff);
  950.                      }
  951. #if defined(PNG_READ_GAMMA_SUPPORTED)
  952.                      else
  953.                      {
  954.                         png_uint_16 g, v;
  955.                         g = (png_uint_16)(((*sp) << 8) + *(sp + 1));
  956.                         png_composite_16(v, g, a, background_1->gray);
  957.                         *dp = (png_byte)((v >> 8) & 0xff);
  958.                         *(dp + 1) = (png_byte)(v & 0xff);
  959.                      }
  960. #endif
  961.                   }
  962.                }
  963.             }
  964.             break;
  965.          }
  966.          case PNG_COLOR_TYPE_RGB_ALPHA:
  967.          {
  968.             if (row_info->bit_depth == 8)
  969.             {
  970. #if defined(PNG_READ_GAMMA_SUPPORTED)
  971.                if (gamma_to_1 != NULL && gamma_from_1 != NULL &&
  972.                    gamma_table != NULL)
  973.                {
  974.                   sp = row;
  975.                   dp = row;
  976.                   for (i = 0; i < row_width; i++, sp += 4, dp += 3)
  977.                   {
  978.                      png_byte a = *(sp + 3);
  979.                      if (a == 0xff)
  980.                      {
  981.                         *dp = gamma_table[*sp];
  982.                         *(dp + 1) = gamma_table[*(sp + 1)];
  983.                         *(dp + 2) = gamma_table[*(sp + 2)];
  984.                      }
  985.                      else if (a == 0)
  986.                      {
  987.                         /* background is already in screen gamma */
  988.                         *dp = (png_byte)background->red;
  989.                         *(dp + 1) = (png_byte)background->green;
  990.                         *(dp + 2) = (png_byte)background->blue;
  991.                      }
  992.                      else
  993.                      {
  994.                         png_byte v, w;
  995.                         v = gamma_to_1[*sp];
  996.                         png_composite(w, v, a, background_1->red);
  997.                         *dp = gamma_from_1[w];
  998.                         v = gamma_to_1[*(sp + 1)];
  999.                         png_composite(w, v, a, background_1->green);
  1000.                         *(dp + 1) = gamma_from_1[w];
  1001.                         v = gamma_to_1[*(sp + 2)];
  1002.                         png_composite(w, v, a, background_1->blue);
  1003.                         *(dp + 2) = gamma_from_1[w];
  1004.                      }
  1005.                   }
  1006.                }
  1007.                else
  1008. #endif
  1009.                {
  1010.                   sp = row;
  1011.                   dp = row;
  1012.                   for (i = 0; i < row_width; i++, sp += 4, dp += 3)
  1013.                   {
  1014.                      png_byte a = *(sp + 3);
  1015.                      if (a == 0xff)
  1016.                      {
  1017.                         *dp = *sp;
  1018.                         *(dp + 1) = *(sp + 1);
  1019.                         *(dp + 2) = *(sp + 2);
  1020.                      }
  1021.                      else if (a == 0)
  1022.                      {
  1023.                         *dp = (png_byte)background->red;
  1024.                         *(dp + 1) = (png_byte)background->green;
  1025.                         *(dp + 2) = (png_byte)background->blue;
  1026.                      }
  1027.                      else
  1028.                      {
  1029.                         png_composite(*dp, *sp, a, background->red);
  1030.                         png_composite(*(dp + 1), *(sp + 1), a,
  1031.                            background->green);
  1032.                         png_composite(*(dp + 2), *(sp + 2), a,
  1033.                            background->blue);
  1034.                      }
  1035.                   }
  1036.                }
  1037.             }
  1038.             else /* if (row_info->bit_depth == 16) */
  1039.             {
  1040. #if defined(PNG_READ_GAMMA_SUPPORTED)
  1041.                if (gamma_16 != NULL && gamma_16_from_1 != NULL &&
  1042.                    gamma_16_to_1 != NULL)
  1043.                {
  1044.                   sp = row;
  1045.                   dp = row;
  1046.                   for (i = 0; i < row_width; i++, sp += 8, dp += 6)
  1047.                   {
  1048.                      png_uint_16 a = (png_uint_16)(((png_uint_16)(*(sp + 6))
  1049.                          << 8) + (png_uint_16)(*(sp + 7)));
  1050.                      if (a == (png_uint_16)0xffff)
  1051.                      {
  1052.                         png_uint_16 v;
  1053.                         v = gamma_16[*(sp + 1) >> gamma_shift][*sp];
  1054.                         *dp = (png_byte)((v >> 8) & 0xff);
  1055.                         *(dp + 1) = (png_byte)(v & 0xff);
  1056.                         v = gamma_16[*(sp + 3) >> gamma_shift][*(sp + 2)];
  1057.                         *(dp + 2) = (png_byte)((v >> 8) & 0xff);
  1058.                         *(dp + 3) = (png_byte)(v & 0xff);
  1059.                         v = gamma_16[*(sp + 5) >> gamma_shift][*(sp + 4)];
  1060.                         *(dp + 4) = (png_byte)((v >> 8) & 0xff);
  1061.                         *(dp + 5) = (png_byte)(v & 0xff);
  1062.                      }
  1063.                      else if (a == 0)
  1064.                      {
  1065.                         /* background is already in screen gamma */
  1066.                         *dp = (png_byte)((background->red >> 8) & 0xff);
  1067.                         *(dp + 1) = (png_byte)(background->red & 0xff);
  1068.                         *(dp + 2) = (png_byte)((background->green >> 8) & 0xff);
  1069.                         *(dp + 3) = (png_byte)(background->green & 0xff);
  1070.                         *(dp + 4) = (png_byte)((background->blue >> 8) & 0xff);
  1071.                         *(dp + 5) = (png_byte)(background->blue & 0xff);
  1072.                      }
  1073.                      else
  1074.                      {
  1075.                         png_uint_16 v, w, x;
  1076.                         v = gamma_16_to_1[*(sp + 1) >> gamma_shift][*sp];
  1077.                         png_composite_16(w, v, a, background_1->red);
  1078.                         x = gamma_16_from_1[((w&0xff) >> gamma_shift)][w >> 8];
  1079.                         *dp = (png_byte)((x >> 8) & 0xff);
  1080.                         *(dp + 1) = (png_byte)(x & 0xff);
  1081.                         v = gamma_16_to_1[*(sp + 3) >> gamma_shift][*(sp + 2)];
  1082.                         png_composite_16(w, v, a, background_1->green);
  1083.                         x = gamma_16_from_1[((w&0xff) >> gamma_shift)][w >> 8];
  1084.                         *(dp + 2) = (png_byte)((x >> 8) & 0xff);
  1085.                         *(dp + 3) = (png_byte)(x & 0xff);
  1086.                         v = gamma_16_to_1[*(sp + 5) >> gamma_shift][*(sp + 4)];
  1087.                         png_composite_16(w, v, a, background_1->blue);
  1088.                         x = gamma_16_from_1[(w & 0xff) >> gamma_shift][w >> 8];
  1089.                         *(dp + 4) = (png_byte)((x >> 8) & 0xff);
  1090.                         *(dp + 5) = (png_byte)(x & 0xff);
  1091.                      }
  1092.                   }
  1093.                }
  1094.                else
  1095. #endif
  1096.                {
  1097.                   sp = row;
  1098.                   dp = row;
  1099.                   for (i = 0; i < row_width; i++, sp += 8, dp += 6)
  1100.                   {
  1101.                      png_uint_16 a = (png_uint_16)(((png_uint_16)(*(sp + 6))
  1102.                         << 8) + (png_uint_16)(*(sp + 7)));
  1103.                      if (a == (png_uint_16)0xffff)
  1104.                      {
  1105.                         png_memcpy(dp, sp, 6);
  1106.                      }
  1107.                      else if (a == 0)
  1108.                      {
  1109.                         *dp = (png_byte)((background->red >> 8) & 0xff);
  1110.                         *(dp + 1) = (png_byte)(background->red & 0xff);
  1111.                         *(dp + 2) = (png_byte)((background->green >> 8) & 0xff);
  1112.                         *(dp + 3) = (png_byte)(background->green & 0xff);
  1113.                         *(dp + 4) = (png_byte)((background->blue >> 8) & 0xff);
  1114.                         *(dp + 5) = (png_byte)(background->blue & 0xff);
  1115.                      }
  1116.                      else
  1117.                      {
  1118.                         png_uint_16 v;
  1119.                         png_uint_16 r = (png_uint_16)(((*sp) << 8) + *(sp + 1));
  1120.                         png_uint_16 g = (png_uint_16)(((*(sp + 2)) << 8)
  1121.                             + *(sp + 3));
  1122.                         png_uint_16 b = (png_uint_16)(((*(sp + 4)) << 8)
  1123.                             + *(sp + 5));
  1124.                         png_composite_16(v, r, a, background->red);
  1125.                         *dp = (png_byte)((v >> 8) & 0xff);
  1126.                         *(dp + 1) = (png_byte)(v & 0xff);
  1127.                         png_composite_16(v, g, a, background->green);
  1128.                         *(dp + 2) = (png_byte)((v >> 8) & 0xff);
  1129.                         *(dp + 3) = (png_byte)(v & 0xff);
  1130.                         png_composite_16(v, b, a, background->blue);
  1131.                         *(dp + 4) = (png_byte)((v >> 8) & 0xff);
  1132.                         *(dp + 5) = (png_byte)(v & 0xff);
  1133.                      }
  1134.                   }
  1135.                }
  1136.             }
  1137.             break;
  1138.          }
  1139.       }
  1140.       if (row_info->color_type & PNG_COLOR_MASK_ALPHA)
  1141.       {
  1142.          row_info->color_type &= ~PNG_COLOR_MASK_ALPHA;
  1143.          row_info->channels--;
  1144.          row_info->pixel_depth = (png_byte)(row_info->channels *
  1145.             row_info->bit_depth);
  1146.          row_info->rowbytes = PNG_ROWBYTES(row_info->pixel_depth,row_width);
  1147.       }
  1148.    }
  1149. }
  1150. #endif
  1151. #if defined(PNG_READ_GAMMA_SUPPORTED)
  1152. /* Gamma correct the image, avoiding the alpha channel.  Make sure
  1153.  * you do this after you deal with the transparency issue on grayscale
  1154.  * or RGB images. If your bit depth is 8, use gamma_table, if it
  1155.  * is 16, use gamma_16_table and gamma_shift.  Build these with
  1156.  * build_gamma_table().
  1157.  */
  1158. void /* PRIVATE */
  1159. png_do_gamma(png_row_infop row_info, png_bytep row,
  1160.    png_bytep gamma_table, png_uint_16pp gamma_16_table,
  1161.    int gamma_shift)
  1162. {
  1163.    png_bytep sp;
  1164.    png_uint_32 i;
  1165.    png_uint_32 row_width=row_info->width;
  1166.    png_debug(1, "in png_do_gamman");
  1167.    if (
  1168. #if defined(PNG_USELESS_TESTS_SUPPORTED)
  1169.        row != NULL && row_info != NULL &&
  1170. #endif
  1171.        ((row_info->bit_depth <= 8 && gamma_table != NULL) ||
  1172.         (row_info->bit_depth == 16 && gamma_16_table != NULL)))
  1173.    {
  1174.       switch (row_info->color_type)
  1175.       {
  1176.          case PNG_COLOR_TYPE_RGB:
  1177.          {
  1178.             if (row_info->bit_depth == 8)
  1179.             {
  1180.                sp = row;
  1181.                for (i = 0; i < row_width; i++)
  1182.                {
  1183.                   *sp = gamma_table[*sp];
  1184.                   sp++;
  1185.                   *sp = gamma_table[*sp];
  1186.                   sp++;
  1187.                   *sp = gamma_table[*sp];
  1188.                   sp++;
  1189.                }
  1190.             }
  1191.             else /* if (row_info->bit_depth == 16) */
  1192.             {
  1193.                sp = row;
  1194.                for (i = 0; i < row_width; i++)
  1195.                {
  1196.                   png_uint_16 v;
  1197.                   v = gamma_16_table[*(sp + 1) >> gamma_shift][*sp];
  1198.                   *sp = (png_byte)((v >> 8) & 0xff);
  1199.                   *(sp + 1) = (png_byte)(v & 0xff);
  1200.                   sp += 2;
  1201.                   v = gamma_16_table[*(sp + 1) >> gamma_shift][*sp];
  1202.                   *sp = (png_byte)((v >> 8) & 0xff);
  1203.                   *(sp + 1) = (png_byte)(v & 0xff);
  1204.                   sp += 2;
  1205.                   v = gamma_16_table[*(sp + 1) >> gamma_shift][*sp];
  1206.                   *sp = (png_byte)((v >> 8) & 0xff);
  1207.                   *(sp + 1) = (png_byte)(v & 0xff);
  1208.                   sp += 2;
  1209.                }
  1210.             }
  1211.             break;
  1212.          }
  1213.          case PNG_COLOR_TYPE_RGB_ALPHA:
  1214.          {
  1215.             if (row_info->bit_depth == 8)
  1216.             {
  1217.                sp = row;
  1218.                for (i = 0; i < row_width; i++)
  1219.                {
  1220.                   *sp = gamma_table[*sp];
  1221.                   sp++;
  1222.                   *sp = gamma_table[*sp];
  1223.                   sp++;
  1224.                   *sp = gamma_table[*sp];
  1225.                   sp++;
  1226.                   sp++;
  1227.                }
  1228.             }
  1229.             else /* if (row_info->bit_depth == 16) */
  1230.             {
  1231.                sp = row;
  1232.                for (i = 0; i < row_width; i++)
  1233.                {
  1234.                   png_uint_16 v = gamma_16_table[*(sp + 1) >> gamma_shift][*sp];
  1235.                   *sp = (png_byte)((v >> 8) & 0xff);
  1236.                   *(sp + 1) = (png_byte)(v & 0xff);
  1237.                   sp += 2;
  1238.                   v = gamma_16_table[*(sp + 1) >> gamma_shift][*sp];
  1239.                   *sp = (png_byte)((v >> 8) & 0xff);
  1240.                   *(sp + 1) = (png_byte)(v & 0xff);
  1241.                   sp += 2;
  1242.                   v = gamma_16_table[*(sp + 1) >> gamma_shift][*sp];
  1243.                   *sp = (png_byte)((v >> 8) & 0xff);
  1244.                   *(sp + 1) = (png_byte)(v & 0xff);
  1245.                   sp += 4;
  1246.                }
  1247.             }
  1248.             break;
  1249.          }
  1250.          case PNG_COLOR_TYPE_GRAY_ALPHA:
  1251.          {
  1252.             if (row_info->bit_depth == 8)
  1253.             {
  1254.                sp = row;
  1255.                for (i = 0; i < row_width; i++)
  1256.                {
  1257.                   *sp = gamma_table[*sp];
  1258.                   sp += 2;
  1259.                }
  1260.             }
  1261.             else /* if (row_info->bit_depth == 16) */
  1262.             {
  1263.                sp = row;
  1264.                for (i = 0; i < row_width; i++)
  1265.                {
  1266.                   png_uint_16 v = gamma_16_table[*(sp + 1) >> gamma_shift][*sp];
  1267.                   *sp = (png_byte)((v >> 8) & 0xff);
  1268.                   *(sp + 1) = (png_byte)(v & 0xff);
  1269.                   sp += 4;
  1270.                }
  1271.             }
  1272.             break;
  1273.          }
  1274.          case PNG_COLOR_TYPE_GRAY:
  1275.          {
  1276.             if (row_info->bit_depth == 2)
  1277.             {
  1278.                sp = row;
  1279.                for (i = 0; i < row_width; i += 4)
  1280.                {
  1281.                   int a = *sp & 0xc0;
  1282.                   int b = *sp & 0x30;
  1283.                   int c = *sp & 0x0c;
  1284.                   int d = *sp & 0x03;
  1285.                   *sp = (png_byte)(
  1286.                         ((((int)gamma_table[a|(a>>2)|(a>>4)|(a>>6)])   ) & 0xc0)|
  1287.                         ((((int)gamma_table[(b<<2)|b|(b>>2)|(b>>4)])>>2) & 0x30)|
  1288.                         ((((int)gamma_table[(c<<4)|(c<<2)|c|(c>>2)])>>4) & 0x0c)|
  1289.                         ((((int)gamma_table[(d<<6)|(d<<4)|(d<<2)|d])>>6) ));
  1290.                   sp++;
  1291.                }
  1292.             }
  1293.             if (row_info->bit_depth == 4)
  1294.             {
  1295.                sp = row;
  1296.                for (i = 0; i < row_width; i += 2)
  1297.                {
  1298.                   int msb = *sp & 0xf0;
  1299.                   int lsb = *sp & 0x0f;
  1300.                   *sp = (png_byte)((((int)gamma_table[msb | (msb >> 4)]) & 0xf0)
  1301.                           | (((int)gamma_table[(lsb << 4) | lsb]) >> 4));
  1302.                   sp++;
  1303.                }
  1304.             }
  1305.             else if (row_info->bit_depth == 8)
  1306.             {
  1307.                sp = row;
  1308.                for (i = 0; i < row_width; i++)
  1309.                {
  1310.                   *sp = gamma_table[*sp];
  1311.                   sp++;
  1312.                }
  1313.             }
  1314.             else if (row_info->bit_depth == 16)
  1315.             {
  1316.                sp = row;
  1317.                for (i = 0; i < row_width; i++)
  1318.                {
  1319.                   png_uint_16 v = gamma_16_table[*(sp + 1) >> gamma_shift][*sp];
  1320.                   *sp = (png_byte)((v >> 8) & 0xff);
  1321.                   *(sp + 1) = (png_byte)(v & 0xff);
  1322.                   sp += 2;
  1323.                }
  1324.             }
  1325.             break;
  1326.          }
  1327.       }
  1328.    }
  1329. }
  1330. #endif
  1331. #if defined(PNG_READ_EXPAND_SUPPORTED)
  1332. /* Expands a palette row to an RGB or RGBA row depending
  1333.  * upon whether you supply trans and num_trans.
  1334.  */
  1335. void /* PRIVATE */
  1336. png_do_expand_palette(png_row_infop row_info, png_bytep row,
  1337.    png_colorp palette, png_bytep trans, int num_trans)
  1338. {
  1339.    int shift, value;
  1340.    png_bytep sp, dp;
  1341.    png_uint_32 i;
  1342.    png_uint_32 row_width=row_info->width;
  1343.    png_debug(1, "in png_do_expand_paletten");
  1344.    if (
  1345. #if defined(PNG_USELESS_TESTS_SUPPORTED)
  1346.        row != NULL && row_info != NULL &&
  1347. #endif
  1348.        row_info->color_type == PNG_COLOR_TYPE_PALETTE)
  1349.    {
  1350.       if (row_info->bit_depth < 8)
  1351.       {
  1352.          switch (row_info->bit_depth)
  1353.          {
  1354.             case 1:
  1355.             {
  1356.                sp = row + (png_size_t)((row_width - 1) >> 3);
  1357.                dp = row + (png_size_t)row_width - 1;
  1358.                shift = 7 - (int)((row_width + 7) & 0x07);
  1359.                for (i = 0; i < row_width; i++)
  1360.                {
  1361.                   if ((*sp >> shift) & 0x01)
  1362.                      *dp = 1;
  1363.                   else
  1364.                      *dp = 0;
  1365.                   if (shift == 7)
  1366.                   {
  1367.                      shift = 0;
  1368.                      sp--;
  1369.                   }
  1370.                   else
  1371.                      shift++;
  1372.                   dp--;
  1373.                }
  1374.                break;
  1375.             }
  1376.             case 2:
  1377.             {
  1378.                sp = row + (png_size_t)((row_width - 1) >> 2);
  1379.                dp = row + (png_size_t)row_width - 1;
  1380.                shift = (int)((3 - ((row_width + 3) & 0x03)) << 1);
  1381.                for (i = 0; i < row_width; i++)
  1382.                {
  1383.                   value = (*sp >> shift) & 0x03;
  1384.                   *dp = (png_byte)value;
  1385.                   if (shift == 6)
  1386.                   {
  1387.                      shift = 0;
  1388.                      sp--;
  1389.                   }
  1390.                   else
  1391.                      shift += 2;
  1392.                   dp--;
  1393.                }
  1394.                break;
  1395.             }
  1396.             case 4:
  1397.             {
  1398.                sp = row + (png_size_t)((row_width - 1) >> 1);
  1399.                dp = row + (png_size_t)row_width - 1;
  1400.                shift = (int)((row_width & 0x01) << 2);
  1401.                for (i = 0; i < row_width; i++)
  1402.                {
  1403.                   value = (*sp >> shift) & 0x0f;
  1404.                   *dp = (png_byte)value;
  1405.                   if (shift == 4)
  1406.                   {
  1407.                      shift = 0;
  1408.                      sp--;
  1409.                   }
  1410.                   else
  1411.                      shift += 4;
  1412.                   dp--;
  1413.                }
  1414.                break;
  1415.             }
  1416.          }
  1417.          row_info->bit_depth = 8;
  1418.          row_info->pixel_depth = 8;
  1419.          row_info->rowbytes = row_width;
  1420.       }
  1421.       switch (row_info->bit_depth)
  1422.       {
  1423.          case 8:
  1424.          {
  1425.             if (trans != NULL)
  1426.             {
  1427.                sp = row + (png_size_t)row_width - 1;
  1428.                dp = row + (png_size_t)(row_width << 2) - 1;
  1429.                for (i = 0; i < row_width; i++)
  1430.                {
  1431.                   if ((int)(*sp) >= num_trans)
  1432.                      *dp-- = 0xff;
  1433.                   else
  1434.                      *dp-- = trans[*sp];
  1435.                   *dp-- = palette[*sp].blue;
  1436.                   *dp-- = palette[*sp].green;
  1437.                   *dp-- = palette[*sp].red;
  1438.                   sp--;
  1439.                }
  1440.                row_info->bit_depth = 8;
  1441.                row_info->pixel_depth = 32;
  1442.                row_info->rowbytes = row_width * 4;
  1443.                row_info->color_type = 6;
  1444.                row_info->channels = 4;
  1445.             }
  1446.             else
  1447.             {
  1448.                sp = row + (png_size_t)row_width - 1;
  1449.                dp = row + (png_size_t)(row_width * 3) - 1;
  1450.                for (i = 0; i < row_width; i++)
  1451.                {
  1452.                   *dp-- = palette[*sp].blue;
  1453.                   *dp-- = palette[*sp].green;
  1454.                   *dp-- = palette[*sp].red;
  1455.                   sp--;
  1456.                }
  1457.                row_info->bit_depth = 8;
  1458.                row_info->pixel_depth = 24;
  1459.                row_info->rowbytes = row_width * 3;
  1460.                row_info->color_type = 2;
  1461.                row_info->channels = 3;
  1462.             }
  1463.             break;
  1464.          }
  1465.       }
  1466.    }
  1467. }
  1468. /* If the bit depth < 8, it is expanded to 8.  Also, if the already
  1469.  * expanded transparency value is supplied, an alpha channel is built.
  1470.  */
  1471. void /* PRIVATE */
  1472. png_do_expand(png_row_infop row_info, png_bytep row,
  1473.    png_color_16p trans_value)
  1474. {
  1475.    int shift, value;
  1476.    png_bytep sp, dp;
  1477.    png_uint_32 i;
  1478.    png_uint_32 row_width=row_info->width;
  1479.    png_debug(1, "in png_do_expandn");
  1480. #if defined(PNG_USELESS_TESTS_SUPPORTED)
  1481.    if (row != NULL && row_info != NULL)
  1482. #endif
  1483.    {
  1484.       if (row_info->color_type == PNG_COLOR_TYPE_GRAY)
  1485.       {
  1486.          png_uint_16 gray = (png_uint_16)(trans_value ? trans_value->gray : 0);
  1487.          if (row_info->bit_depth < 8)
  1488.          {
  1489.             switch (row_info->bit_depth)
  1490.             {
  1491.                case 1:
  1492.                {
  1493.                   gray = (png_uint_16)(gray*0xff);
  1494.                   sp = row + (png_size_t)((row_width - 1) >> 3);
  1495.                   dp = row + (png_size_t)row_width - 1;
  1496.                   shift = 7 - (int)((row_width + 7) & 0x07);
  1497.                   for (i = 0; i < row_width; i++)
  1498.                   {
  1499.                      if ((*sp >> shift) & 0x01)
  1500.                         *dp = 0xff;
  1501.                      else
  1502.                         *dp = 0;
  1503.                      if (shift == 7)
  1504.                      {
  1505.                         shift = 0;
  1506.                         sp--;
  1507.                      }
  1508.                      else
  1509.                         shift++;
  1510.                      dp--;
  1511.                   }
  1512.                   break;
  1513.                }
  1514.                case 2:
  1515.                {
  1516.                   gray = (png_uint_16)(gray*0x55);
  1517.                   sp = row + (png_size_t)((row_width - 1) >> 2);
  1518.                   dp = row + (png_size_t)row_width - 1;
  1519.                   shift = (int)((3 - ((row_width + 3) & 0x03)) << 1);
  1520.                   for (i = 0; i < row_width; i++)
  1521.                   {
  1522.                      value = (*sp >> shift) & 0x03;
  1523.                      *dp = (png_byte)(value | (value << 2) | (value << 4) |
  1524.                         (value << 6));
  1525.                      if (shift == 6)
  1526.                      {
  1527.                         shift = 0;
  1528.                         sp--;
  1529.                      }
  1530.                      else
  1531.                         shift += 2;
  1532.                      dp--;
  1533.                   }
  1534.                   break;
  1535.                }
  1536.                case 4:
  1537.                {
  1538.                   gray = (png_uint_16)(gray*0x11);
  1539.                   sp = row + (png_size_t)((row_width - 1) >> 1);
  1540.                   dp = row + (png_size_t)row_width - 1;
  1541.                   shift = (int)((1 - ((row_width + 1) & 0x01)) << 2);
  1542.                   for (i = 0; i < row_width; i++)
  1543.                   {
  1544.                      value = (*sp >> shift) & 0x0f;
  1545.                      *dp = (png_byte)(value | (value << 4));
  1546.                      if (shift == 4)
  1547.                      {
  1548.                         shift = 0;
  1549.                         sp--;
  1550.                      }
  1551.                      else
  1552.                         shift = 4;
  1553.                      dp--;
  1554.                   }
  1555.                   break;
  1556.                }
  1557.             }
  1558.             row_info->bit_depth = 8;
  1559.             row_info->pixel_depth = 8;
  1560.             row_info->rowbytes = row_width;
  1561.          }
  1562.          if (trans_value != NULL)
  1563.          {
  1564.             if (row_info->bit_depth == 8)
  1565.             {
  1566.                sp = row + (png_size_t)row_width - 1;
  1567.                dp = row + (png_size_t)(row_width << 1) - 1;
  1568.                for (i = 0; i < row_width; i++)
  1569.                {
  1570.                   if (*sp == gray)
  1571.                      *dp-- = 0;
  1572.                   else
  1573.                      *dp-- = 0xff;
  1574.                   *dp-- = *sp--;
  1575.                }
  1576.             }
  1577.             else if (row_info->bit_depth == 16)
  1578.             {
  1579.                sp = row + row_info->rowbytes - 1;
  1580.                dp = row + (row_info->rowbytes << 1) - 1;
  1581.                for (i = 0; i < row_width; i++)
  1582.                {
  1583.                   if (((png_uint_16)*(sp) |
  1584.                      ((png_uint_16)*(sp - 1) << 8)) == gray)
  1585.                   {
  1586.                      *dp-- = 0;
  1587.                      *dp-- = 0;
  1588.                   }
  1589.                   else
  1590.                   {
  1591.                      *dp-- = 0xff;
  1592.                      *dp-- = 0xff;
  1593.                   }
  1594.                   *dp-- = *sp--;
  1595.                   *dp-- = *sp--;
  1596.                }
  1597.             }
  1598.             row_info->color_type = PNG_COLOR_TYPE_GRAY_ALPHA;
  1599.             row_info->channels = 2;
  1600.             row_info->pixel_depth = (png_byte)(row_info->bit_depth << 1);
  1601.             row_info->rowbytes = PNG_ROWBYTES(row_info->pixel_depth,
  1602.                row_width);
  1603.          }
  1604.       }
  1605.       else if (row_info->color_type == PNG_COLOR_TYPE_RGB && trans_value)
  1606.       {
  1607.          if (row_info->bit_depth == 8)
  1608.          {
  1609.             sp = row + (png_size_t)row_info->rowbytes - 1;
  1610.             dp = row + (png_size_t)(row_width << 2) - 1;
  1611.             for (i = 0; i < row_width; i++)
  1612.             {
  1613.                if (*(sp - 2) == trans_value->red &&
  1614.                   *(sp - 1) == trans_value->green &&
  1615.                   *(sp - 0) == trans_value->blue)
  1616.                   *dp-- = 0;
  1617.                else
  1618.                   *dp-- = 0xff;
  1619.                *dp-- = *sp--;
  1620.                *dp-- = *sp--;
  1621.                *dp-- = *sp--;
  1622.             }
  1623.          }
  1624.          else if (row_info->bit_depth == 16)
  1625.          {
  1626.             sp = row + row_info->rowbytes - 1;
  1627.             dp = row + (png_size_t)(row_width << 3) - 1;
  1628.             for (i = 0; i < row_width; i++)
  1629.             {
  1630.                if ((((png_uint_16)*(sp - 4) |
  1631.                   ((png_uint_16)*(sp - 5) << 8)) == trans_value->red) &&
  1632.                   (((png_uint_16)*(sp - 2) |
  1633.                   ((png_uint_16)*(sp - 3) << 8)) == trans_value->green) &&
  1634.                   (((png_uint_16)*(sp - 0) |
  1635.                   ((png_uint_16)*(sp - 1) << 8)) == trans_value->blue))
  1636.                {
  1637.                   *dp-- = 0;
  1638.                   *dp-- = 0;
  1639.                }
  1640.                else
  1641.                {
  1642.                   *dp-- = 0xff;
  1643.                   *dp-- = 0xff;
  1644.                }
  1645.                *dp-- = *sp--;
  1646.                *dp-- = *sp--;
  1647.                *dp-- = *sp--;
  1648.                *dp-- = *sp--;
  1649.                *dp-- = *sp--;
  1650.                *dp-- = *sp--;
  1651.             }
  1652.          }
  1653.          row_info->color_type = PNG_COLOR_TYPE_RGB_ALPHA;
  1654.          row_info->channels = 4;
  1655.          row_info->pixel_depth = (png_byte)(row_info->bit_depth << 2);
  1656.          row_info->rowbytes = PNG_ROWBYTES(row_info->pixel_depth,row_width);
  1657.       }
  1658.    }
  1659. }
  1660. #endif
  1661. #if defined(PNG_READ_DITHER_SUPPORTED)
  1662. void /* PRIVATE */
  1663. png_do_dither(png_row_infop row_info, png_bytep row,
  1664.     png_bytep palette_lookup, png_bytep dither_lookup)
  1665. {
  1666.    png_bytep sp, dp;
  1667.    png_uint_32 i;
  1668.    png_uint_32 row_width=row_info->width;
  1669.    png_debug(1, "in png_do_dithern");
  1670. #if defined(PNG_USELESS_TESTS_SUPPORTED)
  1671.    if (row != NULL && row_info != NULL)
  1672. #endif
  1673.    {
  1674.       if (row_info->color_type == PNG_COLOR_TYPE_RGB &&
  1675.          palette_lookup && row_info->bit_depth == 8)
  1676.       {
  1677.          int r, g, b, p;
  1678.          sp = row;
  1679.          dp = row;
  1680.          for (i = 0; i < row_width; i++)
  1681.          {
  1682.             r = *sp++;
  1683.             g = *sp++;
  1684.             b = *sp++;
  1685.             /* this looks real messy, but the compiler will reduce
  1686.                it down to a reasonable formula.  For example, with
  1687.                5 bits per color, we get:
  1688.                p = (((r >> 3) & 0x1f) << 10) |
  1689.                   (((g >> 3) & 0x1f) << 5) |
  1690.                   ((b >> 3) & 0x1f);
  1691.                */
  1692.             p = (((r >> (8 - PNG_DITHER_RED_BITS)) &
  1693.                ((1 << PNG_DITHER_RED_BITS) - 1)) <<
  1694.                (PNG_DITHER_GREEN_BITS + PNG_DITHER_BLUE_BITS)) |
  1695.                (((g >> (8 - PNG_DITHER_GREEN_BITS)) &
  1696.                ((1 << PNG_DITHER_GREEN_BITS) - 1)) <<
  1697.                (PNG_DITHER_BLUE_BITS)) |
  1698.                ((b >> (8 - PNG_DITHER_BLUE_BITS)) &
  1699.                ((1 << PNG_DITHER_BLUE_BITS) - 1));
  1700.             *dp++ = palette_lookup[p];
  1701.          }
  1702.          row_info->color_type = PNG_COLOR_TYPE_PALETTE;
  1703.          row_info->channels = 1;
  1704.          row_info->pixel_depth = row_info->bit_depth;
  1705.          row_info->rowbytes = PNG_ROWBYTES(row_info->pixel_depth,row_width);
  1706.       }
  1707.       else if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA &&
  1708.          palette_lookup != NULL && row_info->bit_depth == 8)
  1709.       {
  1710.          int r, g, b, p;
  1711.          sp = row;
  1712.          dp = row;
  1713.          for (i = 0; i < row_width; i++)
  1714.          {
  1715.             r = *sp++;
  1716.             g = *sp++;
  1717.             b = *sp++;
  1718.             sp++;
  1719.             p = (((r >> (8 - PNG_DITHER_RED_BITS)) &
  1720.                ((1 << PNG_DITHER_RED_BITS) - 1)) <<
  1721.                (PNG_DITHER_GREEN_BITS + PNG_DITHER_BLUE_BITS)) |
  1722.                (((g >> (8 - PNG_DITHER_GREEN_BITS)) &
  1723.                ((1 << PNG_DITHER_GREEN_BITS) - 1)) <<
  1724.                (PNG_DITHER_BLUE_BITS)) |
  1725.                ((b >> (8 - PNG_DITHER_BLUE_BITS)) &
  1726.                ((1 << PNG_DITHER_BLUE_BITS) - 1));
  1727.             *dp++ = palette_lookup[p];
  1728.          }
  1729.          row_info->color_type = PNG_COLOR_TYPE_PALETTE;
  1730.          row_info->channels = 1;
  1731.          row_info->pixel_depth = row_info->bit_depth;
  1732.          row_info->rowbytes = PNG_ROWBYTES(row_info->pixel_depth,row_width);
  1733.       }
  1734.       else if (row_info->color_type == PNG_COLOR_TYPE_PALETTE &&
  1735.          dither_lookup && row_info->bit_depth == 8)
  1736.       {
  1737.          sp = row;
  1738.          for (i = 0; i < row_width; i++, sp++)
  1739.          {
  1740.             *sp = dither_lookup[*sp];
  1741.          }
  1742.       }
  1743.    }
  1744. }
  1745. #endif
  1746. #ifdef PNG_FLOATING_POINT_SUPPORTED
  1747. #if defined(PNG_READ_GAMMA_SUPPORTED)
  1748. static int png_gamma_shift[] =
  1749.    {0x10, 0x21, 0x42, 0x84, 0x110, 0x248, 0x550, 0xff0};
  1750. /* We build the 8- or 16-bit gamma tables here.  Note that for 16-bit
  1751.  * tables, we don't make a full table if we are reducing to 8-bit in
  1752.  * the future.  Note also how the gamma_16 tables are segmented so that
  1753.  * we don't need to allocate > 64K chunks for a full 16-bit table.
  1754.  */
  1755. void /* PRIVATE */
  1756. png_build_gamma_table(png_structp png_ptr)
  1757. {
  1758.   png_debug(1, "in png_build_gamma_tablen");
  1759.   if (png_ptr->bit_depth <= 8)
  1760.   {
  1761.      int i;
  1762.      double g;
  1763.      if (png_ptr->screen_gamma > .000001)
  1764.         g = 1.0 / (png_ptr->gamma * png_ptr->screen_gamma);
  1765.      else
  1766.         g = 1.0;
  1767.      png_ptr->gamma_table = (png_bytep)png_malloc(png_ptr,
  1768.         (png_uint_32)256);
  1769.      for (i = 0; i < 256; i++)
  1770.      {
  1771.         png_ptr->gamma_table[i] = (png_byte)(pow((double)i / 255.0,
  1772.            g) * 255.0 + .5);
  1773.      }
  1774. #if defined(PNG_READ_BACKGROUND_SUPPORTED) || 
  1775.    defined(PNG_READ_RGB_TO_GRAY_SUPPORTED)
  1776.      if (png_ptr->transformations & ((PNG_BACKGROUND) | PNG_RGB_TO_GRAY))
  1777.      {
  1778.         g = 1.0 / (png_ptr->gamma);
  1779.         png_ptr->gamma_to_1 = (png_bytep)png_malloc(png_ptr,
  1780.            (png_uint_32)256);
  1781.         for (i = 0; i < 256; i++)
  1782.         {
  1783.            png_ptr->gamma_to_1[i] = (png_byte)(pow((double)i / 255.0,
  1784.               g) * 255.0 + .5);
  1785.         }
  1786.         png_ptr->gamma_from_1 = (png_bytep)png_malloc(png_ptr,
  1787.            (png_uint_32)256);
  1788.         if(png_ptr->screen_gamma > 0.000001)
  1789.            g = 1.0 / png_ptr->screen_gamma;
  1790.         else
  1791.            g = png_ptr->gamma;   /* probably doing rgb_to_gray */
  1792.         for (i = 0; i < 256; i++)
  1793.         {
  1794.            png_ptr->gamma_from_1[i] = (png_byte)(pow((double)i / 255.0,
  1795.               g) * 255.0 + .5);
  1796.         }
  1797.      }
  1798. #endif /* PNG_READ_BACKGROUND_SUPPORTED || PNG_RGB_TO_GRAY_SUPPORTED */
  1799.   }
  1800.   else
  1801.   {
  1802.      double g;
  1803.      int i, j, shift, num;
  1804.      int sig_bit;
  1805.      png_uint_32 ig;
  1806.      if (png_ptr->color_type & PNG_COLOR_MASK_COLOR)
  1807.      {
  1808.         sig_bit = (int)png_ptr->sig_bit.red;
  1809.         if ((int)png_ptr->sig_bit.green > sig_bit)
  1810.            sig_bit = png_ptr->sig_bit.green;
  1811.         if ((int)png_ptr->sig_bit.blue > sig_bit)
  1812.            sig_bit = png_ptr->sig_bit.blue;
  1813.      }
  1814.      else
  1815.      {
  1816.         sig_bit = (int)png_ptr->sig_bit.gray;
  1817.      }
  1818.      if (sig_bit > 0)
  1819.         shift = 16 - sig_bit;
  1820.      else
  1821.         shift = 0;
  1822.      if (png_ptr->transformations & PNG_16_TO_8)
  1823.      {
  1824.         if (shift < (16 - PNG_MAX_GAMMA_8))
  1825.            shift = (16 - PNG_MAX_GAMMA_8);
  1826.      }
  1827.      if (shift > 8)
  1828.         shift = 8;
  1829.      if (shift < 0)
  1830.         shift = 0;
  1831.      png_ptr->gamma_shift = (png_byte)shift;
  1832.      num = (1 << (8 - shift));
  1833.      if (png_ptr->screen_gamma > .000001)
  1834.         g = 1.0 / (png_ptr->gamma * png_ptr->screen_gamma);
  1835.      else
  1836.         g = 1.0;
  1837.      png_ptr->gamma_16_table = (png_uint_16pp)png_malloc(png_ptr,
  1838.         (png_uint_32)(num * png_sizeof (png_uint_16p)));
  1839.      if (png_ptr->transformations & (PNG_16_TO_8 | PNG_BACKGROUND))
  1840.      {
  1841.         double fin, fout;
  1842.         png_uint_32 last, max;
  1843.         for (i = 0; i < num; i++)
  1844.         {
  1845.            png_ptr->gamma_16_table[i] = (png_uint_16p)png_malloc(png_ptr,
  1846.               (png_uint_32)(256 * png_sizeof (png_uint_16)));
  1847.         }
  1848.         g = 1.0 / g;
  1849.         last = 0;
  1850.         for (i = 0; i < 256; i++)
  1851.         {
  1852.            fout = ((double)i + 0.5) / 256.0;
  1853.            fin = pow(fout, g);
  1854.            max = (png_uint_32)(fin * (double)((png_uint_32)num << 8));
  1855.            while (last <= max)
  1856.            {
  1857.               png_ptr->gamma_16_table[(int)(last & (0xff >> shift))]
  1858.                  [(int)(last >> (8 - shift))] = (png_uint_16)(
  1859.                  (png_uint_16)i | ((png_uint_16)i << 8));
  1860.               last++;
  1861.            }
  1862.         }
  1863.         while (last < ((png_uint_32)num << 8))
  1864.         {
  1865.            png_ptr->gamma_16_table[(int)(last & (0xff >> shift))]
  1866.               [(int)(last >> (8 - shift))] = (png_uint_16)65535L;
  1867.            last++;
  1868.         }
  1869.      }
  1870.      else
  1871.      {
  1872.         for (i = 0; i < num; i++)
  1873.         {
  1874.            png_ptr->gamma_16_table[i] = (png_uint_16p)png_malloc(png_ptr,
  1875.               (png_uint_32)(256 * png_sizeof (png_uint_16)));
  1876.            ig = (((png_uint_32)i * (png_uint_32)png_gamma_shift[shift]) >> 4);
  1877.            for (j = 0; j < 256; j++)
  1878.            {
  1879.               png_ptr->gamma_16_table[i][j] =
  1880.                  (png_uint_16)(pow((double)(ig + ((png_uint_32)j << 8)) /
  1881.                     65535.0, g) * 65535.0 + .5);
  1882.            }
  1883.         }
  1884.      }
  1885. #if defined(PNG_READ_BACKGROUND_SUPPORTED) || 
  1886.    defined(PNG_READ_RGB_TO_GRAY_SUPPORTED)
  1887.      if (png_ptr->transformations & (PNG_BACKGROUND | PNG_RGB_TO_GRAY))
  1888.      {
  1889.         g = 1.0 / (png_ptr->gamma);
  1890.         png_ptr->gamma_16_to_1 = (png_uint_16pp)png_malloc(png_ptr,
  1891.            (png_uint_32)(num * png_sizeof (png_uint_16p )));
  1892.         for (i = 0; i < num; i++)
  1893.         {
  1894.            png_ptr->gamma_16_to_1[i] = (png_uint_16p)png_malloc(png_ptr,
  1895.               (png_uint_32)(256 * png_sizeof (png_uint_16)));
  1896.            ig = (((png_uint_32)i *
  1897.               (png_uint_32)png_gamma_shift[shift]) >> 4);
  1898.            for (j = 0; j < 256; j++)
  1899.            {
  1900.               png_ptr->gamma_16_to_1[i][j] =
  1901.                  (png_uint_16)(pow((double)(ig + ((png_uint_32)j << 8)) /
  1902.                     65535.0, g) * 65535.0 + .5);
  1903.            }
  1904.         }
  1905.         if(png_ptr->screen_gamma > 0.000001)
  1906.            g = 1.0 / png_ptr->screen_gamma;
  1907.         else
  1908.            g = png_ptr->gamma;   /* probably doing rgb_to_gray */
  1909.         png_ptr->gamma_16_from_1 = (png_uint_16pp)png_malloc(png_ptr,
  1910.            (png_uint_32)(num * png_sizeof (png_uint_16p)));
  1911.         for (i = 0; i < num; i++)
  1912.         {
  1913.            png_ptr->gamma_16_from_1[i] = (png_uint_16p)png_malloc(png_ptr,
  1914.               (png_uint_32)(256 * png_sizeof (png_uint_16)));
  1915.            ig = (((png_uint_32)i *
  1916.               (png_uint_32)png_gamma_shift[shift]) >> 4);
  1917.            for (j = 0; j < 256; j++)
  1918.            {
  1919.               png_ptr->gamma_16_from_1[i][j] =
  1920.                  (png_uint_16)(pow((double)(ig + ((png_uint_32)j << 8)) /
  1921.                     65535.0, g) * 65535.0 + .5);
  1922.            }
  1923.         }
  1924.      }
  1925. #endif /* PNG_READ_BACKGROUND_SUPPORTED || PNG_RGB_TO_GRAY_SUPPORTED */
  1926.   }
  1927. }
  1928. #endif
  1929. /* To do: install integer version of png_build_gamma_table here */
  1930. #endif
  1931. #if defined(PNG_MNG_FEATURES_SUPPORTED)
  1932. /* undoes intrapixel differencing  */
  1933. void /* PRIVATE */
  1934. png_do_read_intrapixel(png_row_infop row_info, png_bytep row)
  1935. {
  1936.    png_debug(1, "in png_do_read_intrapixeln");
  1937.    if (
  1938. #if defined(PNG_USELESS_TESTS_SUPPORTED)
  1939.        row != NULL && row_info != NULL &&
  1940. #endif
  1941.        (row_info->color_type & PNG_COLOR_MASK_COLOR))
  1942.    {
  1943.       int bytes_per_pixel;
  1944.       png_uint_32 row_width = row_info->width;
  1945.       if (row_info->bit_depth == 8)
  1946.       {
  1947.          png_bytep rp;
  1948.          png_uint_32 i;
  1949.          if (row_info->color_type == PNG_COLOR_TYPE_RGB)
  1950.             bytes_per_pixel = 3;
  1951.          else if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA)
  1952.             bytes_per_pixel = 4;
  1953.          else
  1954.             return;
  1955.          for (i = 0, rp = row; i < row_width; i++, rp += bytes_per_pixel)
  1956.          {
  1957.             *(rp) = (png_byte)((256 + *rp + *(rp+1))&0xff);
  1958.             *(rp+2) = (png_byte)((256 + *(rp+2) + *(rp+1))&0xff);
  1959.          }
  1960.       }
  1961.       else if (row_info->bit_depth == 16)
  1962.       {
  1963.          png_bytep rp;
  1964.          png_uint_32 i;
  1965.          if (row_info->color_type == PNG_COLOR_TYPE_RGB)
  1966.             bytes_per_pixel = 6;
  1967.          else if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA)
  1968.             bytes_per_pixel = 8;
  1969.          else
  1970.             return;
  1971.          for (i = 0, rp = row; i < row_width; i++, rp += bytes_per_pixel)
  1972.          {
  1973.             png_uint_32 s0   = (*(rp  ) << 8) | *(rp+1);
  1974.             png_uint_32 s1   = (*(rp+2) << 8) | *(rp+3);
  1975.             png_uint_32 s2   = (*(rp+4) << 8) | *(rp+5);
  1976.             png_uint_32 red  = (png_uint_32)((s0+s1+65536L) & 0xffffL);
  1977.             png_uint_32 blue = (png_uint_32)((s2+s1+65536L) & 0xffffL);
  1978.             *(rp  ) = (png_byte)((red >> 8) & 0xff);
  1979.             *(rp+1) = (png_byte)(red & 0xff);
  1980.             *(rp+4) = (png_byte)((blue >> 8) & 0xff);
  1981.             *(rp+5) = (png_byte)(blue & 0xff);
  1982.          }
  1983.       }
  1984.    }
  1985. }
  1986. #endif /* PNG_MNG_FEATURES_SUPPORTED */
  1987. #endif /* PNG_READ_SUPPORTED */