Added new encoding options to WebP encoding

This commit is contained in:
Antti Kortetmaa 2020-12-10 14:31:08 +02:00
parent e252beb187
commit 66cfc23f1c
5 changed files with 284 additions and 7 deletions

View File

@ -7,6 +7,14 @@
objects = {
/* Begin PBXBuildFile section */
220A6239257EAFB300262720 /* SDWebImageWebPCoderDefine.m in Sources */ = {isa = PBXBuildFile; fileRef = 220A6237257EAFB300262720 /* SDWebImageWebPCoderDefine.m */; };
220A623A257EAFB300262720 /* SDWebImageWebPCoderDefine.m in Sources */ = {isa = PBXBuildFile; fileRef = 220A6237257EAFB300262720 /* SDWebImageWebPCoderDefine.m */; };
220A623B257EAFB300262720 /* SDWebImageWebPCoderDefine.m in Sources */ = {isa = PBXBuildFile; fileRef = 220A6237257EAFB300262720 /* SDWebImageWebPCoderDefine.m */; };
220A623C257EAFB300262720 /* SDWebImageWebPCoderDefine.m in Sources */ = {isa = PBXBuildFile; fileRef = 220A6237257EAFB300262720 /* SDWebImageWebPCoderDefine.m */; };
220A623D257EAFB300262720 /* SDWebImageWebPCoderDefine.h in Headers */ = {isa = PBXBuildFile; fileRef = 220A6238257EAFB300262720 /* SDWebImageWebPCoderDefine.h */; };
220A623E257EAFB300262720 /* SDWebImageWebPCoderDefine.h in Headers */ = {isa = PBXBuildFile; fileRef = 220A6238257EAFB300262720 /* SDWebImageWebPCoderDefine.h */; };
220A623F257EAFB300262720 /* SDWebImageWebPCoderDefine.h in Headers */ = {isa = PBXBuildFile; fileRef = 220A6238257EAFB300262720 /* SDWebImageWebPCoderDefine.h */; };
220A6240257EAFB300262720 /* SDWebImageWebPCoderDefine.h in Headers */ = {isa = PBXBuildFile; fileRef = 220A6238257EAFB300262720 /* SDWebImageWebPCoderDefine.h */; };
806E77B32136A2E900A316D2 /* UIImage+WebP.m in Sources */ = {isa = PBXBuildFile; fileRef = 806E77AA2136A2E900A316D2 /* UIImage+WebP.m */; };
806E77B42136A2E900A316D2 /* SDImageWebPCoder.h in Headers */ = {isa = PBXBuildFile; fileRef = 806E77AB2136A2E900A316D2 /* SDImageWebPCoder.h */; settings = {ATTRIBUTES = (Public, ); }; };
806E77B62136A2E900A316D2 /* UIImage+WebP.h in Headers */ = {isa = PBXBuildFile; fileRef = 806E77AD2136A2E900A316D2 /* UIImage+WebP.h */; settings = {ATTRIBUTES = (Public, ); }; };
@ -38,6 +46,8 @@
/* End PBXBuildFile section */
/* Begin PBXFileReference section */
220A6237257EAFB300262720 /* SDWebImageWebPCoderDefine.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = SDWebImageWebPCoderDefine.m; sourceTree = "<group>"; };
220A6238257EAFB300262720 /* SDWebImageWebPCoderDefine.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SDWebImageWebPCoderDefine.h; sourceTree = "<group>"; };
28D8AA3D3015E075692FD3E3 /* Pods-SDWebImageWebPCoderTests.debug.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-SDWebImageWebPCoderTests.debug.xcconfig"; path = "Tests/Pods/Target Support Files/Pods-SDWebImageWebPCoderTests/Pods-SDWebImageWebPCoderTests.debug.xcconfig"; sourceTree = "<group>"; };
3217BE7B220547EB003D0310 /* SDWebImageWebPCoder.modulemap */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = "sourcecode.module-map"; path = SDWebImageWebPCoder.modulemap; sourceTree = "<group>"; };
46F21AD7D1692EBAC4D0FF33 /* Pods_SDWebImageWebPCoderTests.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Pods_SDWebImageWebPCoderTests.framework; sourceTree = BUILT_PRODUCTS_DIR; };
@ -146,6 +156,8 @@
806E77A92136A2E900A316D2 /* Classes */ = {
isa = PBXGroup;
children = (
220A6238257EAFB300262720 /* SDWebImageWebPCoderDefine.h */,
220A6237257EAFB300262720 /* SDWebImageWebPCoderDefine.m */,
806E77AB2136A2E900A316D2 /* SDImageWebPCoder.h */,
806E77AE2136A2E900A316D2 /* SDImageWebPCoder.m */,
806E77AD2136A2E900A316D2 /* UIImage+WebP.h */,
@ -228,6 +240,7 @@
files = (
806E77C72136A7AD00A316D2 /* SDWebImageWebPCoder.h in Headers */,
806E77B62136A2E900A316D2 /* UIImage+WebP.h in Headers */,
220A623D257EAFB300262720 /* SDWebImageWebPCoderDefine.h in Headers */,
806E77B42136A2E900A316D2 /* SDImageWebPCoder.h in Headers */,
);
runOnlyForDeploymentPostprocessing = 0;
@ -238,6 +251,7 @@
files = (
80BFF2502136BC1500B95470 /* UIImage+WebP.h in Headers */,
80BFF24D2136BC0600B95470 /* SDWebImageWebPCoder.h in Headers */,
220A623E257EAFB300262720 /* SDWebImageWebPCoderDefine.h in Headers */,
80BFF24E2136BC1000B95470 /* SDImageWebPCoder.h in Headers */,
);
runOnlyForDeploymentPostprocessing = 0;
@ -248,6 +262,7 @@
files = (
80BFF2632136BE2200B95470 /* SDWebImageWebPCoder.h in Headers */,
80BFF2612136BE1C00B95470 /* UIImage+WebP.h in Headers */,
220A623F257EAFB300262720 /* SDWebImageWebPCoderDefine.h in Headers */,
80BFF25F2136BE1700B95470 /* SDImageWebPCoder.h in Headers */,
);
runOnlyForDeploymentPostprocessing = 0;
@ -258,6 +273,7 @@
files = (
80BFF27F2136BEF200B95470 /* SDWebImageWebPCoder.h in Headers */,
80BFF27D2136BEED00B95470 /* UIImage+WebP.h in Headers */,
220A6240257EAFB300262720 /* SDWebImageWebPCoderDefine.h in Headers */,
80BFF27B2136BEE700B95470 /* SDImageWebPCoder.h in Headers */,
);
runOnlyForDeploymentPostprocessing = 0;
@ -416,6 +432,7 @@
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
220A6239257EAFB300262720 /* SDWebImageWebPCoderDefine.m in Sources */,
806E77B72136A2E900A316D2 /* SDImageWebPCoder.m in Sources */,
806E77B32136A2E900A316D2 /* UIImage+WebP.m in Sources */,
);
@ -425,6 +442,7 @@
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
220A623A257EAFB300262720 /* SDWebImageWebPCoderDefine.m in Sources */,
80BFF24F2136BC1300B95470 /* SDImageWebPCoder.m in Sources */,
80BFF2512136BC1800B95470 /* UIImage+WebP.m in Sources */,
);
@ -434,6 +452,7 @@
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
220A623B257EAFB300262720 /* SDWebImageWebPCoderDefine.m in Sources */,
80BFF2602136BE1A00B95470 /* SDImageWebPCoder.m in Sources */,
80BFF2622136BE1F00B95470 /* UIImage+WebP.m in Sources */,
);
@ -443,6 +462,7 @@
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
220A623C257EAFB300262720 /* SDWebImageWebPCoderDefine.m in Sources */,
80BFF27C2136BEEB00B95470 /* SDImageWebPCoder.m in Sources */,
80BFF27E2136BEF000B95470 /* UIImage+WebP.m in Sources */,
);

