lib/chunky_png/canvas/png_encoding.rb
# Determines the best possible PNG encoding variables for this image, by analyzing
# the colors used for the image.
#
# You can provide constraints for the encoding variables by passing a hash with
# encoding variables to this method.
#
# @param [Hash, Symbol] constraints The constraints for the encoding. This can be a
#    Hash or a preset symbol.
# @return [Hash] A hash with encoding options for {ChunkyPNG::Canvas::PNGEncoding#to_datastream}
Determines the best possible PNG encoding variables for this image, by analyzing
the colors used for the image.

You can provide constraints for the encoding variables by passing a hash with
encoding variables to this method.
Determines the best possible PNG encoding variables for this image by analyzing the colors used for the image . You can provide constraints for the encoding variables by passing a hash with encoding variables to this method .
def determine_png_encoding(constraints = {})
        encoding = case constraints
          when :fast_rgb         then {color_mode: ChunkyPNG::COLOR_TRUECOLOR, compression: Zlib::BEST_SPEED}
          when :fast_rgba        then {color_mode: ChunkyPNG::COLOR_TRUECOLOR_ALPHA, compression: Zlib::BEST_SPEED}
          when :best_compression then {compression: Zlib::BEST_COMPRESSION, filtering: ChunkyPNG::FILTER_PAETH}
          when :good_compression then {compression: Zlib::BEST_COMPRESSION, filtering: ChunkyPNG::FILTER_NONE}
          when :no_compression   then {compression: Zlib::NO_COMPRESSION}
          when :black_and_white  then {color_mode: ChunkyPNG::COLOR_GRAYSCALE, bit_depth: 1}
          when Hash              then constraints
          else raise ChunkyPNG::Exception, "Unknown encoding preset: #{constraints.inspect}"
        end
        if encoding[:color_mode]
          if encoding[:color_mode] == ChunkyPNG::COLOR_INDEXED
            self.encoding_palette = palette
            encoding[:bit_depth] ||= encoding_palette.determine_bit_depth
          else
            encoding[:bit_depth] ||= 8
          end
        else
          self.encoding_palette = 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
        encoding[:compression] ||= Zlib::DEFAULT_COMPRESSION
        encoding[:interlace] = case encoding[:interlace]
          when nil, false then ChunkyPNG::INTERLACING_NONE
          when true then ChunkyPNG::INTERLACING_ADAM7
          else encoding[:interlace]
        end
        encoding[:filtering] ||= case encoding[:compression]
          when Zlib::BEST_COMPRESSION then ChunkyPNG::FILTER_PAETH
          when Zlib::NO_COMPRESSION..Zlib::BEST_SPEED then ChunkyPNG::FILTER_NONE
          else ChunkyPNG::FILTER_UP
        end
        encoding
      end
def determine_png_encoding ( constraints = { } ) encoding = case constraints when :fast_rgb then { color_mode : ChunkyPNG :: COLOR_TRUECOLOR , compression : Zlib :: BEST_SPEED } when :fast_rgba then { color_mode : ChunkyPNG :: COLOR_TRUECOLOR_ALPHA , compression : Zlib :: BEST_SPEED } when :best_compression then { compression : Zlib :: BEST_COMPRESSION , filtering : ChunkyPNG :: FILTER_PAETH } when :good_compression then { compression : Zlib :: BEST_COMPRESSION , filtering : ChunkyPNG :: FILTER_NONE } when :no_compression then { compression : Zlib :: NO_COMPRESSION } when :black_and_white then { color_mode : ChunkyPNG :: COLOR_GRAYSCALE , bit_depth : 1 } when Hash then constraints else raise ChunkyPNG :: Exception , "Unknown encoding preset: #{constraints.inspect}" end if encoding [ :color_mode ] if encoding [ :color_mode ] == ChunkyPNG :: COLOR_INDEXED self . encoding_palette = palette encoding [ :bit_depth ] ||= encoding_palette . determine_bit_depth else encoding [ :bit_depth ] ||= 8 end else self . encoding_palette = 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 encoding [ :compression ] ||= Zlib :: DEFAULT_COMPRESSION encoding [ :interlace ] = case encoding [ :interlace ] when nil , false then ChunkyPNG :: INTERLACING_NONE when true then ChunkyPNG :: INTERLACING_ADAM7 else encoding [ :interlace ] end encoding [ :filtering ] ||= case encoding [ :compression ] when Zlib :: BEST_COMPRESSION then ChunkyPNG :: FILTER_PAETH when Zlib :: NO_COMPRESSION .. Zlib :: BEST_SPEED then ChunkyPNG :: FILTER_NONE else ChunkyPNG :: FILTER_UP end encoding end
Determines the best possible PNG encoding variables for this image, by analyzing
the colors used for the image.
Determines the best possible PNG encoding variables for this image by analyzing the colors used for the image .
# Do not create a palette when the encoding is given and does not require a palette. # Use Zlib's default for compression unless otherwise provided.
A hash with encoding options for {ChunkyPNG::Canvas::PNGEncoding#to_datastream}
A hash with encoding options for { ChunkyPNG :: Canvas :: PNGEncoding#to_datastream }
Hash
constraints
<not_specific>
<not_specific>
constraints The constraints for the encoding. This can be a Hash or a preset symbol.
constraints The constraints for the encoding . This can be a Hash or a preset symbol .