Class: Miso::Processor::CoreImage

Inherits:
Miso::Processor show all
Defined in:
lib/miso/processor/core_image.rb

Instance Attribute Summary

Attributes inherited from Miso::Processor

#input_file

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Miso::Processor

#auto_orient, #height, #width

Constructor Details

#initialize(input_file) ⇒ CoreImage

Returns a new instance of CoreImage.



9
10
11
12
13
# File 'lib/miso/processor/core_image.rb', line 9

def initialize(input_file)
  super
  require 'osx/cocoa'
  reset!
end

Class Method Details

.available?Boolean

Returns:

  • (Boolean)


5
6
7
# File 'lib/miso/processor/core_image.rb', line 5

def self.available?
  $:.any? { |path| File.exist? File.join(path, 'osx/cocoa.rb') }
end

Instance Method Details

#crop(width, height) ⇒ Object



19
20
21
22
23
24
25
26
27
28
# File 'lib/miso/processor/core_image.rb', line 19

def crop(width, height)
  scale_width, scale_height = scale(width, height)
  multiplier = scale_width > scale_height ? scale_width : scale_height
  transform(multiplier)
  
  # find the center and calculate the new bottom right from there
  x = ((buffer_width.to_f - width.to_f) / 2).round.abs
  y = ((buffer_height.to_f - height.to_f) / 2).round.abs
  _crop(x, y, width, height)
end

#dimensionsObject



15
16
17
# File 'lib/miso/processor/core_image.rb', line 15

def dimensions
  @dimensions ||= image.extent.size.to_a
end

#fit(width, height) ⇒ Object



30
31
32
33
34
35
36
37
38
# File 'lib/miso/processor/core_image.rb', line 30

def fit(width, height)
  scale_width, scale_height = scale(width, height)
  multiplier = scale_width < scale_height ? scale_width : scale_height
  new_width  = (self.width * multiplier).round
  new_height = (self.height * multiplier).round
  
  transform(multiplier)
  _crop(0, 0, new_width, new_height)
end

#write(output_file) ⇒ Object



40
41
42
43
44
45
# File 'lib/miso/processor/core_image.rb', line 40

def write(output_file)
  bitmap = OSX::NSBitmapImageRep.alloc.initWithCIImage(@buffer)
  blob = bitmap.representationUsingType_properties(detect_file_type(output_file), nil)
  blob.writeToFile_atomically(output_file, false)
  reset!
end