Class: CoreImage

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

Overview

CORE IMAGE GEM Created by Spencer Rogers on January 24, 2012 github.com/serogers

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(object) ⇒ CoreImage

Returns a new instance of CoreImage.



13
14
15
# File 'lib/core_image.rb', line 13

def initialize(object)
  initialize_image(object)
end

Instance Attribute Details

#ciimageObject

Returns the value of attribute ciimage.



11
12
13
# File 'lib/core_image.rb', line 11

def ciimage
  @ciimage
end

#image_pathObject

Returns the value of attribute image_path.



9
10
11
# File 'lib/core_image.rb', line 9

def image_path
  @image_path
end

#original_imageObject

Returns the value of attribute original_image.



10
11
12
# File 'lib/core_image.rb', line 10

def original_image
  @original_image
end

Instance Method Details

#color_at(x, y) ⇒ Object

coordinates start in upper left



74
75
76
77
78
79
80
81
82
83
# File 'lib/core_image.rb', line 74

def color_at(x, y) # coordinates start in upper left
  set_context
  nscolor = to_bitmap.colorAtX_y(x, y)
  rgb = {}
  rgb[:red] = (nscolor.redComponent.to_f * 255.0).to_i
  rgb[:green] = (nscolor.greenComponent.to_f * 255.0).to_i
  rgb[:blue] = (nscolor.blueComponent.to_f * 255.0).to_i
  rgb[:alpha] = nscolor.alphaComponent
  rgb
end

#crop(x, y, w, h) ⇒ Object

coordinates start in lower left



45
46
47
48
# File 'lib/core_image.rb', line 45

def crop(x, y, w, h) # coordinates start in lower left
  self.ciimage = self.ciimage.imageByCroppingToRect(OSX::CGRectMake(x, y, w, h))
  self
end

#flip_horizontallyObject



35
36
37
38
39
# File 'lib/core_image.rb', line 35

def flip_horizontally
  transform = OSX::CGAffineTransformMakeScale(-1.0, 1.0)
  self.ciimage = self.ciimage.imageByApplyingTransform(transform)
  self
end

#flip_verticallyObject



41
42
43
# File 'lib/core_image.rb', line 41

def flip_vertically
  flip_horizontally.rotate(180)
end

#overlay(object) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
# File 'lib/core_image.rb', line 62

def overlay(object)
  image = open_object(object)
  context = set_context
  filter = OSX::CIFilter.filterWithName("CISourceOverCompositing")
  filter.setValue_forKey(image, "inputImage")
  filter.setValue_forKey(self.ciimage, "inputBackgroundImage")
  new_image = filter.valueForKey("outputImage")
  new_image.drawAtPoint_fromRect_operation_fraction(OSX::NSZeroPoint, OSX::NSRectFromCGRect(new_image.extent), OSX::NSCompositeCopy, 1.0)
  self.ciimage = new_image
  self
end

#revertObject



130
131
132
133
# File 'lib/core_image.rb', line 130

def revert
  self.ciimage = self.original_image
  self
end

#rotate(degrees = 90) ⇒ Object



28
29
30
31
32
33
# File 'lib/core_image.rb', line 28

def rotate(degrees = 90)
  radians = degrees_to_radians(degrees)
  transform = OSX::CGAffineTransformMakeRotation(radians)
  self.ciimage = self.ciimage.imageByApplyingTransform(transform)
  self
end

#save(save_to = self.image_path) ⇒ Object



135
136
137
138
# File 'lib/core_image.rb', line 135

def save(save_to = self.image_path)
  blob = to_bitmap.representationUsingType_properties(file_type(save_to), nil)
  blob.writeToFile_atomically(save_to, false)
end

#scale(ratio = 1) ⇒ Object



17
18
19
20
21
# File 'lib/core_image.rb', line 17

def scale(ratio = 1)
  scaleFilter = OSX::CGAffineTransformMakeScale(ratio, ratio)
  self.ciimage = self.ciimage.imageByApplyingTransform(scaleFilter)
  self
end

#scale_to_size(maximum = 500) ⇒ Object



23
24
25
26
# File 'lib/core_image.rb', line 23

def scale_to_size(maximum = 500)
  dimensions = size_to_fit_maximum(maximum)
  scale(dimensions[:ratio])
end

#sizeObject



101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/core_image.rb', line 101

def size
  width, height = 0, 0
  
  begin # sometimes ciimage.extent throws an error
    image_size = self.ciimage.extent.size
    width = image_size.width
    height = image_size.height
  rescue
    begin
      cgimage = self.to_cgimage
    	width = OSX::CGImageGetWidth(cgimage)
    	height = OSX::CGImageGetHeight(cgimage)
  	rescue
  	  width = 0
  	  height = 0
	  end
	end
	
	{:width => width, :height => height}
end

#size_to_fit_maximum(max = 500) ⇒ Object



122
123
124
125
126
127
128
# File 'lib/core_image.rb', line 122

def size_to_fit_maximum(max = 500)    
  image_size = size
  width = image_size[:width].to_f
  height = image_size[:height].to_f
  ratio = width > height ? max.to_f / width : max.to_f / height rescue 1
  {:width => (width * ratio).to_i, :height => (height * ratio).to_i, :ratio => ratio}
end

#tint(rgb) ⇒ Object

crop



50
51
52
53
54
55
56
57
58
59
60
# File 'lib/core_image.rb', line 50

def tint(rgb)
  context = set_context
  colored_image = OSX::CIImage.imageWithColor(OSX::CIColor.colorWithString(rgb_hash_to_string(rgb)))
  filter = OSX::CIFilter.filterWithName("CIMultiplyCompositing")
  filter.setValue_forKey(colored_image, "inputImage")
  filter.setValue_forKey(self.ciimage, "inputBackgroundImage")
  new_image = filter.valueForKey("outputImage")
  new_image.drawAtPoint_fromRect_operation_fraction(OSX::NSZeroPoint, OSX::NSRectFromCGRect(new_image.extent), OSX::NSCompositeCopy, 1.0)
  self.ciimage = new_image
  self
end

#to_bitmapObject



85
86
87
# File 'lib/core_image.rb', line 85

def to_bitmap
  OSX::NSBitmapImageRep.alloc.initWithCIImage(self.ciimage)
end

#to_cgimageObject



89
90
91
# File 'lib/core_image.rb', line 89

def to_cgimage
  to_bitmap.CGImage
end

#to_nsimageObject



93
94
95
96
97
98
99
# File 'lib/core_image.rb', line 93

def to_nsimage
  image_size = self.size
  nsimage_rep = OSX::NSCIImageRep.imageRepWithCIImage(self.ciimage)
  nsimage = OSX::NSImage.alloc.initWithSize(OSX::NSMakeSize(image_size[:width], image_size[:height]))
  nsimage.addRepresentation(nsimage_rep)
  nsimage
end