Class: Vizcore::Renderer::PngWriter

Inherits:
Object
  • Object
show all
Defined in:
lib/vizcore/renderer/png_writer.rb

Overview

Minimal PNG encoder for RGBA pixel buffers.

Constant Summary collapse

SIGNATURE =
"\x89PNG\r\n\x1A\n".b
COLOR_TYPE_RGBA =
6
BIT_DEPTH =
8

Class Method Summary collapse

Class Method Details

.encode(width:, height:, rgba:) ⇒ String

Returns PNG bytes.

Parameters:

  • width (Integer)
  • height (Integer)
  • rgba (String)

    RGBA bytes, row-major

Returns:

  • (String)

    PNG bytes

Raises:

  • (ArgumentError)


18
19
20
21
22
23
24
25
26
# File 'lib/vizcore/renderer/png_writer.rb', line 18

def encode(width:, height:, rgba:)
  width = Integer(width)
  height = Integer(height)
  pixels = rgba.to_s.b
  expected_size = width * height * 4
  raise ArgumentError, "RGBA buffer must be #{expected_size} bytes" unless pixels.bytesize == expected_size

  SIGNATURE + chunk("IHDR", ihdr(width, height)) + chunk("IDAT", Zlib::Deflate.deflate(scanlines(width, height, pixels))) + chunk("IEND", "".b)
end

.write(path:, width:, height:, rgba:) ⇒ void

This method returns an undefined value.



29
30
31
# File 'lib/vizcore/renderer/png_writer.rb', line 29

def write(path:, width:, height:, rgba:)
  File.binwrite(path, encode(width: width, height: height, rgba: rgba))
end