image_size 1.4.2 → 1.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,15 +1,15 @@
1
1
  ---
2
2
  !binary "U0hBMQ==":
3
3
  metadata.gz: !binary |-
4
- OWEzNGYyODYwNmFkZTE1ZGEzOTA0YmVlZmNiZWE1MmE0NjExZjYzOQ==
4
+ MTIzODU3ZDBhYTI5M2MyOGZjOGFhY2FjNzAxYWVhOWUwM2Q4YWQyYw==
5
5
  data.tar.gz: !binary |-
6
- YTAzMDA3MTIzYzJjYzgxNDIyNjRjZTlhMmNlZDFlNTY3MzU3MmQyOA==
6
+ N2VmMzkwMWFmNzY5NmM5YTY0NTJmOWU5ZDlkMDBiYTlmYTA0NDY3Yg==
7
7
  SHA512:
8
8
  metadata.gz: !binary |-
9
- NWQ2YTNiNjFiNmY0ZGJiNWZkYjBiODQzMmE5Y2I5NjhkMGVmMzdhMGIyZmEz
10
- MjI1NjRiNDFkMTE1ZGE3YzZmYjgwYTcyYmE2NDg5ZWY2YWM2NWVkZGEwNDVj
11
- OWIyM2M0NjIwMmE5Y2Y1YzY5OWM4NTdlMjA4ZjQxMGMxYzNjMmI=
9
+ OTllZGE4NjkyNTdiMzkyZjYzNjhlNjBlOGIwNjA3ZGU2NGRjZDYzOWJiZTNi
10
+ MjNiNjRhZDVlY2FkMDk0Mzk2YjlkZmIwMGM0ZjM4NzUyNjhkOThiYmM2M2Ji
11
+ NzNhZDNlMjVkZjcxYWM2NjgyYjdhNDNhNGVmNzZhOGVlYTdjMDE=
12
12
  data.tar.gz: !binary |-
13
- MTI2YTEwM2IzNTAwYmExNGE5MTE1NGUyNGQyNTFlNTFiMTA2NzEyYzNlZDYw
14
- MzMzYzFmMTQ4NjY0ZWI2YmUxNGNkYWJlYWVlMjZiMzZmNDczOTU0NDJlZTM2
15
- ODUzZGUxYWJkYTEyOGIwNDdlYTQzNjdmOWQyMmUzNWEzYzIyMDE=
13
+ ZTM0NDI5YmFkOTM3YjAzNWY2OWQ3Y2U0ODBlMDhhYzBiYzYzYWVjMzE0Njg1
14
+ NWI0ZDA4MmZhZDY2ODc1YTExYjIzYWYzZTM2OTFiOTNmNDNhYmJkZjU1ZGRl
15
+ MmZiNDY0NTIyODE1YzZlZGQwODM2MTZlMTA3MThlNGQ4N2I3ODE=
@@ -1,10 +1,15 @@
1
+ sudo: false
1
2
  language: ruby
3
+ before_install:
4
+ - gem update bundler
2
5
  rvm:
3
- - 1.8.7
4
- - 1.9.2
5
- - 1.9.3
6
- - 2.0.0
6
+ - '1.8'
7
+ - '1.9'
8
+ - '2.0'
9
+ - '2.1'
10
+ - '2.2'
11
+ - '2.3.0'
7
12
  - jruby-18mode
8
13
  - jruby-19mode
9
14
  - ree
10
- script: "bundle exec rspec"
15
+ script: bundle exec rspec
@@ -1,7 +1,7 @@
1
1
  # image_size
2
2
 
3
3
  measure image size using pure Ruby
4
- formats: `bmp`, `cur`, `gif`, `jpeg`, `ico`, `pbm`, `pcx`, `pgm`, `png`, `ppm`, `psd`, `swf`, `tiff`, `xbm`, `xpm`
4
+ formats: `apng`, `bmp`, `cur`, `gif`, `jpeg`, `ico`, `mng`, `pbm`, `pcx`, `pgm`, `png`, `ppm`, `psd`, `swf`, `tiff`, `xbm`, `xpm`, `webp`
5
5
 
