static void initambfile( /* initialize ambient file */ int cre8 ) { extern char *progname, *octname; static char *mybuf = NULL; #ifdef F_SETLKW aflock(cre8 ? F_WRLCK : F_RDLCK); #endif SET_FILE_BINARY(ambfp); if (mybuf == NULL) mybuf = (char *)bmalloc(BUFSIZ+8); setbuf(ambfp, mybuf); if (cre8) { /* new file */ newheader("RADIANCE", ambfp); fprintf(ambfp, "%s -av %g %g %g -aw %d -ab %d -aa %g ", progname, colval(ambval,RED), colval(ambval,GRN), colval(ambval,BLU), ambvwt, ambounce, ambacc); fprintf(ambfp, "-ad %d -as %d -ar %d ", ambdiv, ambssamp, ambres); if (octname != NULL) fputs(octname, ambfp); fputc('\n', ambfp); fprintf(ambfp, "SOFTWARE= %s\n", VersionID); fputnow(ambfp); fputformat(AMBFMT, ambfp); fputc('\n', ambfp); putambmagic(ambfp); } else if (checkheader(ambfp, AMBFMT, NULL) < 0 || !hasambmagic(ambfp)) error(USER, "bad ambient file"); }
DWORD DLL_CALLCONV FreeImage_ZLibGUnzip(BYTE *target, DWORD target_size, BYTE *source, DWORD source_size) { DWORD src_len = source_size; DWORD dest_len = target_size; int zerr = Z_DATA_ERROR; if (src_len > 0) { z_stream stream; memset(&stream, 0, sizeof (stream)); if ((zerr = inflateInit2(&stream, -MAX_WBITS)) == Z_OK) { stream.next_in = source; stream.avail_in = source_size; stream.next_out = target; stream.avail_out = target_size; if ((zerr = checkheader(&stream)) == Z_OK) { zerr = inflate (&stream, Z_NO_FLUSH); dest_len = target_size - stream.avail_out; if (zerr == Z_OK || zerr == Z_STREAM_END) inflateEnd(&stream); } } } if (zerr != Z_OK && zerr != Z_STREAM_END) { FreeImage_OutputMessageProc(FIF_UNKNOWN, "Zlib error : %s", zError(zerr)); return 0; } return dest_len; }
/* Write header to the given output stream */ static void printheader(FILE *fout, const char *info) { extern char VersionID[]; /* copy octree header */ if (octname[0] == '!') { newheader("RADIANCE", fout); fputs(octname+1, fout); if (octname[strlen(octname)-1] != '\n') fputc('\n', fout); } else { FILE *fin = fopen(octname, (outfmt=='a') ? "r" : "rb"); if (fin == NULL) quit(1); checkheader(fin, OCTFMT, fout); fclose(fin); } printargs(gargc-1, gargv, fout); /* add our command */ fprintf(fout, "SOFTWARE= %s\n", VersionID); fputnow(fout); fputs("NCOMP=3\n", fout); /* always RGB */ if (info != NULL) /* add extra info if given */ fputs(info, fout); fputformat(formstr(outfmt), fout); fputc('\n', fout); /* empty line ends header */ }
static void doresize(const char *file, const char *sizespec) { enum { M_SET, M_PLUS, M_MINUS } mode; off_t oldsize, newsize; int fd; if (*sizespec == '+') { sizespec++; mode = M_PLUS; } else if (*sizespec == '-') { sizespec++; mode = M_MINUS; } else { mode = M_SET; } newsize = getsize(sizespec); fd = doopen(file, O_RDWR, 0); doflock(file, fd, LOCK_EX); checkheader(file, fd); oldsize = filesize(file, fd); oldsize -= HEADERSIZE; switch (mode) { case M_SET: break; case M_PLUS: if (oldsize + newsize < oldsize) { /* overflow */ fprintf(stderr, "+%s: Result too large\n", sizespec); exit(1); } newsize = oldsize + newsize; break; case M_MINUS: if (oldsize < newsize) { /* underflow */ fprintf(stderr, "-%s: Result too small\n", sizespec); exit(1); } newsize = oldsize - newsize; break; } checksize(newsize); dotruncate(file, fd, HEADERSIZE + newsize); doflock(file, fd, LOCK_UN); close(fd); }
int main( int argc, char *argv[] ) { int i; progname = argv[0]; for (i = 1; i < argc; i++) if (argv[i][0] == '-') switch (argv[i][1]) { case 't': /* threshold value */ thresh = atof(argv[++i]); break; default: goto userr; } else break; if (i < argc-2) goto userr; if (i <= argc-1 && freopen(argv[i], "r", stdin) == NULL) { fprintf(stderr, "%s: can't open input \"%s\"\n", progname, argv[i]); exit(1); } if (i == argc-2 && freopen(argv[i+1], "w", stdout) == NULL) { fprintf(stderr, "%s: can't open output \"%s\"\n", progname, argv[i+1]); exit(1); } /* assign threshold color */ setcolr(threshclr, thresh, thresh, thresh); /* get our header */ if (checkheader(stdin, COLRFMT, NULL) < 0 || fgetresolu(&xmax, &ymax, stdin) < 0) quiterr("bad picture format"); /* convert file */ ra2hex(); exit(0); userr: fprintf(stderr, "Usage: %s [-t thresh] [input [output]]\n", progname); exit(1); }
void HTTPHeader::in(Socket * sock, bool allowpersistent, bool honour_reloadconfig) { if (dirty) reset(); dirty = true; // the RFCs don't specify a max header line length so this should be // dynamic really. Pointed out (well reminded actually) by Daniel Robbins char buff[8192]; // setup a buffer to hold the incomming HTTP line String line; // temp store to hold the line after processing line = "----"; // so we get past the first while bool firsttime = true; bool discard = false; while (line.length() > 3 || discard) { // loop until the stream is // failed or we get to the end of the header (a line by itself) // get a line of header from the stream // on the first time round the loop, honour the reloadconfig flag if desired // - this lets us break when waiting for the next request on a pconn, but not // during receipt of a request in progress. (*sock).getLine(buff, 8192, timeout, firsttime ? honour_reloadconfig : false); // getline will throw an exception if there is an error which will // only be caught by HandleConnection() line = buff; // convert the line to a String // ignore crap left in buffer from old pconns (in particular, the IE "extra CRLF after POST" bug) discard = false; if (not (firsttime && line.length() <= 3)) header.push_back(line); // stick the line in the deque that holds the header else { discard = true; #ifdef DGDEBUG std::cout << "Discarding unwanted bytes at head of request (pconn closed or IE multipart POST bug)" << std::endl; #endif } firsttime = false; } header.pop_back(); // remove the final blank line of a header if (header.size() == 0) throw std::exception(); checkheader(allowpersistent); // sort out a few bits in the header }
static FILE * lblopen( /* open pipe to label generator */ char *s, int *xp, int *yp ) { char com[PATH_MAX]; FILE *fp; sprintf(com, "psign -s -.15 -a 2 -x %d -y %d '%.90s'", *xp, *yp, s); if ((fp = popen(com, "r")) == NULL) return(NULL); if (checkheader(fp, COLRFMT, NULL) < 0) goto err; if (fgetresolu(xp, yp, fp) < 0) goto err; return(fp); err: pclose(fp); return(NULL); }
static void doinfo(const char *file) { int fd; struct stat st; long long amt; fd = doopen(file, O_RDWR, 0); checkheader(file, fd); dofstat(file, fd, &st); amt = st.st_size - HEADERSIZE; printf("%s size %lld bytes (%lld sectors; %lldK; %lldM)\n", file, amt, amt / SECTORSIZE, amt / 1024, amt / (1024*1024)); amt = st.st_blocks * 512LL; printf("%s spaceused %lld bytes (%lld sectors; %lldK; %lldM)\n", file, amt, amt / SECTORSIZE, amt / 1024, amt / (1024*1024)); close(fd); }
int main( int argc, char **argv ) { int i; progname = argv[0]; for (i = 1; i < argc && argv[i][0] == '-'; i++) switch (argv[i][1]) { case 'd': /* debug output */ i++; if (badarg(argc-i, argv+i, "s")) goto userr; if ((debugfp = fopen(argv[i], "w")) == NULL) { perror(argv[i]); exit(1); } SET_FILE_BINARY(debugfp); newheader("RADIANCE", debugfp); /* start */ printargs(argc, argv, debugfp); /* header */ break; case 'p': /* picture position */ if (badarg(argc-i-1, argv+i+1, "iiiiiiii")) goto userr; bounds[0][0] = atoi(argv[++i]); bounds[0][1] = atoi(argv[++i]); bounds[1][0] = atoi(argv[++i]); bounds[1][1] = atoi(argv[++i]); bounds[2][0] = atoi(argv[++i]); bounds[2][1] = atoi(argv[++i]); bounds[3][0] = atoi(argv[++i]); bounds[3][1] = atoi(argv[++i]); scanning = 2; break; case 'P': /* pick position */ scanning = 3; break; case 'i': /* irradiance factor */ i++; if (badarg(argc-i, argv+i, "f")) goto userr; irrad = atof(argv[i]); break; case 'm': /* raw map output */ rawmap = 1; break; case 'c': /* color input */ scanning = 0; break; default: goto userr; } /* open files */ if (i < argc && freopen(argv[i], "r", stdin) == NULL) { perror(argv[i]); exit(1); } if (i+1 < argc && freopen(argv[i+1], "w", stdout) == NULL) { perror(argv[i+1]); exit(1); } if (scanning) { /* load input picture header */ SET_FILE_BINARY(stdin); if (checkheader(stdin, COLRFMT, NULL) < 0 || fgetresolu(&xmax, &ymax, stdin) < 0) { fprintf(stderr, "%s: bad input picture\n", progname); exit(1); } if (scanning == 3) { if (i >= argc) goto userr; pickchartpos(argv[i]); scanning = 2; } } else { /* else set default xmax and ymax */ xmax = 512; ymax = 2*512/3; } if (scanning != 2) { /* use default boundaries */ bounds[0][0] = bounds[2][0] = .029*xmax + .5; bounds[0][1] = bounds[1][1] = .956*ymax + .5; bounds[1][0] = bounds[3][0] = .971*xmax + .5; bounds[2][1] = bounds[3][1] = .056*ymax + .5; } init(); /* initialize */ if (scanning) /* get picture colors */ getpicture(); else getcolors(); compute(); /* compute color mapping */ if (rawmap) { /* print out raw correspondence */ register int j; printf("# Color correspondence produced by:\n#\t\t"); printargs(argc, argv, stdout); printf("#\tUsage: pcwarp %s uncorrected.hdr > corrected.hdr\n", i+1 < argc ? argv[i+1] : "{this_file}"); printf("#\t Or: pcond [options] -m %s orig.hdr > output.hdr\n", i+1 < argc ? argv[i+1] : "{this_file}"); for (j = 0; j < 24; j++) printf("%f %f %f %f %f %f\n", colval(inpRGB[j],RED), colval(inpRGB[j],GRN), colval(inpRGB[j],BLU), colval(mbRGB[j],RED), colval(mbRGB[j],GRN), colval(mbRGB[j],BLU)); if (scanning && debugfp != NULL) cwarp(); /* color warp for debugging */ } else { /* print color mapping */ /* print header */ printf("{\n\tColor correction file computed by:\n\t\t"); printargs(argc, argv, stdout); printf("\n\tUsage: pcomb -f %s uncorrected.hdr > corrected.hdr\n", i+1 < argc ? argv[i+1] : "{this_file}"); if (!scanning) printf("\t Or: pcond [options] -f %s orig.hdr > output.hdr\n", i+1 < argc ? argv[i+1] : "{this_file}"); printf("}\n"); putmapping(); /* put out color mapping */ } if (debugfp != NULL) { /* put out debug picture */ if (scanning) picdebug(); else clrdebug(); } exit(0); userr: fprintf(stderr, "Usage: %s [-d dbg.hdr][-P | -p xul yul xur yur xll yll xlr ylr][-i irrad][-m] input.hdr [output.{cal|cwp}]\n", progname); fprintf(stderr, " or: %s [-d dbg.hdr][-i irrad][-m] -c [xyY.dat [output.{cal|cwp}]]\n", progname); exit(1); return 1; /* pro forma return */ }
int readoct( /* read in octree file or stream */ char *inpspec, int load, CUBE *scene, char *ofn[] ) { char sbuf[512]; int nf; int i; long m; if (inpspec == NULL) { infn = "standard input"; infp = stdin; } else if (inpspec[0] == '!') { infn = inpspec; if ((infp = popen(inpspec+1, "r")) == NULL) { sprintf(errmsg, "cannot execute \"%s\"", inpspec); error(SYSTEM, errmsg); } } else { infn = inpspec; if ((infp = fopen(inpspec, "r")) == NULL) { sprintf(errmsg, "cannot open octree file \"%s\"", inpspec); error(SYSTEM, errmsg); } } SET_FILE_BINARY(infp); /* get header */ if (checkheader(infp, OCTFMT, load&IO_INFO ? stdout : (FILE *)NULL) < 0) octerror(USER, "not an octree"); /* check format */ if ((objsize = ogetint(2)-OCTMAGIC) <= 0 || objsize > MAXOBJSIZ || objsize > sizeof(long)) octerror(USER, "incompatible octree format"); /* get boundaries */ if (load & IO_BOUNDS) { for (i = 0; i < 3; i++) scene->cuorg[i] = atof(ogetstr(sbuf)); scene->cusize = atof(ogetstr(sbuf)); } else { for (i = 0; i < 4; i++) ogetstr(sbuf); } objorig = nobjects; /* set object offset */ nf = 0; /* get object files */ while (*ogetstr(sbuf)) { if (load & IO_SCENE) readobj(sbuf); if (load & IO_FILES) ofn[nf] = savqstr(sbuf); nf++; } if (load & IO_FILES) ofn[nf] = NULL; /* get number of objects */ fnobjects = m = ogetint(objsize); if (fnobjects != m) octerror(USER, "too many objects"); if (load & IO_TREE) /* get the octree */ scene->cutree = gettree(); else if (load & IO_SCENE && nf == 0) skiptree(); if (load & IO_SCENE) { /* get the scene */ if (nf == 0) { /* load binary scene data */ readscene(infp, objsize); } else { /* consistency checks */ /* check object count */ if (nobjects != objorig+fnobjects) octerror(USER, "bad object count; octree stale?"); /* check for non-surfaces */ if (nonsurfintree(scene->cutree)) octerror(USER, "modifier in tree; octree stale?"); } } /* close the input */ if (infn[0] == '!') pclose(infp); else fclose(infp); return(nf); }
int main( int argc, char *argv[] ) { char inpbuf[2]; int gryflag = 0; int binflag = 1; int reverse = 0; int ptype; int i; progname = argv[0]; for (i = 1; i < argc; i++) if (argv[i][0] == '-') switch (argv[i][1]) { case 's': maxval = atoi(argv[++i]) & 0xffff; break; case 'b': gryflag = 1; break; case 'g': gamcor = atof(argv[++i]); break; case 'e': if (argv[i+1][0] != '+' && argv[i+1][0] != '-') goto userr; bradj = atoi(argv[++i]); break; case 'a': binflag = 0; break; case 'r': reverse = !reverse; break; default: goto userr; } else break; if (i < argc-2) goto userr; if (i <= argc-1 && freopen(argv[i], "r", stdin) == NULL) { fprintf(stderr, "%s: can't open input \"%s\"\n", progname, argv[i]); exit(1); } if (i == argc-2 && freopen(argv[i+1], "w", stdout) == NULL) { fprintf(stderr, "%s: can't open output \"%s\"\n", progname, argv[i+1]); exit(1); } if (maxval < 256) setcolrgam(gamcor); if (reverse) { /* get header */ if (read(fileno(stdin), inpbuf, 2) != 2 || inpbuf[0] != 'P') quiterr("input not a Poskanzer Pixmap"); ptype = inpbuf[1]; #ifdef _WIN32 if (ptype > 4) SET_FILE_BINARY(stdin); SET_FILE_BINARY(stdout); #endif xmax = scanint(stdin); ymax = scanint(stdin); maxval = scanint(stdin); /* put header */ newheader("RADIANCE", stdout); printargs(i, argv, stdout); fputformat(COLRFMT, stdout); putchar('\n'); fprtresolu(xmax, ymax, stdout); /* convert file */ if (maxval >= 256) switch (ptype) { case '2': ppm2ra2(agryscan2); break; case '5': ppm2ra2(bgryscan2); break; case '3': ppm2ra2(aclrscan2); break; case '6': ppm2ra2(bclrscan2); break; default: quiterr("unsupported Pixmap type"); } else switch (ptype) { case '2': ppm2ra(agryscan); break; case '5': ppm2ra(bgryscan); break; case '3': ppm2ra(aclrscan); break; case '6': ppm2ra(bclrscan); break; default: quiterr("unsupported Pixmap type"); } } else { #ifdef _WIN32 SET_FILE_BINARY(stdin); if (binflag) SET_FILE_BINARY(stdout); #endif /* get header info. */ if (checkheader(stdin, COLRFMT, NULL) < 0 || fgetresolu(&xmax, &ymax, stdin) < 0) quiterr("bad picture format"); /* write PPM header */ printf("P%1d\n%d %d\n%u\n", (gryflag?2:3)+(binflag?3:0), xmax, ymax, maxval); /* convert file */ if (maxval >= 256) ra2ppm2(binflag, gryflag); else ra2ppm(binflag, gryflag); } exit(0); userr: fprintf(stderr, "Usage: %s [-r][-a][-b][-s maxv][-g gamma][-e +/-stops] [input [output]]\n", progname); exit(1); }
/* Recover output if possible */ void recover_output() { off_t lastout = -1; int outvsiz, recsiz; char *outvfmt; int i, j; MODCONT *mp; int ofl; char oname[1024]; LUENT *oent; STREAMOUT sout; off_t nvals; int xr, yr; switch (outfmt) { case 'a': error(USER, "cannot recover ASCII output"); return; case 'f': outvsiz = sizeof(float)*3; break; case 'd': outvsiz = sizeof(double)*3; break; case 'c': outvsiz = sizeof(COLR); break; default: error(INTERNAL, "botched output format"); return; } outvfmt = formstr(outfmt); /* check modifier outputs */ for (i = 0; i < nmods; i++) { mp = (MODCONT *)lu_find(&modconttab,modname[i])->data; if (mp->outspec == NULL) error(USER, "cannot recover from stdout"); if (mp->outspec[0] == '!') error(USER, "cannot recover from command"); for (j = 0; ; j++) { /* check each bin's file */ ofl = ofname(oname, mp->outspec, mp->modname, j); if (ofl < 0) error(USER, "bad output file specification"); oent = lu_find(&ofiletab, oname); if (oent->data != NULL) { sout = *(STREAMOUT *)oent->data; } else { sout.reclen = 0; sout.outpipe = 0; sout.ofp = NULL; } if (sout.ofp != NULL) { /* already open? */ if (ofl & OF_BIN) continue; break; } /* open output */ sout.ofp = fopen(oname, "rb+"); if (sout.ofp == NULL) { if (j == mp->nbins) break; /* assume end of modifier */ sprintf(errmsg, "missing recover file '%s'", oname); error(WARNING, errmsg); break; } nvals = lseek(fileno(sout.ofp), 0, SEEK_END); if (nvals <= 0) { lastout = 0; /* empty output, quit here */ fclose(sout.ofp); break; } if (!sout.reclen) { if (!(ofl & OF_BIN)) { sprintf(errmsg, "need -bn to recover file '%s'", oname); error(USER, errmsg); } recsiz = outvsiz; } else recsiz = outvsiz * sout.reclen; lseek(fileno(sout.ofp), 0, SEEK_SET); if (header && checkheader(sout.ofp, outvfmt, NULL) != 1) { sprintf(errmsg, "format mismatch for '%s'", oname); error(USER, errmsg); } sout.xr = xres; sout.yr = yres; if ((sout.xr > 0) & (sout.yr > 0) && (!fscnresolu(&xr, &yr, sout.ofp) || (xr != sout.xr) | (yr != sout.yr))) { sprintf(errmsg, "resolution mismatch for '%s'", oname); error(USER, errmsg); } nvals = (nvals - (off_t)ftell(sout.ofp)) / recsiz; if ((lastout < 0) | (nvals < lastout)) lastout = nvals; if (oent->key == NULL) /* new entry */ oent->key = strcpy((char *) malloc(strlen(oname)+1), oname); if (oent->data == NULL) oent->data = (char *)malloc(sizeof(STREAMOUT)); *(STREAMOUT *)oent->data = sout; if (!(ofl & OF_BIN)) break; /* no bin separation */ } if (!lastout) { /* empty output */ error(WARNING, "no previous data to recover"); lu_done(&ofiletab); /* reclose all outputs */ return; } if (j > mp->nbins) { /* check modifier size */ sprintf(errmsg, "mismatched -bn setting for recovering '%s'", modname[i]); error(USER, errmsg); } } if (lastout < 0) { error(WARNING, "no output files to recover"); return; } if (raysleft && lastout >= raysleft/accumulate) { error(WARNING, "output appears to be complete"); /* XXX should read & discard input? */ quit(0); } /* seek on all files */ nvals = lastout * outvsiz; lu_doall(&ofiletab, &myseeko, &nvals); /* skip repeated input */ lastout *= accumulate; for (nvals = 0; nvals < lastout; nvals++) { FVECT vdummy; if (getvec(vdummy) < 0 || getvec(vdummy) < 0) error(USER, "unexpected EOF on input"); } lastray = lastdone = (RNUMBER)lastout; if (raysleft) raysleft -= lastray; }
/* Load previously accumulated values */ void reload_output() { int i, j; MODCONT *mp; int ofl; char oname[1024]; char *fmode = "rb"; char *outvfmt; LUENT *oent; int xr, yr; STREAMOUT sout; DCOLOR rgbv; if (outfmt == 'a') fmode = "r"; outvfmt = formstr(outfmt); /* reload modifier values */ for (i = 0; i < nmods; i++) { mp = (MODCONT *)lu_find(&modconttab,modname[i])->data; if (mp->outspec == NULL) error(USER, "cannot reload from stdout"); if (mp->outspec[0] == '!') error(USER, "cannot reload from command"); for (j = 0; ; j++) { /* load each modifier bin */ ofl = ofname(oname, mp->outspec, mp->modname, j); if (ofl < 0) error(USER, "bad output file specification"); oent = lu_find(&ofiletab, oname); if (oent->data != NULL) { sout = *(STREAMOUT *)oent->data; } else { sout.reclen = 0; sout.outpipe = 0; sout.xr = xres; sout.yr = yres; sout.ofp = NULL; } if (sout.ofp == NULL) { /* open output as input */ sout.ofp = fopen(oname, fmode); if (sout.ofp == NULL) { if (j == mp->nbins) break; /* assume end of modifier */ sprintf(errmsg, "missing reload file '%s'", oname); error(WARNING, errmsg); break; } #ifdef getc_unlocked flockfile(sout.ofp); #endif if (header && checkheader(sout.ofp, outvfmt, NULL) != 1) { sprintf(errmsg, "format mismatch for '%s'", oname); error(USER, errmsg); } if ((sout.xr > 0) & (sout.yr > 0) && (!fscnresolu(&xr, &yr, sout.ofp) || (xr != sout.xr) | (yr != sout.yr))) { sprintf(errmsg, "resolution mismatch for '%s'", oname); error(USER, errmsg); } } /* read in RGB value */ if (!get_contrib(rgbv, sout.ofp)) { if (!j) { fclose(sout.ofp); break; /* ignore empty file */ } if (j < mp->nbins) { sprintf(errmsg, "missing data in '%s'", oname); error(USER, errmsg); } break; } if (j >= mp->nbins) { /* check modifier size */ sprintf(errmsg, "mismatched -bn setting for reloading '%s'", modname[i]); error(USER, errmsg); } copycolor(mp->cbin[j], rgbv); if (oent->key == NULL) /* new file entry */ oent->key = strcpy((char *) malloc(strlen(oname)+1), oname); if (oent->data == NULL) oent->data = (char *)malloc(sizeof(STREAMOUT)); *(STREAMOUT *)oent->data = sout; } } lu_doall(&ofiletab, &myclose, NULL); /* close all files */ }
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 */ }
int main(int argc, char *argv[]) { int bitsperpix; int i; SET_DEFAULT_BINARY(); SET_FILE_BINARY(stdin); SET_FILE_BINARY(stdout); progname = argv[0]; samplefac = 0; for (i = 1; i < argc; i++) if (argv[i][0] == '-') switch (argv[i][1]) { case 'g': gamv = atof(argv[++i]); break; case 'b': greyscale = 1; break; case 'd': dither = !dither; break; case 'c': ncolors = atoi(argv[++i]); break; case 'e': if (argv[i+1][0] != '+' && argv[i+1][0] != '-') goto userr; bradj = atoi(argv[++i]); break; case 'n': samplefac = atoi(argv[++i]); break; default: goto userr; } else break; if (i < argc-2) goto userr; if (i <= argc-1 && freopen(argv[i], "r", stdin) == NULL) { fprintf(stderr, "%s: cannot open input \"%s\"\n", progname, argv[i]); exit(1); } if (i == argc-2 && freopen(argv[i+1], "w", stdout) == NULL) { fprintf(stderr, "%s: cannot open output \"%s\"\n", progname, argv[i+1]); exit(1); } if (checkheader(stdin, COLRFMT, NULL) < 0 || fgetresolu(&xmax, &ymax, stdin) < 0) { fprintf(stderr, "%s: bad picture format\n", progname); exit(1); } picstart = ftell(stdin); currow = -1; scanln = (COLR *)malloc(xmax*sizeof(COLR)); if (scanln == NULL) { fprintf(stderr, "%s: out of memory\n", progname); exit(1); } /* set up gamma correction */ setcolrgam(gamv); /* figure out the bits per pixel */ if ((ncolors < 2) | (ncolors > MAXCOLORS)) ncolors = MAXCOLORS; for (bitsperpix = 1; ncolors > 1<<bitsperpix; bitsperpix++) ; /* compute color map */ if (greyscale) mkgrymap(ncolors); else mkclrmap(ncolors); /* convert image */ GIFEncode(stdout, xmax, ymax, 0, 0, bitsperpix, rmap, gmap, bmap, getgifpix); exit(0); userr: fprintf(stderr, "Usage: %s [-b][-d][-n samp][-c ncolors][-g gamv][-e +/-stops] input [output]\n", progname); exit(1); }
int main( int argc, char *argv[] ) { static char picfmt[MAXFMTLEN] = PICFMT; int rval; FILE *fin; SET_DEFAULT_BINARY(); SET_FILE_BINARY(stdout); progname = argv[0]; while (argc > 2 && argv[1][0] == '-') { switch (argv[1][1]) { case 'c': correctorder = 1; break; case 'r': ccw = 1; break; default: goto userr; } argc--; argv++; } if (argc != 2 && argc != 3) goto userr; if ((fin = fopen(argv[1], "r")) == NULL) { fprintf(stderr, "%s: cannot open\n", argv[1]); exit(1); } if (argc == 3 && freopen(argv[2], "w", stdout) == NULL) { fprintf(stderr, "%s: cannot open\n", argv[2]); exit(1); } /* transfer header */ if ((rval = checkheader(fin, picfmt, stdout)) < 0) { fprintf(stderr, "%s: not a Radiance picture\n", progname); exit(1); } if (rval) fputformat(picfmt, stdout); /* add new header info. */ fputs(progname, stdout); if (ccw) fputs(" -r", stdout); if (correctorder) fputs(" -c", stdout); fputs("\n\n", stdout); /* get picture size */ if ((order = fgetresolu(&xres, &yres, fin)) < 0) { fprintf(stderr, "%s: bad picture size\n", progname); exit(1); } /* write new picture size */ fputresolu(neworder(), yres, xres, stdout); /* compute buffer capacity */ nrows = sizeof(buf)/sizeof(COLR)/yres; if (ccw) /* rotate the image */ rotateccw(fin); else rotatecw(fin); exit(0); userr: fprintf(stderr, "Usage: %s [-r][-c] infile [outfile]\n", progname); exit(1); }
/* Load previously accumulated values */ void reload_output() { int i, j; MODCONT *mp; int ofl; char oname[1024]; char *fmode = "rb"; char *outvfmt; LUENT *oent; int xr, yr; STREAMOUT *sop; DCOLOR rgbv; if (outfmt == 'a') fmode = "r"; outvfmt = formstr(outfmt); /* reload modifier values */ for (i = 0; i < nmods; i++) { mp = (MODCONT *)lu_find(&modconttab,modname[i])->data; if (mp->outspec == NULL) error(USER, "cannot reload from stdout"); if (mp->outspec[0] == '!') error(USER, "cannot reload from command"); for (j = 0; j < mp->nbins; j++) { /* load each modifier bin */ ofl = ofname(oname, mp->outspec, mp->modname, mp->bin0+j); if (ofl < 0) error(USER, "bad output file specification"); oent = lu_find(&ofiletab, oname); if (oent->data == NULL) error(INTERNAL, "unallocated stream in reload_output()"); sop = (STREAMOUT *)oent->data; if (sop->ofp == NULL) { /* open output as input */ sop->ofp = fopen(oname, fmode); if (sop->ofp == NULL) { sprintf(errmsg, "missing reload file '%s'", oname); error(WARNING, errmsg); break; } #ifdef getc_unlocked flockfile(sop->ofp); #endif if (header && checkheader(sop->ofp, outvfmt, NULL) != 1) { sprintf(errmsg, "format mismatch for '%s'", oname); error(USER, errmsg); } if ((sop->reclen == 1) & (sop->xr > 0) & (sop->yr > 0) && (!fscnresolu(&xr, &yr, sop->ofp) || (xr != sop->xr) | (yr != sop->yr))) { sprintf(errmsg, "resolution mismatch for '%s'", oname); error(USER, errmsg); } } /* read in RGB value */ if (!get_contrib(rgbv, sop->ofp)) { if (!j) { fclose(sop->ofp); break; /* ignore empty file */ } if (j < mp->nbins) { sprintf(errmsg, "missing data in '%s'", oname); error(USER, errmsg); } break; } copycolor(mp->cbin[j], rgbv); } } lu_doall(&ofiletab, &myclose, NULL); /* close all files */ }