module ChunkyPNG::Canvas::StreamImporting
Methods to quickly load a canvas from a stream, encoded in RGB, RGBA, BGR or ABGR format.
Public Instance Methods
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 69 def from_abgr_stream(width, height, stream) 70 string = stream.respond_to?(:read) ? stream.read(4 * width * height) : stream.to_s[0, 4 * width * height] 71 new(width, height, string.unpack("V*")) 72 end
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 51 def from_bgr_stream(width, height, stream) 52 string = ChunkyPNG::EXTRA_BYTE.dup # Add a first byte to the first BGR triple. 53 string << (stream.respond_to?(:read) ? stream.read(3 * width * height) : stream.to_s[0, 3 * width * height]) 54 pixels = string.unpack("@1" << ("XV" * (width * height))).map { |color| color | 0x000000ff } 55 new(width, height, pixels) 56 end
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 16 def from_rgb_stream(width, height, stream) 17 string = stream.respond_to?(:read) ? stream.read(3 * width * height) : stream.to_s[0, 3 * width * height] 18 string << ChunkyPNG::EXTRA_BYTE # Add a fourth byte to the last RGB triple. 19 unpacker = "NX" * (width * height) 20 pixels = string.unpack(unpacker).map { |color| color | 0x000000ff } 21 new(width, height, pixels) 22 end
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 35 def from_rgba_stream(width, height, stream) 36 string = stream.respond_to?(:read) ? stream.read(4 * width * height) : stream.to_s[0, 4 * width * height] 37 new(width, height, string.unpack("N*")) 38 end