Skip to content

Commit a197fa1

Browse files
author
Antti Kortetmaa
committed
Fixed issue that caused WebP option that has been set to 0 to be ignored
1 parent 76edf76 commit a197fa1

File tree

2 files changed

+75
-76
lines changed

2 files changed

+75
-76
lines changed

SDWebImageWebPCoder/Classes/SDImageWebPCoder.m

Lines changed: 44 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -884,87 +884,57 @@ - (nullable NSData *)sd_encodedWebpDataWithImage:(nullable CGImageRef)imageRef
884884
return webpData;
885885
}
886886

887+
- (int) getIntValueFor:(SDImageCoderOption) option
888+
defaultValue:(int) defaultValue
889+
options:(nullable SDImageCoderOptions *)options {
890+
id value = [options objectForKey:option];
891+
if (value != nil) {
892+
if ([value isKindOfClass: [NSNumber class]]) {
893+
return [value intValue];
894+
}
895+
}
896+
return defaultValue;
897+
}
898+
899+
- (float) getFloatValueFor:(SDImageCoderOption) option
900+
defaultValue:(float) defaultValue
901+
options:(nullable SDImageCoderOptions *)options {
902+
id value = [options objectForKey:option];
903+
if (value != nil) {
904+
if ([value isKindOfClass: [NSNumber class]]) {
905+
return [value floatValue];
906+
}
907+
}
908+
return defaultValue;
909+
}
910+
887911
- (void) updateWebPOptionsToConfig:(WebPConfig * _Nonnull)config
888912
maxFileSize:(NSUInteger)maxFileSize
889913
options:(nullable SDImageCoderOptions *)options {
890914

891915
config->target_size = (int)maxFileSize; // Max filesize for output, 0 means use quality instead
892916
config->pass = maxFileSize > 0 ? 6 : 1; // Use 6 passes for file size limited encoding, which is the default value of `cwebp` command line
893917
config->lossless = 0; // Disable lossless encoding (If we need, can add new Encoding Options in future version)
894-
895-
if ([options[SDImageCoderEncodeWebPMethod] intValue]) {
896-
config->method = [options[SDImageCoderEncodeWebPMethod] intValue];
897-
}
898-
if ([options[SDImageCoderEncodeWebPPass] intValue]) {
899-
config->pass = [options[SDImageCoderEncodeWebPPass] intValue];
900-
}
901-
if ([options[SDImageCoderEncodeWebPPreprocessing] intValue]) {
902-
config->preprocessing = [options[SDImageCoderEncodeWebPPreprocessing] intValue];
903-
}
904-
if ([options[SDImageCoderEncodeWebPThreadLevel] intValue]) {
905-
config->thread_level = [options[SDImageCoderEncodeWebPThreadLevel] intValue];
906-
} else {
907-
config->thread_level = 1;
908-
}
909-
if ([options[SDImageCoderEncodeWebPLowMemory] intValue]) {
910-
config->low_memory = [options[SDImageCoderEncodeWebPLowMemory] intValue];
911-
}
912-
913-
if ([options[SDImageCoderEncodeWebPTargetPSNR] floatValue]) {
914-
config->target_PSNR = [options[SDImageCoderEncodeWebPTargetPSNR] floatValue];
915-
}
916-
917-
if ([options[SDImageCoderEncodeWebPSegments] intValue]) {
918-
config->segments = [options[SDImageCoderEncodeWebPSegments] intValue];
919-
}
920-
921-
if ([options[SDImageCoderEncodeWebPSnsStrength] intValue]) {
922-
config->sns_strength = [options[SDImageCoderEncodeWebPSnsStrength] intValue];
923-
}
924-
925-
if ([options[SDImageCoderEncodeWebPFilterStrength] intValue]) {
926-
config->filter_strength = [options[SDImageCoderEncodeWebPFilterStrength] intValue];
927-
}
928-
929-
if ([options[SDImageCoderEncodeWebPFilterSharpness] intValue]) {
930-
config->filter_sharpness = [options[SDImageCoderEncodeWebPFilterSharpness] intValue];
931-
}
932-
933-
if ([options[SDImageCoderEncodeWebPFilterType] intValue]) {
934-
config->filter_type = [options[SDImageCoderEncodeWebPFilterType] intValue];
935-
}
936-
937-
if ([options[SDImageCoderEncodeWebPAutofilter] intValue]) {
938-
config->autofilter = [options[SDImageCoderEncodeWebPAutofilter] intValue];
939-
}
940-
941-
if ([options[SDImageCoderEncodeWebPAlphaCompression] intValue]) {
942-
config->alpha_compression = [options[SDImageCoderEncodeWebPAlphaCompression] intValue];
943-
}
944-
945-
if ([options[SDImageCoderEncodeWebPAlphaFiltering] intValue]) {
946-
config->alpha_filtering = [options[SDImageCoderEncodeWebPAlphaFiltering] intValue];
947-
}
948-
949-
if ([options[SDImageCoderEncodeWebPAlphaQuality] intValue]) {
950-
config->alpha_quality = [options[SDImageCoderEncodeWebPAlphaQuality] intValue];
951-
}
952-
953-
if ([options[SDImageCoderEncodeWebPShowCompressed] intValue]) {
954-
config->show_compressed = [options[SDImageCoderEncodeWebPShowCompressed] intValue];
955-
}
956-
957-
if ([options[SDImageCoderEncodeWebPPartitions] intValue]) {
958-
config->partitions = [options[SDImageCoderEncodeWebPPartitions] intValue];
959-
}
960-
961-
if ([options[SDImageCoderEncodeWebPPartitionLimit] intValue]) {
962-
config->partition_limit = [options[SDImageCoderEncodeWebPPartitionLimit] intValue];
963-
}
964-
965-
if ([options[SDImageCoderEncodeWebPUseSharpYuv] intValue]) {
966-
config->use_sharp_yuv = [options[SDImageCoderEncodeWebPUseSharpYuv] intValue];
967-
}
918+
919+
config->method = [self getIntValueFor: SDImageCoderEncodeWebPMethod defaultValue: config->method options:options];
920+
config->pass = [self getIntValueFor: SDImageCoderEncodeWebPPass defaultValue: config->pass options:options];
921+
config->preprocessing = [self getIntValueFor: SDImageCoderEncodeWebPPreprocessing defaultValue: config->preprocessing options:options];
922+
config->thread_level = [self getIntValueFor: SDImageCoderEncodeWebPThreadLevel defaultValue: 1 options:options];
923+
config->low_memory = [self getIntValueFor: SDImageCoderEncodeWebPLowMemory defaultValue: config->low_memory options:options];
924+
config->target_PSNR = [self getFloatValueFor: SDImageCoderEncodeWebPTargetPSNR defaultValue: config->target_PSNR options:options];
925+
config->segments = [self getIntValueFor: SDImageCoderEncodeWebPSegments defaultValue: config->segments options:options];
926+
config->sns_strength = [self getIntValueFor: SDImageCoderEncodeWebPSnsStrength defaultValue: config->sns_strength options:options];
927+
config->filter_strength = [self getIntValueFor: SDImageCoderEncodeWebPFilterStrength defaultValue: config->filter_strength options:options];
928+
config->filter_sharpness = [self getIntValueFor: SDImageCoderEncodeWebPFilterSharpness defaultValue: config->filter_sharpness options:options];
929+
config->filter_type = [self getIntValueFor: SDImageCoderEncodeWebPFilterType defaultValue: config->filter_type options:options];
930+
config->autofilter = [self getIntValueFor: SDImageCoderEncodeWebPAutofilter defaultValue: config->autofilter options:options];
931+
config->alpha_compression = [self getIntValueFor: SDImageCoderEncodeWebPAlphaCompression defaultValue: config->alpha_compression options:options];
932+
config->alpha_filtering = [self getIntValueFor: SDImageCoderEncodeWebPAlphaFiltering defaultValue: config->alpha_filtering options:options];
933+
config->alpha_quality = [self getIntValueFor: SDImageCoderEncodeWebPAlphaQuality defaultValue: config->alpha_quality options:options];
934+
config->show_compressed = [self getIntValueFor: SDImageCoderEncodeWebPShowCompressed defaultValue: config->show_compressed options:options];
935+
config->partitions = [self getIntValueFor: SDImageCoderEncodeWebPPartitions defaultValue: config->partitions options:options];
936+
config->partition_limit = [self getIntValueFor: SDImageCoderEncodeWebPPartitionLimit defaultValue: config->partition_limit options:options];
937+
config->use_sharp_yuv = [self getIntValueFor: SDImageCoderEncodeWebPUseSharpYuv defaultValue: config->use_sharp_yuv options:options];
968938
}
969939

