File: image_size.rb

package info (click to toggle)
libimage-size-ruby 20040618-1
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 204 kB
  • ctags: 27
  • sloc: ruby: 317; makefile: 64
file content (277 lines) | stat: -rwxr-xr-x 7,901 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
#!ruby

class ImageSize
# Image Type Constants
	module Type
		OTHER = "OTHER"
		GIF  = "GIF"
		PNG  = "PNG"
		JPEG = "JPEG"
		BMP  = "BMP"
		PPM  = "PPM" # PPM is like PBM, PGM, & XV
		PBM  = "PBM"
		PGM  = "PGM"
#		XV   = "XV"
		XBM  = "XBM"
		TIFF = "TIFF"
		XPM  = "XPM"
		PSD  = "PSD"
		PCX  = "PCX"
		SWF  = "SWF"
	end

	JpegCodeCheck = [
		"\xc0", "\xc1", "\xc2", "\xc3",
		"\xc5", "\xc6", "\xc7",
		"\xc9", "\xca", "\xcb",
		"\xcd", "\xce", "\xcf",
	]

# image type list
	def ImageSize.type_list
		Type.constants 
	end

# receive image & make size
# argument is image String or IO
	def initialize(img_data, img_type = nil)
		@img_data = img_data.dup
		@img_wedth = nil
		@img_height = nil

		if @img_data.is_a?(IO)
			@img_top = @img_data.read(128)
			@img_data.seek(0, 0)
# define Singleton-method definition to IO (byte, offset)
			def @img_data.read_o(length = 1, offset = nil)
				self.seek(offset, 0) if offset
				ret = self.read(length)
				raise "cannot read!!" unless ret
				ret
			end
		elsif @img_data.is_a?(String)
			@img_top = @img_data[0, 128]
# define Singleton-method definition to String (byte, offset)
			def @img_data.read_o(length = 1, offset = nil)
				@img_offset = 0 if !(defined?(@img_offset))
				@img_offset = offset if offset
				ret = self[@img_offset, length]
				@img_offset += length
				ret
			end
		else
			raise "argument class error!! #{img_data.type}"
		end

		if img_type.nil?
			@img_type = check_type()
		else
			match = false
			Type.constants.each do |t|
				match = true if img_type == t
			end
			raise("img_type is failed. #{img_type}\n") if match == false
			@img_type = img_type
		end

		eval("@img_width, @img_height = measure_" + @img_type + "()") if @img_type != Type::OTHER
	end

