Skip to content

Commit

Permalink
Add ability to set dpi on physical chunk
Browse files Browse the repository at this point in the history
  • Loading branch information
kobsy committed Nov 7, 2016
1 parent 13fdc7f commit b89144a
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
10 changes: 10 additions & 0 deletions lib/chunky_png/chunk.rb
Original file line number Diff line number Diff line change
Expand Up @@ -338,11 +338,21 @@ def dpix
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 * METERS_PER_INCH
end

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

def self.read(type, content)
ppux, ppuy, unit = content.unpack('NNC')
unit = unit == 1 ? :meters : :unknown
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

0 comments on commit b89144a

Please sign in to comment.