--- ray/src/px/panim.c 1989/09/12 13:04:21 1.2 +++ ray/src/px/panim.c 2003/06/30 14:59:12 2.3 @@ -1,9 +1,6 @@ -/* Copyright (c) 1988 Regents of the University of California */ - #ifndef lint -static char SCCSid[] = "$SunId$ LBL"; +static const char RCSid[] = "$Id: panim.c,v 2.3 2003/06/30 14:59:12 schorsch Exp $"; #endif - /* * Send pictures to PC animation system. * @@ -11,7 +8,7 @@ static char SCCSid[] = "$SunId$ LBL"; */ #include -#include +#include #include "random.h" #include "color.h" @@ -20,6 +17,10 @@ static char SCCSid[] = "$SunId$ LBL"; #define GAMMA 2.0 /* gamma correction factor */ +FILE *popen(); + +char *pcom = NULL; /* pipe command */ + BYTE gammamap[256]; /* gamma correction table */ @@ -35,12 +36,14 @@ char *argv[]; for (progname = *argv++; --argc; argv++) if (!strcmp(*argv, "-p") && argv[1]) { port = atoi(*++argv); argc--; + } else if (!strcmp(*argv, "-u") && argv[1]) { + pcom = *++argv; argc--; } else break; if (!argc) { fputs("Usage: ", stderr); fputs(progname, stderr); - fputs(" [-p port] hostname [-c copies][-r record] [frame] ..\n", + fputs(" [-p port] [-u uncompress] hostname [-c copies][-r record] [frame] ..\n", stderr); exit(1); } @@ -71,24 +74,38 @@ char *argv[]; sendframe(file) /* convert and send a frame */ char *file; { + char command[128]; COLR scanin[SCANLINE]; int xres, yres; int xbeg, ybeg; FILE *fp; - int j; - register int k, c; - register BYTE *in; - short *out; + int y; + register int x; /* open file */ if (file == NULL) { - fp = stdin; + if (pcom != NULL) + fp = popen(pcom, "r"); + else + fp = stdin; file = ""; - } else if ((fp = fopen(file, "r")) == NULL) { + } else { + if (pcom != NULL) { + sprintf(command, "( %s ) < %s", pcom, file); + fp = popen(command, "r"); + } else + fp = fopen(file, "r"); + } + if (fp == NULL) { perror(file); exit(1); } /* get dimensions */ getheader(fp, NULL); + if (checkheader(fp, COLRFMT, NULL) < 0) { + fputs(file, stderr); + fputs(": not a Radiance picture\n", stderr); + exit(1); + } if (fgetresolu(&xres, &yres, fp) != (YMAJOR|YDECR) || xres > SCANLINE || yres > NUMSCANS) { fputs(file, stderr); @@ -99,45 +116,28 @@ char *file; xbeg = (SCANLINE-xres)/2; ybeg = (NUMSCANS-yres)/2; /* clear output */ - bzero(sc_frame_arr, sizeof(sc_frame_arr)); + memset(sc_frame_arr, '\0', sizeof(sc_frame_arr)); /* get frame */ - for (j = yres-1; j >= 0; j--) { + for (y = yres-1; y >= 0; y--) { if (freadcolrs(scanin, xres, fp) < 0) { fputs(file, stderr); fputs(": read error\n", stderr); exit(1); } - for (in = scanin[0], out = sc_frame_arr[ybeg+j]+xbeg; - in < scanin[xres]; in += 4, out++) { - k = in[EXP] - COLXS; - if (k >= 8) { - in[RED] = - in[GRN] = - in[BLU] = 255; - } else if (k <= -8) { - in[RED] = - in[GRN] = - in[BLU] = 0; - } else if (k > 0) { - c = in[RED] << k; - in[RED] = c > 255 ? 255 : c; - c = in[GRN] << k; - in[GRN] = c > 255 ? 255 : c; - c = in[BLU] << k; - in[BLU] = c > 255 ? 255 : c; - } else if (k < 0) { - in[RED] = in[RED] >> -k; - in[GRN] = in[GRN] >> -k; - in[BLU] = in[BLU] >> -k; - } - for (k = 0; k < 3; k++) - *out = *out<<5 | (gammamap[in[k]]+(random()&0x7))>>3; - } + normcolrs(scanin, xres, 0); /* normalize */ + for (x = 0; x < xres; x++) /* convert */ + sc_frame_arr[y+ybeg][x+xbeg] = + ((gammamap[scanin[x][RED]]+(random()&7))&0xf8)<<7 + | ((gammamap[scanin[x][GRN]]+(random()&7))&0xf8)<<2 + | (gammamap[scanin[x][BLU]]+(random()&7))>>3; } /* send frame */ scry_send_frame(); /* close file */ - fclose(fp); + if (pcom != NULL) + pclose(fp); + else + fclose(fp); } @@ -147,7 +147,7 @@ compgamma() /* compute gamma correction map */ register int i, val; for (i = 0; i < 256; i++) { - val = pow(i/256.0, 1.0/GAMMA) * 256.0; + val = pow((i+0.5)/256.0, 1.0/GAMMA) * 256.0; if (val > 248) val = 248; gammamap[i] = val; }