Class: ImageSize

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

Overview

This is an adapted version of the image_size library to support some extra information from the JPEG file format. Additions have a comment with the word ‘koan’.

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



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

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

Class Method Details

.type_listObject

image type list



36
37
38
# File 'lib/image_size.rb', line 36

def ImageSize.type_list
	Type.constants 
end

Instance Method Details

#get_bpcObject

koan: additions to get the precision and number of components of a JPEG file



95
96
97
# File 'lib/image_size.rb', line 95

def get_bpc
	if @img_type == Type::JPEG then @img_bpc else nil end
end

#get_componentsObject



98
99
100
# File 'lib/image_size.rb', line 98

def get_components
	if @img_type == Type::JPEG then @img_components else nil end
end

#get_heightObject



87
88
89
# File 'lib/image_size.rb', line 87

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

#get_typeObject

get parameter



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

def get_type; @img_type; end

#get_widthObject



90
91
92
# File 'lib/image_size.rb', line 90

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

#measure_SWFObject



256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
# File 'lib/image_size.rb', line 256

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