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 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); } }
int freadscan( /* read in a scanline */ COLOR *scanline, int len, FILE *fp ) { COLR *clrscan; if ((clrscan = (COLR *)tempbuffer(len*sizeof(COLR))) == NULL) return(-1); if (freadcolrs(clrscan, len, fp) < 0) return(-1); /* convert scanline */ colr_color(scanline[0], clrscan[0]); while (--len > 0) { scanline++; clrscan++; if (clrscan[0][GRN] == clrscan[-1][GRN] && (clrscan[0][RED] == clrscan[-1][RED]) & (clrscan[0][BLU] == clrscan[-1][BLU]) & (clrscan[0][EXP] == clrscan[-1][EXP])) copycolor(scanline[0], scanline[-1]); else colr_color(scanline[0], clrscan[0]); } return(0); }
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 getpicture(void) /* load in picture colors */ { COLR *scanln; COLOR pval; int ccount[24]; double d; int y, i; register int x; scanln = (COLR *)malloc(xmax*sizeof(COLR)); if (scanln == NULL) { perror(progname); exit(1); } for (i = 0; i < 24; i++) { setcolor(inpRGB[i], 0., 0., 0.); ccount[i] = 0; } for (y = ymax-1; y >= 0; y--) { if (freadcolrs(scanln, xmax, stdin) < 0) { fprintf(stderr, "%s: error reading input picture\n", progname); exit(1); } for (x = 0; x < xmax; x++) if (chartndx(x, y, &i) == RG_CENT) { colr_color(pval, scanln[x]); addcolor(inpRGB[i], pval); ccount[i]++; } } for (i = 0; i < 24; i++) { /* compute averages */ if (ccount[i] == 0) continue; d = 1./ccount[i]; scalecolor(inpRGB[i], d); inpflags |= 1L<<i; } free((void *)scanln); }
static void rotateccw( /* rotate picture counter-clockwise */ FILE *fp ) { register COLR *inln; register int xoff, inx, iny; long start, ftell(); if ((inln = (COLR *)malloc(xres*sizeof(COLR))) == NULL) { fprintf(stderr, "%s: out of memory\n", progname); exit(1); } start = ftell(fp); for (xoff = xres-1; xoff >= 0; xoff -= nrows) { if (fseek(fp, start, 0) < 0) { fprintf(stderr, "%s: seek error\n", progname); exit(1); } for (iny = 0; iny < yres; iny++) { if (freadcolrs(inln, xres, fp) < 0) { fprintf(stderr, "%s: read error\n", progname); exit(1); } for (inx = 0; inx < nrows && xoff-inx >= 0; inx++) copycolr(scanbar[inx*yres+iny], inln[xoff-inx]); } for (inx = 0; inx < nrows && xoff-inx >= 0; inx++) if (fwritecolrs(scanbar+inx*yres, yres, stdout) < 0) { fprintf(stderr, "%s: write error\n", progname); exit(1); } } free((void *)inln); }
struct ImBuf *imb_loadhdr(unsigned char *mem, size_t size, int flags) { struct ImBuf* ibuf; RGBE* sline; fCOLOR fcol; float* rect_float; int found=0; int width=0, height=0; int x, y; unsigned char* ptr; char oriY[80], oriX[80]; if (imb_is_a_hdr((void*)mem)) { /* find empty line, next line is resolution info */ for (x=1;x<size;x++) { if ((mem[x-1]=='\n') && (mem[x]=='\n')) { found = 1; break; } } if (found && (x<(size + 2))) { if (sscanf((char *)&mem[x+1], "%79s %d %79s %d", (char*)&oriY, &height, (char*)&oriX, &width) != 4) return NULL; /* find end of this line, data right behind it */ ptr = (unsigned char *)strchr((char*)&mem[x+1], '\n'); ptr++; if (flags & IB_test) ibuf = IMB_allocImBuf(width, height, 32, 0); else ibuf = IMB_allocImBuf(width, height, 32, (flags & IB_rect)|IB_rectfloat); if (ibuf==NULL) return NULL; ibuf->ftype = RADHDR; ibuf->profile = IB_PROFILE_LINEAR_RGB; if (flags & IB_test) return ibuf; /* read in and decode the actual data */ sline = (RGBE*)MEM_mallocN(sizeof(RGBE)*width, "radhdr_read_tmpscan"); rect_float = (float *)ibuf->rect_float; for (y=0;y<height;y++) { ptr = freadcolrs(sline, ptr, width); if (ptr==NULL) { printf("HDR decode error\n"); MEM_freeN(sline); return ibuf; } for (x=0;x<width;x++) { /* convert to ldr */ RGBE2FLOAT(sline[x], fcol); *rect_float++ = fcol[RED]; *rect_float++ = fcol[GRN]; *rect_float++ = fcol[BLU]; *rect_float++ = 1.0f; } } MEM_freeN(sline); if (oriY[0]=='-') IMB_flipy(ibuf); if (flags & IB_rect) { IMB_rect_from_float(ibuf); } return ibuf; } //else printf("Data not found!\n"); } //else printf("Not a valid radiance HDR file!\n"); return NULL; }
static void compos(void) /* composite pictures */ { COLR *scanin, *scanout; int y; register int x, i; scanin = (COLR *)malloc((xmax-xmin)*sizeof(COLR)); if (scanin == NULL) goto memerr; scanin -= xmin; if (checkthresh) { scanout = (COLR *)malloc(xsiz*sizeof(COLR)); if (scanout == NULL) goto memerr; } else scanout = scanin; for (y = ymax-1; y >= 0; y--) { for (x = 0; x < xsiz; x++) copycolr(scanout[x], bgcolr); for (i = 0; i < nfile; i++) { if (input[i].yloc > y || input[i].yloc+input[i].yres <= y) continue; if (freadcolrs(scanin+input[i].xloc, input[i].xres, input[i].fp) < 0) { fprintf(stderr, "%s: read error (y==%d)\n", input[i].name, y-input[i].yloc); quit(1); } if (y >= ysiz) continue; if (checkthresh) { x = input[i].xloc+input[i].xres; if (x > xsiz) x = xsiz; for (x--; x >= 0 && x >= input[i].xloc; x--) { if (input[i].flags & HASMIN && cmpcolr(scanin[x], input[i].thmin) <= 0) continue; if (input[i].flags & HASMAX && cmpcolr(scanin[x], input[i].thmax) >= 0) continue; copycolr(scanout[x], scanin[x]); } } } if (y >= ysiz) continue; if (fwritecolrs(scanout, xsiz, stdout) < 0) { perror(progname); quit(1); } } /* read remainders from streams */ for (i = 0; i < nfile; i++) if (input[i].name[0] == '<') while (getc(input[i].fp) != EOF) ; return; memerr: perror(progname); quit(1); }
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); }
struct ImBuf *imb_loadhdr(const unsigned char *mem, size_t size, int flags, char colorspace[IM_MAX_SPACE]) { struct ImBuf *ibuf; RGBE *sline; fCOLOR fcol; float *rect_float; int found = 0; int width = 0, height = 0; int x, y; const unsigned char *ptr, *mem_eof = mem + size; char oriY[80], oriX[80]; if (imb_is_a_hdr((void *)mem)) { colorspace_set_default_role(colorspace, IM_MAX_SPACE, COLOR_ROLE_DEFAULT_FLOAT); /* find empty line, next line is resolution info */ for (x = 1; x < size; x++) { if ((mem[x - 1] == '\n') && (mem[x] == '\n')) { found = 1; break; } } if (found && (x < (size + 2))) { if (sscanf((char *)&mem[x + 1], "%79s %d %79s %d", (char *)&oriY, &height, (char *)&oriX, &width) != 4) { return NULL; } /* find end of this line, data right behind it */ ptr = (unsigned char *)strchr((char *)&mem[x + 1], '\n'); ptr++; if (flags & IB_test) ibuf = IMB_allocImBuf(width, height, 32, 0); else ibuf = IMB_allocImBuf(width, height, 32, (flags & IB_rect) | IB_rectfloat); if (ibuf == NULL) return NULL; ibuf->ftype = IMB_FTYPE_RADHDR; if (flags & IB_alphamode_detect) ibuf->flags |= IB_alphamode_premul; if (flags & IB_test) return ibuf; /* read in and decode the actual data */ sline = (RGBE *)MEM_mallocN(sizeof(*sline) * width, __func__); rect_float = ibuf->rect_float; for (y = 0; y < height; y++) { ptr = freadcolrs(sline, ptr, width, mem_eof); if (ptr == NULL) { printf("WARNING! HDR decode error, image may be just truncated, or completely wrong...\n"); break; } for (x = 0; x < width; x++) { /* convert to ldr */ RGBE2FLOAT(sline[x], fcol); *rect_float++ = fcol[RED]; *rect_float++ = fcol[GRN]; *rect_float++ = fcol[BLU]; *rect_float++ = 1.0f; } } MEM_freeN(sline); if (oriY[0] == '-') IMB_flipy(ibuf); if (flags & IB_rect) { IMB_rect_from_float(ibuf); } return ibuf; } //else printf("Data not found!\n"); } //else printf("Not a valid radiance HDR file!\n"); return NULL; }
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); }