970940
static void FreeImageData(void *info, const void *data, size_t size) {

Tests/SDWebImageWebPCoderTests.m

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,7 @@ - (void)testEncodingSettings {
219219
WebPConfig config;
220220
WebPConfigPreset(&config, WEBP_PRESET_DEFAULT, 0.2);
221221

222-
SDImageCoderOptions *options = @{ SDImageCoderEncodeWebPMethod: @1,
222+
SDImageCoderOptions *options = @{ SDImageCoderEncodeWebPMethod: @0,
223223
SDImageCoderEncodeWebPPass: @2,
224224
SDImageCoderEncodeWebPPreprocessing: @3,
225225
SDImageCoderEncodeWebPThreadLevel: @4,
@@ -241,7 +241,7 @@ - (void)testEncodingSettings {
241241

242242
[SDImageWebPCoder.sharedCoder updateWebPOptionsToConfig:&config maxFileSize:1200 options:options];
243243

244-
expect(config.method).to.equal(1);
244+
expect(config.method).to.equal(0);
245245
expect(config.pass).to.equal(2);
246246
expect(config.preprocessing).to.equal(3);
247247
expect(config.thread_level).to.equal(4);
@@ -260,7 +260,36 @@ - (void)testEncodingSettings {
260260
expect(config.partitions).to.equal(17);
261261
expect(config.partition_limit).to.equal(18);
262262
expect(config.use_sharp_yuv).to.equal(19);
263+
}
264+
265+
- (void)testEncodingSettingsDefaultValue {
266+
// Ensure that default value is used for values that haven't been defined in options.
267+
WebPConfig config;
268+
WebPConfigPreset(&config, WEBP_PRESET_DEFAULT, 0.2);
269+
270+
SDImageCoderOptions *options = @{
271+
SDImageCoderEncodeWebPThreadLevel: @4,
272+
SDImageCoderEncodeWebPTargetPSNR: @6.9
273+
};
274+
275+
[SDImageWebPCoder.sharedCoder updateWebPOptionsToConfig:&config maxFileSize:1200 options:options];
276+
277+
expect(config.method).to.equal(4);
278+
expect(config.target_PSNR).to.equal(6.9);
279+
}
280+
281+
- (void)testEncodingSettingsIncorrectType {
282+
// Ensure that default value is used if incorrect type of value is given as option.
283+
WebPConfig config;
284+
WebPConfigPreset(&config, WEBP_PRESET_DEFAULT, 0.2);
285+
286+
SDImageCoderOptions *options = @{
287+
SDImageCoderEncodeWebPMethod: @"Foo"
288+
};
289+
290+
[SDImageWebPCoder.sharedCoder updateWebPOptionsToConfig:&config maxFileSize:1200 options:options];
263291

292+
expect(config.method).to.equal(4);
264293
}
265294

266295
@end

0 commit comments

Comments
 (0)