core_image 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (2) hide show
  1. data/lib/core_image.rb +52 -25
  2. metadata +5 -5
data/lib/core_image.rb CHANGED
@@ -1,30 +1,35 @@
1
+ # CORE IMAGE GEM
2
+ # Created by Spencer Rogers on January 24, 2012
3
+ # https://github.com/serogers
4
+
1
5
  class CoreImage
2
6
  require 'osx/cocoa'
3
7
 
4
- # CORE IMAGE PROTOTYPE
5
- # Created by Spencer Rogers on January 24, 2012
6
-
7
8
  attr_accessor :image_path
8
9
  attr_accessor :original_image
9
10
  attr_accessor :ciimage
10
- attr_accessor :bitmap
11
11
 
12
12
  def initialize(object)
13
13
  initialize_image(object)
14
14
  end
15
15
 
16
- def scale(ratio)
16
+ def scale(ratio = 1)
17
17
  scaleFilter = OSX::CGAffineTransformMakeScale(ratio, ratio)
18
18
  self.ciimage = self.ciimage.imageByApplyingTransform(scaleFilter)
19
19
  self
20
- end # scale
20
+ end
21
21
 
22
- def rotate(degrees)
22
+ def scale_to_size(maximum = 500)
23
+ dimensions = size_to_fit_maximum(maximum)
24
+ scale(dimensions[:ratio])
25
+ end
26
+
27
+ def rotate(degrees = 0)
23
28
  radians = degrees_to_radians(degrees)
24
29
  transform = OSX::CGAffineTransformMakeRotation(radians)
25
30
  self.ciimage = self.ciimage.imageByApplyingTransform(transform)
26
31
  self
27
- end # rotate
32
+ end
28
33
 
29
34
  def flip_horizontal
30
35
  transform = OSX::CGAffineTransformMakeScale(-1.0, 1.0)
@@ -38,14 +43,28 @@ class CoreImage
38
43
  self
39
44
  end # crop
40
45
 
41
- def tint(x, y, w, h, rgb_string)
42
- context = create_ci_context(w, h)
46
+ def tint(rgb_string)
47
+ image_size = size
48
+ context = create_ci_context(image_size[:width], image_size[:height])
43
49
  colored_image = OSX::CIImage.imageWithColor(OSX::CIColor.colorWithString(rgb_string))
44
50
  filter = OSX::CIFilter.filterWithName("CIMultiplyCompositing")
45
51
  filter.setValue_forKey(colored_image, "inputImage")
46
52
  filter.setValue_forKey(self.ciimage, "inputBackgroundImage")
47
53
  new_image = filter.valueForKey("outputImage")
48
- new_image.drawAtPoint_fromRect_operation_fraction(OSX::NSMakePoint(x, y), OSX::NSRectFromCGRect(new_image.extent), OSX::NSCompositeCopy, 1.0)
54
+ new_image.drawAtPoint_fromRect_operation_fraction(OSX::NSZeroPoint, OSX::NSRectFromCGRect(new_image.extent), OSX::NSCompositeCopy, 1.0)
55
+ self.ciimage = new_image
56
+ self
57
+ end
58
+
59
+ def overlay_image(object)
60
+ image = open_object(object)
61
+ image_size = size
62
+ context = create_ci_context(image_size[:width], image_size[:height])
63
+ filter = OSX::CIFilter.filterWithName("CISourceOverCompositing")
64
+ filter.setValue_forKey(image, "inputImage")
65
+ filter.setValue_forKey(self.ciimage, "inputBackgroundImage")
66
+ new_image = filter.valueForKey("outputImage")
67
+ new_image.drawAtPoint_fromRect_operation_fraction(OSX::NSZeroPoint, OSX::NSRectFromCGRect(new_image.extent), OSX::NSCompositeCopy, 1.0)
49
68
  self.ciimage = new_image
50
69
  self
51
70
  end
@@ -66,7 +85,7 @@ class CoreImage
66
85
  width, height = 0, 0
67
86
 
68
87
  begin # sometimes ciimage.extent throws an error
69
- size = self.image.extent.size
88
+ size = self.ciimage.extent.size
70
89
  width = size.width
