Fix the bitmapInfo support for greyscale color, update test case
This commit is contained in:
parent
34236ac522
commit
1ed76f4f47
|
@ -57,9 +57,85 @@ static inline CGRect SDCGRectFitWithScaleMode(CGRect rect, CGSize size, SDImageS
|
|||
return rect;
|
||||
}
|
||||
|
||||
static inline UIColor * SDGetColorFromGrayscale(Pixel_88 pixel) {
|
||||
CGFloat w = pixel[0] / 255.0;
|
||||
CGFloat a = pixel[1] / 255.0;
|
||||
static inline UIColor * SDGetColorFromGrayscale(Pixel_88 pixel, CGBitmapInfo bitmapInfo) {
|
||||
// Get alpha info, byteOrder info
|
||||
CGImageAlphaInfo alphaInfo = bitmapInfo & kCGBitmapAlphaInfoMask;
|
||||
CGBitmapInfo byteOrderInfo = bitmapInfo & kCGBitmapByteOrderMask;
|
||||
CGFloat w = 0, a = 1;
|
||||
|
||||
BOOL byteOrderNormal = NO;
|
||||
switch (byteOrderInfo) {
|
||||
case kCGBitmapByteOrderDefault: {
|
||||
byteOrderNormal = YES;
|
||||
} break;
|
||||
case kCGBitmapByteOrder32Little: {
|
||||
} break;
|
||||
case kCGBitmapByteOrder32Big: {
|
||||
byteOrderNormal = YES;
|
||||
} break;
|
||||
default: break;
|
||||
}
|
||||
switch (alphaInfo) {
|
||||
case kCGImageAlphaPremultipliedFirst:
|
||||
case kCGImageAlphaFirst: {
|
||||
if (byteOrderNormal) {
|
||||
// AW
|
||||
a = pixel[0] / 255.0;
|
||||
w = pixel[1] / 255.0;
|
||||
} else {
|
||||
// WA
|
||||
w = pixel[0] / 255.0;
|
||||
a = pixel[1] / 255.0;
|
||||
}
|
||||
}
|
||||
break;
|
||||
case kCGImageAlphaPremultipliedLast:
|
||||
case kCGImageAlphaLast: {
|
||||
if (byteOrderNormal) {
|
||||
// WA
|
||||
w = pixel[0] / 255.0;
|
||||
a = pixel[1] / 255.0;
|
||||
} else {
|
||||
// AW
|
||||
a = pixel[0] / 255.0;
|
||||
w = pixel[1] / 255.0;
|
||||
}
|
||||
}
|
||||
break;
|
||||
case kCGImageAlphaNone: {
|
||||
// W
|
||||
w = pixel[0] / 255.0;
|
||||
}
|
||||
break;
|
||||
case kCGImageAlphaNoneSkipLast: {
|
||||
if (byteOrderNormal) {
|
||||
// WX
|
||||
w = pixel[0] / 255.0;
|
||||
} else {
|
||||
// XW
|
||||
a = pixel[1] / 255.0;
|
||||
}
|
||||
}
|
||||
break;
|
||||
case kCGImageAlphaNoneSkipFirst: {
|
||||
if (byteOrderNormal) {
|
||||
// XW
|
||||
a = pixel[1] / 255.0;
|
||||
} else {
|
||||
// WX
|
||||
a = pixel[0] / 255.0;
|
||||
}
|
||||
}
|
||||
break;
|
||||
case kCGImageAlphaOnly: {
|
||||
// A
|
||||
a = pixel[0] / 255.0;
|
||||
}
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
return [UIColor colorWithWhite:w alpha:a];
|
||||
}
|
||||
|
||||
|
@ -489,8 +565,8 @@ static inline CGImageRef _Nullable SDCreateCGImageFromCIImage(CIImage * _Nonnull
|
|||
CFRelease(data);
|
||||
CGImageRelease(imageRef);
|
||||
// Convert to color
|
||||
return SDGetColorFromGrayscale(pixel);
|
||||
} else {
|
||||
return SDGetColorFromGrayscale(pixel, bitmapInfo);
|
||||
} else if (components == 3 || components == 4) {
|
||||
// RGB/RGBA
|
||||
Pixel_8888 pixel = {0};
|
||||
CFDataGetBytes(data, range, pixel);
|
||||
|
@ -498,6 +574,9 @@ static inline CGImageRef _Nullable SDCreateCGImageFromCIImage(CIImage * _Nonnull
|
|||
CGImageRelease(imageRef);
|
||||
// Convert to color
|
||||
return SDGetColorFromRGBA(pixel, bitmapInfo);
|
||||
} else {
|
||||
NSLog(@"Unsupported components: %zu", components);
|
||||
return nil;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -567,17 +646,21 @@ static inline CGImageRef _Nullable SDCreateCGImageFromCIImage(CIImage * _Nonnull
|
|||
UIColor *color;
|
||||
if (components == 2) {
|
||||
Pixel_88 pixel = {pixels[index], pixel[index+1]};
|
||||
color = SDGetColorFromGrayscale(pixel);
|
||||
color = SDGetColorFromGrayscale(pixel, bitmapInfo);
|
||||
} else {
|
||||
if (components == 3) {
|
||||
Pixel_8888 pixel = {pixels[index], pixels[index+1], pixels[index+2], 0};
|
||||
color = SDGetColorFromRGBA(pixel, bitmapInfo);
|
||||
} else {
|
||||
} else if (components == 4) {
|
||||
Pixel_8888 pixel = {pixels[index], pixels[index+1], pixels[index+2], pixels[index+3]};
|
||||
color = SDGetColorFromRGBA(pixel, bitmapInfo);
|
||||
} else {
|
||||
NSLog(@"Unsupported components: %zu", components);
|
||||
}
|
||||
}
|
||||
[colors addObject:color];
|
||||
if (color) {
|
||||
[colors addObject:color];
|
||||
}
|
||||
}
|
||||
CFRelease(data);
|
||||
CGImageRelease(imageRef);
|
||||
|
|
|
@ -127,11 +127,18 @@
|
|||
SDGraphicsImageRenderer *renderer = [[SDGraphicsImageRenderer alloc] initWithSize:size format:format];
|
||||
UIColor *color = UIColor.redColor;
|
||||
UIImage *image = [renderer imageWithActions:^(CGContextRef _Nonnull context) {
|
||||
[color setFill];
|
||||
CGContextSetFillColorWithColor(context, [color CGColor]);
|
||||
CGContextFillRect(context, CGRectMake(0, 0, size.width, size.height));
|
||||
}];
|
||||
expect(image.scale).equal(format.scale);
|
||||
expect([[image sd_colorAtPoint:CGPointMake(50, 50)].sd_hexString isEqualToString:color.sd_hexString]).beTruthy();
|
||||
expect([image sd_colorAtPoint:CGPointMake(50, 50)].sd_hexString).equal(color.sd_hexString);
|
||||
|
||||
UIColor *grayscaleColor = UIColor.blackColor;
|
||||
UIImage *grayscaleImage = [renderer imageWithActions:^(CGContextRef _Nonnull context) {
|
||||
CGContextSetFillColorWithColor(context, [grayscaleColor CGColor]);
|
||||
CGContextFillRect(context, CGRectMake(0, 0, size.width, size.height));
|
||||
}];
|
||||
expect([grayscaleImage sd_colorAtPoint:CGPointMake(50, 50)].sd_hexString).equal(grayscaleColor.sd_hexString);
|
||||
}
|
||||
|
||||
- (void)testSDScaledImageForKey {
|
||||
|
|
Loading…
Reference in New Issue