chunky_png 1.3.7 → 1.3.8

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 2774ecc9f2d9e0bb09588d857ec1bf66fe0161e2
4
- data.tar.gz: 0144ec4242f3bf5c180a8ef5a1bb51fd3e42ca65
3
+ metadata.gz: de005461c2c1664cf8c8328e5769415e26b365fe
4
+ data.tar.gz: 0cf85db842b733efc160097379b39af523930694
5
5
  SHA512:
6
- metadata.gz: ea93bfb789b22f4158605984c46fdc333a6e2a83d4f0cf2165b739be6da12383128fd61d89558bed9cef213d8546010c482b1244ccc3aa2423eb1943a0df3c34
7
- data.tar.gz: baa158877d3f95359219b5a4c1d31e517a9853a947567a3e5bb4d27c5d9ab876be7321b5e7087d5e56f7895b0e5d515f0d2c44c2d3acb35ba32e92ab7cc9d00a
6
+ metadata.gz: 68ceb6232a10ad75694c1f908b2c7a16d9bb5b0f9c5cf0f2c017b02b60e5655e99341ab44d6135975888b8855ced580054365d670494f64ea35eeee2eabc1ed6
7
+ data.tar.gz: 791b4ce5e7e2228b5d3ba0c41edb1f9d0b649bbe15f6ea808bca5c716fad10205f1cff408a3f79daf8389664e58124f4e53e74018ece8da38afed0cf685e3ce7
@@ -9,6 +9,10 @@ The file documents the changes to this library over the different versions.
9
9
 
10
10
  - Nothing yet!
11
11
 
12
+ === 1.3.8 - 2016-08-31
13
+
14
+ - Add support for reading and writing an image's physical dimension (pHYs chunks).
15
+
12
16
  === 1.3.7 - 2016-08-31
13
17
 
14
18
  - Performance improvement for <tt>Color.euclidean_distance_rgba</tt>.
@@ -121,6 +121,11 @@ module ChunkyPNG
121
121
  class OutOfBounds < ChunkyPNG::ExpectationFailed
122
122
  end
123
123
 
124
+ # Exception that is raised when requesting the DPI of a PNG that doesn't
125
+ # specify the units of its physical pixel dimensions.
126
+ class UnitsUnknown < ChunkyPNG::Exception
127
+ end
128
+
124
129
  def self.force_binary(str)
125
130
  str.respond_to?(:force_encoding) ? str.force_encoding('BINARY') : str
126
131
  end
@@ -319,6 +319,47 @@ module ChunkyPNG
319
319
  end
320
320
  end
321
321
 
322
+ # The Physical (pHYs) chunk specifies the intended pixel size or aspect
323
+ # ratio for display of the image.
324
+ #
325
+ # http://www.libpng.org/pub/png/spec/1.2/PNG-Chunks.html#C.pHYs
326
+ #
327
+ class Physical < Generic
328
+ attr_accessor :ppux, :ppuy, :unit
329
+
330
+ def initialize(ppux, ppuy, unit = :unknown)
331
+ raise ArgumentError, 'unit must be either :meters or :unknown' unless [:meters, :unknown].member?(unit)
332
+ super('pHYs')
333
+ @ppux, @ppuy, @unit = ppux, ppuy, unit
334
+ end
335
+
336
+ def dpix
337
+ raise ChunkyPNG::UnitsUnknown, 'the PNG specifies its physical aspect ratio, but does not specify the units of its pixels\' physical dimensions' unless unit == :meters
338
+ ppux * INCHES_PER_METER
339
+ end
340
+
341
+ def dpiy
342
+ raise ChunkyPNG::UnitsUnknown, 'the PNG specifies its physical aspect ratio, but does not specify the units of its pixels\' physical dimensions' unless unit == :meters
343
+ ppuy * INCHES_PER_METER
344
+ end
345
+
346
+ def self.read(type, content)
347
+ ppux, ppuy, unit = content.unpack('NNC')
348
+ unit = unit == 1 ? :meters : :unknown
349
+ new(ppux, ppuy, unit)
350
+ end
351
+
352
+ # Creates the content to write to the stream, by concatenating the
353
+ # keyword with the deflated value, joined by a null character.
354
+ #
355
+ # @return The content that should be written to the datastream.
356
+ def content
357
+ [ppux, ppuy, unit == :meters ? 1 : 0].pack('NNC')
358
+ end
359
+
360
+ INCHES_PER_METER = 0.0254
361
+ end
362
+
322
363
  # The Text (iTXt) chunk contains keyword/value metadata about the PNG
