Skip to content

Commit 2460818

Browse files
authored
Merge pull request #53 from SDWebImage/bugfix_grayscale
Fix the issue when WebP Encoding grayscale image with only 1 channel
2 parents cdf6ce8 + 1903ac2 commit 2460818

File tree

4 files changed

+55
-12
lines changed

4 files changed

+55
-12
lines changed

SDWebImageWebPCoder/Classes/SDImageWebPCoder.m

+22-12
Original file line numberDiff line numberDiff line change
@@ -736,6 +736,9 @@ - (nullable NSData *)sd_encodedWebpDataWithImage:(nullable CGImageRef)imageRef
736736
}
737737

738738
size_t bytesPerRow = CGImageGetBytesPerRow(imageRef);
739+
size_t bitsPerComponent = CGImageGetBitsPerComponent(imageRef);
740+
size_t bitsPerPixel = CGImageGetBitsPerPixel(imageRef);
741+
size_t components = bitsPerPixel / bitsPerComponent;
739742
CGBitmapInfo bitmapInfo = CGImageGetBitmapInfo(imageRef);
740743
CGImageAlphaInfo alphaInfo = bitmapInfo & kCGBitmapAlphaInfoMask;
741744
CGBitmapInfo byteOrderInfo = bitmapInfo & kCGBitmapByteOrderMask;
@@ -763,10 +766,15 @@ - (nullable NSData *)sd_encodedWebpDataWithImage:(nullable CGImageRef)imageRef
763766
if (!dataRef) {
764767
return nil;
765768
}
769+
// Check colorSpace is RGB/RGBA
770+
CGColorSpaceRef colorSpace = CGImageGetColorSpace(imageRef);
771+
BOOL isRGB = CGColorSpaceGetModel(colorSpace) == kCGColorSpaceModelRGB;
766772

