0
+require 'active_support'
0
+require 'red_artisan/core_image/filters/scale'
0
+require 'red_artisan/core_image/filters/color'
0
+require 'red_artisan/core_image/filters/watermark'
0
+require 'red_artisan/core_image/filters/quality'
0
+require 'red_artisan/core_image/filters/perspective'
0
+require 'red_artisan/core_image/filters/effects'
0
+# Generic image processor for scaling images based on CoreImage via RubyCocoa.
0
+# p = Processor.new OSX::CIImage.from(path_to_image)
0
+# result.save('resized.jpg', OSX::NSJPEGFileType)
0
+# This will resize the image to the given dimensions exactly, if you'd like to ensure that aspect ratio is preserved:
0
+# p = Processor.new OSX::CIImage.from(path_to_image)
0
+# result.save('resized.jpg', OSX::NSJPEGFileType)
0
+# fit(size) will attempt its best to resize the image so that the longest width/height (depending on image orientation) will match
0
+# the given size. The second axis will be calculated automatically based on the aspect ratio.
0
+# Scaling is performed by first clamping the image so that its external bounds become infinite, this helps when scaling so that any
0
+# rounding discrepencies in dimensions don't affect the resultant image. We then perform a Lanczos transform on the image which scales
0
+# it to the target size. We then crop the image to the traget dimensions.
0
+# If you are generating smaller images such as thumbnails where high quality rendering isn't as important, an additional method is
0
+# p = Processor.new OSX::CIImage.from(path_to_image)
0
+# p.thumbnail(100, 100)
0
+# result.save('resized.jpg', OSX::NSJPEGFileType)
0
+# This will perform a straight affine transform and scale the X and Y boundaries to the requested size. Generally, this will be faster
0
+# than a lanczos scale transform, but with a scaling quality trade.
0
+# More than welcome to intregrate any patches, improvements - feel free to mail me with ideas.
0
+# * Satoshi Nakagawa for working out that OCObjWrapper needs inclusion when aliasing method_missing on existing OSX::* classes.
0
+# * Vasantha Crabb for general help and inspiration with Cocoa
0
+# * Ben Schwarz for example image data and collaboration during performance testing
0
+# Copyright (c) Marcus Crafter <crafterm@redartisan.com> released under the MIT license
0
+ def initialize(original)
0
+ if original.respond_to? :to_str
0
+ @original = OSX::CIImage.from(original.to_str)
0
+ raise "unprocessed image: #{@original}" unless @target
0
+ include Filters::Scale, Filters::Color, Filters::Watermark, Filters::Quality, Filters::Perspective, Filters::Effects
0
+ def create_core_image_context(width, height)
0
+ output = OSX::NSBitmapImageRep.alloc.initWithBitmapDataPlanes_pixelsWide_pixelsHigh_bitsPerSample_samplesPerPixel_hasAlpha_isPlanar_colorSpaceName_bytesPerRow_bitsPerPixel(nil, width, height, 8, 4, true, false, OSX::NSDeviceRGBColorSpace, 0, 0)
0
+ context = OSX::NSGraphicsContext.graphicsContextWithBitmapImageRep(output)
0
+ OSX::NSGraphicsContext.setCurrentContext(context)
0
+ @ci_context = context.CIContext
0
+ def vector(x, y, w, h)
0
+ OSX::CIVector.vectorWithX_Y_Z_W(x, y, w, h)
0
+ def method_missing_with_filter_processing(sym, *args, &block)
0
+ f = OSX::CIFilter.filterWithName("CI#{sym.to_s.camelize}")
0
+ return method_missing_without_filter_processing(sym, *args, &block) unless f
0
+ f.setDefaults if f.respond_to? :setDefaults
0
+ f.setValue_forKey(self, 'inputImage')
0
+ options = args.last.is_a?(Hash) ? args.last : {}
0
+ options.each { |k, v| f.setValue_forKey(v, k.to_s) }
0
+ block.call f.valueForKey('outputImage')
0
+ alias_method_chain :method_missing, :filter_processing
0
+ def save(target, format = OSX::NSJPEGFileType, properties = nil)
0
+ bitmapRep = OSX::NSBitmapImageRep.alloc.initWithCIImage(self)
0
+ blob = bitmapRep.representationUsingType_properties(format, properties)
0
+ blob.writeToFile_atomically(target, false)
0
+ def self.from(filepath)
0
+ raise Errno::ENOENT, "No such file or directory - #{filepath}" unless File.exists?(filepath)
0
+ OSX::CIImage.imageWithContentsOfURL(OSX::NSURL.fileURLWithPath(filepath))
Comments
No one has commented yet.