323
364
  # stream.
324
365
  #
@@ -352,6 +393,7 @@ module ChunkyPNG
352
393
  'tEXt' => Text,
353
394
  'zTXt' => CompressedText,
354
395
  'iTXt' => InternationalText,
396
+ 'pHYs' => Physical,
355
397
  }
356
398
  end
357
399
  end
@@ -29,6 +29,10 @@ module ChunkyPNG
29
29
  # @return [ChunkyPNG::Chunk::Transparency]
30
30
  attr_accessor :transparency_chunk
31
31
 
32
+ # The chunk containing the physical dimensions of the PNG's pixels.
33
+ # @return [ChunkyPNG::Chunk::Physical]
34
+ attr_accessor :physical_chunk
35
+
32
36
  # The chunks that together compose the images pixel data.
33
37
  # @return [Array<ChunkyPNG::Chunk::ImageData>]
34
38
  attr_accessor :data_chunks
@@ -81,6 +85,7 @@ module ChunkyPNG
81
85
  when ChunkyPNG::Chunk::Palette; ds.palette_chunk = chunk
82
86
  when ChunkyPNG::Chunk::Transparency; ds.transparency_chunk = chunk
83
87
  when ChunkyPNG::Chunk::ImageData; ds.data_chunks << chunk
88
+ when ChunkyPNG::Chunk::Physical; ds.physical_chunk = chunk
84
89
  when ChunkyPNG::Chunk::End; ds.end_chunk = chunk
85
90
  else ds.other_chunks << chunk
86
91
  end
@@ -121,6 +126,7 @@ module ChunkyPNG
121
126
  other_chunks.each { |chunk| yield(chunk) }
122
127
  yield(palette_chunk) if palette_chunk
123
128
  yield(transparency_chunk) if transparency_chunk
129
+ yield(physical_chunk) if physical_chunk
124
130
  data_chunks.each { |chunk| yield(chunk) }
125
131
  yield(end_chunk)
126
132
  end
@@ -1,5 +1,5 @@
1
1
  module ChunkyPNG
2
2
  # The current version of ChunkyPNG.
3
3
  # Set it and commit the change this before running rake release.
4
- VERSION = "1.3.7"
4
+ VERSION = "1.3.8"
5
5
  end
@@ -40,4 +40,22 @@ describe ChunkyPNG::Datastream do
40
40
  expect(ds.metadata['Copyright']).to eql "Copyright Willem van Schaik, Singapore 1995-96"
41
41
  end
42
42
  end
43
+
44
+ describe '#physical_chunk' do
45
+ it 'should read and write pHYs chunks correctly' do
46
+ filename = resource_file('clock.png')
47
+ ds = ChunkyPNG::Datastream.from_file(filename)
48
+ expect(ds.physical_chunk.unit).to eql :meters
49
+ expect(ds.physical_chunk.dpix.round).to eql 72
50
+ expect(ds.physical_chunk.dpiy.round).to eql 72
51
+ ds = ChunkyPNG::Datastream.from_blob(ds.to_blob)
52
+ expect(ds.physical_chunk).not_to be_nil
53
+ end
54
+
55
+ it 'should raise ChunkyPNG::UnitsUnknown if we request dpi but the units are unknown' do
56
+ physical_chunk = ChunkyPNG::Chunk::Physical.new(2835, 2835, :unknown)
57
+ expect{physical_chunk.dpix}.to raise_error(ChunkyPNG::UnitsUnknown)
58
+ expect{physical_chunk.dpiy}.to raise_error(ChunkyPNG::UnitsUnknown)
59
+ end
60
+ end
43
61
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: chunky_png
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.3.7
4
+ version: 1.3.8
5
5
  platform: ruby
6
6
  authors:
7
7
  - Willem van Bergen
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-08-31 00:00:00.000000000 Z
11
+ date: 2016-11-11 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rake