Skip to content

Commit 84e4376

Browse files
authored
Merge pull request #95 from SDWebImage/bugfix/monochome_colorspace
Fix the issue when monochome colorspace cause the WebP encoding failed
2 parents db46039 + f146fa4 commit 84e4376

File tree

2 files changed

+29
-0
lines changed

2 files changed

+29
-0
lines changed

SDWebImageWebPCoder/Classes/SDImageWebPCoder.m

+6
Original file line numberDiff line numberDiff line change
@@ -885,6 +885,12 @@ - (nullable NSData *)sd_encodedWebpDataWithImage:(nullable CGImageRef)imageRef
885885
uint8_t *rgba = NULL; // RGBA Buffer managed by CFData, don't call `free` on it, instead call `CFRelease` on `dataRef`
886886
// We must prefer the input CGImage's color space, which may contains ICC profile
887887
CGColorSpaceRef colorSpace = CGImageGetColorSpace(imageRef);
888+
// We only supports RGB colorspace, filter the un-supported one (like Monochrome, CMYK, etc)
889+
if (CGColorSpaceGetModel(colorSpace) != kCGColorSpaceModelRGB) {
890+
// Ignore and convert, we don't know how to encode this colorspace directlly to WebP
891+
// This may cause little visible difference because of colorpsace conversion
892+
colorSpace = NULL;
893+
}
888894
if (!colorSpace) {
889895
colorSpace = [SDImageCoderHelper colorSpaceGetDeviceRGB];
890896
}

Tests/SDWebImageWebPCoderTests.m

+23
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,21 @@ - (void)test45WebPEncodingMaxFileSize {
218218
XCTAssertLessThanOrEqual(dataWithLimit.length, maxFileSize);
219219
}
220220

221+
- (void)test46WebPEncodingMonochrome {
222+
CGSize size = CGSizeMake(512, 512);
223+
SDGraphicsImageRendererFormat *format = [[SDGraphicsImageRendererFormat alloc] init];
224+
format.scale = 1;
225+
SDGraphicsImageRenderer *renderer = [[SDGraphicsImageRenderer alloc] initWithSize:size format:format];
226+
UIColor *monochromeColor = UIColor.clearColor;
227+
UIImage *monochromeImage = [renderer imageWithActions:^(CGContextRef ctx) {
228+
[monochromeColor setFill];
229+
CGContextFillRect(ctx, CGRectMake(0, 0, size.width, size.height));
230+
}];
231+
XCTAssert(monochromeImage);
232+
NSData *data = [SDImageWebPCoder.sharedCoder encodedDataWithImage:monochromeImage format:SDImageFormatWebP options:nil];
233+
XCTAssert(data);
234+
}
235+
221236
- (void)testWebPDecodeDoesNotTriggerCACopyImage {
222237
NSURL *staticWebPURL = [[NSBundle bundleForClass:[self class]] URLForResource:@"TestColorspaceStatic" withExtension:@"webp"];
223238
NSData *data = [NSData dataWithContentsOfURL:staticWebPURL];
@@ -356,10 +371,18 @@ - (void)testWebPEncodingWithICCProfile {
356371
CGFloat r1;
357372
CGFloat g1;
358373
CGFloat b1;
374+
#if SD_UIKIT
359375
[color1 getRed:&r1 green:&g1 blue:&b1 alpha:nil];
360376
expect(255 * r1).beCloseToWithin(0, 5);
361377
expect(255 * g1).beCloseToWithin(38, 5);
362378
expect(255 * b1).beCloseToWithin(135, 5);
379+
#else
380+
@try {
381+
[color1 getRed:&r1 green:&g1 blue:&b1 alpha:nil];
382+
}
383+
@catch (NSException *exception) {}
384+
expect(255 * r1).beCloseToWithin(0, 5);
385+
#endif
363386
}
364387

365388
@end

0 commit comments

Comments
 (0)