| 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 <stdint.h> |
| 15 |
#include <stdlib.h> |
| 16 |
#include <string.h> |
| 17 |
|
| 18 |
#include <gvc/gvplugin_loadimage.h> |
| 19 |
#include <gvc/gvio.h> |
| 20 |
|
| 21 |
#include <cairo.h> |
| 22 |
|
| 23 |
enum { |
| 24 |
FORMAT_PNG_CAIRO, FORMAT_PNG_PS, |
| 25 |
}; |
| 26 |
|
| 27 |
static cairo_status_t |
| 28 |
reader (void *closure, unsigned char *data, unsigned int length) |
| 29 |
{ |
| 30 |
assert(closure); |
| 31 |
if (length == fread(data, 1, length, (FILE *)closure) |
| 32 |
|| feof((FILE *)closure)) |
| 33 |
return CAIRO_STATUS_SUCCESS; |
| 34 |
return CAIRO_STATUS_READ_ERROR; |
| 35 |
} |
| 36 |
|
| 37 |
static void cairo_freeimage(usershape_t *us) |
| 38 |
{ |
| 39 |
cairo_surface_destroy(us->data); |
| 40 |
} |
| 41 |
|
| 42 |
static cairo_surface_t *cairo_loadimage(usershape_t *us) { |
| 43 |
cairo_surface_t *surface = NULL; /* source surface */ |
| 44 |
|
| 45 |
assert(us); |
| 46 |
assert(us->name); |
| 47 |
assert(us->name[0]); |
| 48 |
|
| 49 |
if (us->data) { |
| 50 |
if (us->datafree == cairo_freeimage) |
| 51 |
surface = us->data; /* use cached data */ |
| 52 |
else { |
| 53 |
us->datafree(us); /* free incompatible cache data */ |
| 54 |
us->datafree = NULL; |
| 55 |
us->data = NULL; |
| 56 |
} |
| 57 |
} |
| 58 |
if (!surface) { /* read file into cache */ |
| 59 |
if (!gvusershape_file_access(us)) |
| 60 |
return NULL; |
| 61 |
assert(us->f); |
| 62 |
switch (us->type) { |
| 63 |
#ifdef CAIRO_HAS_PNG_FUNCTIONS |
| 64 |
case FT_PNG: |
| 65 |
surface = cairo_image_surface_create_from_png_stream(reader, us->f); |
| 66 |
cairo_surface_reference(surface); |
| 67 |
break; |
| 68 |
#endif |
| 69 |
default: |
| 70 |
surface = NULL; |
| 71 |
} |
| 72 |
if (surface) { |
| 73 |
us->data = surface; |
| 74 |
us->datafree = cairo_freeimage; |
| 75 |
} |
| 76 |
gvusershape_file_release(us); |
| 77 |
} |
| 78 |
return surface; |
| 79 |
} |
| 80 |
|
| 81 |
static void pango_loadimage_cairo(GVJ_t * job, usershape_t *us, boxf b, bool filled) |
| 82 |
{ |
| 83 |
cairo_t *cr = job->context; /* target context */ |
| 84 |
cairo_surface_t *surface; /* source surface */ |
| 85 |
|
| 86 |
assert(job); |
| 87 |
assert(us); |
| 88 |
assert(us->name); |
| 89 |
assert(us->name[0]); |
| 90 |
|
| 91 |
// suppress unused parameter warning |
| 92 |
(void)filled; |
| 93 |
|
| 94 |
surface = cairo_loadimage(us); |
| 95 |
if (surface) { |
| 96 |
cairo_save(cr); |
| 97 |
cairo_translate(cr, b.LL.x, -b.UR.y); |
| 98 |
cairo_scale(cr, (b.UR.x - b.LL.x)/(us->w), (b.UR.y - b.LL.y)/(us->h)); |
| 99 |
cairo_set_source_surface (cr, surface, 0, 0); |
| 100 |
cairo_paint (cr); |
| 101 |
cairo_restore(cr); |
| 102 |
} |
| 103 |
} |
| 104 |
|
| 105 |
static void pango_loadimage_ps(GVJ_t * job, usershape_t *us, boxf b, bool filled) |
| 106 |
{ |
| 107 |
// suppress unused parameter warning |
| 108 |
(void)filled; |
| 109 |
|
| 110 |
cairo_surface_t *const surface = cairo_loadimage(us); // source surface |
| 111 |
if (surface) { |
| 112 |
const cairo_format_t format = cairo_image_surface_get_format(surface); |
| 113 |
if ((format != CAIRO_FORMAT_ARGB32) && (format != CAIRO_FORMAT_RGB24)) |
| 114 |
return; |
| 115 |
|
| 116 |
const int X = cairo_image_surface_get_width(surface); |
| 117 |
const int Y = cairo_image_surface_get_height(surface); |
| 118 |
const int stride = cairo_image_surface_get_stride(surface); |
| 119 |
const unsigned char *data = cairo_image_surface_get_data(surface); |
| 120 |
|
| 121 |
gvputs(job, "save\n"); |
| 122 |
|
| 123 |
/* define image data as string array (one per raster line) */ |
| 124 |
/* see parallel code in gd_loadimage_ps(). FIXME: refactor... */ |
| 125 |
gvputs(job, "/myctr 0 def\n"); |
| 126 |
gvputs(job, "/myarray [\n"); |
| 127 |
for (int y = 0; y < Y; y++) { |
| 128 |
gvputs(job, "<"); |
| 129 |
const unsigned char *ix = data + y * stride; |
| 130 |
for (int x = 0; x < X; x++) { |
| 131 |
uint32_t rgba; |
| 132 |
memcpy(&rgba, ix, sizeof(rgba)); |
| 133 |
ix += sizeof(rgba); |
| 134 |
const unsigned blue = rgba & 0xff; |
| 135 |
const unsigned green = (rgba >> 8) & 0xff; |
| 136 |
const unsigned red = (rgba >> 16) & 0xff; |
| 137 |
const unsigned alpha = (rgba >> 24) & 0xff; |
| 138 |
if (alpha < 0x7f) |
| 139 |
gvputs(job, "ffffff"); |
| 140 |
else |
| 141 |
gvprintf(job, "%02x%02x%02x", red, green, blue); |
| 142 |
} |
| 143 |
gvputs(job, ">\n"); |
| 144 |
} |
| 145 |
gvputs(job, "] def\n"); |
| 146 |
gvputs(job,"/myproc { myarray myctr get /myctr myctr 1 add def } def\n"); |
| 147 |
|
| 148 |
/* this sets the position of the image */ |
| 149 |
gvprintf(job, "%g %g translate\n", |
| 150 |
(b.LL.x + (b.UR.x - b.LL.x) * (1. - (job->dpi.x) / 96.) / 2.), |
| 151 |
(b.LL.y + (b.UR.y - b.LL.y) * (1. - (job->dpi.y) / 96.) / 2.)); |
| 152 |
|
| 153 |
/* this sets the rendered size to fit the box */ |
| 154 |
gvprintf(job,"%g %g scale\n", |
| 155 |
((b.UR.x - b.LL.x) * 72. / 96.), |
| 156 |
((b.UR.y - b.LL.y) * 72. / 96.)); |
| 157 |
|
| 158 |
/* xsize ysize bits-per-sample [matrix] */ |
| 159 |
gvprintf(job, "%d %d 8 [%d 0 0 %d 0 %d]\n", X, Y, X, -Y, Y); |
| 160 |
|
| 161 |
gvputs(job, "{myproc} false 3 colorimage\n"); |
| 162 |
|
| 163 |
gvputs(job, "restore\n"); |
| 164 |
} |
| 165 |
} |
| 166 |
|
| 167 |
static gvloadimage_engine_t engine_cairo = { |
| 168 |
pango_loadimage_cairo |
| 169 |
}; |
| 170 |
|
| 171 |
static gvloadimage_engine_t engine_ps = { |
| 172 |
pango_loadimage_ps |
| 173 |
}; |
| 174 |
|
| 175 |
gvplugin_installed_t gvloadimage_pango_types[] = { |
| 176 |
{FORMAT_PNG_CAIRO, "png:cairo", 1, &engine_cairo, NULL}, |
| 177 |
{FORMAT_PNG_PS, "png:lasi", 2, &engine_ps, NULL}, |
| 178 |
{FORMAT_PNG_PS, "png:ps", 2, &engine_ps, NULL}, |
| 179 |
{0, NULL, 0, NULL, NULL} |
| 180 |
}; |