static void ra2hex(void) /* convert Radiance scanlines to 4x1 bit hex */ { static char cmap[] = "0123456789ABCDEF"; COLR *scanin; register int x, c, t; int y; /* allocate scanline */ scanin = (COLR *)malloc(xmax*sizeof(COLR)); if (scanin == NULL) quiterr("out of memory in ra2skel"); /* convert image */ for (y = ymax-1; y >= 0; y--) { if (freadcolrs(scanin, xmax, stdin) < 0) quiterr("error reading Radiance picture"); c = 0; for (x = 0; x < xmax; x++) if ((t = 03 - (x&03))) c |= abovethresh(scanin[x]) << t; else { c |= abovethresh(scanin[x]); putchar(cmap[c]); c = 0; } if (t) fputc(cmap[c], stdout); fputc('\n', stdout); if (ferror(stdout)) quiterr("error writing hex bit file"); } /* free scanline */ free((void *)scanin); }
static unsigned int scanint( /* scan the next positive integer value */ register FILE *fp ) { register int c; register unsigned int i; tryagain: while (isspace(c = getc(fp))) ; if (c == EOF) quiterr("unexpected end of file"); if (c == '#') { /* comment */ while ((c = getc(fp)) != EOF && c != '\n') ; goto tryagain; } /* should be integer */ i = 0; do { if (!isdigit(c)) quiterr("error reading integer"); i = 10*i + c - '0'; c = getc(fp); } while (c != EOF && !isspace(c)); return(i); }
static void ra2ps(void) /* convert Radiance scanlines to 6-bit */ { register COLR *scanin; int y; /* allocate scanline */ scanin = (COLR *)malloc(xmax*sizeof(COLR)); if (scanin == NULL) quiterr("out of memory in ra2ps"); /* convert image */ for (y = ymax-1; y >= 0; y--) { if (freadcolrs(scanin, xmax, stdin) < 0) quiterr("error reading Radiance picture"); if (putprim == Cputprim || devgam != 1.) { if (bradj) /* adjust exposure */ shiftcolrs(scanin, xmax, bradj); colrs_gambs(scanin, xmax); /* gamma compression */ } else normcolrs(scanin, xmax, bradj); if (docolor) { (*putprim)(scanin, RED); (*putprim)(scanin, GRN); (*putprim)(scanin, BLU); } else (*putprim)(scanin, GRY); if (ferror(stdout)) quiterr("error writing PostScript file"); } putchar('\n'); /* free scanline */ free((void *)scanin); }
static void ppm2ra2( /* convert 2-byte Pixmap to Radiance picture */ colorscanf_t *getscan ) { COLOR *scanout; double mult; int y; register int x; /* allocate scanline */ scanout = (COLOR *)malloc(xmax*sizeof(COLOR)); if (scanout == NULL) quiterr("out of memory in ppm2ra2"); if (bradj) mult = pow(2., (double)bradj); /* convert image */ for (y = ymax-1; y >= 0; y--) { if ((*getscan)(scanout, xmax, stdin) < 0) quiterr("error reading Pixmap"); for (x = (gamcor>1.01)|(gamcor<0.99)?xmax:0; x--; ) { colval(scanout[x],RED) = pow(colval(scanout[x],RED), gamcor); colval(scanout[x],GRN) = pow(colval(scanout[x],GRN), gamcor); colval(scanout[x],BLU) = pow(colval(scanout[x],BLU), gamcor); } for (x = bradj?xmax:0; x--; ) scalecolor(scanout[x], mult); if (fwritescan(scanout, xmax, stdout) < 0) quiterr("error writing Radiance picture"); } /* free scanline */ free((void *)scanout); }
int main (int ac, char *av[]) { char *prog_name = av[0]; char *fn, *tn; struct stat fs, ts; while (++av, --ac) { if (strcmp(*av, "-silent") == 0) silent = 1; else if (strcmp(*av, "-ignorelinks") == 0) ignore_links = 1; else if (strcmp(*av, "-withrevinfo") == 0) with_revinfo = 1; else if (strcmp(*av, "--") == 0) { ++av, --ac; break; } else break; } if (ac < 1 || ac > 2) quit (1, "usage: %s [-silent] [-ignorelinks] fromdir [todir]", prog_name); fn = av[0]; if (ac == 2) tn = av[1]; else tn = "."; /* to directory */ if (stat (tn, &ts) < 0) quiterr (1, tn); #ifdef S_ISDIR if (!(S_ISDIR(ts.st_mode))) #else if (!(ts.st_mode & S_IFMT) == S_IFDIR) #endif quit (2, "%s: Not a directory", tn); if (chdir (tn) < 0) quiterr (1, tn); /* from directory */ if (stat (fn, &fs) < 0) quiterr (1, fn); #ifdef S_ISDIR if (!(S_ISDIR(fs.st_mode))) #else if (!(fs.st_mode & S_IFMT) == S_IFDIR) #endif quit (2, "%s: Not a directory", fn); exit (dodir (fn, &fs, &ts, 0)); }
static void ra2ppm( /* convert Radiance picture to 1-byte/sample Pixmap */ int binary, int grey ) { COLR *scanin; register int x; int y; /* allocate scanline */ scanin = (COLR *)malloc(xmax*sizeof(COLR)); if (scanin == NULL) quiterr("out of memory in ra2ppm"); /* convert image */ for (y = ymax-1; y >= 0; y--) { if (freadcolrs(scanin, xmax, stdin) < 0) quiterr("error reading Radiance picture"); if (bradj) shiftcolrs(scanin, xmax, bradj); for (x = grey?xmax:0; x--; ) scanin[x][GRN] = normbright(scanin[x]); colrs_gambs(scanin, xmax); if (grey) if (binary) for (x = 0; x < xmax; x++) putc(scanin[x][GRN], stdout); else for (x = 0; x < xmax; x++) printf("%d\n", scanin[x][GRN]); else if (binary) for (x = 0; x < xmax; x++) { putc(scanin[x][RED], stdout); putc(scanin[x][GRN], stdout); putc(scanin[x][BLU], stdout); } else for (x = 0; x < xmax; x++) printf("%d %d %d\n", scanin[x][RED], scanin[x][GRN], scanin[x][BLU]); if (ferror(stdout)) quiterr("error writing Pixmap"); } /* free scanline */ free((void *)scanin); }
static int getint2( /* get a 2-byte positive integer */ register FILE *fp ) { register int b1, b2; if ((b1 = getc(fp)) == EOF || (b2 = getc(fp)) == EOF) quiterr("read error"); return(b1 | b2<<8); }
static void ppm2ra( /* convert 1-byte Pixmap to Radiance picture */ colrscanf_t *getscan ) { COLR *scanout; int y; /* allocate scanline */ scanout = (COLR *)malloc(xmax*sizeof(COLR)); if (scanout == NULL) quiterr("out of memory in ppm2ra"); /* convert image */ for (y = ymax-1; y >= 0; y--) { if ((*getscan)(scanout, xmax, stdin) < 0) quiterr("error reading Pixmap"); gambs_colrs(scanout, xmax); if (bradj) shiftcolrs(scanout, xmax, bradj); if (fwritecolrs(scanout, xmax, stdout) < 0) quiterr("error writing Radiance picture"); } /* free scanline */ free((void *)scanout); }
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); }
/* Recursively create symbolic links from the current directory to the "from" directory. Assumes that files described by fs and ts are directories. */ static int dodir (char *fn, /* name of "from" directory, either absolute or relative to cwd */ struct stat *fs, struct stat *ts, /* stats for the "from" directory and cwd */ int rel) /* if true, prepend "../" to fn before using */ { DIR *df; struct dirent *dp; char buf[MAXPATHLEN + 1], *p; char symbuf[MAXPATHLEN + 1]; char basesym[MAXPATHLEN + 1]; struct stat sb, sc; int n_dirs; int symlen; int basesymlen = -1; char *ocurdir; if ((fs->st_dev == ts->st_dev) && (fs->st_ino == ts->st_ino)) { msg ("%s: From and to directories are identical!", fn); return 1; } if (rel) strcpy (buf, "../"); else buf[0] = '\0'; strcat (buf, fn); if (!(df = opendir (buf))) { msg ("%s: Cannot opendir", buf); return 1; } p = buf + strlen (buf); if (*(p - 1) != '/') *p++ = '/'; n_dirs = fs->st_nlink; while ((dp = readdir (df))) { if (dp->d_name[strlen(dp->d_name) - 1] == '~') continue; #ifdef __DARWIN__ /* Ignore these Mac OS X Finder data files */ if (!strcmp(dp->d_name, ".DS_Store") || !strcmp(dp->d_name, "._.DS_Store")) continue; #endif strcpy (p, dp->d_name); if (n_dirs > 0) { if (lstat (buf, &sb) < 0) { mperror (buf); continue; } #ifdef S_ISDIR if(S_ISDIR(sb.st_mode)) #else if ((sb.st_mode & S_IFMT) == S_IFDIR) #endif { /* directory */ n_dirs--; if (dp->d_name[0] == '.' && (dp->d_name[1] == '\0' || (dp->d_name[1] == '.' && dp->d_name[2] == '\0'))) continue; if (!with_revinfo) { if (!strcmp (dp->d_name, "BitKeeper")) continue; if (!strcmp (dp->d_name, "RCS")) continue; if (!strcmp (dp->d_name, "SCCS")) continue; if (!strcmp (dp->d_name, "CVS")) continue; if (!strcmp (dp->d_name, "CVS.adm")) continue; if (!strcmp (dp->d_name, ".svn")) continue; } ocurdir = rcurdir; rcurdir = buf; curdir = silent ? buf : (char *)0; if (!silent) printf ("%s:\n", buf); if ((stat (dp->d_name, &sc) < 0) && (errno == ENOENT)) { if (mkdir (dp->d_name, 0777) < 0 || stat (dp->d_name, &sc) < 0) { mperror (dp->d_name); curdir = rcurdir = ocurdir; continue; } } if (readlink (dp->d_name, symbuf, sizeof(symbuf) - 1) >= 0) { msg ("%s: is a link instead of a directory", dp->d_name); curdir = rcurdir = ocurdir; continue; } if (chdir (dp->d_name) < 0) { mperror (dp->d_name); curdir = rcurdir = ocurdir; continue; } dodir (buf, &sb, &sc, (buf[0] != '/')); if (chdir ("..") < 0) quiterr (1, ".."); curdir = rcurdir = ocurdir; continue; } } /* non-directory */ symlen = readlink (dp->d_name, symbuf, sizeof(symbuf) - 1); if (symlen >= 0) symbuf[symlen] = '\0'; /* The option to ignore links exists mostly because checking for them slows us down by 10-20%. But it is off by default because this really is a useful check. */ if (!ignore_links) { /* see if the file in the base tree was a symlink */ basesymlen = readlink(buf, basesym, sizeof(basesym) - 1); if (basesymlen >= 0) basesym[basesymlen] = '\0'; } if (symlen >= 0) { /* Link exists in new tree. Print message if it doesn't match. */ if (!equivalent (basesymlen>=0 ? basesym : buf, symbuf, basesymlen>=0 ? (char **) 0 : &p)) msg ("%s: %s", dp->d_name, symbuf); } else { char *sympath; if (basesymlen>=0) { if ((buf[0] == '.') && (buf[1] == '.') && (buf[2] == '/') && (basesym[0] == '.') && (basesym[1] == '.') && (basesym[2] == '/')) { /* It becomes very tricky here. We have ../../bar/foo symlinked to ../xxx/yyy. We can't just use ../xxx/yyy. We have to use ../../bar/foo/../xxx/yyy. */ int i; char *start, *end; strcpy (symbuf, buf); /* Find the first char after "../" in symbuf. */ start = symbuf; do { start += 3; } while ((start[0] == '.') && (start[1] == '.') && (start[2] == '/')); /* Then try to eliminate "../"s in basesym. */ i = 0; end = strrchr (symbuf, '/'); if (start < end) { do { i += 3; end--; while ((*end != '/') && (end != start)) end--; if (end == start) break; } while ((basesym[i] == '.') && (basesym[i + 1] == '.') && (basesym[i + 2] == '/')); } if (*end == '/') end++; strcpy (end, &basesym[i]); sympath = symbuf; } else sympath = basesym; } else sympath = buf; if (symlink (sympath, dp->d_name) < 0) mperror (dp->d_name); } } closedir (df); return 0; }
int main( int argc, char *argv[] ) { struct hdStruct head; int dither = 1; int reverse = 0; int ncolors = 256; int greyscale = 0; 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 'd': dither = !dither; break; case 'g': gamv = atof(argv[++i]); break; case 'r': reverse = !reverse; break; case 'b': greyscale = 1; break; case 'e': if (argv[i+1][0] != '+' && argv[i+1][0] != '-') goto userr; bradj = atoi(argv[++i]); break; case 'c': ncolors = 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) { sprintf(errmsg, "cannot open input \"%s\"", argv[i]); quiterr(errmsg); } if (i == argc-2 && freopen(argv[i+1], "w", stdout) == NULL) { sprintf(errmsg, "cannot open output \"%s\"", argv[i+1]); quiterr(errmsg); } if (reverse) { /* get header */ if (getthead(&head, NULL, stdin) < 0) quiterr("bad targa file"); if (!goodpic(&head)) quiterr("incompatible format"); xmax = head.x; ymax = head.y; /* put header */ newheader("RADIANCE", stdout); printargs(i, argv, stdout); fputformat(COLRFMT, stdout); putchar('\n'); fprtresolu(xmax, ymax, stdout); /* convert file */ tg2ra(&head); } else { if (getrhead(&head, stdin) < 0) quiterr("bad Radiance input"); /* write header */ putthead(&head, NULL, stdout); /* convert file */ if (greyscale) getgrey(ncolors); else getmapped(ncolors, dither); /* write data */ writetarga(&head, tarData, stdout); } quiterr(NULL); userr: fprintf(stderr, "Usage: %s [-d][-n samp][-c ncolors][-b][-g gamv][-e +/-stops] input [output]\n", progname); fprintf(stderr, " Or: %s -r [-g gamv][-e +/-stops] [input [output]]\n", progname); exit(1); }
int main(int argc, char *argv[]) { int i; double d; progname = argv[0]; for (i = 1; i < argc; i++) if (argv[i][0] == '-') switch (argv[i][1]) { case 'b': /* produce b&w PostScript */ docolor = 0; break; case 'c': /* produce color PostScript */ docolor = 1; break; case 'A': /* standard ASCII encoding */ putprim = Aputprim; break; case 'B': /* standard binary encoding */ putprim = Bputprim; break; case 'C': /* compressed ASCII encoding */ putprim = Cputprim; break; case 'd': /* print density */ dpi = atof(argv[++i]); break; case 'g': /* device gamma adjustment */ devgam = atof(argv[++i]); break; case 'p': /* paper size */ parsepaper(argv[++i]); break; case 'm': /* margin */ d = atof(argv[i+1]); d *= unit2inch(argv[i+1]); switch (argv[i][2]) { case '\0': hmarg = vmarg = d; break; case 'h': hmarg = d; break; case 'v': vmarg = d; break; default: goto userr; } i++; break; case 'e': /* exposure adjustment */ if (argv[i+1][0] != '+' && argv[i+1][0] != '-') goto userr; bradj = atoi(argv[++i]); break; case 'n': /* number of copies */ ncopies = 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: 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); } SET_FILE_BINARY(stdin); /* get our header */ getheader(stdin, headline, NULL); if (wrongformat || fgetresolu(&xmax, &ymax, stdin) < 0) quiterr("bad picture format"); /* gamma compression */ if (devgam <= 0.05) devgam = docolor ? DEFCGAM : DEFGGAM; if (putprim == Cputprim) setcolrgam(CODE6GAM); else if (devgam != 1.) setcolrgam(devgam); /* write header */ PSheader(argc, argv); /* convert file */ ra2ps(); /* write trailer */ PStrailer(); exit(0); userr: fprintf(stderr, "Usage: %s [-b|c][-A|B|C][-e +/-stops][-p paper][-m[h|v] margin][-d dpi][-g gamma] [input [output]]\n", progname); exit(1); }
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); }
static void ra2ppm2( /* convert Radiance picture to Pixmap (2-byte) */ int binary, int grey ) { COLOR *scanin; double mult, d; register int x; int y; /* allocate scanline */ scanin = (COLOR *)malloc(xmax*sizeof(COLOR)); if (scanin == NULL) quiterr("out of memory in ra2ppm2"); if (bradj) mult = pow(2., (double)bradj); /* convert image */ for (y = ymax-1; y >= 0; y--) { if (freadscan(scanin, xmax, stdin) < 0) quiterr("error reading Radiance picture"); for (x = bradj?xmax:0; x--; ) scalecolor(scanin[x], mult); for (x = grey?xmax:0; x--; ) colval(scanin[x],GRN) = bright(scanin[x]); d = 1./gamcor; for (x = (d>1.01)|(d<0.99)?xmax:0; x--; ) { colval(scanin[x],GRN) = pow(colval(scanin[x],GRN), d); if (!grey) { colval(scanin[x],RED) = pow(colval(scanin[x],RED), d); colval(scanin[x],BLU) = pow(colval(scanin[x],BLU), d); } } if (grey) if (binary) for (x = 0; x < xmax; x++) putby2(intv(colval(scanin[x],GRN)), stdout); else for (x = 0; x < xmax; x++) printf("%u\n", intv(colval(scanin[x],GRN))); else if (binary) for (x = 0; x < xmax; x++) { putby2(intv(colval(scanin[x],RED)), stdout); putby2(intv(colval(scanin[x],GRN)), stdout); putby2(intv(colval(scanin[x],BLU)), stdout); } else for (x = 0; x < xmax; x++) printf("%u %u %u\n", intv(colval(scanin[x],RED)), intv(colval(scanin[x],GRN)), intv(colval(scanin[x],BLU))); if (ferror(stdout)) quiterr("error writing Pixmap"); } /* free scanline */ free((void *)scanin); }