1from Cocoa import * 2from Quartz import * 3 4import objc 5 6_hazeRemovalKernel = None 7 8class MyHazeFilter (CIFilter): 9 inputImage = objc.ivar() 10 inputColor = objc.ivar() 11 inputDistance = objc.ivar() 12 inputSlope = objc.ivar() 13 14 15 @classmethod 16 def initialize(cls): 17 CIFilter.registerFilterName_constructor_classAttributes_( 18 "MyHazeRemover", cls, { 19 kCIAttributeFilterDisplayName: "Haze Remover" , 20 kCIAttributeFilterCategories: [ 21 kCICategoryColorAdjustment, kCICategoryVideo, 22 kCICategoryStillImage, kCICategoryInterlaced, 23 kCICategoryNonSquarePixels, 24 ], 25 "inputDistance": { 26 kCIAttributeMin: 0.0, 27 kCIAttributeMax: 1.0, 28 kCIAttributeSliderMin: 0.0, 29 kCIAttributeSliderMax: 0.7, 30 kCIAttributeDefault: 0.2, 31 kCIAttributeIdentity: 0.0, 32 kCIAttributeType: kCIAttributeTypeScalar, 33 }, 34 35 "inputSlope": { 36 kCIAttributeSliderMin: -0.01, 37 kCIAttributeSliderMax: 0.01, 38 kCIAttributeDefault: 0.0, 39 kCIAttributeIdentity: 0.0, 40 kCIAttributeType: kCIAttributeTypeScalar, 41 }, 42 43 "inputColor": { 44 kCIAttributeDefault: CIColor.colorWithRed_green_blue_alpha_( 45 1.0, 1.0, 1.0, 1.0), 46 }, 47 }) 48 49 @classmethod 50 def filterWithName_(cls, name): 51 filter = cls.alloc().init() 52 return filter 53 54 def init(self): 55 global _hazeRemovalKernel 56 57 if _hazeRemovalKernel is None: 58 bundle = NSBundle.bundleForClass_(type(self)) 59 code = open(bundle.pathForResource_ofType_( 60 "MyHazeRemoval", "cikernel"), 'rb').read() 61 kernels = CIKernel.kernelsWithString_(code) 62 _hazeRemovalKernel = kernels[0] 63 64 return super(MyHazeFilter, self).init() 65 66 def outputImage(self): 67 src = CISampler.samplerWithImage_(self.inputImage) 68 69 return self.apply_arguments_options_(_hazeRemovalKernel, 70 (src, self.inputColor, self.inputDistance, self.inputSlope), 71 { "definition": src.definition() }) 72