Class: ChunkyPNG::Chunk::ImageData

Inherits:
Generic show all
Defined in:
lib/chunky_png/chunk.rb

Overview

An image data (IDAT) chunk holds (part of) the compressed image pixel data.

The data of an image can be split over multiple chunks, which will have to be combined and inflated in order to decode an image. See {{.combine_chunks} to combine chunks to decode, and {{.split_in_chunks} for encoding a pixeldata stream into IDAT chunks.

Instance Attribute Summary

Attributes inherited from Generic

#content

Attributes inherited from Base

#type

Class Method Summary collapse

Methods inherited from Generic

#initialize, read

Methods inherited from Base

#initialize, #write, #write_with_crc

Constructor Details

This class inherits a constructor from ChunkyPNG::Chunk::Generic

Class Method Details

.combine_chunks(data_chunks) ⇒ String

Combines the list of IDAT chunks and inflates their contents to produce the pixeldata stream for the image.

Returns:

  • (String)

    The combined, inflated pixeldata as binary string



272
273
274
275
276
277
278
# File 'lib/chunky_png/chunk.rb', line 272

def self.combine_chunks(data_chunks)
  zstream = Zlib::Inflate.new
  data_chunks.each { |c| zstream << c.content }
  inflated = zstream.finish
  zstream.close
  inflated
end

.split_in_chunks(data, level = Zlib::DEFAULT_COMPRESSION, chunk_size = 2147483647) ⇒ Object

Splits and compresses a pixeldata stream into a list of IDAT chunks.

Parameters:

  • data (String)

    The binary string of pixeldata

  • level (Integer) (defaults to: Zlib::DEFAULT_COMPRESSION)

    The compression level to use.

  • chunk_size (Integer) (defaults to: 2147483647)

    The maximum size of a chunk.



286
287
288
289
290
# File 'lib/chunky_png/chunk.rb', line 286

def self.split_in_chunks(data, level = Zlib::DEFAULT_COMPRESSION, chunk_size = 2147483647)
  streamdata = Zlib::Deflate.deflate(data, level)
  # TODO: Split long streamdata over multiple chunks
  [ChunkyPNG::Chunk::ImageData.new("IDAT", streamdata)]
end