Graphviz 13.0.0~dev.20250210.0415
Loading...
Searching...
No Matches
gvloadimage_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 <assert.h>
14#include <stdbool.h>
15#include <stdio.h>
16#include <stdlib.h>
17#include <string.h>
18#include <util/gv_math.h>
19#include <util/prisize_t.h>
20
22#include <gvc/gvio.h>
23
24#ifdef HAVE_WEBP
25#ifdef HAVE_PANGOCAIRO
26#include <cairo.h>
27#include <webp/decode.h>
28
29static const char* const kStatusMessages[] = {
30 "OK", "OUT_OF_MEMORY", "INVALID_PARAM", "BITSTREAM_ERROR",
31 "UNSUPPORTED_FEATURE", "SUSPENDED", "USER_ABORT", "NOT_ENOUGH_DATA"
32};
33
34enum {
35 FORMAT_WEBP_CAIRO,
36};
37
38static void webp_freeimage(usershape_t *us)
39{
40 cairo_surface_destroy(us->data);
41}
42
43static cairo_surface_t* webp_really_loadimage(const char *in_file, FILE* const in)
44{
45 WebPDecoderConfig config;
46 WebPDecBuffer* const output_buffer = &config.output;
47 WebPBitstreamFeatures* const bitstream = &config.input;
48 VP8StatusCode status = VP8_STATUS_OK;
49 cairo_surface_t *surface = NULL; /* source surface */
50 int ok;
51 void* data = NULL;
52
53 if (!WebPInitDecoderConfig(&config)) {
54 fprintf(stderr, "Error: WebP library version mismatch!\n");
55 return NULL;
56 }
57
58 fseek(in, 0, SEEK_END);
59 long size = ftell(in);
60 if (size < 0) {
61 fprintf(stderr, "Error: WebP could not determine %s size\n", in_file);
62 return NULL;
63 }
64 size_t data_size = (size_t)size;
65 rewind(in);
66 data = malloc(data_size);
67 ok = data_size == 0 || (data != NULL && fread(data, data_size, 1, in) == 1);
68 if (!ok) {
69 fprintf(stderr, "Error: WebP could not read %" PRISIZE_T
70 " bytes of data from %s\n", data_size, in_file);
71 free(data);
72 return NULL;
73 }
74
75 status = WebPGetFeatures(data, data_size, bitstream);
76 if (status != VP8_STATUS_OK) {
77 goto end;
78 }
79
80 output_buffer->colorspace = MODE_RGBA;
81 status = WebPDecode(data, data_size, &config);
82
83 /* FIXME - this is ugly */
84 if (! bitstream->has_alpha) {
85 assert(output_buffer->width >= 0);
86 assert(output_buffer->height >= 0);
87 argb2rgba((size_t)output_buffer->width, (size_t)output_buffer->height,
88 output_buffer->u.RGBA.rgba);
89 }
90
91end:
92 free(data);
93 ok = status == VP8_STATUS_OK;
94 if (!ok) {
95 fprintf(stderr, "Error: WebP decoding of %s failed.\n", in_file);
96 fprintf(stderr, "Status: %d (%s)\n", status, kStatusMessages[status]);
97 return NULL;
98 }
99
100 surface = cairo_image_surface_create_for_data (
101 output_buffer->u.RGBA.rgba,
102 CAIRO_FORMAT_ARGB32,
103 output_buffer->width,
104 output_buffer->height,
105 output_buffer->u.RGBA.stride);
106
107 return surface;
108}
109
110/* get image either from cached surface, or from freskly loaded surface */
111static cairo_surface_t* webp_loadimage(GVJ_t * job, usershape_t *us)
112{
113 cairo_surface_t *surface = NULL; /* source surface */
114
115 assert(job);
116 assert(us);
117 assert(us->name);
118
119 if (us->data) {
120 if (us->datafree == webp_freeimage)
121 surface = us->data; /* use cached data */
122 else {
123 us->datafree(us); /* free incompatible cache data */
124 us->datafree = NULL;
125 us->data = NULL;
126 }
127 }
128 if (!surface) { /* read file into cache */
130 return NULL;
131 switch (us->type) {
132 case FT_WEBP:
133 if ((surface = webp_really_loadimage(us->name, us->f)))
134 cairo_surface_reference(surface);
135 break;
136 default:
137 surface = NULL;
138 }
139 if (surface) {
140 us->data = surface;
141 us->datafree = webp_freeimage;
142 }
144 }
145 return surface;
146}
147
148/* paint image into required location in graph */
149static void webp_loadimage_cairo(GVJ_t * job, usershape_t *us, boxf b, bool filled)
150{
151 (void)filled;
152
153 cairo_t *cr = job->context; /* target context */
154 cairo_surface_t *surface; /* source surface */
155
156 surface = webp_loadimage(job, us);
157 if (surface) {
158 cairo_save(cr);
159 cairo_translate(cr, b.LL.x, -b.UR.y);
160 cairo_scale(cr, (b.UR.x - b.LL.x) / us->w, (b.UR.y - b.LL.y) / us->h);
161 cairo_set_source_surface (cr, surface, 0, 0);
162 cairo_paint (cr);
163 cairo_restore(cr);
164 }
165}
166
167static gvloadimage_engine_t engine_webp = {
168 webp_loadimage_cairo
169};
170#endif
171#endif
172
174#ifdef HAVE_WEBP
175#ifdef HAVE_PANGOCAIRO
176 {FORMAT_WEBP_CAIRO, "webp:cairo", 1, &engine_webp, NULL},
177#endif
178#endif
179 {0, NULL, 0, NULL, NULL}
180};
void * malloc(YYSIZE_T)
void free(void *)
node NULL
Definition grammar.y:163
void gvusershape_file_release(usershape_t *us)
bool gvusershape_file_access(usershape_t *us)
bool ok(Agraph_t *g)
Definition gv.cpp:362
Arithmetic helper functions.
static void argb2rgba(size_t width, size_t height, unsigned char *data)
Definition gv_math.h:105
gvplugin_installed_t gvloadimage_webp_types[]
#define PRISIZE_T
Definition prisize_t.h:27
void * context
Definition gvcjob.h:295
Definition geom.h:41
pointf UR
Definition geom.h:41
pointf LL
Definition geom.h:41
Definition legal.c:50
ingroup plugin_api
Definition gvplugin.h:35
double x
Definition geom.h:29
double y
Definition geom.h:29
const char * name
Definition usershape.h:54
FILE * f
Definition usershape.h:58
void(* datafree)(usershape_t *us)
Definition usershape.h:65
void * data
Definition usershape.h:63
imagetype_t type
Definition usershape.h:59
double h
Definition usershape.h:61
double w
Definition usershape.h:61
@ FT_WEBP
Definition usershape.h:27