Back to index

radiance  4R0+20100331
pcwarp.c
Go to the documentation of this file.
00001 #ifndef lint
00002 static const char    RCSid[] = "$Id: pcwarp.c,v 3.5 2008/11/10 19:08:19 greg Exp $";
00003 #endif
00004 /*
00005  * Warp colors in Radiance picture to correct for input/output changes.
00006  */
00007 
00008 #include <stdio.h>
00009 
00010 #include "resolu.h"
00011 #include "color.h"
00012 #include "warp3d.h"
00013 
00014 char   *progname;                  /* global argv[0] */
00015 
00016 FILE   *infp = NULL;               /* input stream */
00017 int    xres, yres;                 /* input picture resolution */
00018 
00019 WARP3D *cwarp;                            /* our warp map */
00020 int    iclip = CGAMUT_UPPER;              /* input value gamut clipping */
00021 int    oclip = CGAMUT_LOWER;              /* output value gamut clipping */
00022 
00023 static void syserror(char *s);
00024 static void picwarp(void);
00025 
00026 
00027 int
00028 main(
00029        int    argc,
00030        char   *argv[]
00031 )
00032 {
00033        static char   picfmt[LPICFMT+1] = PICFMT;
00034        int    cwflags = 0;
00035        int    rval;
00036        int    i;
00037 
00038        progname = argv[0];
00039        infp = stdin;
00040                                    /* get options */
00041        for (i = 1; i < argc && argv[i][0] == '-'; i++)
00042               switch (argv[i][1]) {
00043               case 'e':
00044                      cwflags = W3EXACT;
00045                      break;
00046               case 'f':
00047                      cwflags = W3FAST;
00048                      break;
00049               case 'i':
00050                      iclip = 0;
00051                      break;
00052               case 'o':
00053                      oclip = CGAMUT;
00054                      break;
00055               default:
00056                      goto userr;
00057               }
00058                                    /* load warp map */
00059        if (i >= argc)
00060               goto userr;
00061        if ((cwarp = load3dw(argv[i++], NULL)) == NULL)
00062               syserror("load3dw");
00063        set3dwfl(cwarp, cwflags);
00064                                    /* open input and output pictures */
00065        if (i < argc && (infp = fopen(argv[i], "r")) == NULL)
00066               syserror(argv[i]);
00067        if (i < argc-1 && freopen(argv[i+1], "w", stdout) == NULL)
00068               syserror(argv[i+1]);
00069                                    /* transfer header */
00070        if ((rval = checkheader(infp, picfmt, stdout)) < 0) {
00071               fprintf(stderr, "%s: input not a Radiance picture\n",
00072                             progname);
00073               exit(1);
00074        }
00075        if (rval)
00076               fputformat(picfmt, stdout);
00077                                    /* add new header info. */
00078        printargs(i, argv, stdout);
00079        putchar('\n');
00080                                    /* get picture size */
00081        if ((rval = fgetresolu(&xres, &yres, infp)) < 0) {
00082               fprintf(stderr, "%s: bad picture size\n", progname);
00083               exit(1);
00084        }
00085                                    /* new picture size the same */
00086        fputresolu(rval, xres, yres, stdout);
00087                                    /* warp those colors! */
00088        picwarp();
00089        exit(0);
00090 userr:
00091        fprintf(stderr,
00092               "Usage: %s [-i][-o][-e|-f] map.cwp [input.hdr [output.hdr]]\n",
00093                      progname);
00094        exit(1);
00095 }
00096 
00097 
00098 static void
00099 syserror(                   /* print system error and exit */
00100        char   *s
00101 )
00102 {
00103        fprintf(stderr, "%s: ", progname);
00104        perror(s);
00105        exit(2);
00106 }
00107 
00108 
00109 static void
00110 picwarp(void)               /* warp our picture scanlines */
00111 {
00112        register COLOR       *scan;
00113        long   ngamut = 0;
00114        int    rval;
00115        int    y;
00116        register int  x;
00117 
00118        scan = (COLOR *)malloc(xres*sizeof(COLOR));
00119        if (scan == NULL)
00120               syserror("picwarp");
00121        for (y = 0; y < yres; y++) {
00122               if (freadscan(scan, xres, infp) < 0) {
00123                      fprintf(stderr, "%s: error reading input picture\n",
00124                                    progname);
00125                      exit(1);
00126               }
00127               for (x = 0; x < xres; x++) {
00128                      if (iclip)
00129                             clipgamut(scan[x], bright(scan[x]), iclip,
00130                                           cblack, cwhite);
00131                      rval = warp3d(scan[x], scan[x], cwarp);
00132                      if (rval & W3ERROR)
00133                             syserror("warp3d");
00134                      if (rval & W3BADMAP) {
00135                             fprintf(stderr, "%s: singular color mapping\n",
00136                                           progname);
00137                             exit(1);
00138                      }
00139                      if (rval & W3GAMUT)
00140                             ngamut++;
00141                      if (oclip)
00142                             clipgamut(scan[x], bright(scan[x]), oclip,
00143                                           cblack, cwhite);
00144               }
00145               if (fwritescan(scan, xres, stdout) < 0) {
00146                      fprintf(stderr, "%s: error writing output picture\n",
00147                                    progname);
00148                      exit(1);
00149               }
00150        }
00151        if (ngamut >= (long)xres*yres/100)
00152               fprintf(stderr, "%s: warning - %ld%% of pixels out of gamut\n",
00153                             progname, 100*ngamut/((long)xres*yres));
00154        free((void *)scan);
00155 }