6
6
  [![Build Status](https://travis-ci.org/toy/image_size.png?branch=master)](https://travis-ci.org/toy/image_size)
7
7
 
@@ -13,14 +13,24 @@ The latest version of image\_size can be found at http://github.com/toy/image_si
13
13
 
14
14
  gem install image_size
15
15
 
16
+ ## Usage
17
+
18
+ image_size = ImageSize.path('spec/images/jpeg/320x240.jpeg')
19
+ image_size.format #=> :jpec
20
+ image_size.width #=> 320
21
+ image_size.height #=> 240
22
+ image_size.size #=> [320, 240]
23
+
24
+ `width` and `height` have aliases `w` and `h`.
25
+
16
26
  ## Examples
17
27
 
18
28
  require 'image_size'
19
29
 
20
- p ImageSize.path('spec/test.jpg').size
30
+ ImageSize.path('spec/test.jpg')
21
31
 
22
32
  open('spec/test.jpg', 'rb') do |fh|
23
- p ImageSize.new(fh).size
33
+ ImageSize.new(fh)
24
34
  end
25
35
 
26
36
 
@@ -28,12 +38,12 @@ The latest version of image\_size can be found at http://github.com/toy/image_si
28
38
  require 'open-uri'
29
39
 
30
40
  open('http://www.rubycgi.org/image/ruby_gtk_book_title.jpg', 'rb') do |fh|
31
- p ImageSize.new(fh).size
41
+ ImageSize.new(fh)
32
42
  end
33
43
 
34
44
  open('http://www.rubycgi.org/image/ruby_gtk_book_title.jpg', 'rb') do |fh|
35
45
  data = fh.read
36
- p ImageSize.new(data).size
46
+ ImageSize.new(data)
37
47
  end
38
48
 
39
49
  ## Licence
@@ -2,9 +2,9 @@
2
2
 
3
3
  Gem::Specification.new do |s|
4
4
  s.name = 'image_size'
5
- s.version = '1.4.2'
5
+ s.version = '1.5.0'
6
6
  s.summary = %q{Measure image size using pure Ruby}
7
- s.description = %q{Measure following file dimensions: bmp, cur, gif, jpeg, ico, pbm, pcx, pgm, png, ppm, psd, swf, tiff, xbm, xpm}
7
+ s.description = %q{Measure following file dimensions: apng, bmp, cur, gif, jpeg, ico, mng, pbm, pcx, pgm, png, ppm, psd, swf, tiff, xbm, xpm, webp}
8
8
  s.homepage = "http://github.com/toy/#{s.name}"
9
9
  s.authors = ['Keisuke Minami', 'Ivan Kuchin']
10
10
  s.license = 'Ruby'
@@ -85,9 +85,7 @@ class ImageSize
85
85
 
86
86
  # get image width and height as an array which to_s method returns "#{width}x#{height}"
87
87
  def size
88
- if format
89
- Size.new([width, height])
90
- end
88
+ Size.new([width, height]) if format
91
89
  end
92
90
 
93
91
  private
@@ -96,36 +94,59 @@ private
96
94
  def detect_format(ir)
97
95
  head = ir[0, 1024]
98
96
  case
99
- when head =~ /^GIF8[7,9]a/ then :gif
100
- when head[0, 8] == "\211PNG\r\n\032\n" then :png
97
+ when head[0, 6] =~ /GIF8[79]a/ then :gif
98
+ when head[0, 8] == "\211PNG\r\n\032\n" then detect_png_type(ir)
99
+ when head[0, 8] == "\212MNG\r\n\032\n" then :mng
101
100
  when head[0, 2] == "\377\330" then :jpeg
102
101
  when head[0, 2] == 'BM' then :bmp
103
- when head =~ /^P[1-7]/ then :ppm
102
+ when head[0, 2] =~ /P[1-7]/ then :ppm
104
103
  when head =~ /\#define\s+\S+\s+\d+/ then :xbm
105
104
  when head[0, 4] == "II*\000" then :tiff
106
105
  when head[0, 4] == "MM\000*" then :tiff
107
106
  when head =~ /\/\* XPM \*\// then :xpm
108
107
  when head[0, 4] == '8BPS' then :psd
109
- when head =~ /^[FC]WS/ then :swf
108
+ when head[0, 3] =~ /[FC]WS/ then :swf
110
109
  when head[SVG_R] ||
111
110
  head =~ /<\?xml|<!--/ && ir[0, 4096][SVG_R]
112
111
  then :svg
113
112
  when head[0, 2] =~ /\n[\000-\005]/ then :pcx
113
+ when head[0, 12] =~ /RIFF(?m:....)WEBP/ then :webp
114
114
  when head[0, 4] == "\000\000\001\000" then :ico
115
115
  when head[0, 4] == "\000\000\002\000" then :cur
116
116
  end
117
117
  end
118
118
 
119
+ def detect_png_type(ir)
120
+ offset = 8
121
+ loop do
122
+ type = ir[offset + 4, 4]
123
+ break if ['IDAT', 'IEND', nil].include?(type)
124
+ return :apng if type == 'acTL'
125
+
126
+ length = ir[offset, 4].unpack('N')[0]
127
+ offset += 8 + length + 4
128
+ end
129
+ :png
130
+ end
131
+
119
132
  def size_of_gif(ir)
120
133
  ir[6, 4].unpack('vv')
121
134
  end
122
135
 
136
+ def size_of_mng(ir)
137
+ unless ir[12, 4] == 'MHDR'
138
+ raise FormatError, 'MHDR not in place for MNG'
139
+ end
140
+ ir[16, 8].unpack('NN')
141
+ end
142
+
123
143
  def size_of_png(ir)
124
144
  unless ir[12, 4] == 'IHDR'
125
145
  raise FormatError, 'IHDR not in place for PNG'
126
146
  end
127
147
  ir[16, 8].unpack('NN')
128
148
  end
149
+ alias_method :size_of_apng, :size_of_png
129
150
 
130
151
  JpegCodeCheck = [
131
152
  "\xc0", "\xc1", "\xc2", "\xc3",
@@ -241,7 +262,6 @@ private
241
262
  [(x_max - x_min) / 20, (y_max - y_min) / 20]
242
263
  end
243
264
 
244
- DPI = 72
245
265
  def size_of_svg(ir)
246
266
  attributes = {}
247
267
  ir.data[SVG_R, 1].scan(/(\S+)=(?:'([^']*)'|"([^"]*)"|([^'"\s]*))/) do |name, v0, v1, v2|
@@ -268,4 +288,17 @@ private
268
288
  ir[6, 2].unpack('CC').map{ |v| v.zero? ? 256 : v }
269
289
  end
270
290
  alias_method :size_of_cur, :size_of_ico
291
+
292
+ def size_of_webp(ir)
293
+ case ir[12, 4]
294
+ when 'VP8 '
295
+ ir[26, 4].unpack('vv').map{ |v| v & 0x3fff }
296
+ when 'VP8L'
297
+ n = ir[21, 4].unpack('V')[0]
298
+ [(n & 0x3fff) + 1, (n >> 14 & 0x3fff) + 1]
299
+ when 'VP8X'
300
+ w16, w8, h16, h8 = ir[24, 6].unpack('vCvC')
301
+ [(w16 | w8 << 16) + 1, (h16 | h8 << 16) + 1]
302
+ end
303
+ end
271
304
  end
@@ -1,70 +1,51 @@
1
- $:.unshift File.join(File.dirname(__FILE__), '..', 'lib')
2
1
  require 'rspec'
3
2
  require 'image_size'
4
3
 
5
4
  describe ImageSize do
6
- [
7
- ['test2.bmp', :bmp, 42, 50],
8
- ['test3b.bmp',:bmp, 42, 50],
9
- ['test3t.bmp',:bmp, 42, 50],
10
- ['test.gif', :gif, 668, 481],
11
- ['test.jpg', :jpeg, 320, 240],
12
- ['test2.jpg',:jpeg, 320, 240],
13
- ['test.pbm', :pbm, 85, 55],
14
- ['test.pcx', :pcx, 70, 60],
15
- ['test.pgm', :pgm, 90, 55],
16
- ['test.png', :png, 640, 532],
17
- ['test.psd', :psd, 16, 20],
18
- ['test.swf', :swf, 450, 200],
19
- ['test.tif', :tiff, 48, 64],
20
- ['test.xbm', :xbm, 16, 32],
21
- ['test.xpm', :xpm, 24, 32],
22
- ['test.svg', :svg, 72, 100],
23
- ['test.ico', :ico, 256, 27],
24
- ['test.cur', :cur, 50, 256],
25
- ['image_size_spec.rb', nil, nil, nil],
26
- ].each do |name, format, width, height|
27
- path = File.join(File.dirname(__FILE__), name)
28
- file_data = File.open(path, 'rb', &:read)
5
+ (Dir['spec/images/*/*.*'] + [__FILE__]).each do |path|
6
+ name = File.basename(path)
7
+ match = /(\d+)x(\d+)\.([^.]+)$/.match(name)
8
+ width, height, format = match[1].to_i, match[2].to_i, match[3].to_sym if match
9
+ data = File.open(path, 'rb', &:read)
29
10
 
30
- it "should get format and dimensions for #{name} given IO" do
11
+ it "gets format and dimensions of #{name} given as IO" do
31
12
  File.open(path, 'rb') do |fh|
32
13
  is = ImageSize.new(fh)
33
14
  expect([is.format, is.width, is.height]).to eq([format, width, height])
34
15
  expect(fh).not_to be_closed
35
16
  fh.rewind
36
- expect(fh.read).to eq(file_data)
17
+ expect(fh.read).to eq(data)
37
18
  end
38
19
  end
39
20
 
40
- it "should get format and dimensions for #{name} given StringIO" do
41
- io = StringIO.new(file_data)
21
+ it "gets format and dimensions of #{name} given as StringIO" do
22
+ io = StringIO.new(data)
42
23
  is = ImageSize.new(io)
43
24
  expect([is.format, is.width, is.height]).to eq([format, width, height])
44
25
  expect(io).not_to be_closed
45
26
  io.rewind
46
- expect(io.read).to eq(file_data)
27
+ expect(io.read).to eq(data)
47
28
  end
48
29
 
49
- it "should get format and dimensions for #{name} given file data" do
50
- is = ImageSize.new(file_data)
30
+ it "gets format and dimensions of #{name} given as data" do
31
+ is = ImageSize.new(data)
51
32
  expect([is.format, is.width, is.height]).to eq([format, width, height])
52
33
  end
53
34
 
54
- it "should get format and dimensions for #{name} given Tempfile" do
35
+ it "gets format and dimensions of #{name} given as Tempfile" do
55
36
  Tempfile.open(name) do |tf|
56
37
  tf.binmode
57
- tf.write(file_data)
38
+ tf.write(data)
58
39
  tf.rewind
59
40
  is = ImageSize.new(tf)
60
41
  expect([is.format, is.width, is.height]).to eq([format, width, height])
61
42
  expect(tf).not_to be_closed
62
43
  tf.rewind
63
- expect(tf.read).to eq(file_data)
44
+ expect(tf.read).to eq(data)
64
45
  end
65
46
  end
66
47
 
67
- it "should get format and dimensions for #{name} given IO when run twice" do
48
+ it "gets format and dimensions of #{name} given as IO when run twice" do
68
49
  File.open(path, 'rb') do |fh|
69
50
  is = ImageSize.new(fh)
70
51
  expect([is.format, is.width, is.height]).to eq([format, width, height])
@@ -73,13 +54,13 @@ describe ImageSize do
73
54
  end
74
55
  end
75
56
 
76
- it "should get format and dimensions for #{name} as path" do
57
+ it "gets format and dimensions of #{name} as path" do
77
58
  is = ImageSize.path(path)
78
59
  expect([is.format, is.width, is.height]).to eq([format, width, height])
79
60
  end
80
61
  end
81
62
 
82
- it "should raise ArgumentError if argument is not valid" do
63
+ it "raises ArgumentError if argument is not valid" do
83
64
  expect {
84
65
  ImageSize.new(Object)
85
66
  }.to raise_error(ArgumentError)
@@ -89,7 +70,7 @@ describe ImageSize do
89
70
  :png => "\211PNG\r\n\032\n",
90
71
  :jpeg => "\377\330",
91
72
  }.each do |type, data|
92
- it "should raise FormatError if invalid #{type} given" do
73
+ it "raises FormatError if invalid #{type} given" do
93
74
  expect {
94
75
  ImageSize.new(data)
95
76
  }.to raise_error(ImageSize::FormatError)
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: image_size
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.4.2
4
+ version: 1.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Keisuke Minami
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2016-02-18 00:00:00.000000000 Z
12
+ date: 2016-11-20 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rspec
@@ -25,8 +25,8 @@ dependencies:
25
25
  - - ~>
26
26
  - !ruby/object:Gem::Version
27
27
  version: '3.0'
28
- description: ! 'Measure following file dimensions: bmp, cur, gif, jpeg, ico, pbm,
29
- pcx, pgm, png, ppm, psd, swf, tiff, xbm, xpm'
28
+ description: ! 'Measure following file dimensions: apng, bmp, cur, gif, jpeg, ico,
29
+ mng, pbm, pcx, pgm, png, ppm, psd, swf, tiff, xbm, xpm, webp'
30
30
  email:
31
31
  executables: []
32
32
  extensions: []
@@ -39,24 +39,29 @@ files:
39
39
  - image_size.gemspec
40
40
  - lib/image_size.rb
41
41
  - spec/image_size_spec.rb
42
- - spec/test.cur
43
- - spec/test.gif
44
- - spec/test.ico
45
- - spec/test.jpg
46
- - spec/test.pbm
47
- - spec/test.pcx
48
- - spec/test.pgm
49
- - spec/test.png
50
- - spec/test.psd
51
- - spec/test.svg
52
- - spec/test.swf
53
- - spec/test.tif
54
- - spec/test.xbm
55
- - spec/test.xpm
56
- - spec/test2.bmp
57
- - spec/test2.jpg
58
- - spec/test3b.bmp
59
- - spec/test3t.bmp
42
+ - spec/images/apng/192x110.apng
43
+ - spec/images/bmp/v2.42x50.bmp
44
+ - spec/images/bmp/v3-bottom2top.42x50.bmp
45
+ - spec/images/bmp/v3-top2bottom.42x50.bmp
46
+ - spec/images/cur/50x256.cur
47
+ - spec/images/gif/668x481.gif
48
+ - spec/images/ico/256x27.ico
49
+ - spec/images/jpeg/320x240.jpeg
50
+ - spec/images/jpeg/extraneous-bytes.320x240.jpeg
51
+ - spec/images/mng/612x132.mng
52
+ - spec/images/pbm/85x55.pbm
53
+ - spec/images/pcx/70x60.pcx
54
+ - spec/images/pgm/90x55.pgm
55
+ - spec/images/png/640x532.png
56
+ - spec/images/psd/16x20.psd
57
+ - spec/images/svg/72x100.svg
58
+ - spec/images/swf/450x200.swf
59
+ - spec/images/tiff/48x64.tiff
60
+ - spec/images/webp/extended.16x32.webp
61
+ - spec/images/webp/lossless.16x32.webp
62
+ - spec/images/webp/lossy.16x32.webp
63
+ - spec/images/xbm/16x32.xbm
64
+ - spec/images/xpm/24x32.xpm
60
65
  homepage: http://github.com/toy/image_size
61
66
  licenses:
62
67
  - Ruby
@@ -77,28 +82,33 @@ required_rubygems_version: !ruby/object:Gem::Requirement
77
82
  version: '0'
78
83
  requirements: []
79
84
  rubyforge_project: image_size
80
- rubygems_version: 2.4.7
85
+ rubygems_version: 2.6.4
81
86
  signing_key:
82
87
  specification_version: 4
83
88
  summary: Measure image size using pure Ruby
84
89
  test_files:
85
90
  - spec/image_size_spec.rb
86
- - spec/test.cur
87
- - spec/test.gif
88
- - spec/test.ico
89
- - spec/test.jpg
90
- - spec/test.pbm
91
- - spec/test.pcx
92
- - spec/test.pgm
93
- - spec/test.png
94
- - spec/test.psd
95
- - spec/test.svg
96
- - spec/test.swf
97
- - spec/test.tif
98
- - spec/test.xbm
99
- - spec/test.xpm
100
- - spec/test2.bmp
101
- - spec/test2.jpg
102
- - spec/test3b.bmp
103
- - spec/test3t.bmp
91
+ - spec/images/apng/192x110.apng
92
+ - spec/images/bmp/v2.42x50.bmp
93
+ - spec/images/bmp/v3-bottom2top.42x50.bmp
94
+ - spec/images/bmp/v3-top2bottom.42x50.bmp
95
+ - spec/images/cur/50x256.cur
96
+ - spec/images/gif/668x481.gif
97
+ - spec/images/ico/256x27.ico
98
+ - spec/images/jpeg/320x240.jpeg
99
+ - spec/images/jpeg/extraneous-bytes.320x240.jpeg
100
+ - spec/images/mng/612x132.mng
101
+ - spec/images/pbm/85x55.pbm
102
+ - spec/images/pcx/70x60.pcx
103
+ - spec/images/pgm/90x55.pgm
104
+ - spec/images/png/640x532.png
105
+ - spec/images/psd/16x20.psd
106
+ - spec/images/svg/72x100.svg
107
+ - spec/images/swf/450x200.swf
108
+ - spec/images/tiff/48x64.tiff
109
+ - spec/images/webp/extended.16x32.webp
110
+ - spec/images/webp/lossless.16x32.webp
111
+ - spec/images/webp/lossy.16x32.webp
112
+ - spec/images/xbm/16x32.xbm
113
+ - spec/images/xpm/24x32.xpm
104
114
  has_rdoc: