File: genbitmaps.c

package info (click to toggle)
transfig 1%3A3.2.0-2.2
  • links: PTS
  • area: main
  • in suites: hamm
  • size: 876 kB
  • ctags: 1,965
  • sloc: ansic: 14,075; makefile: 1,150; sh: 38; csh: 10
file content (196 lines) | stat: -rw-r--r-- 5,507 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
/*
 * TransFig: Facility for Translating Fig code
 * Copyright (c) 1985 Supoj Sutantavibul
 * Copyright (c) 1991 Micah Beck
 *
 * THE AUTHORS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
 * EVENT SHALL THE AUTHORS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
 * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
 * PERFORMANCE OF THIS SOFTWARE.
 *
 * The X Consortium, and any party obtaining a copy of these files from
 * the X Consortium, directly or indirectly, is granted, free of charge, a
 * full and unrestricted irrevocable, world-wide, paid up, royalty-free,
 * nonexclusive right and license to deal in this software and
 * documentation files (the "Software"), including without limitation the
 * rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons who receive
 * copies from any such party to do so, with the only requirement being
 * that this copyright notice remain intact.  This license includes without
 * limitation a license to do the foregoing actions under any patents of
 * the party supplying this software to the X Consortium.
 */

/* 
 *	genbitmaps.c : bitmap driver for fig2dev 
 *
 *	Author: Brian V. Smith
 *		Handles GIF, PCX, JPEG, TIFF, XBM and XPM.
 *		Uses genps functions to generate PostScript output then
 *		calls ghostscript to convert it to the output language
 *		if ghostscript has a driver for that language, or to ppm
 *		if ghostscript otherwise. If the latter, ppmtoxxx is then
 *		called to make the final XXX file.
 */

#include <sys/file.h>
#include <stdio.h>
#include <math.h>
#include "fig2dev.h"
#include "object.h"
#include "texfonts.h"

extern void 
	genps_arc(),
	genps_ellipse(),
	genps_line(),
	genps_spline(),
	genps_text();

static char gscom[1000],*gsdev,tmpname[PATH_MAX];
Boolean	direct;
char	*ofile;
int	width,height;
int	jpeg_quality=75;

void
genbitmaps_option(opt, optarg)
char opt;
char *optarg;
{
    switch (opt) {

	case 'q':			/* jpeg image quality */
	    if (strcmp(lang,"jpeg") != 0)
		fprintf(stderr,"-q option only allowed for jpeg quality; ignored\n");
	    sscanf(optarg,"%d",&jpeg_quality);
	    break;
	default:
	    break;
    }
}

void
genbitmaps_start(objects)
F_compound	*objects;
{

    char extra_options[200];
    float fscale;

    /* make small margin around in case bounding box errors */

    llx -= 200;
    ury += 200;

fscale = 14.0;

    /* make command for ghostscript */
    width=round(mag*(urx-llx)/fscale/*THICK_SCALE*/);
    height=round(mag*(ury-lly)/fscale/*THICK_SCALE*/);

/*
    width += 45;
    height += 13;
*/

    /* Add conditionals here if gs has a driver built-in */
    /* gs has a driver for png, ppm, pcx, jpeg and tiff */

    direct = TRUE;
    ofile = to;
    extra_options[0]='\0';

    if (strcmp(lang,"png")==0) {
	gsdev="png256";
    } else if (strcmp(lang,"pcx")==0) {
	gsdev="pcx256";
    } else if (strcmp(lang,"ppm")==0) {
	gsdev="ppmraw";
    } else if (strcmp(lang,"tiff")==0) {
	/* use the 24-bit - unfortunately, it doesn't use compression */
	gsdev="tiff24nc";
    } else if (strcmp(lang,"jpeg")==0) {
	gsdev="jpeg";
	/* set quality for JPEG */
	sprintf(extra_options,"-dJPEGQ=%d",jpeg_quality);
    } else {
	/* no driver in gs, use ppm output then use ppmtoxxx later */
	gsdev="ppmraw";
	/* make a unique name for the temporary ppm file */
	sprintf(tmpname,"f2d%d.ppm",getpid());
	ofile = tmpname;
	direct = FALSE;
    }
    /* make up the command for gs */
    sprintf(gscom, "gs -q -dSAFER -sDEVICE=%s -r80 -g%dx%d -sOutputFile=%s %s -",
		   gsdev,width,height,ofile,extra_options);
    /* divert output from ps driver to the pipe into ghostscript */
    /* but first close the output file that main() opened */
    fclose(tfp);
    if ((tfp = popen(gscom,"w" )) == 0) {
	fprintf(stderr,"Can't open pipe to ghostscript\n");
	exit(1);
    }
    genps_start(objects);
}

void
genbitmaps_end()
{
	char com[200];
	char *outfile;

	/* wrap up the postscript output */
	genps_end();
	/* add a showpage so ghostscript will produce output */
	fprintf(tfp, "showpage\n");

	if (pclose(tfp) != 0) {
	    fprintf(stderr,"Error in ghostcript command\n");
	    return;
	}
	/* for the formats that are only 8-bits, reduce the colors to 256 */
	/* and pipe through the ppm converter for that format */
	if (!direct) {
	    if (strcmp(lang, "gif")==0) {
		sprintf(com,"(ppmquant 256 %s | ppmtogif",tmpname);
	    } else if (strcmp(lang, "xbm")==0) {
		sprintf(com,"(ppmtopgm %s | pgmtopbm | pbmtoxbm",tmpname);
	    } else if (strcmp(lang, "xpm")==0) {
		sprintf(com,"(ppmquant 256 %s | ppmtoxpm",tmpname);
	    } else if (strcmp(lang, "acad")==0) {
		sprintf(com,"(ppmtoacad %s",tmpname);
	    }
	    if (tfp != stdout) {
		/* finally, route output from ppmtoxxx to final output file, if
		   not going to stdout */
		strcat(com," > ");
		strcat(com,to);
	    }
	    strcat(com,") 2> /dev/null");
	    /* execute the ppm program */
	    system(com);
	    /* finally, remove the temporary file */
	    unlink(tmpname);
	}
	/* we've already closed the original output file */
	tfp = 0;
}

struct driver dev_bitmaps = {
  	genbitmaps_option,
	genbitmaps_start,
	genps_arc,
	genps_ellipse,
	genps_line,
	genps_spline,
	genps_text,
	genbitmaps_end,
	INCLUDE_TEXT
};