module ChunkyPNG::Canvas::PNGEncoding

def encode_png_image_pass_to_stream(stream, color_mode, bit_depth, filtering)

Parameters:
  • filtering (Integer) -- The filtering method to use.
  • bit_depth (Integer) -- The bit depth of the image.
  • color_mode (Integer) -- The color mode to use for encoding.
  • stream (String) -- The stream to write to.
def encode_png_image_pass_to_stream(stream, color_mode, bit_depth, filtering)
  start_pos  = stream.bytesize
  pixel_size = Color.pixel_bytesize(color_mode)
  line_width = Color.scanline_bytesize(color_mode, bit_depth, width)
  
  # Determine the filter method
  encode_method = encode_png_pixels_to_scanline_method(color_mode, bit_depth)
  filter_method = case filtering
    when ChunkyPNG::FILTER_SUB;     :encode_png_str_scanline_sub
    when ChunkyPNG::FILTER_UP;      :encode_png_str_scanline_up
    when ChunkyPNG::FILTER_AVERAGE; :encode_png_str_scanline_average
    when ChunkyPNG::FILTER_PAETH;   :encode_png_str_scanline_paeth
    else nil
  end
  
  0.upto(height - 1) do |y|
    stream << send(encode_method, row(y))
  end
  
  # Now, apply filtering if any
  if filter_method
    (height - 1).downto(0) do |y|
      pos = start_pos + y * (line_width + 1)
      prev_pos = (y == 0) ? nil : pos - (line_width + 1)
      send(filter_method, stream, pos, prev_pos, line_width, pixel_size)
    end
  end
end