1 |
– |
/* Copyright (c) 1991 Regents of the University of California */ |
2 |
– |
|
1 |
|
#ifndef lint |
2 |
< |
static char SCCSid[] = "$SunId$ LBL"; |
2 |
> |
static const char RCSid[] = "$Id$"; |
3 |
|
#endif |
6 |
– |
|
4 |
|
/* |
5 |
|
* psign.c - produce picture from text. |
6 |
|
* |
7 |
|
* 7/1/87 |
8 |
|
*/ |
9 |
|
|
10 |
< |
#include "standard.h" |
10 |
> |
#include "copyright.h" |
11 |
|
|
12 |
< |
#include "color.h" |
12 |
> |
#include <string.h> |
13 |
|
|
14 |
+ |
#include "standard.h" |
15 |
+ |
#include "platform.h" |
16 |
+ |
#include "color.h" |
17 |
|
#include "font.h" |
18 |
|
|
19 |
< |
#define SSS 2 /* super-sample size */ |
20 |
< |
|
21 |
< |
#define MAXLINE 512 /* longest allowable line */ |
22 |
< |
|
23 |
< |
#ifndef DEFPATH |
24 |
< |
#define DEFPATH ":/usr/local/lib/ray" |
19 |
> |
#ifndef SSS |
20 |
> |
#define SSS 3 /* super-sample size */ |
21 |
|
#endif |
26 |
– |
#ifndef ULIBVAR |
27 |
– |
#define ULIBVAR "RAYPATH" |
28 |
– |
#endif |
22 |
|
|
23 |
+ |
#define MAXLINE 512 /* longest allowable line */ |
24 |
+ |
|
25 |
|
char *fontfile = "helvet.fnt"; /* our font file */ |
26 |
|
|
27 |
< |
COLR bgcolr = WHTCOLR; /* background color */ |
28 |
< |
COLR fgcolr = BLKCOLR; /* foreground color */ |
27 |
> |
COLOR bgcolor = WHTCOLOR; /* background color */ |
28 |
> |
COLOR fgcolor = BLKCOLOR; /* foreground color */ |
29 |
|
|
30 |
|
int direct = 'r'; /* direction (right, up, left, down) */ |
31 |
|
|
32 |
< |
int cheight = 32; /* character height */ |
33 |
< |
double aspect = 1.67; /* height/width character aspect */ |
32 |
> |
int cheight = 32*SSS; /* character height */ |
33 |
> |
double aspect = 1.67; /* height/width character aspect */ |
34 |
> |
double spacing = 0.0; /* character spacing */ |
35 |
|
int cwidth; /* computed character width */ |
36 |
|
|
37 |
|
unsigned char *ourbitmap; /* our output bitmap */ |
38 |
|
int xsiz, ysiz; /* bitmap dimensions */ |
39 |
|
int xdim; /* size of horizontal scan (bytes) */ |
40 |
|
|
41 |
< |
#define bitop(x,y,op) (ourbitmap[(y)*xdim+((x)>>3)] op (1<<((x)&7))) |
42 |
< |
#define tstbit(x,y) bitop(x,y,&) |
43 |
< |
#define setbit(x,y) bitop(x,y,|=) |
44 |
< |
#define clrbit(x,y) bitop(x,y,&=~) |
45 |
< |
#define tglbit(x,y) bitop(x,y,^=) |
41 |
> |
/* conflicting def's in param.h */ |
42 |
> |
#undef tstbit |
43 |
> |
#undef setbit |
44 |
> |
#undef clrbit |
45 |
> |
#undef tglbit |
46 |
|
|
47 |
+ |
#define bitop(x,y,op) (ourbitmap[(y)*xdim+((x)>>3)] op (1<<((x)&7))) |
48 |
+ |
#define tstbit(x,y) bitop(x,y,&) |
49 |
+ |
#define setbit(x,y) bitop(x,y,|=) |
50 |
+ |
#define clrbit(x,y) bitop(x,y,&=~) |
51 |
+ |
#define tglbit(x,y) bitop(x,y,^=) |
52 |
+ |
|
53 |
|
FONT *ourfont; /* our font */ |
54 |
|
|
53 |
– |
char *libpath; /* library search path */ |
54 |
– |
|
55 |
|
typedef struct line { |
56 |
|
char *s; /* line w/o LF */ |
57 |
|
short *sp; /* character spacing */ |
62 |
|
int nlines, maxline; /* text dimensions */ |
63 |
|
int maxwidth; /* maximum line width (dvi) */ |
64 |
|
|
65 |
– |
extern char *getenv(); |
66 |
– |
extern char *malloc(), *calloc(); |
65 |
|
|
68 |
– |
|
66 |
|
main(argc, argv) |
67 |
|
int argc; |
68 |
|
char *argv[]; |
69 |
|
{ |
70 |
|
int an; |
71 |
< |
|
71 |
> |
SET_FILE_BINARY(stdout); |
72 |
|
for (an = 1; an < argc && argv[an][0] == '-'; an++) |
73 |
|
switch (argv[an][1]) { |
74 |
|
case 'c': /* color */ |
75 |
|
switch (argv[an][2]) { |
76 |
|
case 'f': /* foreground */ |
77 |
< |
setcolr(fgcolr, atof(argv[an+1]), |
77 |
> |
setcolor(fgcolor, atof(argv[an+1]), |
78 |
|
atof(argv[an+2]), |
79 |
|
atof(argv[an+3])); |
80 |
|
an += 3; |
81 |
|
break; |
82 |
|
case 'b': /* background */ |
83 |
< |
setcolr(bgcolr, atof(argv[an+1]), |
83 |
> |
setcolor(bgcolor, atof(argv[an+1]), |
84 |
|
atof(argv[an+2]), |
85 |
|
atof(argv[an+3])); |
86 |
|
an += 3; |
104 |
|
goto unkopt; |
105 |
|
} |
106 |
|
break; |
107 |
+ |
case 'x': /* x resolution */ |
108 |
+ |
xsiz = atoi(argv[++an])*SSS; |
109 |
+ |
break; |
110 |
+ |
case 'y': |
111 |
+ |
ysiz = atoi(argv[++an])*SSS; |
112 |
+ |
break; |
113 |
|
case 'h': /* height of characters */ |
114 |
< |
cheight = atoi(argv[++an]); |
114 |
> |
cheight = atoi(argv[++an])*SSS; |
115 |
|
break; |
116 |
|
case 'a': /* aspect ratio */ |
117 |
|
aspect = atof(argv[++an]); |
118 |
|
break; |
119 |
+ |
case 's': /* spacing */ |
120 |
+ |
spacing = atof(argv[++an]); |
121 |
+ |
break; |
122 |
|
default:; |
123 |
|
unkopt: |
124 |
|
fprintf(stderr, "%s: unknown option: %s\n", |
126 |
|
exit(1); |
127 |
|
} |
128 |
|
/* load font file */ |
123 |
– |
if ((libpath = getenv(ULIBVAR)) == NULL) |
124 |
– |
libpath = DEFPATH; |
129 |
|
ourfont = getfont(fontfile); |
130 |
|
/* get text */ |
131 |
|
if (an == argc) |
138 |
|
/* convert text to bitmap */ |
139 |
|
maptext(); |
140 |
|
/* print header */ |
141 |
+ |
newheader("RADIANCE", stdout); |
142 |
|
printargs(argc, argv, stdout); |
143 |
|
fputformat(COLRFMT, stdout); |
144 |
|
putchar('\n'); |
151 |
|
|
152 |
|
makemap() /* create the bit map */ |
153 |
|
{ |
154 |
< |
cwidth = cheight/aspect + 0.5; |
154 |
> |
double pictaspect; |
155 |
> |
|
156 |
|
if (direct == 'r' || direct == 'l') { |
157 |
< |
xsiz = maxline*cwidth; |
158 |
< |
ysiz = nlines*cheight; |
157 |
> |
if (xsiz <= 0) { |
158 |
> |
cwidth = cheight/aspect + 0.5; |
159 |
> |
xsiz = (long)maxwidth*cwidth >> 8; |
160 |
> |
ysiz = nlines*cheight; |
161 |
> |
} else if (aspect > FTINY) { |
162 |
> |
if (ysiz <= 0) |
163 |
> |
ysiz = cheight*nlines; |
164 |
> |
pictaspect = 256*nlines*aspect/maxwidth; |
165 |
> |
if (pictaspect*xsiz < ysiz) |
166 |
> |
ysiz = pictaspect*xsiz + 0.5; |
167 |
> |
else |
168 |
> |
xsiz = ysiz/pictaspect + 0.5; |
169 |
> |
cheight = ysiz/nlines; |
170 |
> |
cwidth = cheight/aspect; |
171 |
> |
} else { |
172 |
> |
if (ysiz <= 0) |
173 |
> |
ysiz = cheight*nlines; |
174 |
> |
pictaspect = (double)ysiz/xsiz; |
175 |
> |
aspect = pictaspect*maxwidth/(256*nlines); |
176 |
> |
cheight = ysiz/nlines; |
177 |
> |
cwidth = cheight/aspect; |
178 |
> |
} |
179 |
|
} else { /* reverse orientation */ |
180 |
< |
xsiz = nlines*cheight; |
181 |
< |
ysiz = maxline*cwidth; |
180 |
> |
if (ysiz <= 0) { |
181 |
> |
cwidth = cheight/aspect + 0.5; |
182 |
> |
xsiz = nlines*cheight; |
183 |
> |
ysiz = (long)maxwidth*cwidth >> 8; |
184 |
> |
} else if (aspect > FTINY) { |
185 |
> |
if (xsiz <= 0) |
186 |
> |
xsiz = cheight*nlines; |
187 |
> |
pictaspect = maxwidth/(256*nlines*aspect); |
188 |
> |
if (pictaspect*xsiz < ysiz) |
189 |
> |
ysiz = pictaspect*xsiz + 0.5; |
190 |
> |
else |
191 |
> |
xsiz = ysiz/pictaspect + 0.5; |
192 |
> |
cheight = xsiz/nlines; |
193 |
> |
cwidth = cheight/aspect; |
194 |
> |
} else { |
195 |
> |
if (xsiz <= 0) |
196 |
> |
xsiz = cheight*nlines; |
197 |
> |
pictaspect = (double)ysiz/xsiz; |
198 |
> |
aspect = maxwidth/(256*nlines*pictaspect); |
199 |
> |
cheight = xsiz/nlines; |
200 |
> |
cwidth = cheight/aspect; |
201 |
> |
} |
202 |
|
} |
203 |
+ |
if (xsiz % SSS) |
204 |
+ |
xsiz += SSS - xsiz%SSS; |
205 |
+ |
if (ysiz % SSS) |
206 |
+ |
ysiz += SSS - ysiz%SSS; |
207 |
|
xdim = (xsiz+7)/8; |
208 |
< |
ourbitmap = (BYTE *)calloc(ysiz, xdim); |
209 |
< |
if (ourbitmap == NULL) { |
210 |
< |
fprintf(stderr, "out of memory in makemap\n"); |
211 |
< |
exit(1); |
162 |
< |
} |
208 |
> |
ourbitmap = (BYTE *)bmalloc(ysiz*xdim); |
209 |
> |
if (ourbitmap == NULL) |
210 |
> |
error(SYSTEM, "Out of memory in makemap"); |
211 |
> |
memset((char *)ourbitmap, '\0', ysiz*xdim); |
212 |
|
} |
213 |
|
|
214 |
|
|
215 |
|
gettext(fp) /* get text from a file */ |
216 |
|
FILE *fp; |
217 |
|
{ |
169 |
– |
char *fgets(); |
218 |
|
char buf[MAXLINE]; |
219 |
|
register LINE *curl; |
220 |
|
int len; |
227 |
|
if (curl == NULL) |
228 |
|
goto memerr; |
229 |
|
len = strlen(buf); |
230 |
< |
curl->s = malloc(len); |
230 |
> |
curl->s = (char *)malloc(len); |
231 |
|
curl->sp = (short *)malloc(sizeof(short)*len--); |
232 |
< |
if (curl->s == NULL | curl->sp == NULL) |
232 |
> |
if ((curl->s == NULL) | (curl->sp == NULL)) |
233 |
|
goto memerr; |
234 |
|
if (len > maxline) |
235 |
|
maxline = len; |
236 |
|
strncpy(curl->s, buf, len); |
237 |
|
curl->s[len] = '\0'; |
238 |
< |
len = uniftext(curl->sp, curl->s, ourfont); |
238 |
> |
if (spacing < -1./256.) |
239 |
> |
len = squeeztext(curl->sp, curl->s, ourfont, |
240 |
> |
(int)(spacing*-256.)); |
241 |
> |
else if (spacing > 1./256.) |
242 |
> |
len = proptext(curl->sp, curl->s, ourfont, |
243 |
> |
(int)(spacing*256.), 3); |
244 |
> |
else |
245 |
> |
len = uniftext(curl->sp, curl->s, ourfont); |
246 |
|
if (len > maxwidth) |
247 |
|
maxwidth = len; |
248 |
|
curl->next = ourtext; |
251 |
|
} |
252 |
|
return; |
253 |
|
memerr: |
254 |
< |
fprintf(stderr, "out of memory in gettext\n"); |
200 |
< |
exit(1); |
254 |
> |
error(SYSTEM, "Out of memory in gettext"); |
255 |
|
} |
256 |
|
|
257 |
|
|
264 |
|
ourtext = (LINE *)malloc(sizeof(LINE)); |
265 |
|
if (ourtext == NULL) |
266 |
|
goto memerr; |
267 |
< |
ourtext->s = malloc(MAXLINE); |
267 |
> |
ourtext->s = (char *)malloc(MAXLINE); |
268 |
|
if (ourtext->s == NULL) |
269 |
|
goto memerr; |
270 |
|
for (cp = ourtext->s; ac-- > 0; av++) { |
278 |
|
ourtext->sp = (short *)malloc(sizeof(short)*(maxline+1)); |
279 |
|
if (ourtext->sp == NULL) |
280 |
|
goto memerr; |
281 |
< |
maxwidth = uniftext(ourtext->sp, ourtext->s, ourfont); |
281 |
> |
if (spacing < -1./256.) |
282 |
> |
maxwidth = squeeztext(ourtext->sp, ourtext->s, ourfont, |
283 |
> |
(int)(spacing*-256.)); |
284 |
> |
else if (spacing > 1./256.) |
285 |
> |
maxwidth = proptext(ourtext->sp, ourtext->s, ourfont, |
286 |
> |
(int)(spacing*256.), 3); |
287 |
> |
else |
288 |
> |
maxwidth = uniftext(ourtext->sp, ourtext->s, ourfont); |
289 |
|
nlines = 1; |
290 |
|
return; |
291 |
|
memerr: |
292 |
< |
fprintf(stderr, "out of memory in arg_text\n"); |
232 |
< |
exit(1); |
292 |
> |
error(SYSTEM, "Out of memory in arg_text"); |
293 |
|
} |
294 |
|
|
295 |
|
|
399 |
|
writemap(fp) /* write out bitmap */ |
400 |
|
FILE *fp; |
401 |
|
{ |
402 |
+ |
COLR pixval[SSS*SSS+1]; /* possible pixel values */ |
403 |
+ |
COLOR ctmp0, ctmp1; |
404 |
+ |
double d; |
405 |
|
COLR *scanout; |
406 |
< |
int y; |
407 |
< |
register int x; |
406 |
> |
int x, y; |
407 |
> |
register int i, j; |
408 |
> |
int cnt; |
409 |
|
register int inglyph; |
410 |
|
|
411 |
< |
fprintf(fp, "-Y %d +X %d\n", ysiz, xsiz); |
411 |
> |
fprintf(fp, "-Y %d +X %d\n", ysiz/SSS, xsiz/SSS); |
412 |
|
|
413 |
< |
scanout = (COLR *)malloc(xsiz*sizeof(COLR)); |
414 |
< |
if (scanout == NULL) { |
415 |
< |
fprintf(stderr, "out of memory in writemap\n"); |
416 |
< |
exit(1); |
413 |
> |
scanout = (COLR *)malloc(xsiz/SSS*sizeof(COLR)); |
414 |
> |
if (scanout == NULL) |
415 |
> |
error(SYSTEM, "Out of memory in writemap"); |
416 |
> |
for (i = 0; i <= SSS*SSS; i++) { /* compute possible values */ |
417 |
> |
copycolor(ctmp0, fgcolor); |
418 |
> |
d = (double)i/(SSS*SSS); |
419 |
> |
scalecolor(ctmp0, d); |
420 |
> |
copycolor(ctmp1, bgcolor); |
421 |
> |
d = 1.0 - d; |
422 |
> |
scalecolor(ctmp1, d); |
423 |
> |
addcolor(ctmp0, ctmp1); |
424 |
> |
setcolr(pixval[i], colval(ctmp0,RED), |
425 |
> |
colval(ctmp0,GRN), colval(ctmp0,BLU)); |
426 |
|
} |
427 |
< |
for (y = ysiz-1; y >= 0; y--) { |
427 |
> |
for (y = ysiz/SSS-1; y >= 0; y--) { |
428 |
|
inglyph = 0; |
429 |
< |
for (x = 0; x < xsiz; x++) { |
430 |
< |
if (tstbit(x, y)) |
431 |
< |
inglyph ^= 1; |
432 |
< |
if (inglyph) |
433 |
< |
copycolr(scanout[x], fgcolr); |
434 |
< |
else |
435 |
< |
copycolr(scanout[x], bgcolr); |
429 |
> |
for (x = 0; x < xsiz/SSS; x++) { |
430 |
> |
cnt = 0; |
431 |
> |
for (j = 0; j < SSS; j++) |
432 |
> |
for (i = 0; i < SSS; i++) { |
433 |
> |
if (tstbit(x*SSS+i, y*SSS+j)) |
434 |
> |
inglyph ^= 1<<j; |
435 |
> |
if (inglyph & 1<<j) |
436 |
> |
cnt++; |
437 |
> |
} |
438 |
> |
copycolr(scanout[x], pixval[cnt]); |
439 |
|
} |
440 |
< |
if (fwritecolrs(scanout, xsiz, fp) < 0) { |
440 |
> |
if (fwritecolrs(scanout, xsiz/SSS, fp) < 0) { |
441 |
|
fprintf(stderr, "write error in writemap\n"); |
442 |
|
exit(1); |
443 |
|
} |
444 |
|
} |
445 |
< |
free((char *)scanout); |
445 |
> |
free((void *)scanout); |
446 |
|
} |