# get parameter
	def get_type; @img_type; end
	def get_height
		if @img_type == Type::OTHER then nil else @img_height end
	end
	def get_width
		if @img_type == Type::OTHER then nil else @img_width end
	end

	def check_type()
		if @img_top =~ /^GIF8[7,9]a/                      then Type::GIF
		elsif @img_top[0, 8] == "\x89PNG\x0d\x0a\x1a\x0a" then Type::PNG
		elsif @img_top[0, 2] == "\xFF\xD8"                then Type::JPEG
		elsif @img_top[0, 2] == 'BM'                      then Type::BMP
		elsif @img_top =~ /^P[1-7]/                       then Type::PPM
		elsif @img_top =~ /\#define\s+\S+\s+\d+/          then Type::XBM
		elsif @img_top[0, 4] == "MM\x00\x2a"              then Type::TIFF
		elsif @img_top[0, 4] == "II\x2a\x00"              then Type::TIFF
		elsif @img_top =~ /\/\* XPM \*\//                 then Type::XPM
		elsif @img_top[0, 4] == "8BPS"                    then Type::PSD
		elsif @img_top[0, 3] == "FWS"                     then Type::SWF
		elsif @img_top[0] == 10                           then Type::PCX
		else Type::OTHER
		end
	end
	private(:check_type)

	def measure_GIF()
		@img_data.read_o(6)
		@img_data.read_o(4).unpack('vv')
	end
	private(:measure_GIF)

	def measure_PNG()
		@img_data.read_o(12)
		raise "This file is not PNG." unless @img_data.read_o(4) == "IHDR"
		@img_data.read_o(8).unpack('NN')
	end
	private(:measure_PNG)

	def measure_JPEG()
		c_marker = "\xFF"   # Section marker.
		@img_data.read_o(2)
		while(true)
			marker, code, length = @img_data.read_o(4).unpack('aan')
			raise "JPEG marker not found!" if marker != c_marker

			if JpegCodeCheck.include?(code)
				height, width = @img_data.read_o(5).unpack('xnn')
				return([width, height])
			end
			@img_data.read_o(length - 2)
		end
	end
	private(:measure_JPEG)

	def measure_BMP()
		@img_data.read_o(26).unpack("x18VV");
	end
	private(:measure_BMP)

	def measure_PPM()
		header = @img_data.read_o(1024)
		header.gsub!(/^\#[^\n\r]*/m, "")
		header =~ /^(P[1-6])\s+?(\d+)\s+?(\d+)/m
		width = $2.to_i; height = $3.to_i
		case $1
			when "P1", "P4" then @img_type = "PBM"
			when "P2", "P5" then @img_type = "PGM"
			when "P3", "P6" then @img_type = "PPM"
#			when "P7"
#				@img_type = "XV"
#				header =~ /IMGINFO:(\d+)x(\d+)/m
#				width = $1.to_i; height = $2.to_i
		end
		[width, height]
	end
	private(:measure_PPM)

	alias :measure_PGM :measure_PPM
	private(:measure_PGM)
	alias :measure_PBM :measure_PPM
	private(:measure_PBM)

	def measure_XBM()
		@img_data.read_o(1024) =~ /^\#define\s*\S*\s*(\d+)\s*\n\#define\s*\S*\s*(\d+)/mi
		[$1.to_i, $2.to_i]
	end
	private(:measure_XBM)

	def measure_XPM()
		width = height = nil
		while(line = @img_data.read_o(1024))
			if line =~ /"\s*(\d+)\s+(\d+)(\s+\d+\s+\d+){1,2}\s*"/m
				width = $1.to_i; height = $2.to_i
				break
			end
		end
		[width, height]
	end
	private(:measure_XPM)

	def measure_PSD()
		@img_data.read_o(26).unpack("x14NN")
	end
	private(:measure_PSD)

	def measure_TIFF()
		endian = if (@img_data.read_o(4) =~ /II\x2a\x00/o) then 'v' else 'n' end
# 'v' little-endian   'n' default to big-endian

		packspec = [
			nil,           # nothing (shouldn't happen)
			'C',           # BYTE (8-bit unsigned integer)
			nil,           # ASCII
			endian,        # SHORT (16-bit unsigned integer)
			endian.upcase, # LONG (32-bit unsigned integer)
			nil,           # RATIONAL
			'c',           # SBYTE (8-bit signed integer)
			nil,           # UNDEFINED
			endian,        # SSHORT (16-bit unsigned integer)
			endian.upcase, # SLONG (32-bit unsigned integer)
		]

		offset = @img_data.read_o(4).unpack(endian.upcase)[0] # Get offset to IFD

		ifd = @img_data.read_o(2, offset)
		num_dirent = ifd.unpack(endian)[0]                   # Make it useful
		offset += 2
		num_dirent = offset + (num_dirent * 12);             # Calc. maximum offset of IFD

		ifd = width = height = nil
		while(width.nil? || height.nil?)
			ifd = @img_data.read_o(12, offset)                 # Get first directory entry
			break if (ifd.nil? || (offset > num_dirent))
			offset += 12
			tag = ifd.unpack(endian)[0]                       # ...and decode its tag
			type = ifd[2, 2].unpack(endian)[0]                # ...and the data type

     # Check the type for sanity.
			next if (type > packspec.size + 0) || (packspec[type].nil?)
			if tag == 0x0100                                  # Decode the value
				width = ifd[8, 4].unpack(packspec[type])[0]
			elsif tag == 0x0101                               # Decode the value
				height = ifd[8, 4].unpack(packspec[type])[0]
			end
		end

		raise "#{if width.nil? then 'width not defined.' end} #{if height.nil? then 'height not defined.' end}" if width.nil? || height.nil?
		[width, height]
	end
	private(:measure_TIFF)

	def measure_PCX()
		header = @img_data.read_o(128)
		head_part = header.unpack('C4S4')
		width = head_part[6] - head_part[4] + 1
		height = head_part[7] - head_part[5] + 1
		[width, height]
	end
	private(:measure_PCX)

	def measure_SWF()
		header = @img_data.read_o(9)
		raise("This file is not SWF.") unless header.unpack('a3')[0] == 'FWS'

		bit_length = Integer("0b#{header.unpack('@8B5')}")
		header << @img_data.read_o(bit_length*4/8+1)
		str = header.unpack("@8B#{5+bit_length*4}")[0]
		last = 5
		x_min = Integer("0b#{str[last,bit_length]}")
		x_max = Integer("0b#{str[(last += bit_length),bit_length]}")
		y_min = Integer("0b#{str[(last += bit_length),bit_length]}")
		y_max = Integer("0b#{str[(last += bit_length),bit_length]}")
		width = (x_max - x_min)/20
		height = (y_max - y_min)/20
		[width, height]
	end
	private(:measure_PCX)
end


if __FILE__ == $0
	print "TypeList: #{ImageSize.type.inspect}\n"

	Dir.glob("*").each do |file|
		print "#{file} (string)\n"
		open(file, "rb") do |fh|
			img = ImageSize.new(fh.read)
			print <<-EOF
type:   #{img.get_type.inspect}
width:  #{img.get_width.inspect}
height: #{img.get_height.inspect}
			EOF
		end
	end
end