Graphviz 12.0.1~dev.20240716.0800
Loading...
Searching...
No Matches
gvdevice_webp.c
Go to the documentation of this file.
1/*************************************************************************
2 * Copyright (c) 2011 AT&T Intellectual Property
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Eclipse Public License v1.0
5 * which accompanies this distribution, and is available at
6 * https://www.eclipse.org/legal/epl-v10.html
7 *
8 * Contributors: Details at https://graphviz.org
9 *************************************************************************/
10
11#include "config.h"
12
13#include <gvc/gvplugin_device.h>
14#include <gvc/gvio.h>
15
16#ifdef HAVE_WEBP
17#include "webp/encode.h"
18
19static const char* const kErrorMessages[] = {
20 "OK",
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"
36};
37
38typedef enum {
39 FORMAT_WEBP,
41
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;
44}
45
46static void webp_format(GVJ_t * job)
47{
48 WebPPicture picture;
49 WebPPreset preset;
50 WebPConfig config;
51 int stride;
52
53 if (!WebPPictureInit(&picture) || !WebPConfigInit(&config)) {
54 fprintf(stderr, "Error! Version mismatch!\n");
55 goto Error;
56 }
57
58 // if either dimension exceeds the WebP API, map this to one of its errors
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]);
63 goto Error;
64 }
65
66 picture.width = (int)job->width;
67 picture.height = (int)job->height;
68 stride = 4 * (int)job->width;
69
70 picture.writer = writer;
71 picture.custom_ptr = job;
72
73 preset = WEBP_PRESET_DRAWING;
74
75 if (!WebPConfigPreset(&config, preset, config.quality)) {
76 fprintf(stderr, "Error! Could initialize configuration with preset.\n");
77 goto Error;
78 }
79
80 if (!WebPValidateConfig(&config)) {
81 fprintf(stderr, "Error! Invalid configuration.\n");
82 goto Error;
83 }
84
85 if (!WebPPictureAlloc(&picture)) {
86 fprintf(stderr, "Error! Cannot allocate memory\n");
87 return;
88 }
89
90 if (!WebPPictureImportBGRA(&picture,
91 (const uint8_t * const)job->imagedata, stride)) {
92 fprintf(stderr, "Error! Cannot import picture\n");
93 goto Error;
94 }
95
96 if (!WebPEncode(&config, &picture)) {
97 fprintf(stderr, "Error! Cannot encode picture as WebP\n");
98 fprintf(stderr, "Error code: %d (%s)\n",
99 picture.error_code, kErrorMessages[picture.error_code]);
100 goto Error;
101 }
102
103Error:
104 WebPPictureFree(&picture);
105}
106
107static gvdevice_engine_t webp_engine = {
108 NULL, /* webp_initialize */
109 webp_format,
110 NULL, /* webp_finalize */
111};
112
113static gvdevice_features_t device_features_webp = {
116 | GVDEVICE_DOES_TRUECOLOR,/* flags */
117 {0.,0.}, /* default margin - points */
118 {0.,0.}, /* default page width, height - points */
119 {96.,96.}, /* 96 dpi */
120};
121#endif
122
124#ifdef HAVE_WEBP
125 {FORMAT_WEBP, "webp:cairo", 1, &webp_engine, &device_features_webp},
126#endif
127 {0, NULL, 0, NULL, NULL}
128};
void error(int level, const char *s,...)
Definition error.c:83
node NULL
Definition grammar.y:149
#define GVDEVICE_NO_WRITER
Definition gvcjob.h:93
#define GVDEVICE_DOES_TRUECOLOR
Definition gvcjob.h:90
#define GVDEVICE_BINARY_FORMAT
Definition gvcjob.h:91
size_t gvwrite(GVJ_t *job, const char *s, size_t len)
Definition gvdevice.c:179
gvplugin_installed_t gvdevice_webp_types[]
format_type
static cairo_status_t writer(void *closure, const unsigned char *data, unsigned int length)
@ Error
Definition parse.h:20
char * imagedata
Definition gvcjob.h:297
unsigned int width
Definition gvcjob.h:327
unsigned int height
Definition gvcjob.h:328
Definition legal.c:50
ingroup plugin_api
Definition gvplugin.h:35