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 stream.

Overview of the decoding process:

  • The optional PLTE and tRNS chunk are decoded for the color palette of the original image.

  • The contents of the IDAT chunks is combined, and uncompressed using Inflate decompression to the image pixelstream.

  • Based on the color mode, width and height of the original image, which is read from the PNG header (IHDR chunk), the amount of bytes per line is determined.

  • For every line of pixels in the encoded image, the original byte values are restored by unapplying the milter method for that line.

  • The read bytes are unfiltered given by the filter function specified by the first byte of the line.

  • The unfiltered pixelstream are is into colored pixels, using the color mode.

  • All lines combined to form the original image.

For interlaced images, the original image was split into 7 subimages. These images get decoded just like the process above (from step 3), and get combined to form the original images.

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#decoding_paletteChunkyPNG::Palette

The palette that is used to decode the image, loading from the PLTE and tRNS chunk from the PNG stream. For RGB(A) images, no palette is required.

Returns:



35
36
37
# File 'lib/chunky_png/canvas/png_decoding.rb', line 35

def decoding_palette
  @decoding_palette
end

Instance Method Details

#decode_png_pixelstream(stream, width, height, color_mode, depth, interlace) ⇒ ChunkyPNG::Canvas

Decodes a canvas from a PNG encoded pixelstream, using a given width, height, color mode and interlacing mode.

Parameters:

  • stream (String)

    The pixelstream to read from.

  • width (Integer)

    The width of the image.

  • width (Integer)

    The height of the image.

  • color_mode (Integer)

    The color mode of the encoded pixelstream.

  • depth (Integer)

    The bit depth of the pixel samples.

  • interlace (Integer)

    The interlace method of the encoded pixelstream.

Returns:

Raises:



87
88
89
90
91
92
93
94
# File 'lib/chunky_png/canvas/png_decoding.rb', line 87

def decode_png_pixelstream(stream, width, height, color_mode, depth, interlace)
  raise ChunkyPNG::ExpectationFailed, "This palette is not suitable for decoding!" if decoding_palette && !decoding_palette.can_decode?
  case interlace
    when ChunkyPNG::INTERLACING_NONE;  decode_png_without_interlacing(stream, width, height, color_mode, depth)
    when ChunkyPNG::INTERLACING_ADAM7; decode_png_with_adam7_interlacing(stream, width, height, color_mode, depth)
    else raise ChunkyPNG::NotSupported, "Don't know how the handle interlacing method #{interlace}!"
  end
end

#from_blob(str) ⇒ ChunkyPNG::Canvas Also known as: from_string

Decodes a Canvas from a PNG encoded string.

Parameters:

  • str (String)

    The string to read from.

Returns:



40
41
42
# File 'lib/chunky_png/canvas/png_decoding.rb', line 40

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

#from_datastream(ds) ⇒ ChunkyPNG::Canvas

Decodes the Canvas from a PNG datastream instance.

Parameters:

Returns:



65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/chunky_png/canvas/png_decoding.rb', line 65

def from_datastream(ds)
  # raise ChunkyPNG::NotSupported, "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
  depth      = ds.header_chunk.depth

  self.decoding_palette = ChunkyPNG::Palette.from_chunks(ds.palette_chunk, ds.transparency_chunk)
  decode_png_pixelstream(ds.imagedata, width, height, color_mode, depth, interlace)
end

#from_file(filename) ⇒ ChunkyPNG::Canvas

Decodes a Canvas from a PNG encoded file.

Parameters:

  • filename (String)

    The file to read from.

Returns:



49
50
51
# File 'lib/chunky_png/canvas/png_decoding.rb', line 49

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

#from_io(io) ⇒ ChunkyPNG::Canvas Also known as: from_stream

Decodes a Canvas from a PNG encoded stream.

Parameters:

  • io (IO, #read)

    The stream to read from.

Returns:



56
57
58
# File 'lib/chunky_png/canvas/png_decoding.rb', line 56

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