module ChunkyPNG::Canvas::StreamImporting
def from_rgb_stream(width, height, stream)
-
(ChunkyPNG::Canvas)
- The newly constructed canvas instance.
Parameters:
-
stream
(#read, String
) -- The stream to read the pixel data from. -
height
(Integer
) -- The height of the new canvas. -
width
(Integer
) -- The width of the new canvas.
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 << "\255" # 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