Module: ChunkyPNG::Canvas::PNGDecoding

Included in:
ChunkyPNG::Canvas
Defined in:
lib/chunky_png/canvas/png_decoding.rb

Overview

The PNGDecoding contains methods for decoding PNG datastreams to create a Canvas object. The datastream can be provided as filename, string or IO object.

Instance Method Summary collapse

Instance Method Details

#decode_png_pixelstream(stream, width, height, color_mode = ChunkyPNG::COLOR_TRUECOLOR, palette = nil, interlace = ChunkyPNG::INTERLACING_NONE) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/chunky_png/canvas/png_decoding.rb', line 34

def decode_png_pixelstream(stream, width, height, color_mode = ChunkyPNG::COLOR_TRUECOLOR, palette = nil, interlace = ChunkyPNG::INTERLACING_NONE)
  raise "This palette is not suitable for decoding!" if palette && !palette.can_decode?

  pixel_size    = Color.bytesize(color_mode)
  pixel_decoder = case color_mode
    when ChunkyPNG::COLOR_TRUECOLOR       then lambda { |bytes| ChunkyPNG::Color.rgb(*bytes) }
    when ChunkyPNG::COLOR_TRUECOLOR_ALPHA then lambda { |bytes| ChunkyPNG::Color.rgba(*bytes) }
    when ChunkyPNG::COLOR_INDEXED         then lambda { |bytes| palette[bytes.first] }
    when ChunkyPNG::COLOR_GRAYSCALE       then lambda { |bytes| ChunkyPNG::Color.grayscale(*bytes) }
    when ChunkyPNG::COLOR_GRAYSCALE_ALPHA then lambda { |bytes| ChunkyPNG::Color.grayscale(*bytes) }
    else raise "No suitable pixel decoder found for color mode #{color_mode}!"
  end

  return case interlace
    when ChunkyPNG::INTERLACING_NONE  then decode_png_without_interlacing(stream, width, height, pixel_size, pixel_decoder)
    when ChunkyPNG::INTERLACING_ADAM7 then decode_png_with_adam7_interlacing(stream, width, height, pixel_size, pixel_decoder)
    else raise "Don't know how the handle interlacing method #{interlace}!"
  end
end

#from_blob(str) ⇒ Object Also known as: from_string



8
9
10
# File 'lib/chunky_png/canvas/png_decoding.rb', line 8

def from_blob(str)
  from_datastream(ChunkyPNG::Datastream.from_blob(str))
end

#from_datastream(ds) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
# File 'lib/chunky_png/canvas/png_decoding.rb', line 22

def from_datastream(ds)
  raise "Only 8-bit color depth is currently supported by ChunkyPNG!" unless ds.header_chunk.depth == 8

  width      = ds.header_chunk.width
  height     = ds.header_chunk.height
  color_mode = ds.header_chunk.color
  interlace  = ds.header_chunk.interlace
  palette    = ChunkyPNG::Palette.from_chunks(ds.palette_chunk, ds.transparency_chunk)
  stream     = ChunkyPNG::Chunk::ImageData.combine_chunks(ds.data_chunks)
  decode_png_pixelstream(stream, width, height, color_mode, palette, interlace)
end

#from_file(filename) ⇒ Object



14
15
16
# File 'lib/chunky_png/canvas/png_decoding.rb', line 14

def from_file(filename)
  from_datastream(ChunkyPNG::Datastream.from_file(filename))
end

#from_io(io) ⇒ Object



18
19
20
# File 'lib/chunky_png/canvas/png_decoding.rb', line 18

def from_io(io)
  from_datastream(ChunkyPNG::Datastream.from_io(io))
end