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