module ChunkyPNG::Canvas::PNGEncoding

def determine_png_encoding(constraints = {})

Returns:
  • (Hash) - A hash with encoding options for {ChunkyPNG::Canvas::PNGEncoding#to_datastream}

Parameters:
  • constraints (Hash, Symbol) -- The constraints for the encoding. This can be a
def determine_png_encoding(constraints = {})
  encoding = case constraints
    when :fast_rgb;         { :color_mode => ChunkyPNG::COLOR_TRUECOLOR, :compression => Zlib::BEST_SPEED }
    when :fast_rgba;        { :color_mode => ChunkyPNG::COLOR_TRUECOLOR_ALPHA, :compression => Zlib::BEST_SPEED }
    when :best_compression; { :compression => Zlib::BEST_COMPRESSION, :filtering => ChunkyPNG::FILTER_PAETH }
    when :good_compression; { :compression => Zlib::BEST_COMPRESSION, :filtering => ChunkyPNG::FILTER_NONE }
    when :no_compression;   { :compression => Zlib::NO_COMPRESSION }
    when :black_and_white;  { :color_mode => ChunkyPNG::COLOR_GRAYSCALE, :bit_depth => 1 } 
    when Hash; constraints
    else raise ChunkyPNG::Exception, "Unknown encoding preset: #{constraints.inspect}"
  end
  # Do not create a palette when the encoding is given and does not require a palette.
  if encoding[:color_mode]
    if encoding[:color_mode] == ChunkyPNG::COLOR_INDEXED
      self.encoding_palette = self.palette
      encoding[:bit_depth] ||= self.encoding_palette.determine_bit_depth
    else
      encoding[:bit_depth] ||= 8
    end
  else
    self.encoding_palette = self.palette
    suggested_color_mode, suggested_bit_depth = encoding_palette.best_color_settings
    encoding[:color_mode] ||= suggested_color_mode
    encoding[:bit_depth]  ||= suggested_bit_depth
  end
  # Use Zlib's default for compression unless otherwise provided.
  encoding[:compression] ||= Zlib::DEFAULT_COMPRESSION
  encoding[:interlace] = case encoding[:interlace]
    when nil, false, ChunkyPNG::INTERLACING_NONE; ChunkyPNG::INTERLACING_NONE
    when true, ChunkyPNG::INTERLACING_ADAM7;      ChunkyPNG::INTERLACING_ADAM7
    else encoding[:interlace]
  end
  encoding[:filtering] ||= case encoding[:compression]
    when Zlib::BEST_COMPRESSION; ChunkyPNG::FILTER_PAETH
    when Zlib::NO_COMPRESSION..Zlib::BEST_SPEED; ChunkyPNG::FILTER_NONE
    else ChunkyPNG::FILTER_UP
  end
  return encoding
end