13
13
@interface CardIOIplImage ()
14
14
15
15
@property (nonatomic , assign , readwrite ) IplImage *image;
16
+ @property (nonatomic , assign , readwrite ) CVImageBufferRef imageBuffer;
16
17
17
18
@end
18
19
@@ -26,8 +27,10 @@ + (CardIOIplImage *)imageWithSize:(CvSize)size depth:(int)depth channels:(int)ch
26
27
}
27
28
28
29
+ (CardIOIplImage *)imageFromYCbCrBuffer : (CVImageBufferRef)imageBuffer plane : (size_t )plane {
30
+ CVPixelBufferLockBaseAddress (imageBuffer, 0 );
29
31
char *planeBaseAddress = (char *)CVPixelBufferGetBaseAddressOfPlane (imageBuffer, plane);
30
-
32
+ // CVPixelBufferUnlocked in dealloc of CardIOIplImage to ensure lock of memory
33
+
31
34
size_t width = CVPixelBufferGetWidthOfPlane (imageBuffer, plane);
32
35
size_t height = CVPixelBufferGetHeightOfPlane (imageBuffer, plane);
33
36
size_t bytesPerRow = CVPixelBufferGetBytesPerRowOfPlane (imageBuffer, plane);
@@ -37,16 +40,25 @@ + (CardIOIplImage *)imageFromYCbCrBuffer:(CVImageBufferRef)imageBuffer plane:(si
37
40
colocatedImage->imageData = planeBaseAddress;
38
41
colocatedImage->widthStep = (int )bytesPerRow;
39
42
40
- return [self imageWithIplImage : colocatedImage];
43
+ return [[ self alloc ] initWithIplImage : colocatedImage imageBuffer: imageBuffer ];
41
44
}
42
45
43
46
+ (CardIOIplImage *)imageWithIplImage : (IplImage *)anImage {
44
47
return [[self alloc ] initWithIplImage: anImage];
45
48
}
46
49
47
50
- (id )initWithIplImage : (IplImage *)anImage {
51
+ return [self initWithIplImage: anImage imageBuffer: NULL ];
52
+ }
53
+
54
+ - (id )initWithIplImage : (IplImage *)anImage imageBuffer : (CVImageBufferRef)imageBuffer {
48
55
if ((self = [super init ])) {
49
56
self.image = anImage;
57
+
58
+ self.imageBuffer = imageBuffer;
59
+ if (imageBuffer != NULL ) {
60
+ CFRetain (imageBuffer);
61
+ }
50
62
}
51
63
return self;
52
64
}
@@ -78,6 +90,10 @@ - (NSString *)description {
78
90
79
91
- (void )dealloc {
80
92
cvReleaseImage (&image);
93
+ if (_imageBuffer != nil ) {
94
+ CVPixelBufferUnlockBaseAddress (_imageBuffer, 0 );
95
+ CFRelease (_imageBuffer);
96
+ }
81
97
}
82
98
83
99
- (IplImage *)image {
0 commit comments