core_image 0.0.1

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 +188 -0
  2. metadata +67 -0
@@ -0,0 +1,188 @@
1
+ class CoreImage
2
+ require 'osx/cocoa'
3
+
4
+ # CORE IMAGE PROTOTYPE
5
+ # Created by Spencer Rogers on January 24, 2012
6
+
7
+ attr_accessor :image_path
8
+ attr_accessor :original_image
9
+ attr_accessor :ciimage
10
+ attr_accessor :bitmap
11
+
12
+ def initialize(object)
13
+ initialize_image(object)
14
+ end
15
+
16
+ def scale(ratio)
17
+ scaleFilter = OSX::CGAffineTransformMakeScale(ratio, ratio)
18
+ self.ciimage = self.ciimage.imageByApplyingTransform(scaleFilter)
19
+ self
20
+ end # scale
21
+
22
+ def rotate(degrees)
23
+ radians = degrees_to_radians(degrees)
24
+ transform = OSX::CGAffineTransformMakeRotation(radians)
25
+ self.ciimage = self.ciimage.imageByApplyingTransform(transform)
26
+ self
27
+ end # rotate
28
+
29
+ def flip_horizontal
30
+ transform = OSX::CGAffineTransformMakeScale(-1.0, 1.0)
31
+ self.ciimage = self.ciimage.imageByApplyingTransform(transform)
32
+ self
33
+ end
34
+
35
+ def crop(x, y, w, h)
36
+ # coordinates start in lower left
37
+ self.ciimage = self.ciimage.imageByCroppingToRect(OSX::CGRectMake(x, y, w, h))
38
+ self
39
+ end # crop
40
+
41
+ def tint(x, y, w, h, rgb_string)
42
+ context = create_ci_context(w, h)
43
+ colored_image = OSX::CIImage.imageWithColor(OSX::CIColor.colorWithString(rgb_string))
44
+ filter = OSX::CIFilter.filterWithName("CIMultiplyCompositing")
45
+ filter.setValue_forKey(colored_image, "inputImage")
46
+ filter.setValue_forKey(self.ciimage, "inputBackgroundImage")
47
+ 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)
49
+ self.ciimage = new_image
50
+ self
51
+ end
52
+
53
+ def color_at(x, y)
54
+ to_bitmap.colorAtX_y(x, y) # returns NSColor
55
+ end
56
+
57
+ def to_bitmap
58
+ OSX::NSBitmapImageRep.alloc.initWithCIImage(self.ciimage)
59
+ end
60
+
61
+ def to_cgimage
62
+ to_bitmap.CGImage
63
+ end
64
+
65
+ def size
66
+ width, height = 0, 0
67
+
68
+ begin # sometimes ciimage.extent throws an error
69
+ size = self.image.extent.size
70
+ width = size.width
71
+ height = size.height
72
+ rescue
73
+ # if image.extent fails, use another method for measuring size (by converting to a cgimage)
74
+ begin
75
+ cgimage = self.to_cgimage
76
+ width = OSX::CGImageGetWidth(cgimage)
77
+ height = OSX::CGImageGetHeight(cgimage)
78
+ rescue # if the second image size function fails set zero
79
+ width = 0
80
+ height = 0
81
+ end # begin/rescue
82
+ end # begin/rescue
83
+
84
+ {:width => width, :height => height}
85
+ end
86
+
87
+ def revert
88
+ self.ciimage = self.original_image
89
+ self
90
+ end
91
+
92
+ def save(save_to = self.image_path)
93
+ blob = to_bitmap.representationUsingType_properties(file_type(save_to), nil)
94
+ blob.writeToFile_atomically(save_to, false)
95
+ end
96
+
97
+ private
98
+
99
+ def initialize_image(object)
100
+
101
+ case object.class.to_s
102
+ 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
109
+ when "OSX::CIImage"
110
+ self.ciimage = object
111
+ end
112
+
113
+ if self.ciimage
114
+ self.original_image = self.ciimage
115
+ end
116
+ end
117
+
118
+ def open_from_path(path_to_image)
119
+ OSX::CIImage.imageWithContentsOfURL(OSX::NSURL.fileURLWithPath(path_to_image))
120
+ end # open
121
+
122
+ def open_from_pdf_path(pdf_path, scale = 1, dpi = 72.0, preserve_alpha = false)
123
+ data = OSX::NSData.dataWithContentsOfURL(OSX::NSURL.fileURLWithPath(pdfPath))
124
+ pdf_rep = OSX::NSPDFImageRep.imageRepWithData(data)
125
+ nsimage = OSX::NSImage.alloc.initWithData(data)
126
+ nssize = nsimage.size
127
+
128
+ # calculate the width and height of the new image
129
+ width = (nssize.width.to_f * 200.0 * scale) / 72.0
130
+ height = (nssize.height.to_f * 200.0 * scale) / 72.0
131
+
132
+ # set the size of the new image
133
+ nssize = OSX::NSMakeSize(width, height)
134
+ context = create_ns_context(width, height)
135
+ context.setShouldAntialias(true)
136
+
137
+ # create a bounding rectangle to grab the contexts of the pdf
138
+ destination_rectangle = pdf_rep.bounds
139
+ destination_rectangle.size = nssize
140
+
141
+ OSX::NSRectFill(destination_rectangle) if preserve_alpha # defaults to black
142
+
143
+ # grab the contents of the rectangle from the pdf and store it in the graphics port
144
+ pdf_rep.drawInRect(destination_rectangle)
145
+
146
+ # grab the new image from the graphics port and convert to cgimage
147
+ cgimage = OSX::CGBitmapContextCreateImage(context.graphicsPort)
148
+
149
+ # return a ciimage
150
+ OSX::CIImage.initWithCGImage(cgimage)
151
+ end
152
+
153
+ def degrees_to_radians(degrees)
154
+ degrees.to_f * (Math::PI / 180.0)
155
+ end
156
+
157
+ def file_type(image_name)
158
+ return nil if image_name.nil?
159
+
160
+ case File.extname(image_name).downcase
161
+ when ".jpg", ".jpeg"
162
+ OSX::NSJPEGFileType
163
+ when ".png"
164
+ OSX::NSPNGFileType
165
+ when ".tiff", ".tif"
166
+ OSX::NSTIFFFileType
167
+ when ".gif"
168
+ OSX::NSGIFFileType
169
+ when ".pct"
170
+ OSX::NSPCTFileType
171
+ when ".bmp"
172
+ OSX::NSBMPFileType
173
+ end
174
+
175
+ end
176
+
177
+ def create_ci_context(width, height)
178
+ create_ns_context(width, height).CIContext
179
+ end
180
+
181
+ 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)
184
+ OSX::NSGraphicsContext.setCurrentContext(context)
185
+ context
186
+ end # createContext
187
+
188
+ end
metadata ADDED
@@ -0,0 +1,67 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: core_image
3
+ version: !ruby/object:Gem::Version
4
+ hash: 29
5
+ prerelease:
6
+ segments:
7
+ - 0
8
+ - 0
9
+ - 1
10
+ version: 0.0.1
11
+ platform: ruby
12
+ authors:
13
+ - Spencer Rogers
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2012-01-24 00:00:00 -08:00
19
+ default_executable:
20
+ dependencies: []
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!
23
+ email: SpencerRogers@gmail.com
24
+ executables: []
25
+
26
+ extensions: []
27
+
28
+ extra_rdoc_files: []
29
+
30
+ files:
31
+ - lib/core_image.rb
32
+ has_rdoc: true
33
+ homepage: https://github.com/serogers
34
+ licenses: []
35
+
36
+ post_install_message:
37
+ rdoc_options: []
38
+
39
+ require_paths:
40
+ - lib
41
+ required_ruby_version: !ruby/object:Gem::Requirement
42
+ none: false
43
+ requirements:
44
+ - - ">="
45
+ - !ruby/object:Gem::Version
46
+ hash: 3
47
+ segments:
48
+ - 0
49
+ version: "0"
50
+ required_rubygems_version: !ruby/object:Gem::Requirement
51
+ none: false
52
+ requirements:
53
+ - - ">="
54
+ - !ruby/object:Gem::Version
55
+ hash: 3
56
+ segments:
57
+ - 0
58
+ version: "0"
59
+ requirements:
60
+ - Mac OS X
61
+ rubyforge_project:
62
+ rubygems_version: 1.5.3
63
+ signing_key:
64
+ specification_version: 3
65
+ summary: Manipulate images via Apple's Core Image technology
66
+ test_files: []
67
+