1 /*
2 * Fig2dev: Translate Fig code to various Devices
3 * Copyright (c) 1991 by Micah Beck
4 * Parts Copyright (c) 1985-1988 by Supoj Sutanthavibul
5 * Parts Copyright (c) 1989-2015 by Brian V. Smith
6 * Parts Copyright (c) 2015-2020 by Thomas Loimer
7 *
8 * Any party obtaining a copy of these files is granted, free of charge, a
9 * full and unrestricted irrevocable, world-wide, paid up, royalty-free,
10 * nonexclusive right and license to deal in this software and documentation
11 * files (the "Software"), including without limitation the rights to use,
12 * copy, modify, merge, publish, distribute, sublicense and/or sell copies
13 * of the Software, and to permit persons who receive copies from any such
14 * party to do so, with the only requirement being that the above copyright
15 * and this permission notice remain intact.
16 *
17 */
18
19 /*
20 * readtif.c: import tiff into PostScript
21 *
22 */
23
24 #ifdef HAVE_CONFIG_H
25 #include "config.h"
26 #endif
27
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <string.h>
31
32 #include "fig2dev.h"
33 #include "object.h"
34 #include "messages.h"
35 #include "readpics.h"
36
37 extern int _read_pcx(FILE *pcxfile, F_pic *pic); /* readpcx.c */
38
39 /* return codes: 1 : success
40 0 : invalid file
41 */
42
43 int
read_tif(F_pic * pic,struct xfig_stream * restrict pic_stream,int * llx,int * lly)44 read_tif(F_pic *pic, struct xfig_stream *restrict pic_stream, int *llx,int *lly)
45 {
46 char cmd_buf[128];
47 char *cmd = cmd_buf;
48 static char *cmd_fmt = NULL;
49 int stat;
50 size_t len;
51 FILE *tiftopcx;
52
53 /* tifftopnm needs a seekable file as input, no pipe.
54 See man tifftopnm(0). */
55 if (uncompressed_content(pic_stream))
56 return 0;
57
58 *llx = *lly = 0;
59
60 /* find command to convert tiff to pcx */
61 if (cmd_fmt == NULL) {
62 if (!system("{ tifftopnm -version && ppmtopcx -version; } "
63 "2>/dev/null"))
64 cmd_fmt = "tifftopnm -quiet '%s' | ppmtopcx -quiet";
65 else if (!system("convert -version >/dev/null"))
66 cmd_fmt = "convert tiff:'%s' pcx:-";
67 else if (!system("gm -version >/dev/null"))
68 cmd_fmt = "gm convert tiff:'%s' pcx:-";
69 else {
70 cmd_fmt = "";
71 put_msg("Cannot read tiff files.\n"
72 "To read tiff files, install either the netpbm, or the imagemagick,\n"
73 "or the graphicsmagick package.");
74 return 0;
75 }
76 }
77 if (*cmd_fmt == '\0')
78 return 0;
79
80 /* write command string, allocating a buffer if necessary */
81 if ((len = sizeof cmd_fmt + strlen(pic_stream->content) - 2) >
82 sizeof cmd_buf &&
83 (cmd = malloc(len)) == NULL) {
84 put_msg(Err_mem);
85 return 0;
86 }
87 sprintf(cmd, cmd_fmt, pic_stream->content);
88
89 if ((tiftopcx = popen(cmd, "r")) == NULL) {
90 err_msg("Cannot convert tiff to pcx, %s", cmd);
91 if (cmd != cmd_buf)
92 free(cmd);
93 return 0;
94 }
95 if (cmd != cmd_buf)
96 free(cmd);
97
98 /* output PostScript comment */
99 fprintf(tfp, "%% Originally from a TIFF File: %s\n\n", pic->file);
100
101 /* now call _read_pcx to read the pcx file */
102 stat = _read_pcx(tiftopcx, pic);
103 /* close the pipe */
104 pclose(tiftopcx);
105
106 return stat;
107 }
108