Graphviz 13.0.0~dev.20241220.2304
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, job->imagedata, stride)) {
91 fprintf(stderr, "Error! Cannot import picture\n");
92 goto Error;
93 }
94
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]);
99 goto Error;
100 }
101
102Error:
103 WebPPictureFree(&picture);
104}
105
106static gvdevice_engine_t webp_engine = {
107 NULL, /* webp_initialize */
108 webp_format,
109 NULL, /* webp_finalize */
110};
111
112static gvdevice_features_t device_features_webp = {
115 | GVDEVICE_DOES_TRUECOLOR,/* flags */
116 {0.,0.}, /* default margin - points */
117 {0.,0.}, /* default page width, height - points */
118 {96.,96.}, /* 96 dpi */
119};
120#endif
121
123#ifdef HAVE_WEBP
124 {FORMAT_WEBP, "webp:cairo", 1, &webp_engine, &device_features_webp},
125#endif
126 {0, NULL, 0, NULL, NULL}
127};
node NULL
Definition grammar.y:163
#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:180
gvplugin_installed_t gvdevice_webp_types[]
format_type
static cairo_status_t writer(void *closure, const unsigned char *data, unsigned int length)
table Syntax error
Definition htmlparse.y:294
@ Error
Definition parse.h:21
unsigned char * imagedata
location of 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