newer_image_size 1.1.1

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.
@@ -0,0 +1,12 @@
1
+ /pkg/
2
+ /*.gem
3
+
4
+ /doc/
5
+ /rdoc/
6
+ /.yardoc/
7
+ /coverage/
8
+
9
+ Makefile
10
+ *.o
11
+ *.bundle
12
+ /tmp/
@@ -0,0 +1 @@
1
+ /spec/test.*
@@ -0,0 +1,43 @@
1
+ # image_size
2
+
3
+ measure image size using pure Ruby
4
+ formats: `bmp`, `gif`, `jpeg`, `pbm`, `pcx`, `pgm`, `png`, `ppm`, `psd`, `swf`, `tiff`, `xbm`, `xpm`
5
+
6
+ ## Download
7
+
8
+ The latest version of image\_size can be found at http://github.com/toy/image_size
9
+
10
+ ## Installation
11
+
12
+ gem install image_size
13
+
14
+ ## Examples
15
+
16
+ require 'image_size'
17
+
18
+ p NewerImageSize.path('spec/test.jpg').size
19
+
20
+ open('spec/test.jpg', 'rb') do |fh|
21
+ p NewerImageSize.new(fh).size
22
+ end
23
+
24
+
25
+ require 'image_size'
26
+ require 'open-uri'
27
+
28
+ open('http://www.rubycgi.org/image/ruby_gtk_book_title.jpg', 'rb') do |fh|
29
+ p NewerImageSize.new(fh).size
30
+ end
31
+
32
+ open('http://www.rubycgi.org/image/ruby_gtk_book_title.jpg', 'rb') do |fh|
33
+ data = fh.read
34
+ p NewerImageSize.new(data).size
35
+ end
36
+
37
+ ## Licence
38
+
39
+ This code is free to use under the terms of the Ruby's licence.
40
+
41
+ ## Contact
42
+
43
+ Original author: "Keisuke Minami": mailto:keisuke@rccn.com
@@ -0,0 +1,204 @@
1
+ require 'stringio'
2
+ require 'tempfile'
3
+
4
+ class NewerImageSize
5
+ class Size < Array
6
+ # join using 'x'
7
+ def to_s
8
+ join('x')
9
+ end
10
+ end
11
+
12
+ class ImageReader # :nodoc:
13
+ def initialize(data_or_io)
14
+ @io = case data_or_io
15
+ when IO, StringIO, Tempfile
16
+ data_or_io.dup.tap(&:rewind)
17
+ when String
18
+ StringIO.new(data_or_io)
19
+ else
20
+ raise ArgumentError.new("expected instance of IO, StringIO, Tempfile or String, got #{data_or_io.class}")
21
+ end
22
+ @read = 0
23
+ @data = ''
24
+ end
25
+
26
+ def rewind
27
+ @io.rewind
28
+ end
29
+
30
+ CHUNK = 1024
31
+ def [](offset, length)
32
+ while offset + length > @read
33
+ @read += CHUNK
34
+ if data = @io.read(CHUNK)
35
+ @data << data
36
+ end
37
+ end
38
+ @data[offset, length]
39
+ end
40
+ end
41
+
42
+ # Given path to image finds its format, width and height
43
+ def self.path(path)
44
+ open(path, 'rb'){ |f| new(f) }
45
+ end
46
+
47
+ # Given image as IO, StringIO, Tempfile or String finds its format and dimensions
48
+ def initialize(data)
49
+ ir = ImageReader.new(data)
50
+ if @format = detect_format(ir)
51
+ @width, @height = self.send("size_of_#{@format}", ir)
52
+ end
53
+ ir.rewind
54
+ end
55
+
56
+ # Image format
57
+ attr_reader :format
58
+
59
+ # Image width
60
+ attr_reader :width
61
+ alias :w :width
62
+
63
+ # Image height
64
+ attr_reader :height
65
+ alias :h :height
66
+
67
+ # get image width and height as an array which to_s method returns "#{width}x#{height}"
68
+ def size
69
+ if format
70
+ Size.new([width, height])
71
+ end
72
+ end
73
+
74
+ private
75
+
76
+ def detect_format(ir)
77
+ head = ir[0, 1024]
78
+ case
79
+ when head =~ /^GIF8[7,9]a/ then :gif
80
+ when head[0, 8] == "\211PNG\r\n\032\n" then :png
81
+ when head[0, 2] == "\377\330" then :jpeg
82
+ when head[0, 2] == 'BM' then :bmp
83
+ when head =~ /^P[1-7]/ then :ppm
84
+ when head =~ /\#define\s+\S+\s+\d+/ then :xbm
85
+ when head[0, 4] == "II*\000" then :tiff
86
+ when head[0, 4] == "MM\000*" then :tiff
87
+ when head =~ /\/\* XPM \*\// then :xpm
88
+ when head[0, 4] == '8BPS' then :psd
89
+ when head =~ /^[FC]WS/ then :swf
90
+ when head[0, 1] == "\n" then :pcx
91
+ end
92
+ end
93
+
94
+ def size_of_gif(ir)
95
+ ir[6, 4].unpack('vv')
96
+ end
97
+
98
+ def size_of_png(ir)
99
+ unless ir[12, 4] == 'IHDR'
100
+ raise 'IHDR not in place for PNG'
101
+ end
102
+ ir[16, 8].unpack('NN')
103
+ end
104
+
105
+ JpegCodeCheck = [
106
+ "\xc0", "\xc1", "\xc2", "\xc3",
107
+ "\xc5", "\xc6", "\xc7",
108
+ "\xc9", "\xca", "\xcb",
109
+ "\xcd", "\xce", "\xcf",
110
+ ] # :nodoc:
111
+ def size_of_jpeg(ir)
112
+ section_marker = "\xFF"
113
+ offset = 2
114
+ loop do
115
+ marker, code, length = ir[offset, 4].unpack('aan')
116
+ offset += 4
117
+ raise 'JPEG marker not found' if marker != section_marker
118
+
119
+ if JpegCodeCheck.include?(code)
120
+ return ir[offset + 1, 4].unpack('nn').reverse
121
+ end
122
+ offset += length - 2
123
+ end
124
+ end
125
+
126
+ def size_of_bmp(ir)
127
+ ir[18, 8].unpack('VV')
128
+ end
129
+
130
+ def size_of_ppm(ir)
131
+ header = ir[0, 1024]
132
+ header.gsub!(/^\#[^\n\r]*/m, '')
133
+ header =~ /^(P[1-6])\s+?(\d+)\s+?(\d+)/m
134
+ case $1
135
+ when 'P1', 'P4' then @format = :pbm
136
+ when 'P2', 'P5' then @format = :pgm
137
+ end
138
+ [$2.to_i, $3.to_i]
139
+ end
140
+
141
+ def size_of_xbm(ir)
142
+ ir[0, 1024] =~ /^\#define\s*\S*\s*(\d+)\s*\n\#define\s*\S*\s*(\d+)/mi
143
+ [$1.to_i, $2.to_i]
144
+ end
145
+
146
+ def size_of_xpm(ir)
147
+ length = 1024
148
+ until (data = ir[0, length]) =~ /"\s*(\d+)\s+(\d+)(\s+\d+\s+\d+){1,2}\s*"/m
149
+ if data.length != length
150
+ raise 'XPM size not found'
151
+ end
152
+ length += 1024
153
+ end
154
+ [$1.to_i, $2.to_i]
155
+ end
156
+
157
+ def size_of_psd(ir)
158
+ ir[14, 8].unpack('NN')
159
+ end
160
+
161
+ def size_of_tiff(ir)
162
+ endian2b = (ir[0, 4] == "II*\000") ? 'v' : 'n'
163
+ endian4b = endian2b.upcase
164
+ packspec = [nil, 'C', nil, endian2b, endian4b, nil, 'c', nil, endian2b, endian4b]
165
+
166
+ offset = ir[4, 4].unpack(endian4b)[0]
167
+ num_dirent = ir[offset, 2].unpack(endian2b)[0]
168
+ offset += 2
169
+ num_dirent = offset + (num_dirent * 12)
170
+
171
+ width = height = nil
172
+ until width && height
173
+ ifd = ir[offset, 12]
174
+ raise 'Reached end of directory entries in TIFF' if ifd.nil? || offset > num_dirent
175
+ tag, type = ifd.unpack(endian2b * 2)
176
+ offset += 12
177
+
178
+ unless packspec[type].nil?
179
+ value = ifd[8, 4].unpack(packspec[type])[0]
180
+ case tag
181
+ when 0x0100
182
+ width = value
183
+ when 0x0101
184
+ height = value
185
+ end
186
+ end
187
+ end
188
+ [width, height]
189
+ end
190
+
191
+ def size_of_pcx(ir)
192
+ parts = ir[4, 8].unpack('S4')
193
+ [parts[2] - parts[0] + 1, parts[3] - parts[1] + 1]
194
+ end
195
+
196
+ def size_of_swf(ir)
197
+ value_bit_length = ir[8, 1].unpack('B5').first.to_i(2)
198
+ bit_length = 5 + value_bit_length * 4
199
+ rect_bits = ir[8, bit_length / 8 + 1].unpack("B#{bit_length}").first
200
+ values = rect_bits.unpack('@5' + "a#{value_bit_length}" * 4).map{ |bits| bits.to_i(2) }
201
+ x_min, x_max, y_min, y_max = values
202
+ [(x_max - x_min) / 20, (y_max - y_min) / 20]
203
+ end
204
+ end
@@ -0,0 +1,20 @@
1
+ # encoding: UTF-8
2
+
3
+ Gem::Specification.new do |s|
4
+ s.name = 'newer_image_size'
5
+ s.version = '1.1.1'
6
+ s.summary = %q{Measure image size using pure Ruby}
7
+ s.description = %q{Measure following file dimensions: bmp, gif, jpeg, pbm, pcx, pgm, png, ppm, psd, swf, tiff, xbm, xpm}
8
+ s.homepage = "http://github.com/gzohari/#{s.name}"
9
+ s.authors = ['Keisuke Minami', 'Ivan Kuchin']
10
+ s.license = 'MIT'
11
+
12
+ s.rubyforge_project = s.name
13
+
14
+ s.files = `git ls-files`.split("\n")
15
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
16
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
17
+ s.require_paths = %w[lib]
18
+
19
+ s.add_development_dependency 'rspec'
20
+ end
@@ -0,0 +1,74 @@
1
+ $:.unshift File.join(File.dirname(__FILE__), '..', 'lib')
2
+ require 'rspec'
3
+ require 'newer_image_size'
4
+
5
+ describe NewerImageSize do
6
+ [
7
+ ['test.bmp', :bmp, 50, 50],
8
+ ['test.gif', :gif, 668, 481],
9
+ ['test.jpg', :jpeg, 320, 240],
10
+ ['test.pbm', :pbm, 85, 55],
11
+ ['test.pcx', :pcx, 70, 60],
12
+ ['test.pgm', :pgm, 90, 55],
13
+ ['test.png', :png, 640, 532],
14
+ ['test.psd', :psd, 20, 20],
15
+ ['test.swf', :swf, 450, 200],
16
+ ['test.tif', :tiff, 64, 64],
17
+ ['test.xbm', :xbm, 16, 16],
18
+ ['test.xpm', :xpm, 32, 32],
19
+ ['newer_image_size_spec.rb', nil, nil, nil],
20
+ ].each do |name, format, width, height|
21
+ path = File.join(File.dirname(__FILE__), name)
22
+
23
+ it "should get format and dimensions for #{name} given IO" do
24
+ File.open(path, 'rb') do |fh|
25
+ is = NewerImageSize.new(fh)
26
+ [is.format, is.width, is.height].should == [format, width, height]
27
+ end
28
+ end
29
+
30
+ it "should get format and dimensions for #{name} given StringIO" do
31
+ File.open(path, 'rb') do |fh|
32
+ is = NewerImageSize.new(StringIO.new(fh.read))
33
+ [is.format, is.width, is.height].should == [format, width, height]
34
+ end
35
+ end
36
+
37
+ it "should get format and dimensions for #{name} given file data" do
38
+ File.open(path, 'rb') do |fh|
39
+ is = NewerImageSize.new(fh.read)
40
+ [is.format, is.width, is.height].should == [format, width, height]
41
+ end
42
+ end
43
+
44
+ it "should get format and dimensions for #{name} given Tempfile" do
45
+ file_data = File.open(path, 'rb') { |fh| fh.read }
46
+ Tempfile.open(name) do |tf|
47
+ tf.write(file_data)
48
+ tf.rewind
49
+ is = NewerImageSize.new(tf)
50
+ [is.format, is.width, is.height].should == [format, width, height]
51
+ end
52
+ end
53
+
54
+ it "should get format and dimensions for #{name} given IO when run twice" do
55
+ File.open(path, 'rb') do |fh|
56
+ is = NewerImageSize.new(fh)
57
+ [is.format, is.width, is.height].should == [format, width, height]
58
+ is = NewerImageSize.new(fh)
59
+ [is.format, is.width, is.height].should == [format, width, height]
60
+ end
61
+ end
62
+
63
+ it "should get format and dimensions for #{name} as path" do
64
+ is = NewerImageSize.path(path)
65
+ [is.format, is.width, is.height].should == [format, width, height]
66
+ end
67
+ end
68
+
69
+ it "should raise ArgumentError if argument is not valid" do
70
+ lambda {
71
+ NewerImageSize.new(Object)
72
+ }.should raise_error(ArgumentError)
73
+ end
74
+ end
Binary file
Binary file
Binary file
Binary file
Binary file
@@ -0,0 +1,5 @@
1
+ P5
2
+ #Created with The GIMP
3
+ 90 55
4
+ 255
5
+ ������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������
Binary file
Binary file
Binary file
Binary file
@@ -0,0 +1,6 @@
1
+ #define cursor_width 16
2
+ #define cursor_height 16
3
+ static unsigned char cursor_bits[] = {
4
+ 0x00, 0x00, 0x00, 0x00, 0x80, 0x00, 0x84, 0x10, 0xe8, 0x0b, 0x90, 0x04,
5
+ 0xa8, 0x0a, 0x88, 0x08, 0xfe, 0x3f, 0x88, 0x08, 0xa8, 0x0a, 0x90, 0x04,
6
+ 0xe8, 0x0b, 0x84, 0x10, 0x80, 0x00, 0x00, 0x00};
@@ -0,0 +1,38 @@
1
+ /* XPM */
2
+ static char * sample2_xpm[] = {
3
+ "32 32 3 1",
4
+ " c None",
5
+ ". c #FFFFFF",
6
+ "+ c #FF0000",
7
+ "................................",
8
+ "................................",
9
+ "................................",
10
+ "................................",
11
+ "................................",
12
+ "................................",
13
+ "......++..................++....",
14
+ "......+++................+++....",
15
+ ".......+++..............+++.....",
16
+ "........+++............+++......",
17
+ ".........+++..........+++.......",
18
+ "..........+++........+++........",
19
+ "...........+++......+++.........",
20
+ "............+++....+++..........",
21
+ ".............+++..+++...........",
22
+ "..............++++++............",
23
+ "...............++++.............",
24
+ "................+++.............",
25
+ "...............+++++............",
26
+ "..............+++.+++...........",
27
+ ".............+++...+++..........",
28
+ "............+++.....+++.........",
29
+ "...........+++.......+++........",
30
+ "..........+++.........+++.......",
31
+ ".........+++...........+++......",
32
+ "........+++.............+++.....",
33
+ ".......+++...............+++....",
34
+ "......+++.................+++...",
35
+ "......++...................++...",
36
+ "................................",
37
+ "................................",
38
+ "................................"};
metadata ADDED
@@ -0,0 +1,94 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: newer_image_size
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.1.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Keisuke Minami
9
+ - Ivan Kuchin
10
+ autorequire:
11
+ bindir: bin
12
+ cert_chain: []
13
+ date: 2012-08-08 00:00:00.000000000 Z
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: rspec
17
+ requirement: !ruby/object:Gem::Requirement
18
+ none: false
19
+ requirements:
20
+ - - ! '>='
21
+ - !ruby/object:Gem::Version
22
+ version: '0'
23
+ type: :development
24
+ prerelease: false
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ none: false
27
+ requirements:
28
+ - - ! '>='
29
+ - !ruby/object:Gem::Version
30
+ version: '0'
31
+ description: ! 'Measure following file dimensions: bmp, gif, jpeg, pbm, pcx, pgm,
32
+ png, ppm, psd, swf, tiff, xbm, xpm'
33
+ email:
34
+ executables: []
35
+ extensions: []
36
+ extra_rdoc_files: []
37
+ files:
38
+ - .gitignore
39
+ - .tmignore
40
+ - README.markdown
41
+ - lib/newer_image_size.rb
42
+ - newer_image_size.gemspec
43
+ - spec/newer_image_size_spec.rb
44
+ - spec/test.bmp
45
+ - spec/test.gif
46
+ - spec/test.jpg
47
+ - spec/test.pbm
48
+ - spec/test.pcx
49
+ - spec/test.pgm
50
+ - spec/test.png
51
+ - spec/test.psd
52
+ - spec/test.swf
53
+ - spec/test.tif
54
+ - spec/test.xbm
55
+ - spec/test.xpm
56
+ homepage: http://github.com/gzohari/newer_image_size
57
+ licenses:
58
+ - MIT
59
+ post_install_message:
60
+ rdoc_options: []
61
+ require_paths:
62
+ - lib
63
+ required_ruby_version: !ruby/object:Gem::Requirement
64
+ none: false
65
+ requirements:
66
+ - - ! '>='
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ required_rubygems_version: !ruby/object:Gem::Requirement
70
+ none: false
71
+ requirements:
72
+ - - ! '>='
73
+ - !ruby/object:Gem::Version
74
+ version: '0'
75
+ requirements: []
76
+ rubyforge_project: newer_image_size
77
+ rubygems_version: 1.8.24
78
+ signing_key:
79
+ specification_version: 3
80
+ summary: Measure image size using pure Ruby
81
+ test_files:
82
+ - spec/newer_image_size_spec.rb
83
+ - spec/test.bmp
84
+ - spec/test.gif
85
+ - spec/test.jpg
86
+ - spec/test.pbm
87
+ - spec/test.pcx
88
+ - spec/test.pgm
89
+ - spec/test.png
90
+ - spec/test.psd
91
+ - spec/test.swf
92
+ - spec/test.tif
93
+ - spec/test.xbm
94
+ - spec/test.xpm