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