767773
uint8_t *rgba = NULL; // RGBA Buffer managed by CFData, don't call `free` on it, instead call `CFRelease` on `dataRef`
768774
// We could not assume that input CGImage's color mode is always RGB888/RGBA8888. Convert all other cases to target color mode using vImage
769-
if (byteOrderNormal && ((alphaInfo == kCGImageAlphaNone) || (alphaInfo == kCGImageAlphaLast))) {
775+
BOOL isRGB888 = isRGB && byteOrderNormal && alphaInfo == kCGImageAlphaNone && components == 3;
776+
BOOL isRGBA8888 = isRGB && byteOrderNormal && alphaInfo == kCGImageAlphaLast && components == 4;
777+
if (isRGB888 || isRGBA8888) {
770778
// If the input CGImage is already RGB888/RGBA8888
771779
rgba = (uint8_t *)CFDataGetBytePtr(dataRef);
772780
} else {
@@ -775,10 +783,11 @@ - (nullable NSData *)sd_encodedWebpDataWithImage:(nullable CGImageRef)imageRef
775783
vImage_Error error = kvImageNoError;
776784

777785
vImage_CGImageFormat srcFormat = {
778-
.bitsPerComponent = (uint32_t)CGImageGetBitsPerComponent(imageRef),
779-
.bitsPerPixel = (uint32_t)CGImageGetBitsPerPixel(imageRef),
780-
.colorSpace = CGImageGetColorSpace(imageRef),
781-
.bitmapInfo = bitmapInfo
786+
.bitsPerComponent = (uint32_t)bitsPerComponent,
787+
.bitsPerPixel = (uint32_t)bitsPerPixel,
788+
.colorSpace = colorSpace,
789+
.bitmapInfo = bitmapInfo,
790+
.renderingIntent = CGImageGetRenderingIntent(imageRef)
782791
};
783792
vImage_CGImageFormat destFormat = {
784793
.bitsPerComponent = 8,
@@ -793,14 +802,15 @@ - (nullable NSData *)sd_encodedWebpDataWithImage:(nullable CGImageRef)imageRef
793802
return nil;
794803
}
795804

796-
vImage_Buffer src = {
797-
.data = (uint8_t *)CFDataGetBytePtr(dataRef),
798-
.width = width,
799-
.height = height,
800-
.rowBytes = bytesPerRow
801-
};
802-
vImage_Buffer dest;
805+
vImage_Buffer src;
806+
error = vImageBuffer_InitWithCGImage(&src, &srcFormat, nil, imageRef, kvImageNoFlags);
807+
if (error != kvImageNoError) {
808+
vImageConverter_Release(convertor);
809+
CFRelease(dataRef);
810+
return nil;
811+
}
803812

813+
vImage_Buffer dest;
804814
error = vImageBuffer_Init(&dest, height, width, destFormat.bitsPerPixel, kvImageNoFlags);
805815
if (error != kvImageNoError) {
806816
vImageConverter_Release(convertor);

Tests/Images/TestImageGrayscale.jpg

1.27 MB
Loading

Tests/SDWebImageWebPCoderTests.m

+29
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,35 @@ - (void)testEncodingSettingsIncorrectType {
292292
expect(config.method).to.equal(4);
293293
}
294294

295+
296+
- (void)testEncodingGrayscaleImage {
297+
NSURL *grayscaleImageURL = [[NSBundle bundleForClass:[self class]] URLForResource:@"TestImageGrayscale" withExtension:@"jpg"];
298+
NSData *grayscaleImageData = [NSData dataWithContentsOfURL:grayscaleImageURL];
299+
UIImage *grayscaleImage = [[UIImage alloc] initWithData:grayscaleImageData];
300+
expect(grayscaleImage).notTo.beNil();
301+
302+
NSData *webpData = [SDImageWebPCoder.sharedCoder encodedDataWithImage:grayscaleImage format:SDImageFormatWebP options:nil];
303+
expect(webpData).notTo.beNil();
304+
305+
UIImage *decodedImage = [UIImage sd_imageWithData:webpData];
306+
expect(decodedImage).notTo.beNil();
307+
308+
// Sample to verify that encoded WebP image's color is correct.
309+
// The wrong case before bugfix is that each column color will repeats 3 times.
310+
CGPoint point1 = CGPointMake(271, 764);
311+
CGPoint point2 = CGPointMake(round(point1.x + decodedImage.size.width / 3), point1.y);
312+
UIColor *color1 = [decodedImage sd_colorAtPoint:point1];
313+
UIColor *color2 = [decodedImage sd_colorAtPoint:point2];
314+
CGFloat r1, r2;
315+
CGFloat g1, g2;
316+
CGFloat b1, b2;
317+
[color1 getRed:&r1 green:&g1 blue:&b1 alpha:nil];
318+
[color2 getRed:&r2 green:&g2 blue:&b2 alpha:nil];
319+
expect(255 * r1).notTo.equal(255 * r2);
320+
expect(255 * g1).notTo.equal(255 * g2);
321+
expect(255 * b1).notTo.equal(255 * b2);
322+
}
323+
295324
@end
296325

297326
@implementation SDWebImageWebPCoderTests (Helpers)

Tests/SDWebImageWebPCoderTests.xcodeproj/project.pbxproj

+4
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
/* Begin PBXBuildFile section */
1010
0EF5B6264833B7BC20894578 /* Pods_SDWebImageWebPCoderTests.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 46F21AD7D1692EBAC4D0FF33 /* Pods_SDWebImageWebPCoderTests.framework */; };
1111
3219F3B2228B0453003822A6 /* TestImageBlendAnimated.webp in Resources */ = {isa = PBXBuildFile; fileRef = 3219F3B1228B0453003822A6 /* TestImageBlendAnimated.webp */; };
12+
325E268E25C82BE1000B807B /* TestImageGrayscale.jpg in Resources */ = {isa = PBXBuildFile; fileRef = 325E268D25C82BE1000B807B /* TestImageGrayscale.jpg */; };
1213
808C918E213FD131004B0F7C /* SDWebImageWebPCoderTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 808C918D213FD131004B0F7C /* SDWebImageWebPCoderTests.m */; };
1314
808C919C213FD2B2004B0F7C /* TestImageStatic.webp in Resources */ = {isa = PBXBuildFile; fileRef = 808C919A213FD2B2004B0F7C /* TestImageStatic.webp */; };
1415
808C919D213FD2B2004B0F7C /* TestImageAnimated.webp in Resources */ = {isa = PBXBuildFile; fileRef = 808C919B213FD2B2004B0F7C /* TestImageAnimated.webp */; };
@@ -17,6 +18,7 @@
1718
/* Begin PBXFileReference section */
1819
28D8AA3D3015E075692FD3E3 /* Pods-SDWebImageWebPCoderTests.debug.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-SDWebImageWebPCoderTests.debug.xcconfig"; path = "../Pods/Target Support Files/Pods-SDWebImageWebPCoderTests/Pods-SDWebImageWebPCoderTests.debug.xcconfig"; sourceTree = "<group>"; };
1920
3219F3B1228B0453003822A6 /* TestImageBlendAnimated.webp */ = {isa = PBXFileReference; lastKnownFileType = file; path = TestImageBlendAnimated.webp; sourceTree = "<group>"; };
21+
325E268D25C82BE1000B807B /* TestImageGrayscale.jpg */ = {isa = PBXFileReference; lastKnownFileType = image.jpeg; path = TestImageGrayscale.jpg; sourceTree = "<group>"; };
2022
46F21AD7D1692EBAC4D0FF33 /* Pods_SDWebImageWebPCoderTests.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Pods_SDWebImageWebPCoderTests.framework; sourceTree = BUILT_PRODUCTS_DIR; };
2123
808C918B213FD130004B0F7C /* SDWebImageWebPCoderTests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = SDWebImageWebPCoderTests.xctest; sourceTree = BUILT_PRODUCTS_DIR; };
2224
808C918D213FD131004B0F7C /* SDWebImageWebPCoderTests.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = SDWebImageWebPCoderTests.m; sourceTree = "<group>"; };
@@ -78,6 +80,7 @@
7880
808C9199213FD2B2004B0F7C /* Images */ = {
7981
isa = PBXGroup;
8082
children = (
83+
325E268D25C82BE1000B807B /* TestImageGrayscale.jpg */,
8184
808C919A213FD2B2004B0F7C /* TestImageStatic.webp */,
8285
808C919B213FD2B2004B0F7C /* TestImageAnimated.webp */,
8386
3219F3B1228B0453003822A6 /* TestImageBlendAnimated.webp */,
@@ -154,6 +157,7 @@
154157
3219F3B2228B0453003822A6 /* TestImageBlendAnimated.webp in Resources */,
155158
808C919D213FD2B2004B0F7C /* TestImageAnimated.webp in Resources */,
156159
808C919C213FD2B2004B0F7C /* TestImageStatic.webp in Resources */,
160+
325E268E25C82BE1000B807B /* TestImageGrayscale.jpg in Resources */,
157161
);
158162
runOnlyForDeploymentPostprocessing = 0;
159163
};

0 commit comments

Comments
 (0)