@@ -884,87 +884,57 @@ - (nullable NSData *)sd_encodedWebpDataWithImage:(nullable CGImageRef)imageRef
884
884
return webpData;
885
885
}
886
886
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
+
887
911
- (void ) updateWebPOptionsToConfig : (WebPConfig * _Nonnull)config
888
912
maxFileSize : (NSUInteger )maxFileSize
889
913
options : (nullable SDImageCoderOptions *)options {
890
914
891
915
config->target_size = (int )maxFileSize; // Max filesize for output, 0 means use quality instead
892
916
config->pass = maxFileSize > 0 ? 6 : 1 ; // Use 6 passes for file size limited encoding, which is the default value of `cwebp` command line
893
917
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];
968
938
}
969
939
970
940
static void FreeImageData (void *info, const void *data, size_t size) {
0 commit comments