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