Class: ImageSize

Inherits:
Object
  • Object
show all
Defined in:
lib/image_size.rb

Defined Under Namespace

Modules: Type

Constant Summary collapse

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(img_data, img_type = nil) ⇒ ImageSize

receive image & make size argument is image String or IO



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
# File 'lib/image_size.rb', line 37

def initialize(img_data, img_type = nil)
	@img_data = img_data.dup
	@img_width = 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

Class Method Details

.type_listObject

image type list



31
32
33
# File 'lib/image_size.rb', line 31

def ImageSize.type_list
	Type.constants 
end

Instance Method Details

#get_heightObject



82
83
84
# File 'lib/image_size.rb', line 82

def get_height
	if @img_type == Type::OTHER then nil else @img_height end
end

#get_typeObject

get parameter



81
# File 'lib/image_size.rb', line 81

def get_type; @img_type; end

#get_widthObject



85
86
87
# File 'lib/image_size.rb', line 85

def get_width
	if @img_type == Type::OTHER then nil else @img_width end
end

#measure_SWFObject



242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
# File 'lib/image_size.rb', line 242

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