Subversion Repositories Games.Descent

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
3 pmbaty 1
// Copyright 2011 Google Inc.
2
//
3
// This code is licensed under the same terms as WebM:
4
//  Software License Agreement:  http://www.webmproject.org/license/software/
5
//  Additional IP Rights Grant:  http://www.webmproject.org/license/additional/
6
// -----------------------------------------------------------------------------
7
//
8
//   WebP encoder: main interface
9
//
10
// Author: Skal (pascal.massimino@gmail.com)
11
 
12
#ifndef WEBP_WEBP_ENCODE_H_
13
#define WEBP_WEBP_ENCODE_H_
14
 
15
#include <stdlib.h>
16
 
17
#include "./types.h"
18
 
19
#if defined(__cplusplus) || defined(c_plusplus)
20
extern "C" {
21
#endif
22
 
23
#define WEBP_ENCODER_ABI_VERSION 0x0003
24
 
25
// Return the encoder's version number, packed in hexadecimal using 8bits for
26
// each of major/minor/revision. E.g: v2.5.7 is 0x020507.
27
WEBP_EXTERN(int) WebPGetEncoderVersion(void);
28
 
29
//------------------------------------------------------------------------------
30
// One-stop-shop call! No questions asked:
31
 
32
// Returns the size of the compressed data (pointed to by *output), or 0 if
33
// an error occurred. The compressed data must be released by the caller
34
// using the call 'free(*output)'.
35
WEBP_EXTERN(size_t) WebPEncodeRGB(const uint8_t* rgb,
36
                                  int width, int height, int stride,
37
                                  float quality_factor, uint8_t** output);
38
WEBP_EXTERN(size_t) WebPEncodeBGR(const uint8_t* bgr,
39
                                  int width, int height, int stride,
40
                                  float quality_factor, uint8_t** output);
41
WEBP_EXTERN(size_t) WebPEncodeRGBA(const uint8_t* rgba,
42
                                   int width, int height, int stride,
43
                                   float quality_factor, uint8_t** output);
44
WEBP_EXTERN(size_t) WebPEncodeBGRA(const uint8_t* bgra,
45
                                   int width, int height, int stride,
46
                                   float quality_factor, uint8_t** output);
47
 
48
//------------------------------------------------------------------------------
49
// Coding parameters
50
 
51
typedef struct {
52
  float quality;         // between 0 (smallest file) and 100 (biggest)
53
  int target_size;       // if non-zero, set the desired target size in bytes.
54
                         // Takes precedence over the 'compression' parameter.
55
  float target_PSNR;     // if non-zero, specifies the minimal distortion to
56
                         // try to achieve. Takes precedence over target_size.
57
  int method;            // quality/speed trade-off (0=fast, 6=slower-better)
58
  int segments;          // maximum number of segments to use, in [1..4]
59
  int sns_strength;      // Spatial Noise Shaping. 0=off, 100=maximum.
60
  int filter_strength;   // range: [0 = off .. 100 = strongest]
61
  int filter_sharpness;  // range: [0 = off .. 7 = least sharp]
62
  int filter_type;       // filtering type: 0 = simple, 1 = strong
63
                         // (only used if filter_strength > 0 or autofilter > 0)
64
  int autofilter;        // Auto adjust filter's strength [0 = off, 1 = on]
65
  int pass;              // number of entropy-analysis passes (in [1..10]).
66
 
67
  int show_compressed;   // if true, export the compressed picture back.
68
                         // In-loop filtering is not applied.
69
  int preprocessing;     // preprocessing filter (0=none, 1=segment-smooth)
70
  int partitions;        // log2(number of token partitions) in [0..3]
71
                         // Default is set to 0 for easier progressive decoding.
72
  int partition_limit;   // quality degradation allowed to fit the 512k limit on
73
                         // prediction modes coding (0=no degradation, 100=full)
74
  int alpha_compression;  // Algorithm for encoding the alpha plane (0 = none,
75
                          // 1 = backward reference counts encoded with
76
                          // arithmetic encoder). Default is 1.
77
  int alpha_quality;      // Between 0 (smallest size) and 100 (lossless).
78
                          // Default is 100.
79
} WebPConfig;
80
 
81
// Enumerate some predefined settings for WebPConfig, depending on the type
82
// of source picture. These presets are used when calling WebPConfigPreset().
83
typedef enum {
84
  WEBP_PRESET_DEFAULT = 0,  // default preset.
85
  WEBP_PRESET_PICTURE,      // digital picture, like portrait, inner shot
86
  WEBP_PRESET_PHOTO,        // outdoor photograph, with natural lighting
87
  WEBP_PRESET_DRAWING,      // hand or line drawing, with high-contrast details
88
  WEBP_PRESET_ICON,         // small-sized colorful images
89
  WEBP_PRESET_TEXT          // text-like
90
} WebPPreset;
91
 
92
// Internal, version-checked, entry point
93
WEBP_EXTERN(int) WebPConfigInitInternal(
94
    WebPConfig* const, WebPPreset, float, int);
95
 
96
// Should always be called, to initialize a fresh WebPConfig structure before
97
// modification. Returns 0 in case of version mismatch. WebPConfigInit() must
98
// have succeeded before using the 'config' object.
99
static WEBP_INLINE int WebPConfigInit(WebPConfig* const config) {
100
  return WebPConfigInitInternal(config, WEBP_PRESET_DEFAULT, 75.f,
101
                                WEBP_ENCODER_ABI_VERSION);
102
}
103
 
104
// This function will initialize the configuration according to a predefined
105
// set of parameters (referred to by 'preset') and a given quality factor.
106
// This function can be called as a replacement to WebPConfigInit(). Will
107
// return 0 in case of error.
108
static WEBP_INLINE int WebPConfigPreset(WebPConfig* const config,
109
                                        WebPPreset preset, float quality) {
110
  return WebPConfigInitInternal(config, preset, quality,
111
                                WEBP_ENCODER_ABI_VERSION);
112
}
113
 
114
// Returns 1 if all parameters are in valid range and the configuration is OK.
115
WEBP_EXTERN(int) WebPValidateConfig(const WebPConfig* const config);
116
 
117
//------------------------------------------------------------------------------
118
// Input / Output
119
 
120
typedef struct WebPPicture WebPPicture;   // main structure for I/O
121
 
122
// non-essential structure for storing auxiliary statistics
123
typedef struct {
124
  float PSNR[4];          // peak-signal-to-noise ratio for Y/U/V/All
125
  int coded_size;         // final size
126
  int block_count[3];     // number of intra4/intra16/skipped macroblocks
127
  int header_bytes[2];    // approximate number of bytes spent for header
128
                          // and mode-partition #0
129
  int residual_bytes[3][4];  // approximate number of bytes spent for
130
                             // DC/AC/uv coefficients for each (0..3) segments.
131
  int segment_size[4];    // number of macroblocks in each segments
132
  int segment_quant[4];   // quantizer values for each segments
133
  int segment_level[4];   // filtering strength for each segments [0..63]
134
 
135
  int alpha_data_size;    // size of the transparency data
136
  int layer_data_size;    // size of the enhancement layer data
137
 
138
  void* user_data;        // this field is free to be set to any value and
139
                          // used during callbacks (like progress-report e.g.).
140
} WebPAuxStats;
141
 
142
// Signature for output function. Should return 1 if writing was successful.
143
// data/data_size is the segment of data to write, and 'picture' is for
144
// reference (and so one can make use of picture->custom_ptr).
145
typedef int (*WebPWriterFunction)(const uint8_t* data, size_t data_size,
146
                                  const WebPPicture* const picture);
147
 
148
// Progress hook, called from time to time to report progress. It can return 0
149
// to request an abort of the encoding process, or 1 otherwise if all is OK.
150
typedef int (*WebPProgressHook)(int percent, const WebPPicture* const picture);
151
 
152
typedef enum {
153
  // chroma sampling
154
  WEBP_YUV420 = 0,   // 4:2:0
155
  WEBP_YUV422 = 1,   // 4:2:2
156
  WEBP_YUV444 = 2,   // 4:4:4
157
  WEBP_YUV400 = 3,   // grayscale
158
  WEBP_CSP_UV_MASK = 3,   // bit-mask to get the UV sampling factors
159
  // alpha channel variants
160
  WEBP_YUV420A = 4,
161
  WEBP_YUV422A = 5,
162
  WEBP_YUV444A = 6,
163
  WEBP_YUV400A = 7,   // grayscale + alpha
164
  WEBP_CSP_ALPHA_BIT = 4   // bit that is set if alpha is present
165
} WebPEncCSP;
166
 
167
// Encoding error conditions.
168
typedef enum {
169
  VP8_ENC_OK = 0,
170
  VP8_ENC_ERROR_OUT_OF_MEMORY,            // memory error allocating objects
171
  VP8_ENC_ERROR_BITSTREAM_OUT_OF_MEMORY,  // memory error while flushing bits
172
  VP8_ENC_ERROR_NULL_PARAMETER,           // a pointer parameter is NULL
173
  VP8_ENC_ERROR_INVALID_CONFIGURATION,    // configuration is invalid
174
  VP8_ENC_ERROR_BAD_DIMENSION,            // picture has invalid width/height
175
  VP8_ENC_ERROR_PARTITION0_OVERFLOW,      // partition is bigger than 512k
176
  VP8_ENC_ERROR_PARTITION_OVERFLOW,       // partition is bigger than 16M
177
  VP8_ENC_ERROR_BAD_WRITE,                // error while flushing bytes
178
  VP8_ENC_ERROR_FILE_TOO_BIG,             // file is bigger than 4G
179
  VP8_ENC_ERROR_USER_ABORT,               // abort request by user
180
  VP8_ENC_ERROR_LAST                      // list terminator. always last.
181
} WebPEncodingError;
182
 
183
// maximum width/height allowed (inclusive), in pixels
184
#define WEBP_MAX_DIMENSION 16383
185
 
186
struct WebPPicture {
187
  // input
188
  WebPEncCSP colorspace;     // colorspace: should be YUV420 for now (=Y'CbCr).
189
  int width, height;         // dimensions (less or equal to WEBP_MAX_DIMENSION)
190
  uint8_t *y, *u, *v;        // pointers to luma/chroma planes.
191
  int y_stride, uv_stride;   // luma/chroma strides.
192
  uint8_t *a;                // pointer to the alpha plane
193
  int a_stride;              // stride of the alpha plane
194
 
195
  // output
196
  WebPWriterFunction writer;  // can be NULL
197
  void* custom_ptr;           // can be used by the writer.
198
 
199
  // map for extra information
200
  int extra_info_type;    // 1: intra type, 2: segment, 3: quant
201
                          // 4: intra-16 prediction mode,
202
                          // 5: chroma prediction mode,
203
                          // 6: bit cost, 7: distortion
204
  uint8_t* extra_info;    // if not NULL, points to an array of size
205
                          // ((width + 15) / 16) * ((height + 15) / 16) that
206
                          // will be filled with a macroblock map, depending
207
                          // on extra_info_type.
208
 
209
  // where to store statistics, if not NULL:
210
  WebPAuxStats* stats;
211
 
212
  // original samples (for non-YUV420 modes)
213
  uint8_t *u0, *v0;
214
  int uv0_stride;
215
 
216
  WebPEncodingError error_code;   // error code in case of problem.
217
 
218
  WebPProgressHook progress_hook;  // if not NULL, called while encoding.
219
};
220
 
221
// Internal, version-checked, entry point
222
WEBP_EXTERN(int) WebPPictureInitInternal(WebPPicture* const, int);
223
 
224
// Should always be called, to initialize the structure. Returns 0 in case of
225
// version mismatch. WebPPictureInit() must have succeeded before using the
226
// 'picture' object.
227
static WEBP_INLINE int WebPPictureInit(WebPPicture* const picture) {
228
  return WebPPictureInitInternal(picture, WEBP_ENCODER_ABI_VERSION);
229
}
230
 
231
//------------------------------------------------------------------------------
232
// WebPPicture utils
233
 
234
// Convenience allocation / deallocation based on picture->width/height:
235
// Allocate y/u/v buffers as per colorspace/width/height specification.
236
// Note! This function will free the previous buffer if needed.
237
// Returns 0 in case of memory error.
238
WEBP_EXTERN(int) WebPPictureAlloc(WebPPicture* const picture);
239
 
240
// Release memory allocated by WebPPictureAlloc() or WebPPictureImport*()
241
// Note that this function does _not_ free the memory pointed to by 'picture'.
242
WEBP_EXTERN(void) WebPPictureFree(WebPPicture* const picture);
243
 
244
// Copy the pixels of *src into *dst, using WebPPictureAlloc.
245
// Returns 0 in case of memory allocation error.
246
WEBP_EXTERN(int) WebPPictureCopy(const WebPPicture* const src,
247
                                 WebPPicture* const dst);
248
 
249
// self-crops a picture to the rectangle defined by top/left/width/height.
250
// Returns 0 in case of memory allocation error, or if the rectangle is
251
// outside of the source picture.
252
WEBP_EXTERN(int) WebPPictureCrop(WebPPicture* const picture,
253
                                 int left, int top, int width, int height);
254
 
255
// Rescale a picture to new dimension width x height.
256
// Now gamma correction is applied.
257
// Returns false in case of error (invalid parameter or insufficient memory).
258
WEBP_EXTERN(int) WebPPictureRescale(WebPPicture* const pic,
259
                                    int width, int height);
260
 
261
// Colorspace conversion function to import RGB samples.
262
// Previous buffer will be free'd, if any.
263
// *rgb buffer should have a size of at least height * rgb_stride.
264
// Returns 0 in case of memory error.
265
WEBP_EXTERN(int) WebPPictureImportRGB(
266
    WebPPicture* const picture, const uint8_t* const rgb, int rgb_stride);
267
// Same, but for RGBA buffer
268
WEBP_EXTERN(int) WebPPictureImportRGBA(
269
    WebPPicture* const picture, const uint8_t* const rgba, int rgba_stride);
270
 
271
// Variant of the above, but taking BGR(A) input:
272
WEBP_EXTERN(int) WebPPictureImportBGR(
273
    WebPPicture* const picture, const uint8_t* const bgr, int bgr_stride);
274
WEBP_EXTERN(int) WebPPictureImportBGRA(
275
    WebPPicture* const picture, const uint8_t* const bgra, int bgra_stride);
276
 
277
//------------------------------------------------------------------------------
278
// Main call
279
 
280
// Main encoding call, after config and picture have been initialized.
281
// 'picture' must be less than 16384x16384 in dimension (cf WEBP_MAX_DIMENSION),
282
// and the 'config' object must be a valid one.
283
// Returns false in case of error, true otherwise.
284
// In case of error, picture->error_code is updated accordingly.
285
WEBP_EXTERN(int) WebPEncode(
286
    const WebPConfig* const config, WebPPicture* const picture);
287
 
288
//------------------------------------------------------------------------------
289
 
290
#if defined(__cplusplus) || defined(c_plusplus)
291
}    // extern "C"
292
#endif
293
 
294
#endif  /* WEBP_WEBP_ENCODE_H_ */