From 64cdf3e75626d43f29ce559395a611fdc9f6c631 Mon Sep 17 00:00:00 2001 From: Liu YuanYuan Date: Sun, 8 Oct 2023 03:22:36 -0500 Subject: [PATCH] Enable lossless mode (#85) Enable lossless mode with `SDImageCoderEncodeWebPLossless` --------- Co-authored-by: DreamPiggy --- SDWebImageWebPCoder/Classes/SDImageWebPCoder.m | 2 +- SDWebImageWebPCoder/Classes/SDWebImageWebPCoderDefine.h | 8 ++++++++ SDWebImageWebPCoder/Classes/SDWebImageWebPCoderDefine.m | 1 + Tests/SDWebImageWebPCoderTests.m | 4 +++- 4 files changed, 13 insertions(+), 2 deletions(-) diff --git a/SDWebImageWebPCoder/Classes/SDImageWebPCoder.m b/SDWebImageWebPCoder/Classes/SDImageWebPCoder.m index c2064e6..8d8403f 100644 --- a/SDWebImageWebPCoder/Classes/SDImageWebPCoder.m +++ b/SDWebImageWebPCoder/Classes/SDImageWebPCoder.m @@ -955,7 +955,7 @@ WEBP_CSP_MODE ConvertCSPMode(CGBitmapInfo bitmapInfo) { 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 - config->lossless = 0; // Disable lossless encoding (If we need, can add new Encoding Options in future version) + config->lossless = GetIntValueForKey(options, SDImageCoderEncodeWebPLossless, config->lossless); config->method = GetIntValueForKey(options, SDImageCoderEncodeWebPMethod, config->method); config->pass = GetIntValueForKey(options, SDImageCoderEncodeWebPPass, config->pass); diff --git a/SDWebImageWebPCoder/Classes/SDWebImageWebPCoderDefine.h b/SDWebImageWebPCoder/Classes/SDWebImageWebPCoderDefine.h index dd972fe..c7bfc2d 100644 --- a/SDWebImageWebPCoder/Classes/SDWebImageWebPCoderDefine.h +++ b/SDWebImageWebPCoder/Classes/SDWebImageWebPCoderDefine.h @@ -132,4 +132,12 @@ FOUNDATION_EXPORT SDImageCoderOption _Nonnull const SDImageCoderEncodeWebPPartit */ FOUNDATION_EXPORT SDImageCoderOption _Nonnull const SDImageCoderEncodeWebPUseSharpYuv; +/** + 0: Disabled, 1: Enabled. + Lossless mode. Note that if lossless is enabled, encoder quality param specifies + compression effort. 100 means maximum compression. + Details on cwebp documentation: https://developers.google.com/speed/webp/docs/cwebp#lossless + */ +FOUNDATION_EXPORT SDImageCoderOption _Nonnull const SDImageCoderEncodeWebPLossless; + NS_ASSUME_NONNULL_END diff --git a/SDWebImageWebPCoder/Classes/SDWebImageWebPCoderDefine.m b/SDWebImageWebPCoder/Classes/SDWebImageWebPCoderDefine.m index ff333de..2eed21f 100644 --- a/SDWebImageWebPCoder/Classes/SDWebImageWebPCoderDefine.m +++ b/SDWebImageWebPCoder/Classes/SDWebImageWebPCoderDefine.m @@ -26,3 +26,4 @@ SDImageCoderOption _Nonnull const SDImageCoderEncodeWebPShowCompressed = @"webPS SDImageCoderOption _Nonnull const SDImageCoderEncodeWebPPartitions = @"webPPartitions"; SDImageCoderOption _Nonnull const SDImageCoderEncodeWebPPartitionLimit = @"webPPartitionLimit"; SDImageCoderOption _Nonnull const SDImageCoderEncodeWebPUseSharpYuv = @"webPUseSharpYuv"; +SDImageCoderOption _Nonnull const SDImageCoderEncodeWebPLossless = @"webPLossless"; diff --git a/Tests/SDWebImageWebPCoderTests.m b/Tests/SDWebImageWebPCoderTests.m index 40bdf9f..2dc5a9d 100644 --- a/Tests/SDWebImageWebPCoderTests.m +++ b/Tests/SDWebImageWebPCoderTests.m @@ -252,7 +252,8 @@ const int64_t kAsyncTestTimeout = 5; SDImageCoderEncodeWebPShowCompressed: @16, SDImageCoderEncodeWebPPartitions: @17, SDImageCoderEncodeWebPPartitionLimit: @18, - SDImageCoderEncodeWebPUseSharpYuv: @19 }; + SDImageCoderEncodeWebPUseSharpYuv: @19, + SDImageCoderEncodeWebPLossless: @1 }; [SDImageWebPCoder.sharedCoder updateWebPOptionsToConfig:&config maxFileSize:1200 options:options]; @@ -275,6 +276,7 @@ const int64_t kAsyncTestTimeout = 5; expect(config.partitions).to.equal(17); expect(config.partition_limit).to.equal(18); expect(config.use_sharp_yuv).to.equal(19); + expect(config.lossless).to.equal(1); } - (void)testEncodingSettingsDefaultValue {