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.



50
51
52
53
54
# File 'lib/tesseract/api/image.rb', line 50

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.



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

def x
  @x
end

#yObject

Returns the value of attribute y.



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

def y
  @y
end

Class Method Details

.finalize(pointer) ⇒ Object



56
57
58
# File 'lib/tesseract/api/image.rb', line 56

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

.new(image, x = 0, y = 0) ⇒ Object

Raises:

  • (ArgumentError)


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

def self.new (image, x = 0, y = 0)
  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)
  else
    image
  end

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

  super(image, x, y)
end

Instance Method Details

#heightObject



64
65
66
# File 'lib/tesseract/api/image.rb', line 64

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

#to_blob(format = :default) ⇒ Object



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

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))
  C.free(data.typecast(:pointer))

  result
end

#to_ffiObject



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

def to_ffi
  @internal
end

#widthObject



60
61
62
# File 'lib/tesseract/api/image.rb', line 60

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