Skip to content

Commit

Permalink
s
Browse files Browse the repository at this point in the history
  • Loading branch information
msft-Jeyaram committed Jan 12, 2017
1 parent e736ca7 commit 59fa390
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 41 deletions.
58 changes: 27 additions & 31 deletions Frameworks/CoreImage/CIImage.mm
Original file line number Diff line number Diff line change
Expand Up @@ -75,14 +75,15 @@ @implementation CIImage
@Status Interoperable
*/
+ (CIImage*)emptyImage {
return [[[CIImage alloc] init] autorelease];
static CIImage* emptyImg = [CIImage new];
return emptyImg;
}

/**
@Status Interoperable
*/
- (CGRect)extent {
return self->_extent;
return _extent;
}

/**
Expand Down Expand Up @@ -137,12 +138,11 @@ - (instancetype)initWithCGImage:(CGImageRef)cgImage {
}

/**
@Status Stub
@Notes
@Status Caveat
@Notes options not supported
*/
- (instancetype)initWithCGImage:(CGImageRef)image options:(NSDictionary*)d {
UNIMPLEMENTED();
return StubReturn();
- (instancetype)initWithCGImage:(CGImageRef)image options:(NSDictionary*)options {
return [self initWithCGImage:image];
}

/**
Expand All @@ -157,12 +157,11 @@ - (instancetype)initWithImage:(UIImage*)image {
}

/**
@Status Stub
@Notes
@Status Caveat
@Notes options not supported
*/
- (instancetype)initWithImage:(UIImage*)image options:(NSDictionary*)options {
UNIMPLEMENTED();
return StubReturn();
return [self initWithImage:image];
}

/**
Expand All @@ -177,12 +176,11 @@ - (instancetype)initWithData:(NSData*)data {
}

/**
@Status Stub
@Notes
@Status Caveat
@Notes options not supported
*/
- (instancetype)initWithData:(NSData*)data options:(NSDictionary*)d {
UNIMPLEMENTED();
return StubReturn();
- (instancetype)initWithData:(NSData*)data options:(NSDictionary*)options {
return [self initWithData:data];
}

/**
Expand Down Expand Up @@ -211,12 +209,11 @@ + (CIImage*)imageWithData:(NSData*)data {
}

/**
@Status Stub
@Notes
@Status Caveat
@Notes options not supported
*/
+ (CIImage*)imageWithData:(NSData*)data options:(NSDictionary*)d {
UNIMPLEMENTED();
return StubReturn();
+ (CIImage*)imageWithData:(NSData*)data options:(NSDictionary*)options {
return [CIImage imageWithData:data];
}

/**
Expand All @@ -228,12 +225,11 @@ + (CIImage*)imageWithCGImage:(CGImageRef)image {
}

/**
@Status Stub
@Notes
@Status Caveat
@Notes options not supported
*/
+ (CIImage*)imageWithCGImage:(CGImageRef)image options:(NSDictionary*)d {
UNIMPLEMENTED();
return StubReturn();
+ (CIImage*)imageWithCGImage:(CGImageRef)image options:(NSDictionary*)options {
return [self imageWithCGImage:image];
}

/**
Expand Down Expand Up @@ -347,18 +343,18 @@ - (CIImage*)imageByApplyingTransform:(CGAffineTransform)matrix {
@Status Interoperable
*/
- (CIImage*)imageByCroppingToRect:(CGRect)rect {
if (self->_cgImage != nil) {
woc::unique_cf<CGImageRef> croppedImage{ CGImageCreateWithImageInRect(self->_cgImage.get(), rect) };
if (_cgImage != nil) {
woc::unique_cf<CGImageRef> croppedImage{ CGImageCreateWithImageInRect(_cgImage.get(), rect) };
return [CIImage imageWithCGImage:croppedImage.get()];
} else if (self->_color != nil) {
} else if (_color != nil) {
UIGraphicsBeginImageContext(rect.size);
CGContextRef context = UIGraphicsGetCurrentContext();
CIColor* color = static_cast<CIColor*>(self->_color);
CIColor* color = static_cast<CIColor*>(_color);
CGContextSetRGBFillColor(context, color.red, color.green, color.blue, color.alpha);
CGContextFillRect(context, rect);
woc::unique_cf<CGImageRef> imageRef{ CGBitmapContextCreateImage(context) };
UIGraphicsEndImageContext();
return [[[CIImage alloc] _initWithCGImage:imageRef.get() color:self->_color] autorelease];
return [[[CIImage alloc] _initWithCGImage:imageRef.get() color:_color] autorelease];
}

return [CIImage emptyImage];
Expand Down
2 changes: 1 addition & 1 deletion Frameworks/include/CIImageInternal.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@

@interface CIImage () {
woc::unique_cf<CGImageRef> _cgImage;
idretain _color;
StrongId<CIColor> _color;
CIFilter* _filter;
CGRect _extent;
}
Expand Down
18 changes: 9 additions & 9 deletions tests/unittests/CoreImage/CIContextTests.mm
Original file line number Diff line number Diff line change
Expand Up @@ -50,29 +50,29 @@ static CFMutableDataRef ObtainJPEGRepresentationFromCGImage(CGImageRef image) {
TEST(CoreImage, CGImageFromRect) {
SetCACompositor(new NullCompositor);
CIContext* context = [CIContext contextWithOptions:nil];
ASSERT_TRUE_MSG(context != nil, "Failed: CIContext is nil.");
ASSERT_OBJCNE(nil, context);

UIImage* photo = [UIImage imageNamed:getPathToFile(@"Photo2.jpg")];
ASSERT_TRUE(photo != nil);
ASSERT_OBJCNE(nil, photo);

CIImage* ciImage = [CIImage imageWithCGImage:photo.CGImage];
ASSERT_TRUE(ciImage != nil);
ASSERT_OBJCNE(nil, ciImage);
CGImageRef cgImage = [context createCGImage:ciImage fromRect:CGRectMake(300, 600, 200, 200)];
ASSERT_TRUE(cgImage != nullptr);
ASSERT_NE(nullptr, cgImage);

NSData* data = [NSData dataWithContentsOfFile:getPathToFile(@"CroppedPhoto2.jpg")];
ASSERT_TRUE(data != nil);
ASSERT_OBJCNE(nil, data);
CIImage* croppedPhotoCIImage = [CIImage imageWithData:data];
ASSERT_TRUE(croppedPhotoCIImage != nil);
ASSERT_OBJCNE(nil, croppedPhotoCIImage);

CGImageRef croppedPhoto = [context createCGImage:croppedPhotoCIImage fromRect:[croppedPhotoCIImage extent]];
ASSERT_TRUE(croppedPhoto != nullptr);
ASSERT_NE(nullptr, croppedPhoto);

CFMutableDataRef originalImageCropped = ObtainJPEGRepresentationFromCGImage(cgImage);
ASSERT_TRUE(originalImageCropped != nil);
ASSERT_NE(nil, originalImageCropped);

CFMutableDataRef croppedImage = ObtainJPEGRepresentationFromCGImage(croppedPhoto);
ASSERT_TRUE(croppedImage != nil);
ASSERT_NE(nil, croppedImage);

ASSERT_TRUE(CFEqual(originalImageCropped, croppedImage));
}

0 comments on commit 59fa390

Please sign in to comment.