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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines