rEFIt_UEFI/libeg/lodepng.cpp
/*Computes a minimal PNG color model that can contain all colors as indicated by the stats.
The stats should be computed with lodepng_compute_color_stats.
mode_in is raw color profile of the image the stats were computed on, to copy palette order from when relevant.
Minimal PNG color model means the color type and bit depth that gives smallest amount of bits in the output image,
e.g. gray if only grayscale pixels, palette if less than 256 colors, color key if only single transparent color, ...
This is used if auto_convert is enabled (it is by default).
*/
Computes a minimal PNG color model that can contain all colors as indicated by the stats.
The stats should be computed with lodepng_compute_color_stats.
mode_in is raw color profile of the image the stats were computed on, to copy palette order from when relevant.
Minimal PNG color model means the color type and bit depth that gives smallest amount of bits in the output image,
e.g. gray if only grayscale pixels, palette if less than 256 colors, color key if only single transparent color,
This is used if auto_convert is enabled (it is by default).
Computes a minimal PNG color model that can contain all colors as indicated by the stats . The stats should be computed with lodepng_compute_color_stats . mode_in is raw color profile of the image the stats were computed on to copy palette order from when relevant . Minimal PNG color model means the color type and bit depth that gives smallest amount of bits in the output image e . g . gray if only grayscale pixels palette if less than 256 colors color key if only single transparent color This is used if auto_convert is enabled ( it is by default ) .
static unsigned auto_choose_color(LodePNGColorMode* mode_out,
                                  const LodePNGColorMode* mode_in,
                                  const LodePNGColorStats* stats) {
  unsigned error = 0;
  unsigned palettebits;
  size_t i, n;
  size_t numpixels = stats->numpixels;
  unsigned palette_ok, gray_ok;
  unsigned alpha = stats->alpha;
  unsigned key = stats->key;
  unsigned bits = stats->bits;
  mode_out->key_defined = 0;
  if(key && numpixels <= 16) {
    alpha = 1; 
    key = 0;
    if(bits < 8) bits = 8; 
  }
  gray_ok = !stats->colored;
  if(!stats->allow_greyscale) gray_ok = 0;
  if(!gray_ok && bits < 8) bits = 8;
  n = stats->numcolors;
  palettebits = n <= 2 ? 1 : (n <= 4 ? 2 : (n <= 16 ? 4 : 8));
  palette_ok = n <= 256 && bits <= 8 && n != 0; 
  if(numpixels < n * 2) palette_ok = 0; 
  if(gray_ok && !alpha && bits <= palettebits) palette_ok = 0; 
  if(!stats->allow_palette) palette_ok = 0;
  if(palette_ok) {
    const unsigned char* p = stats->palette;
    lodepng_palette_clear(mode_out); 
    for(i = 0; i != stats->numcolors; ++i) {
      error = lodepng_palette_add(mode_out, p[i * 4 + 0], p[i * 4 + 1], p[i * 4 + 2], p[i * 4 + 3]);
      if(error) break;
    }
    mode_out->colortype = LCT_PALETTE;
    mode_out->bitdepth = palettebits;
    if(mode_in->colortype == LCT_PALETTE && mode_in->palettesize >= mode_out->palettesize
        && mode_in->bitdepth == mode_out->bitdepth) {
      lodepng_color_mode_cleanup(mode_out);
      lodepng_color_mode_copy(mode_out, mode_in);
    }
  } else  {
    mode_out->bitdepth = bits;
    mode_out->colortype = alpha ? (gray_ok ? LCT_GREY_ALPHA : LCT_RGBA)
                                : (gray_ok ? LCT_GREY : LCT_RGB);
    if(key) {
      unsigned mask = (1u << mode_out->bitdepth) - 1u; 
      mode_out->key_r = stats->key_r & mask;
      mode_out->key_g = stats->key_g & mask;
      mode_out->key_b = stats->key_b & mask;
      mode_out->key_defined = 1;
    }
  }
  return error;
}
static unsigned auto_choose_color ( LodePNGColorMode * mode_out , const LodePNGColorMode * mode_in , const LodePNGColorStats * stats ) { unsigned error = 0 ; unsigned palettebits ; size_t i , n ; size_t numpixels = stats -> numpixels ; unsigned palette_ok , gray_ok ; unsigned alpha = stats -> alpha ; unsigned key = stats -> key ; unsigned bits = stats -> bits ; mode_out -> key_defined = 0 ; if ( key && numpixels <= 16 ) { alpha = 1 ; key = 0 ; if ( bits < 8 ) bits = 8 ; } gray_ok = ! stats -> colored ; if ( ! stats -> allow_greyscale ) gray_ok = 0 ; if ( ! gray_ok && bits < 8 ) bits = 8 ; n = stats -> numcolors ; palettebits = n <= 2 ? 1 : ( n <= 4 ? 2 : ( n <= 16 ? 4 : 8 ) ) ; palette_ok = n <= 256 && bits <= 8 && n != 0 ; if ( numpixels < n * 2 ) palette_ok = 0 ; if ( gray_ok && ! alpha && bits <= palettebits ) palette_ok = 0 ; if ( ! stats -> allow_palette ) palette_ok = 0 ; if ( palette_ok ) { const unsigned char * p = stats -> palette ; lodepng_palette_clear ( mode_out ) ; for ( i = 0 ; i != stats -> numcolors ; ++ i ) { error = lodepng_palette_add ( mode_out , p [ i * 4 + 0 ] , p [ i * 4 + 1 ] , p [ i * 4 + 2 ] , p [ i * 4 + 3 ] ) ; if ( error ) break ; } mode_out -> colortype = LCT_PALETTE ; mode_out -> bitdepth = palettebits ; if ( mode_in -> colortype == LCT_PALETTE && mode_in -> palettesize >= mode_out -> palettesize && mode_in -> bitdepth == mode_out -> bitdepth ) { lodepng_color_mode_cleanup ( mode_out ) ; lodepng_color_mode_copy ( mode_out , mode_in ) ; } } else { mode_out -> bitdepth = bits ; mode_out -> colortype = alpha ? ( gray_ok ? LCT_GREY_ALPHA : LCT_RGBA ) : ( gray_ok ? LCT_GREY : LCT_RGB ) ; if ( key ) { unsigned mask = ( 1u << mode_out -> bitdepth ) - 1u ; mode_out -> key_r = stats -> key_r & mask ; mode_out -> key_g = stats -> key_g & mask ; mode_out -> key_b = stats -> key_b & mask ; mode_out -> key_defined = 1 ; } } return error ; }
Computes a minimal PNG color model that can contain all colors as indicated by the stats.
Computes a minimal PNG color model that can contain all colors as indicated by the stats .
/*too few pixels to justify tRNS chunk overhead*/ /*PNG has no alphachannel modes with less than 8-bit per channel*/ /*n==0 means likely numcolors wasn't computed*/ /*don't add palette overhead if image has only a few pixels*/ /*gray is less overhead*/ /*remove potential earlier palette*/ /*If input should have same palette colors, keep original to preserve its order and prevent conversion*/ /*8-bit or 16-bit per channel*/ /*stats always uses 16-bit, mask converts it*/