Skip to content

Commit

Permalink
Merge pull request #95 from SDWebImage/bugfix/monochome_colorspace
Browse files Browse the repository at this point in the history
Fix the issue when monochome colorspace cause the WebP encoding failed
  • Loading branch information
dreampiggy authored Jan 30, 2024
2 parents db46039 + f146fa4 commit 84e4376
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
6 changes: 6 additions & 0 deletions SDWebImageWebPCoder/Classes/SDImageWebPCoder.m
Original file line number Diff line number Diff line change
Expand Up @@ -885,6 +885,12 @@ - (nullable NSData *)sd_encodedWebpDataWithImage:(nullable CGImageRef)imageRef
uint8_t *rgba = NULL; // RGBA Buffer managed by CFData, don't call `free` on it, instead call `CFRelease` on `dataRef`
// We must prefer the input CGImage's color space, which may contains ICC profile
CGColorSpaceRef colorSpace = CGImageGetColorSpace(imageRef);
// We only supports RGB colorspace, filter the un-supported one (like Monochrome, CMYK, etc)
if (CGColorSpaceGetModel(colorSpace) != kCGColorSpaceModelRGB) {
// Ignore and convert, we don't know how to encode this colorspace directlly to WebP
// This may cause little visible difference because of colorpsace conversion
colorSpace = NULL;
}
if (!colorSpace) {
colorSpace = [SDImageCoderHelper colorSpaceGetDeviceRGB];
}
Expand Down
23 changes: 23 additions & 0 deletions Tests/SDWebImageWebPCoderTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,21 @@ - (void)test45WebPEncodingMaxFileSize {
XCTAssertLessThanOrEqual(dataWithLimit.length, maxFileSize);
}

- (void)test46WebPEncodingMonochrome {
CGSize size = CGSizeMake(512, 512);
SDGraphicsImageRendererFormat *format = [[SDGraphicsImageRendererFormat alloc] init];
format.scale = 1;
SDGraphicsImageRenderer *renderer = [[SDGraphicsImageRenderer alloc] initWithSize:size format:format];
UIColor *monochromeColor = UIColor.clearColor;
UIImage *monochromeImage = [renderer imageWithActions:^(CGContextRef ctx) {
[monochromeColor setFill];
CGContextFillRect(ctx, CGRectMake(0, 0, size.width, size.height));
}];
XCTAssert(monochromeImage);
NSData *data = [SDImageWebPCoder.sharedCoder encodedDataWithImage:monochromeImage format:SDImageFormatWebP options:nil];
XCTAssert(data);
}

- (void)testWebPDecodeDoesNotTriggerCACopyImage {
NSURL *staticWebPURL = [[NSBundle bundleForClass:[self class]] URLForResource:@"TestColorspaceStatic" withExtension:@"webp"];
NSData *data = [NSData dataWithContentsOfURL:staticWebPURL];
Expand Down Expand Up @@ -356,10 +371,18 @@ - (void)testWebPEncodingWithICCProfile {
CGFloat r1;
CGFloat g1;
CGFloat b1;
#if SD_UIKIT
[color1 getRed:&r1 green:&g1 blue:&b1 alpha:nil];
expect(255 * r1).beCloseToWithin(0, 5);
expect(255 * g1).beCloseToWithin(38, 5);
expect(255 * b1).beCloseToWithin(135, 5);
#else
@try {
[color1 getRed:&r1 green:&g1 blue:&b1 alpha:nil];
}
@catch (NSException *exception) {}
expect(255 * r1).beCloseToWithin(0, 5);
#endif
}

@end
Expand Down

0 comments on commit 84e4376

Please sign in to comment.