| 1 |
/* |
| 2 |
Imagemagick support for xdvik, written by Zhang Linbo <[email protected]> |
| 3 |
|
| 4 |
Permission is hereby granted, free of charge, to any person obtaining a copy |
| 5 |
of this software and associated documentation files (the "Software"), to |
| 6 |
deal in the Software without restriction, including without limitation the |
| 7 |
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or |
| 8 |
sell copies of the Software, and to permit persons to whom the Software is |
| 9 |
furnished to do so, subject to the following conditions: |
| 10 |
|
| 11 |
The above copyright notice and this permission notice shall be included in |
| 12 |
all copies or substantial portions of the Software. |
| 13 |
|
| 14 |
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 15 |
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 16 |
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
| 17 |
PAUL VOJTA OR ANY OTHER AUTHOR OF THIS SOFTWARE BE LIABLE FOR ANY CLAIM, |
| 18 |
DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, |
| 19 |
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR |
| 20 |
OTHER DEALINGS IN THE SOFTWARE. |
| 21 |
*/ |
| 22 |
|
| 23 |
/* |
| 24 |
TODO: |
| 25 |
|
| 26 |
1. Specials other than 'psfile=' and 'em:graph' which include |
| 27 |
image files. |
| 28 |
|
| 29 |
[ NOTE SU: I consider this more or less done now with the backtick |
| 30 |
parsing ;-) ] |
| 31 |
|
| 32 |
2. Use an XtWorkProc or similar to do some background pre-processing |
| 33 |
of image files before they are displayed. |
| 34 |
|
| 35 |
3. Replace popen() by fork and explicit pipes, and make the |
| 36 |
use of xdvi_temp_fd safer. |
| 37 |
|
| 38 |
4. PS figs are always clipped to the bbox defined in the special |
| 39 |
string, which is not what we want when clip=false. |
| 40 |
The problem can be partly solved by increasing the value of |
| 41 |
the PSBW macro, or we may get the correct bbox by reading the |
| 42 |
'%%BoundingBox' or '%%HiresBoundingBox' lines in the PS file |
| 43 |
and set the image geometry accordingly. |
| 44 |
|
| 45 |
5. The code only(?) works with true color displays. |
| 46 |
|
| 47 |
6. There's also work to do to respond to window events in |
| 48 |
load_image() and display_image() functions. |
| 49 |
*/ |
| 50 |
#include "xdvi-config.h" |
| 51 |
|
| 52 |
#include <ctype.h> |
| 53 |
|
| 54 |
#include "xdvi.h" |
| 55 |
#include "dvi-init.h" |
| 56 |
#include "events.h" |
| 57 |
#include "dvi-draw.h" |
| 58 |
#include "util.h" |
| 59 |
#include "special.h" |
| 60 |
#include "image-magick.h" |
| 61 |
|
| 62 |
#ifdef MAGICK /* entire file */ |
| 63 |
|
| 64 |
#ifndef MAGICK_RENDER_PS |
| 65 |
# define MAGICK_RENDER_PS 0 |
| 66 |
#endif |
| 67 |
|
| 68 |
#include "magick/api.h" |
| 69 |
#if MAGICK_VER_MAJOR > 5 || MAGICK_VER_MINOR >= 4 |
| 70 |
# include "magick/xwindow.h" /* IM >= 5.4 */ |
| 71 |
#else |
| 72 |
# include "magick/xwindows.h" /* IM <= 5.3 */ |
| 73 |
#endif |
| 74 |
|
| 75 |
#if MAGICK_VER_MAJOR > 5 || MAGICK_VER_MINOR >= 4 /* >= 5.4 */ |
| 76 |
# define CATCH_EXCEPTION(e) do { \ |
| 77 |
if (e.severity != UndefinedException) CatchException(&e); } while (0) |
| 78 |
#elif MAGICK_VER_MINOR >= 3 /* 5.3 */ |
| 79 |
# define CATCH_EXCEPTION(e) if (image == NULL) \ |
| 80 |
MagickWarning(e.severity, e.reason, e.description); |
| 81 |
#else /* <= 5.2 (no check) */ |
| 82 |
# define CATCH_EXCEPTION(e) if (image == NULL) \ |
| 83 |
MagickWarning(e.severity, e.message, e.qualifier); |
| 84 |
#endif |
| 85 |
|
| 86 |
#if 0 |
| 87 |
#define CATCH_EXCEPTION(e) /* do nothing */ |
| 88 |
#endif |
| 89 |
|
| 90 |
#ifndef False |
| 91 |
# define False 0 |
| 92 |
#endif |
| 93 |
|
| 94 |
#ifndef True |
| 95 |
# define True 1 |
| 96 |
#endif |
| 97 |
|
| 98 |
#define TWOPI (2*3.141592653589793) |
| 99 |
|
| 100 |
static double ROUND_tmp; |
| 101 |
#define ROUND(x) ( (ROUND_tmp=(x)) >= 0 ? (long)(ROUND_tmp + 0.5) : \ |
| 102 |
- (long)(- ROUND_tmp + 0.5) ) |
| 103 |
|
| 104 |
static void |
| 105 |
showtime(const char *msg) |
| 106 |
{ |
| 107 |
if (globals.debug & DBG_PS) { |
| 108 |
static double time0 = -1; |
| 109 |
double time; |
| 110 |
struct timeval tv; |
| 111 |
|
| 112 |
gettimeofday(&tv, (struct timezone *) 0); |
| 113 |
time = tv.tv_sec + (double) tv.tv_usec * 1e-6; |
| 114 |
if (time0 < 0) |
| 115 |
time0 = time; |
| 116 |
fprintf(stderr, " %-40s %0.4f\n", msg, time - time0); |
| 117 |
} |
| 118 |
} |
| 119 |
|
| 120 |
#define DPI ( resource.pixels_per_inch*0.001*magnification/mane.shrinkfactor ) |
| 121 |
|
| 122 |
typedef struct XDviImageInfo { |
| 123 |
Boolean clip; |
| 124 |
int rwi, rhi, angle; |
| 125 |
double llx, lly, urx, ury; |
| 126 |
char filename[MaxTextExtent]; |
| 127 |
} XDviImageInfo; |
| 128 |
|
| 129 |
typedef struct { |
| 130 |
XDviImageInfo info; |
| 131 |
XImage *ximage; /* ximage */ |
| 132 |
unsigned long bgpixel; /* background pixel */ |
| 133 |
int xoff, yoff; /* offsets in pixels */ |
| 134 |
} XDviImage; |
| 135 |
|
| 136 |
static size_t cache_size = 0; |
| 137 |
/* default value for cache_limit (16MB), it can be specified with the |
| 138 |
* "-magick_cache size" option */ |
| 139 |
static size_t cache_limit = 16 * 1024 * 1024; |
| 140 |
static int ncached = 0; |
| 141 |
static XDviImage *cache = NULL; |
| 142 |
|
| 143 |
static char *magick_tmp; |
| 144 |
static double current_dpi = -1; |
| 145 |
|
| 146 |
/* Input image types */ |
| 147 |
enum { |
| 148 |
TYPE_UNKNOWN, /* unknown */ |
| 149 |
TYPE_CONVERT, /* `convert ... */ |
| 150 |
}; |
| 151 |
static int image_type; /* type of current input image */ |
| 152 |
|
| 153 |
static void |
| 154 |
cleanup(void) |
| 155 |
{ |
| 156 |
if (magick_tmp != NULL) |
| 157 |
unlink(magick_tmp); |
| 158 |
} |
| 159 |
|
| 160 |
static void |
| 161 |
free_ximage(XImage * ximage) |
| 162 |
{ |
| 163 |
if (ximage == NULL) |
| 164 |
return; |
| 165 |
if (ximage->data != NULL) { |
| 166 |
free(ximage->data); |
| 167 |
ximage->data = NULL; |
| 168 |
} |
| 169 |
XDestroyImage(ximage); |
| 170 |
} |
| 171 |
|
| 172 |
static void |
| 173 |
clear_cache(void) |
| 174 |
{ |
| 175 |
XDviImage *c; |
| 176 |
if (!ncached) |
| 177 |
return; |
| 178 |
for (c = cache; c < cache + ncached; c++) |
| 179 |
free_ximage(c->ximage); |
| 180 |
free(cache); |
| 181 |
ncached = 0; |
| 182 |
cache = NULL; |
| 183 |
cache_size = 0; |
| 184 |
} |
| 185 |
|
| 186 |
static void |
| 187 |
parse_backtick(char *fn) |
| 188 |
{ |
| 189 |
char *p, *q, *r, c; |
| 190 |
|
| 191 |
if (fn[0] != '`') |
| 192 |
return; |
| 193 |
|
| 194 |
if (globals.debug & DBG_PS) |
| 195 |
fprintf(stderr, "Magick: parsing backtick command |%s|\n", fn); |
| 196 |
|
| 197 |
#ifdef __GNUC__ |
| 198 |
#warning FIXME: add code to execute the cmd then load the output file, \ |
| 199 |
if '-allowshell' is effective and the user requests it \ |
| 200 |
somehow (slow but reliable). |
| 201 |
#endif |
| 202 |
|
| 203 |
fn[0] = '\0'; |
| 204 |
p = fn + 1; |
| 205 |
/* Skip 'env' and 'VAR=value' */ |
| 206 |
do { |
| 207 |
while (isspace((int)*p)) |
| 208 |
++p; |
| 209 |
q = p; |
| 210 |
while (*q != '\0' && !isspace((int)*q)) |
| 211 |
++q; |
| 212 |
if (*q == '\0' || q <= p) |
| 213 |
return; |
| 214 |
if (((r = strchr(p, '=')) == NULL || r >= q) && |
| 215 |
(!isspace((int)*q) || strncmp(p, "env", 3))) |
| 216 |
break; |
| 217 |
p = q; |
| 218 |
} while (1); |
| 219 |
|
| 220 |
/* Now [p ... q-1] is the command name */ |
| 221 |
if (q - p == 7 && !memcmp(p, "convert", 7)) |
| 222 |
image_type = TYPE_CONVERT; |
| 223 |
else |
| 224 |
image_type = TYPE_UNKNOWN; |
| 225 |
p = q; |
| 226 |
do { |
| 227 |
while (isspace((int)*p)) |
| 228 |
++p; |
| 229 |
q = p; |
| 230 |
if (*q == '`' || *q == '\'' || *q == '"') { |
| 231 |
c = *p; |
| 232 |
++q; |
| 233 |
while (*q != '\0' && *q != c) |
| 234 |
q++; |
| 235 |
if (*q != '\0') |
| 236 |
q++; |
| 237 |
} |
| 238 |
else { |
| 239 |
while (*q != '\0' && !isspace((int)*q)) |
| 240 |
q++; |
| 241 |
} |
| 242 |
#ifdef __GNUC__ |
| 243 |
#warning FIXME: check for some cmdline options |
| 244 |
break; |
| 245 |
#endif |
| 246 |
} while (1); |
| 247 |
*q = '\0'; |
| 248 |
|
| 249 |
if (globals.debug & DBG_PS) |
| 250 |
fprintf(stderr, "Magick: filename=|%s|\n", p); |
| 251 |
|
| 252 |
/* check for the case `kpsewhich -n ... #1` */ |
| 253 |
if (*p == '`') { |
| 254 |
#if 0 |
| 255 |
/* WARNING: unsafe! */ |
| 256 |
FILE *pipe; |
| 257 |
|
| 258 |
if (globals.debug & DBG_PS) |
| 259 |
fprintf(stderr, "Magick: executing %s\n", p); |
| 260 |
if (*(--q) == '`') |
| 261 |
*q = '\0'; |
| 262 |
if ((pipe = popen(p + 1, "r")) == NULL) |
| 263 |
return; |
| 264 |
fscanf(pipe, "%s", fn); |
| 265 |
pclose(pipe); |
| 266 |
return; |
| 267 |
#else |
| 268 |
if (memcmp(++p, "kpsewhich", sizeof("kpsewhich") - 1)) |
| 269 |
return; |
| 270 |
#ifdef __GNUC__ |
| 271 |
#warning FIXME: better make KPSE library call here. |
| 272 |
#endif |
| 273 |
if (*(--q) == '`') |
| 274 |
--q; |
| 275 |
while (q >= p && isspace((int)*q)) |
| 276 |
--q; |
| 277 |
if (q < p) |
| 278 |
return; |
| 279 |
*(q + 1) = '\0'; |
| 280 |
while (q >= p && !isspace((int)*q)) |
| 281 |
--q; |
| 282 |
p = q + 1; |
| 283 |
#endif |
| 284 |
} |
| 285 |
|
| 286 |
strcpy(fn, p); |
| 287 |
} |
| 288 |
|
| 289 |
static Boolean |
| 290 |
imagick_eps_to_jpeg(const char *fullpath, |
| 291 |
unsigned long *columns, unsigned long *rows, |
| 292 |
XDviImage *img, PixelPacket *bgcolor, |
| 293 |
double xdpi, double ydpi) |
| 294 |
{ |
| 295 |
size_t size; |
| 296 |
#ifdef __GNUC__ |
| 297 |
#warning FIXME: security (check 'allowshell'?), backticks, pathname canonicalization, |
| 298 |
/* note SU: better use fork()/exec() here, popen() is too unsafe. */ |
| 299 |
#endif |
| 300 |
static char *cmdline = NULL; |
| 301 |
static size_t cmdline_size = 0; |
| 302 |
static char *fmt = "%s -q -dBATCH -dMaxBitmap=300000000 " |
| 303 |
"-dNOPAUSE -dNOSAFER -sDEVICE=jpeg " |
| 304 |
"-dTextAlphaBits=4 -dGraphicsAlphaBits=4 " |
| 305 |
"-g%ldx%ld -r%0.4fx%0.4f -sOutputFile=%s - -c quit"; |
| 306 |
static const char strsafe[] = |
| 307 |
"{ << /PermitFileReading [ (*) ] /PermitFileWriting [ ] /PermitFileControl [ ] " |
| 308 |
">> setuserparams .locksafe " |
| 309 |
"} stopped pop\n"; |
| 310 |
FILE *pipe; |
| 311 |
|
| 312 |
showtime("PS/EPS -> JPEG:"); |
| 313 |
/* Add a border to the drawing box (PSBW=border width in bp) */ |
| 314 |
#define PSBW 1 |
| 315 |
*columns += ROUND(2 * PSBW / 72.0 * xdpi); |
| 316 |
*rows += ROUND(2 * PSBW / 72.0 * ydpi); |
| 317 |
img->xoff = ROUND(PSBW / 72.0 * xdpi); |
| 318 |
img->yoff = -ROUND(PSBW / 72.0 * ydpi); |
| 319 |
|
| 320 |
size = strlen(fmt) + 25 * 2 + 10 * 2 + strlen(magick_tmp) + strlen(resource.gs_path) + 1; |
| 321 |
if (size > cmdline_size) { |
| 322 |
cmdline_size = size; |
| 323 |
cmdline = xrealloc(cmdline, cmdline_size); |
| 324 |
} |
| 325 |
unlink(magick_tmp); |
| 326 |
sprintf(cmdline, fmt, resource.gs_path, *columns, *rows, xdpi, ydpi, |
| 327 |
magick_tmp); |
| 328 |
if ((pipe = popen(cmdline, "w")) == NULL) { |
| 329 |
fprintf(stderr, "%s: Cannot execute %s, ignoring image %s.\n", |
| 330 |
globals.program_name, resource.gs_path, fullpath); |
| 331 |
return False; |
| 332 |
} |
| 333 |
|
| 334 |
#define ColorToPixelPacket(c) ((c*((1L<<(QuantumDepth))-1L)+32768L)/65535L) |
| 335 |
#define UnifiedColorValue(c) ((c)/(double)((1L<<(QuantumDepth))-1L)) |
| 336 |
#ifdef __GNUC__ |
| 337 |
#warning FIXME: how to control roundoff error which may produce a \ |
| 338 |
different backgroung pixel in the output image? |
| 339 |
#endif |
| 340 |
#if COLOR |
| 341 |
bgcolor->red = ColorToPixelPacket(bg_current->color.r); |
| 342 |
bgcolor->green = ColorToPixelPacket(bg_current->color.g); |
| 343 |
bgcolor->blue = ColorToPixelPacket(bg_current->color.b); |
| 344 |
#else |
| 345 |
bgcolor->red = bgcolor->green = bgcolor->blue = ColorToPixelPacket(65535); |
| 346 |
#endif |
| 347 |
bgcolor->opacity = (1L << QuantumDepth) - 1L; |
| 348 |
|
| 349 |
#ifdef __GNUC__ |
| 350 |
#warning FIXME: how to set bg color within gs (instead of filling the bbox) ? |
| 351 |
#endif |
| 352 |
if (resource.gs_safer) { |
| 353 |
fprintf(pipe, strsafe); |
| 354 |
} |
| 355 |
/* fill PS paper with background color */ |
| 356 |
fprintf(pipe, |
| 357 |
"newpath -5 -5 moveto %f 0 rlineto " |
| 358 |
"0 %f rlineto %f 0 rlineto 0 %f rlineto " |
| 359 |
"closepath %f %f %f setrgbcolor fill\n", |
| 360 |
img->info.urx - img->info.llx + 2 * (PSBW + 5), |
| 361 |
img->info.ury - img->info.lly + 2 * (PSBW + 5), |
| 362 |
-(img->info.urx - img->info.llx + 2 * (PSBW + 5)), |
| 363 |
-(img->info.ury - img->info.lly + 2 * (PSBW + 5)), |
| 364 |
UnifiedColorValue(bgcolor->red), |
| 365 |
UnifiedColorValue(bgcolor->green), |
| 366 |
UnifiedColorValue(bgcolor->blue)); |
| 367 |
|
| 368 |
/* shift PS coordinates */ |
| 369 |
fprintf(pipe, "%f %f translate\n", -img->info.llx + PSBW, -img->info.lly + PSBW); |
| 370 |
if (globals.debug & DBG_PS) |
| 371 |
fprintf(stderr, " clip=%s\n", img->info.clip ? "yes" : "no"); |
| 372 |
if (img->info.clip) { |
| 373 |
fprintf(pipe, "newpath %f %f moveto %f 0 rlineto 0 %f rlineto " |
| 374 |
"%f 0 rlineto 0 %f rlineto closepath clip\n", |
| 375 |
img->info.llx, img->info.lly, img->info.urx - img->info.llx, |
| 376 |
img->info.ury - img->info.lly, -(img->info.urx - img->info.llx), |
| 377 |
-(img->info.ury - img->info.lly)); |
| 378 |
} |
| 379 |
fprintf(pipe, "%f %f moveto\n", img->info.llx, img->info.lly); |
| 380 |
fprintf(pipe, "(%s) run showpage\n", fullpath); |
| 381 |
pclose(pipe); |
| 382 |
return True; |
| 383 |
} |
| 384 |
|
| 385 |
|
| 386 |
static Image * |
| 387 |
rotate_image(XDviImage *img, Image *image, ExceptionInfo exception) |
| 388 |
{ |
| 389 |
Image *tmp_image; |
| 390 |
unsigned long w = image->columns, h = image->rows; |
| 391 |
double sin_a = sin(img->info.angle * (TWOPI / 360)); |
| 392 |
double cos_a = cos(img->info.angle * (TWOPI / 360)); |
| 393 |
|
| 394 |
if (globals.debug & DBG_PS) |
| 395 |
fprintf(stderr, "\t rotating image by %d degrees\n", |
| 396 |
img->info.angle); |
| 397 |
|
| 398 |
/* adjust xoff, yoff, assuming (0,0) the origin of rotation */ |
| 399 |
if (img->xoff || img->yoff) { |
| 400 |
double a, b; |
| 401 |
a = img->xoff * cos_a + img->yoff * sin_a; |
| 402 |
b = -img->xoff * sin_a + img->yoff * cos_a; |
| 403 |
img->xoff = ROUND(a); |
| 404 |
img->yoff = ROUND(b); |
| 405 |
} |
| 406 |
|
| 407 |
showtime("RotateImage:"); |
| 408 |
tmp_image = RotateImage(image, -(double)img->info.angle, &exception); |
| 409 |
DestroyImage(image); |
| 410 |
image = tmp_image; |
| 411 |
CATCH_EXCEPTION(exception); |
| 412 |
if (image == NULL) |
| 413 |
return NULL; |
| 414 |
/* |
| 415 |
* _______________________________________ |
| 416 |
* | * | |
| 417 |
* | * * | |
| 418 |
* | * * | |
| 419 |
* | * * | |
| 420 |
* | * * | |
| 421 |
* | * * | |
| 422 |
* | * * | |
| 423 |
* | * * | |
| 424 |
* | * * | |
| 425 |
* |* *| |
| 426 |
* | * * | |
| 427 |
* | * * | |
| 428 |
* | * * | |
| 429 |
* | * * | |
| 430 |
* | * * | |
| 431 |
* | * * | |
| 432 |
* | * * | |
| 433 |
* | * * | |
| 434 |
* | O (origin) | |
| 435 |
* O-------------------------------------- |
| 436 |
* (lower-left corner of image box after rotation) |
| 437 |
*/ |
| 438 |
if (img->info.angle <= 90) { |
| 439 |
img->xoff += ROUND(h * sin_a); |
| 440 |
} |
| 441 |
else if (img->info.angle <= 180) { |
| 442 |
img->xoff += ROUND(h * sin_a - w * cos_a); |
| 443 |
img->yoff += ROUND(h * cos_a); |
| 444 |
} |
| 445 |
else if (img->info.angle <= 270) { |
| 446 |
img->xoff -= ROUND(w * cos_a); |
| 447 |
img->yoff += ROUND(w * sin_a + h * cos_a); |
| 448 |
} |
| 449 |
else { |
| 450 |
img->yoff += ROUND(w * sin_a); |
| 451 |
} |
| 452 |
return image; |
| 453 |
} |
| 454 |
|
| 455 |
static Image * |
| 456 |
crop_shift_image(XDviImage *img, Image *image, |
| 457 |
unsigned long *columns, unsigned long *rows, |
| 458 |
double xscale, double yscale, |
| 459 |
ExceptionInfo exception) |
| 460 |
{ |
| 461 |
/* ZLB: the code below works for the case where the BoundingBox |
| 462 |
* of the PS figure generated by the shell command is (0 0 w h), |
| 463 |
* where w and h are the pixel width and height of the input image. |
| 464 |
* (this is true for the program 'convert' without options). |
| 465 |
* The image should then be cropped or/and shifted depending |
| 466 |
* on the value of 'clip' */ |
| 467 |
int w = image->columns, h = image->rows; |
| 468 |
|
| 469 |
if (globals.debug & DBG_PS) |
| 470 |
fprintf(stderr, "Magick: type of input image = CONVERT\n"); |
| 471 |
|
| 472 |
if (!img->info.clip) { |
| 473 |
/* We only need to shift the image */ |
| 474 |
img->xoff = +ROUND(xscale * DPI * img->info.llx / 72.0); |
| 475 |
img->yoff = -ROUND(yscale * DPI * img->info.lly / 72.0); |
| 476 |
*columns = ROUND(xscale * DPI * w / 72.0); |
| 477 |
*rows = ROUND(yscale * DPI * h / 72.0); |
| 478 |
} |
| 479 |
else { |
| 480 |
int llx = ROUND(img->info.llx); |
| 481 |
int lly = ROUND(img->info.lly); |
| 482 |
int urx = ROUND(img->info.urx); |
| 483 |
int ury = ROUND(img->info.ury); |
| 484 |
|
| 485 |
if (llx < 0) { |
| 486 |
img->xoff = +ROUND(xscale * DPI * llx / 72.0); |
| 487 |
llx = 0; |
| 488 |
} |
| 489 |
if (lly < 0) { |
| 490 |
img->yoff = -ROUND(yscale * DPI * lly / 72.0); |
| 491 |
lly = 0; |
| 492 |
} |
| 493 |
if (urx > w) |
| 494 |
urx = w; |
| 495 |
if (ury > h) |
| 496 |
ury = h; |
| 497 |
|
| 498 |
if ((urx - llx) < 1 || (urx - llx) < 1) { |
| 499 |
DestroyImage(image); |
| 500 |
return NULL; |
| 501 |
} |
| 502 |
|
| 503 |
if (llx != 0 || lly != 0 || urx != w || ury != h) { |
| 504 |
/* Crop the image */ |
| 505 |
Image *tmp_image; |
| 506 |
RectangleInfo geo; |
| 507 |
geo.width = urx - llx; |
| 508 |
geo.height = ury - lly; |
| 509 |
geo.x = llx; |
| 510 |
geo.y = h - ury; |
| 511 |
if (globals.debug & DBG_PS) |
| 512 |
fprintf(stderr, "\t cropping image (%d %d %d %d)\n", |
| 513 |
llx, lly, urx, ury); |
| 514 |
showtime("CropImage:"); |
| 515 |
tmp_image = CropImage(image, &geo, &exception); |
| 516 |
DestroyImage(image); |
| 517 |
image = tmp_image; |
| 518 |
CATCH_EXCEPTION(exception); |
| 519 |
if (image == NULL) |
| 520 |
return NULL; /* error cropping image */ |
| 521 |
} |
| 522 |
*columns = ROUND(xscale * DPI * (urx - llx) / 72.0); |
| 523 |
*rows = ROUND(yscale * DPI * (ury - lly) / 72.0); |
| 524 |
} |
| 525 |
return image; |
| 526 |
} |
| 527 |
|
| 528 |
|
| 529 |
/* parse and return the `magick_cache' resource size_str */ |
| 530 |
static long |
| 531 |
parse_cache_setting(const char *size_str) |
| 532 |
{ |
| 533 |
char *p; |
| 534 |
long size = strtol(size_str, &p, 0); |
| 535 |
switch (*p) { |
| 536 |
case 'g': |
| 537 |
case 'G': |
| 538 |
size *= 1024; |
| 539 |
/* fall through */ |
| 540 |
case 'm': |
| 541 |
case 'M': |
| 542 |
size *= 1024; |
| 543 |
/* fall through */ |
| 544 |
case 'k': |
| 545 |
case 'K': |
| 546 |
size *= 1024; |
| 547 |
break; |
| 548 |
default: |
| 549 |
if (*p != '\0') { |
| 550 |
fprintf(stderr, "Magick: invalid suffix (%c) in " |
| 551 |
"magick_cache option\n", *p); |
| 552 |
} |
| 553 |
} |
| 554 |
return size; |
| 555 |
} |
| 556 |
|
| 557 |
static XDviImage * |
| 558 |
load_image(XDviImageInfo *info) |
| 559 |
{ |
| 560 |
static ExceptionInfo exception; |
| 561 |
static ImageInfo *image_info; |
| 562 |
static XVisualInfo visual_info; |
| 563 |
static XStandardColormap map_info; |
| 564 |
static Boolean initialized = False; |
| 565 |
static Boolean disabled = False; |
| 566 |
|
| 567 |
static XDviImage img = { {False, 0, 0, 0, 0, 0, 0, 0, ""}, NULL, 0, 0, 0 }; |
| 568 |
|
| 569 |
Image *image, *tmp_image; |
| 570 |
PixelPacket *pp; |
| 571 |
double xscale, yscale, xdpi, ydpi; |
| 572 |
int n; |
| 573 |
unsigned int x, y; |
| 574 |
unsigned long columns, rows; |
| 575 |
char density[40]; |
| 576 |
size_t size; |
| 577 |
char *fn = info->filename; |
| 578 |
XDviImage *c; |
| 579 |
PixelPacket bgcolor; |
| 580 |
|
| 581 |
struct stat statbuf; |
| 582 |
char *path, *fullpath; |
| 583 |
char canonical_path[MAXPATHLEN + 1]; |
| 584 |
|
| 585 |
if (disabled) |
| 586 |
return NULL; |
| 587 |
|
| 588 |
parse_backtick(info->filename); |
| 589 |
if (info->filename[0] == '\0') { |
| 590 |
if (globals.debug & DBG_PS) |
| 591 |
fprintf(stderr, "Magick: skipping malformed backtick command.\n"); |
| 592 |
return NULL; |
| 593 |
} |
| 594 |
else { |
| 595 |
if (globals.debug & DBG_PS) |
| 596 |
fprintf(stderr, "Magick: filename=|%s|\n", info->filename); |
| 597 |
} |
| 598 |
|
| 599 |
img.info = *info; |
| 600 |
if (globals.debug & DBG_PS) |
| 601 |
fprintf(stderr, "Magick: load_image: fn=%s\n", fn); |
| 602 |
|
| 603 |
/* check for changes in mag/shrink/resolution */ |
| 604 |
if (fabs(current_dpi - DPI) > 1e-4) { |
| 605 |
current_dpi = DPI; |
| 606 |
clear_cache(); |
| 607 |
} |
| 608 |
|
| 609 |
/* look it up in the cache */ |
| 610 |
if (ncached) { |
| 611 |
for (c = cache; c < cache + ncached; c++) { |
| 612 |
if (memcmp(&c->info, info, sizeof *info)) |
| 613 |
continue; |
| 614 |
if (globals.debug & DBG_PS) |
| 615 |
fprintf(stderr, " Image found in cache.\n"); |
| 616 |
return c; |
| 617 |
} |
| 618 |
} |
| 619 |
|
| 620 |
if (!initialized) { |
| 621 |
magick_tmp = NULL; |
| 622 |
if ((n = xdvi_temp_fd(&magick_tmp)) == -1) { |
| 623 |
fprintf(stderr, "Magick: cannot create tmp filename, disabled.\n"); |
| 624 |
disabled = True; |
| 625 |
return NULL; |
| 626 |
} |
| 627 |
/* FIXME: mustn't close filedescriptor returned by xdvi_temp_fd(), |
| 628 |
else we have a race condition again; see e.g. |
| 629 |
http://www.dwheeler.com/secure-programs/Secure-Programs-HOWTO/avoid-race.html |
| 630 |
*/ |
| 631 |
close(n); |
| 632 |
unlink(magick_tmp); |
| 633 |
atexit(cleanup); /* remove magick_tmp at exit. */ |
| 634 |
|
| 635 |
showtime("Initializing load_image:"); |
| 636 |
InitializeMagick("xdvi.bin"); |
| 637 |
GetExceptionInfo(&exception); |
| 638 |
image_info = CloneImageInfo((ImageInfo *) NULL); |
| 639 |
|
| 640 |
if (!XMatchVisualInfo(DISP, XScreenNumberOfScreen(SCRN), |
| 641 |
G_depth, G_visual->class, &visual_info)) { |
| 642 |
fprintf(stderr, "Magick: can't get visual info, image disabled.\n"); |
| 643 |
disabled = True; |
| 644 |
return NULL; |
| 645 |
} |
| 646 |
|
| 647 |
if (resource.magick_cache != NULL) { |
| 648 |
cache_limit = parse_cache_setting(resource.magick_cache); |
| 649 |
if (globals.debug & DBG_PS) |
| 650 |
fprintf(stderr, " Setting magick_cache to %lu\n", |
| 651 |
(unsigned long)cache_limit); |
| 652 |
} |
| 653 |
initialized = True; |
| 654 |
} |
| 655 |
|
| 656 |
xscale = info->rwi / (double) ((int) (10 * (info->urx - info->llx) + .5)); |
| 657 |
yscale = info->rhi / (double) ((int) (10 * (info->ury - info->lly) + .5)); |
| 658 |
xdpi = xscale * DPI; |
| 659 |
ydpi = yscale * DPI; |
| 660 |
/* Compute scaled size of the image */ |
| 661 |
columns = ROUND(DPI * info->rwi * 0.1 / 72); |
| 662 |
rows = ROUND(DPI * info->rhi * 0.1 / 72); |
| 663 |
if (globals.debug & DBG_PS) |
| 664 |
fprintf(stderr, "\tscale=%0.2fx%0.2f, dpi=%0.2fx%0.2f, size=%ldx%ld\n", |
| 665 |
xscale, yscale, xdpi, ydpi, columns, rows); |
| 666 |
|
| 667 |
/* expand and canonicalize path name */ |
| 668 |
path = find_file(fn, &statbuf, kpse_pict_format); |
| 669 |
if (path != NULL) { |
| 670 |
fullpath = REALPATH(path, canonical_path); |
| 671 |
if (fullpath == NULL) { |
| 672 |
XDVI_WARNING((stderr, "Couldn't canonicalize %s to full path - returning unexpanded.", |
| 673 |
path)); |
| 674 |
fullpath = path; |
| 675 |
} |
| 676 |
else { |
| 677 |
free(path); |
| 678 |
} |
| 679 |
} |
| 680 |
else { |
| 681 |
fprintf(stderr, "%s: %s:%d: Can't find image file %s!\n", |
| 682 |
globals.program_name, __FILE__, __LINE__, fn); |
| 683 |
return NULL; |
| 684 |
} |
| 685 |
|
| 686 |
/* If the file is PS/EPS transform it to JPEG first (faster) */ |
| 687 |
n = strlen(fullpath); |
| 688 |
if ((n >= 3 && !memicmp(fullpath + n - 3, ".ps", 3)) || |
| 689 |
(n >= 4 && !memicmp(fullpath + n - 4, ".eps", 4))) { |
| 690 |
if (!imagick_eps_to_jpeg(fullpath, &columns, &rows, &img, &bgcolor, xdpi, ydpi)) { |
| 691 |
return NULL; |
| 692 |
} |
| 693 |
/* Should we append the 'JPEG:' prefix to the filename to help |
| 694 |
* ImageMagick to identify the file format? */ |
| 695 |
fullpath = magick_tmp; |
| 696 |
} |
| 697 |
else { |
| 698 |
img.xoff = img.yoff = 0; |
| 699 |
} |
| 700 |
|
| 701 |
strcpy(image_info->filename, fullpath); |
| 702 |
showtime("ReadImage:"); |
| 703 |
if (fullpath == magick_tmp) { |
| 704 |
/* seems 'density' only affects image size of PS/EPS files */ |
| 705 |
sprintf(density, "%0.2fx%0.2f", xdpi, ydpi); |
| 706 |
image_info->density = density; |
| 707 |
image_info->units = PixelsPerInchResolution; |
| 708 |
} |
| 709 |
image = ReadImage(image_info, &exception); |
| 710 |
CATCH_EXCEPTION(exception); |
| 711 |
unlink(magick_tmp); |
| 712 |
if (image == NULL) /* error loading image */ |
| 713 |
return NULL; |
| 714 |
|
| 715 |
/* Set background color for PS/EPS fig */ |
| 716 |
if (fullpath == magick_tmp) /* input is PS */ |
| 717 |
image->background_color = bgcolor; |
| 718 |
|
| 719 |
if (globals.debug & DBG_PS) { |
| 720 |
int i = image->units; |
| 721 |
fprintf(stderr, "\t image size = %ldx%ld\n", |
| 722 |
image->columns, image->rows); |
| 723 |
fprintf(stderr, "\t image resolution = %0.2fx%0.2f (units=%s)\n", |
| 724 |
image->x_resolution, image->y_resolution, |
| 725 |
i == UndefinedResolution ? "???" : |
| 726 |
i == PixelsPerInchResolution ? "PPI" : "PPCM"); |
| 727 |
} |
| 728 |
|
| 729 |
if (fullpath != magick_tmp) { /* non PS image */ |
| 730 |
/* Note: (info->llx info->lly info->urx info->ury) should be |
| 731 |
* regarded as the viewport */ |
| 732 |
if (image_type == TYPE_CONVERT) { |
| 733 |
image = crop_shift_image(&img, image, &columns, &rows, xscale, yscale, exception); |
| 734 |
} |
| 735 |
else { |
| 736 |
/* Do nothing. The image will be scaled to the size of the bbox. |
| 737 |
* ZLB: the output will be identical to dvips iff the bbox of |
| 738 |
* the PS figure generated by the shell command is equal to |
| 739 |
* (info->llx info->lly info->urx info->ury). |
| 740 |
* (this is true if the bbox set via 'bb', 'viewport', etc., |
| 741 |
* is equal to the real bbox of the PS figure) */ |
| 742 |
} |
| 743 |
|
| 744 |
if (columns != image->columns || rows != image->rows) { |
| 745 |
/* Scale image */ |
| 746 |
if (globals.debug & DBG_PS) { |
| 747 |
fprintf(stderr, "\t scaling image %ldx%ld -> %ldx%ld\n", |
| 748 |
image->columns, image->rows, columns, rows); |
| 749 |
} |
| 750 |
showtime("ScaleImage:"); |
| 751 |
tmp_image = ScaleImage(image, columns, rows, &exception); |
| 752 |
DestroyImage(image); |
| 753 |
image = tmp_image; |
| 754 |
CATCH_EXCEPTION(exception); |
| 755 |
if (image == NULL) |
| 756 |
return NULL; /* error resizing image */ |
| 757 |
} |
| 758 |
} |
| 759 |
|
| 760 |
/* rotate image */ |
| 761 |
if (info->angle) { |
| 762 |
image = rotate_image(&img, image, exception); |
| 763 |
} |
| 764 |
|
| 765 |
#if 0 |
| 766 |
DescribeImage(image, stdout, 1 /* verbosity */ ); |
| 767 |
DisplayImages(image_info, image); |
| 768 |
#endif |
| 769 |
|
| 770 |
#define XStandardPixel(map,color,dx) (unsigned long) (map.base_pixel + \ |
| 771 |
(((color).red * map.red_max + (1L << (dx - 1L))) / ((1L << dx) - 1L)) * map.red_mult + \ |
| 772 |
(((color).green * map.green_max + (1L << (dx - 1L))) / ((1L << dx) - 1L)) * map.green_mult + \ |
| 773 |
(((color).blue * map.blue_max + (1L << (dx - 1L))) / ((1L << dx) - 1L)) * map.blue_mult) |
| 774 |
|
| 775 |
img.bgpixel = XStandardPixel(map_info, image->background_color, 16); |
| 776 |
#undef XStandardPixel |
| 777 |
if (img.ximage != NULL) { |
| 778 |
free_ximage(img.ximage); |
| 779 |
img.ximage = NULL; |
| 780 |
} |
| 781 |
|
| 782 |
/* Transform to XImage */ |
| 783 |
pp = GetImagePixels(image, 0, 0, image->columns, image->rows); |
| 784 |
img.ximage = XCreateImage(DISP, G_visual, G_depth, ZPixmap, 0, NULL, |
| 785 |
image->columns, image->rows, BMBITS, 0); |
| 786 |
if (img.ximage == NULL) |
| 787 |
return NULL; |
| 788 |
size = img.ximage->bytes_per_line * img.ximage->height; |
| 789 |
img.ximage->data = malloc(size ? size : 1); /* NOTE: no xmalloc! */ |
| 790 |
if (img.ximage->data == NULL) { |
| 791 |
fprintf(stderr, "Magick: cannot allocate memory for ximage data.\n"); |
| 792 |
XDestroyImage(img.ximage); |
| 793 |
img.ximage = NULL; |
| 794 |
return NULL; |
| 795 |
} |
| 796 |
for (y = 0; y < image->rows; y++) { |
| 797 |
for (x = 0; x < image->columns; x++, pp++) { |
| 798 |
struct rgb color; |
| 799 |
color.r = USHRT_MAX * pp->red / MaxRGB; |
| 800 |
color.g = USHRT_MAX * pp->green / MaxRGB; |
| 801 |
color.b = USHRT_MAX * pp->blue / MaxRGB; |
| 802 |
XPutPixel(img.ximage, x, y, alloc_color(&color, img.bgpixel)); |
| 803 |
} |
| 804 |
} |
| 805 |
DestroyImage(image); |
| 806 |
|
| 807 |
size += sizeof(XDviImage) + sizeof(XImage); |
| 808 |
if (cache_size + size > cache_limit) { |
| 809 |
if (globals.debug & DBG_PS) |
| 810 |
fprintf(stderr, " image not cached (cache limit).\n"); |
| 811 |
return &img; |
| 812 |
} |
| 813 |
|
| 814 |
c = realloc(cache, sizeof(XDviImage) * (ncached + 1)); /* NOTE: no xrealloc! */ |
| 815 |
if (c == NULL) { |
| 816 |
if (globals.debug & DBG_PS) |
| 817 |
fprintf(stderr, " image not cached (cache alloc).\n"); |
| 818 |
return &img; |
| 819 |
} |
| 820 |
cache_size += size; |
| 821 |
cache = c; |
| 822 |
c = cache + (ncached++); |
| 823 |
*c = img; |
| 824 |
img.ximage = NULL; /* so next call won't delete this ximage */ |
| 825 |
|
| 826 |
if (globals.debug & DBG_PS) |
| 827 |
fprintf(stderr, " image cached (cache_size=%uKB, limit=%uKB).\n", |
| 828 |
cache_size / 1024, cache_limit / 1024); |
| 829 |
#if 0 |
| 830 |
DestroyImageInfo(image_info); |
| 831 |
DestroyExceptionInfo(&exception); |
| 832 |
DestroyMagick(); |
| 833 |
#endif |
| 834 |
|
| 835 |
return c; |
| 836 |
} |
| 837 |
|
| 838 |
static void |
| 839 |
display_image(XDviImage * img, int xul, int yul, |
| 840 |
const struct window_expose_info *expose, |
| 841 |
const GC gc) |
| 842 |
{ |
| 843 |
if (globals.debug & DBG_PS) |
| 844 |
fprintf(stderr, " display_image: pos=%d, %d\n", xul, yul); |
| 845 |
|
| 846 |
xul -= img->xoff; |
| 847 |
yul -= img->yoff; |
| 848 |
|
| 849 |
#if 0 |
| 850 |
XPutImage(DISP, mane.win, globals.gc.copy, img->ximage, 0, 0, |
| 851 |
xul, yul - img->ximage.height + 1, |
| 852 |
img->ximage->width, img->ximage->height); |
| 853 |
#else |
| 854 |
/* Try to make background pixels transparent */ |
| 855 |
{ |
| 856 |
XImage *scr; |
| 857 |
int x0 = xul; |
| 858 |
int y0 = yul - img->ximage->height + 1; |
| 859 |
int w = img->ximage->width; |
| 860 |
int h = img->ximage->height; |
| 861 |
int xoff = 0; |
| 862 |
int yoff = 0; |
| 863 |
int x, y; |
| 864 |
|
| 865 |
if (x0 + w <= expose->min_x || x0 >= expose->max_x || |
| 866 |
y0 + h <= expose->min_y || y0 >= expose->max_y) |
| 867 |
return; |
| 868 |
if (x0 < expose->min_x) { |
| 869 |
xoff = expose->min_x - x0; |
| 870 |
x0 = expose->min_x; |
| 871 |
w -= xoff; |
| 872 |
} |
| 873 |
if (x0 + w > expose->max_x) |
| 874 |
w = expose->max_x - x0 + 0; |
| 875 |
if (y0 < expose->min_y) { |
| 876 |
yoff = expose->min_y - y0; |
| 877 |
y0 = expose->min_y; |
| 878 |
h -= yoff; |
| 879 |
} |
| 880 |
if (y0 + h > expose->max_y) |
| 881 |
h = expose->max_y - y0 + 0; |
| 882 |
|
| 883 |
{ |
| 884 |
/* Need to double check the bounds here, because redraw_page() in |
| 885 |
* events.c may set larger bounding box (expose->min_x, expose->min_y, etc.) |
| 886 |
* than actual viewport causing 'BadMatch' error in XGetImage(). |
| 887 |
* |
| 888 |
* Here we clip the rectangle w*h+x0+y0 with the root window. */ |
| 889 |
XWindowAttributes a; |
| 890 |
Status status; |
| 891 |
Window child; |
| 892 |
|
| 893 |
status = XGetWindowAttributes(DISP, mane.win, &a); |
| 894 |
if (!status || a.map_state != IsViewable) |
| 895 |
return; |
| 896 |
XTranslateCoordinates(DISP, mane.win, a.root, x0, y0, &x, &y, |
| 897 |
&child); |
| 898 |
XGetWindowAttributes(DISP, a.root, &a); |
| 899 |
if (x + w <= a.x || x >= a.x + a.width || y + h <= a.y |
| 900 |
|| y >= a.y + a.height) return; |
| 901 |
if (x < a.x) { |
| 902 |
x0 += a.x - x; |
| 903 |
xoff += a.x - x; |
| 904 |
w -= a.x - x; |
| 905 |
x = a.x; |
| 906 |
} |
| 907 |
if (x + w > a.x + a.width) |
| 908 |
w = a.x + a.width - x; |
| 909 |
if (y < a.y) { |
| 910 |
y0 += a.y - y; |
| 911 |
yoff += a.y - y; |
| 912 |
h -= a.y - y; |
| 913 |
y = a.y; |
| 914 |
} |
| 915 |
if (y + h > a.y + a.height) |
| 916 |
h = a.y + a.height - y; |
| 917 |
#if 0 |
| 918 |
XDrawLine(DISP, mane.win, gc, x0, y0, x0 + w - 1, y0); |
| 919 |
XDrawLine(DISP, mane.win, gc, x0 + w - 1, y0, x0 + w - 1, y0 + h - 1); |
| 920 |
XDrawLine(DISP, mane.win, gc, x0 + w - 1, y0 + h - 1, x0, y0 + h - 1); |
| 921 |
XDrawLine(DISP, mane.win, gc, x0, y0 + h - 1, x0, y0); |
| 922 |
XDrawLine(DISP, mane.win, gc, x0, y0, x0 + w - 1, y0 + h - 1); |
| 923 |
XDrawLine(DISP, mane.win, gc, x0 + w - 1, y0, x0, y0 + h - 1); |
| 924 |
|
| 925 |
XDrawLine(DISP, mane.win, gc, expose->min_x, |
| 926 |
expose->min_y, expose->max_x, expose->min_y); |
| 927 |
XDrawLine(DISP, mane.win, gc, expose->max_x, |
| 928 |
expose->min_y, expose->max_x, expose->max_y); |
| 929 |
XDrawLine(DISP, mane.win, gc, expose->max_x, |
| 930 |
expose->max_y, expose->min_x, expose->max_y); |
| 931 |
XDrawLine(DISP, mane.win, gc, expose->min_x, |
| 932 |
expose->max_y, expose->min_x, expose->min_y); |
| 933 |
XDrawLine(DISP, mane.win, gc, expose->max_x, |
| 934 |
expose->max_y, expose->min_x, expose->min_y); |
| 935 |
XDrawLine(DISP, mane.win, gc, expose->min_x, |
| 936 |
expose->max_y, expose->max_x, expose->min_y); |
| 937 |
#endif |
| 938 |
} |
| 939 |
|
| 940 |
#ifdef __GNUC__ |
| 941 |
#warning FIXME: GetImage may still produce 'BadMatch' when dragging \ |
| 942 |
the window around (toward outside of the desktop). |
| 943 |
#endif |
| 944 |
scr = XGetImage(DISP, mane.win, x0, y0, w, h, AllPlanes, ZPixmap); |
| 945 |
|
| 946 |
#warning FIXME: the loops are only a quick hack and are slow. |
| 947 |
for (y = 0; y < h; y++) |
| 948 |
for (x = 0; x < w; x++) { |
| 949 |
unsigned long pixel = 0; |
| 950 |
pixel = XGetPixel(img->ximage, x + xoff, y + yoff); |
| 951 |
if (pixel == img->bgpixel) |
| 952 |
continue; |
| 953 |
XPutPixel(scr, x, y, pixel); |
| 954 |
} |
| 955 |
XPutImage(DISP, mane.win, globals.gc.copy, scr, 0, 0, x0, y0, w, h); |
| 956 |
XDestroyImage(scr); |
| 957 |
} |
| 958 |
#endif |
| 959 |
showtime(" Done."); |
| 960 |
} |
| 961 |
|
| 962 |
static void |
| 963 |
render_image_file(XDviImageInfo *info, int x, int y) |
| 964 |
{ |
| 965 |
XDviImage *im = NULL; |
| 966 |
int w, h; |
| 967 |
|
| 968 |
if (globals.debug & DBG_PS) |
| 969 |
fprintf(stderr, "Magick: render_image_file: pos=(%d,%d)\n", x, y); |
| 970 |
|
| 971 |
#ifdef __GNUC__ |
| 972 |
#warning FIXME: immediate return if image not exposed. |
| 973 |
#endif |
| 974 |
|
| 975 |
if (!INSIDE_MANE_WIN) |
| 976 |
return; |
| 977 |
|
| 978 |
info->angle = bbox_angle >= 0 ? bbox_angle % 360 : 360 - (-bbox_angle) % 360; |
| 979 |
if (globals.debug & DBG_PS) |
| 980 |
fprintf(stderr, "Magick: render_image_file: angle=%d\n", info->angle); |
| 981 |
|
| 982 |
if (resource.postscript && (im = load_image(info)) != NULL) |
| 983 |
display_image(im, x, y, &globals.win_expose, globals.gc.rule); |
| 984 |
|
| 985 |
if (im != NULL && resource.postscript == 1) |
| 986 |
return; |
| 987 |
|
| 988 |
/* Draw bbox of the image */ |
| 989 |
w = ROUND(DPI * info->rwi / 720.0); |
| 990 |
h = ROUND(DPI * info->rhi / 720.0); |
| 991 |
|
| 992 |
if (globals.debug & DBG_PS) |
| 993 |
fprintf(stderr, "Magick: render_image_file: box=%dx%d+%d+%d\n", |
| 994 |
w, h, x, y - h + 1); |
| 995 |
|
| 996 |
if (!info->angle) { |
| 997 |
XDrawRectangle(DISP, currwin.win, globals.gc.high, x, y - h + 1, w, h); |
| 998 |
} |
| 999 |
else |
| 1000 |
{ |
| 1001 |
#if 1 |
| 1002 |
bbox_valid = True; |
| 1003 |
bbox_width = w; |
| 1004 |
bbox_height = bbox_voffset = h; |
| 1005 |
save_bbox(); |
| 1006 |
bbox_valid = False; |
| 1007 |
#else |
| 1008 |
/* code borrowed from special.c:draw_bbox() */ |
| 1009 |
double sin_a = sin(info->angle * (TWOPI / 360)); |
| 1010 |
double cos_a = cos(info->angle * (TWOPI / 360)); |
| 1011 |
double a, b, c, d; |
| 1012 |
|
| 1013 |
a = cos_a * (w-1); |
| 1014 |
b = -sin_a * (w-1); |
| 1015 |
c = -sin_a * (h-1); |
| 1016 |
d = -cos_a * (h-1); |
| 1017 |
|
| 1018 |
XDrawLine(DISP, currwin.win, globals.gc.high, |
| 1019 |
x, y, x + ROUND(a), y + ROUND(b)); |
| 1020 |
XDrawLine(DISP, currwin.win, globals.gc.high, |
| 1021 |
x + ROUND(a), y + ROUND(b), |
| 1022 |
x + ROUND(a + c), y + ROUND(b + d)); |
| 1023 |
XDrawLine(DISP, currwin.win, globals.gc.high, |
| 1024 |
x + ROUND(a + c), y + ROUND(b + d), |
| 1025 |
x + ROUND(c), y + ROUND(d)); |
| 1026 |
XDrawLine(DISP, currwin.win, globals.gc.high, |
| 1027 |
x + ROUND(c), y + ROUND(d), x, y); |
| 1028 |
#endif |
| 1029 |
} |
| 1030 |
} |
| 1031 |
|
| 1032 |
#if !MAGICK_RENDER_PS |
| 1033 |
static Boolean |
| 1034 |
is_ps(char *fn) |
| 1035 |
{ |
| 1036 |
int l; |
| 1037 |
char *p; |
| 1038 |
|
| 1039 |
if (strchr(fn, '`') != NULL) |
| 1040 |
return False; |
| 1041 |
|
| 1042 |
l = strlen(fn); |
| 1043 |
p = fn + l - 1; |
| 1044 |
while (p > fn && *p != '.' && *p != '/') |
| 1045 |
p--; |
| 1046 |
l -= p - fn; |
| 1047 |
if (*p == '.' && ((l >= 3 && !memicmp(p, ".ps", 3)) || |
| 1048 |
(l >= 4 && !memicmp(p, ".eps", 4)))) { |
| 1049 |
if (globals.debug & DBG_PS) |
| 1050 |
fprintf(stderr, "Magick: passing PS/EPS file to psgs.c\n"); |
| 1051 |
return True; |
| 1052 |
} |
| 1053 |
return False; |
| 1054 |
} |
| 1055 |
#endif |
| 1056 |
|
| 1057 |
/* Process EPSF specials */ |
| 1058 |
|
| 1059 |
static Boolean |
| 1060 |
epsf_special(char *cp) |
| 1061 |
{ |
| 1062 |
char *filename; |
| 1063 |
unsigned filename_len; |
| 1064 |
char *p; |
| 1065 |
int n; |
| 1066 |
unsigned long flags = 0, u; |
| 1067 |
double rwi, rhi; |
| 1068 |
enum { LLX, LLY, |
| 1069 |
URX, URY, |
| 1070 |
RWI, RHI, |
| 1071 |
HSIZE, VSIZE, |
| 1072 |
HOFFSET, VOFFSET, |
| 1073 |
HSCALE, VSCALE, |
| 1074 |
ANGLE |
| 1075 |
}; |
| 1076 |
XDviImageInfo info; |
| 1077 |
|
| 1078 |
struct { |
| 1079 |
double val; |
| 1080 |
const char *key; |
| 1081 |
long mask; |
| 1082 |
} key_info[] = { |
| 1083 |
{ 0, "llx", 0x0001 }, |
| 1084 |
{ 0, "lly", 0x0002 }, |
| 1085 |
{ 0, "urx", 0x0004 }, |
| 1086 |
{ 0, "ury", 0x0008 }, |
| 1087 |
{ 0, "rwi", 0x0010 }, |
| 1088 |
{ 0, "rhi", 0x0020 }, |
| 1089 |
{ 0, "hsize", 0x0040 }, |
| 1090 |
{ 0, "vsize", 0x0080 }, |
| 1091 |
{ 0, "hoffset", 0x0100 }, |
| 1092 |
{ 0, "voffset", 0x0200 }, |
| 1093 |
{ 0, "hscale", 0x0400 }, |
| 1094 |
{ 0, "vscale", 0x0800 }, |
| 1095 |
{ 0, "angle", 0x1000 }, |
| 1096 |
}; |
| 1097 |
|
| 1098 |
memset(&info, 0, sizeof info); /* for validating memcmp(&info, ... */ |
| 1099 |
|
| 1100 |
filename = cp; |
| 1101 |
if (*cp == '\'' || *cp == '"') { |
| 1102 |
do |
| 1103 |
++cp; |
| 1104 |
while (*cp != '\0' && *cp != *filename); |
| 1105 |
filename_len = cp - filename - 1; |
| 1106 |
if (*cp == *filename) |
| 1107 |
++cp; |
| 1108 |
++filename; |
| 1109 |
} |
| 1110 |
else { |
| 1111 |
while (*cp != '\0' && !isspace((int)*cp)) |
| 1112 |
++cp; |
| 1113 |
filename_len = cp - filename; |
| 1114 |
} |
| 1115 |
|
| 1116 |
if (filename_len + 1 > MaxTextExtent) { |
| 1117 |
if (globals.debug & DBG_PS) |
| 1118 |
fprintf(stderr, "Magick: filename too long, passing to psgs.c\n"); |
| 1119 |
return False; |
| 1120 |
} |
| 1121 |
|
| 1122 |
memcpy(info.filename, filename, filename_len); /* no need to terminate because of memset(...) above */ |
| 1123 |
|
| 1124 |
if (globals.debug & DBG_PS) |
| 1125 |
fprintf(stderr, "Magick: epsf_special: filename = '%s'\n", |
| 1126 |
info.filename); |
| 1127 |
|
| 1128 |
#if !MAGICK_RENDER_PS |
| 1129 |
if (is_ps(info.filename)) |
| 1130 |
return False; |
| 1131 |
#endif |
| 1132 |
|
| 1133 |
/* Scan for EPSF keywords */ |
| 1134 |
while (*cp != '\0') { |
| 1135 |
while (isspace((int)*cp)) |
| 1136 |
++cp; |
| 1137 |
p = cp; |
| 1138 |
while (*p != '=' && !isspace((int)*p) && *p != '\0') |
| 1139 |
p++; |
| 1140 |
n = p - cp; |
| 1141 |
if (!n) |
| 1142 |
break; |
| 1143 |
if (*p == '=') |
| 1144 |
++p; |
| 1145 |
while (isspace((int)*p)) |
| 1146 |
++p; |
| 1147 |
|
| 1148 |
if (!memcmp(cp, "clip", n)) { |
| 1149 |
/* this is the only key without value */ |
| 1150 |
info.clip = True; |
| 1151 |
} |
| 1152 |
else { |
| 1153 |
size_t i; |
| 1154 |
Boolean found = False; |
| 1155 |
for (i = 0; !found && i < XtNumber(key_info); i++) { |
| 1156 |
if (memicmp(cp, key_info[i].key, n) == 0) { |
| 1157 |
if (sscanf(p, "%lf", &(key_info[i].val)) == 1) { |
| 1158 |
flags |= key_info[i].mask; |
| 1159 |
while (!isspace((int)*p) && *p != '\0') |
| 1160 |
++p; |
| 1161 |
found = True; |
| 1162 |
} |
| 1163 |
else { |
| 1164 |
fprintf(stderr, "Magick: invalid value for %s\n", key_info[i].key); |
| 1165 |
return False; |
| 1166 |
} |
| 1167 |
} |
| 1168 |
} |
| 1169 |
|
| 1170 |
if (!found) { |
| 1171 |
i = *(cp + n); |
| 1172 |
*(cp + n) = '\0'; |
| 1173 |
fprintf(stderr,"Magick: ignoring unknown EPSF key \"%s\".\n", cp); |
| 1174 |
*(cp + n) = i; |
| 1175 |
/* skip '=...' */ |
| 1176 |
while (*p != '\0' && !isspace((int)*p)) |
| 1177 |
++p; |
| 1178 |
} |
| 1179 |
} |
| 1180 |
cp = p; |
| 1181 |
} |
| 1182 |
|
| 1183 |
if (globals.debug & DBG_PS) |
| 1184 |
fprintf(stderr, "Magick: epsf_special: flags = 0x%lx\n", flags); |
| 1185 |
|
| 1186 |
/* only accept the special when llx, lly, urx, ury are properly defined */ |
| 1187 |
u = key_info[LLX].mask | key_info[LLY].mask | |
| 1188 |
key_info[URX].mask | key_info[URY].mask; |
| 1189 |
if ((flags & u) != u) |
| 1190 |
return False; |
| 1191 |
|
| 1192 |
#ifdef __GNUC__ |
| 1193 |
#warning keywords to handle: [hv]size, [hv]offset, [hv]scale, angle \ |
| 1194 |
(ZLB: not used by the graphicx/graphics packages) |
| 1195 |
#endif |
| 1196 |
if (flags & (key_info[HSIZE].mask | key_info[VSIZE].mask | |
| 1197 |
key_info[HOFFSET].mask | key_info[VOFFSET].mask | |
| 1198 |
key_info[HSCALE].mask | key_info[VSCALE].mask | |
| 1199 |
key_info[ANGLE].mask)) |
| 1200 |
fprintf(stderr, "Magick: warning: EPSF keywords '[hv]size', " |
| 1201 |
"'[hv]offset', '[hv]scale', and 'angle' are not " |
| 1202 |
"implemented.\n"); |
| 1203 |
|
| 1204 |
info.urx = key_info[URX].val; |
| 1205 |
info.ury = key_info[URY].val; |
| 1206 |
info.llx = key_info[LLX].val; |
| 1207 |
info.lly = key_info[LLY].val; |
| 1208 |
rwi = key_info[RWI].val; |
| 1209 |
rhi = key_info[RHI].val; |
| 1210 |
|
| 1211 |
if (globals.debug & DBG_PS) |
| 1212 |
fprintf(stderr, "Magick: epsf_special: llx=%0.2f, lly=%0.2f, " |
| 1213 |
"urx=%0.2f, ury=%0.2f\n", |
| 1214 |
info.llx, info.lly, info.urx, info.ury); |
| 1215 |
|
| 1216 |
if (info.urx - info.llx < 1 || info.ury - info.lly < 1) |
| 1217 |
return False; |
| 1218 |
if (info.urx - info.llx > 1e+4 || info.ury - info.lly > 1e+4) |
| 1219 |
return False; |
| 1220 |
if (fabs(info.llx) > 1e+10 || fabs(info.lly) > 1e+10) |
| 1221 |
return False; |
| 1222 |
|
| 1223 |
if (!(flags & (key_info[RWI].mask | key_info[RHI].mask))) { |
| 1224 |
/* both rwi and rhi undefined */ |
| 1225 |
rwi = 10 * (info.urx - info.llx); |
| 1226 |
rhi = 10 * (info.ury - info.lly); |
| 1227 |
} |
| 1228 |
else if (!(flags & key_info[RWI].mask)) { |
| 1229 |
/* rwi undefined, rhi defined */ |
| 1230 |
rwi = rhi * (info.urx - info.llx) / (info.ury - info.lly); |
| 1231 |
} |
| 1232 |
else if (!(flags & key_info[RHI].mask)) { |
| 1233 |
/* rhi undefined, rwi defined */ |
| 1234 |
rhi = rwi * (info.ury - info.lly) / (info.urx - info.llx); |
| 1235 |
} |
| 1236 |
|
| 1237 |
if (globals.debug & DBG_PS) |
| 1238 |
fprintf(stderr, "Magick: epsf_special: rwi=%f, rhi=%f\n", rwi, rhi); |
| 1239 |
|
| 1240 |
if (rwi < 1 || rhi < 1) |
| 1241 |
return False; |
| 1242 |
|
| 1243 |
/* We don't want IM to process large images (>16MB) */ |
| 1244 |
if (DPI * rwi / 720 * DPI * rhi / 720 * G_depth / 8 / 1024 / 1024 > 16) { |
| 1245 |
p = filename + (n = filename_len); |
| 1246 |
if ((n >= 3 && !memicmp(p - 3, ".ps", 3)) || |
| 1247 |
(n >= 4 && !memicmp(p - 4, ".eps", 4))) { |
| 1248 |
if (globals.debug & DBG_PS) |
| 1249 |
fprintf(stderr, "Magick: image too large, passing to psgs.c\n"); |
| 1250 |
return False; |
| 1251 |
} |
| 1252 |
else { |
| 1253 |
/* don't pass non-PS files to psgs.c */ |
| 1254 |
if (globals.debug & DBG_PS) |
| 1255 |
fprintf(stderr, "Magick: image too large, ignored.\n"); |
| 1256 |
return True; |
| 1257 |
} |
| 1258 |
} |
| 1259 |
|
| 1260 |
info.rwi = ROUND(rwi); |
| 1261 |
info.rhi = ROUND(rhi); |
| 1262 |
|
| 1263 |
render_image_file(&info, PXL_H - currwin.base_x, PXL_V - currwin.base_y); |
| 1264 |
|
| 1265 |
return True; |
| 1266 |
} |
| 1267 |
|
| 1268 |
static char * |
| 1269 |
emgraph_get_dimen(char *cp, double *v) |
| 1270 |
{ |
| 1271 |
char *p; |
| 1272 |
typedef struct { |
| 1273 |
int name_len; |
| 1274 |
char *name; |
| 1275 |
double factor; |
| 1276 |
} Unit; |
| 1277 |
Unit *u; |
| 1278 |
/* table for converting an arbitrary unit to bp */ |
| 1279 |
#define UNIT(u) sizeof(u)-1, u |
| 1280 |
static Unit units[] = { |
| 1281 |
{ UNIT("bp"), 1.0 }, |
| 1282 |
{ UNIT("in"), 72.0 }, |
| 1283 |
{ UNIT("cm"), 72.0 / 2.54 }, |
| 1284 |
{ UNIT("mm"), 72.0 / 2.54 * 0.1 }, |
| 1285 |
{ UNIT("pt"), 72.0 / 72.27 }, |
| 1286 |
{ UNIT("sp"), 72.0 / 72.27 / 65536.0 }, |
| 1287 |
{ UNIT("pc"), 12.0 * 72.0 / 72.27 }, |
| 1288 |
{ UNIT("dd"), 1238.0 / 1157.0 * 72.0 / 72.27 }, |
| 1289 |
{ UNIT("cc"), 12.0 * 1238.0 / 1157.0 * 72.0 / 72.27 } |
| 1290 |
}; |
| 1291 |
|
| 1292 |
*v = strtod(cp, &p); |
| 1293 |
if (p == NULL) { |
| 1294 |
if (globals.debug & DBG_PS) |
| 1295 |
fprintf(stderr, "Magick: can't get dimension, image ignored.\n"); |
| 1296 |
return NULL; |
| 1297 |
} |
| 1298 |
|
| 1299 |
while (isspace((int)*p)) |
| 1300 |
++p; |
| 1301 |
for (u = units; u < units + XtNumber(units); u++) |
| 1302 |
if (!memicmp(p, u->name, u->name_len)) |
| 1303 |
break; |
| 1304 |
if (u >= units + XtNumber(units)) { |
| 1305 |
if (globals.debug & DBG_PS) |
| 1306 |
fprintf(stderr, "Magick: unknown unit, image ignored.\n"); |
| 1307 |
return NULL; |
| 1308 |
} |
| 1309 |
*v *= u->factor; |
| 1310 |
|
| 1311 |
if (globals.debug & DBG_PS) |
| 1312 |
fprintf(stderr, "Magick: unit=%s, len=%d, factor=%0.4f, value=%0.4f\n", |
| 1313 |
u->name, u->name_len, u->factor, *v); |
| 1314 |
|
| 1315 |
if (*v < 0.01 || *v > 10000) { |
| 1316 |
if (globals.debug & DBG_PS) |
| 1317 |
fprintf(stderr, "Magick: invalid dimension, image ignored.\n"); |
| 1318 |
return NULL; |
| 1319 |
} |
| 1320 |
|
| 1321 |
return p + u->name_len; |
| 1322 |
} |
| 1323 |
|
| 1324 |
static Boolean |
| 1325 |
emgraph_special(char *cp) |
| 1326 |
{ |
| 1327 |
char *p; |
| 1328 |
double w, h; |
| 1329 |
XDviImageInfo info; |
| 1330 |
|
| 1331 |
memset(&info, 0, sizeof info); |
| 1332 |
|
| 1333 |
/* get filename */ |
| 1334 |
while (isspace((int)*cp)) |
| 1335 |
++cp; |
| 1336 |
p = cp; |
| 1337 |
while (!isspace((int)*p) && *p != '\0' && *p != ',') |
| 1338 |
++p; |
| 1339 |
if ((unsigned)(p - cp) >= sizeof info.filename) { |
| 1340 |
if (globals.debug & DBG_PS) |
| 1341 |
fprintf(stderr, "Magick: filename too long, image ignored.\n"); |
| 1342 |
return False; |
| 1343 |
} |
| 1344 |
if (p <= cp) { |
| 1345 |
if (globals.debug & DBG_PS) |
| 1346 |
fprintf(stderr, "Magick: empty filename, image ignored.\n"); |
| 1347 |
return False; |
| 1348 |
} |
| 1349 |
memcpy(info.filename, cp, p - cp); /* no need to terminate because of memset(...) above */ |
| 1350 |
|
| 1351 |
if (globals.debug & DBG_PS) |
| 1352 |
fprintf(stderr, "Magick: emgraph_special: filename = '%s'\n", |
| 1353 |
info.filename); |
| 1354 |
|
| 1355 |
#if !MAGICK_RENDER_PS |
| 1356 |
if (is_ps(info.filename)) |
| 1357 |
return False; |
| 1358 |
#endif |
| 1359 |
|
| 1360 |
cp = p; |
| 1361 |
|
| 1362 |
/* get width */ |
| 1363 |
if ((cp = strchr(cp, ',')) == NULL) { |
| 1364 |
if (globals.debug & DBG_PS) |
| 1365 |
fprintf(stderr, "Magick: width undefined, image ignored.\n"); |
| 1366 |
return False; |
| 1367 |
} |
| 1368 |
if ((cp = emgraph_get_dimen(++cp, &w)) == NULL) |
| 1369 |
return False; |
| 1370 |
|
| 1371 |
/* get height */ |
| 1372 |
if ((cp = strchr(cp, ',')) == NULL) { |
| 1373 |
if (globals.debug & DBG_PS) |
| 1374 |
fprintf(stderr, "Magick: height undefined, image ignored.\n"); |
| 1375 |
return False; |
| 1376 |
} |
| 1377 |
if ((cp = emgraph_get_dimen(++cp, &h)) == NULL) |
| 1378 |
return False; |
| 1379 |
|
| 1380 |
if (globals.debug & DBG_PS) |
| 1381 |
fprintf(stderr, "Magick: filename=\"%s\", width=%0.2f, height=%0.2f\n", |
| 1382 |
info.filename, w, h); |
| 1383 |
|
| 1384 |
info.llx = info.lly = 0; |
| 1385 |
info.urx = w; |
| 1386 |
info.ury = h; |
| 1387 |
info.rwi = ROUND(10 * w); |
| 1388 |
info.rhi = ROUND(10 * h); |
| 1389 |
|
| 1390 |
if (info.rwi < 1 || info.rhi < 1) |
| 1391 |
return False; |
| 1392 |
|
| 1393 |
render_image_file(&info, PXL_H - currwin.base_x, |
| 1394 |
PXL_V - currwin.base_y + ROUND(h / 72 * DPI) - 1); |
| 1395 |
|
| 1396 |
return True; |
| 1397 |
} |
| 1398 |
|
| 1399 |
/* Filter specials. Returns True if the special has been processed |
| 1400 |
* by ImageMagick, False otherwise. */ |
| 1401 |
Boolean |
| 1402 |
Magick_parse_special(char *cp) |
| 1403 |
{ |
| 1404 |
switch (*cp) { |
| 1405 |
case 'p': |
| 1406 |
case 'P': |
| 1407 |
if (!memicmp(cp, "psfile", 6)) { |
| 1408 |
if (globals.debug & DBG_PS) |
| 1409 |
fprintf(stderr, "Magick: parsing string |%s|\n", cp); |
| 1410 |
cp += 6; |
| 1411 |
while (isspace((int)*cp)) |
| 1412 |
++cp; |
| 1413 |
if (*cp != '=') |
| 1414 |
return False; |
| 1415 |
do |
| 1416 |
++cp; |
| 1417 |
while (isspace((int)*cp)); |
| 1418 |
return epsf_special(cp); |
| 1419 |
} |
| 1420 |
break; |
| 1421 |
case 'e': |
| 1422 |
case 'E': |
| 1423 |
if (!memicmp(cp, "em:", 3)) { |
| 1424 |
cp += 3; |
| 1425 |
while (isspace((int)*cp)) |
| 1426 |
++cp; |
| 1427 |
if (memicmp(cp, "graph", 5)) |
| 1428 |
return False; |
| 1429 |
cp += 5; |
| 1430 |
if (globals.debug & DBG_PS) |
| 1431 |
fprintf(stderr, "Magick: parsing string |em:graph %s|\n", cp); |
| 1432 |
return emgraph_special(cp); |
| 1433 |
} |
| 1434 |
break; |
| 1435 |
} |
| 1436 |
|
| 1437 |
return False; |
| 1438 |
} |
| 1439 |
|
| 1440 |
#else |
| 1441 |
/* silence `empty compilation unit' warnings */ |
| 1442 |
static void bar(void); static void foo(void) { bar(); } static void bar(void) { foo(); } |
| 1443 |
#endif /* MAGICK */ |