void writepict( /* write the picture to a file */ char *s ) { static char buf[128]; char *fname; FILE *fp; COLR *scanline; int y; /* XXX relies on words.c 2.11 behavior */ if (nextword(buf, sizeof(buf), s) == NULL && !buf[0]) { error(COMMAND, "no file"); return; } if ((fname = getpath(buf, NULL, 0)) == NULL || (fp = fopen(fname, "w")) == NULL) { sprintf(errmsg, "cannot open \"%s\"", buf); error(COMMAND, errmsg); return; } SET_FILE_BINARY(fp); (*dev->comout)("writing \""); (*dev->comout)(fname); (*dev->comout)("\"...\n"); /* write header */ newheader("RADIANCE", fp); fputs(progname, fp); fprintview(&ourview, fp); if (octname != NULL) fprintf(fp, " %s\n", octname); else putc('\n', fp); fprintf(fp, "SOFTWARE= %s\n", VersionID); fputnow(fp); if (exposure != 1.0) fputexpos(exposure, fp); if (dev->pixaspect != 1.0) fputaspect(dev->pixaspect, fp); fputformat(COLRFMT, fp); putc('\n', fp); fprtresolu(hresolu, vresolu, fp); scanline = (COLR *)malloc(hresolu*sizeof(COLR)); if (scanline == NULL) { error(COMMAND, "not enough memory!"); fclose(fp); unlink(fname); return; } for (y = vresolu-1; y >= 0; y--) { getpictcolrs(y, scanline, &ptrunk, hresolu, vresolu); if (fwritecolrs(scanline, hresolu, fp) < 0) break; } free((void *)scanline); if (fclose(fp) < 0) error(COMMAND, "write error"); }
static void mapimage(void) /* map picture and send to stdout */ { COLOR *scan; comphist(); /* generate adaptation histogram */ check2do(); /* modify what2do flags */ if (what2do&DO_VEIL) compveil(); /* compute veil image */ if (!(what2do&DO_LINEAR)) if (mkbrmap() < 0) /* make tone map */ what2do |= DO_LINEAR; /* failed! -- use linear */ #if ADJ_VEIL else if (what2do&DO_VEIL) adjveil(); /* else adjust veil image */ #endif if (what2do&DO_LINEAR) { if (scalef <= FTINY) { if (what2do&DO_HSENS) scalef = htcontrs(Lb(0.5*(Bldmax+Bldmin))) / htcontrs(Lb(bwavg)); else scalef = Lb(0.5*(Bldmax+Bldmin)) / Lb(bwavg); scalef *= WHTEFFICACY/(inpexp*ldmax); } fputexpos(inpexp*scalef, stdout); /* record exposure */ if (lumf == cielum) scalef /= WHTEFFICACY; } fputformat(COLRFMT, stdout); /* complete header */ putchar('\n'); fputsresolu(&inpres, stdout); /* resolution doesn't change */ /* condition our image */ for (scan = firstscan(); scan != NULL; scan = nextscan()) if (fwritescan(scan, scanlen(&inpres), stdout) < 0) { fprintf(stderr, "%s: scanline write error\n", progname); exit(1); } }
int main( int argc, char **argv ) { double d, expval = 1.0; int i; progname = argv[0]; mybright = &rgb_bright; /* default */ for (i = 1; i < argc; i++) if (argv[i][0] == '-' || argv[i][0] == '+') switch (argv[i][1]) { case 'h': /* header */ header = argv[i][0] == '+'; break; case 'H': /* resolution string */ resolution = argv[i][0] == '+'; break; case 's': /* skip bytes in header */ skipbytes = atol(argv[++i]); break; case 'u': /* unique values */ uniq = argv[i][0] == '-'; break; case 'o': /* original values */ original = argv[i][0] == '-'; break; case 'g': /* gamma correction */ gamcor = atof(argv[i+1]); if (argv[i][0] == '+') gamcor = 1.0/gamcor; i++; break; case 'e': /* exposure correction */ d = atof(argv[i+1]); if (argv[i+1][0] == '-' || argv[i+1][0] == '+') d = pow(2.0, d); if (argv[i][0] == '-') expval *= d; scalecolor(exposure, d); doexposure++; i++; break; case 'R': /* reverse byte sequence */ if (argv[i][0] == '-') { ord[0]=BLU; ord[1]=GRN; ord[2]=RED; } else { ord[0]=RED; ord[1]=GRN; ord[2]=BLU; } break; case 'r': /* reverse conversion */ reverse = argv[i][0] == '-'; break; case 'n': /* non-interleaved RGB */ interleave = argv[i][0] == '+'; break; case 'b': /* brightness values */ putprim = argv[i][0] == '-' ? BRIGHT : ALL; break; case 'p': /* primary controls */ switch (argv[i][2]) { /* these two options affect -r conversion */ case '\0': myprims[RED][CIEX] = atof(argv[++i]); myprims[RED][CIEY] = atof(argv[++i]); myprims[GRN][CIEX] = atof(argv[++i]); myprims[GRN][CIEY] = atof(argv[++i]); myprims[BLU][CIEX] = atof(argv[++i]); myprims[BLU][CIEY] = atof(argv[++i]); myprims[WHT][CIEX] = atof(argv[++i]); myprims[WHT][CIEY] = atof(argv[++i]); outprims = myprims; break; case 'x': case 'X': outprims = NULL; break; /* the following options affect +r only */ case 'r': case 'R': putprim = RED; break; case 'g': case 'G': putprim = GRN; break; case 'b': case 'B': putprim = BLU; break; default: goto unkopt; } break; case 'd': /* data only (no indices) */ dataonly = argv[i][0] == '-'; switch (argv[i][2]) { case '\0': case 'a': /* ascii */ format = 'a'; fmtid = "ascii"; break; case 'i': /* integer */ format = 'i'; fmtid = "ascii"; break; case 'b': /* byte */ dataonly = 1; format = 'b'; fmtid = "byte"; break; case 'W': /* 16-bit swapped */ swapbytes = 1; case 'w': /* 16-bit */ dataonly = 1; format = 'w'; fmtid = "16-bit"; break; case 'F': /* swapped floats */ swapbytes = 1; case 'f': /* float */ dataonly = 1; format = 'f'; fmtid = "float"; break; case 'D': /* swapped doubles */ swapbytes = 1; case 'd': /* double */ dataonly = 1; format = 'd'; fmtid = "double"; break; default: goto unkopt; } break; case 'x': /* x resolution */ case 'X': /* x resolution */ resolution = 0; if (argv[i][0] == '-') picres.rt |= XDECR; picres.xr = atoi(argv[++i]); break; case 'y': /* y resolution */ case 'Y': /* y resolution */ resolution = 0; if (argv[i][0] == '-') picres.rt |= YDECR; if (picres.xr == 0) picres.rt |= YMAJOR; picres.yr = atoi(argv[++i]); break; default: unkopt: fprintf(stderr, "%s: unknown option: %s\n", progname, argv[i]); quit(1); break; } else break; /* recognize special formats */ if (dataonly && format == 'b') { if (putprim == ALL) fmtid = "24-bit_rgb"; else fmtid = "8-bit_grey"; } if (dataonly && format == 'w') { if (putprim == ALL) fmtid = "48-bit_rgb"; else fmtid = "16-bit_grey"; } /* assign reverse ordering */ rord[ord[0]] = 0; rord[ord[1]] = 1; rord[ord[2]] = 2; /* get input */ if (i == argc) { fin = stdin; } else if (i < argc) { if ((fin = fopen(argv[i], "r")) == NULL) { fprintf(stderr, "%s: can't open file \"%s\"\n", progname, argv[i]); quit(1); } if (reverse && putprim != BRIGHT && i == argc-3) { if ((fin2 = fopen(argv[i+1], "r")) == NULL) { fprintf(stderr, "%s: can't open file \"%s\"\n", progname, argv[i+1]); quit(1); } if ((fin3 = fopen(argv[i+2], "r")) == NULL) { fprintf(stderr, "%s: can't open file \"%s\"\n", progname, argv[i+2]); quit(1); } interleave = -1; } else if (i != argc-1) fin = NULL; if (reverse && putprim != BRIGHT && !interleave) { fin2 = fopen(argv[i], "r"); fin3 = fopen(argv[i], "r"); } if (skipbytes && (fseek(fin, skipbytes, 0) || (fin2 != NULL && (fseek(fin2, skipbytes, 0) || fseek(fin3, skipbytes, 0))))) { fprintf(stderr, "%s: cannot skip %ld bytes on input\n", progname, skipbytes); quit(1); } } if (fin == NULL) { fprintf(stderr, "%s: bad # file arguments\n", progname); quit(1); } if (reverse) { #ifdef _WIN32 SET_FILE_BINARY(stdout); if (format != 'a' && format != 'i') SET_FILE_BINARY(fin); #endif /* get header */ if (header) { if (checkheader(fin, fmtid, stdout) < 0) { fprintf(stderr, "%s: wrong input format\n", progname); quit(1); } if (fin2 != NULL) { getheader(fin2, NULL, NULL); getheader(fin3, NULL, NULL); } } else newheader("RADIANCE", stdout); /* get resolution */ if ((resolution && !fgetsresolu(&picres, fin)) || picres.xr <= 0 || picres.yr <= 0) { fprintf(stderr, "%s: missing resolution\n", progname); quit(1); } if (resolution && fin2 != NULL) { RESOLU pres2; if (!fgetsresolu(&pres2, fin2) || pres2.rt != picres.rt || pres2.xr != picres.xr || pres2.yr != picres.yr || !fgetsresolu(&pres2, fin3) || pres2.rt != picres.rt || pres2.xr != picres.xr || pres2.yr != picres.yr) { fprintf(stderr, "%s: resolution mismatch\n", progname); quit(1); } } /* add to header */ printargs(i, argv, stdout); if (expval < .99 || expval > 1.01) fputexpos(expval, stdout); if (outprims != NULL) { if (outprims != stdprims) fputprims(outprims, stdout); fputformat(COLRFMT, stdout); } else /* XYZ data */ fputformat(CIEFMT, stdout); putchar('\n'); fputsresolu(&picres, stdout); /* always put resolution */ valtopix(); } else { #ifdef _WIN32 SET_FILE_BINARY(fin); if (format != 'a' && format != 'i') SET_FILE_BINARY(stdout); #endif /* get header */ getheader(fin, checkhead, NULL); if (wrongformat) { fprintf(stderr, "%s: input not a Radiance RGBE picture\n", progname); quit(1); } if (!fgetsresolu(&picres, fin)) { fprintf(stderr, "%s: missing resolution\n", progname); quit(1); } if (header) { printargs(i, argv, stdout); if (expval < .99 || expval > 1.01) fputexpos(expval, stdout); fputformat(fmtid, stdout); putchar('\n'); } if (resolution) /* put resolution */ fputsresolu(&picres, stdout); pixtoval(); } quit(0); return 0; /* pro forma return */ }
static int transfer( /* transfer a Radiance picture */ char *ospec ) { char oname[PATH_MAX]; FILE *fp; int order; int xmax, ymax; COLR *scanin; int y; /* get header info. */ if (!(y = loadheader(stdin))) return(0); if (y < 0 || (order = fgetresolu(&xmax, &ymax, stdin)) < 0) { fprintf(stderr, "%s: bad input format\n", progname); exit(1); } /* did we pass the target frame? */ if (findframe && findframe < frameno) return(0); /* allocate scanline */ scanin = (COLR *)tempbuffer(xmax*sizeof(COLR)); if (scanin == NULL) { perror(progname); exit(1); } /* skip frame? */ if (findframe > frameno) { for (y = ymax; y--; ) if (freadcolrs(scanin, xmax, stdin) < 0) { fprintf(stderr, "%s: error reading input picture\n", progname); exit(1); } return(1); } /* open output file/command */ if (ospec == NULL) { strcpy(oname, "<stdout>"); fp = stdout; } else { sprintf(oname, ospec, frameno); if (oname[0] == '!') { if ((fp = popen(oname+1, "w")) == NULL) { fprintf(stderr, "%s: cannot start \"%s\"\n", progname, oname); exit(1); } } else { if (!force && access(oname, 0) >= 0) { fprintf(stderr, "%s: output file \"%s\" exists\n", progname, oname); exit(1); } if ((fp = fopen(oname, "w")) == NULL) { fprintf(stderr, "%s: ", progname); perror(oname); exit(1); } } } SET_FILE_BINARY(fp); dumpheader(fp); /* put out header */ fputs(progname, fp); if (bradj) fprintf(fp, " -e %+d", bradj); if (!doflat) fputs(" -r", fp); fputc('\n', fp); if (bradj) fputexpos(pow(2.0, (double)bradj), fp); fputc('\n', fp); fputresolu(order, xmax, ymax, fp); /* transfer picture */ for (y = ymax; y--; ) { if (freadcolrs(scanin, xmax, stdin) < 0) { fprintf(stderr, "%s: error reading input picture\n", progname); exit(1); } if (bradj) shiftcolrs(scanin, xmax, bradj); if (doflat) putbinary((char *)scanin, sizeof(COLR), xmax, fp); else fwritecolrs(scanin, xmax, fp); if (ferror(fp)) { fprintf(stderr, "%s: error writing output to \"%s\"\n", progname, oname); exit(1); } } /* clean up */ if (oname[0] == '!') pclose(fp); else if (ospec != NULL) fclose(fp); return(1); }