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/
# File lib/compass/grid_builder.rb, line 21 def chunk(type, data="") [data.size, type, data, crc(type + data)].pack("Na*a*N") end
# 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
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
# File lib/compass/grid_builder.rb, line 50 def png_join @data.map { |row| "\00"" + row.map { |p| "%c%c%c" % p}.join }.join end
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
Generated with the Darkfish Rdoc Generator 2.