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

@ -891,86 +891,53 @@ static CGSize SDCalculateThumbnailSize(CGSize fullSize, BOOL preserveAspectRatio
config->target_size = (int)maxFileSize; // Max filesize for output, 0 means use quality instead 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->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 = GetIntValueForKey(options, SDImageCoderEncodeWebPMethod, config->method);
config->method = [options[SDImageCoderEncodeWebPMethod] intValue]; config->pass = GetIntValueForKey(options, SDImageCoderEncodeWebPPass, config->pass);
} config->preprocessing = GetIntValueForKey(options, SDImageCoderEncodeWebPPreprocessing, config->preprocessing);
if ([options[SDImageCoderEncodeWebPPass] intValue]) { config->thread_level = GetIntValueForKey(options, SDImageCoderEncodeWebPThreadLevel, 1);
config->pass = [options[SDImageCoderEncodeWebPPass] intValue]; config->low_memory = GetIntValueForKey(options, SDImageCoderEncodeWebPLowMemory, config->low_memory);
} config->target_PSNR = GetFloatValueForKey(options, SDImageCoderEncodeWebPTargetPSNR, config->target_PSNR);
if ([options[SDImageCoderEncodeWebPPreprocessing] intValue]) { config->segments = GetIntValueForKey(options, SDImageCoderEncodeWebPSegments, config->segments);
config->preprocessing = [options[SDImageCoderEncodeWebPPreprocessing] intValue]; config->sns_strength = GetIntValueForKey(options, SDImageCoderEncodeWebPSnsStrength, config->sns_strength);
} config->filter_strength = GetIntValueForKey(options, SDImageCoderEncodeWebPFilterStrength, config->filter_strength);
if ([options[SDImageCoderEncodeWebPThreadLevel] intValue]) { config->filter_sharpness = GetIntValueForKey(options, SDImageCoderEncodeWebPFilterSharpness, config->filter_sharpness);
config->thread_level = [options[SDImageCoderEncodeWebPThreadLevel] intValue]; config->filter_type = GetIntValueForKey(options, SDImageCoderEncodeWebPFilterType, config->filter_type);
} else { config->autofilter = GetIntValueForKey(options, SDImageCoderEncodeWebPAutofilter, config->autofilter);
config->thread_level = 1; config->alpha_compression = GetIntValueForKey(options, SDImageCoderEncodeWebPAlphaCompression, config->alpha_compression);
} config->alpha_filtering = GetIntValueForKey(options, SDImageCoderEncodeWebPAlphaFiltering, config->alpha_filtering);
if ([options[SDImageCoderEncodeWebPLowMemory] intValue]) { config->alpha_quality = GetIntValueForKey(options, SDImageCoderEncodeWebPAlphaQuality, config->alpha_quality);
config->low_memory = [options[SDImageCoderEncodeWebPLowMemory] intValue]; 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);
if ([options[SDImageCoderEncodeWebPTargetPSNR] floatValue]) { config->use_sharp_yuv = GetIntValueForKey(options, SDImageCoderEncodeWebPUseSharpYuv, config->use_sharp_yuv);
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) {
free((void *)data); 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 #pragma mark - SDAnimatedImageCoder
- (instancetype)initWithAnimatedImageData:(NSData *)data options:(nullable SDImageCoderOptions *)options { - (instancetype)initWithAnimatedImageData:(NSData *)data options:(nullable SDImageCoderOptions *)options {
if (!data) { if (!data) {

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