From e330f22573720b70b658c80319012619185896d2 Mon Sep 17 00:00:00 2001 From: Antti Kortetmaa Date: Thu, 10 Dec 2020 21:07:37 +0200 Subject: [PATCH] Added unit tests for WebP settings. --- SDWebImageWebPCoder.xcodeproj/project.pbxproj | 4 +- .../Classes/SDImageWebPCoder.h | 18 +++++++ .../Classes/SDImageWebPCoder.m | 6 +-- Tests/SDWebImageWebPCoderTests.m | 49 +++++++++++++++++++ 4 files changed, 72 insertions(+), 5 deletions(-) diff --git a/SDWebImageWebPCoder.xcodeproj/project.pbxproj b/SDWebImageWebPCoder.xcodeproj/project.pbxproj index 6023d9e..86d8a32 100644 --- a/SDWebImageWebPCoder.xcodeproj/project.pbxproj +++ b/SDWebImageWebPCoder.xcodeproj/project.pbxproj @@ -7,7 +7,6 @@ objects = { /* Begin PBXBuildFile section */ - 220A6239257EAFB300262720 /* SDWebImageWebPCoderDefine.m in Sources */ = {isa = PBXBuildFile; fileRef = 220A6237257EAFB300262720 /* SDWebImageWebPCoderDefine.m */; }; 220A623A257EAFB300262720 /* SDWebImageWebPCoderDefine.m in Sources */ = {isa = PBXBuildFile; fileRef = 220A6237257EAFB300262720 /* SDWebImageWebPCoderDefine.m */; }; 220A623B257EAFB300262720 /* SDWebImageWebPCoderDefine.m in Sources */ = {isa = PBXBuildFile; fileRef = 220A6237257EAFB300262720 /* SDWebImageWebPCoderDefine.m */; }; 220A623C257EAFB300262720 /* SDWebImageWebPCoderDefine.m in Sources */ = {isa = PBXBuildFile; fileRef = 220A6237257EAFB300262720 /* SDWebImageWebPCoderDefine.m */; }; @@ -15,6 +14,7 @@ 220A623E257EAFB300262720 /* SDWebImageWebPCoderDefine.h in Headers */ = {isa = PBXBuildFile; fileRef = 220A6238257EAFB300262720 /* SDWebImageWebPCoderDefine.h */; }; 220A623F257EAFB300262720 /* SDWebImageWebPCoderDefine.h in Headers */ = {isa = PBXBuildFile; fileRef = 220A6238257EAFB300262720 /* SDWebImageWebPCoderDefine.h */; }; 220A6240257EAFB300262720 /* SDWebImageWebPCoderDefine.h in Headers */ = {isa = PBXBuildFile; fileRef = 220A6238257EAFB300262720 /* SDWebImageWebPCoderDefine.h */; }; + 228EA36125825A52005903D9 /* SDWebImageWebPCoderDefine.m in Sources */ = {isa = PBXBuildFile; fileRef = 220A6237257EAFB300262720 /* SDWebImageWebPCoderDefine.m */; }; 806E77B32136A2E900A316D2 /* UIImage+WebP.m in Sources */ = {isa = PBXBuildFile; fileRef = 806E77AA2136A2E900A316D2 /* UIImage+WebP.m */; }; 806E77B42136A2E900A316D2 /* SDImageWebPCoder.h in Headers */ = {isa = PBXBuildFile; fileRef = 806E77AB2136A2E900A316D2 /* SDImageWebPCoder.h */; settings = {ATTRIBUTES = (Public, ); }; }; 806E77B62136A2E900A316D2 /* UIImage+WebP.h in Headers */ = {isa = PBXBuildFile; fileRef = 806E77AD2136A2E900A316D2 /* UIImage+WebP.h */; settings = {ATTRIBUTES = (Public, ); }; }; @@ -432,7 +432,7 @@ isa = PBXSourcesBuildPhase; buildActionMask = 2147483647; files = ( - 220A6239257EAFB300262720 /* SDWebImageWebPCoderDefine.m in Sources */, + 228EA36125825A52005903D9 /* SDWebImageWebPCoderDefine.m in Sources */, 806E77B72136A2E900A316D2 /* SDImageWebPCoder.m in Sources */, 806E77B32136A2E900A316D2 /* UIImage+WebP.m in Sources */, ); diff --git a/SDWebImageWebPCoder/Classes/SDImageWebPCoder.h b/SDWebImageWebPCoder/Classes/SDImageWebPCoder.h index 7629ded..849259f 100644 --- a/SDWebImageWebPCoder/Classes/SDImageWebPCoder.h +++ b/SDWebImageWebPCoder/Classes/SDImageWebPCoder.h @@ -12,6 +12,20 @@ @import SDWebImage; #endif +#if __has_include("webp/decode.h") && __has_include("webp/encode.h") && __has_include("webp/demux.h") && __has_include("webp/mux.h") +#import "webp/decode.h" +#import "webp/encode.h" +#import "webp/demux.h" +#import "webp/mux.h" +#elif __has_include() && __has_include() && __has_include() && __has_include() +#import +#import +#import +#import +#else +@import libwebp; +#endif + /** Built in coder that supports WebP and animated WebP */ @@ -19,4 +33,8 @@ @property (nonatomic, class, readonly, nonnull) SDImageWebPCoder *sharedCoder; +- (void) updateWebPOptionsToConfig:(WebPConfig * _Nonnull)config + maxFileSize:(NSUInteger)maxFileSize + options:(nullable SDImageCoderOptions *)options; + @end diff --git a/SDWebImageWebPCoder/Classes/SDImageWebPCoder.m b/SDWebImageWebPCoder/Classes/SDImageWebPCoder.m index 1759fd3..612ed91 100644 --- a/SDWebImageWebPCoder/Classes/SDImageWebPCoder.m +++ b/SDWebImageWebPCoder/Classes/SDImageWebPCoder.m @@ -841,9 +841,9 @@ static CGSize SDCalculateThumbnailSize(CGSize fullSize, BOOL preserveAspectRatio return webpData; } -- (void) updateWebPOptionsToConfig:(WebPConfig*)config - maxFileSize:(NSUInteger)maxFileSize - options:(nullable SDImageCoderOptions *)options { +- (void) updateWebPOptionsToConfig:(WebPConfig * _Nonnull)config + maxFileSize:(NSUInteger)maxFileSize + options:(nullable SDImageCoderOptions *)options { config->target_size = (int)maxFileSize; // Max filesize for output, 0 means use quality instead config->pass = maxFileSize > 0 ? 6 : 1; // Use 6 passes for file size limited encoding, which is the default value of `cwebp` command line diff --git a/Tests/SDWebImageWebPCoderTests.m b/Tests/SDWebImageWebPCoderTests.m index f2deee5..7583d13 100644 --- a/Tests/SDWebImageWebPCoderTests.m +++ b/Tests/SDWebImageWebPCoderTests.m @@ -10,6 +10,7 @@ @import XCTest; #import #import +#import #import #import @@ -196,6 +197,54 @@ const int64_t kAsyncTestTimeout = 5; XCTAssertLessThanOrEqual(dataWithLimit.length, maxFileSize); } +- (void)testEncodingSettings { + WebPConfig config; + WebPConfigPreset(&config, WEBP_PRESET_DEFAULT, 0.2); + + SDImageCoderOptions *options = @{ SDImageCoderEncodeWebPMethod: @1, + SDImageCoderEncodeWebPPass: @2, + SDImageCoderEncodeWebPPreprocessing: @3, + SDImageCoderEncodeWebPThreadLevel: @4, + SDImageCoderEncodeWebPLowMemory: @5, + SDImageCoderEncodeWebPTargetPSNR: @6, + SDImageCoderEncodeWebPSegments: @7, + SDImageCoderEncodeWebPSnsStrength: @8, + SDImageCoderEncodeWebPFilterStrength: @9, + SDImageCoderEncodeWebPFilterSharpness: @10, + SDImageCoderEncodeWebPFilterType: @11, + SDImageCoderEncodeWebPAutofilter: @12, + SDImageCoderEncodeWebPAlphaCompression: @13, + SDImageCoderEncodeWebPAlphaFiltering: @14, + SDImageCoderEncodeWebPAlphaQuality: @15, + SDImageCoderEncodeWebPShowCompressed: @16, + SDImageCoderEncodeWebPPartitions: @17, + SDImageCoderEncodeWebPPartitionLimit: @18, + SDImageCoderEncodeWebPUseSharpYuv: @19 }; + + [SDImageWebPCoder.sharedCoder updateWebPOptionsToConfig:&config maxFileSize:1200 options:options]; + + expect(config.method).to.equal(1); + expect(config.pass).to.equal(2); + expect(config.preprocessing).to.equal(3); + expect(config.thread_level).to.equal(4); + expect(config.low_memory).to.equal(5); + expect(config.target_PSNR).to.equal(6); + expect(config.segments).to.equal(7); + expect(config.sns_strength).to.equal(8); + expect(config.filter_strength).to.equal(9); + expect(config.filter_sharpness).to.equal(10); + expect(config.filter_type).to.equal(11); + expect(config.autofilter).to.equal(12); + expect(config.alpha_compression).to.equal(13); + expect(config.alpha_filtering).to.equal(14); + expect(config.alpha_quality).to.equal(15); + expect(config.show_compressed).to.equal(16); + expect(config.partitions).to.equal(17); + expect(config.partition_limit).to.equal(18); + expect(config.use_sharp_yuv).to.equal(19); + +} + @end @implementation SDWebImageWebPCoderTests (Helpers)