class Compass::PNG
A simple class to represent and create a PNG-File No drawing features given
Just subclass and write [R,G,B]-Byte-Values into the @data
matrix Build for compactness, so not much error checking!
Code based on seattlerb's png, see seattlerb.rubyforge.org/png/
Constants
- BITS
- CRC_TABLE
- NONE
- RGB
Public Class Methods
new(width, height, background = [255,255,255])
click to toggle source
Initiates a new PNG-Object
-
width
: Width of the image in pixels -
height
: Height of the image in pixels -
background
: Background-color represented as [R,G,B]-Byte-Array
# File lib/compass/grid_builder.rb, line 30 def initialize(width, height, background = [255,255,255]) @height = height @width = width @data = Array.new(@height) { |x| Array.new(@width, background) } end
Public Instance Methods
chunk(type, data="")
click to toggle source
# File lib/compass/grid_builder.rb, line 21 def chunk(type, data="") [data.size, type, data, crc(type + data)].pack("Na*a*N") end
crc(chunkdata='')
click to toggle source
# File lib/compass/grid_builder.rb, line 17 def crc(chunkdata='') chunkdata.unpack('C*').inject(0xffffffff){|crc, byte| CRC_TABLE[(crc ^ byte) & 0xff] ^ (crc >> 8) } ^ 0xffffffff end
png_join()
click to toggle source
# File lib/compass/grid_builder.rb, line 50 def png_join @data.map { |row| "\0" + row.map { |p| "%c%c%c" % p}.join }.join end
to_blob()
click to toggle source
binary representation of the PNG, write to file with binary mode
# File lib/compass/grid_builder.rb, line 41 def to_blob blob = [] blob << [137, 80, 78, 71, 13, 10, 26, 10].pack("C*") blob << PNG.chunk('IHDR', [@width, @height, BITS, RGB, NONE, NONE, NONE].pack("N2C5")) blob << PNG.chunk('IDAT', Zlib::Deflate.deflate(self.png_join)) blob << PNG.chunk('IEND', '') blob.join end