Merge pull request #3372 from dreampiggy/bugfix_color_at_point_grayscale
Fix sd_colorAtPoint and sd_colorsWithRect support for grayscale image (white and alpha)
This commit is contained in:
commit
3277824d43
|
@ -57,7 +57,89 @@ static inline CGRect SDCGRectFitWithScaleMode(CGRect rect, CGSize size, SDImageS
|
|||
return rect;
|
||||
}
|
||||
|
||||
static inline UIColor * SDGetColorFromPixel(Pixel_8888 pixel, CGBitmapInfo bitmapInfo) {
|
||||
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];
|
||||
}
|
||||
|
||||
static inline UIColor * SDGetColorFromRGBA(Pixel_8888 pixel, CGBitmapInfo bitmapInfo) {
|
||||
// Get alpha info, byteOrder info
|
||||
CGImageAlphaInfo alphaInfo = bitmapInfo & kCGBitmapAlphaInfoMask;
|
||||
CGBitmapInfo byteOrderInfo = bitmapInfo & kCGBitmapByteOrderMask;
|
||||
|
@ -470,18 +552,32 @@ static inline CGImageRef _Nullable SDCreateCGImageFromCIImage(CIImage * _Nonnull
|
|||
size_t components = CGImageGetBitsPerPixel(imageRef) / CGImageGetBitsPerComponent(imageRef);
|
||||
CGBitmapInfo bitmapInfo = CGImageGetBitmapInfo(imageRef);
|
||||
|
||||
CFRange range = CFRangeMake(bytesPerRow * point.y + components * point.x, 4);
|
||||
CFRange range = CFRangeMake(bytesPerRow * point.y + components * point.x, components);
|
||||
if (CFDataGetLength(data) < range.location + range.length) {
|
||||
CFRelease(data);
|
||||
CGImageRelease(imageRef);
|
||||
return nil;
|
||||
}
|
||||
Pixel_8888 pixel = {0};
|
||||
CFDataGetBytes(data, range, pixel);
|
||||
CFRelease(data);
|
||||
CGImageRelease(imageRef);
|
||||
// Convert to color
|
||||
return SDGetColorFromPixel(pixel, bitmapInfo);
|
||||
// greyscale
|
||||
if (components == 2) {
|
||||
Pixel_88 pixel = {0};
|
||||
CFDataGetBytes(data, range, pixel);
|
||||
CFRelease(data);
|
||||
CGImageRelease(imageRef);
|
||||
// Convert to color
|
||||
return SDGetColorFromGrayscale(pixel, bitmapInfo);
|
||||
} else if (components == 3 || components == 4) {
|
||||
// RGB/RGBA
|
||||
Pixel_8888 pixel = {0};
|
||||
CFDataGetBytes(data, range, pixel);
|
||||
CFRelease(data);
|
||||
CGImageRelease(imageRef);
|
||||
// Convert to color
|
||||
return SDGetColorFromRGBA(pixel, bitmapInfo);
|
||||
} else {
|
||||
NSLog(@"Unsupported components: %zu", components);
|
||||
return nil;
|
||||
}
|
||||
}
|
||||
|
||||
- (nullable NSArray<UIColor *> *)sd_colorsWithRect:(CGRect)rect {
|
||||
|
@ -539,17 +635,32 @@ static inline CGImageRef _Nullable SDCreateCGImageFromCIImage(CIImage * _Nonnull
|
|||
// Convert to color
|
||||
CGBitmapInfo bitmapInfo = CGImageGetBitmapInfo(imageRef);
|
||||
NSMutableArray<UIColor *> *colors = [NSMutableArray arrayWithCapacity:CGRectGetWidth(rect) * CGRectGetHeight(rect)];
|
||||
for (size_t index = start; index < end; index += 4) {
|
||||
for (size_t index = start; index < end; index += components) {
|
||||
if (index >= row * bytesPerRow + col * components) {
|
||||
// Index beyond the end of current row, go next row
|
||||
row++;
|
||||
index = row * bytesPerRow + CGRectGetMinX(rect) * components;
|
||||
index -= 4;
|
||||
index -= components;
|
||||
continue;
|
||||
}
|
||||
Pixel_8888 pixel = {pixels[index], pixels[index+1], pixels[index+2], pixels[index+3]};
|
||||
UIColor *color = SDGetColorFromPixel(pixel, bitmapInfo);
|
||||
[colors addObject:color];
|
||||
UIColor *color;
|
||||
if (components == 2) {
|
||||
Pixel_88 pixel = {pixels[index], pixel[index+1]};
|
||||
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 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);
|
||||
}
|
||||
}
|
||||
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