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.10 by schorsch, Thu Jun 5 19:29:34 2003 UTC vs.
Revision 2.18 by greg, Fri Jun 6 19:11:21 2025 UTC

# Line 5 | Line 5 | static const char      RCSid[] = "$Id$";
5   *  program to convert between RADIANCE and Poskanzer Pixmaps
6   */
7  
8 #include  <stdio.h>
8   #include  <math.h>
9   #include  <ctype.h>
10 < #include  <time.h>
12 <
10 > #include  "paths.h"
11   #include  "platform.h"
12 + #include  "rtio.h"
13   #include  "color.h"
14   #include  "resolu.h"
15  
17
18 int  agryscan(), bgryscan(), aclrscan(), bclrscan();
19 int  agryscan2(), bgryscan2(), aclrscan2(), bclrscan2();
20 int  normval();
21 unsigned int    scanint(), intv(), getby2();
22
16   #define fltv(i)         (((i)+.5)/(maxval+1.))
24
17   int  bradj = 0;                         /* brightness adjustment */
26
18   double  gamcor = 2.2;                   /* gamma correction value */
28
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 44 | 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 94 | 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 _WIN32
104 > #if defined(_WIN32) || defined(_WIN64)
105                  if (ptype > 4)
106                          SET_FILE_BINARY(stdin);
107                  SET_FILE_BINARY(stdout);
# Line 144 | Line 151 | char  *argv[];
151                                  quiterr("unsupported Pixmap type");
152                          }
153          } else {
154 < #ifdef _WIN32
154 > #if defined(_WIN32) || defined(_WIN64)
155                  SET_FILE_BINARY(stdin);
156                  if (binflag)
157                          SET_FILE_BINARY(stdout);
# Line 171 | 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 182 | 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 206 | Line 217 | int  (*getscan)();
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 252 | Line 266 | int  binary, grey;
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 269 | 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 287 | Line 303 | int  (*getscan)();
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 309 | 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 351 | Line 370 | int  binary, grey;
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 366 | 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 387 | 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 402 | 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 429 | 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 444 | 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 463 | 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 478 | 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 499 | 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 527 | 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 539 | 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  
548        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   {
560        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 566 | 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