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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines