This repository was archived by the owner on Aug 8, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathYUCIHistogramEqualization.m
executable file
·69 lines (56 loc) · 2.46 KB
/
YUCIHistogramEqualization.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
//
// YUCIHistogramEqualization.m
// Pods
//
// Created by YuAo on 2/15/16.
//
//
#import "YUCIHistogramEqualization.h"
#import "YUCIFilterConstructor.h"
#import <Accelerate/Accelerate.h>
@interface YUCIHistogramEqualization ()
@property (nonatomic, strong) CIContext *context;
@end
@implementation YUCIHistogramEqualization
+ (void)load {
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
@autoreleasepool {
if ([CIFilter respondsToSelector:@selector(registerFilterName:constructor:classAttributes:)]) {
[CIFilter registerFilterName:NSStringFromClass([YUCIHistogramEqualization class])
constructor:[YUCIFilterConstructor constructor]
classAttributes:@{kCIAttributeFilterCategories: @[kCICategoryStillImage,kCICategoryVideo,kCICategoryColorAdjustment],
kCIAttributeFilterDisplayName: @"Histogram Equalization"}];
}
}
});
}
- (CIContext *)context {
if (!_context) {
_context = [CIContext contextWithOptions:@{kCIContextWorkingColorSpace: CFBridgingRelease(CGColorSpaceCreateWithName(kCGColorSpaceSRGB))}];
}
return _context;
}
- (CIImage *)outputImage {
if (!self.inputImage) {
return nil;
}
ptrdiff_t rowBytes = self.inputImage.extent.size.width * 4; // ARGB has 4 components
uint8_t *byteBuffer = calloc(rowBytes * self.inputImage.extent.size.height, sizeof(uint8_t)); // Buffer to render into
[self.context render:self.inputImage
toBitmap:byteBuffer
rowBytes:rowBytes
bounds:self.inputImage.extent
format:kCIFormatARGB8
colorSpace:self.context.workingColorSpace];
vImage_Buffer vImageBuffer;
vImageBuffer.data = byteBuffer;
vImageBuffer.width = self.inputImage.extent.size.width;
vImageBuffer.height = self.inputImage.extent.size.height;
vImageBuffer.rowBytes = rowBytes;
vImageEqualization_ARGB8888(&vImageBuffer, &vImageBuffer, kvImageNoFlags);
NSData *bitmapData = [NSData dataWithBytesNoCopy:vImageBuffer.data length:vImageBuffer.rowBytes * vImageBuffer.height freeWhenDone:YES];
CIImage *result = [[CIImage alloc] initWithBitmapData:bitmapData bytesPerRow:vImageBuffer.rowBytes size:CGSizeMake(vImageBuffer.width, vImageBuffer.height) format:kCIFormatARGB8 colorSpace:self.context.workingColorSpace];
return result;
}
@end