17#include "webp/encode.h"
19static const char*
const kErrorMessages[] = {
21 "OUT_OF_MEMORY: Out of memory allocating objects",
22 "BITSTREAM_OUT_OF_MEMORY: Out of memory re-allocating byte buffer",
23 "NULL_PARAMETER: NULL parameter passed to function",
24 "INVALID_CONFIGURATION: configuration is invalid",
25 "BAD_DIMENSION: Bad picture dimension. Maximum width and height "
26 "allowed is 16383 pixels.",
27 "PARTITION0_OVERFLOW: Partition #0 is too big to fit 512k.\n"
28 "To reduce the size of this partition, try using less segments "
29 "with the -segments option, and eventually reduce the number of "
30 "header bits using -partition_limit. More details are available "
31 "in the manual (`man cwebp`)",
32 "PARTITION_OVERFLOW: Partition is too big to fit 16M",
33 "BAD_WRITE: Picture writer returned an I/O error",
34 "FILE_TOO_BIG: File would be too big to fit in 4G",
35 "USER_ABORT: encoding abort requested by user"
42static int writer(
const uint8_t*
data,
size_t data_size,
const WebPPicture*
const pic) {
43 return gvwrite(pic->custom_ptr, (
const char *)
data, data_size) == data_size ? 1 : 0;
46static void webp_format(
GVJ_t * job)
53 if (!WebPPictureInit(&picture) || !WebPConfigInit(&config)) {
54 fprintf(stderr,
"Error! Version mismatch!\n");
59 if ((
unsigned)INT_MAX / 4 < job->
width || job->
height > (
unsigned)INT_MAX) {
60 int error = VP8_ENC_ERROR_BAD_DIMENSION;
61 fprintf(stderr,
"Error! Cannot encode picture as WebP\n");
62 fprintf(stderr,
"Error code: %d (%s)\n",
error, kErrorMessages[
error]);
66 picture.width = (int)job->
width;
67 picture.height = (int)job->
height;
68 stride = 4 * (int)job->
width;
71 picture.custom_ptr = job;
73 preset = WEBP_PRESET_DRAWING;
75 if (!WebPConfigPreset(&config, preset, config.quality)) {
76 fprintf(stderr,
"Error! Could initialize configuration with preset.\n");
80 if (!WebPValidateConfig(&config)) {
81 fprintf(stderr,
"Error! Invalid configuration.\n");
85 if (!WebPPictureAlloc(&picture)) {
86 fprintf(stderr,
"Error! Cannot allocate memory\n");
90 if (!WebPPictureImportBGRA(&picture, job->
imagedata, stride)) {
91 fprintf(stderr,
"Error! Cannot import picture\n");
95 if (!WebPEncode(&config, &picture)) {
96 fprintf(stderr,
"Error! Cannot encode picture as WebP\n");
97 fprintf(stderr,
"Error code: %d (%s)\n",
98 picture.error_code, kErrorMessages[picture.error_code]);
103 WebPPictureFree(&picture);
124 {FORMAT_WEBP,
"webp:cairo", 1, &webp_engine, &device_features_webp},
#define GVDEVICE_NO_WRITER
#define GVDEVICE_DOES_TRUECOLOR
#define GVDEVICE_BINARY_FORMAT
size_t gvwrite(GVJ_t *job, const char *s, size_t len)
gvplugin_installed_t gvdevice_webp_types[]
static cairo_status_t writer(void *closure, const unsigned char *data, unsigned int length)
unsigned char * imagedata
location of imagedata