2323#endif
2424
2525#import < Accelerate/Accelerate.h>
26+ #import " SDWebImageWebPCoderDefine.h"
2627
2728// / Calculate the actual thumnail pixel size
2829static CGSize SDCalculateThumbnailSize (CGSize fullSize, BOOL preserveAspectRatio, CGSize thumbnailSize) {
@@ -617,7 +618,11 @@ - (NSData *)encodedDataWithImage:(UIImage *)image format:(SDImageFormat)format o
617618 BOOL encodeFirstFrame = [options[SDImageCoderEncodeFirstFrameOnly] boolValue ];
618619 if (encodeFirstFrame || frames.count == 0 ) {
619620 // 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];
621626 } else {
622627 // for animated webp image
623628 WebPMux *mux = WebPMuxNew ();
@@ -626,7 +631,11 @@ - (NSData *)encodedDataWithImage:(UIImage *)image format:(SDImageFormat)format o
626631 }
627632 for (size_t i = 0 ; i < frames.count ; i++) {
628633 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];
630639 int duration = currentFrame.duration * 1000 ;
631640 WebPMuxFrameInfo frame = { .bitstream .bytes = webpData.bytes ,
632641 .bitstream .size = webpData.length ,
@@ -663,7 +672,12 @@ - (NSData *)encodedDataWithImage:(UIImage *)image format:(SDImageFormat)format o
663672 return data;
664673}
665674
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+ {
667681 NSData *webpData;
668682 if (!imageRef) {
669683 return nil ;
@@ -779,10 +793,7 @@ - (nullable NSData *)sd_encodedWebpDataWithImage:(nullable CGImageRef)imageRef q
779793 return nil ;
780794 }
781795
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];
786797 picture.use_argb = 0 ; // Lossy encoding use YUV for internel bitstream
787798 picture.width = (int )width;
788799 picture.height = (int )height;
@@ -830,6 +841,89 @@ - (nullable NSData *)sd_encodedWebpDataWithImage:(nullable CGImageRef)imageRef q
830841 return webpData;
831842}
832843
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+
833927static void FreeImageData (void *info, const void *data, size_t size) {
834928 free ((void *)data);
835929}
0 commit comments