lib/chunky_png/image.rb in chunky_png-0.0.5 vs lib/chunky_png/image.rb in chunky_png-0.5.0

- old
+ new

@@ -1,43 +1,44 @@ module ChunkyPNG - class Image + + # Image class + # + class Image < Canvas + + METADATA_COMPRESSION_TRESHOLD = 300 - attr_reader :pixel_matrix + attr_accessor :metadata - def initialize(width, height, background_color = ChunkyPNG::Pixel::TRANSPARENT) - @pixel_matrix = ChunkyPNG::PixelMatrix.new(width, height, background_color) + def initialize(width, height, initial = ChunkyPNG::Color::TRANSPARENT, metadata = {}) + super(width, height, initial) + @metadata = metadata + @metadata_compression_treshhold = 300 end - def self.from_pixel_matrix(matrix) - self.new(matrix.width, matrix.height, matrix.pixels) + def initialize_copy(other) + super(other) + @metdata = other.metadata end - def width - pixel_matrix.width + def metadata_chunks + metadata.map do |key, value| + if value.length >= METADATA_COMPRESSION_TRESHOLD + ChunkyPNG::Chunk::CompressedText.new(key, value) + else + ChunkyPNG::Chunk::Text.new(key, value) + end + end end - def height - pixel_matrix.height + def to_datastream(constraints = {}) + ds = super(constraints) + ds.other_chunks += metadata_chunks + return ds end - def [](x, y) - pixel_matrix[x,y] + def self.from_datastream(ds) + image = super(ds) + image.metadata = ds.metadata + return image end - - def []=(x, y, pixel) - pixel_matrix[x,y] = pixel - end - - def pixels - pixel_matrix.pixels - end - - def write(io, constraints = {}) - datastream = pixel_matrix.to_datastream(constraints) - datastream.write(io) - end - - def save(filename, constraints = {}) - File.open(filename, 'w') { |io| write(io, constraints) } - end end -end \ No newline at end of file +end