Merge pull request #52 from AaaK00/fix_zero_value_in_webp_options_ignored

Fixed bug that caused WebP parameter value 0 to be ignored
This commit is contained in:
DreamPiggy 2020-12-24 20:37:19 +08:00 committed by GitHub
commit 25f5908fd8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 72 additions and 76 deletions

View File

@ -892,85 +892,52 @@ 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->lossless = 0; // Disable lossless encoding (If we need, can add new Encoding Options in future version)
if ([options[SDImageCoderEncodeWebPMethod] intValue]) {
config->method = [options[SDImageCoderEncodeWebPMethod] intValue];
}
if ([options[SDImageCoderEncodeWebPPass] intValue]) {
config->pass = [options[SDImageCoderEncodeWebPPass] intValue];
}
if ([options[SDImageCoderEncodeWebPPreprocessing] intValue]) {
config->preprocessing = [options[SDImageCoderEncodeWebPPreprocessing] intValue];
}
if ([options[SDImageCoderEncodeWebPThreadLevel] intValue]) {
config->thread_level = [options[SDImageCoderEncodeWebPThreadLevel] intValue];
} else {
config->thread_level = 1;
}
if ([options[SDImageCoderEncodeWebPLowMemory] intValue]) {
config->low_memory = [options[SDImageCoderEncodeWebPLowMemory] intValue];
}
if ([options[SDImageCoderEncodeWebPTargetPSNR] floatValue]) {
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];
}
config->method = GetIntValueForKey(options, SDImageCoderEncodeWebPMethod, config->method);
config->pass = GetIntValueForKey(options, SDImageCoderEncodeWebPPass, config->pass);
config->preprocessing = GetIntValueForKey(options, SDImageCoderEncodeWebPPreprocessing, config->preprocessing);
config->thread_level = GetIntValueForKey(options, SDImageCoderEncodeWebPThreadLevel, 1);
config->low_memory = GetIntValueForKey(options, SDImageCoderEncodeWebPLowMemory, config->low_memory);
config->target_PSNR = GetFloatValueForKey(options, SDImageCoderEncodeWebPTargetPSNR, config->target_PSNR);
config->segments = GetIntValueForKey(options, SDImageCoderEncodeWebPSegments, config->segments);
config->sns_strength = GetIntValueForKey(options, SDImageCoderEncodeWebPSnsStrength, config->sns_strength);
config->filter_strength = GetIntValueForKey(options, SDImageCoderEncodeWebPFilterStrength, config->filter_strength);
config->filter_sharpness = GetIntValueForKey(options, SDImageCoderEncodeWebPFilterSharpness, config->filter_sharpness);
config->filter_type = GetIntValueForKey(options, SDImageCoderEncodeWebPFilterType, config->filter_type);
config->autofilter = GetIntValueForKey(options, SDImageCoderEncodeWebPAutofilter, config->autofilter);
config->alpha_compression = GetIntValueForKey(options, SDImageCoderEncodeWebPAlphaCompression, config->alpha_compression);
config->alpha_filtering = GetIntValueForKey(options, SDImageCoderEncodeWebPAlphaFiltering, config->alpha_filtering);
config->alpha_quality = GetIntValueForKey(options, SDImageCoderEncodeWebPAlphaQuality, config->alpha_quality);
config->show_compressed = GetIntValueForKey(options, SDImageCoderEncodeWebPShowCompressed, config->show_compressed);
config->partitions = GetIntValueForKey(options, SDImageCoderEncodeWebPPartitions, config->partitions);
config->partition_limit = GetIntValueForKey(options, SDImageCoderEncodeWebPPartitionLimit, config->partition_limit);
config->use_sharp_yuv = GetIntValueForKey(options, SDImageCoderEncodeWebPUseSharpYuv, config->use_sharp_yuv);
}
static void FreeImageData(void *info, const void *data, size_t size) {
free((void *)data);
}
static int GetIntValueForKey(NSDictionary * _Nonnull dictionary, NSString * _Nonnull key, int defaultValue) {
id value = [dictionary objectForKey:key];
if (value != nil) {
if ([value isKindOfClass: [NSNumber class]]) {
return [value intValue];
}
}
return defaultValue;
}
static float GetFloatValueForKey(NSDictionary * _Nonnull dictionary, NSString * _Nonnull key, float defaultValue) {
id value = [dictionary objectForKey:key];
if (value != nil) {
if ([value isKindOfClass: [NSNumber class]]) {
return [value floatValue];
}
}
return defaultValue;
}
#pragma mark - SDAnimatedImageCoder
- (instancetype)initWithAnimatedImageData:(NSData *)data options:(nullable SDImageCoderOptions *)options {
if (!data) {

View File

@ -219,7 +219,7 @@ const int64_t kAsyncTestTimeout = 5;
WebPConfig config;
WebPConfigPreset(&config, WEBP_PRESET_DEFAULT, 0.2);
SDImageCoderOptions *options = @{ SDImageCoderEncodeWebPMethod: @1,
SDImageCoderOptions *options = @{ SDImageCoderEncodeWebPMethod: @0,
SDImageCoderEncodeWebPPass: @2,
SDImageCoderEncodeWebPPreprocessing: @3,
SDImageCoderEncodeWebPThreadLevel: @4,
@ -241,7 +241,7 @@ const int64_t kAsyncTestTimeout = 5;
[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.preprocessing).to.equal(3);
expect(config.thread_level).to.equal(4);
@ -260,7 +260,36 @@ 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);
}
- (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