Back to index

radiance  4R0+20100331
ra_hexbit.c
Go to the documentation of this file.
00001 #ifndef lint
00002 static const char    RCSid[] = "$Id: ra_hexbit.c,v 3.3 2004/03/28 20:33:14 schorsch Exp $";
00003 #endif
00004 /*
00005  * Create a 4x1 hex bitmap from a Radiance picture.
00006  */
00007 
00008 #include  <stdio.h>
00009 #include  <time.h>
00010 
00011 #include  "color.h"
00012 #include  "resolu.h"
00013 
00014 char  *progname;
00015 int  xmax, ymax;
00016 double thresh = 0.5;        /* threshold value */
00017 COLR   threshclr;
00018 
00019 #define abovethresh(c)      ((c)[EXP]>threshclr[EXP] || \
00020                      ((c)[EXP]==threshclr[EXP] && (c)[GRN]>threshclr[GRN]))
00021 
00022 static void quiterr(char *err);
00023 static void ra2hex(void);
00024 
00025 
00026 int
00027 main(
00028        int  argc,
00029        char  *argv[]
00030 )
00031 {
00032        int  i;
00033        
00034        progname = argv[0];
00035 
00036        for (i = 1; i < argc; i++)
00037               if (argv[i][0] == '-')
00038                      switch (argv[i][1]) {
00039                      case 't':            /* threshold value */
00040                             thresh = atof(argv[++i]);
00041                             break;
00042                      default:
00043                             goto userr;
00044                      }
00045               else
00046                      break;
00047 
00048        if (i < argc-2)
00049               goto userr;
00050        if (i <= argc-1 && freopen(argv[i], "r", stdin) == NULL) {
00051               fprintf(stderr, "%s: can't open input \"%s\"\n",
00052                             progname, argv[i]);
00053               exit(1);
00054        }
00055        if (i == argc-2 && freopen(argv[i+1], "w", stdout) == NULL) {
00056               fprintf(stderr, "%s: can't open output \"%s\"\n",
00057                             progname, argv[i+1]);
00058               exit(1);
00059        }
00060                             /* assign threshold color */
00061        setcolr(threshclr, thresh, thresh, thresh);
00062                             /* get our header */
00063        if (checkheader(stdin, COLRFMT, NULL) < 0 ||
00064                      fgetresolu(&xmax, &ymax, stdin) < 0)
00065               quiterr("bad picture format");
00066                             /* convert file */
00067        ra2hex();
00068        exit(0);
00069 userr:
00070        fprintf(stderr,
00071               "Usage: %s [-t thresh] [input [output]]\n", progname);
00072        exit(1);
00073 }
00074 
00075 
00076 static void
00077 quiterr(             /* print message and exit */
00078        char  *err
00079 )
00080 {
00081        if (err != NULL) {
00082               fprintf(stderr, "%s: %s\n", progname, err);
00083               exit(1);
00084        }
00085        exit(0);
00086 }
00087 
00088 
00089 static void
00090 ra2hex(void)         /* convert Radiance scanlines to 4x1 bit hex */
00091 {
00092        static char   cmap[] = "0123456789ABCDEF";
00093        COLR   *scanin;
00094        register int  x, c, t;
00095        int    y;
00096                                           /* allocate scanline */
00097        scanin = (COLR *)malloc(xmax*sizeof(COLR));
00098        if (scanin == NULL)
00099               quiterr("out of memory in ra2skel");
00100                                           /* convert image */
00101        for (y = ymax-1; y >= 0; y--) {
00102               if (freadcolrs(scanin, xmax, stdin) < 0)
00103                      quiterr("error reading Radiance picture");
00104               c = 0;
00105               for (x = 0; x < xmax; x++)
00106                      if ((t = 03 - (x&03)))
00107                             c |= abovethresh(scanin[x]) << t;
00108                      else {
00109                             c |= abovethresh(scanin[x]);
00110                             putchar(cmap[c]);
00111                             c = 0;
00112                      }
00113               if (t)
00114                      fputc(cmap[c], stdout);
00115               fputc('\n', stdout);
00116               if (ferror(stdout))
00117                      quiterr("error writing hex bit file");
00118        }
00119                                           /* free scanline */
00120        free((void *)scanin);
00121 }