Class: Imgry::Processor::JavaAdapter

Inherits:
Adapter
  • Object
show all
Includes:
Java
Defined in:
lib/imgry/processor/java_adapter.rb

Direct Known Subclasses

ImageVoodoo, ImgScalr

Instance Attribute Summary

Attributes inherited from Adapter

#img, #img_blob

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Adapter

#aspect_ratio, #initialize, #inspect, #load_image!, load_lib!

Constructor Details

This class inherits a constructor from Imgry::Processor::Adapter

Class Method Details

.from_file(path) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
# File 'lib/imgry/processor/java_adapter.rb', line 21

def self.from_file(path)
  if !File.readable?(path)
    raise FileUnreadableError, path.to_s
  end

  img_blob = IO.read(path.to_s)
  with_bytes(img_blob)

  # TODO: read the file using Java file io instead..?
  # input_stream = java.io.FileInputStream.new(java.io.File.new(path.to_s))
end

.supported_formatsObject



43
44
45
46
47
48
49
# File 'lib/imgry/processor/java_adapter.rb', line 43

def self.supported_formats
  @supported_formats ||= begin
    # NOTE: assuming read and write formats are the same..
    # Typical formats: bmp, jpg, wbmp, jpeg, png, gif
    ImageIO.getReaderFormatNames.to_a.map(&:downcase).uniq
  end
end

.with_bytes(img_blob) ⇒ Object



16
17
18
19
# File 'lib/imgry/processor/java_adapter.rb', line 16

def self.with_bytes(img_blob)
  img_blob = img_blob.to_java_bytes if img_blob.is_a?(String)
  new(img_blob)
end

Instance Method Details

#clean!Object



63
64
65
66
# File 'lib/imgry/processor/java_adapter.rb', line 63

def clean!
  @img = nil
  @img_blob = nil
end

#crop_geometry(geometry) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/imgry/processor/java_adapter.rb', line 68

def crop_geometry(geometry)
  # no gravity support yet, so all offsets should be > 0
  width, height, offset_x, offset_y, flag = Geometry.from_s(geometry)

  offset_x = 0 if offset_x > self.width || offset_x < 0
  offset_y = 0 if offset_y > self.height || offset_y < 0

  width  = self.width - offset_x if width + offset_x > self.width
  height = self.height - offset_y if height + offset_y > self.height

  [width, height, offset_x, offset_y, flag]
end

#detect_image_format!(image_input_stream) ⇒ Object



33
34
35
36
37
38
39
40
41
# File 'lib/imgry/processor/java_adapter.rb', line 33

def detect_image_format!(image_input_stream)
  @format = nil # reset..

  reader = ImageIO.get_image_readers(image_input_stream).first
  raise UnsupportedFormatError if reader.nil?

  @format = reader.format_name.downcase
  @format = 'jpg' if @format == 'jpeg' # prefer this way..
end

#formatObject



51
52
53
# File 'lib/imgry/processor/java_adapter.rb', line 51

def format
  @format
end

#heightObject



59
60
61
# File 'lib/imgry/processor/java_adapter.rb', line 59

def height
  @img.height
end

#save(path) ⇒ Object



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/imgry/processor/java_adapter.rb', line 93

def save(path)
  if !File.writable?(File.dirname(path))
    raise FileUnwritableError, path.to_s
  end

  ext = File.extname(path)
  format = !ext.nil? ? ext[1..-1].downcase : @format

  if !self.class.supported_formats.include?(format)
    raise UnsupportedFormatError, format
  end

  ImageIO.write(src, format.downcase, java.io.File.new(path.to_s))
  true
end

#to_blob(format = nil) ⇒ Object



81
82
83
84
85
86
87
88
89
90
91
# File 'lib/imgry/processor/java_adapter.rb', line 81

def to_blob(format=nil)
  format ||= @format

  if !self.class.supported_formats.include?(format.downcase)
    raise UnsupportedFormatError, format
  end

  out = ByteArrayOutputStream.new
  ImageIO.write(src, format, out)
  String.from_java_bytes(out.to_byte_array)
end

#widthObject



55
56
57
# File 'lib/imgry/processor/java_adapter.rb', line 55

def width
  @img.width
end