| 1 | gwlarson | 3.1 | /* Copyright (c) 1998 Silicon Graphics, Inc. */ | 
| 2 |  |  |  | 
| 3 |  |  | #ifndef lint | 
| 4 |  |  | static char SCCSid[] = "$SunId$ SGI"; | 
| 5 |  |  | #endif | 
| 6 |  |  |  | 
| 7 |  |  | /* | 
| 8 |  |  | * Create a 4x1 hex bitmap from a Radiance picture. | 
| 9 |  |  | */ | 
| 10 |  |  |  | 
| 11 |  |  | #include  <stdio.h> | 
| 12 |  |  | #include  "color.h" | 
| 13 |  |  | #include  "resolu.h" | 
| 14 |  |  |  | 
| 15 |  |  | extern char  *malloc(); | 
| 16 |  |  |  | 
| 17 |  |  | char  *progname; | 
| 18 |  |  |  | 
| 19 |  |  | int  xmax, ymax; | 
| 20 |  |  |  | 
| 21 |  |  | double  thresh = 0.5;           /* threshold value */ | 
| 22 |  |  |  | 
| 23 |  |  | COLR    threshclr; | 
| 24 |  |  |  | 
| 25 |  |  | #define abovethresh(c)  ((c)[EXP]>threshclr[EXP] || \ | 
| 26 |  |  | ((c)[EXP]==threshclr[EXP] && (c)[GRN]>threshclr[GRN])) | 
| 27 |  |  |  | 
| 28 |  |  |  | 
| 29 |  |  | main(argc, argv) | 
| 30 |  |  | int  argc; | 
| 31 |  |  | char  *argv[]; | 
| 32 |  |  | { | 
| 33 |  |  | int  i; | 
| 34 |  |  |  | 
| 35 |  |  | progname = argv[0]; | 
| 36 |  |  |  | 
| 37 |  |  | for (i = 1; i < argc; i++) | 
| 38 |  |  | if (argv[i][0] == '-') | 
| 39 |  |  | switch (argv[i][1]) { | 
| 40 |  |  | case 't':               /* threshold value */ | 
| 41 |  |  | thresh = atof(argv[++i]); | 
| 42 |  |  | break; | 
| 43 |  |  | default: | 
| 44 |  |  | goto userr; | 
| 45 |  |  | } | 
| 46 |  |  | else | 
| 47 |  |  | break; | 
| 48 |  |  |  | 
| 49 |  |  | if (i < argc-2) | 
| 50 |  |  | goto userr; | 
| 51 |  |  | if (i <= argc-1 && freopen(argv[i], "r", stdin) == NULL) { | 
| 52 |  |  | fprintf(stderr, "%s: can't open input \"%s\"\n", | 
| 53 |  |  | progname, argv[i]); | 
| 54 |  |  | exit(1); | 
| 55 |  |  | } | 
| 56 |  |  | if (i == argc-2 && freopen(argv[i+1], "w", stdout) == NULL) { | 
| 57 |  |  | fprintf(stderr, "%s: can't open output \"%s\"\n", | 
| 58 |  |  | progname, argv[i+1]); | 
| 59 |  |  | exit(1); | 
| 60 |  |  | } | 
| 61 |  |  | /* assign threshold color */ | 
| 62 |  |  | setcolr(threshclr, thresh, thresh, thresh); | 
| 63 |  |  | /* get our header */ | 
| 64 |  |  | if (checkheader(stdin, COLRFMT, NULL) < 0 || | 
| 65 |  |  | fgetresolu(&xmax, &ymax, stdin) < 0) | 
| 66 |  |  | quiterr("bad picture format"); | 
| 67 |  |  | /* convert file */ | 
| 68 |  |  | ra2hex(); | 
| 69 |  |  | exit(0); | 
| 70 |  |  | userr: | 
| 71 |  |  | fprintf(stderr, | 
| 72 |  |  | "Usage: %s [-t thresh] [input [output]]\n", progname); | 
| 73 |  |  | exit(1); | 
| 74 |  |  | } | 
| 75 |  |  |  | 
| 76 |  |  |  | 
| 77 |  |  | quiterr(err)            /* print message and exit */ | 
| 78 |  |  | char  *err; | 
| 79 |  |  | { | 
| 80 |  |  | if (err != NULL) { | 
| 81 |  |  | fprintf(stderr, "%s: %s\n", progname, err); | 
| 82 |  |  | exit(1); | 
| 83 |  |  | } | 
| 84 |  |  | exit(0); | 
| 85 |  |  | } | 
| 86 |  |  |  | 
| 87 |  |  |  | 
| 88 |  |  | ra2hex()                /* convert Radiance scanlines to 4x1 bit hex */ | 
| 89 |  |  | { | 
| 90 |  |  | static char     cmap[] = "0123456789ABCDEF"; | 
| 91 |  |  | COLR    *scanin; | 
| 92 |  |  | register int    x, c, t; | 
| 93 |  |  | int     y; | 
| 94 |  |  | /* allocate scanline */ | 
| 95 |  |  | scanin = (COLR *)malloc(xmax*sizeof(COLR)); | 
| 96 |  |  | if (scanin == NULL) | 
| 97 |  |  | quiterr("out of memory in ra2skel"); | 
| 98 |  |  | /* convert image */ | 
| 99 |  |  | for (y = ymax-1; y >= 0; y--) { | 
| 100 |  |  | if (freadcolrs(scanin, xmax, stdin) < 0) | 
| 101 |  |  | quiterr("error reading Radiance picture"); | 
| 102 |  |  | c = 0; | 
| 103 |  |  | for (x = 0; x < xmax; x++) | 
| 104 |  |  | if ((t = 03 - (x&03))) | 
| 105 |  |  | c |= abovethresh(scanin[x]) << t; | 
| 106 |  |  | else { | 
| 107 |  |  | c |= abovethresh(scanin[x]); | 
| 108 |  |  | putchar(cmap[c]); | 
| 109 |  |  | c = 0; | 
| 110 |  |  | } | 
| 111 |  |  | if (t) | 
| 112 |  |  | fputc(cmap[c], stdout); | 
| 113 |  |  | fputc('\n', stdout); | 
| 114 |  |  | if (ferror(stdout)) | 
| 115 |  |  | quiterr("error writing hex bit file"); | 
| 116 |  |  | } | 
| 117 |  |  | /* free scanline */ | 
| 118 |  |  | free((char *)scanin); | 
| 119 |  |  | } |