ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/px/psign.c
(Generate patch)

Comparing ray/src/px/psign.c (file contents):
Revision 1.1 by greg, Thu Feb 2 10:49:28 1989 UTC vs.
Revision 2.20 by schorsch, Sun Jun 8 12:03:10 2003 UTC

# Line 1 | Line 1
1 /* Copyright (c) 1987 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  <stdio.h>
11 <
10 > #include  "standard.h"
11 > #include  "platform.h"
12   #include  "color.h"
13 + #include  "font.h"
14  
15 < #define  MAXLINE                512     /* longest allowable line */
15 > #ifndef  SSS
16 > #define  SSS                    3       /* super-sample size */
17 > #endif
18  
19 < char  *fontfile = "/usr/local/lib/ray/helvet.fnt";      /* our font file */
19 > #define  MAXLINE                512     /* longest allowable line */
20  
21 < COLR  bgcolr = WHTCOLR;                 /* background color */
22 < COLR  fgcolr = BLKCOLR;                 /* foreground color */
21 > char  *fontfile = "helvet.fnt";         /* our font file */
22  
23 + COLOR  bgcolor = WHTCOLOR;              /* background color */
24 + COLOR  fgcolor = BLKCOLOR;              /* foreground color */
25 +
26   int  direct = 'r';                      /* direction (right, up, left, down) */
27  
28 < int  cheight = 32;                      /* character height */
29 < double  aspect = 1.67;                  /* height/width character aspect */
28 > int  cheight = 32*SSS;                  /* character height */
29 > double  aspect = 1.67;                  /* height/width character aspect */
30 > double  spacing = 0.0;                  /* character spacing */
31   int  cwidth;                            /* computed character width */
32  
33   unsigned char  *ourbitmap;              /* our output bitmap */
34   int  xsiz, ysiz;                        /* bitmap dimensions */
35   int  xdim;                              /* size of horizontal scan (bytes) */
36  
37 < #define  bitop(x,y,op)          (ourbitmap[(y)*xdim+((x)>>3)] op (1<<((x)&7)))
38 < #define  tstbit(x,y)            bitop(x,y,&)
39 < #define  setbit(x,y)            bitop(x,y,|=)
40 < #define  clrbit(x,y)            bitop(x,y,&=~)
41 < #define  tglbit(x,y)            bitop(x,y,^=)
37 > #define  bitop(x,y,op)          (ourbitmap[(y)*xdim+((x)>>3)] op (1<<((x)&7)))
38 > #define  tstbit(x,y)            bitop(x,y,&)
39 > #define  setbit(x,y)            bitop(x,y,|=)
40 > #define  clrbit(x,y)            bitop(x,y,&=~)
41 > #define  tglbit(x,y)            bitop(x,y,^=)
42  
43 < typedef unsigned char  GLYPH;
43 > FONT  *ourfont;                         /* our font */
44  
42 GLYPH  *ourfont[128];                   /* our font */
43
45   typedef struct line {
46          char  *s;               /* line w/o LF */
47 +        short  *sp;             /* character spacing */
48          struct line  *next;     /* next line up */
49   } LINE;
50  
51   LINE  *ourtext;                         /* our text */
52   int  nlines, maxline;                   /* text dimensions */
53 + int  maxwidth;                          /* maximum line width (dvi) */
54  
52 char  *malloc(), *calloc();
55  
54
56   main(argc, argv)
57   int  argc;
58   char  *argv[];
59   {
59        double  atof();
60          int  an;
61 <
61 >        SET_FILE_BINARY(stdout);
62          for (an = 1; an < argc && argv[an][0] == '-'; an++)
63                  switch (argv[an][1]) {
64                  case 'c':                       /* color */
65                          switch (argv[an][2]) {
66                          case 'f':                       /* foreground */
67 <                                setcolr(fgcolr, atof(argv[an+1]),
67 >                                setcolor(fgcolor, atof(argv[an+1]),
68                                                  atof(argv[an+2]),
69                                                  atof(argv[an+3]));
70                                  an += 3;
71                                  break;
72                          case 'b':                       /* background */
73 <                                setcolr(bgcolr, atof(argv[an+1]),
73 >                                setcolor(bgcolor, atof(argv[an+1]),
74                                                  atof(argv[an+2]),
75                                                  atof(argv[an+3]));
76                                  an += 3;
# Line 94 | Line 94 | char  *argv[];
94                                  goto unkopt;
95                          }
96                          break;
97 +                case 'x':                       /* x resolution */
98 +                        xsiz = atoi(argv[++an])*SSS;
99 +                        break;
100 +                case 'y':
101 +                        ysiz = atoi(argv[++an])*SSS;
102 +                        break;
103                  case 'h':                       /* height of characters */
104 <                        cheight = atoi(argv[++an]);
104 >                        cheight = atoi(argv[++an])*SSS;
105                          break;
106                  case 'a':                       /* aspect ratio */
107                          aspect = atof(argv[++an]);
108                          break;
109 +                case 's':                       /* spacing */
110 +                        spacing = atof(argv[++an]);
111 +                        break;
112                  default:;
113   unkopt:
114                          fprintf(stderr, "%s: unknown option: %s\n",
115                                          argv[0], argv[an]);
116                          exit(1);
117                  }
118 +                                        /* load font file */
119 +        ourfont = getfont(fontfile);
120                                          /* get text */
121          if (an == argc)
122                  gettext(stdin);
# Line 114 | Line 125 | unkopt:
125  
126                                          /* create bit map */
127          makemap();
117                                        /* load font file */
118        loadfont();
128                                          /* convert text to bitmap */
129          maptext();
130                                          /* print header */
131 +        newheader("RADIANCE", stdout);
132          printargs(argc, argv, stdout);
133 <        printf("\n\n");
133 >        fputformat(COLRFMT, stdout);
134 >        putchar('\n');
135                                          /* write out bitmap */
136          writemap(stdout);
137  
# Line 130 | Line 141 | unkopt:
141  
142   makemap()                       /* create the bit map */
143   {
144 <        cwidth = cheight/aspect + 0.5;
144 >        double  pictaspect;
145 >        
146          if (direct == 'r' || direct == 'l') {
147 <                xsiz = maxline*cwidth;
148 <                ysiz = nlines*cheight;
147 >                if (xsiz <= 0) {
148 >                        cwidth = cheight/aspect + 0.5;
149 >                        xsiz = (long)maxwidth*cwidth >> 8;
150 >                        ysiz = nlines*cheight;
151 >                } else if (aspect > FTINY) {
152 >                        if (ysiz <= 0)
153 >                                ysiz = cheight*nlines;
154 >                        pictaspect = 256*nlines*aspect/maxwidth;
155 >                        if (pictaspect*xsiz < ysiz)
156 >                                ysiz = pictaspect*xsiz + 0.5;
157 >                        else
158 >                                xsiz = ysiz/pictaspect + 0.5;
159 >                        cheight = ysiz/nlines;
160 >                        cwidth = cheight/aspect;
161 >                } else {
162 >                        if (ysiz <= 0)
163 >                                ysiz = cheight*nlines;
164 >                        pictaspect = (double)ysiz/xsiz;
165 >                        aspect = pictaspect*maxwidth/(256*nlines);
166 >                        cheight = ysiz/nlines;
167 >                        cwidth = cheight/aspect;
168 >                }
169          } else {                        /* reverse orientation */
170 <                xsiz = nlines*cheight;
171 <                ysiz = maxline*cwidth;
170 >                if (ysiz <= 0) {
171 >                        cwidth = cheight/aspect + 0.5;
172 >                        xsiz = nlines*cheight;
173 >                        ysiz = (long)maxwidth*cwidth >> 8;
174 >                } else if (aspect > FTINY) {
175 >                        if (xsiz <= 0)
176 >                                xsiz = cheight*nlines;
177 >                        pictaspect = maxwidth/(256*nlines*aspect);
178 >                        if (pictaspect*xsiz < ysiz)
179 >                                ysiz = pictaspect*xsiz + 0.5;
180 >                        else
181 >                                xsiz = ysiz/pictaspect + 0.5;
182 >                        cheight = xsiz/nlines;
183 >                        cwidth = cheight/aspect;
184 >                } else {
185 >                        if (xsiz <= 0)
186 >                                xsiz = cheight*nlines;
187 >                        pictaspect = (double)ysiz/xsiz;
188 >                        aspect = maxwidth/(256*nlines*pictaspect);
189 >                        cheight = xsiz/nlines;
190 >                        cwidth = cheight/aspect;
191 >                }
192          }
193 +        if (xsiz % SSS)
194 +                xsiz += SSS - xsiz%SSS;
195 +        if (ysiz % SSS)
196 +                ysiz += SSS - ysiz%SSS;
197          xdim = (xsiz+7)/8;
198 <        ourbitmap = (BYTE *)calloc(ysiz, xdim);
199 <        if (ourbitmap == NULL) {
200 <                fprintf(stderr, "out of memory in makemap\n");
201 <                exit(1);
146 <        }
198 >        ourbitmap = (BYTE *)bmalloc(ysiz*xdim);
199 >        if (ourbitmap == NULL)
200 >                error(SYSTEM, "Out of memory in makemap");
201 >        bzero((char *)ourbitmap, ysiz*xdim);
202   }
203  
204  
150 loadfont()                      /* load the font file */
151 {
152        FILE  *fp;
153        char  *err;
154        int  gn, ngv, gv;
155        register GLYPH  *g;
156
157        if ((fp = fopen(fontfile, "r")) == NULL) {
158                fprintf(stderr, "cannot open font file \"%s\"\n",
159                                fontfile);
160                exit(1);
161        }
162        while (fscanf(fp, "%d", &gn) == 1) {    /* get each glyph */
163                if (gn < 0 || gn > 127) {
164                        err = "illegal";
165                        goto fonterr;
166                }
167                if (ourfont[gn] != NULL) {
168                        err = "duplicate";
169                        goto fonterr;
170                }
171                if (fscanf(fp, "%d", &ngv) != 1 ||
172                                ngv < 0 || ngv > 255) {
173                        err = "bad # vertices for";
174                        goto fonterr;
175                }
176                g = (GLYPH *)malloc((2*ngv+1)*sizeof(GLYPH));
177                if (g == NULL)
178                        goto memerr;
179                ourfont[gn] = g;
180                *g++ = ngv;
181                ngv *= 2;
182                while (ngv--) {
183                        if (fscanf(fp, "%d", &gv) != 1 ||
184                                        gv < 0 || gv > 255) {
185                                err = "bad vertex for";
186                                goto fonterr;
187                        }
188                        *g++ = gv;
189                }
190        }
191        fclose(fp);
192        return;
193 fonterr:
194        fprintf(stderr, "%s character (%d) in font file \"%s\"\n",
195                        err, gn, fontfile);
196        exit(1);
197 memerr:
198        fprintf(stderr, "out of memory in loadfont\n");
199        exit(1);
200 }
201
202
205   gettext(fp)                     /* get text from a file */
206   FILE  *fp;
207   {
206        char  *fgets();
208          char  buf[MAXLINE];
209          register LINE  *curl;
210          int  len;
211  
212          maxline = 0;
213 +        maxwidth = 0;
214          nlines = 0;
215          while (fgets(buf, MAXLINE, fp) != NULL) {
216                  curl = (LINE *)malloc(sizeof(LINE));
217                  if (curl == NULL)
218                          goto memerr;
219                  len = strlen(buf);
220 <                curl->s = malloc(len--);
221 <                if (curl->s == NULL)
220 >                curl->s = (char *)malloc(len);
221 >                curl->sp = (short *)malloc(sizeof(short)*len--);
222 >                if (curl->s == NULL | curl->sp == NULL)
223                          goto memerr;
224 +                if (len > maxline)
225 +                        maxline = len;
226                  strncpy(curl->s, buf, len);
227                  curl->s[len] = '\0';
228 +                if (spacing < -1./256.)
229 +                        len = squeeztext(curl->sp, curl->s, ourfont,
230 +                                        (int)(spacing*-256.));
231 +                else if (spacing > 1./256.)
232 +                        len = proptext(curl->sp, curl->s, ourfont,
233 +                                        (int)(spacing*256.), 3);
234 +                else
235 +                        len = uniftext(curl->sp, curl->s, ourfont);
236 +                if (len > maxwidth)
237 +                        maxwidth = len;
238                  curl->next = ourtext;
239                  ourtext = curl;
225                if (len > maxline)
226                        maxline = len;
240                  nlines++;
241          }
242          return;
243   memerr:
244 <        fprintf(stderr, "out of memory in gettext\n");
232 <        exit(1);
244 >        error(SYSTEM, "Out of memory in gettext");
245   }
246  
247  
# Line 242 | Line 254 | char  *av[];
254          ourtext = (LINE *)malloc(sizeof(LINE));
255          if (ourtext == NULL)
256                  goto memerr;
257 <        ourtext->s = malloc(MAXLINE);
257 >        ourtext->s = (char *)malloc(MAXLINE);
258          if (ourtext->s == NULL)
259                  goto memerr;
260          for (cp = ourtext->s; ac-- > 0; av++) {
# Line 253 | Line 265 | char  *av[];
265          *--cp = '\0';
266          ourtext->next = NULL;
267          maxline = strlen(ourtext->s);
268 +        ourtext->sp = (short *)malloc(sizeof(short)*(maxline+1));
269 +        if (ourtext->sp == NULL)
270 +                goto memerr;
271 +        if (spacing < -1./256.)
272 +                maxwidth = squeeztext(ourtext->sp, ourtext->s, ourfont,
273 +                                (int)(spacing*-256.));
274 +        else if (spacing > 1./256.)
275 +                maxwidth = proptext(ourtext->sp, ourtext->s, ourfont,
276 +                                (int)(spacing*256.), 3);
277 +        else
278 +                maxwidth = uniftext(ourtext->sp, ourtext->s, ourfont);
279          nlines = 1;
280          return;
281   memerr:
282 <        fprintf(stderr, "out of memory in arg_text\n");
260 <        exit(1);
282 >        error(SYSTEM, "Out of memory in arg_text");
283   }
284  
285  
286   maptext()                       /* map our text */
287   {
288          register LINE  *curl;
289 <        int  l;
290 <        register int  c;
289 >        int  l, len;
290 >        register int  i, c;
291  
292 <        for (l = 0, curl = ourtext; curl != NULL; l++, curl = curl->next)
293 <                for (c = strlen(curl->s)-1; c >= 0; c--)
294 <                        mapglyph(ourfont[curl->s[c]], c, l);
292 >        for (l = 0, curl = ourtext; curl != NULL; l += 256, curl = curl->next) {
293 >                len = strlen(curl->s); c = 0;
294 >                for (i = 0; i < len; i++) {
295 >                        c += curl->sp[i];
296 >                        mapglyph(ourfont->fg[curl->s[i]&0xff], c, l);
297 >                }
298 >        }
299   }
300  
301  
302   mapglyph(gl, tx0, ty0)          /* convert a glyph */
303 < register GLYPH  *gl;
303 > GLYPH  *gl;
304   int  tx0, ty0;
305   {
306          int  n;
307 +        register GORD  *gp;
308          int  p0[2], p1[2];
309  
310          if (gl == NULL)
311                  return;
312  
313 <        tx0 <<= 8; ty0 <<= 8;
314 <        n = *gl++;
315 <        mapcoord(p0, gl[2*n-2]+tx0, gl[2*n-1]+ty0);
313 >        n = gl->nverts;
314 >        gp = gvlist(gl);
315 >        mapcoord(p0, gp[2*n-2]+tx0, gp[2*n-1]+ty0);
316          while (n--) {
317 <                mapcoord(p1, gl[0]+tx0, gl[1]+ty0);
317 >                mapcoord(p1, gp[0]+tx0, gp[1]+ty0);
318                  mapedge(p0[0], p0[1], p1[0]-p0[0], p1[1]-p0[1]);
319                  p0[0] = p1[0]; p0[1] = p1[1];
320 <                gl += 2;
320 >                gp += 2;
321          }
322   }
323  
# Line 362 | Line 389 | int  run, rise;
389   writemap(fp)                    /* write out bitmap */
390   FILE  *fp;
391   {
392 +        COLR  pixval[SSS*SSS+1];        /* possible pixel values */
393 +        COLOR  ctmp0, ctmp1;
394 +        double  d;
395          COLR  *scanout;
396 <        int  y;
397 <        register int  x;
396 >        int  x, y;
397 >        register int  i, j;
398 >        int  cnt;
399          register int  inglyph;
400  
401 <        fprintf(fp, "-Y %d +X %d\n", ysiz, xsiz);
401 >        fprintf(fp, "-Y %d +X %d\n", ysiz/SSS, xsiz/SSS);
402  
403 <        scanout = (COLR *)malloc(xsiz*sizeof(COLR));
404 <        if (scanout == NULL) {
405 <                fprintf(stderr, "out of memory in writemap\n");
406 <                exit(1);
403 >        scanout = (COLR *)malloc(xsiz/SSS*sizeof(COLR));
404 >        if (scanout == NULL)
405 >                error(SYSTEM, "Out of memory in writemap");
406 >        for (i = 0; i <= SSS*SSS; i++) {        /* compute possible values */
407 >                copycolor(ctmp0, fgcolor);
408 >                d = (double)i/(SSS*SSS);
409 >                scalecolor(ctmp0, d);
410 >                copycolor(ctmp1, bgcolor);
411 >                d = 1.0 - d;
412 >                scalecolor(ctmp1, d);
413 >                addcolor(ctmp0, ctmp1);
414 >                setcolr(pixval[i], colval(ctmp0,RED),
415 >                                colval(ctmp0,GRN), colval(ctmp0,BLU));
416          }
417 <        for (y = ysiz-1; y >= 0; y--) {
417 >        for (y = ysiz/SSS-1; y >= 0; y--) {
418                  inglyph = 0;
419 <                for (x = 0; x < xsiz; x++) {
420 <                        if (tstbit(x, y))
421 <                                inglyph ^= 1;
422 <                        if (inglyph)
423 <                                copycolr(scanout[x], fgcolr);
424 <                        else
425 <                                copycolr(scanout[x], bgcolr);
419 >                for (x = 0; x < xsiz/SSS; x++) {
420 >                        cnt = 0;
421 >                        for (j = 0; j < SSS; j++)
422 >                                for (i = 0; i < SSS; i++) {
423 >                                        if (tstbit(x*SSS+i, y*SSS+j))
424 >                                                inglyph ^= 1<<j;
425 >                                        if (inglyph & 1<<j)
426 >                                                cnt++;
427 >                                }
428 >                        copycolr(scanout[x], pixval[cnt]);
429                  }
430 <                if (fwritecolrs(scanout, xsiz, fp) < 0) {
430 >                if (fwritecolrs(scanout, xsiz/SSS, fp) < 0) {
431                          fprintf(stderr, "write error in writemap\n");
432                          exit(1);
433                  }
434          }
435 <        free((char *)scanout);
393 < }
394 <
395 <
396 < printargs(ac, av, fp)           /* print arguments to a file */
397 < int  ac;
398 < char  **av;
399 < FILE  *fp;
400 < {
401 <        while (ac-- > 0) {
402 <                fputs(*av++, fp);
403 <                putc(' ', fp);
404 <        }
435 >        free((void *)scanout);
436   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines