18#include "webp/encode.h"
20static const char*
const kErrorMessages[] = {
22 "OUT_OF_MEMORY: Out of memory allocating objects",
23 "BITSTREAM_OUT_OF_MEMORY: Out of memory re-allocating byte buffer",
24 "NULL_PARAMETER: NULL parameter passed to function",
25 "INVALID_CONFIGURATION: configuration is invalid",
26 "BAD_DIMENSION: Bad picture dimension. Maximum width and height "
27 "allowed is 16383 pixels.",
28 "PARTITION0_OVERFLOW: Partition #0 is too big to fit 512k.\n"
29 "To reduce the size of this partition, try using less segments "
30 "with the -segments option, and eventually reduce the number of "
31 "header bits using -partition_limit. More details are available "
32 "in the manual (`man cwebp`)",
33 "PARTITION_OVERFLOW: Partition is too big to fit 16M",
34 "BAD_WRITE: Picture writer returned an I/O error",
35 "FILE_TOO_BIG: File would be too big to fit in 4G",
36 "USER_ABORT: encoding abort requested by user"
43static int writer(
const uint8_t*
data,
size_t data_size,
const WebPPicture*
const pic) {
44 return gvwrite(pic->custom_ptr, (
const char *)
data, data_size) == data_size ? 1 : 0;
47static void webp_format(
GVJ_t * job)
53 if (!WebPPictureInit(&picture) || !WebPConfigInit(&config)) {
54 fprintf(stderr,
"Error! Version mismatch!\n");
59 if (INT_MAX / BYTES_PER_PIXEL < job->width || job->
height > 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;
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},
Arithmetic helper functions.
#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