71
90
  height = size.height
72
91
  rescue
@@ -84,6 +103,14 @@ class CoreImage
84
103
  {:width => width, :height => height}
85
104
  end
86
105
 
106
+ def size_to_fit_maximum(max = 500)
107
+ image_size = size
108
+ width = image_size[:width].to_f
109
+ height = image_size[:height].to_f
110
+ ratio = width > height ? max.to_f / width : max.to_f / height rescue 1
111
+ {:width => (width * ratio).to_i, :height => (height * ratio).to_i, :ratio => ratio}
112
+ end
113
+
87
114
  def revert
88
115
  self.ciimage = self.original_image
89
116
  self
@@ -97,21 +124,21 @@ class CoreImage
97
124
  private
98
125
 
99
126
  def initialize_image(object)
127
+ self.ciimage = open_object(object)
128
+ self.image_path = object if object.class == String
100
129
 
130
+ # remember the original image for reversion
131
+ if self.ciimage
132
+ self.original_image = self.ciimage
133
+ end
134
+ end
135
+
136
+ def open_object(object)
101
137
  case object.class.to_s
102
138
  when "String"
103
- self.image_path = object
104
- if File.extname(object).downcase == ".pdf"
105
- self.ciimage = open_from_pdf_path(object)
106
- else
107
- self.ciimage = open_from_path(object)
108
- end
139
+ ciimage = File.extname(object).downcase == ".pdf" ? open_from_pdf_path(object) : open_from_path(object)
109
140
  when "OSX::CIImage"
110
- self.ciimage = object
111
- end
112
-
113
- if self.ciimage
114
- self.original_image = self.ciimage
141
+ ciimage = object
115
142
  end
116
143
  end
117
144
 
@@ -179,8 +206,8 @@ class CoreImage
179
206
  end
180
207
 
181
208
  def create_ns_context(width, height)
182
- bitmapRep = OSX::NSBitmapImageRep.alloc.initWithBitmapDataPlanes_pixelsWide_pixelsHigh_bitsPerSample_samplesPerPixel_hasAlpha_isPlanar_colorSpaceName_bytesPerRow_bitsPerPixel(nil, width, height, 8, 4, true, false, OSX::NSDeviceRGBColorSpace, 0, 0)
183
- context = OSX::NSGraphicsContext.graphicsContextWithBitmapImageRep(bitmapRep)
209
+ blank_bitmap = OSX::NSBitmapImageRep.alloc.initWithBitmapDataPlanes_pixelsWide_pixelsHigh_bitsPerSample_samplesPerPixel_hasAlpha_isPlanar_colorSpaceName_bytesPerRow_bitsPerPixel(nil, width, height, 8, 4, true, false, OSX::NSDeviceRGBColorSpace, 0, 0)
210
+ context = OSX::NSGraphicsContext.graphicsContextWithBitmapImageRep(blank_bitmap)
184
211
  OSX::NSGraphicsContext.setCurrentContext(context)
185
212
  context
186
213
  end # createContext
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: core_image
3
3
  version: !ruby/object:Gem::Version
4
- hash: 29
4
+ hash: 27
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 0
9
- - 1
10
- version: 0.0.1
9
+ - 2
10
+ version: 0.0.2
11
11
  platform: ruby
12
12
  authors:
13
13
  - Spencer Rogers
@@ -19,7 +19,7 @@ date: 2012-01-24 00:00:00 -08:00
19
19
  default_executable:
20
20
  dependencies: []
21
21
 
22
- description: A gem to harness the power of Apple's Core Image technology that is built into Mac OS X. Scale, rotate, flip, convert, tint, and more!
22
+ description: Simple image manipulation using Apple's Core Image technology. Scale, rotate, flip, convert, overlay, and more.
23
23
  email: SpencerRogers@gmail.com
24
24
  executables: []
25
25
 
@@ -57,7 +57,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
57
57
  - 0
58
58
  version: "0"
59
59
  requirements:
60
- - Mac OS X
60
+ - - Mac OS X
61
61
  rubyforge_project:
62
62
  rubygems_version: 1.5.3
63
63
  signing_key: