1a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora// Copyright 2011 Google Inc. All Rights Reserved. 27c970a0a679089e416c5887cf7fcece15a70bfa4Vikas Arora// 30406ce1417f76f2034833414dcecc9f56253640cVikas Arora// Use of this source code is governed by a BSD-style license 40406ce1417f76f2034833414dcecc9f56253640cVikas Arora// that can be found in the COPYING file in the root of the source 50406ce1417f76f2034833414dcecc9f56253640cVikas Arora// tree. An additional intellectual property rights grant can be found 60406ce1417f76f2034833414dcecc9f56253640cVikas Arora// in the file PATENTS. All contributing project authors may 70406ce1417f76f2034833414dcecc9f56253640cVikas Arora// be found in the AUTHORS file in the root of the source tree. 87c970a0a679089e416c5887cf7fcece15a70bfa4Vikas Arora// ----------------------------------------------------------------------------- 97c970a0a679089e416c5887cf7fcece15a70bfa4Vikas Arora// 107c970a0a679089e416c5887cf7fcece15a70bfa4Vikas Arora// WebP encoder: main interface 117c970a0a679089e416c5887cf7fcece15a70bfa4Vikas Arora// 127c970a0a679089e416c5887cf7fcece15a70bfa4Vikas Arora// Author: Skal (pascal.massimino@gmail.com) 137c970a0a679089e416c5887cf7fcece15a70bfa4Vikas Arora 147c970a0a679089e416c5887cf7fcece15a70bfa4Vikas Arora#ifndef WEBP_WEBP_ENCODE_H_ 157c970a0a679089e416c5887cf7fcece15a70bfa4Vikas Arora#define WEBP_WEBP_ENCODE_H_ 167c970a0a679089e416c5887cf7fcece15a70bfa4Vikas Arora 17466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora#include "./types.h" 187c970a0a679089e416c5887cf7fcece15a70bfa4Vikas Arora 198b720228d581a84fd173b6dcb2fa295b59db489aVikas Arora#ifdef __cplusplus 207c970a0a679089e416c5887cf7fcece15a70bfa4Vikas Aroraextern "C" { 217c970a0a679089e416c5887cf7fcece15a70bfa4Vikas Arora#endif 227c970a0a679089e416c5887cf7fcece15a70bfa4Vikas Arora 238b720228d581a84fd173b6dcb2fa295b59db489aVikas Arora#define WEBP_ENCODER_ABI_VERSION 0x0202 // MAJOR(8b) + MINOR(8b) 241e7bf8805bd030c19924a5306837ecd72c295751Vikas Arora 250406ce1417f76f2034833414dcecc9f56253640cVikas Arora// Note: forward declaring enumerations is not allowed in (strict) C and C++, 260406ce1417f76f2034833414dcecc9f56253640cVikas Arora// the types are left here for reference. 270406ce1417f76f2034833414dcecc9f56253640cVikas Arora// typedef enum WebPImageHint WebPImageHint; 280406ce1417f76f2034833414dcecc9f56253640cVikas Arora// typedef enum WebPEncCSP WebPEncCSP; 290406ce1417f76f2034833414dcecc9f56253640cVikas Arora// typedef enum WebPPreset WebPPreset; 300406ce1417f76f2034833414dcecc9f56253640cVikas Arora// typedef enum WebPEncodingError WebPEncodingError; 311e7bf8805bd030c19924a5306837ecd72c295751Vikas Aroratypedef struct WebPConfig WebPConfig; 321e7bf8805bd030c19924a5306837ecd72c295751Vikas Aroratypedef struct WebPPicture WebPPicture; // main structure for I/O 331e7bf8805bd030c19924a5306837ecd72c295751Vikas Aroratypedef struct WebPAuxStats WebPAuxStats; 341e7bf8805bd030c19924a5306837ecd72c295751Vikas Aroratypedef struct WebPMemoryWriter WebPMemoryWriter; 357c970a0a679089e416c5887cf7fcece15a70bfa4Vikas Arora 367c970a0a679089e416c5887cf7fcece15a70bfa4Vikas Arora// Return the encoder's version number, packed in hexadecimal using 8bits for 377c970a0a679089e416c5887cf7fcece15a70bfa4Vikas Arora// each of major/minor/revision. E.g: v2.5.7 is 0x020507. 38466727975bcc57c0c5597bcd0747a2fe4777b303Vikas AroraWEBP_EXTERN(int) WebPGetEncoderVersion(void); 397c970a0a679089e416c5887cf7fcece15a70bfa4Vikas Arora 40a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora//------------------------------------------------------------------------------ 417c970a0a679089e416c5887cf7fcece15a70bfa4Vikas Arora// One-stop-shop call! No questions asked: 427c970a0a679089e416c5887cf7fcece15a70bfa4Vikas Arora 437c970a0a679089e416c5887cf7fcece15a70bfa4Vikas Arora// Returns the size of the compressed data (pointed to by *output), or 0 if 447c970a0a679089e416c5887cf7fcece15a70bfa4Vikas Arora// an error occurred. The compressed data must be released by the caller 457c970a0a679089e416c5887cf7fcece15a70bfa4Vikas Arora// using the call 'free(*output)'. 46a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora// These functions compress using the lossy format, and the quality_factor 47a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora// can go from 0 (smaller output, lower quality) to 100 (best quality, 48a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora// larger output). 49466727975bcc57c0c5597bcd0747a2fe4777b303Vikas AroraWEBP_EXTERN(size_t) WebPEncodeRGB(const uint8_t* rgb, 50466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora int width, int height, int stride, 51466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora float quality_factor, uint8_t** output); 52466727975bcc57c0c5597bcd0747a2fe4777b303Vikas AroraWEBP_EXTERN(size_t) WebPEncodeBGR(const uint8_t* bgr, 53466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora int width, int height, int stride, 54466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora float quality_factor, uint8_t** output); 55466727975bcc57c0c5597bcd0747a2fe4777b303Vikas AroraWEBP_EXTERN(size_t) WebPEncodeRGBA(const uint8_t* rgba, 56466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora int width, int height, int stride, 57466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora float quality_factor, uint8_t** output); 58466727975bcc57c0c5597bcd0747a2fe4777b303Vikas AroraWEBP_EXTERN(size_t) WebPEncodeBGRA(const uint8_t* bgra, 59466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora int width, int height, int stride, 60466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora float quality_factor, uint8_t** output); 617c970a0a679089e416c5887cf7fcece15a70bfa4Vikas Arora 62a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora// These functions are the equivalent of the above, but compressing in a 63a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora// lossless manner. Files are usually larger than lossy format, but will 64a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora// not suffer any compression loss. 65a2415724fb3466168b2af5b08bd94ba732c0e753Vikas AroraWEBP_EXTERN(size_t) WebPEncodeLosslessRGB(const uint8_t* rgb, 66a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora int width, int height, int stride, 67a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora uint8_t** output); 68a2415724fb3466168b2af5b08bd94ba732c0e753Vikas AroraWEBP_EXTERN(size_t) WebPEncodeLosslessBGR(const uint8_t* bgr, 69a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora int width, int height, int stride, 70a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora uint8_t** output); 71a2415724fb3466168b2af5b08bd94ba732c0e753Vikas AroraWEBP_EXTERN(size_t) WebPEncodeLosslessRGBA(const uint8_t* rgba, 72a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora int width, int height, int stride, 73a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora uint8_t** output); 74a2415724fb3466168b2af5b08bd94ba732c0e753Vikas AroraWEBP_EXTERN(size_t) WebPEncodeLosslessBGRA(const uint8_t* bgra, 75a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora int width, int height, int stride, 76a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora uint8_t** output); 77a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora 78a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora//------------------------------------------------------------------------------ 797c970a0a679089e416c5887cf7fcece15a70bfa4Vikas Arora// Coding parameters 807c970a0a679089e416c5887cf7fcece15a70bfa4Vikas Arora 81a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora// Image characteristics hint for the underlying encoder. 820406ce1417f76f2034833414dcecc9f56253640cVikas Aroratypedef enum WebPImageHint { 83a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora WEBP_HINT_DEFAULT = 0, // default preset. 84a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora WEBP_HINT_PICTURE, // digital picture, like portrait, inner shot 85a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora WEBP_HINT_PHOTO, // outdoor photograph, with natural lighting 86a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora WEBP_HINT_GRAPH, // Discrete tone image (graph, map-tile etc). 87a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora WEBP_HINT_LAST 880406ce1417f76f2034833414dcecc9f56253640cVikas Arora} WebPImageHint; 89a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora 901e7bf8805bd030c19924a5306837ecd72c295751Vikas Arora// Compression parameters. 911e7bf8805bd030c19924a5306837ecd72c295751Vikas Arorastruct WebPConfig { 92a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora int lossless; // Lossless encoding (0=lossy(default), 1=lossless). 93a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora float quality; // between 0 (smallest file) and 100 (biggest) 94a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora int method; // quality/speed trade-off (0=fast, 6=slower-better) 95a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora 96a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora WebPImageHint image_hint; // Hint for image type (lossless only for now). 97a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora 98a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora // Parameters related to lossy compression only: 99a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora int target_size; // if non-zero, set the desired target size in bytes. 100a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora // Takes precedence over the 'compression' parameter. 101a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora float target_PSNR; // if non-zero, specifies the minimal distortion to 102a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora // try to achieve. Takes precedence over target_size. 103a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora int segments; // maximum number of segments to use, in [1..4] 104a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora int sns_strength; // Spatial Noise Shaping. 0=off, 100=maximum. 105a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora int filter_strength; // range: [0 = off .. 100 = strongest] 106a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora int filter_sharpness; // range: [0 = off .. 7 = least sharp] 107a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora int filter_type; // filtering type: 0 = simple, 1 = strong (only used 108a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora // if filter_strength > 0 or autofilter > 0) 109a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora int autofilter; // Auto adjust filter's strength [0 = off, 1 = on] 110a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora int alpha_compression; // Algorithm for encoding the alpha plane (0 = none, 111a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora // 1 = compressed with WebP lossless). Default is 1. 112a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora int alpha_filtering; // Predictive filtering method for alpha plane. 113a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora // 0: none, 1: fast, 2: best. Default if 1. 114a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora int alpha_quality; // Between 0 (smallest size) and 100 (lossless). 115a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora // Default is 100. 116a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora int pass; // number of entropy-analysis passes (in [1..10]). 117a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora 118a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora int show_compressed; // if true, export the compressed picture back. 119a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora // In-loop filtering is not applied. 1208b720228d581a84fd173b6dcb2fa295b59db489aVikas Arora int preprocessing; // preprocessing filter: 1218b720228d581a84fd173b6dcb2fa295b59db489aVikas Arora // 0=none, 1=segment-smooth, 2=pseudo-random dithering 122a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora int partitions; // log2(number of token partitions) in [0..3]. Default 123a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora // is set to 0 for easier progressive decoding. 124a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora int partition_limit; // quality degradation allowed to fit the 512k limit 125a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora // on prediction modes coding (0: no degradation, 126a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora // 100: maximum possible degradation). 1271e7bf8805bd030c19924a5306837ecd72c295751Vikas Arora int emulate_jpeg_size; // If true, compression parameters will be remapped 1281e7bf8805bd030c19924a5306837ecd72c295751Vikas Arora // to better match the expected output size from 1291e7bf8805bd030c19924a5306837ecd72c295751Vikas Arora // JPEG compression. Generally, the output size will 1301e7bf8805bd030c19924a5306837ecd72c295751Vikas Arora // be similar but the degradation will be lower. 1311e7bf8805bd030c19924a5306837ecd72c295751Vikas Arora int thread_level; // If non-zero, try and use multi-threaded encoding. 1321e7bf8805bd030c19924a5306837ecd72c295751Vikas Arora int low_memory; // If set, reduce memory usage (but increase CPU use). 1331e7bf8805bd030c19924a5306837ecd72c295751Vikas Arora 1341e7bf8805bd030c19924a5306837ecd72c295751Vikas Arora uint32_t pad[5]; // padding for later use 1351e7bf8805bd030c19924a5306837ecd72c295751Vikas Arora}; 1367c970a0a679089e416c5887cf7fcece15a70bfa4Vikas Arora 1377c970a0a679089e416c5887cf7fcece15a70bfa4Vikas Arora// Enumerate some predefined settings for WebPConfig, depending on the type 1387c970a0a679089e416c5887cf7fcece15a70bfa4Vikas Arora// of source picture. These presets are used when calling WebPConfigPreset(). 1390406ce1417f76f2034833414dcecc9f56253640cVikas Aroratypedef enum WebPPreset { 1407c970a0a679089e416c5887cf7fcece15a70bfa4Vikas Arora WEBP_PRESET_DEFAULT = 0, // default preset. 1417c970a0a679089e416c5887cf7fcece15a70bfa4Vikas Arora WEBP_PRESET_PICTURE, // digital picture, like portrait, inner shot 1427c970a0a679089e416c5887cf7fcece15a70bfa4Vikas Arora WEBP_PRESET_PHOTO, // outdoor photograph, with natural lighting 1437c970a0a679089e416c5887cf7fcece15a70bfa4Vikas Arora WEBP_PRESET_DRAWING, // hand or line drawing, with high-contrast details 1447c970a0a679089e416c5887cf7fcece15a70bfa4Vikas Arora WEBP_PRESET_ICON, // small-sized colorful images 1457c970a0a679089e416c5887cf7fcece15a70bfa4Vikas Arora WEBP_PRESET_TEXT // text-like 1460406ce1417f76f2034833414dcecc9f56253640cVikas Arora} WebPPreset; 1477c970a0a679089e416c5887cf7fcece15a70bfa4Vikas Arora 1487c970a0a679089e416c5887cf7fcece15a70bfa4Vikas Arora// Internal, version-checked, entry point 149a2415724fb3466168b2af5b08bd94ba732c0e753Vikas AroraWEBP_EXTERN(int) WebPConfigInitInternal(WebPConfig*, WebPPreset, float, int); 1507c970a0a679089e416c5887cf7fcece15a70bfa4Vikas Arora 1517c970a0a679089e416c5887cf7fcece15a70bfa4Vikas Arora// Should always be called, to initialize a fresh WebPConfig structure before 152a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora// modification. Returns false in case of version mismatch. WebPConfigInit() 153a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora// must have succeeded before using the 'config' object. 154a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora// Note that the default values are lossless=0 and quality=75. 155a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arorastatic WEBP_INLINE int WebPConfigInit(WebPConfig* config) { 1567c970a0a679089e416c5887cf7fcece15a70bfa4Vikas Arora return WebPConfigInitInternal(config, WEBP_PRESET_DEFAULT, 75.f, 1577c970a0a679089e416c5887cf7fcece15a70bfa4Vikas Arora WEBP_ENCODER_ABI_VERSION); 1587c970a0a679089e416c5887cf7fcece15a70bfa4Vikas Arora} 1597c970a0a679089e416c5887cf7fcece15a70bfa4Vikas Arora 1607c970a0a679089e416c5887cf7fcece15a70bfa4Vikas Arora// This function will initialize the configuration according to a predefined 1617c970a0a679089e416c5887cf7fcece15a70bfa4Vikas Arora// set of parameters (referred to by 'preset') and a given quality factor. 1627c970a0a679089e416c5887cf7fcece15a70bfa4Vikas Arora// This function can be called as a replacement to WebPConfigInit(). Will 163a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora// return false in case of error. 164a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arorastatic WEBP_INLINE int WebPConfigPreset(WebPConfig* config, 165a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora WebPPreset preset, float quality) { 1667c970a0a679089e416c5887cf7fcece15a70bfa4Vikas Arora return WebPConfigInitInternal(config, preset, quality, 1677c970a0a679089e416c5887cf7fcece15a70bfa4Vikas Arora WEBP_ENCODER_ABI_VERSION); 1687c970a0a679089e416c5887cf7fcece15a70bfa4Vikas Arora} 1697c970a0a679089e416c5887cf7fcece15a70bfa4Vikas Arora 170af51b94a435132e9014c324e25fb686b3d07a8c8Vikas Arora#if WEBP_ENCODER_ABI_VERSION > 0x0202 171af51b94a435132e9014c324e25fb686b3d07a8c8Vikas Arora// Activate the lossless compression mode with the desired efficiency level 172af51b94a435132e9014c324e25fb686b3d07a8c8Vikas Arora// between 0 (fastest, lowest compression) and 9 (slower, best compression). 173af51b94a435132e9014c324e25fb686b3d07a8c8Vikas Arora// A good default level is '6', providing a fair tradeoff between compression 174af51b94a435132e9014c324e25fb686b3d07a8c8Vikas Arora// speed and final compressed size. 175af51b94a435132e9014c324e25fb686b3d07a8c8Vikas Arora// This function will overwrite several fields from config: 'method', 'quality' 176af51b94a435132e9014c324e25fb686b3d07a8c8Vikas Arora// and 'lossless'. Returns false in case of parameter error. 177af51b94a435132e9014c324e25fb686b3d07a8c8Vikas AroraWEBP_EXTERN(int) WebPConfigLosslessPreset(WebPConfig* config, int level); 178af51b94a435132e9014c324e25fb686b3d07a8c8Vikas Arora#endif 179af51b94a435132e9014c324e25fb686b3d07a8c8Vikas Arora 180a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora// Returns true if 'config' is non-NULL and all configuration parameters are 181a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora// within their valid ranges. 182a2415724fb3466168b2af5b08bd94ba732c0e753Vikas AroraWEBP_EXTERN(int) WebPValidateConfig(const WebPConfig* config); 1837c970a0a679089e416c5887cf7fcece15a70bfa4Vikas Arora 184a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora//------------------------------------------------------------------------------ 1857c970a0a679089e416c5887cf7fcece15a70bfa4Vikas Arora// Input / Output 186b6dbce6bfeaabde2a7b581c4c6888d532d32f3acDerek Sollenberger// Structure for storing auxiliary statistics (mostly for lossy encoding). 1871e7bf8805bd030c19924a5306837ecd72c295751Vikas Arora 1881e7bf8805bd030c19924a5306837ecd72c295751Vikas Arorastruct WebPAuxStats { 1897c970a0a679089e416c5887cf7fcece15a70bfa4Vikas Arora int coded_size; // final size 190a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora 191a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora float PSNR[5]; // peak-signal-to-noise ratio for Y/U/V/All/Alpha 1927c970a0a679089e416c5887cf7fcece15a70bfa4Vikas Arora int block_count[3]; // number of intra4/intra16/skipped macroblocks 19388fe2b83c4b9232cd08729556fd0485d6a6a92cdVikas Arora int header_bytes[2]; // approximate number of bytes spent for header 1947c970a0a679089e416c5887cf7fcece15a70bfa4Vikas Arora // and mode-partition #0 19588fe2b83c4b9232cd08729556fd0485d6a6a92cdVikas Arora int residual_bytes[3][4]; // approximate number of bytes spent for 1967c970a0a679089e416c5887cf7fcece15a70bfa4Vikas Arora // DC/AC/uv coefficients for each (0..3) segments. 1977c970a0a679089e416c5887cf7fcece15a70bfa4Vikas Arora int segment_size[4]; // number of macroblocks in each segments 1987c970a0a679089e416c5887cf7fcece15a70bfa4Vikas Arora int segment_quant[4]; // quantizer values for each segments 1997c970a0a679089e416c5887cf7fcece15a70bfa4Vikas Arora int segment_level[4]; // filtering strength for each segments [0..63] 200466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora 201466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora int alpha_data_size; // size of the transparency data 202466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora int layer_data_size; // size of the enhancement layer data 203a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora 204a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora // lossless encoder statistics 205a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora uint32_t lossless_features; // bit0:predictor bit1:cross-color transform 206a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora // bit2:subtract-green bit3:color indexing 207a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora int histogram_bits; // number of precision bits of histogram 208a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora int transform_bits; // precision bits for transform 209a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora int cache_bits; // number of bits for color cache lookup 210a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora int palette_size; // number of color in palette, if used 211a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora int lossless_size; // final lossless size 212a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora 213a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora uint32_t pad[4]; // padding for later use 2141e7bf8805bd030c19924a5306837ecd72c295751Vikas Arora}; 2157c970a0a679089e416c5887cf7fcece15a70bfa4Vikas Arora 216a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora// Signature for output function. Should return true if writing was successful. 2177c970a0a679089e416c5887cf7fcece15a70bfa4Vikas Arora// data/data_size is the segment of data to write, and 'picture' is for 2187c970a0a679089e416c5887cf7fcece15a70bfa4Vikas Arora// reference (and so one can make use of picture->custom_ptr). 2197c970a0a679089e416c5887cf7fcece15a70bfa4Vikas Aroratypedef int (*WebPWriterFunction)(const uint8_t* data, size_t data_size, 220a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora const WebPPicture* picture); 221a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora 222a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora// WebPMemoryWrite: a special WebPWriterFunction that writes to memory using 223a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora// the following WebPMemoryWriter object (to be set as a custom_ptr). 2241e7bf8805bd030c19924a5306837ecd72c295751Vikas Arorastruct WebPMemoryWriter { 225a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora uint8_t* mem; // final buffer (of size 'max_size', larger than 'size'). 226a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora size_t size; // final size 227a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora size_t max_size; // total capacity 228a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora uint32_t pad[1]; // padding for later use 2291e7bf8805bd030c19924a5306837ecd72c295751Vikas Arora}; 230a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora 231a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora// The following must be called first before any use. 232a2415724fb3466168b2af5b08bd94ba732c0e753Vikas AroraWEBP_EXTERN(void) WebPMemoryWriterInit(WebPMemoryWriter* writer); 233a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora 234af51b94a435132e9014c324e25fb686b3d07a8c8Vikas Arora#if WEBP_ENCODER_ABI_VERSION > 0x0202 235af51b94a435132e9014c324e25fb686b3d07a8c8Vikas Arora// The following must be called to deallocate writer->mem memory. The 'writer' 236af51b94a435132e9014c324e25fb686b3d07a8c8Vikas Arora// object itself is not deallocated. 237af51b94a435132e9014c324e25fb686b3d07a8c8Vikas AroraWEBP_EXTERN(void) WebPMemoryWriterClear(WebPMemoryWriter* writer); 238af51b94a435132e9014c324e25fb686b3d07a8c8Vikas Arora#endif 239a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora// The custom writer to be used with WebPMemoryWriter as custom_ptr. Upon 240a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora// completion, writer.mem and writer.size will hold the coded data. 241af51b94a435132e9014c324e25fb686b3d07a8c8Vikas Arora#if WEBP_ENCODER_ABI_VERSION > 0x0202 242af51b94a435132e9014c324e25fb686b3d07a8c8Vikas Arora// writer.mem must be freed by calling WebPMemoryWriterClear. 243af51b94a435132e9014c324e25fb686b3d07a8c8Vikas Arora#else 244af51b94a435132e9014c324e25fb686b3d07a8c8Vikas Arora// writer.mem must be freed by calling 'free(writer.mem)'. 245af51b94a435132e9014c324e25fb686b3d07a8c8Vikas Arora#endif 246a2415724fb3466168b2af5b08bd94ba732c0e753Vikas AroraWEBP_EXTERN(int) WebPMemoryWrite(const uint8_t* data, size_t data_size, 247a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora const WebPPicture* picture); 248a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora 249a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora// Progress hook, called from time to time to report progress. It can return 250a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora// false to request an abort of the encoding process, or true otherwise if 251a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora// everything is OK. 252a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Aroratypedef int (*WebPProgressHook)(int percent, const WebPPicture* picture); 2537c970a0a679089e416c5887cf7fcece15a70bfa4Vikas Arora 2541e7bf8805bd030c19924a5306837ecd72c295751Vikas Arora// Color spaces. 2550406ce1417f76f2034833414dcecc9f56253640cVikas Aroratypedef enum WebPEncCSP { 256466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora // chroma sampling 257af51b94a435132e9014c324e25fb686b3d07a8c8Vikas Arora WEBP_YUV420 = 0, // 4:2:0 258af51b94a435132e9014c324e25fb686b3d07a8c8Vikas Arora WEBP_YUV420A = 4, // alpha channel variant 259af51b94a435132e9014c324e25fb686b3d07a8c8Vikas Arora WEBP_CSP_UV_MASK = 3, // bit-mask to get the UV sampling factors 260466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora WEBP_CSP_ALPHA_BIT = 4 // bit that is set if alpha is present 2610406ce1417f76f2034833414dcecc9f56253640cVikas Arora} WebPEncCSP; 262466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora 263466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora// Encoding error conditions. 2640406ce1417f76f2034833414dcecc9f56253640cVikas Aroratypedef enum WebPEncodingError { 265466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora VP8_ENC_OK = 0, 266466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora VP8_ENC_ERROR_OUT_OF_MEMORY, // memory error allocating objects 267466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora VP8_ENC_ERROR_BITSTREAM_OUT_OF_MEMORY, // memory error while flushing bits 268466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora VP8_ENC_ERROR_NULL_PARAMETER, // a pointer parameter is NULL 269466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora VP8_ENC_ERROR_INVALID_CONFIGURATION, // configuration is invalid 270466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora VP8_ENC_ERROR_BAD_DIMENSION, // picture has invalid width/height 271a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora VP8_ENC_ERROR_PARTITION0_OVERFLOW, // partition is bigger than 512k 272a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora VP8_ENC_ERROR_PARTITION_OVERFLOW, // partition is bigger than 16M 273466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora VP8_ENC_ERROR_BAD_WRITE, // error while flushing bytes 274a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora VP8_ENC_ERROR_FILE_TOO_BIG, // file is bigger than 4G 275a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora VP8_ENC_ERROR_USER_ABORT, // abort request by user 276a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora VP8_ENC_ERROR_LAST // list terminator. always last. 2770406ce1417f76f2034833414dcecc9f56253640cVikas Arora} WebPEncodingError; 278466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora 279a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora// maximum width/height allowed (inclusive), in pixels 280a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora#define WEBP_MAX_DIMENSION 16383 281a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora 282a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora// Main exchange structure (input samples, output bytes, statistics) 2837c970a0a679089e416c5887cf7fcece15a70bfa4Vikas Arorastruct WebPPicture { 284a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora // INPUT 285a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora ////////////// 286a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora // Main flag for encoder selecting between ARGB or YUV input. 287a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora // It is recommended to use ARGB input (*argb, argb_stride) for lossless 288a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora // compression, and YUV input (*y, *u, *v, etc.) for lossy compression 289a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora // since these are the respective native colorspace for these formats. 290a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora int use_argb; 291a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora 292a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora // YUV input (mostly used for input to lossy compression) 293466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora WebPEncCSP colorspace; // colorspace: should be YUV420 for now (=Y'CbCr). 294a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora int width, height; // dimensions (less or equal to WEBP_MAX_DIMENSION) 2957c970a0a679089e416c5887cf7fcece15a70bfa4Vikas Arora uint8_t *y, *u, *v; // pointers to luma/chroma planes. 2967c970a0a679089e416c5887cf7fcece15a70bfa4Vikas Arora int y_stride, uv_stride; // luma/chroma strides. 297a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora uint8_t* a; // pointer to the alpha plane 298466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora int a_stride; // stride of the alpha plane 299a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora uint32_t pad1[2]; // padding for later use 300a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora 301a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora // ARGB input (mostly used for input to lossless compression) 302a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora uint32_t* argb; // Pointer to argb (32 bit) plane. 303a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora int argb_stride; // This is stride in pixels units, not bytes. 304a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora uint32_t pad2[3]; // padding for later use 3057c970a0a679089e416c5887cf7fcece15a70bfa4Vikas Arora 306a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora // OUTPUT 307a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora /////////////// 308a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora // Byte-emission hook, to store compressed bytes as they are ready. 3097c970a0a679089e416c5887cf7fcece15a70bfa4Vikas Arora WebPWriterFunction writer; // can be NULL 3107c970a0a679089e416c5887cf7fcece15a70bfa4Vikas Arora void* custom_ptr; // can be used by the writer. 3117c970a0a679089e416c5887cf7fcece15a70bfa4Vikas Arora 312a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora // map for extra information (only for lossy compression mode) 3137c970a0a679089e416c5887cf7fcece15a70bfa4Vikas Arora int extra_info_type; // 1: intra type, 2: segment, 3: quant 3147c970a0a679089e416c5887cf7fcece15a70bfa4Vikas Arora // 4: intra-16 prediction mode, 3157c970a0a679089e416c5887cf7fcece15a70bfa4Vikas Arora // 5: chroma prediction mode, 3167c970a0a679089e416c5887cf7fcece15a70bfa4Vikas Arora // 6: bit cost, 7: distortion 3177c970a0a679089e416c5887cf7fcece15a70bfa4Vikas Arora uint8_t* extra_info; // if not NULL, points to an array of size 3187c970a0a679089e416c5887cf7fcece15a70bfa4Vikas Arora // ((width + 15) / 16) * ((height + 15) / 16) that 3197c970a0a679089e416c5887cf7fcece15a70bfa4Vikas Arora // will be filled with a macroblock map, depending 3207c970a0a679089e416c5887cf7fcece15a70bfa4Vikas Arora // on extra_info_type. 3217c970a0a679089e416c5887cf7fcece15a70bfa4Vikas Arora 322a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora // STATS AND REPORTS 323a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora /////////////////////////// 324a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora // Pointer to side statistics (updated only if not NULL) 3257c970a0a679089e416c5887cf7fcece15a70bfa4Vikas Arora WebPAuxStats* stats; 326466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora 327a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora // Error code for the latest error encountered during encoding 328a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora WebPEncodingError error_code; 329a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora 330a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora // If not NULL, report progress during encoding. 331a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora WebPProgressHook progress_hook; 332a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora 333a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora void* user_data; // this field is free to be set to any value and 334a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora // used during callbacks (like progress-report e.g.). 335a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora 336a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora uint32_t pad3[3]; // padding for later use 337a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora 338af51b94a435132e9014c324e25fb686b3d07a8c8Vikas Arora // Unused for now 339af51b94a435132e9014c324e25fb686b3d07a8c8Vikas Arora uint8_t *pad4, *pad5; 340af51b94a435132e9014c324e25fb686b3d07a8c8Vikas Arora uint32_t pad6[8]; // padding for later use 341a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora 342a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora // PRIVATE FIELDS 343a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora //////////////////// 344a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora void* memory_; // row chunk of memory for yuva planes 345a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora void* memory_argb_; // and for argb too. 346af51b94a435132e9014c324e25fb686b3d07a8c8Vikas Arora void* pad7[2]; // padding for later use 3477c970a0a679089e416c5887cf7fcece15a70bfa4Vikas Arora}; 3487c970a0a679089e416c5887cf7fcece15a70bfa4Vikas Arora 3497c970a0a679089e416c5887cf7fcece15a70bfa4Vikas Arora// Internal, version-checked, entry point 350a2415724fb3466168b2af5b08bd94ba732c0e753Vikas AroraWEBP_EXTERN(int) WebPPictureInitInternal(WebPPicture*, int); 3517c970a0a679089e416c5887cf7fcece15a70bfa4Vikas Arora 352a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora// Should always be called, to initialize the structure. Returns false in case 353a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora// of version mismatch. WebPPictureInit() must have succeeded before using the 3547c970a0a679089e416c5887cf7fcece15a70bfa4Vikas Arora// 'picture' object. 355a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora// Note that, by default, use_argb is false and colorspace is WEBP_YUV420. 356a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arorastatic WEBP_INLINE int WebPPictureInit(WebPPicture* picture) { 3577c970a0a679089e416c5887cf7fcece15a70bfa4Vikas Arora return WebPPictureInitInternal(picture, WEBP_ENCODER_ABI_VERSION); 3587c970a0a679089e416c5887cf7fcece15a70bfa4Vikas Arora} 3597c970a0a679089e416c5887cf7fcece15a70bfa4Vikas Arora 360a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora//------------------------------------------------------------------------------ 3617c970a0a679089e416c5887cf7fcece15a70bfa4Vikas Arora// WebPPicture utils 3627c970a0a679089e416c5887cf7fcece15a70bfa4Vikas Arora 3637c970a0a679089e416c5887cf7fcece15a70bfa4Vikas Arora// Convenience allocation / deallocation based on picture->width/height: 364466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora// Allocate y/u/v buffers as per colorspace/width/height specification. 3657c970a0a679089e416c5887cf7fcece15a70bfa4Vikas Arora// Note! This function will free the previous buffer if needed. 366a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora// Returns false in case of memory error. 367a2415724fb3466168b2af5b08bd94ba732c0e753Vikas AroraWEBP_EXTERN(int) WebPPictureAlloc(WebPPicture* picture); 368a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora 369a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora// Release the memory allocated by WebPPictureAlloc() or WebPPictureImport*(). 370a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora// Note that this function does _not_ free the memory used by the 'picture' 371a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora// object itself. 372a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora// Besides memory (which is reclaimed) all other fields of 'picture' are 373a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora// preserved. 374a2415724fb3466168b2af5b08bd94ba732c0e753Vikas AroraWEBP_EXTERN(void) WebPPictureFree(WebPPicture* picture); 375a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora 3760406ce1417f76f2034833414dcecc9f56253640cVikas Arora// Copy the pixels of *src into *dst, using WebPPictureAlloc. Upon return, *dst 3770406ce1417f76f2034833414dcecc9f56253640cVikas Arora// will fully own the copied pixels (this is not a view). The 'dst' picture need 3780406ce1417f76f2034833414dcecc9f56253640cVikas Arora// not be initialized as its content is overwritten. 379a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora// Returns false in case of memory allocation error. 380a2415724fb3466168b2af5b08bd94ba732c0e753Vikas AroraWEBP_EXTERN(int) WebPPictureCopy(const WebPPicture* src, WebPPicture* dst); 381a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora 382a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora// Compute PSNR, SSIM or LSIM distortion metric between two pictures. 383a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora// Result is in dB, stores in result[] in the Y/U/V/Alpha/All order. 3841e7bf8805bd030c19924a5306837ecd72c295751Vikas Arora// Returns false in case of error (src and ref don't have same dimension, ...) 385a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora// Warning: this function is rather CPU-intensive. 386a2415724fb3466168b2af5b08bd94ba732c0e753Vikas AroraWEBP_EXTERN(int) WebPPictureDistortion( 3871e7bf8805bd030c19924a5306837ecd72c295751Vikas Arora const WebPPicture* src, const WebPPicture* ref, 388a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora int metric_type, // 0 = PSNR, 1 = SSIM, 2 = LSIM 389a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora float result[5]); 3907c970a0a679089e416c5887cf7fcece15a70bfa4Vikas Arora 3917c970a0a679089e416c5887cf7fcece15a70bfa4Vikas Arora// self-crops a picture to the rectangle defined by top/left/width/height. 392a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora// Returns false in case of memory allocation error, or if the rectangle is 3937c970a0a679089e416c5887cf7fcece15a70bfa4Vikas Arora// outside of the source picture. 394a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora// The rectangle for the view is defined by the top-left corner pixel 395a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora// coordinates (left, top) as well as its width and height. This rectangle 396a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora// must be fully be comprised inside the 'src' source picture. If the source 397a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora// picture uses the YUV420 colorspace, the top and left coordinates will be 398a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora// snapped to even values. 399a2415724fb3466168b2af5b08bd94ba732c0e753Vikas AroraWEBP_EXTERN(int) WebPPictureCrop(WebPPicture* picture, 400466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora int left, int top, int width, int height); 401466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora 402a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora// Extracts a view from 'src' picture into 'dst'. The rectangle for the view 403a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora// is defined by the top-left corner pixel coordinates (left, top) as well 404a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora// as its width and height. This rectangle must be fully be comprised inside 405a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora// the 'src' source picture. If the source picture uses the YUV420 colorspace, 406a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora// the top and left coordinates will be snapped to even values. 407a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora// Picture 'src' must out-live 'dst' picture. Self-extraction of view is allowed 408a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora// ('src' equal to 'dst') as a mean of fast-cropping (but note that doing so, 4090406ce1417f76f2034833414dcecc9f56253640cVikas Arora// the original dimension will be lost). Picture 'dst' need not be initialized 4100406ce1417f76f2034833414dcecc9f56253640cVikas Arora// with WebPPictureInit() if it is different from 'src', since its content will 4110406ce1417f76f2034833414dcecc9f56253640cVikas Arora// be overwritten. 412a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora// Returns false in case of memory allocation error or invalid parameters. 413a2415724fb3466168b2af5b08bd94ba732c0e753Vikas AroraWEBP_EXTERN(int) WebPPictureView(const WebPPicture* src, 414a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora int left, int top, int width, int height, 415a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora WebPPicture* dst); 416a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora 417a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora// Returns true if the 'picture' is actually a view and therefore does 418a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora// not own the memory for pixels. 419a2415724fb3466168b2af5b08bd94ba732c0e753Vikas AroraWEBP_EXTERN(int) WebPPictureIsView(const WebPPicture* picture); 420a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora 421466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora// Rescale a picture to new dimension width x height. 422466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora// Now gamma correction is applied. 423466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora// Returns false in case of error (invalid parameter or insufficient memory). 424a2415724fb3466168b2af5b08bd94ba732c0e753Vikas AroraWEBP_EXTERN(int) WebPPictureRescale(WebPPicture* pic, int width, int height); 4257c970a0a679089e416c5887cf7fcece15a70bfa4Vikas Arora 426466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora// Colorspace conversion function to import RGB samples. 427466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora// Previous buffer will be free'd, if any. 4287c970a0a679089e416c5887cf7fcece15a70bfa4Vikas Arora// *rgb buffer should have a size of at least height * rgb_stride. 429a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora// Returns false in case of memory error. 430466727975bcc57c0c5597bcd0747a2fe4777b303Vikas AroraWEBP_EXTERN(int) WebPPictureImportRGB( 431a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora WebPPicture* picture, const uint8_t* rgb, int rgb_stride); 432a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora// Same, but for RGBA buffer. 433466727975bcc57c0c5597bcd0747a2fe4777b303Vikas AroraWEBP_EXTERN(int) WebPPictureImportRGBA( 434a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora WebPPicture* picture, const uint8_t* rgba, int rgba_stride); 435a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora// Same, but for RGBA buffer. Imports the RGB direct from the 32-bit format 436a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora// input buffer ignoring the alpha channel. Avoids needing to copy the data 437a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora// to a temporary 24-bit RGB buffer to import the RGB only. 438a2415724fb3466168b2af5b08bd94ba732c0e753Vikas AroraWEBP_EXTERN(int) WebPPictureImportRGBX( 439a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora WebPPicture* picture, const uint8_t* rgbx, int rgbx_stride); 440a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora 441a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora// Variants of the above, but taking BGR(A|X) input. 442466727975bcc57c0c5597bcd0747a2fe4777b303Vikas AroraWEBP_EXTERN(int) WebPPictureImportBGR( 443a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora WebPPicture* picture, const uint8_t* bgr, int bgr_stride); 444466727975bcc57c0c5597bcd0747a2fe4777b303Vikas AroraWEBP_EXTERN(int) WebPPictureImportBGRA( 445a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora WebPPicture* picture, const uint8_t* bgra, int bgra_stride); 446a2415724fb3466168b2af5b08bd94ba732c0e753Vikas AroraWEBP_EXTERN(int) WebPPictureImportBGRX( 447a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora WebPPicture* picture, const uint8_t* bgrx, int bgrx_stride); 448a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora 449a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora// Converts picture->argb data to the YUVA format specified by 'colorspace'. 450a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora// Upon return, picture->use_argb is set to false. The presence of real 451a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora// non-opaque transparent values is detected, and 'colorspace' will be 452a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora// adjusted accordingly. Note that this method is lossy. 453a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora// Returns false in case of error. 454a2415724fb3466168b2af5b08bd94ba732c0e753Vikas AroraWEBP_EXTERN(int) WebPPictureARGBToYUVA(WebPPicture* picture, 455a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora WebPEncCSP colorspace); 456a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora 4578b720228d581a84fd173b6dcb2fa295b59db489aVikas Arora// Same as WebPPictureARGBToYUVA(), but the conversion is done using 4588b720228d581a84fd173b6dcb2fa295b59db489aVikas Arora// pseudo-random dithering with a strength 'dithering' between 4598b720228d581a84fd173b6dcb2fa295b59db489aVikas Arora// 0.0 (no dithering) and 1.0 (maximum dithering). This is useful 4608b720228d581a84fd173b6dcb2fa295b59db489aVikas Arora// for photographic picture. 4618b720228d581a84fd173b6dcb2fa295b59db489aVikas AroraWEBP_EXTERN(int) WebPPictureARGBToYUVADithered( 4628b720228d581a84fd173b6dcb2fa295b59db489aVikas Arora WebPPicture* picture, WebPEncCSP colorspace, float dithering); 4638b720228d581a84fd173b6dcb2fa295b59db489aVikas Arora 464a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora// Converts picture->yuv to picture->argb and sets picture->use_argb to true. 465a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora// The input format must be YUV_420 or YUV_420A. 466a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora// Note that the use of this method is discouraged if one has access to the 467a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora// raw ARGB samples, since using YUV420 is comparatively lossy. Also, the 468a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora// conversion from YUV420 to ARGB incurs a small loss too. 469a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora// Returns false in case of error. 470a2415724fb3466168b2af5b08bd94ba732c0e753Vikas AroraWEBP_EXTERN(int) WebPPictureYUVAToARGB(WebPPicture* picture); 471a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora 472af51b94a435132e9014c324e25fb686b3d07a8c8Vikas Arora// Helper function: given a width x height plane of RGBA or YUV(A) samples 473af51b94a435132e9014c324e25fb686b3d07a8c8Vikas Arora// clean-up the YUV or RGB samples under fully transparent area, to help 474af51b94a435132e9014c324e25fb686b3d07a8c8Vikas Arora// compressibility (no guarantee, though). 475a2415724fb3466168b2af5b08bd94ba732c0e753Vikas AroraWEBP_EXTERN(void) WebPCleanupTransparentArea(WebPPicture* picture); 476a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora 477a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora// Scan the picture 'picture' for the presence of non fully opaque alpha values. 478a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora// Returns true in such case. Otherwise returns false (indicating that the 479a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora// alpha plane can be ignored altogether e.g.). 480a2415724fb3466168b2af5b08bd94ba732c0e753Vikas AroraWEBP_EXTERN(int) WebPPictureHasTransparency(const WebPPicture* picture); 481a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora 4828b720228d581a84fd173b6dcb2fa295b59db489aVikas Arora// Remove the transparency information (if present) by blending the color with 4838b720228d581a84fd173b6dcb2fa295b59db489aVikas Arora// the background color 'background_rgb' (specified as 24bit RGB triplet). 4848b720228d581a84fd173b6dcb2fa295b59db489aVikas Arora// After this call, all alpha values are reset to 0xff. 4858b720228d581a84fd173b6dcb2fa295b59db489aVikas AroraWEBP_EXTERN(void) WebPBlendAlpha(WebPPicture* pic, uint32_t background_rgb); 4868b720228d581a84fd173b6dcb2fa295b59db489aVikas Arora 487a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora//------------------------------------------------------------------------------ 4887c970a0a679089e416c5887cf7fcece15a70bfa4Vikas Arora// Main call 4897c970a0a679089e416c5887cf7fcece15a70bfa4Vikas Arora 49088fe2b83c4b9232cd08729556fd0485d6a6a92cdVikas Arora// Main encoding call, after config and picture have been initialized. 491a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora// 'picture' must be less than 16384x16384 in dimension (cf WEBP_MAX_DIMENSION), 492a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora// and the 'config' object must be a valid one. 4937c970a0a679089e416c5887cf7fcece15a70bfa4Vikas Arora// Returns false in case of error, true otherwise. 494466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora// In case of error, picture->error_code is updated accordingly. 495a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora// 'picture' can hold the source samples in both YUV(A) or ARGB input, depending 496a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora// on the value of 'picture->use_argb'. It is highly recommended to use 497a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora// the former for lossy encoding, and the latter for lossless encoding 498a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora// (when config.lossless is true). Automatic conversion from one format to 499a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora// another is provided but they both incur some loss. 500a2415724fb3466168b2af5b08bd94ba732c0e753Vikas AroraWEBP_EXTERN(int) WebPEncode(const WebPConfig* config, WebPPicture* picture); 501a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora 502a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora//------------------------------------------------------------------------------ 5037c970a0a679089e416c5887cf7fcece15a70bfa4Vikas Arora 5048b720228d581a84fd173b6dcb2fa295b59db489aVikas Arora#ifdef __cplusplus 5057c970a0a679089e416c5887cf7fcece15a70bfa4Vikas Arora} // extern "C" 5067c970a0a679089e416c5887cf7fcece15a70bfa4Vikas Arora#endif 5077c970a0a679089e416c5887cf7fcece15a70bfa4Vikas Arora 5087c970a0a679089e416c5887cf7fcece15a70bfa4Vikas Arora#endif /* WEBP_WEBP_ENCODE_H_ */ 509