23
23
#endif
24
24
25
25
#import < Accelerate/Accelerate.h>
26
+ #import " SDWebImageWebPCoderDefine.h"
26
27
27
28
// / Calculate the actual thumnail pixel size
28
29
static CGSize SDCalculateThumbnailSize (CGSize fullSize, BOOL preserveAspectRatio, CGSize thumbnailSize) {
@@ -617,7 +618,11 @@ - (NSData *)encodedDataWithImage:(UIImage *)image format:(SDImageFormat)format o
617
618
BOOL encodeFirstFrame = [options[SDImageCoderEncodeFirstFrameOnly] boolValue ];
618
619
if (encodeFirstFrame || frames.count == 0 ) {
619
620
// for static single webp image
620
- data = [self sd_encodedWebpDataWithImage: image.CGImage quality: compressionQuality maxPixelSize: maxPixelSize maxFileSize: maxFileSize];
621
+ data = [self sd_encodedWebpDataWithImage: image.CGImage
622
+ quality: compressionQuality
623
+ maxPixelSize: maxPixelSize
624
+ maxFileSize: maxFileSize
625
+ options: options];
621
626
} else {
622
627
// for animated webp image
623
628
WebPMux *mux = WebPMuxNew ();
@@ -626,7 +631,11 @@ - (NSData *)encodedDataWithImage:(UIImage *)image format:(SDImageFormat)format o
626
631
}
627
632
for (size_t i = 0 ; i < frames.count ; i++) {
628
633
SDImageFrame *currentFrame = frames[i];
629
- NSData *webpData = [self sd_encodedWebpDataWithImage: currentFrame.image.CGImage quality: compressionQuality maxPixelSize: maxPixelSize maxFileSize: maxFileSize];
634
+ NSData *webpData = [self sd_encodedWebpDataWithImage: currentFrame.image.CGImage
635
+ quality: compressionQuality
636
+ maxPixelSize: maxPixelSize
637
+ maxFileSize: maxFileSize
638
+ options: options];
630
639
int duration = currentFrame.duration * 1000 ;
631
640
WebPMuxFrameInfo frame = { .bitstream .bytes = webpData.bytes ,
632
641
.bitstream .size = webpData.length ,
@@ -663,7 +672,12 @@ - (NSData *)encodedDataWithImage:(UIImage *)image format:(SDImageFormat)format o
663
672
return data;
664
673
}
665
674
666
- - (nullable NSData *)sd_encodedWebpDataWithImage : (nullable CGImageRef)imageRef quality : (double )quality maxPixelSize : (CGSize)maxPixelSize maxFileSize : (NSUInteger )maxFileSize {
675
+ - (nullable NSData *)sd_encodedWebpDataWithImage : (nullable CGImageRef)imageRef
676
+ quality : (double )quality
677
+ maxPixelSize : (CGSize)maxPixelSize
678
+ maxFileSize : (NSUInteger )maxFileSize
679
+ options : (nullable SDImageCoderOptions *)options
680
+ {
667
681
NSData *webpData;
668
682
if (!imageRef) {
669
683
return nil ;
@@ -779,10 +793,7 @@ - (nullable NSData *)sd_encodedWebpDataWithImage:(nullable CGImageRef)imageRef q
779
793
return nil ;
780
794
}
781
795
782
- config.target_size = (int )maxFileSize; // Max filesize for output, 0 means use quality instead
783
- config.pass = maxFileSize > 0 ? 6 : 1 ; // Use 6 passes for file size limited encoding, which is the default value of `cwebp` command line
784
- config.thread_level = 1 ; // Thread encoding for fast
785
- config.lossless = 0 ; // Disable lossless encoding (If we need, can add new Encoding Options in future version)
796
+ [self updateWebPOptionsToConfig: &config maxFileSize: maxFileSize options: options];
786
797
picture.use_argb = 0 ; // Lossy encoding use YUV for internel bitstream
787
798
picture.width = (int )width;
788
799
picture.height = (int )height;
@@ -830,6 +841,89 @@ - (nullable NSData *)sd_encodedWebpDataWithImage:(nullable CGImageRef)imageRef q
830
841
return webpData;
831
842
}
832
843
844
+ - (void ) updateWebPOptionsToConfig : (WebPConfig * _Nonnull)config
845
+ maxFileSize : (NSUInteger )maxFileSize
846
+ options : (nullable SDImageCoderOptions *)options {
847
+
848
+ config->target_size = (int )maxFileSize; // Max filesize for output, 0 means use quality instead
849
+ config->pass = maxFileSize > 0 ? 6 : 1 ; // Use 6 passes for file size limited encoding, which is the default value of `cwebp` command line
850
+ config->lossless = 0 ; // Disable lossless encoding (If we need, can add new Encoding Options in future version)
851
+
852
+ if ([options[SDImageCoderEncodeWebPMethod] intValue ]) {
853
+ config->method = [options[SDImageCoderEncodeWebPMethod] intValue ];
854
+ }
855
+ if ([options[SDImageCoderEncodeWebPPass] intValue ]) {
856
+ config->pass = [options[SDImageCoderEncodeWebPPass] intValue ];
857
+ }
858
+ if ([options[SDImageCoderEncodeWebPPreprocessing] intValue ]) {
859
+ config->preprocessing = [options[SDImageCoderEncodeWebPPreprocessing] intValue ];
860
+ }
861
+ if ([options[SDImageCoderEncodeWebPThreadLevel] intValue ]) {
862
+ config->thread_level = [options[SDImageCoderEncodeWebPThreadLevel] intValue ];
863
+ } else {
864
+ config->thread_level = 1 ;
865
+ }
866
+ if ([options[SDImageCoderEncodeWebPLowMemory] intValue ]) {
867
+ config->low_memory = [options[SDImageCoderEncodeWebPLowMemory] intValue ];
868
+ }
869
+
870
+ if ([options[SDImageCoderEncodeWebPTargetPSNR] floatValue ]) {
871
+ config->target_PSNR = [options[SDImageCoderEncodeWebPTargetPSNR] floatValue ];
872
+ }
873
+
874
+ if ([options[SDImageCoderEncodeWebPSegments] intValue ]) {
875
+ config->segments = [options[SDImageCoderEncodeWebPSegments] intValue ];
876
+ }
877
+
878
+ if ([options[SDImageCoderEncodeWebPSnsStrength] intValue ]) {
879
+ config->sns_strength = [options[SDImageCoderEncodeWebPSnsStrength] intValue ];
880
+ }
881
+
882
+ if ([options[SDImageCoderEncodeWebPFilterStrength] intValue ]) {
883
+ config->filter_strength = [options[SDImageCoderEncodeWebPFilterStrength] intValue ];
884
+ }
885
+
886
+ if ([options[SDImageCoderEncodeWebPFilterSharpness] intValue ]) {
887
+ config->filter_sharpness = [options[SDImageCoderEncodeWebPFilterSharpness] intValue ];
888
+ }
889
+
890
+ if ([options[SDImageCoderEncodeWebPFilterType] intValue ]) {
891
+ config->filter_type = [options[SDImageCoderEncodeWebPFilterType] intValue ];
892
+ }
893
+
894
+ if ([options[SDImageCoderEncodeWebPAutofilter] intValue ]) {
895
+ config->autofilter = [options[SDImageCoderEncodeWebPAutofilter] intValue ];
896
+ }
897
+
898
+ if ([options[SDImageCoderEncodeWebPAlphaCompression] intValue ]) {
899
+ config->alpha_compression = [options[SDImageCoderEncodeWebPAlphaCompression] intValue ];
900
+ }
901
+
902
+ if ([options[SDImageCoderEncodeWebPAlphaFiltering] intValue ]) {
903
+ config->alpha_filtering = [options[SDImageCoderEncodeWebPAlphaFiltering] intValue ];
904
+ }
905
+
906
+ if ([options[SDImageCoderEncodeWebPAlphaQuality] intValue ]) {
907
+ config->alpha_quality = [options[SDImageCoderEncodeWebPAlphaQuality] intValue ];
908
+ }
909
+
910
+ if ([options[SDImageCoderEncodeWebPShowCompressed] intValue ]) {
911
+ config->show_compressed = [options[SDImageCoderEncodeWebPShowCompressed] intValue ];
912
+ }
913
+
914
+ if ([options[SDImageCoderEncodeWebPPartitions] intValue ]) {
915
+ config->partitions = [options[SDImageCoderEncodeWebPPartitions] intValue ];
916
+ }
917
+
918
+ if ([options[SDImageCoderEncodeWebPPartitionLimit] intValue ]) {
919
+ config->partition_limit = [options[SDImageCoderEncodeWebPPartitionLimit] intValue ];
920
+ }
921
+
922
+ if ([options[SDImageCoderEncodeWebPUseSharpYuv] intValue ]) {
923
+ config->use_sharp_yuv = [options[SDImageCoderEncodeWebPUseSharpYuv] intValue ];
924
+ }
925
+ }
926
+
833
927
static void FreeImageData (void *info, const void *data, size_t size) {
834
928
free ((void *)data);
835
929
}
0 commit comments