Class: Tesseract::API::Image

Inherits:
Object
  • Object
show all
Defined in:
lib/tesseract/api/image.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(pointer, x = 0, y = 0) ⇒ Image

Returns a new instance of Image.



48
49
50
51
52
# File 'lib/tesseract/api/image.rb', line 48

def initialize (pointer, x = 0, y = 0)
	@internal = FFI::AutoPointer.new(pointer, self.class.method(:finalize))
	@x        = x
	@y        = y
end

Instance Attribute Details

#xObject

Returns the value of attribute x.



46
47
48
# File 'lib/tesseract/api/image.rb', line 46

def x
  @x
end

#yObject

Returns the value of attribute y.



46
47
48
# File 'lib/tesseract/api/image.rb', line 46

def y
  @y
end

Class Method Details

.finalize(pointer) ⇒ Object



54
55
56
# File 'lib/tesseract/api/image.rb', line 54

def self.finalize (pointer)
	C::Leptonica.pix_destroy(pointer)
end

.new(image) ⇒ Object

Raises:

  • (ArgumentError)


28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/tesseract/api/image.rb', line 28

def self.new (image)
	image = if image.is_a?(String) && (File.exists?(File.expand_path(image)) rescue nil)
		C::Leptonica.pix_read(File.expand_path(image))
	elsif image.is_a?(String)
		C::Leptonica.pix_read_mem(image, image.bytesize)
	elsif image.is_a?(IO)
		C::Leptonica.pix_read_stream(image.to_i)
	elsif image.respond_to? :to_blob
		image = image.to_blob

		C::Leptonica.pix_read_mem(image, image.bytesize)
	end

	raise ArgumentError, 'invalid image' if image.nil? || image.null?

	super(image)
end

Instance Method Details

#heightObject



62
63
64
# File 'lib/tesseract/api/image.rb', line 62

def height
	C::Leptonica.pix_get_height(to_ffi)
end

#to_blob(format = :default) ⇒ Object



66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/tesseract/api/image.rb', line 66

def to_blob (format = :default)
	data = FFI::MemoryPointer.new(:pointer)
	size = FFI::MemoryPointer.new(:size_t)

	C::Leptonica.pix_write_mem(to_ffi, data, size, C.for_enum(format))

	result = data.typecast(:pointer).read_string(size.typecast(:size_t))

	data.typecast(:pointer).free

	result
end

#to_ffiObject



79
80
81
# File 'lib/tesseract/api/image.rb', line 79

def to_ffi
	@internal
end

#widthObject



58
59
60
# File 'lib/tesseract/api/image.rb', line 58

def width
	C::Leptonica.pix_get_width(to_ffi)
end