static void getrow( /* get the specified row from our image */ int y ) { if (y == currow) return; if (y < currow) { fseek(stdin, picstart, 0); currow = -1; } do if (freadcolrs(scanln, xmax, stdin) < 0) { fprintf(stderr, "%s: error reading picture (y==%d)\n", progname, ymax-1-y); exit(1); } while (++currow < y); if (bradj) shiftcolrs(scanln, xmax, bradj); colrs_gambs(scanln, xmax); if (pixscan != NULL) { if (samplefac) neu_dith_colrs(pixscan, scanln, xmax); else dith_colrs(pixscan, scanln, xmax); } }
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 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 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); }
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); }
void addpicz( /* add a picture + depth-buffer */ char *pcf, char *zbf ) { FILE *pfp; int zfd; COLR *cscn; float *zscn; struct phead phd; int eshft; double emult; RESOLU prs; RREAL vl[2]; FVECT ro, rd; double aftd; COLOR ctmp; int j; register int i; /* open files */ if ((pfp = fopen(pcf, "r")) == NULL) { sprintf(errmsg, "cannot open picture file \"%s\"", pcf); error(SYSTEM, pcf); } if ((zfd = open(zbf, O_RDONLY)) < 0) { sprintf(errmsg, "cannot open depth file \"%s\"", zbf); error(SYSTEM, pcf); } /* load picture header */ phd.vw = stdview; phd.expos = 1.0; phd.badfmt = phd.gotview = phd.altprims = 0; if (getheader(pfp, picheadline, &phd) < 0 || phd.badfmt || !fgetsresolu(&prs, pfp)) { sprintf(errmsg, "bad format for picture file \"%s\"", pcf); error(USER, errmsg); } if (!phd.gotview || setview(&phd.vw) != NULL) { sprintf(errmsg, "missing/illegal view in picture \"%s\"", pcf); error(USER, errmsg); } if (phd.altprims) { sprintf(errmsg, "ignoring primary values in picture \"%s\"", pcf); error(WARNING, errmsg); } /* figure out what to do about exposure */ if ((phd.expos < 0.99) | (phd.expos > 1.01)) { emult = -log(phd.expos)/log(2.); eshft = emult >= 0. ? emult+.5 : emult-.5; emult -= (double)eshft; if ((emult <= 0.01) & (emult >= -0.01)) emult = -1.; else { emult = 1./phd.expos; eshft = 0; } } else { emult = -1.; eshft = 0; } /* allocate buffers */ cscn = (COLR *)malloc(scanlen(&prs)*sizeof(COLR)); zscn = (float *)malloc(scanlen(&prs)*sizeof(float)); if ((cscn == NULL) | (zscn == NULL)) error(SYSTEM, "out of memory in addpicz"); /* read and process each scanline */ for (j = 0; j < numscans(&prs); j++) { i = scanlen(&prs); /* read colrs */ if (freadcolrs(cscn, i, pfp) < 0) { sprintf(errmsg, "error reading picture \"%s\"", pcf); error(USER, errmsg); } if (eshft) /* shift exposure */ shiftcolrs(cscn, i, eshft); i *= sizeof(float); /* read depth */ if (read(zfd, (char *)zscn, i) != i) { sprintf(errmsg, "error reading depth file \"%s\"", zbf); error(USER, errmsg); } for (i = scanlen(&prs); i--; ) { /* do each pixel */ pix2loc(vl, &prs, i, j); aftd = viewray(ro, rd, &phd.vw, vl[0], vl[1]); if (aftd < -FTINY) continue; /* off view */ if (aftd > FTINY && zscn[i] > aftd) continue; /* aft clipped */ if (emult > 0.) { /* whatta pain */ colr_color(ctmp, cscn[i]); scalecolor(ctmp, emult); setcolr(cscn[i], colval(ctmp,RED), colval(ctmp,GRN), colval(ctmp,BLU)); } addray(ro, rd, (double)zscn[i], cscn[i]); } } /* write output and free beams */ hdflush(NULL); /* clean up */ free((void *)cscn); free((void *)zscn); fclose(pfp); close(zfd); }