@@ -891,85 +891,52 @@ - (void) updateWebPOptionsToConfig:(WebPConfig * _Nonnull)config
891
891
config->target_size = (int )maxFileSize; // Max filesize for output, 0 means use quality instead
892
892
config->pass = maxFileSize > 0 ? 6 : 1 ; // Use 6 passes for file size limited encoding, which is the default value of `cwebp` command line
893
893
config->lossless = 0 ; // Disable lossless encoding (If we need, can add new Encoding Options in future version)
894
+
895
+ config->method = GetIntValueForKey (options, SDImageCoderEncodeWebPMethod, config->method );
896
+ config->pass = GetIntValueForKey (options, SDImageCoderEncodeWebPPass, config->pass );
897
+ config->preprocessing = GetIntValueForKey (options, SDImageCoderEncodeWebPPreprocessing, config->preprocessing );
898
+ config->thread_level = GetIntValueForKey (options, SDImageCoderEncodeWebPThreadLevel, 1 );
899
+ config->low_memory = GetIntValueForKey (options, SDImageCoderEncodeWebPLowMemory, config->low_memory );
900
+ config->target_PSNR = GetFloatValueForKey (options, SDImageCoderEncodeWebPTargetPSNR, config->target_PSNR );
901
+ config->segments = GetIntValueForKey (options, SDImageCoderEncodeWebPSegments, config->segments );
902
+ config->sns_strength = GetIntValueForKey (options, SDImageCoderEncodeWebPSnsStrength, config->sns_strength );
903
+ config->filter_strength = GetIntValueForKey (options, SDImageCoderEncodeWebPFilterStrength, config->filter_strength );
904
+ config->filter_sharpness = GetIntValueForKey (options, SDImageCoderEncodeWebPFilterSharpness, config->filter_sharpness );
905
+ config->filter_type = GetIntValueForKey (options, SDImageCoderEncodeWebPFilterType, config->filter_type );
906
+ config->autofilter = GetIntValueForKey (options, SDImageCoderEncodeWebPAutofilter, config->autofilter );
907
+ config->alpha_compression = GetIntValueForKey (options, SDImageCoderEncodeWebPAlphaCompression, config->alpha_compression );
908
+ config->alpha_filtering = GetIntValueForKey (options, SDImageCoderEncodeWebPAlphaFiltering, config->alpha_filtering );
909
+ config->alpha_quality = GetIntValueForKey (options, SDImageCoderEncodeWebPAlphaQuality, config->alpha_quality );
910
+ config->show_compressed = GetIntValueForKey (options, SDImageCoderEncodeWebPShowCompressed, config->show_compressed );
911
+ config->partitions = GetIntValueForKey (options, SDImageCoderEncodeWebPPartitions, config->partitions );
912
+ config->partition_limit = GetIntValueForKey (options, SDImageCoderEncodeWebPPartitionLimit, config->partition_limit );
913
+ config->use_sharp_yuv = GetIntValueForKey (options, SDImageCoderEncodeWebPUseSharpYuv, config->use_sharp_yuv );
914
+ }
894
915
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
- }
916
+ static void FreeImageData (void *info, const void *data, size_t size) {
917
+ free ((void *)data);
918
+ }
960
919
961
- if ([options[SDImageCoderEncodeWebPPartitionLimit] intValue ]) {
962
- config->partition_limit = [options[SDImageCoderEncodeWebPPartitionLimit] intValue ];
920
+ static int GetIntValueForKey (NSDictionary * _Nonnull dictionary, NSString * _Nonnull key, int defaultValue) {
921
+ id value = [dictionary objectForKey: key];
922
+ if (value != nil ) {
923
+ if ([value isKindOfClass: [NSNumber class ]]) {
924
+ return [value intValue ];
925
+ }
963
926
}
927
+ return defaultValue;
928
+ }
964
929
965
- if ([options[SDImageCoderEncodeWebPUseSharpYuv] intValue ]) {
966
- config->use_sharp_yuv = [options[SDImageCoderEncodeWebPUseSharpYuv] intValue ];
930
+ static float GetFloatValueForKey (NSDictionary * _Nonnull dictionary, NSString * _Nonnull key, float defaultValue) {
931
+ id value = [dictionary objectForKey: key];
932
+ if (value != nil ) {
933
+ if ([value isKindOfClass: [NSNumber class ]]) {
934
+ return [value floatValue ];
935
+ }
967
936
}
937
+ return defaultValue;
968
938
}
969
939
970
- static void FreeImageData (void *info, const void *data, size_t size) {
971
- free ((void *)data);
972
- }
973
940
974
941
#pragma mark - SDAnimatedImageCoder
975
942
- (instancetype )initWithAnimatedImageData : (NSData *)data options : (nullable SDImageCoderOptions *)options {
0 commit comments