Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added support for writing DPI back to physical dimensions chunk. #117

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
16 changes: 13 additions & 3 deletions lib/chunky_png/chunk.rb
Original file line number Diff line number Diff line change
Expand Up @@ -335,12 +335,22 @@ def initialize(ppux, ppuy, unit = :unknown)

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
ppux * METERS_PER_INCH
end

def dpix=(value)
@unit = :meters
@ppux = (value / METERS_PER_INCH).round
end

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
ppuy * METERS_PER_INCH
end

def dpiy=(value)
@unit = :meters
@ppuy = (value / METERS_PER_INCH).round
end

def self.read(type, content)
Expand All @@ -357,7 +367,7 @@ def content
[ppux, ppuy, unit == :meters ? 1 : 0].pack('NNC')
end

INCHES_PER_METER = 0.0254
METERS_PER_INCH = 0.0254
end

# The Text (iTXt) chunk contains keyword/value metadata about the PNG
Expand Down
1 change: 1 addition & 0 deletions lib/chunky_png/datastream.rb
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ def each_chunk
other_chunks.each { |chunk| yield(chunk) }
yield(palette_chunk) if palette_chunk
yield(transparency_chunk) if transparency_chunk
yield(physical_chunk) if physical_chunk
data_chunks.each { |chunk| yield(chunk) }
yield(end_chunk)
end
Expand Down
9 changes: 9 additions & 0 deletions spec/chunky_png/datastream_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,5 +55,14 @@
expect{physical_chunk.dpix}.to raise_error(ChunkyPNG::UnitsUnknown)
expect{physical_chunk.dpiy}.to raise_error(ChunkyPNG::UnitsUnknown)
end

it 'should set units and ppu correctly when setting dpi' do
physical_chunk = ChunkyPNG::Chunk::Physical.new(9001, 9001, :unknown)
physical_chunk.dpix = 72
physical_chunk.dpiy = 72
expect(physical_chunk.unit).to eql :meters
expect(physical_chunk.ppux).to eql 2835
expect(physical_chunk.ppuy).to eql 2835
end
end
end