image_size 1.0.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.
@@ -0,0 +1,5 @@
1
+ .DS_Store
2
+ pkg
3
+ doc
4
+ *.gem
5
+ *.swp
@@ -0,0 +1,31 @@
1
+ = image_size -- measure image size(GIF, PNG, JPEG ,,, etc)
2
+
3
+ measure image size using pure Ruby
4
+ ["PCX", "PSD", "XPM", "TIFF", "XBM", "PGM", "PBM", "PPM", "BMP", "JPEG", "PNG", "GIF", "SWF"]
5
+
6
+ == Download
7
+
8
+ The latest version of image_size can be found at
9
+
10
+ * http://github.com/toy/image_size
11
+
12
+ == Installation
13
+
14
+ gem install image_size
15
+
16
+ == Simple Example
17
+
18
+ ruby "rubygems" # you use rubygems
19
+ ruby "image_size"
20
+ ruby "open-uri"
21
+ open("http://www.rubycgi.org/image/ruby_gtk_book_title.jpg", "rb") do |fh|
22
+ p ImageSize.new(fh.read).size
23
+ end
24
+
25
+ == Licence
26
+
27
+ This code is free to use under the terms of the Ruby's licence.
28
+
29
+ == Contact
30
+
31
+ Comments are welcome. Send an email to "Keisuke Minami":mailto:keisuke@rccn.com
@@ -0,0 +1,39 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+
4
+ name = 'image_size'
5
+
6
+ begin
7
+ require 'jeweler'
8
+
9
+ Jeweler::Tasks.new do |gem|
10
+ gem.name = name
11
+ gem.summary = %Q{measure image size using pure Ruby}
12
+ gem.description = %Q{measure following file dimensions: PCX, PSD, XPM, TIFF, XBM, PGM, PBM, PPM, BMP, JPEG, PNG, GIF, SWF}
13
+ gem.homepage = "http://github.com/toy/#{name}"
14
+ gem.authors = ['Keisuke Minami', 'Boba Fat']
15
+ end
16
+ Jeweler::GemcutterTasks.new
17
+ rescue LoadError
18
+ puts 'Jeweler (or a dependency) not available. Install it with: gem install jeweler'
19
+ end
20
+
21
+ require 'rake/testtask'
22
+ Rake::TestTask.new(:test) do |test|
23
+ test.libs << 'lib' << 'test'
24
+ test.pattern = 'test/**/test_*.rb'
25
+ test.verbose = true
26
+ end
27
+
28
+ task :test => :check_dependencies
29
+ task :default => :test
30
+
31
+ require 'rake/rdoctask'
32
+ Rake::RDocTask.new do |rdoc|
33
+ version = File.exist?('VERSION') ? File.read('VERSION') : ''
34
+
35
+ rdoc.rdoc_dir = 'rdoc'
36
+ rdoc.title = "#{name} #{version}"
37
+ rdoc.rdoc_files.include('README*')
38
+ rdoc.rdoc_files.include('lib/**/*.rb')
39
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 1.0.0
@@ -0,0 +1,59 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{image_size}
8
+ s.version = "1.0.0"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Keisuke Minami", "Boba Fat"]
12
+ s.date = %q{2010-11-01}
13
+ s.description = %q{measure following file dimensions: PCX, PSD, XPM, TIFF, XBM, PGM, PBM, PPM, BMP, JPEG, PNG, GIF, SWF}
14
+ s.extra_rdoc_files = [
15
+ "README.rdoc"
16
+ ]
17
+ s.files = [
18
+ ".gitignore",
19
+ "README.rdoc",
20
+ "Rakefile",
21
+ "VERSION",
22
+ "image_size.gemspec",
23
+ "lib/image_size.rb",
24
+ "test/2-4-7.png",
25
+ "test/4_1_2.gif",
26
+ "test/bmp.bmp",
27
+ "test/cursor.xbm",
28
+ "test/detect.swf",
29
+ "test/pbm.pbm",
30
+ "test/pcx.pcx",
31
+ "test/pgm.pgm",
32
+ "test/test.xpm",
33
+ "test/test_helper.rb",
34
+ "test/test_image_size.rb",
35
+ "test/tiff.tiff",
36
+ "test/tokyo_tower.jpg",
37
+ "test/tower_e.psd"
38
+ ]
39
+ s.homepage = %q{http://github.com/toy/image_size}
40
+ s.rdoc_options = ["--charset=UTF-8"]
41
+ s.require_paths = ["lib"]
42
+ s.rubygems_version = %q{1.3.7}
43
+ s.summary = %q{measure image size using pure Ruby}
44
+ s.test_files = [
45
+ "test/test_helper.rb",
46
+ "test/test_image_size.rb"
47
+ ]
48
+
49
+ if s.respond_to? :specification_version then
50
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
51
+ s.specification_version = 3
52
+
53
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
54
+ else
55
+ end
56
+ else
57
+ end
58
+ end
59
+
@@ -0,0 +1,230 @@
1
+ require 'stringio'
2
+
3
+ class ImageSize
4
+ JpegCodeCheck = [
5
+ "\xc0", "\xc1", "\xc2", "\xc3",
6
+ "\xc5", "\xc6", "\xc7",
7
+ "\xc9", "\xca", "\xcb",
8
+ "\xcd", "\xce", "\xcf",
9
+ ]
10
+
11
+ attr_reader :format, :width, :height
12
+
13
+ # receive image & make size
14
+ # argument is image String, StringIO or IO
15
+ def initialize(data)
16
+ @data = data.dup
17
+ @width = nil
18
+ @height = nil
19
+ @format = nil
20
+
21
+ if @data.is_a?(IO)
22
+ img_top = @data.read(1024)
23
+ img_io = def_read_o(@data)
24
+ elsif @data.is_a?(StringIO)
25
+ img_top = @data.read(1024)
26
+ img_io = def_read_o(@data)
27
+ elsif @data.is_a?(String)
28
+ img_top = @data[0, 1024]
29
+ img_io = StringIO.open(@data)
30
+ img_io = def_read_o(img_io)
31
+ else
32
+ raise "argument class error!! #{data.class}"
33
+ end
34
+
35
+ if @format = check_format(img_top)
36
+ @width, @height = self.send("measure_#{@format}", img_io)
37
+ end
38
+
39
+ if @data.is_a?(String)
40
+ img_io.close
41
+ end
42
+ end
43
+
44
+ # get image width and height as an array which to_s method returns "#{width}x#{height}"
45
+ def size
46
+ if format
47
+ size = [width, height]
48
+ def size.to_s
49
+ join('x')
50
+ end
51
+ size
52
+ end
53
+ end
54
+
55
+ alias :h :height
56
+ alias :w :width
57
+
58
+ private
59
+
60
+ def def_read_o(io)
61
+ io.seek(0, 0)
62
+ # define Singleton-method definition to IO (byte, offset)
63
+ def io.read_o(length = 1, offset = nil)
64
+ self.seek(offset, 0) if offset
65
+ ret = self.read(length)
66
+ raise 'cannot read!!' unless ret
67
+ ret
68
+ end
69
+ io
70
+ end
71
+
72
+ def check_format(img_top)
73
+ case
74
+ when img_top =~ /^GIF8[7,9]a/ then :gif
75
+ when img_top[0, 8] == "\x89PNG\x0d\x0a\x1a\x0a" then :png
76
+ when img_top[0, 2] == "\xFF\xD8" then :jpeg
77
+ when img_top[0, 2] == 'BM' then :bmp
78
+ when img_top =~ /^P[1-7]/ then :ppm
79
+ when img_top =~ /\#define\s+\S+\s+\d+/ then :xbm
80
+ when img_top[0, 4] == "MM\x00\x2a" then :tiff
81
+ when img_top[0, 4] == "II\x2a\x00" then :tiff
82
+ when img_top[0, 4] == "MM\x00\x2a" then :tiff
83
+ when img_top[0, 4] == "II\x2a\x00" then :tiff
84
+ when img_top =~ /\/\* XPM \*\// then :xpm
85
+ when img_top[0, 4] == '8BPS' then :psd
86
+ when img_top[1, 2] == 'WS' then :swf
87
+ when img_top[0] == 10 then :pcx
88
+ end
89
+ end
90
+
91
+ def measure_gif(img_io)
92
+ img_io.read_o(6)
93
+ img_io.read_o(4).unpack('vv')
94
+ end
95
+
96
+ def measure_png(img_io)
97
+ img_io.read_o(12)
98
+ raise 'This file is not PNG.' unless img_io.read_o(4) == 'IHDR'
99
+ img_io.read_o(8).unpack('NN')
100
+ end
101
+
102
+ def measure_jpeg(img_io)
103
+ c_marker = "\xFF" # Section marker.
104
+ img_io.read_o(2)
105
+ loop do
106
+ marker, code, length = img_io.read_o(4).unpack('aan')
107
+ raise 'JPEG marker not found!' if marker != c_marker
108
+
109
+ if JpegCodeCheck.include?(code)
110
+ height, width = img_io.read_o(5).unpack('xnn')
111
+ return [width, height]
112
+ end
113
+ img_io.read_o(length - 2)
114
+ end
115
+ end
116
+
117
+ def measure_bmp(img_io)
118
+ img_io.read_o(26).unpack('x18VV');
119
+ end
120
+
121
+ def measure_ppm(img_io)
122
+ header = img_io.read_o(1024)
123
+ header.gsub!(/^\#[^\n\r]*/m, '')
124
+ header =~ /^(P[1-6])\s+?(\d+)\s+?(\d+)/m
125
+ case $1
126
+ when 'P1', 'P4' then @format = :pbm
127
+ when 'P2', 'P5' then @format = :pgm
128
+ end
129
+
130
+ [$2.to_i, $3.to_i]
131
+ end
132
+
133
+ alias :measure_pgm :measure_ppm
134
+ alias :measure_pbm :measure_ppm
135
+
136
+ def measure_xbm(img_io)
137
+ img_io.read_o(1024) =~ /^\#define\s*\S*\s*(\d+)\s*\n\#define\s*\S*\s*(\d+)/mi
138
+
139
+ [$1.to_i, $2.to_i]
140
+ end
141
+
142
+ def measure_xpm(img_io)
143
+ width = height = nil
144
+ while(line = img_io.read_o(1024))
145
+ if line =~ /"\s*(\d+)\s+(\d+)(\s+\d+\s+\d+){1,2}\s*"/m
146
+ width = $1.to_i; height = $2.to_i
147
+ break
148
+ end
149
+ end
150
+
151
+ [width, height]
152
+ end
153
+
154
+ def measure_psd(img_io)
155
+ img_io.read_o(26).unpack('x14NN')
156
+ end
157
+
158
+ def measure_tiff(img_io)
159
+ endian = (img_io.read_o(4) =~ /II\x2a\x00/o) ? 'v' : 'n' # 'v' little-endian 'n' default to big-endian
160
+
161
+ packspec = [
162
+ nil, # nothing (shouldn't happen)
163
+ 'C', # BYTE (8-bit unsigned integer)
164
+ nil, # ASCII
165
+ endian, # SHORT (16-bit unsigned integer)
166
+ endian.upcase, # LONG (32-bit unsigned integer)
167
+ nil, # RATIONAL
168
+ 'c', # SBYTE (8-bit signed integer)
169
+ nil, # UNDEFINED
170
+ endian, # SSHORT (16-bit unsigned integer)
171
+ endian.upcase, # SLONG (32-bit unsigned integer)
172
+ ]
173
+
174
+ offset = img_io.read_o(4).unpack(endian.upcase)[0] # Get offset to IFD
175
+
176
+ ifd = img_io.read_o(2, offset)
177
+ num_dirent = ifd.unpack(endian)[0] # Make it useful
178
+ offset += 2
179
+ num_dirent = offset + (num_dirent * 12); # Calc. maximum offset of IFD
180
+
181
+ ifd = width = height = nil
182
+ while(width.nil? || height.nil?)
183
+ ifd = img_io.read_o(12, offset) # Get first directory entry
184
+ break if (ifd.nil? || (offset > num_dirent))
185
+ offset += 12
186
+ tag = ifd.unpack(endian)[0] # ...and decode its tag
187
+ type = ifd[2, 2].unpack(endian)[0] # ...and the data type
188
+
189
+ # Check the type for sanity.
190
+ next if (type > packspec.size + 0) || (packspec[type].nil?)
191
+ if tag == 0x0100 # Decode the value
192
+ width = ifd[8, 4].unpack(packspec[type])[0]
193
+ elsif tag == 0x0101 # Decode the value
194
+ height = ifd[8, 4].unpack(packspec[type])[0]
195
+ end
196
+ end
197
+
198
+ [width, height]
199
+ end
200
+
201
+ def measure_pcx(img_io)
202
+ header = img_io.read_o(128)
203
+ head_part = header.unpack('C4S4')
204
+
205
+ [head_part[6] - head_part[4] + 1, head_part[7] - head_part[5] + 1]
206
+ end
207
+
208
+ def measure_swf(img_io)
209
+ header = img_io.read_o(9)
210
+
211
+ sig1 = header[0, 1]
212
+ sig2 = header[1, 1]
213
+ sig3 = header[2, 1]
214
+
215
+ unless ((sig1 == 'F' || sig1 == 'C') && sig2 == 'W' && sig3 == 'S')
216
+ raise 'This file is not SWF.'
217
+ end
218
+
219
+ bit_length = Integer("0b#{header.unpack('@8B5')}")
220
+ header << img_io.read_o(bit_length * 4 / 8 + 1)
221
+ str = header.unpack("@8B#{5 + bit_length * 4}")[0]
222
+ last = 5
223
+ x_min = Integer("0b#{str[last, bit_length]}")
224
+ x_max = Integer("0b#{str[(last += bit_length), bit_length]}")
225
+ y_min = Integer("0b#{str[(last += bit_length), bit_length]}")
226
+ y_max = Integer("0b#{str[(last += bit_length), bit_length]}")
227
+
228
+ [(x_max - x_min) / 20, (y_max - y_min) / 20]
229
+ end
230
+ end
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};
Binary file
Binary file
Binary file
@@ -0,0 +1,5 @@
1
+ P5
2
+ #Created with The GIMP
3
+ 90 55
4
+ 255
5
+ ������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������
@@ -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
+ "................................"};
@@ -0,0 +1,2 @@
1
+ require 'test/unit'
2
+ require File.dirname(__FILE__) + '/../lib/image_size'
@@ -0,0 +1,50 @@
1
+ require File.dirname(__FILE__) + '/test_helper.rb'
2
+
3
+ class TestImageSize < Test::Unit::TestCase
4
+
5
+ def setup
6
+ @dir = File.dirname(__FILE__)
7
+ @data = [
8
+ ['4_1_2.gif', :gif, 668, 481],
9
+ ['2-4-7.png', :png, 640, 532],
10
+ ['tokyo_tower.jpg', :jpeg, 320, 240],
11
+ ['bmp.bmp', :bmp, 50, 50],
12
+ ['pgm.pgm', :pgm, 90, 55],
13
+ ['pbm.pbm', :pbm, 85, 55],
14
+ ['cursor.xbm', :xbm, 16, 16],
15
+ ['tiff.tiff', :tiff, 64, 64],
16
+ ['test.xpm', :xpm, 32, 32],
17
+ ['tower_e.psd', :psd, 20, 20],
18
+ ['pcx.pcx', :pcx, 70, 60],
19
+ ['detect.swf', :swf, 450, 200],
20
+ ['test_helper.rb', nil, nil, nil],
21
+ ]
22
+ end
23
+
24
+ def teardown
25
+ end
26
+
27
+ def test_string
28
+ @data.each do |file_name, format, widht, height|
29
+ open(File.join(@dir, file_name), 'rb') do |fh|
30
+ img_data = fh.read
31
+
32
+ img = ImageSize.new(img_data)
33
+ assert_equal(format, img.format)
34
+ assert_equal(widht, img.width)
35
+ assert_equal(height, img.height)
36
+ end
37
+ end
38
+ end
39
+
40
+ def test_io
41
+ @data.each do |file_name, format, widht, height|
42
+ open(File.join(@dir, file_name), 'rb') do |fh|
43
+ img = ImageSize.new(fh)
44
+ assert_equal(format, img.format)
45
+ assert_equal(widht, img.width)
46
+ assert_equal(height, img.height)
47
+ end
48
+ end
49
+ end
50
+ end
Binary file
Binary file
Binary file
metadata ADDED
@@ -0,0 +1,88 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: image_size
3
+ version: !ruby/object:Gem::Version
4
+ hash: 23
5
+ prerelease: false
6
+ segments:
7
+ - 1
8
+ - 0
9
+ - 0
10
+ version: 1.0.0
11
+ platform: ruby
12
+ authors:
13
+ - Keisuke Minami
14
+ - Boba Fat
15
+ autorequire:
16
+ bindir: bin
17
+ cert_chain: []
18
+
19
+ date: 2010-11-01 00:00:00 +03:00
20
+ default_executable:
21
+ dependencies: []
22
+
23
+ description: "measure following file dimensions: PCX, PSD, XPM, TIFF, XBM, PGM, PBM, PPM, BMP, JPEG, PNG, GIF, SWF"
24
+ email:
25
+ executables: []
26
+
27
+ extensions: []
28
+
29
+ extra_rdoc_files:
30
+ - README.rdoc
31
+ files:
32
+ - .gitignore
33
+ - README.rdoc
34
+ - Rakefile
35
+ - VERSION
36
+ - image_size.gemspec
37
+ - lib/image_size.rb
38
+ - test/2-4-7.png
39
+ - test/4_1_2.gif
40
+ - test/bmp.bmp
41
+ - test/cursor.xbm
42
+ - test/detect.swf
43
+ - test/pbm.pbm
44
+ - test/pcx.pcx
45
+ - test/pgm.pgm
46
+ - test/test.xpm
47
+ - test/test_helper.rb
48
+ - test/test_image_size.rb
49
+ - test/tiff.tiff
50
+ - test/tokyo_tower.jpg
51
+ - test/tower_e.psd
52
+ has_rdoc: true
53
+ homepage: http://github.com/toy/image_size
54
+ licenses: []
55
+
56
+ post_install_message:
57
+ rdoc_options:
58
+ - --charset=UTF-8
59
+ require_paths:
60
+ - lib
61
+ required_ruby_version: !ruby/object:Gem::Requirement
62
+ none: false
63
+ requirements:
64
+ - - ">="
65
+ - !ruby/object:Gem::Version
66
+ hash: 3
67
+ segments:
68
+ - 0
69
+ version: "0"
70
+ required_rubygems_version: !ruby/object:Gem::Requirement
71
+ none: false
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ hash: 3
76
+ segments:
77
+ - 0
78
+ version: "0"
79
+ requirements: []
80
+
81
+ rubyforge_project:
82
+ rubygems_version: 1.3.7
83
+ signing_key:
84
+ specification_version: 3
85
+ summary: measure image size using pure Ruby
86
+ test_files:
87
+ - test/test_helper.rb
88
+ - test/test_image_size.rb