Skip to content

Commit 0713611

Browse files
authored
Merge pull request #14 from SDWebImage/bugfix_webp_blend_canvas
Fix the logic for Animated WebP images which contains a subsequence blend from the first frame, impact the `SDAnimatedImage`
2 parents e5c9608 + 5c4c284 commit 0713611

File tree

4 files changed

+72
-10
lines changed

4 files changed

+72
-10
lines changed

SDWebImageWebPCoder.xcodeproj/project.pbxproj

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
/* Begin PBXBuildFile section */
1010
0EF5B6264833B7BC20894578 /* Pods_SDWebImageWebPCoderTests.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 46F21AD7D1692EBAC4D0FF33 /* Pods_SDWebImageWebPCoderTests.framework */; };
11+
3219F3B2228B0453003822A6 /* TestImageBlendAnimated.webp in Resources */ = {isa = PBXBuildFile; fileRef = 3219F3B1228B0453003822A6 /* TestImageBlendAnimated.webp */; };
1112
806E77B32136A2E900A316D2 /* UIImage+WebP.m in Sources */ = {isa = PBXBuildFile; fileRef = 806E77AA2136A2E900A316D2 /* UIImage+WebP.m */; };
1213
806E77B42136A2E900A316D2 /* SDImageWebPCoder.h in Headers */ = {isa = PBXBuildFile; fileRef = 806E77AB2136A2E900A316D2 /* SDImageWebPCoder.h */; settings = {ATTRIBUTES = (Public, ); }; };
1314
806E77B62136A2E900A316D2 /* UIImage+WebP.h in Headers */ = {isa = PBXBuildFile; fileRef = 806E77AD2136A2E900A316D2 /* UIImage+WebP.h */; settings = {ATTRIBUTES = (Public, ); }; };
@@ -44,6 +45,7 @@
4445
/* Begin PBXFileReference section */
4546
28D8AA3D3015E075692FD3E3 /* Pods-SDWebImageWebPCoderTests.debug.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-SDWebImageWebPCoderTests.debug.xcconfig"; path = "SDWebImageWebPCoderTests/Pods/Target Support Files/Pods-SDWebImageWebPCoderTests/Pods-SDWebImageWebPCoderTests.debug.xcconfig"; sourceTree = "<group>"; };
4647
3217BE7B220547EB003D0310 /* SDWebImageWebPCoder.modulemap */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = "sourcecode.module-map"; path = SDWebImageWebPCoder.modulemap; sourceTree = "<group>"; };
48+
3219F3B1228B0453003822A6 /* TestImageBlendAnimated.webp */ = {isa = PBXFileReference; lastKnownFileType = file; path = TestImageBlendAnimated.webp; sourceTree = "<group>"; };
4749
46F21AD7D1692EBAC4D0FF33 /* Pods_SDWebImageWebPCoderTests.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Pods_SDWebImageWebPCoderTests.framework; sourceTree = BUILT_PRODUCTS_DIR; };
4850
806E779D2136A1C000A316D2 /* SDWebImageWebPCoder.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = SDWebImageWebPCoder.framework; sourceTree = BUILT_PRODUCTS_DIR; };
4951
806E77AA2136A2E900A316D2 /* UIImage+WebP.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "UIImage+WebP.m"; sourceTree = "<group>"; };
@@ -205,6 +207,7 @@
205207
children = (
206208
808C919A213FD2B2004B0F7C /* TestImageStatic.webp */,
207209
808C919B213FD2B2004B0F7C /* TestImageAnimated.webp */,
210+
3219F3B1228B0453003822A6 /* TestImageBlendAnimated.webp */,
208211
);
209212
path = Images;
210213
sourceTree = "<group>";
@@ -453,6 +456,7 @@
453456
isa = PBXResourcesBuildPhase;
454457
buildActionMask = 2147483647;
455458
files = (
459+
3219F3B2228B0453003822A6 /* TestImageBlendAnimated.webp in Resources */,
456460
808C919D213FD2B2004B0F7C /* TestImageAnimated.webp in Resources */,
457461
808C919C213FD2B2004B0F7C /* TestImageStatic.webp in Resources */,
458462
);
@@ -620,8 +624,14 @@
620624
"DEBUG=1",
621625
"$(inherited)",
622626
);
623-
"GCC_PREPROCESSOR_DEFINITIONS[sdk=watchos*]" = "WEBP_USE_INTRINSICS=1 $(inherited)";
624-
"GCC_PREPROCESSOR_DEFINITIONS[sdk=watchsimulator*]" = "WEBP_USE_INTRINSICS=1 $(inherited)";
627+
"GCC_PREPROCESSOR_DEFINITIONS[sdk=watchos*]" = (
628+
"WEBP_USE_INTRINSICS=1",
629+
"$(inherited)",
630+
);
631+
"GCC_PREPROCESSOR_DEFINITIONS[sdk=watchsimulator*]" = (
632+
"WEBP_USE_INTRINSICS=1",
633+
"$(inherited)",
634+
);
625635
GCC_WARN_64_TO_32_BIT_CONVERSION = YES;
626636
GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR;
627637
GCC_WARN_UNDECLARED_SELECTOR = YES;
@@ -689,8 +699,14 @@
689699
GCC_C_LANGUAGE_STANDARD = gnu11;
690700
GCC_NO_COMMON_BLOCKS = YES;
691701
GCC_PREPROCESSOR_DEFINITIONS = "$(inherited)";
692-
"GCC_PREPROCESSOR_DEFINITIONS[sdk=watchos*]" = "WEBP_USE_INTRINSICS=1 $(inherited)";
693-
"GCC_PREPROCESSOR_DEFINITIONS[sdk=watchsimulator*]" = "WEBP_USE_INTRINSICS=1 $(inherited)";
702+
"GCC_PREPROCESSOR_DEFINITIONS[sdk=watchos*]" = (
703+
"WEBP_USE_INTRINSICS=1",
704+
"$(inherited)",
705+
);
706+
"GCC_PREPROCESSOR_DEFINITIONS[sdk=watchsimulator*]" = (
707+
"WEBP_USE_INTRINSICS=1",
708+
"$(inherited)",
709+
);
694710
GCC_WARN_64_TO_32_BIT_CONVERSION = YES;
695711
GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR;
696712
GCC_WARN_UNDECLARED_SELECTOR = YES;

