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 2.1 by greg, Tue Nov 12 16:04:19 1991 UTC vs.
Revision 2.21 by greg, Sat Jun 21 15:05:01 2003 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines