Graphviz 13.0.0~dev.20250210.0415
Loading...
Searching...
No Matches
gvloadimage_gd.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 <stdlib.h>
16#include <string.h>
17#include <util/alloc.h>
18
19#ifdef HAVE_PANGOCAIRO
20#include <cairo.h>
21#endif
22
24#include <gvc/gvio.h>
25#include <gd.h>
26
27enum {
31};
32
33static void gd_freeimage(usershape_t *us)
34{
35 gdImageDestroy(us->data);
36}
37
38static gdImagePtr gd_loadimage(GVJ_t * job, usershape_t *us)
39{
40 assert(job);
41 assert(us);
42 assert(us->name);
43
44 if (us->data) {
45 if (us->datafree != gd_freeimage) {
46 us->datafree(us); /* free incompatible cache data */
47 us->data = NULL;
48 us->datafree = NULL;
49 }
50 }
51 if (!us->data) { /* read file into cache */
53 return NULL;
54 switch (us->type) {
55#ifdef HAVE_GD_PNG
56 case FT_PNG:
57 us->data = gdImageCreateFromPng(us->f);
58 break;
59#endif
60#ifdef HAVE_GD_GIF
61 case FT_GIF:
62 us->data = gdImageCreateFromGif(us->f);
63 break;
64#endif
65#ifdef HAVE_GD_JPEG
66 case FT_JPEG:
67 us->data = gdImageCreateFromJpeg(us->f);
68 break;
69#endif
70 default:
71 break;
72 }
73 if (us->data)
75
77 }
78 return us->data;
79}
80
81static gdImagePtr gd_rotateimage(gdImagePtr im, int rotation)
82{
83 gdImagePtr im2 = gdImageCreate(im->sy, im->sx);
84
85 gdImageCopyRotated(im2, im, im2->sx / 2., im2->sy / 2.,
86 0, 0, im->sx, im->sy, rotation);
87 gdImageDestroy(im);
88 return im2;
89}
90
91static void gd_loadimage_gd(GVJ_t * job, usershape_t *us, boxf b, bool filled)
92{
93 (void)filled;
94
95 gdImagePtr im2, im = job->context;
96
97 if ((im2 = gd_loadimage(job, us))) {
98 if (job->rotation) {
99 im2 = gd_rotateimage(im2, job->rotation);
100 us->data = im2;
101 }
102 gdImageCopyResized(im, im2, ROUND(b.LL.x), ROUND(b.LL.y), 0, 0,
103 ROUND(b.UR.x - b.LL.x), ROUND(b.UR.y - b.LL.y), im2->sx, im2->sy);
104 }
105}
106
107#ifdef HAVE_PANGOCAIRO
108static void gd_loadimage_cairo(GVJ_t * job, usershape_t *us, boxf b, bool filled)
109{
110 (void)filled;
111
112 cairo_t *cr = job->context; /* target context */
113 int x, y, width, height;
114 cairo_surface_t *surface; /* source surface */
115 gdImagePtr im;
116
117 if ((im = gd_loadimage(job, us))) {
118 width = im->sx;
119 height = im->sy;
120 const int stride = cairo_format_stride_for_width(CAIRO_FORMAT_ARGB32,
121 width);
122 assert(stride >= 0);
123 assert(height >= 0);
124 unsigned char *data = gv_calloc((size_t)stride, (size_t)height);
125 surface = cairo_image_surface_create_for_data (data, CAIRO_FORMAT_ARGB32,
126 width, height, stride);
127 unsigned char *const orig_data = data;
128
129 if (im->trueColor) {
130 if (im->saveAlphaFlag) {
131 for (y = 0; y < height; y++) {
132 for (x = 0; x < width; x++) {
133 const int px = gdImageTrueColorPixel(im, x, y);
134 *data++ = (unsigned char)gdTrueColorGetBlue(px);
135 *data++ = (unsigned char)gdTrueColorGetGreen(px);
136 *data++ = (unsigned char)gdTrueColorGetRed(px);
137 // gd’s alpha is 7-bit, so scale up ×2 to our 8-bit
138 *data++ = (unsigned char)((0x7F - gdTrueColorGetAlpha(px)) << 1);
139 }
140 }
141 }
142 else {
143 for (y = 0; y < height; y++) {
144 for (x = 0; x < width; x++) {
145 const int px = gdImageTrueColorPixel(im, x, y);
146 *data++ = (unsigned char)gdTrueColorGetBlue(px);
147 *data++ = (unsigned char)gdTrueColorGetGreen(px);
148 *data++ = (unsigned char)gdTrueColorGetRed(px);
149 *data++ = 0xFF;
150 }
151 }
152 }
153 }
154 else {
155 for (y = 0; y < height; y++) {
156 for (x = 0; x < width; x++) {
157 const int px = gdImagePalettePixel(im, x, y);
158 *data++ = (unsigned char)im->blue[px];
159 *data++ = (unsigned char)im->green[px];
160 *data++ = (unsigned char)im->red[px];
161 *data++ = px == im->transparent ? 0x00 : 0xff;
162 }
163 }
164 }
165
166 cairo_save(cr);
167 cairo_translate(cr, b.LL.x, -b.UR.y);
168 cairo_scale(cr, (b.UR.x - b.LL.x) / us->w, (b.UR.y - b.LL.y) / us->h);
169 cairo_set_source_surface (cr, surface, 0, 0);
170 cairo_paint (cr);
171 cairo_restore(cr);
172
173 cairo_surface_destroy(surface);
174 free(orig_data);
175 }
176}
177#endif
178
179static void gd_loadimage_ps(GVJ_t * job, usershape_t *us, boxf b, bool filled)
180{
181 (void)filled;
182
183 gdImagePtr im = NULL;
184 int X, Y, x, y, px;
185
186 if ((im = gd_loadimage(job, us))) {
187 X = im->sx;
188 Y = im->sy;
189
190 gvputs(job, "save\n");
191
192 /* define image data as string array (one per raster line) */
193 gvputs(job, "/myctr 0 def\n");
194 gvputs(job, "/myarray [\n");
195 if (im->trueColor) {
196 for (y = 0; y < Y; y++) {
197 gvputs(job, "<");
198 for (x = 0; x < X; x++) {
199 px = gdImageTrueColorPixel(im, x, y);
200 gvprintf(job, "%02x%02x%02x",
201 gdTrueColorGetRed(px),
202 gdTrueColorGetGreen(px),
203 gdTrueColorGetBlue(px));
204 }
205 gvputs(job, ">\n");
206 }
207 }
208 else {
209 for (y = 0; y < Y; y++) {
210 gvputs(job, "<");
211 for (x = 0; x < X; x++) {
212 px = gdImagePalettePixel(im, x, y);
213 gvprintf(job, "%02x%02x%02x",
214 im->red[px],
215 im->green[px],
216 im->blue[px]);
217 }
218 gvputs(job, ">\n");
219 }
220 }
221 gvputs(job, "] def\n");
222 gvputs(job,"/myproc { myarray myctr get /myctr myctr 1 add def } def\n");
223
224 /* this sets the position of the image */
225 gvprintf(job, "%g %g translate\n",
226 b.LL.x + (b.UR.x - b.LL.x) * (1. - (job->dpi.x) / 96.) / 2.,
227 b.LL.y + (b.UR.y - b.LL.y) * (1. - (job->dpi.y) / 96.) / 2.);
228
229 /* this sets the rendered size to fit the box */
230 gvprintf(job,"%g %g scale\n",
231 (b.UR.x - b.LL.x) * (job->dpi.x) / 96.,
232 (b.UR.y - b.LL.y) * (job->dpi.y) / 96.);
233
234 /* xsize ysize bits-per-sample [matrix] */
235 gvprintf(job, "%d %d 8 [%d 0 0 %d 0 %d]\n", X, Y, X, -Y, Y);
236
237 gvputs(job, "{myproc} false 3 colorimage\n");
238
239 gvputs(job, "restore\n");
240 }
241}
242
246
250
251#ifdef HAVE_PANGOCAIRO
253 gd_loadimage_cairo
254};
255#endif
256
258 {FORMAT_GD_GD, "gd:gd", 1, &engine, NULL},
259 {FORMAT_GD2_GD, "gd2:gd", 1, &engine, NULL},
260#ifdef HAVE_GD_GIF
261 {FORMAT_GIF_GD, "gif:gd", 1, &engine, NULL},
262#endif
263#ifdef HAVE_GD_JPEG
264 {FORMAT_JPG_GD, "jpeg:gd", 1, &engine, NULL},
265 {FORMAT_JPG_GD, "jpe:gd", 1, &engine, NULL},
266 {FORMAT_JPG_GD, "jpg:gd", 1, &engine, NULL},
267#endif
268#ifdef HAVE_GD_PNG
269 {FORMAT_PNG_GD, "png:gd", 1, &engine, NULL},
270#endif
271#ifdef HAVE_GD_WBMP
272 {FORMAT_WBMP_GD, "wbmp:gd", 1, &engine, NULL},
273#endif
274#ifdef HAVE_GD_XPM
275 {FORMAT_XBM_GD, "xbm:gd", 1, &engine, NULL},
276#endif
277
278 {FORMAT_GD_PS, "gd:ps", 1, &engine_ps, NULL},
279 {FORMAT_GD_PS, "gd:lasi", 1, &engine_ps, NULL},
280 {FORMAT_GD2_PS, "gd2:ps", 1, &engine_ps, NULL},
281 {FORMAT_GD2_PS, "gd2:lasi", 1, &engine_ps, NULL},
282#ifdef HAVE_GD_GIF
283 {FORMAT_GIF_PS, "gif:ps", 1, &engine_ps, NULL},
284 {FORMAT_GIF_PS, "gif:lasi", 1, &engine_ps, NULL},
285#endif
286#ifdef HAVE_GD_JPEG
287 {FORMAT_JPG_PS, "jpeg:ps", 1, &engine_ps, NULL},
288 {FORMAT_JPG_PS, "jpg:ps", 1, &engine_ps, NULL},
289 {FORMAT_JPG_PS, "jpe:ps", 1, &engine_ps, NULL},
290 {FORMAT_JPG_PS, "jpeg:lasi", 1, &engine_ps, NULL},
291 {FORMAT_JPG_PS, "jpg:lasi", 1, &engine_ps, NULL},
292 {FORMAT_JPG_PS, "jpe:lasi", 1, &engine_ps, NULL},
293#endif
294#ifdef HAVE_GD_PNG
295 {FORMAT_PNG_PS, "png:ps", 1, &engine_ps, NULL},
296 {FORMAT_PNG_PS, "png:lasi", 1, &engine_ps, NULL},
297#endif
298#ifdef HAVE_GD_WBMP
299 {FORMAT_WBMP_PS, "wbmp:ps", 1, &engine_ps, NULL},
300 {FORMAT_WBMP_PS, "wbmp:lasi", 1, &engine_ps, NULL},
301#endif
302#ifdef HAVE_GD_XPM
303 {FORMAT_XBM_PS, "xbm:ps", 1, &engine_ps, NULL},
304 {FORMAT_XBM_PS, "xbm:lasi", 1, &engine_ps, NULL},
305#endif
306
307#ifdef HAVE_PANGOCAIRO
308 {FORMAT_GD_CAIRO, "gd:cairo", 1, &engine_cairo, NULL},
309 {FORMAT_GD2_CAIRO, "gd2:cairo", 1, &engine_cairo, NULL},
310#ifdef HAVE_GD_GIF
311 {FORMAT_GIF_CAIRO, "gif:cairo", 1, &engine_cairo, NULL},
312#endif
313#ifdef HAVE_GD_JPEG
314 {FORMAT_JPG_CAIRO, "jpeg:cairo", 1, &engine_cairo, NULL},
315 {FORMAT_JPG_CAIRO, "jpg:cairo", 1, &engine_cairo, NULL},
316 {FORMAT_JPG_CAIRO, "jpe:cairo", 1, &engine_cairo, NULL},
317#endif
318#ifdef HAVE_GD_PNG
319 {FORMAT_PNG_CAIRO, "png:cairo", -1, &engine_cairo, NULL},
320#endif
321#ifdef HAVE_GD_WBMP
322 {FORMAT_WBMP_CAIRO, "wbmp:cairo", 1, &engine_cairo, NULL},
323#endif
324#ifdef HAVE_GD_XPM
325 {FORMAT_XBM_CAIRO, "xbm:cairo", 1, &engine_cairo, NULL},
326#endif
327#endif /* HAVE_PANGOCAIRO */
328 {0, NULL, 0, NULL, NULL}
329};
Memory allocation wrappers that exit on failure.
static void * gv_calloc(size_t nmemb, size_t size)
Definition alloc.h:26
#define ROUND(f)
Definition arith.h:48
#define Y(i)
Definition gdefs.h:3
#define X(prefix, name, str, type, subtype,...)
Definition gdefs.h:14
void free(void *)
node NULL
Definition grammar.y:163
void gvusershape_file_release(usershape_t *us)
bool gvusershape_file_access(usershape_t *us)
int gvputs(GVJ_t *job, const char *s)
Definition gvdevice.c:264
void gvprintf(GVJ_t *job, const char *format,...)
Definition gvdevice.c:395
static gvloadimage_engine_t engine_ps
static void gd_loadimage_ps(GVJ_t *job, usershape_t *us, boxf b, bool filled)
static void gd_freeimage(usershape_t *us)
@ FORMAT_JPG_PS
@ FORMAT_GD_GD
@ FORMAT_WBMP_PS
@ FORMAT_GIF_GD
@ FORMAT_XBM_PS
@ FORMAT_XBM_GD
@ FORMAT_XPM_GD
@ FORMAT_WBMP_CAIRO
@ FORMAT_GD_PS
@ FORMAT_JPG_GD
@ FORMAT_PNG_GD
@ FORMAT_WBMP_GD
@ FORMAT_GD_CAIRO
@ FORMAT_XBM_CAIRO
@ FORMAT_GD2_CAIRO
@ FORMAT_GIF_CAIRO
@ FORMAT_GIF_PS
@ FORMAT_JPG_CAIRO
@ FORMAT_GD2_GD
@ FORMAT_XPM_PS
@ FORMAT_PNG_PS
@ FORMAT_PNG_CAIRO
@ FORMAT_GD2_PS
@ FORMAT_XPM_CAIRO
static gvloadimage_engine_t engine
gvplugin_installed_t gvloadimage_gd_types[]
static void gd_loadimage_gd(GVJ_t *job, usershape_t *us, boxf b, bool filled)
static gdImagePtr gd_loadimage(GVJ_t *job, usershape_t *us)
static gdImagePtr gd_rotateimage(gdImagePtr im, int rotation)
static gvloadimage_engine_t engine_cairo
int rotation
Definition gvcjob.h:319
pointf dpi
Definition gvcjob.h:325
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_GIF
Definition usershape.h:25
@ FT_JPEG
Definition usershape.h:25
@ FT_PNG
Definition usershape.h:25