/* write out matrix to file (precede by resolution string if picture) */ int cm_write(const CMATRIX *cm, int dtype, FILE *fp) { static const char tabEOL[2] = {'\t','\n'}; const COLORV *mp = cm->cmem; int r, c; switch (dtype) { case DTascii: for (r = 0; r < cm->nrows; r++) for (c = 0; c < cm->ncols; c++, mp += 3) fprintf(fp, "%.6e %.6e %.6e%c", mp[0], mp[1], mp[2], tabEOL[c >= cm->ncols-1]); break; case DTfloat: case DTdouble: if (sizeof(COLOR) == cm_elem_size[dtype]) { r = cm->ncols*cm->nrows; while (r > 0) { c = putbinary(mp, sizeof(COLOR), r, fp); if (c <= 0) return(0); mp += 3*c; r -= c; } } else if (dtype == DTdouble) { double dc[3]; r = cm->ncols*cm->nrows; while (r--) { copycolor(dc, mp); if (putbinary(dc, sizeof(double), 3, fp) != 3) return(0); mp += 3; } } else /* dtype == DTfloat */ { float fc[3]; r = cm->ncols*cm->nrows; while (r--) { copycolor(fc, mp); if (putbinary(fc, sizeof(float), 3, fp) != 3) return(0); mp += 3; } } break; case DTrgbe: case DTxyze: fprtresolu(cm->ncols, cm->nrows, fp); for (r = 0; r < cm->nrows; r++, mp += 3*cm->ncols) if (fwritescan((COLOR *)mp, cm->ncols, fp) < 0) return(0); break; default: fputs("Unsupported data type in cm_write()!\n", stderr); return(0); } return(fflush(fp) == 0); }
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"); }
/* Write matrix to file type indicated by dtype */ int rmx_write(const RMATRIX *rm, int dtype, FILE *fp) { RMATRIX *mydm = NULL; int ok = 1; if ((rm == NULL) | (fp == NULL)) return(0); /* complete header */ if (rm->info) fputs(rm->info, fp); if (dtype == DTfromHeader) dtype = rm->dtype; else if ((dtype == DTrgbe) & (rm->dtype == DTxyze)) dtype = DTxyze; else if ((dtype == DTxyze) & (rm->dtype == DTrgbe)) dtype = DTrgbe; if ((dtype != DTrgbe) & (dtype != DTxyze)) { fprintf(fp, "NROWS=%d\n", rm->nrows); fprintf(fp, "NCOLS=%d\n", rm->ncols); fprintf(fp, "NCOMP=%d\n", rm->ncomp); } else if (rm->ncomp != 3) { /* wrong # components? */ double cmtx[3]; if (rm->ncomp != 1) /* only convert grayscale */ return(0); cmtx[0] = cmtx[1] = cmtx[2] = 1; mydm = rmx_transform(rm, 3, cmtx); if (mydm == NULL) return(0); rm = mydm; } fputformat((char *)cm_fmt_id[dtype], fp); fputc('\n', fp); switch (dtype) { /* write data */ case DTascii: ok = rmx_write_ascii(rm, fp); break; case DTfloat: ok = rmx_write_float(rm, fp); break; case DTdouble: ok = rmx_write_double(rm, fp); break; case DTrgbe: case DTxyze: fprtresolu(rm->ncols, rm->nrows, fp); ok = rmx_write_rgbe(rm, fp); break; default: return(0); } ok &= (fflush(fp) == 0); rmx_free(mydm); return(ok); }
static void picdebug(void) /* put out debugging picture */ { static COLOR blkcol = BLKCOLOR; COLOR *scan; int y, i; register int x, rg; if (fseek(stdin, 0L, 0) == EOF) { fprintf(stderr, "%s: cannot seek on input picture\n", progname); exit(1); } getheader(stdin, NULL, NULL); /* skip input header */ fgetresolu(&xmax, &ymax, stdin); /* allocate scanline */ scan = (COLOR *)malloc(xmax*sizeof(COLOR)); if (scan == NULL) { perror(progname); exit(1); } /* finish debug header */ fputformat(COLRFMT, debugfp); putc('\n', debugfp); fprtresolu(xmax, ymax, debugfp); /* write debug picture */ for (y = ymax-1; y >= 0; y--) { if (freadscan(scan, xmax, stdin) < 0) { fprintf(stderr, "%s: error rereading input picture\n", progname); exit(1); } for (x = 0; x < xmax; x++) { rg = chartndx(x, y, &i); if (rg == RG_CENT) { if (!(1L<<i & gmtflags) || (x+y)&07) { copycolor(scan[x], mbRGB[i]); clipgamut(scan[x], bright(scan[x]), CGAMUT, colmin, colmax); } else copycolor(scan[x], blkcol); } else if (rg == RG_CORR) cvtcolor(scan[x], scan[x]); else if (rg != RG_ORIG) copycolor(scan[x], blkcol); } if (fwritescan(scan, xmax, debugfp) < 0) { fprintf(stderr, "%s: error writing debugging picture\n", progname); exit(1); } } /* clean up */ fclose(debugfp); free((void *)scan); }
int main( int argc, char *argv[] ) { int ncolumns = 0; int autolabel = 0; int curcol = 0, x0 = 0, curx = 0, cury = 0, spacing = 0; int xsgn, ysgn; char *thislabel; int an; SET_DEFAULT_BINARY(); SET_FILE_BINARY(stdin); SET_FILE_BINARY(stdout); progname = argv[0]; for (an = 1; an < argc && argv[an][0] == '-'; an++) switch (argv[an][1]) { case 'h': echoheader = !echoheader; break; case 'x': xsiz = atoi(argv[++an]); break; case 'y': ysiz = atoi(argv[++an]); break; case 'b': setcolr(bgcolr, atof(argv[an+1]), atof(argv[an+2]), atof(argv[an+3])); an += 3; break; case 'a': ncolumns = atoi(argv[++an]); break; case 's': spacing = atoi(argv[++an]); break; case 'o': curx = x0 = atoi(argv[++an]); cury = atoi(argv[++an]); break; case 'l': switch (argv[an][2]) { case 'a': autolabel++; break; case 'h': labelht = atoi(argv[++an]); break; case '\0': goto dofiles; default: goto userr; } break; case '\0': case 't': goto dofiles; default: goto userr; } dofiles: newheader("RADIANCE", stdout); fputnow(stdout); for (nfile = 0; an < argc; nfile++) { if (nfile >= MAXFILE) goto toomany; thislabel = NULL; input[nfile].flags = 0; xsgn = ysgn = '-'; while (an < argc && (argv[an][0] == '-' || argv[an][0] == '+' || argv[an][0] == '=')) { switch (argv[an][1]) { case 't': checkthresh = 1; if (argv[an][0] == '-') { input[nfile].flags |= HASMIN; input[nfile].thmin = atof(argv[an+1]); } else if (argv[an][0] == '+') { input[nfile].flags |= HASMAX; input[nfile].thmax = atof(argv[an+1]); } else goto userr; an++; break; case 'l': if (strcmp(argv[an], "-l")) goto userr; thislabel = argv[++an]; break; case '+': case '-': case '0': if (argv[an][0] != '=') goto userr; xsgn = argv[an][1]; ysgn = argv[an][2]; if (ysgn != '+' && ysgn != '-' && ysgn != '0') goto userr; break; case '\0': if (argv[an][0] == '-') goto getfile; goto userr; default: goto userr; } an++; } getfile: if (argc-an < (ncolumns ? 1 : 3)) goto userr; if (autolabel && thislabel == NULL) thislabel = argv[an]; if (!strcmp(argv[an], "-")) { input[nfile].name = StandardInput; input[nfile].fp = stdin; } else { if (argv[an][0] == '!') { input[nfile].name = Command; input[nfile].fp = popen(argv[an]+1, "r"); } else { input[nfile].name = argv[an]; input[nfile].fp = fopen(argv[an], "r"); } if (input[nfile].fp == NULL) { perror(argv[an]); quit(1); } } an++; /* get header */ if (echoheader) printf("%s:\n", input[nfile].name); getheader(input[nfile].fp, headline, NULL); if (wrongformat) { fprintf(stderr, "%s: incompatible input format\n", input[nfile].name); quit(1); } /* get picture size */ if (fgetresolu(&input[nfile].xres, &input[nfile].yres, input[nfile].fp) < 0) { fprintf(stderr, "%s: bad picture size\n", input[nfile].name); quit(1); } if (ncolumns > 0) { if (curcol >= ncolumns) { cury = ymax + spacing; curx = x0; curcol = 0; } input[nfile].xloc = curx; input[nfile].yloc = cury; curx += input[nfile].xres + spacing; curcol++; } else { input[nfile].xloc = atoi(argv[an++]); if (xsgn == '+') input[nfile].xloc -= input[nfile].xres; else if (xsgn == '0') input[nfile].xloc -= input[nfile].xres/2; input[nfile].yloc = atoi(argv[an++]); if (ysgn == '+') input[nfile].yloc -= input[nfile].yres; else if (ysgn == '0') input[nfile].yloc -= input[nfile].yres/2; } if (input[nfile].xloc < xmin) xmin = input[nfile].xloc; if (input[nfile].yloc < ymin) ymin = input[nfile].yloc; if (input[nfile].xloc+input[nfile].xres > xmax) xmax = input[nfile].xloc+input[nfile].xres; if (input[nfile].yloc+input[nfile].yres > ymax) ymax = input[nfile].yloc+input[nfile].yres; if (thislabel != NULL) { if (++nfile >= MAXFILE) goto toomany; input[nfile].name = Label; input[nfile].flags = 0; input[nfile].xres = input[nfile-1].xres; input[nfile].yres = labelht; if ((input[nfile].fp = lblopen(thislabel, &input[nfile].xres, &input[nfile].yres)) == NULL) goto labelerr; input[nfile].xloc = input[nfile-1].xloc; input[nfile].yloc = input[nfile-1].yloc + input[nfile-1].yres-input[nfile].yres; } } if (xsiz <= 0) xsiz = xmax; else if (xsiz > xmax) xmax = xsiz; if (ysiz <= 0) ysiz = ymax; else if (ysiz > ymax) ymax = ysiz; /* add new header info. */ printargs(argc, argv, stdout); if (strcmp(ourfmt, PICFMT)) fputformat(ourfmt, stdout); /* print format if known */ putchar('\n'); fprtresolu(xsiz, ysiz, stdout); compos(); quit(0); userr: fprintf(stderr, "Usage: %s [-h][-x xr][-y yr][-b r g b][-a n][-s p][-o x0 y0][-la][-lh h] ", progname); fprintf(stderr, "[-t min1][+t max1][-l lab][=SS] pic1 x1 y1 ..\n"); quit(1); toomany: fprintf(stderr, "%s: only %d files and labels allowed\n", progname, MAXFILE); quit(1); labelerr: fprintf(stderr, "%s: error opening label\n", progname); quit(1); return 1; /* pro forma return */ }
/* Write resolution string to given output stream */ static void printresolu(FILE *fout, int xr, int yr) { if ((xr > 0) & (yr > 0)) /* resolution string */ fprtresolu(xr, yr, fout); }
static void clrdebug(void) /* put out debug picture from color input */ { static COLR blkclr = BLKCOLR; COLR mbclr[24], cvclr[24], orclr[24]; COLR *scan; COLOR ctmp, ct2; int y, i; register int x, rg; /* convert colors */ for (i = 0; i < 24; i++) { copycolor(ctmp, mbRGB[i]); clipgamut(ctmp, bright(ctmp), CGAMUT, cblack, cwhite); setcolr(mbclr[i], colval(ctmp,RED), colval(ctmp,GRN), colval(ctmp,BLU)); if (inpflags & 1L<<i) { copycolor(ctmp, inpRGB[i]); clipgamut(ctmp, bright(ctmp), CGAMUT, cblack, cwhite); setcolr(orclr[i], colval(ctmp,RED), colval(ctmp,GRN), colval(ctmp,BLU)); if (rawmap) copycolr(cvclr[i], mbclr[i]); else { bresp(ctmp, inpRGB[i]); colortrans(ct2, solmat, ctmp); clipgamut(ct2, bright(ct2), CGAMUT, cblack, cwhite); setcolr(cvclr[i], colval(ct2,RED), colval(ct2,GRN), colval(ct2,BLU)); } } } /* allocate scanline */ scan = (COLR *)malloc(xmax*sizeof(COLR)); if (scan == NULL) { perror(progname); exit(1); } /* finish debug header */ fputformat(COLRFMT, debugfp); putc('\n', debugfp); fprtresolu(xmax, ymax, debugfp); /* write debug picture */ for (y = ymax-1; y >= 0; y--) { for (x = 0; x < xmax; x++) { rg = chartndx(x, y, &i); if (rg == RG_CENT) { if (!(1L<<i & gmtflags) || (x+y)&07) copycolr(scan[x], mbclr[i]); else copycolr(scan[x], blkclr); } else if (rg == RG_BORD || !(1L<<i & inpflags)) copycolr(scan[x], blkclr); else if (rg == RG_ORIG) copycolr(scan[x], orclr[i]); else /* rg == RG_CORR */ copycolr(scan[x], cvclr[i]); } if (fwritecolrs(scan, xmax, debugfp) < 0) { fprintf(stderr, "%s: error writing debugging picture\n", progname); exit(1); } } /* clean up */ fclose(debugfp); free((void *)scan); }
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[] ) { 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); }
int main( int argc, char *argv[] ) { int original; double f; int a; SET_DEFAULT_BINARY(); SET_FILE_BINARY(stdin); SET_FILE_BINARY(stdout); progname = argv[0]; /* scan options */ for (a = 1; a < argc; a++) { if (argv[a][0] == '-') switch (argv[a][1]) { case 'x': case 'y': a++; continue; case 'w': nowarn = !nowarn; continue; case 'h': echoheader = !echoheader; continue; case 'f': case 'e': a++; continue; } break; } newheader("RADIANCE", stdout); /* start header */ fputnow(stdout); /* process files */ for (nfiles = 0; nfiles < MAXINP; nfiles++) { setcolor(input[nfiles].coef, 1.0, 1.0, 1.0); setcolor(input[nfiles].expos, 1.0, 1.0, 1.0); input[nfiles].vw = stdview; input[nfiles].pa = 1.0; } nfiles = 0; original = 0; for ( ; a < argc; a++) { if (nfiles >= MAXINP) { eputs(argv[0]); eputs(": too many picture files\n"); quit(1); } if (argv[a][0] == '-') switch (argv[a][1]) { case '\0': input[nfiles].name = StandardInput; input[nfiles].fp = stdin; break; case 'o': original++; continue; case 's': f = atof(argv[++a]); scalecolor(input[nfiles].coef, f); continue; case 'c': colval(input[nfiles].coef,RED)*=atof(argv[++a]); colval(input[nfiles].coef,GRN)*=atof(argv[++a]); colval(input[nfiles].coef,BLU)*=atof(argv[++a]); continue; default: goto usage; } else { if (argv[a][0] == '!') { input[nfiles].name = Command; input[nfiles].fp = popen(argv[a]+1, "r"); } else { input[nfiles].name = argv[a]; input[nfiles].fp = fopen(argv[a], "r"); } if (input[nfiles].fp == NULL) { perror(argv[a]); quit(1); } } checkfile(); if (original) { colval(input[nfiles].coef,RED) /= colval(input[nfiles].expos,RED); colval(input[nfiles].coef,GRN) /= colval(input[nfiles].expos,GRN); colval(input[nfiles].coef,BLU) /= colval(input[nfiles].expos,BLU); setcolor(input[nfiles].expos, 1.0, 1.0, 1.0); } nfiles++; original = 0; } init(); /* set constants */ /* go back and get expressions */ for (a = 1; a < argc; a++) { if (argv[a][0] == '-') switch (argv[a][1]) { case 'x': varset(vxres, ':', eval(argv[++a])); continue; case 'y': varset(vyres, ':', eval(argv[++a])); continue; case 'w': continue; case 'h': continue; case 'f': fcompile(argv[++a]); continue; case 'e': scompile(argv[++a], NULL, 0); continue; } break; } /* set/get output resolution */ if (!vardefined(vxres)) varset(vxres, ':', (double)xmax); if (!vardefined(vyres)) varset(vyres, ':', (double)ymax); xres = varvalue(vxres) + .5; yres = varvalue(vyres) + .5; if (xres <= 0 || yres <= 0) { eputs(argv[0]); eputs(": illegal output resolution\n"); quit(1); } /* complete header */ printargs(argc, argv, stdout); if (strcmp(ourfmt, PICFMT)) fputformat(ourfmt, stdout); /* print format if known */ putchar('\n'); fprtresolu(xres, yres, stdout); /* combine pictures */ combine(); quit(0); usage: eputs("Usage: "); eputs(argv[0]); eputs( " [-w][-h][-x xr][-y yr][-e expr][-f file] [ [-o][-s f][-c r g b] hdr ..]\n"); quit(1); return 1; /* pro forma return */ }