Fixed issue that caused WebP option that has been set to 0 to be ignored

This commit is contained in:
Antti Kortetmaa 2020-12-23 17:34:34 +02:00
parent 76edf76a3c
commit a197fa1c21
2 changed files with 75 additions and 76 deletions

View File

@ -884,6 +884,30 @@ static CGSize SDCalculateThumbnailSize(CGSize fullSize, BOOL preserveAspectRatio
return webpData; return webpData;
} }
- (int) getIntValueFor:(SDImageCoderOption) option
defaultValue:(int) defaultValue
options:(nullable SDImageCoderOptions *)options {
id value = [options objectForKey:option];
if (value != nil) {
if ([value isKindOfClass: [NSNumber class]]) {
return [value intValue];
}
}
return defaultValue;
}
- (float) getFloatValueFor:(SDImageCoderOption) option
defaultValue:(float) defaultValue
options:(nullable SDImageCoderOptions *)options {
id value = [options objectForKey:option];
if (value != nil) {
if ([value isKindOfClass: [NSNumber class]]) {
return [value floatValue];
}
}
return defaultValue;
}
- (void) updateWebPOptionsToConfig:(WebPConfig * _Nonnull)config - (void) updateWebPOptionsToConfig:(WebPConfig * _Nonnull)config
maxFileSize:(NSUInteger)maxFileSize maxFileSize:(NSUInteger)maxFileSize
options:(nullable SDImageCoderOptions *)options { options:(nullable SDImageCoderOptions *)options {
@ -892,79 +916,25 @@ static CGSize SDCalculateThumbnailSize(CGSize fullSize, BOOL preserveAspectRatio
config->pass = maxFileSize > 0 ? 6 : 1; // Use 6 passes for file size limited encoding, which is the default value of `cwebp` command line 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 = 0; // Disable lossless encoding (If we need, can add new Encoding Options in future version)
if ([options[SDImageCoderEncodeWebPMethod] intValue]) { config->method = [self getIntValueFor: SDImageCoderEncodeWebPMethod defaultValue: config->method options:options];
config->method = [options[SDImageCoderEncodeWebPMethod] intValue]; config->pass = [self getIntValueFor: SDImageCoderEncodeWebPPass defaultValue: config->pass options:options];
} config->preprocessing = [self getIntValueFor: SDImageCoderEncodeWebPPreprocessing defaultValue: config->preprocessing options:options];
if ([options[SDImageCoderEncodeWebPPass] intValue]) { config->thread_level = [self getIntValueFor: SDImageCoderEncodeWebPThreadLevel defaultValue: 1 options:options];
config->pass = [options[SDImageCoderEncodeWebPPass] intValue]; config->low_memory = [self getIntValueFor: SDImageCoderEncodeWebPLowMemory defaultValue: config->low_memory options:options];
} config->target_PSNR = [self getFloatValueFor: SDImageCoderEncodeWebPTargetPSNR defaultValue: config->target_PSNR options:options];
if ([options[SDImageCoderEncodeWebPPreprocessing] intValue]) { config->segments = [self getIntValueFor: SDImageCoderEncodeWebPSegments defaultValue: config->segments options:options];
config->preprocessing = [options[SDImageCoderEncodeWebPPreprocessing] intValue]; config->sns_strength = [self getIntValueFor: SDImageCoderEncodeWebPSnsStrength defaultValue: config->sns_strength options:options];
} config->filter_strength = [self getIntValueFor: SDImageCoderEncodeWebPFilterStrength defaultValue: config->filter_strength options:options];
if ([options[SDImageCoderEncodeWebPThreadLevel] intValue]) { config->filter_sharpness = [self getIntValueFor: SDImageCoderEncodeWebPFilterSharpness defaultValue: config->filter_sharpness options:options];
config->thread_level = [options[SDImageCoderEncodeWebPThreadLevel] intValue]; config->filter_type = [self getIntValueFor: SDImageCoderEncodeWebPFilterType defaultValue: config->filter_type options:options];
} else { config->autofilter = [self getIntValueFor: SDImageCoderEncodeWebPAutofilter defaultValue: config->autofilter options:options];
config->thread_level = 1; config->alpha_compression = [self getIntValueFor: SDImageCoderEncodeWebPAlphaCompression defaultValue: config->alpha_compression options:options];
} config->alpha_filtering = [self getIntValueFor: SDImageCoderEncodeWebPAlphaFiltering defaultValue: config->alpha_filtering options:options];
if ([options[SDImageCoderEncodeWebPLowMemory] intValue]) { config->alpha_quality = [self getIntValueFor: SDImageCoderEncodeWebPAlphaQuality defaultValue: config->alpha_quality options:options];
config->low_memory = [options[SDImageCoderEncodeWebPLowMemory] intValue]; config->show_compressed = [self getIntValueFor: SDImageCoderEncodeWebPShowCompressed defaultValue: config->show_compressed options:options];
} config->partitions = [self getIntValueFor: SDImageCoderEncodeWebPPartitions defaultValue: config->partitions options:options];
config->partition_limit = [self getIntValueFor: SDImageCoderEncodeWebPPartitionLimit defaultValue: config->partition_limit options:options];
if ([options[SDImageCoderEncodeWebPTargetPSNR] floatValue]) { config->use_sharp_yuv = [self getIntValueFor: SDImageCoderEncodeWebPUseSharpYuv defaultValue: config->use_sharp_yuv options:options];
config->target_PSNR = [options[SDImageCoderEncodeWebPTargetPSNR] floatValue];
}
if ([options[SDImageCoderEncodeWebPSegments] intValue]) {
config->segments = [options[SDImageCoderEncodeWebPSegments] intValue];
}
if ([options[SDImageCoderEncodeWebPSnsStrength] intValue]) {
config->sns_strength = [options[SDImageCoderEncodeWebPSnsStrength] intValue];
}
if ([options[SDImageCoderEncodeWebPFilterStrength] intValue]) {
config->filter_strength = [options[SDImageCoderEncodeWebPFilterStrength] intValue];
}
if ([options[SDImageCoderEncodeWebPFilterSharpness] intValue]) {
config->filter_sharpness = [options[SDImageCoderEncodeWebPFilterSharpness] intValue];
}
if ([options[SDImageCoderEncodeWebPFilterType] intValue]) {
config->filter_type = [options[SDImageCoderEncodeWebPFilterType] intValue];
}
if ([options[SDImageCoderEncodeWebPAutofilter] intValue]) {
config->autofilter = [options[SDImageCoderEncodeWebPAutofilter] intValue];
}
if ([options[SDImageCoderEncodeWebPAlphaCompression] intValue]) {
config->alpha_compression = [options[SDImageCoderEncodeWebPAlphaCompression] intValue];
}
if ([options[SDImageCoderEncodeWebPAlphaFiltering] intValue]) {
config->alpha_filtering = [options[SDImageCoderEncodeWebPAlphaFiltering] intValue];
}
if ([options[SDImageCoderEncodeWebPAlphaQuality] intValue]) {
config->alpha_quality = [options[SDImageCoderEncodeWebPAlphaQuality] intValue];
}
if ([options[SDImageCoderEncodeWebPShowCompressed] intValue]) {
config->show_compressed = [options[SDImageCoderEncodeWebPShowCompressed] intValue];
}
if ([options[SDImageCoderEncodeWebPPartitions] intValue]) {
config->partitions = [options[SDImageCoderEncodeWebPPartitions] intValue];
}
if ([options[SDImageCoderEncodeWebPPartitionLimit] intValue]) {
config->partition_limit = [options[SDImageCoderEncodeWebPPartitionLimit] intValue];
}
if ([options[SDImageCoderEncodeWebPUseSharpYuv] intValue]) {
config->use_sharp_yuv = [options[SDImageCoderEncodeWebPUseSharpYuv] intValue];
}
} }
static void FreeImageData(void *info, const void *data, size_t size) { static void FreeImageData(void *info, const void *data, size_t size) {

View File

@ -219,7 +219,7 @@ const int64_t kAsyncTestTimeout = 5;
WebPConfig config; WebPConfig config;
WebPConfigPreset(&config, WEBP_PRESET_DEFAULT, 0.2); WebPConfigPreset(&config, WEBP_PRESET_DEFAULT, 0.2);
SDImageCoderOptions *options = @{ SDImageCoderEncodeWebPMethod: @1, SDImageCoderOptions *options = @{ SDImageCoderEncodeWebPMethod: @0,
SDImageCoderEncodeWebPPass: @2, SDImageCoderEncodeWebPPass: @2,
SDImageCoderEncodeWebPPreprocessing: @3, SDImageCoderEncodeWebPPreprocessing: @3,
SDImageCoderEncodeWebPThreadLevel: @4, SDImageCoderEncodeWebPThreadLevel: @4,
@ -241,7 +241,7 @@ const int64_t kAsyncTestTimeout = 5;
[SDImageWebPCoder.sharedCoder updateWebPOptionsToConfig:&config maxFileSize:1200 options:options]; [SDImageWebPCoder.sharedCoder updateWebPOptionsToConfig:&config maxFileSize:1200 options:options];
expect(config.method).to.equal(1); expect(config.method).to.equal(0);
expect(config.pass).to.equal(2); expect(config.pass).to.equal(2);
expect(config.preprocessing).to.equal(3); expect(config.preprocessing).to.equal(3);
expect(config.thread_level).to.equal(4); expect(config.thread_level).to.equal(4);
@ -260,7 +260,36 @@ const int64_t kAsyncTestTimeout = 5;
expect(config.partitions).to.equal(17); expect(config.partitions).to.equal(17);
expect(config.partition_limit).to.equal(18); expect(config.partition_limit).to.equal(18);
expect(config.use_sharp_yuv).to.equal(19); expect(config.use_sharp_yuv).to.equal(19);
}
- (void)testEncodingSettingsDefaultValue {
// Ensure that default value is used for values that haven't been defined in options.
WebPConfig config;
WebPConfigPreset(&config, WEBP_PRESET_DEFAULT, 0.2);
SDImageCoderOptions *options = @{
SDImageCoderEncodeWebPThreadLevel: @4,
SDImageCoderEncodeWebPTargetPSNR: @6.9
};
[SDImageWebPCoder.sharedCoder updateWebPOptionsToConfig:&config maxFileSize:1200 options:options];
expect(config.method).to.equal(4);
expect(config.target_PSNR).to.equal(6.9);
}
- (void)testEncodingSettingsIncorrectType {
// Ensure that default value is used if incorrect type of value is given as option.
WebPConfig config;
WebPConfigPreset(&config, WEBP_PRESET_DEFAULT, 0.2);
SDImageCoderOptions *options = @{
SDImageCoderEncodeWebPMethod: @"Foo"
};
[SDImageWebPCoder.sharedCoder updateWebPOptionsToConfig:&config maxFileSize:1200 options:options];
expect(config.method).to.equal(4);
} }
@end @end