View File

@ -23,6 +23,7 @@
#endif
#import <Accelerate/Accelerate.h>
#import "SDWebImageWebPCoderDefine.h"
/// Calculate the actual thumnail pixel size
static CGSize SDCalculateThumbnailSize(CGSize fullSize, BOOL preserveAspectRatio, CGSize thumbnailSize) {
@ -617,7 +618,11 @@ static CGSize SDCalculateThumbnailSize(CGSize fullSize, BOOL preserveAspectRatio
BOOL encodeFirstFrame = [options[SDImageCoderEncodeFirstFrameOnly] boolValue];
if (encodeFirstFrame || frames.count == 0) {
// for static single webp image
data = [self sd_encodedWebpDataWithImage:image.CGImage quality:compressionQuality maxPixelSize:maxPixelSize maxFileSize:maxFileSize];
data = [self sd_encodedWebpDataWithImage:image.CGImage
quality:compressionQuality
maxPixelSize:maxPixelSize
maxFileSize:maxFileSize
options:options];
} else {
// for animated webp image
WebPMux *mux = WebPMuxNew();
@ -626,7 +631,11 @@ static CGSize SDCalculateThumbnailSize(CGSize fullSize, BOOL preserveAspectRatio
}
for (size_t i = 0; i < frames.count; i++) {
SDImageFrame *currentFrame = frames[i];
NSData *webpData = [self sd_encodedWebpDataWithImage:currentFrame.image.CGImage quality:compressionQuality maxPixelSize:maxPixelSize maxFileSize:maxFileSize];
NSData *webpData = [self sd_encodedWebpDataWithImage:currentFrame.image.CGImage
quality:compressionQuality
maxPixelSize:maxPixelSize
maxFileSize:maxFileSize
options:options];
int duration = currentFrame.duration * 1000;
WebPMuxFrameInfo frame = { .bitstream.bytes = webpData.bytes,
.bitstream.size = webpData.length,
@ -663,7 +672,12 @@ static CGSize SDCalculateThumbnailSize(CGSize fullSize, BOOL preserveAspectRatio
return data;
}
- (nullable NSData *)sd_encodedWebpDataWithImage:(nullable CGImageRef)imageRef quality:(double)quality maxPixelSize:(CGSize)maxPixelSize maxFileSize:(NSUInteger)maxFileSize {
- (nullable NSData *)sd_encodedWebpDataWithImage:(nullable CGImageRef)imageRef
quality:(double)quality
maxPixelSize:(CGSize)maxPixelSize
maxFileSize:(NSUInteger)maxFileSize
options:(nullable SDImageCoderOptions *)options
{
NSData *webpData;
if (!imageRef) {
return nil;
@ -779,10 +793,7 @@ static CGSize SDCalculateThumbnailSize(CGSize fullSize, BOOL preserveAspectRatio
return nil;
}
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.thread_level = 1; // Thread encoding for fast
config.lossless = 0; // Disable lossless encoding (If we need, can add new Encoding Options in future version)
[self updateWebPOptionsToConfig:&config maxFileSize:maxFileSize options:options];
picture.use_argb = 0; // Lossy encoding use YUV for internel bitstream
picture.width = (int)width;
picture.height = (int)height;
@ -830,6 +841,89 @@ static CGSize SDCalculateThumbnailSize(CGSize fullSize, BOOL preserveAspectRatio
return webpData;
}
- (void) updateWebPOptionsToConfig:(WebPConfig*)config
maxFileSize:(NSUInteger)maxFileSize
options:(nullable SDImageCoderOptions *)options {
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->lossless = 0; // Disable lossless encoding (If we need, can add new Encoding Options in future version)
if ([options[SDImageCoderEncodeWebPMethod] intValue]) {
config->method = [options[SDImageCoderEncodeWebPMethod] intValue];
}
if ([options[SDImageCoderEncodeWebPPass] intValue]) {
config->pass = [options[SDImageCoderEncodeWebPPass] intValue];
}
if ([options[SDImageCoderEncodeWebPPreprocessing] intValue]) {
config->preprocessing = [options[SDImageCoderEncodeWebPPreprocessing] intValue];
}
if ([options[SDImageCoderEncodeWebPThreadLevel] intValue]) {
config->thread_level = [options[SDImageCoderEncodeWebPThreadLevel] intValue];
} else {
config->thread_level = 1;
}
if ([options[SDImageCoderEncodeWebPLowMemory] intValue]) {
config->low_memory = [options[SDImageCoderEncodeWebPLowMemory] intValue];
}
if ([options[SDImageCoderEncodeWebPTargetPSNR] floatValue]) {
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) {
free((void *)data);
}

View File

@ -0,0 +1,134 @@
//
// SDWebImageWebPCoderDefine.h
// SDWebImageWebPCoderDefine
//
// Created by Antti Kortetmaa on 2020/12/06.
//
#if __has_include(<SDWebImage/SDWebImage.h>)
#import <SDWebImage/SDWebImage.h>
#else
@import SDWebImage;
#endif
NS_ASSUME_NONNULL_BEGIN
/**
Integer value
Quality/speed trade-off (0=fast, 6=slower-better)
*/
FOUNDATION_EXPORT SDImageCoderOption _Nonnull const SDImageCoderEncodeWebPMethod;
/**
Integer value
Number of entropy-analysis passes (in [1..10])
*/
FOUNDATION_EXPORT SDImageCoderOption _Nonnull const SDImageCoderEncodeWebPPass;
/**
Integer value
Preprocessing filter (0=none, 1=segment-smooth)
*/
FOUNDATION_EXPORT SDImageCoderOption _Nonnull const SDImageCoderEncodeWebPPreprocessing;
/**
Float value
if non-zero, specifies the minimal distortion to try to achieve. Takes precedence over target_size.
*/
FOUNDATION_EXPORT SDImageCoderOption _Nonnull const SDImageCoderEncodeWebPTargetPSNR;
/**
Integer value
If non-zero, try and use multi-threaded encoding.
*/
FOUNDATION_EXPORT SDImageCoderOption _Nonnull const SDImageCoderEncodeWebPThreadLevel;
/**
Integer value
If set, reduce memory usage (but increase CPU use).
*/
FOUNDATION_EXPORT SDImageCoderOption _Nonnull const SDImageCoderEncodeWebPLowMemory;
/**
Integer value
if non-zero, specifies the minimal distortion to try to achieve. Takes precedence over target_size.
*/
FOUNDATION_EXPORT SDImageCoderOption _Nonnull const SDImageCoderEncodeWebPSegments;
/**
Integer value
Spatial Noise Shaping. 0=off, 100=maximum.
*/
FOUNDATION_EXPORT SDImageCoderOption _Nonnull const SDImageCoderEncodeWebPSnsStrength;
/**
Integer value
Range: [0 = off .. 100 = strongest]
*/
FOUNDATION_EXPORT SDImageCoderOption _Nonnull const SDImageCoderEncodeWebPFilterStrength;
/**
Integer value
range: [0 = off .. 7 = least sharp]
*/
FOUNDATION_EXPORT SDImageCoderOption _Nonnull const SDImageCoderEncodeWebPFilterSharpness;
/**
Integer value
Filtering type: 0 = simple, 1 = strong (only used If filter_strength > 0 or autofilter > 0)
*/
FOUNDATION_EXPORT SDImageCoderOption _Nonnull const SDImageCoderEncodeWebPFilterType;
/**
Integer value
Auto adjust filter's strength [0 = off, 1 = on]
*/
FOUNDATION_EXPORT SDImageCoderOption _Nonnull const SDImageCoderEncodeWebPAutofilter;
/**
Integer value
Algorithm for encoding the alpha plane (0 = none, 1 = compressed with WebP lossless). Default is 1.
*/
FOUNDATION_EXPORT SDImageCoderOption _Nonnull const SDImageCoderEncodeWebPAlphaCompression;
/**
Integer value
Predictive filtering method for alpha plane. 0: none, 1: fast, 2: best. Default if 1.
*/
FOUNDATION_EXPORT SDImageCoderOption _Nonnull const SDImageCoderEncodeWebPAlphaFiltering;
/**
Integer value
Between 0 (smallest size) and 100 (lossless).
Default is 100.
*/
FOUNDATION_EXPORT SDImageCoderOption _Nonnull const SDImageCoderEncodeWebPAlphaQuality;
/**
Integer value
If true, export the compressed picture back.
In-loop filtering is not applied.
*/
FOUNDATION_EXPORT SDImageCoderOption _Nonnull const SDImageCoderEncodeWebPShowCompressed;
/**
Integer
Log2(number of token partitions) in [0..3]
Default is set to 0 for easier progressive decoding.
*/
FOUNDATION_EXPORT SDImageCoderOption _Nonnull const SDImageCoderEncodeWebPPartitions;
/**
Integer value
Quality degradation allowed to fit the 512k limit on
Prediction modes coding (0: no degradation, 100: maximum possible degradation).
*/
FOUNDATION_EXPORT SDImageCoderOption _Nonnull const SDImageCoderEncodeWebPPartitionLimit;
/**
Integer value
if needed, use sharp (and slow) RGB->YUV conversion
*/
FOUNDATION_EXPORT SDImageCoderOption _Nonnull const SDImageCoderEncodeWebPUseSharpYuv;
NS_ASSUME_NONNULL_END

View File

@ -0,0 +1,28 @@
//
// SDWebImageWebPCoderDefine.m
// SDWebImageWebPCoder
//
// Created by Antti Kortetmaa on 2020/12/06.
//
#import "SDWebImageWebPCoderDefine.h"
SDImageCoderOption _Nonnull const SDImageCoderEncodeWebPMethod = @"webPMethod";
SDImageCoderOption _Nonnull const SDImageCoderEncodeWebPPass = @"webPPass";
SDImageCoderOption _Nonnull const SDImageCoderEncodeWebPPreprocessing = @"webPPreprocessing";
SDImageCoderOption _Nonnull const SDImageCoderEncodeWebPThreadLevel = @"webPThreadLevel";
SDImageCoderOption _Nonnull const SDImageCoderEncodeWebPLowMemory = @"webPLowMemory";
SDImageCoderOption _Nonnull const SDImageCoderEncodeWebPTargetPSNR = @"webPTargetPSNR";
SDImageCoderOption _Nonnull const SDImageCoderEncodeWebPSegments = @"webPSegments";
SDImageCoderOption _Nonnull const SDImageCoderEncodeWebPSnsStrength = @"webPSnsStrength";
SDImageCoderOption _Nonnull const SDImageCoderEncodeWebPFilterStrength = @"webPFilterStrength";
SDImageCoderOption _Nonnull const SDImageCoderEncodeWebPFilterSharpness = @"webPFilterSharpness";
SDImageCoderOption _Nonnull const SDImageCoderEncodeWebPFilterType = @"webPFilterType";
SDImageCoderOption _Nonnull const SDImageCoderEncodeWebPAutofilter = @"webPAutofilter";
SDImageCoderOption _Nonnull const SDImageCoderEncodeWebPAlphaCompression = @"webPAlphaCompression";
SDImageCoderOption _Nonnull const SDImageCoderEncodeWebPAlphaFiltering = @"webPAlphaFiltering";
SDImageCoderOption _Nonnull const SDImageCoderEncodeWebPAlphaQuality = @"webPAlphaQuality";
SDImageCoderOption _Nonnull const SDImageCoderEncodeWebPShowCompressed = @"webPShowCompressed";
SDImageCoderOption _Nonnull const SDImageCoderEncodeWebPPartitions = @"webPPartitions";
SDImageCoderOption _Nonnull const SDImageCoderEncodeWebPPartitionLimit = @"webPPartitionLimit";
SDImageCoderOption _Nonnull const SDImageCoderEncodeWebPUseSharpYuv = @"webPUseSharpYuv";

View File

@ -12,4 +12,5 @@ FOUNDATION_EXPORT double SDWebImageWebPCoderVersionNumber;
FOUNDATION_EXPORT const unsigned char SDWebImageWebPCoderVersionString[];
#import <SDWebImageWebPCoder/SDImageWebPCoder.h>
#import <SDWebImageWebPCoder/SDWebImageWebPCoderDefine.h>
#import <SDWebImageWebPCoder/UIImage+WebP.h>