in iOS/WAStickersThirdParty/YYImageCoder.m [1237:1301]
CFDataRef YYCGImageCreateEncodedWebPData(CGImageRef imageRef, BOOL lossless, CGFloat quality, int compressLevel, YYImagePreset preset) {
if (!imageRef) return nil;
size_t width = CGImageGetWidth(imageRef);
size_t height = CGImageGetHeight(imageRef);
if (width == 0 || width > WEBP_MAX_DIMENSION) return nil;
if (height == 0 || height > WEBP_MAX_DIMENSION) return nil;
vImage_Buffer buffer = {0};
if(!YYCGImageDecodeToBitmapBufferWith32BitFormat(imageRef, &buffer, kCGImageAlphaLast | kCGBitmapByteOrderDefault)) return nil;
WebPConfig config = {0};
WebPPicture picture = {0};
WebPMemoryWriter writer = {0};
CFDataRef webpData = NULL;
BOOL pictureNeedFree = NO;
quality = quality < 0 ? 0 : quality > 1 ? 1 : quality;
preset = preset > YYImagePresetText ? YYImagePresetDefault : preset;
compressLevel = compressLevel < 0 ? 0 : compressLevel > 6 ? 6 : compressLevel;
if (!WebPConfigPreset(&config, (WebPPreset)preset, quality)) goto fail;
config.quality = round(quality * 100.0);
config.lossless = lossless;
config.method = compressLevel;
switch ((WebPPreset)preset) {
case WEBP_PRESET_DEFAULT: {
config.image_hint = WEBP_HINT_DEFAULT;
} break;
case WEBP_PRESET_PICTURE: {
config.image_hint = WEBP_HINT_PICTURE;
} break;
case WEBP_PRESET_PHOTO: {
config.image_hint = WEBP_HINT_PHOTO;
} break;
case WEBP_PRESET_DRAWING:
case WEBP_PRESET_ICON:
case WEBP_PRESET_TEXT: {
config.image_hint = WEBP_HINT_GRAPH;
} break;
}
if (!WebPValidateConfig(&config)) goto fail;
if (!WebPPictureInit(&picture)) goto fail;
pictureNeedFree = YES;
picture.width = (int)buffer.width;
picture.height = (int)buffer.height;
picture.use_argb = lossless;
if(!WebPPictureImportRGBA(&picture, buffer.data, (int)buffer.rowBytes)) goto fail;
WebPMemoryWriterInit(&writer);
picture.writer = WebPMemoryWrite;
picture.custom_ptr = &writer;
if(!WebPEncode(&config, &picture)) goto fail;
webpData = CFDataCreate(CFAllocatorGetDefault(), writer.mem, writer.size);
free(writer.mem);
WebPPictureFree(&picture);
free(buffer.data);
return webpData;
fail:
if (buffer.data) free(buffer.data);
if (pictureNeedFree) WebPPictureFree(&picture);
return nil;
}