SDWebImageWebPCoder/Classes/SDImageWebPCoder.m

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,8 @@ @interface SDWebPCoderFrame : NSObject
4646
@property (nonatomic, assign) NSUInteger offsetY; // Frame origin.y in canvas (left-bottom based)
4747
@property (nonatomic, assign) BOOL hasAlpha; // Whether frame contains alpha
4848
@property (nonatomic, assign) BOOL isFullSize; // Whether frame size is equal to canvas size
49-
@property (nonatomic, assign) WebPMuxAnimBlend blend; // Frame dispose method
50-
@property (nonatomic, assign) WebPMuxAnimDispose dispose; // Frame blend operation
49+
@property (nonatomic, assign) BOOL shouldBlend; // Frame dispose method
50+
@property (nonatomic, assign) BOOL shouldDispose; // Frame blend operation
5151
@property (nonatomic, assign) NSUInteger blendFromIndex; // The nearest previous frame index which blend mode is WEBP_MUX_BLEND
5252

5353
@end
@@ -719,20 +719,20 @@ - (BOOL)scanAndCheckFramesValidWithDemuxer:(WebPDemuxer *)demuxer {
719719
frame.width = iter.width;
720720
frame.height = iter.height;
721721
frame.hasAlpha = iter.has_alpha;
722-
frame.dispose = iter.dispose_method;
723-
frame.blend = iter.blend_method;
722+
frame.shouldDispose = iter.dispose_method == WEBP_MUX_DISPOSE_BACKGROUND;
723+
frame.shouldBlend = iter.blend_method == WEBP_MUX_BLEND;
724724
frame.offsetX = iter.x_offset;
725725
frame.offsetY = canvasHeight - iter.y_offset - iter.height;
726726

727727
BOOL sizeEqualsToCanvas = (iter.width == canvasWidth && iter.height == canvasHeight);
728728
BOOL offsetIsZero = (iter.x_offset == 0 && iter.y_offset == 0);
729729
frame.isFullSize = (sizeEqualsToCanvas && offsetIsZero);
730730

731-
if ((!frame.blend || !frame.hasAlpha) && frame.isFullSize) {
731+
if ((!frame.shouldBlend || !frame.hasAlpha) && frame.isFullSize) {
732732
lastBlendIndex = iterIndex;
733733
frame.blendFromIndex = iterIndex;
734734
} else {
735-
if (frame.dispose && frame.isFullSize) {
735+
if (frame.shouldDispose && frame.isFullSize) {
736736
frame.blendFromIndex = lastBlendIndex;
737737
lastBlendIndex = iterIndex + 1;
738738
} else {
Binary file not shown.

SDWebImageWebPCoderTests/SDWebImageWebPCoderTests.m

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,11 @@ @interface SDAnimatedImageView ()
2828
@property (nonatomic, assign) BOOL isProgressive;
2929
@end
3030

31+
@interface SDWebPCoderFrame : NSObject
32+
@property (nonatomic, assign) NSUInteger index; // Frame index (zero based)
33+
@property (nonatomic, assign) NSUInteger blendFromIndex; // The nearest previous frame index which blend mode is WEBP_MUX_BLEND
34+
@end
35+
3136
@implementation SDWebImageWebPCoderTests
3237

3338
+ (void)setUp {
@@ -126,6 +131,47 @@ - (void)test32AnimatedImageViewCategoryProgressive {
126131
[self waitForExpectationsWithTimeout:kAsyncTestTimeout handler:nil];
127132
}
128133

134+
- (void)test33AnimatedImageBlendMethod {
135+
// Test the optimization for blend and disposal method works without problem
136+
NSURL *animatedWebPURL = [[NSBundle bundleForClass:[self class]] URLForResource:@"TestImageBlendAnimated" withExtension:@"webp"];
137+
NSData *data = [NSData dataWithContentsOfURL:animatedWebPURL];
138+
SDImageWebPCoder *coder = [[SDImageWebPCoder alloc] initWithAnimatedImageData:data options:nil];
139+
XCTAssertNotNil(coder);
140+
/**
141+
This WebP image frames info is below:
142+
Canvas size: 400 x 400
143+
Features present: animation transparency
144+
Background color : 0xFF000000 Loop Count : 0
145+
Number of frames: 12
146+
No.: width height alpha x_offset y_offset duration dispose blend image_size compression
147+
1: 400 400 no 0 0 70 none no 5178 lossless
148+
2: 400 400 yes 0 0 70 none yes 1386 lossless
149+
3: 400 400 yes 0 0 70 none yes 1472 lossless
150+
4: 400 394 yes 0 6 70 none yes 3212 lossless
151+
5: 371 394 yes 0 6 70 none yes 1888 lossless
152+
6: 394 382 yes 6 6 70 none yes 3346 lossless
153+
7: 400 388 yes 0 0 70 none yes 3786 lossless
154+
8: 394 383 yes 0 0 70 none yes 1858 lossless
155+
9: 394 394 yes 0 6 70 none yes 3794 lossless
156+
10: 372 394 yes 22 6 70 none yes 3458 lossless
157+
11: 400 400 no 0 0 70 none no 5270 lossless
158+
12: 320 382 yes 0 6 70 none yes 2506 lossless
159+
*/
160+
NSArray<SDWebPCoderFrame *> *frames = [coder valueForKey:@"_frames"];
161+
XCTAssertEqual(frames.count, 12);
162+
for (SDWebPCoderFrame *frame in frames) {
163+
switch (frame.index) {
164+
// frame: 11 blend == no, means clear the canvas
165+
case 10:
166+
case 11:
167+
XCTAssertEqual(frame.blendFromIndex, 10);
168+
break;
169+
default:
170+
XCTAssertEqual(frame.blendFromIndex, 0);
171+
break;
172+
}
173+
}
174+
}
129175

130176
@end
131177

0 commit comments

Comments
 (0)