Graphviz 14.1.6~dev.20260420.0039
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 v2.0
5 * which accompanies this distribution, and is available at
6 * https://www.eclipse.org/org/documents/epl-2.0/EPL-2.0.html
7 *
8 * Contributors: Details at https://graphviz.org
9 *************************************************************************/
10
11#include "config.h"
12
13#include <assert.h>
14#include <inttypes.h>
15#include <stdbool.h>
16#include <stdint.h>
17#include <stdio.h>
18#include <stdlib.h>
19#include <string.h>
20#include <util/gv_ftell.h>
21#include <util/gv_math.h>
22#include <util/prisize_t.h>
23
25#include <gvc/gvio.h>
26
27#ifdef HAVE_WEBP
28#ifdef HAVE_PANGOCAIRO
29#include <cairo.h>
30#include <webp/decode.h>
31
32static const char* const kStatusMessages[] = {
33 "OK", "OUT_OF_MEMORY", "INVALID_PARAM", "BITSTREAM_ERROR",
34 "UNSUPPORTED_FEATURE", "SUSPENDED", "USER_ABORT", "NOT_ENOUGH_DATA"
35};
36
37enum {
38 FORMAT_WEBP_CAIRO,
39};
40
41static void webp_freeimage(usershape_t *us)
42{
43 cairo_surface_destroy(us->data);
44}
45
46static cairo_surface_t* webp_really_loadimage(const char *in_file, FILE* const in)
47{
48 WebPDecoderConfig config;
49 WebPDecBuffer* const output_buffer = &config.output;
50 WebPBitstreamFeatures* const bitstream = &config.input;
51 VP8StatusCode status = VP8_STATUS_OK;
52 cairo_surface_t *surface = NULL; /* source surface */
53 int ok;
54 void* data = NULL;
55
56 if (!WebPInitDecoderConfig(&config)) {
57 fprintf(stderr, "Error: WebP library version mismatch!\n");
58 return NULL;
59 }
60
61 fseek(in, 0, SEEK_END);
62 const int64_t data_size = gv_ftell(in);
63 if (data_size < 0) {
64 fprintf(stderr, "Error: WebP could not read %s\n", in_file);
65 return NULL;
66 }
67 rewind(in);
68 data = malloc((size_t)data_size);
69 ok = data_size == 0 || (data != NULL && fread(data, (size_t)data_size, 1, in) == 1);
70 if (!ok) {
71 fprintf(stderr, "Error: WebP could not read %" PRId64
72 " bytes of data from %s\n", data_size, in_file);
73 free(data);
74 return NULL;
75 }
76
77 status = WebPGetFeatures(data, (size_t)data_size, bitstream);
78 if (status != VP8_STATUS_OK) {
79 goto end;
80 }
81
82 output_buffer->colorspace = MODE_RGBA;
83 status = WebPDecode(data, (size_t)data_size, &config);
84
85 /* FIXME - this is ugly */
86 if (! bitstream->has_alpha) {
87 assert(output_buffer->width >= 0);
88 assert(output_buffer->height >= 0);
89 argb2rgba((size_t)output_buffer->width, (size_t)output_buffer->height,
90 output_buffer->u.RGBA.rgba);
91 }
92
93end:
94 free(data);
95 ok = status == VP8_STATUS_OK;
96 if (!ok) {
97 fprintf(stderr, "Error: WebP decoding of %s failed.\n", in_file);
98 fprintf(stderr, "Status: %d (%s)\n", status, kStatusMessages[status]);
99 return NULL;
100 }
101
102 surface = cairo_image_surface_create_for_data (
103 output_buffer->u.RGBA.rgba,
104 CAIRO_FORMAT_ARGB32,
105 output_buffer->width,
106 output_buffer->height,
107 output_buffer->u.RGBA.stride);
108
109 return surface;
110}
111
112// get image either from cached surface, or from freshly loaded surface
113static cairo_surface_t *webp_loadimage(usershape_t *us) {
114 cairo_surface_t *surface = NULL; /* source surface */
115
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(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};
static int in(Extype_t lhs, Exid_t *rhs, Exdisc_t *disc)
Definition compile.c:1641
void * malloc(YYSIZE_T)
void free(void *)
node NULL
Definition grammar.y:181
void gvusershape_file_release(usershape_t *us)
bool gvusershape_file_access(usershape_t *us)
bool ok(Agraph_t *g)
Definition gv.cpp:366
Abstraction over ftell
static int64_t gv_ftell(FILE *stream)
ftell, accounting for platform limitations
Definition gv_ftell.h:12
Arithmetic helper functions.
static void argb2rgba(size_t width, size_t height, unsigned char *data)
Definition gv_math.h:109
gvplugin_installed_t gvloadimage_webp_types[]
void * context
Definition gvcjob.h:295
Definition geom.h:41
pointf UR
Definition geom.h:41
pointf LL
Definition geom.h:41
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