From b89144a052bf70f221aa0e805efcaf6e769db53f Mon Sep 17 00:00:00 2001 From: Matthew Kobs Date: Mon, 7 Nov 2016 11:33:35 -0600 Subject: [PATCH] Add ability to set dpi on physical chunk --- lib/chunky_png/chunk.rb | 10 ++++++++++ spec/chunky_png/datastream_spec.rb | 9 +++++++++ 2 files changed, 19 insertions(+) diff --git a/lib/chunky_png/chunk.rb b/lib/chunky_png/chunk.rb index 0b9f51b9..dbbb3e59 100644 --- a/lib/chunky_png/chunk.rb +++ b/lib/chunky_png/chunk.rb @@ -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 diff --git a/spec/chunky_png/datastream_spec.rb b/spec/chunky_png/datastream_spec.rb index 1b095a2b..bbc3b0a8 100644 --- a/spec/chunky_png/datastream_spec.rb +++ b/spec/chunky_png/datastream_spec.rb @@ -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