Module: ChunkyPNG::Canvas::StreamImporting

### Home Classes Methods

### In Files

- lib/chunky_png/canvas/stream_importing.rb

### Methods

- #from_abgr_stream
- #from_bgr_stream
- #from_rgb_stream
- #from_rgba_stream

### Files

- BENCHMARKING.rdoc
- CHANGELOG.rdoc
- CONTRIBUTING.rdoc
- README.rdoc

### Class/Module Index

Quicksearch

- ChunkyPNG
- ChunkyPNG::CRCMismatch
- ChunkyPNG::Canvas
- ChunkyPNG::Canvas::Adam7Interlacing
- ChunkyPNG::Canvas::DataUrlExporting
- ChunkyPNG::Canvas::DataUrlImporting
- ChunkyPNG::Canvas::Drawing
- ChunkyPNG::Canvas::Masking
- ChunkyPNG::Canvas::Operations
- ChunkyPNG::Canvas::PNGDecoding
- ChunkyPNG::Canvas::PNGEncoding
- ChunkyPNG::Canvas::Resampling
- ChunkyPNG::Canvas::StreamExporting
- ChunkyPNG::Canvas::StreamImporting
- ChunkyPNG::Chunk
- ChunkyPNG::Chunk::Base
- ChunkyPNG::Chunk::CompressedText
- ChunkyPNG::Chunk::End
- ChunkyPNG::Chunk::Generic
- ChunkyPNG::Chunk::Header
- ChunkyPNG::Chunk::ImageData
- ChunkyPNG::Chunk::InternationalText
- ChunkyPNG::Chunk::Palette
- ChunkyPNG::Chunk::Text
- ChunkyPNG::Chunk::Transparency
- ChunkyPNG::Color
- ChunkyPNG::Datastream
- ChunkyPNG::Dimension
- ChunkyPNG::Exception
- ChunkyPNG::ExpectationFailed
- ChunkyPNG::Image
- ChunkyPNG::NotSupported
- ChunkyPNG::OutOfBounds
- ChunkyPNG::Palette
- ChunkyPNG::Point
- ChunkyPNG::RMagick
- ChunkyPNG::SignatureMismatch
- ChunkyPNG::Vector
- Enumerable
- String

No matching classes.

# ChunkyPNG::Canvas::StreamImporting

Methods to quickly load a canvas from a stream, encoded in RGB, RGBA, BGR or ABGR format.

### Public Instance Methods

from_abgr_stream(width, height, stream) click to toggle source

Creates a canvas by reading pixels from an ARGB formatted stream with a provided with and height.

Every pixel should be represented by 4 bytes in the stream, in the correct ARGB order. This format is almost like the internal representation of a canvas object, so this kind of stream can be read extremely quickly.

@param [Integer] width The width of the new canvas. @param [Integer] height The height of the new canvas. @param [read, String] stream The stream to read the pixel data from. @return [ChunkyPNG::Canvas] The newly constructed canvas instance.

    # File lib/chunky_png/canvas/stream_importing.rb, line 71
    def from_abgr_stream(width, height, stream)
      string = stream.respond_to?(:read) ? stream.read(4 * width * height) : stream.to_s[0, 4 * width * height]
      self.new(width, height, string.unpack("V*"))
    end

from_bgr_stream(width, height, stream) click to toggle source

Creates a canvas by reading pixels from an BGR formatted stream with a provided with and height.

Every pixel should be represented by 3 bytes in the stream, in the correct BGR order. This format closely resembles the internal representation of a canvas object, so this kind of stream can be read extremely quickly.

@param [Integer] width The width of the new canvas. @param [Integer] height The height of the new canvas. @param [read, String] stream The stream to read the pixel data from. @return [ChunkyPNG::Canvas] The newly constructed canvas instance.

    # File lib/chunky_png/canvas/stream_importing.rb, line 53
    def from_bgr_stream(width, height, stream)
      string = ChunkyPNG::EXTRA_BYTE.dup # Add a first byte to the first BGR triple.
      string << (stream.respond_to?(:read) ? stream.read(3 * width * height) : stream.to_s[0, 3 * width * height])
      pixels = string.unpack("@1" << ('XV' * (width * height))).map { |color| color | 0x000000ff }
      self.new(width, height, pixels)
    end

from_rgb_stream(width, height, stream) click to toggle source

Creates a canvas by reading pixels from an RGB formatted stream with a provided with and height.

Every pixel should be represented by 3 bytes in the stream, in the correct RGB order. This format closely resembles the internal representation of a canvas object, so this kind of stream can be read extremely quickly.

@param [Integer] width The width of the new canvas. @param [Integer] height The height of the new canvas. @param [read, String] stream The stream to read the pixel data from. @return [ChunkyPNG::Canvas] The newly constructed canvas instance.

    # File lib/chunky_png/canvas/stream_importing.rb, line 18
    def from_rgb_stream(width, height, stream)
      string = stream.respond_to?(:read) ? stream.read(3 * width * height) : stream.to_s[0, 3 * width * height]
      string << ChunkyPNG::EXTRA_BYTE # Add a fourth byte to the last RGB triple.
      unpacker = 'NX' * (width * height)
      pixels = string.unpack(unpacker).map { |color| color | 0x000000ff }
      self.new(width, height, pixels)
    end

from_rgba_stream(width, height, stream) click to toggle source

Creates a canvas by reading pixels from an RGBA formatted stream with a provided with and height.

Every pixel should be represented by 4 bytes in the stream, in the correct RGBA order. This format is exactly like the internal representation of a canvas object, so this kind of stream can be read extremely quickly.

@param [Integer] width The width of the new canvas. @param [Integer] height The height of the new canvas. @param [read, String] stream The stream to read the pixel data from. @return [ChunkyPNG::Canvas] The newly constructed canvas instance.

    # File lib/chunky_png/canvas/stream_importing.rb, line 37
    def from_rgba_stream(width, height, stream)
      string = stream.respond_to?(:read) ? stream.read(4 * width * height) : stream.to_s[0, 4 * width * height]
      self.new(width, height, string.unpack("N*"))
    end

[Validate]

Generated with the Darkfish Rdoc Generator 2.