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