Class: ChunkyPNG::Chunk::Physical

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

Overview

The Physical (pHYs) chunk specifies the intended pixel size or aspect ratio for display of the image.

www.libpng.org/pub/png/spec/1.2/PNG-Chunks.html#C.pHYs

Constant Summary

INCHES_PER_METER =
0.0254

Instance Attribute Summary collapse

Attributes inherited from Base

#type

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#write, #write_with_crc

Constructor Details

#initialize(ppux, ppuy, unit = :unknown) ⇒ Physical

Returns a new instance of Physical

Raises:

  • (ArgumentError)


330
331
332
333
334
# File 'lib/chunky_png/chunk.rb', line 330

def initialize(ppux, ppuy, unit = :unknown)
  raise ArgumentError, 'unit must be either :meters or :unknown' unless [:meters, :unknown].member?(unit)
  super('pHYs')
  @ppux, @ppuy, @unit = ppux, ppuy, unit
end

Instance Attribute Details

#ppuxObject

Returns the value of attribute ppux



328
329
330
# File 'lib/chunky_png/chunk.rb', line 328

def ppux
  @ppux
end

#ppuyObject

Returns the value of attribute ppuy



328
329
330
# File 'lib/chunky_png/chunk.rb', line 328

def ppuy
  @ppuy
end

#unitObject

Returns the value of attribute unit



328
329
330
# File 'lib/chunky_png/chunk.rb', line 328

def unit
  @unit
end

Class Method Details

.read(type, content) ⇒ Object



346
347
348
349
350
# File 'lib/chunky_png/chunk.rb', line 346

def self.read(type, content)
  ppux, ppuy, unit = content.unpack('NNC')
  unit = unit == 1 ? :meters : :unknown
  new(ppux, ppuy, unit)
end

Instance Method Details

#contentObject

Creates the content to write to the stream, by concatenating the keyword with the deflated value, joined by a null character.

Returns:

  • The content that should be written to the datastream.



356
357
358
# File 'lib/chunky_png/chunk.rb', line 356

def content
  [ppux, ppuy, unit == :meters ? 1 : 0].pack('NNC')
end

#dpixObject



336
337
338
339
# File 'lib/chunky_png/chunk.rb', line 336

def dpix
  raise ChunkyPNG::UnitsUnknown, 'the PNG specifies its physical aspect ratio, but does not specify the units of its pixels\' physical dimensions' unless unit == :meters
  ppux * INCHES_PER_METER
end

#dpiyObject



341
342
343
344
# File 'lib/chunky_png/chunk.rb', line 341

def dpiy
  raise ChunkyPNG::UnitsUnknown, 'the PNG specifies its physical aspect ratio, but does not specify the units of its pixels\' physical dimensions' unless unit == :meters
  ppuy * INCHES_PER_METER
end