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

Comparing ray/src/px/ra_ppm.c (file contents):
Revision 2.8 by gwlarson, Wed Feb 10 08:43:41 1999 UTC vs.
Revision 2.19 by greg, Sat Jun 7 05:09:46 2025 UTC

# Line 1 | Line 1
1 /* Copyright (c) 1992 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   *  program to convert between RADIANCE and Poskanzer Pixmaps
6   */
7  
11 #include  <stdio.h>
12
13 #ifdef MSDOS
14 #include  <fcntl.h>
15 #endif
16
8   #include  <math.h>
18
9   #include  <ctype.h>
10 <
10 > #include  "platform.h"
11 > #include  "rtio.h"
12   #include  "color.h"
22
13   #include  "resolu.h"
14  
25
26 extern char  *malloc();
27
28 int  agryscan(), bgryscan(), aclrscan(), bclrscan();
29 int  agryscan2(), bgryscan2(), aclrscan2(), bclrscan2();
30 int  normval();
31 unsigned int    scanint(), intv(), getby2();
32
15   #define fltv(i)         (((i)+.5)/(maxval+1.))
34
16   int  bradj = 0;                         /* brightness adjustment */
36
17   double  gamcor = 2.2;                   /* gamma correction value */
38
18   int  maxval = 255;                      /* maximum primary value */
19 + int  xmax, ymax;
20  
21 < char  *progname;
21 > typedef int colrscanf_t(COLR *scan, int len, FILE *fp);
22 > typedef int colorscanf_t(COLOR *scan, int len, FILE *fp);
23  
24 < int  xmax, ymax;
24 > static colrscanf_t agryscan, bgryscan, aclrscan, bclrscan;
25 > static void ppm2ra(colrscanf_t *getscan);
26 > static void ra2ppm(int  binary, int  grey);
27  
28 + static colorscanf_t agryscan2, bgryscan2, aclrscan2, bclrscan2;
29 + static void ppm2ra2(colorscanf_t *getscan);
30 + static void ra2ppm2(int  binary, int  grey);
31  
32 < main(argc, argv)
33 < int  argc;
34 < char  *argv[];
32 > static int normval(unsigned int  v);
33 > static unsigned int scanint(FILE  *fp);
34 > static unsigned int intv(double v);
35 > static unsigned int getby2(FILE *fp);
36 > static void putby2(unsigned int w, FILE *fp);
37 > static void quiterr(char  *err);
38 >
39 >
40 > int
41 > main(
42 >        int  argc,
43 >        char  *argv[]
44 > )
45   {
46          char  inpbuf[2];
47          int  gryflag = 0;
# Line 54 | Line 50 | char  *argv[];
50          int  ptype;
51          int  i;
52          
53 <        progname = argv[0];
53 >        fixargv0(argv[0]);
54  
55          for (i = 1; i < argc; i++)
56                  if (argv[i][0] == '-')
# Line 104 | Line 100 | char  *argv[];
100                  if (read(fileno(stdin), inpbuf, 2) != 2 || inpbuf[0] != 'P')
101                          quiterr("input not a Poskanzer Pixmap");
102                  ptype = inpbuf[1];
103 < #ifdef MSDOS
103 > #if defined(_WIN32) || defined(_WIN64)
104                  if (ptype > 4)
105 <                        setmode(fileno(stdin), O_BINARY);
106 <                setmode(fileno(stdout), O_BINARY);
105 >                        SET_FILE_BINARY(stdin);
106 >                SET_FILE_BINARY(stdout);
107   #endif
108                  xmax = scanint(stdin);
109                  ymax = scanint(stdin);
# Line 154 | Line 150 | char  *argv[];
150                                  quiterr("unsupported Pixmap type");
151                          }
152          } else {
153 < #ifdef MSDOS
154 <                setmode(fileno(stdin), O_BINARY);
153 > #if defined(_WIN32) || defined(_WIN64)
154 >                SET_FILE_BINARY(stdin);
155                  if (binflag)
156 <                        setmode(fileno(stdout), O_BINARY);
156 >                        SET_FILE_BINARY(stdout);
157   #endif
158                                          /* get header info. */
159                  if (checkheader(stdin, COLRFMT, NULL) < 0 ||
# Line 181 | Line 177 | userr:
177   }
178  
179  
180 < quiterr(err)            /* print message and exit */
181 < char  *err;
180 > static void
181 > quiterr(                /* print message and exit */
182 >        char  *err
183 > )
184   {
185          if (err != NULL) {
186                  fprintf(stderr, "%s: %s\n", progname, err);
# Line 192 | Line 190 | char  *err;
190   }
191  
192  
193 < ppm2ra(getscan)         /* convert 1-byte Pixmap to Radiance picture */
194 < int  (*getscan)();
193 > static void
194 > ppm2ra(         /* convert 1-byte Pixmap to Radiance picture */
195 >        colrscanf_t *getscan
196 > )
197   {
198          COLR    *scanout;
199          int     y;
# Line 212 | Line 212 | int  (*getscan)();
212                          quiterr("error writing Radiance picture");
213          }
214                                                  /* free scanline */
215 <        free((char *)scanout);
215 >        free((void *)scanout);
216   }
217  
218  
219 < ra2ppm(binary, grey)    /* convert Radiance picture to Pixmap */
220 < int  binary, grey;
219 > static void
220 > ra2ppm( /* convert Radiance picture to 1-byte/sample Pixmap */
221 >        int  binary,
222 >        int  grey
223 > )
224   {
225          COLR    *scanin;
226          register int    x;
# Line 258 | Line 261 | int  binary, grey;
261                          quiterr("error writing Pixmap");
262          }
263                                                  /* free scanline */
264 <        free((char *)scanin);
264 >        free((void *)scanin);
265   }
266  
267  
268 < ppm2ra2(getscan)        /* convert 2-byte Pixmap to Radiance picture */
269 < int  (*getscan)();
268 > static void
269 > ppm2ra2(        /* convert 2-byte Pixmap to Radiance picture */
270 >        colorscanf_t *getscan
271 > )
272   {
273          COLOR   *scanout;
274          double  mult;
# Line 279 | Line 284 | int  (*getscan)();
284          for (y = ymax-1; y >= 0; y--) {
285                  if ((*getscan)(scanout, xmax, stdin) < 0)
286                          quiterr("error reading Pixmap");
287 <                for (x = gamcor>1.01|gamcor<0.99?xmax:0; x--; ) {
287 >                for (x = (gamcor>1.01)|(gamcor<0.99)?xmax:0; x--; ) {
288                          colval(scanout[x],RED) =
289                                          pow(colval(scanout[x],RED), gamcor);
290                          colval(scanout[x],GRN) =
# Line 293 | Line 298 | int  (*getscan)();
298                          quiterr("error writing Radiance picture");
299          }
300                                                  /* free scanline */
301 <        free((char *)scanout);
301 >        free((void *)scanout);
302   }
303  
304  
305 < ra2ppm2(binary, grey)   /* convert Radiance picture to Pixmap (2-byte) */
306 < int  binary, grey;
305 > static void
306 > ra2ppm2(        /* convert Radiance picture to Pixmap (2-byte) */
307 >        int  binary,
308 >        int  grey
309 > )
310   {
311          COLOR   *scanin;
312          double  mult, d;
# Line 319 | Line 327 | int  binary, grey;
327                  for (x = grey?xmax:0; x--; )
328                          colval(scanin[x],GRN) = bright(scanin[x]);
329                  d = 1./gamcor;
330 <                for (x = d>1.01|d<0.99?xmax:0; x--; ) {
330 >                for (x = (d>1.01)|(d<0.99)?xmax:0; x--; ) {
331                          colval(scanin[x],GRN) = pow(colval(scanin[x],GRN), d);
332                          if (!grey) {
333                                  colval(scanin[x],RED) =
# Line 357 | Line 365 | int  binary, grey;
365                          quiterr("error writing Pixmap");
366          }
367                                                  /* free scanline */
368 <        free((char *)scanin);
368 >        free((void *)scanin);
369   }
370  
371  
372 < agryscan(scan, len, fp)                 /* get an ASCII greyscale scanline */
373 < register COLR  *scan;
374 < register int  len;
375 < FILE  *fp;
372 > static int
373 > agryscan(                       /* get an ASCII greyscale scanline */
374 >        register COLR  *scan,
375 >        register int  len,
376 >        FILE  *fp
377 > )
378   {
379          while (len-- > 0) {
380                  scan[0][RED] =
# Line 376 | Line 386 | FILE  *fp;
386   }
387  
388  
389 < bgryscan(scan, len, fp)                 /* get a binary greyscale scanline */
390 < register COLR  *scan;
391 < int  len;
392 < register FILE  *fp;
389 > static int
390 > bgryscan(                       /* get a binary greyscale scanline */
391 >        register COLR  *scan,
392 >        int  len,
393 >        register FILE  *fp
394 > )
395   {
396          register int  c;
397  
# Line 397 | Line 409 | register FILE  *fp;
409   }
410  
411  
412 < aclrscan(scan, len, fp)                 /* get an ASCII color scanline */
413 < register COLR  *scan;
414 < register int  len;
415 < FILE  *fp;
412 > static int
413 > aclrscan(                       /* get an ASCII color scanline */
414 >        register COLR  *scan,
415 >        register int  len,
416 >        FILE  *fp
417 > )
418   {
419          while (len-- > 0) {
420                  scan[0][RED] = normval(scanint(fp));
# Line 412 | Line 426 | FILE  *fp;
426   }
427  
428  
429 < bclrscan(scan, len, fp)                 /* get a binary color scanline */
430 < register COLR  *scan;
431 < int  len;
432 < register FILE  *fp;
429 > static int
430 > bclrscan(                       /* get a binary color scanline */
431 >        register COLR  *scan,
432 >        int  len,
433 >        register FILE  *fp
434 > )
435   {
436          int  r, g, b;
437  
# Line 439 | Line 455 | register FILE  *fp;
455   }
456  
457  
458 < agryscan2(scan, len, fp)                /* get an ASCII greyscale scanline */
459 < register COLOR  *scan;
460 < register int  len;
461 < FILE  *fp;
458 > static int
459 > agryscan2(              /* get an ASCII greyscale scanline */
460 >        register COLOR  *scan,
461 >        register int  len,
462 >        FILE  *fp
463 > )
464   {
465          while (len-- > 0) {
466                  colval(scan[0],RED) =
# Line 454 | Line 472 | FILE  *fp;
472   }
473  
474  
475 < bgryscan2(scan, len, fp)                /* get a binary greyscale scanline */
476 < register COLOR  *scan;
477 < int  len;
478 < register FILE  *fp;
475 > static int
476 > bgryscan2(              /* get a binary greyscale scanline */
477 >        register COLOR  *scan,
478 >        int  len,
479 >        register FILE  *fp
480 > )
481   {
482          register int  c;
483  
# Line 473 | Line 493 | register FILE  *fp;
493   }
494  
495  
496 < aclrscan2(scan, len, fp)                /* get an ASCII color scanline */
497 < register COLOR  *scan;
498 < register int  len;
499 < FILE  *fp;
496 > static int
497 > aclrscan2(              /* get an ASCII color scanline */
498 >        register COLOR  *scan,
499 >        register int  len,
500 >        FILE  *fp
501 > )
502   {
503          while (len-- > 0) {
504                  colval(scan[0],RED) = fltv(scanint(fp));
# Line 488 | Line 510 | FILE  *fp;
510   }
511  
512  
513 < bclrscan2(scan, len, fp)                /* get a binary color scanline */
514 < register COLOR  *scan;
515 < int  len;
516 < register FILE  *fp;
513 > static int
514 > bclrscan2(              /* get a binary color scanline */
515 >        register COLOR  *scan,
516 >        int  len,
517 >        register FILE  *fp
518 > )
519   {
520          int  r, g, b;
521  
# Line 509 | Line 533 | register FILE  *fp;
533   }
534  
535  
536 < unsigned int
537 < scanint(fp)                     /* scan the next positive integer value */
538 < register FILE  *fp;
536 > static unsigned int
537 > scanint(                        /* scan the next positive integer value */
538 >        register FILE  *fp
539 > )
540   {
541          register int  c;
542          register unsigned int  i;
# Line 537 | Line 562 | tryagain:
562   }
563  
564  
565 < int
566 < normval(v)                      /* normalize a value to [0,255] */
567 < register unsigned int  v;
565 > static int
566 > normval(                        /* normalize a value to [0,255] */
567 >        register unsigned int  v
568 > )
569   {
570          if (v >= maxval)
571                  return(255);
# Line 549 | Line 575 | register unsigned int  v;
575   }
576  
577  
578 < unsigned int
579 < getby2(fp)                      /* return 2-byte quantity from fp */
580 < register FILE   *fp;
578 > static unsigned int
579 > getby2(                 /* return 2-byte quantity from fp */
580 >        register FILE   *fp
581 > )
582   {
583          register int    lowerb, upperb;
584  
558        lowerb = getc(fp);
585          upperb = getc(fp);
586 <        if (upperb == EOF)
586 >        lowerb = getc(fp);
587 >        if (lowerb == EOF)
588                  return(EOF);
589          return(upperb<<8 | lowerb);
590   }
591  
592  
593 < putby2(w, fp)                   /* put 2-byte quantity to fp */
594 < register unsigned int   w;
595 < register FILE   *fp;
593 > static void
594 > putby2(                 /* put 2-byte quantity to fp */
595 >        register unsigned int   w,
596 >        register FILE   *fp
597 > )
598   {
570        putc(w & 0xff, fp);
599          putc(w>>8 & 0xff, fp);
600 +        putc(w & 0xff, fp);
601          if (ferror(fp)) {
602                  fprintf(stderr, "%s: write error on PPM output\n", progname);
603                  exit(1);
# Line 576 | Line 605 | register FILE  *fp;
605   }
606  
607  
608 < unsigned int
609 < intv(v)                         /* return integer quantity for v */
610 < register double v;
608 > static unsigned int
609 > intv(                           /* return integer quantity for v */
610 >        register double v
611 > )
612   {
613          if (v >= 0.99999)
614                  return(maxval);

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines