| 1 |
greg |
1.1 |
#ifndef lint
|
| 2 |
greg |
2.99 |
static const char RCSid[] = "$Id: rpict.c,v 2.98 2023/01/24 21:54:49 greg Exp $";
|
| 3 |
greg |
1.1 |
#endif
|
| 4 |
|
|
/*
|
| 5 |
|
|
* rpict.c - routines and variables for picture generation.
|
| 6 |
|
|
*/
|
| 7 |
|
|
|
| 8 |
greg |
2.53 |
#include "copyright.h"
|
| 9 |
greg |
2.52 |
|
| 10 |
greg |
2.32 |
#include <sys/types.h>
|
| 11 |
|
|
|
| 12 |
schorsch |
2.80 |
#include "platform.h"
|
| 13 |
|
|
#ifdef NON_POSIX
|
| 14 |
|
|
#ifdef MINGW
|
| 15 |
|
|
#include <sys/time.h>
|
| 16 |
|
|
#endif
|
| 17 |
schorsch |
2.79 |
#else
|
| 18 |
schorsch |
2.80 |
#ifdef BSD
|
| 19 |
|
|
#include <sys/time.h>
|
| 20 |
|
|
#include <sys/resource.h>
|
| 21 |
|
|
#else
|
| 22 |
|
|
#include <sys/times.h>
|
| 23 |
|
|
#include <unistd.h>
|
| 24 |
|
|
#endif
|
| 25 |
greg |
2.30 |
#endif
|
| 26 |
greg |
1.34 |
|
| 27 |
schorsch |
2.55 |
#include <time.h>
|
| 28 |
greg |
1.14 |
#include <signal.h>
|
| 29 |
greg |
1.1 |
|
| 30 |
schorsch |
2.69 |
#include "ray.h"
|
| 31 |
|
|
#include "paths.h"
|
| 32 |
|
|
#include "ambient.h"
|
| 33 |
greg |
1.1 |
#include "view.h"
|
| 34 |
|
|
#include "random.h"
|
| 35 |
schorsch |
2.55 |
#include "paths.h"
|
| 36 |
greg |
2.87 |
#include "hilbert.h"
|
| 37 |
greg |
2.89 |
#include "pmapbias.h"
|
| 38 |
|
|
#include "pmapdiag.h"
|
| 39 |
greg |
2.12 |
|
| 40 |
greg |
2.21 |
#define RFTEMPLATE "rfXXXXXX"
|
| 41 |
|
|
|
| 42 |
greg |
2.22 |
#ifndef SIGCONT
|
| 43 |
schorsch |
2.56 |
#ifdef SIGIO /* XXX can we live without this? */
|
| 44 |
greg |
2.22 |
#define SIGCONT SIGIO
|
| 45 |
|
|
#endif
|
| 46 |
schorsch |
2.56 |
#endif
|
| 47 |
greg |
2.22 |
|
| 48 |
greg |
2.52 |
CUBE thescene; /* our scene */
|
| 49 |
|
|
OBJECT nsceneobjs; /* number of objects in our scene */
|
| 50 |
|
|
|
| 51 |
greg |
2.99 |
extern RGBPRIMP out_prims; /* output color primitives (NULL if spectral) */
|
| 52 |
|
|
|
| 53 |
greg |
1.25 |
int dimlist[MAXDIM]; /* sampling dimensions */
|
| 54 |
|
|
int ndims = 0; /* number of sampling dimensions */
|
| 55 |
|
|
int samplendx; /* sample index number */
|
| 56 |
|
|
|
| 57 |
greg |
2.52 |
void (*addobjnotify[])() = {ambnotify, NULL};
|
| 58 |
|
|
|
| 59 |
greg |
1.12 |
VIEW ourview = STDVIEW; /* view parameters */
|
| 60 |
|
|
int hresolu = 512; /* horizontal resolution */
|
| 61 |
|
|
int vresolu = 512; /* vertical resolution */
|
| 62 |
greg |
2.14 |
double pixaspect = 1.0; /* pixel aspect ratio */
|
| 63 |
greg |
1.1 |
|
| 64 |
|
|
int psample = 4; /* pixel sample size */
|
| 65 |
greg |
2.14 |
double maxdiff = .05; /* max. difference for interpolation */
|
| 66 |
|
|
double dstrpix = 0.67; /* square pixel distribution */
|
| 67 |
greg |
1.1 |
|
| 68 |
gwlarson |
2.49 |
double mblur = 0.; /* motion blur parameter */
|
| 69 |
|
|
|
| 70 |
greg |
2.73 |
double dblur = 0.; /* depth-of-field blur parameter */
|
| 71 |
|
|
|
| 72 |
greg |
2.52 |
void (*trace)() = NULL; /* trace call */
|
| 73 |
|
|
|
| 74 |
|
|
int do_irrad = 0; /* compute irradiance? */
|
| 75 |
|
|
|
| 76 |
greg |
2.76 |
int rand_samp = 0; /* pure Monte Carlo sampling? */
|
| 77 |
|
|
|
| 78 |
greg |
2.14 |
double dstrsrc = 0.0; /* square source distribution */
|
| 79 |
|
|
double shadthresh = .05; /* shadow threshold */
|
| 80 |
|
|
double shadcert = .5; /* shadow certainty */
|
| 81 |
greg |
1.35 |
int directrelay = 1; /* number of source relays */
|
| 82 |
greg |
1.29 |
int vspretest = 512; /* virtual source pretest density */
|
| 83 |
greg |
2.20 |
int directvis = 1; /* sources visible? */
|
| 84 |
greg |
2.14 |
double srcsizerat = .25; /* maximum ratio source size/dist. */
|
| 85 |
greg |
2.41 |
|
| 86 |
|
|
COLOR cextinction = BLKCOLOR; /* global extinction coefficient */
|
| 87 |
greg |
2.46 |
COLOR salbedo = BLKCOLOR; /* global scattering albedo */
|
| 88 |
greg |
2.41 |
double seccg = 0.; /* global scattering eccentricity */
|
| 89 |
|
|
double ssampdist = 0.; /* scatter sampling distance */
|
| 90 |
greg |
1.1 |
|
| 91 |
greg |
2.14 |
double specthresh = .15; /* specular sampling threshold */
|
| 92 |
|
|
double specjitter = 1.; /* specular sampling jitter */
|
| 93 |
greg |
2.5 |
|
| 94 |
greg |
2.40 |
int backvis = 1; /* back face visibility */
|
| 95 |
|
|
|
| 96 |
greg |
2.63 |
int maxdepth = 7; /* maximum recursion depth */
|
| 97 |
greg |
2.98 |
double minweight = 1e-4; /* minimum ray weight */
|
| 98 |
greg |
1.1 |
|
| 99 |
greg |
2.52 |
char *ambfile = NULL; /* ambient file name */
|
| 100 |
greg |
1.1 |
COLOR ambval = BLKCOLOR; /* ambient value */
|
| 101 |
greg |
2.44 |
int ambvwt = 0; /* initial weight for ambient value */
|
| 102 |
greg |
2.68 |
double ambacc = 0.2; /* ambient accuracy */
|
| 103 |
greg |
2.63 |
int ambres = 64; /* ambient resolution */
|
| 104 |
|
|
int ambdiv = 512; /* ambient divisions */
|
| 105 |
|
|
int ambssamp = 128; /* ambient super-samples */
|
| 106 |
greg |
1.1 |
int ambounce = 0; /* ambient bounces */
|
| 107 |
greg |
2.72 |
char *amblist[AMBLLEN]; /* ambient include/exclude list */
|
| 108 |
greg |
1.1 |
int ambincl = -1; /* include == 1, exclude == 0 */
|
| 109 |
|
|
|
| 110 |
|
|
int ralrm = 0; /* seconds between reports */
|
| 111 |
|
|
|
| 112 |
greg |
2.14 |
double pctdone = 0.0; /* percentage done */
|
| 113 |
greg |
2.32 |
time_t tlastrept = 0L; /* time at last report */
|
| 114 |
greg |
2.52 |
time_t tstart; /* starting time */
|
| 115 |
greg |
1.1 |
|
| 116 |
greg |
2.14 |
#define MAXDIV 16 /* maximum sample size */
|
| 117 |
greg |
1.1 |
|
| 118 |
greg |
2.14 |
#define pixjitter() (.5+dstrpix*(.5-frandom()))
|
| 119 |
greg |
1.1 |
|
| 120 |
greg |
2.47 |
int hres, vres; /* resolution for this frame */
|
| 121 |
greg |
2.8 |
|
| 122 |
gwlarson |
2.49 |
static VIEW lastview; /* the previous view input */
|
| 123 |
|
|
|
| 124 |
schorsch |
2.69 |
static void report(int);
|
| 125 |
|
|
static int nextview(FILE *fp);
|
| 126 |
|
|
static void render(char *zfile, char *oldfile);
|
| 127 |
|
|
static void fillscanline(COLOR *scanline, float *zline, char *sd, int xres,
|
| 128 |
|
|
int y, int xstep);
|
| 129 |
|
|
static void fillscanbar(COLOR *scanbar[], float *zbar[], int xres,
|
| 130 |
|
|
int y, int ysize);
|
| 131 |
|
|
static int fillsample(COLOR *colline, float *zline, int x, int y,
|
| 132 |
|
|
int xlen, int ylen, int b);
|
| 133 |
|
|
static double pixvalue(COLOR col, int x, int y);
|
| 134 |
|
|
static int salvage(char *oldfile);
|
| 135 |
|
|
static int pixnumber(int x, int y, int xres, int yres);
|
| 136 |
greg |
2.8 |
|
| 137 |
greg |
2.52 |
|
| 138 |
greg |
1.1 |
|
| 139 |
greg |
2.52 |
void
|
| 140 |
greg |
2.94 |
quit(int code) /* quit program */
|
| 141 |
greg |
1.1 |
{
|
| 142 |
greg |
2.8 |
if (code) /* report status */
|
| 143 |
schorsch |
2.69 |
report(0);
|
| 144 |
schorsch |
2.57 |
#ifndef NON_POSIX
|
| 145 |
greg |
2.21 |
headclean(); /* delete header file */
|
| 146 |
|
|
pfclean(); /* clean up persist files */
|
| 147 |
greg |
2.52 |
#endif
|
| 148 |
greg |
1.1 |
exit(code);
|
| 149 |
|
|
}
|
| 150 |
|
|
|
| 151 |
|
|
|
| 152 |
schorsch |
2.57 |
#ifndef NON_POSIX
|
| 153 |
schorsch |
2.69 |
static void
|
| 154 |
|
|
report(int dummy) /* report progress */
|
| 155 |
greg |
1.1 |
{
|
| 156 |
greg |
2.89 |
char bcStat [128];
|
| 157 |
greg |
2.88 |
double u, s;
|
| 158 |
greg |
2.30 |
#ifdef BSD
|
| 159 |
greg |
2.88 |
struct rusage rubuf;
|
| 160 |
greg |
2.30 |
#else
|
| 161 |
greg |
2.88 |
double period = 1.0 / 60.0;
|
| 162 |
|
|
struct tms tbuf;
|
| 163 |
greg |
2.30 |
#endif
|
| 164 |
greg |
1.1 |
|
| 165 |
greg |
2.32 |
tlastrept = time((time_t *)NULL);
|
| 166 |
greg |
2.30 |
#ifdef BSD
|
| 167 |
greg |
1.1 |
getrusage(RUSAGE_SELF, &rubuf);
|
| 168 |
greg |
2.88 |
u = rubuf.ru_utime.tv_sec + rubuf.ru_utime.tv_usec*1e-6;
|
| 169 |
|
|
s = rubuf.ru_stime.tv_sec + rubuf.ru_stime.tv_usec*1e-6;
|
| 170 |
greg |
1.1 |
getrusage(RUSAGE_CHILDREN, &rubuf);
|
| 171 |
greg |
2.88 |
u += rubuf.ru_utime.tv_sec + rubuf.ru_utime.tv_usec*1e-6;
|
| 172 |
|
|
s += rubuf.ru_stime.tv_sec + rubuf.ru_stime.tv_usec*1e-6;
|
| 173 |
greg |
2.30 |
#else
|
| 174 |
|
|
times(&tbuf);
|
| 175 |
greg |
2.36 |
#ifdef _SC_CLK_TCK
|
| 176 |
greg |
2.33 |
period = 1.0 / sysconf(_SC_CLK_TCK);
|
| 177 |
greg |
2.36 |
#endif
|
| 178 |
greg |
2.33 |
u = ( tbuf.tms_utime + tbuf.tms_cutime ) * period;
|
| 179 |
|
|
s = ( tbuf.tms_stime + tbuf.tms_cstime ) * period;
|
| 180 |
greg |
2.30 |
#endif
|
| 181 |
greg |
1.1 |
|
| 182 |
greg |
2.89 |
/* PMAP: Get photon map bias compensation statistics */
|
| 183 |
|
|
pmapBiasCompReport(bcStat);
|
| 184 |
|
|
|
| 185 |
greg |
2.30 |
sprintf(errmsg,
|
| 186 |
rschregle |
2.90 |
"%lu rays, %s%4.2f%% after %.3fu %.3fs %.3fr hours on %s (PID %d)\n",
|
| 187 |
greg |
2.89 |
nrays, bcStat, pctdone, u*(1./3600.), s*(1./3600.),
|
| 188 |
greg |
2.88 |
(tlastrept-tstart)*(1./3600.), myhostname(), getpid());
|
| 189 |
greg |
1.24 |
eputs(errmsg);
|
| 190 |
schorsch |
2.56 |
#ifdef SIGCONT
|
| 191 |
greg |
2.35 |
signal(SIGCONT, report);
|
| 192 |
|
|
#endif
|
| 193 |
greg |
1.24 |
}
|
| 194 |
greg |
1.1 |
#else
|
| 195 |
schorsch |
2.69 |
static void
|
| 196 |
schorsch |
2.71 |
report(int dummy) /* report progress */
|
| 197 |
greg |
1.24 |
{
|
| 198 |
greg |
2.89 |
char bcStat [128];
|
| 199 |
|
|
|
| 200 |
greg |
2.32 |
tlastrept = time((time_t *)NULL);
|
| 201 |
greg |
2.89 |
|
| 202 |
|
|
/* PMAP: Get photon map bias compensation statistics */
|
| 203 |
|
|
pmapBiasCompReport(bcStat);
|
| 204 |
|
|
|
| 205 |
rschregle |
2.90 |
sprintf(errmsg, "%lu rays, %s%4.2f%% after %5.4f hours\n",
|
| 206 |
greg |
2.89 |
nrays, bcStat, pctdone, (tlastrept-tstart)/3600.0);
|
| 207 |
greg |
1.1 |
eputs(errmsg);
|
| 208 |
|
|
}
|
| 209 |
greg |
1.24 |
#endif
|
| 210 |
greg |
1.1 |
|
| 211 |
|
|
|
| 212 |
greg |
2.88 |
void
|
| 213 |
schorsch |
2.69 |
rpict( /* generate image(s) */
|
| 214 |
|
|
int seq,
|
| 215 |
|
|
char *pout,
|
| 216 |
|
|
char *zout,
|
| 217 |
|
|
char *prvr
|
| 218 |
|
|
)
|
| 219 |
greg |
2.8 |
/*
|
| 220 |
|
|
* If seq is greater than zero, then we will render a sequence of
|
| 221 |
|
|
* images based on view parameter strings read from the standard input.
|
| 222 |
|
|
* If pout is NULL, then all images will be sent to the standard ouput.
|
| 223 |
|
|
* If seq is greater than zero and prvr is an integer, then it is the
|
| 224 |
|
|
* frame number at which rendering should begin. Preceeding view parameter
|
| 225 |
|
|
* strings will be skipped in the input.
|
| 226 |
|
|
* If pout and prvr are the same, prvr is renamed to avoid overwriting.
|
| 227 |
|
|
* Note that pout and zout should contain %d format specifications for
|
| 228 |
|
|
* sequenced file naming.
|
| 229 |
|
|
*/
|
| 230 |
|
|
{
|
| 231 |
greg |
2.9 |
char fbuf[128], fbuf2[128];
|
| 232 |
greg |
2.26 |
int npicts;
|
| 233 |
greg |
2.85 |
char *cp;
|
| 234 |
greg |
2.14 |
RESOLU rs;
|
| 235 |
|
|
double pa;
|
| 236 |
greg |
2.8 |
/* check sampling */
|
| 237 |
|
|
if (psample < 1)
|
| 238 |
|
|
psample = 1;
|
| 239 |
|
|
else if (psample > MAXDIV) {
|
| 240 |
|
|
sprintf(errmsg, "pixel sampling reduced from %d to %d",
|
| 241 |
|
|
psample, MAXDIV);
|
| 242 |
|
|
error(WARNING, errmsg);
|
| 243 |
|
|
psample = MAXDIV;
|
| 244 |
|
|
}
|
| 245 |
|
|
/* get starting frame */
|
| 246 |
|
|
if (seq <= 0)
|
| 247 |
|
|
seq = 0;
|
| 248 |
|
|
else if (prvr != NULL && isint(prvr)) {
|
| 249 |
greg |
2.85 |
int rn; /* skip to specified view */
|
| 250 |
greg |
2.8 |
if ((rn = atoi(prvr)) < seq)
|
| 251 |
|
|
error(USER, "recover frame less than start frame");
|
| 252 |
|
|
if (pout == NULL)
|
| 253 |
|
|
error(USER, "missing output file specification");
|
| 254 |
|
|
for ( ; seq < rn; seq++)
|
| 255 |
|
|
if (nextview(stdin) == EOF)
|
| 256 |
|
|
error(USER, "unexpected EOF on view input");
|
| 257 |
gwlarson |
2.50 |
setview(&ourview);
|
| 258 |
greg |
2.8 |
prvr = fbuf; /* mark for renaming */
|
| 259 |
|
|
}
|
| 260 |
schorsch |
2.62 |
if ((pout != NULL) & (prvr != NULL)) {
|
| 261 |
greg |
2.8 |
sprintf(fbuf, pout, seq);
|
| 262 |
greg |
2.18 |
if (!strcmp(prvr, fbuf)) { /* rename */
|
| 263 |
|
|
strcpy(fbuf2, fbuf);
|
| 264 |
|
|
for (cp = fbuf2; *cp; cp++)
|
| 265 |
|
|
;
|
| 266 |
|
|
while (cp > fbuf2 && !ISDIRSEP(cp[-1]))
|
| 267 |
|
|
cp--;
|
| 268 |
|
|
strcpy(cp, RFTEMPLATE);
|
| 269 |
greg |
2.8 |
prvr = mktemp(fbuf2);
|
| 270 |
schorsch |
2.61 |
if (rename(fbuf, prvr) < 0) {
|
| 271 |
greg |
2.28 |
if (errno == ENOENT) { /* ghost file */
|
| 272 |
|
|
sprintf(errmsg,
|
| 273 |
|
|
"new output file \"%s\"",
|
| 274 |
|
|
fbuf);
|
| 275 |
|
|
error(WARNING, errmsg);
|
| 276 |
|
|
prvr = NULL;
|
| 277 |
|
|
} else { /* serious error */
|
| 278 |
|
|
sprintf(errmsg,
|
| 279 |
greg |
2.8 |
"cannot rename \"%s\" to \"%s\"",
|
| 280 |
|
|
fbuf, prvr);
|
| 281 |
greg |
2.28 |
error(SYSTEM, errmsg);
|
| 282 |
|
|
}
|
| 283 |
schorsch |
2.61 |
}
|
| 284 |
greg |
2.8 |
}
|
| 285 |
|
|
}
|
| 286 |
greg |
2.26 |
npicts = 0; /* render sequence */
|
| 287 |
greg |
2.8 |
do {
|
| 288 |
|
|
if (seq && nextview(stdin) == EOF)
|
| 289 |
|
|
break;
|
| 290 |
greg |
2.25 |
pctdone = 0.0;
|
| 291 |
greg |
2.8 |
if (pout != NULL) {
|
| 292 |
greg |
2.93 |
int myfd;
|
| 293 |
greg |
2.92 |
close(1); /* reassign stdout */
|
| 294 |
greg |
2.8 |
sprintf(fbuf, pout, seq);
|
| 295 |
greg |
2.93 |
tryagain:
|
| 296 |
greg |
2.92 |
errno = 0; /* exclusive open */
|
| 297 |
greg |
2.93 |
if ((myfd = open(fbuf, O_WRONLY|O_CREAT|O_EXCL, 0666)) < 0) {
|
| 298 |
greg |
2.92 |
if ((errno != EEXIST) | (prvr != NULL) ||
|
| 299 |
|
|
!strcmp(fbuf, pout)) {
|
| 300 |
greg |
2.27 |
sprintf(errmsg,
|
| 301 |
greg |
2.92 |
"cannot open output file \"%s\"",
|
| 302 |
greg |
2.27 |
fbuf);
|
| 303 |
greg |
2.92 |
error(SYSTEM, errmsg);
|
| 304 |
greg |
2.27 |
}
|
| 305 |
gwlarson |
2.50 |
setview(&ourview);
|
| 306 |
greg |
2.26 |
continue; /* don't clobber */
|
| 307 |
greg |
2.27 |
}
|
| 308 |
greg |
2.93 |
if (myfd != 1) {
|
| 309 |
|
|
unlink(fbuf);
|
| 310 |
|
|
goto tryagain; /* leave it open */
|
| 311 |
|
|
}
|
| 312 |
greg |
2.92 |
SET_FD_BINARY(1);
|
| 313 |
greg |
2.8 |
dupheader();
|
| 314 |
|
|
}
|
| 315 |
|
|
hres = hresolu; vres = vresolu; pa = pixaspect;
|
| 316 |
schorsch |
2.61 |
if (prvr != NULL) {
|
| 317 |
greg |
2.66 |
if (viewfile(prvr, &ourview, &rs) <= 0) {
|
| 318 |
greg |
2.8 |
sprintf(errmsg,
|
| 319 |
|
|
"cannot recover view parameters from \"%s\"", prvr);
|
| 320 |
|
|
error(WARNING, errmsg);
|
| 321 |
|
|
} else {
|
| 322 |
|
|
pa = 0.0;
|
| 323 |
|
|
hres = scanlen(&rs);
|
| 324 |
|
|
vres = numscans(&rs);
|
| 325 |
|
|
}
|
| 326 |
schorsch |
2.61 |
}
|
| 327 |
greg |
2.9 |
if ((cp = setview(&ourview)) != NULL)
|
| 328 |
|
|
error(USER, cp);
|
| 329 |
greg |
2.8 |
normaspect(viewaspect(&ourview), &pa, &hres, &vres);
|
| 330 |
|
|
if (seq) {
|
| 331 |
|
|
if (ralrm > 0) {
|
| 332 |
greg |
2.11 |
fprintf(stderr, "FRAME %d:", seq);
|
| 333 |
|
|
fprintview(&ourview, stderr);
|
| 334 |
|
|
putc('\n', stderr);
|
| 335 |
|
|
fflush(stderr);
|
| 336 |
greg |
2.8 |
}
|
| 337 |
|
|
printf("FRAME=%d\n", seq);
|
| 338 |
|
|
}
|
| 339 |
|
|
fputs(VIEWSTR, stdout);
|
| 340 |
|
|
fprintview(&ourview, stdout);
|
| 341 |
|
|
putchar('\n');
|
| 342 |
greg |
2.85 |
if ((pa < .99) | (pa > 1.01))
|
| 343 |
greg |
2.8 |
fputaspect(pa, stdout);
|
| 344 |
schorsch |
2.60 |
fputnow(stdout);
|
| 345 |
greg |
2.99 |
if (out_prims == xyzprims) {
|
| 346 |
|
|
fputformat(CIEFMT, stdout);
|
| 347 |
|
|
} else {
|
| 348 |
|
|
fputprims(out_prims, stdout);
|
| 349 |
|
|
fputformat(COLRFMT, stdout);
|
| 350 |
|
|
}
|
| 351 |
|
|
putchar('\n'); /* close header */
|
| 352 |
greg |
2.8 |
if (zout != NULL)
|
| 353 |
greg |
2.9 |
sprintf(cp=fbuf, zout, seq);
|
| 354 |
greg |
2.8 |
else
|
| 355 |
greg |
2.9 |
cp = NULL;
|
| 356 |
|
|
render(cp, prvr);
|
| 357 |
greg |
2.8 |
prvr = NULL;
|
| 358 |
greg |
2.26 |
npicts++;
|
| 359 |
greg |
2.8 |
} while (seq++);
|
| 360 |
greg |
2.26 |
/* check that we did something */
|
| 361 |
|
|
if (npicts == 0)
|
| 362 |
|
|
error(WARNING, "no output produced");
|
| 363 |
greg |
2.8 |
}
|
| 364 |
|
|
|
| 365 |
|
|
|
| 366 |
schorsch |
2.69 |
static int
|
| 367 |
|
|
nextview( /* get next view from fp */
|
| 368 |
|
|
FILE *fp
|
| 369 |
|
|
)
|
| 370 |
greg |
2.8 |
{
|
| 371 |
greg |
2.9 |
char linebuf[256];
|
| 372 |
greg |
2.8 |
|
| 373 |
schorsch |
2.61 |
lastview = ourview;
|
| 374 |
greg |
2.8 |
while (fgets(linebuf, sizeof(linebuf), fp) != NULL)
|
| 375 |
greg |
2.9 |
if (isview(linebuf) && sscanview(&ourview, linebuf) > 0)
|
| 376 |
greg |
2.8 |
return(0);
|
| 377 |
|
|
return(EOF);
|
| 378 |
|
|
}
|
| 379 |
|
|
|
| 380 |
|
|
|
| 381 |
schorsch |
2.69 |
static void
|
| 382 |
|
|
render( /* render the scene */
|
| 383 |
|
|
char *zfile,
|
| 384 |
|
|
char *oldfile
|
| 385 |
|
|
)
|
| 386 |
greg |
1.1 |
{
|
| 387 |
|
|
COLOR *scanbar[MAXDIV+1]; /* scanline arrays of pixel values */
|
| 388 |
greg |
1.11 |
float *zbar[MAXDIV+1]; /* z values */
|
| 389 |
greg |
2.2 |
char *sampdens; /* previous sample density */
|
| 390 |
greg |
1.1 |
int ypos; /* current scanline */
|
| 391 |
greg |
1.15 |
int ystep; /* current y step size */
|
| 392 |
greg |
1.33 |
int hstep; /* h step size */
|
| 393 |
greg |
1.16 |
int zfd;
|
| 394 |
greg |
1.1 |
COLOR *colptr;
|
| 395 |
greg |
1.11 |
float *zptr;
|
| 396 |
greg |
2.85 |
int i;
|
| 397 |
greg |
2.52 |
/* check for empty image */
|
| 398 |
greg |
2.85 |
if ((hres <= 0) | (vres <= 0)) {
|
| 399 |
greg |
2.52 |
error(WARNING, "empty output picture");
|
| 400 |
|
|
fprtresolu(0, 0, stdout);
|
| 401 |
|
|
return;
|
| 402 |
|
|
}
|
| 403 |
greg |
1.9 |
/* allocate scanlines */
|
| 404 |
greg |
1.1 |
for (i = 0; i <= psample; i++) {
|
| 405 |
greg |
2.8 |
scanbar[i] = (COLOR *)malloc(hres*sizeof(COLOR));
|
| 406 |
greg |
1.1 |
if (scanbar[i] == NULL)
|
| 407 |
greg |
1.11 |
goto memerr;
|
| 408 |
greg |
1.1 |
}
|
| 409 |
greg |
2.2 |
hstep = (psample*140+49)/99; /* quincunx sampling */
|
| 410 |
|
|
ystep = (psample*99+70)/140;
|
| 411 |
|
|
if (hstep > 2) {
|
| 412 |
greg |
2.8 |
i = hres/hstep + 2;
|
| 413 |
greg |
2.52 |
if ((sampdens = (char *)malloc(i)) == NULL)
|
| 414 |
greg |
2.2 |
goto memerr;
|
| 415 |
|
|
while (i--)
|
| 416 |
|
|
sampdens[i] = hstep;
|
| 417 |
|
|
} else
|
| 418 |
|
|
sampdens = NULL;
|
| 419 |
greg |
2.26 |
/* open z-file */
|
| 420 |
greg |
1.11 |
if (zfile != NULL) {
|
| 421 |
greg |
1.16 |
if ((zfd = open(zfile, O_WRONLY|O_CREAT, 0666)) == -1) {
|
| 422 |
greg |
2.26 |
sprintf(errmsg, "cannot open z-file \"%s\"", zfile);
|
| 423 |
greg |
1.11 |
error(SYSTEM, errmsg);
|
| 424 |
|
|
}
|
| 425 |
schorsch |
2.55 |
SET_FD_BINARY(zfd);
|
| 426 |
greg |
1.11 |
for (i = 0; i <= psample; i++) {
|
| 427 |
greg |
2.8 |
zbar[i] = (float *)malloc(hres*sizeof(float));
|
| 428 |
greg |
1.11 |
if (zbar[i] == NULL)
|
| 429 |
|
|
goto memerr;
|
| 430 |
|
|
}
|
| 431 |
|
|
} else {
|
| 432 |
greg |
1.16 |
zfd = -1;
|
| 433 |
greg |
1.11 |
for (i = 0; i <= psample; i++)
|
| 434 |
|
|
zbar[i] = NULL;
|
| 435 |
|
|
}
|
| 436 |
greg |
1.1 |
/* write out boundaries */
|
| 437 |
greg |
2.8 |
fprtresolu(hres, vres, stdout);
|
| 438 |
greg |
1.10 |
/* recover file and compute first */
|
| 439 |
greg |
1.11 |
i = salvage(oldfile);
|
| 440 |
greg |
2.42 |
if (i >= vres)
|
| 441 |
|
|
goto alldone;
|
| 442 |
greg |
2.85 |
if ((zfd != -1) & (i > 0) &&
|
| 443 |
greg |
2.64 |
lseek(zfd, (off_t)i*hres*sizeof(float), SEEK_SET) < 0)
|
| 444 |
greg |
2.26 |
error(SYSTEM, "z-file seek error in render");
|
| 445 |
greg |
2.8 |
pctdone = 100.0*i/vres;
|
| 446 |
greg |
1.24 |
if (ralrm > 0) /* report init stats */
|
| 447 |
schorsch |
2.69 |
report(0);
|
| 448 |
schorsch |
2.56 |
#ifdef SIGCONT
|
| 449 |
greg |
1.32 |
else
|
| 450 |
schorsch |
2.56 |
signal(SIGCONT, report);
|
| 451 |
greg |
1.32 |
#endif
|
| 452 |
greg |
2.47 |
ypos = vres-1 - i; /* initialize sampling */
|
| 453 |
gregl |
2.48 |
if (directvis)
|
| 454 |
|
|
init_drawsources(psample);
|
| 455 |
greg |
2.8 |
fillscanline(scanbar[0], zbar[0], sampdens, hres, ypos, hstep);
|
| 456 |
greg |
1.10 |
/* compute scanlines */
|
| 457 |
greg |
1.15 |
for (ypos -= ystep; ypos > -ystep; ypos -= ystep) {
|
| 458 |
|
|
/* bottom adjust? */
|
| 459 |
|
|
if (ypos < 0) {
|
| 460 |
|
|
ystep += ypos;
|
| 461 |
|
|
ypos = 0;
|
| 462 |
|
|
}
|
| 463 |
|
|
colptr = scanbar[ystep]; /* move base to top */
|
| 464 |
|
|
scanbar[ystep] = scanbar[0];
|
| 465 |
greg |
1.1 |
scanbar[0] = colptr;
|
| 466 |
greg |
1.15 |
zptr = zbar[ystep];
|
| 467 |
|
|
zbar[ystep] = zbar[0];
|
| 468 |
greg |
1.11 |
zbar[0] = zptr;
|
| 469 |
greg |
1.10 |
/* fill base line */
|
| 470 |
greg |
2.2 |
fillscanline(scanbar[0], zbar[0], sampdens,
|
| 471 |
greg |
2.8 |
hres, ypos, hstep);
|
| 472 |
greg |
1.10 |
/* fill bar */
|
| 473 |
greg |
2.8 |
fillscanbar(scanbar, zbar, hres, ypos, ystep);
|
| 474 |
gregl |
2.48 |
if (directvis) /* add bitty sources */
|
| 475 |
|
|
drawsources(scanbar, zbar, 0, hres, ypos, ystep);
|
| 476 |
greg |
1.10 |
/* write it out */
|
| 477 |
schorsch |
2.56 |
#ifdef SIGCONT
|
| 478 |
greg |
2.22 |
signal(SIGCONT, SIG_IGN); /* don't interrupt writes */
|
| 479 |
greg |
1.32 |
#endif
|
| 480 |
greg |
1.15 |
for (i = ystep; i > 0; i--) {
|
| 481 |
greg |
1.17 |
if (zfd != -1 && write(zfd, (char *)zbar[i],
|
| 482 |
greg |
2.8 |
hres*sizeof(float))
|
| 483 |
|
|
< hres*sizeof(float))
|
| 484 |
greg |
1.1 |
goto writerr;
|
| 485 |
greg |
2.8 |
if (fwritescan(scanbar[i], hres, stdout) < 0)
|
| 486 |
greg |
1.11 |
goto writerr;
|
| 487 |
|
|
}
|
| 488 |
greg |
1.1 |
if (fflush(stdout) == EOF)
|
| 489 |
|
|
goto writerr;
|
| 490 |
greg |
1.24 |
/* record progress */
|
| 491 |
greg |
2.8 |
pctdone = 100.0*(vres-1-ypos)/vres;
|
| 492 |
greg |
2.32 |
if (ralrm > 0 && time((time_t *)NULL) >= tlastrept+ralrm)
|
| 493 |
schorsch |
2.69 |
report(0);
|
| 494 |
schorsch |
2.56 |
#ifdef SIGCONT
|
| 495 |
greg |
1.32 |
else
|
| 496 |
greg |
2.22 |
signal(SIGCONT, report);
|
| 497 |
greg |
1.32 |
#endif
|
| 498 |
greg |
1.1 |
}
|
| 499 |
greg |
1.11 |
/* clean up */
|
| 500 |
schorsch |
2.56 |
#ifdef SIGCONT
|
| 501 |
greg |
2.22 |
signal(SIGCONT, SIG_IGN);
|
| 502 |
schorsch |
2.56 |
#endif
|
| 503 |
greg |
2.42 |
if (zfd != -1 && write(zfd, (char *)zbar[0], hres*sizeof(float))
|
| 504 |
|
|
< hres*sizeof(float))
|
| 505 |
|
|
goto writerr;
|
| 506 |
|
|
fwritescan(scanbar[0], hres, stdout);
|
| 507 |
|
|
if (fflush(stdout) == EOF)
|
| 508 |
|
|
goto writerr;
|
| 509 |
|
|
alldone:
|
| 510 |
greg |
1.16 |
if (zfd != -1) {
|
| 511 |
greg |
1.20 |
if (close(zfd) == -1)
|
| 512 |
|
|
goto writerr;
|
| 513 |
greg |
1.11 |
for (i = 0; i <= psample; i++)
|
| 514 |
greg |
2.52 |
free((void *)zbar[i]);
|
| 515 |
greg |
1.11 |
}
|
| 516 |
greg |
1.1 |
for (i = 0; i <= psample; i++)
|
| 517 |
greg |
2.52 |
free((void *)scanbar[i]);
|
| 518 |
greg |
2.2 |
if (sampdens != NULL)
|
| 519 |
|
|
free(sampdens);
|
| 520 |
greg |
1.11 |
pctdone = 100.0;
|
| 521 |
greg |
2.13 |
if (ralrm > 0)
|
| 522 |
schorsch |
2.69 |
report(0);
|
| 523 |
schorsch |
2.56 |
#ifdef SIGCONT
|
| 524 |
greg |
2.23 |
signal(SIGCONT, SIG_DFL);
|
| 525 |
schorsch |
2.56 |
#endif
|
| 526 |
greg |
1.1 |
return;
|
| 527 |
|
|
writerr:
|
| 528 |
|
|
error(SYSTEM, "write error in render");
|
| 529 |
greg |
1.11 |
memerr:
|
| 530 |
|
|
error(SYSTEM, "out of memory in render");
|
| 531 |
greg |
1.1 |
}
|
| 532 |
|
|
|
| 533 |
|
|
|
| 534 |
schorsch |
2.69 |
static void
|
| 535 |
|
|
fillscanline( /* fill scan at y */
|
| 536 |
greg |
2.85 |
COLOR *scanline,
|
| 537 |
|
|
float *zline,
|
| 538 |
|
|
char *sd,
|
| 539 |
schorsch |
2.69 |
int xres,
|
| 540 |
|
|
int y,
|
| 541 |
|
|
int xstep
|
| 542 |
|
|
)
|
| 543 |
greg |
1.1 |
{
|
| 544 |
greg |
1.33 |
static int nc = 0; /* number of calls */
|
| 545 |
greg |
2.2 |
int bl = xstep, b = xstep;
|
| 546 |
greg |
2.14 |
double z;
|
| 547 |
greg |
2.85 |
int i;
|
| 548 |
greg |
2.45 |
|
| 549 |
greg |
1.11 |
z = pixvalue(scanline[0], 0, y);
|
| 550 |
|
|
if (zline) zline[0] = z;
|
| 551 |
greg |
1.33 |
/* zig-zag start for quincunx pattern */
|
| 552 |
greg |
2.2 |
for (i = ++nc & 1 ? xstep : xstep/2; i < xres-1+xstep; i += xstep) {
|
| 553 |
greg |
1.15 |
if (i >= xres) {
|
| 554 |
|
|
xstep += xres-1-i;
|
| 555 |
|
|
i = xres-1;
|
| 556 |
|
|
}
|
| 557 |
greg |
1.11 |
z = pixvalue(scanline[i], i, y);
|
| 558 |
|
|
if (zline) zline[i] = z;
|
| 559 |
greg |
2.2 |
if (sd) b = sd[0] > sd[1] ? sd[0] : sd[1];
|
| 560 |
greg |
2.3 |
if (i <= xstep)
|
| 561 |
|
|
b = fillsample(scanline, zline, 0, y, i, 0, b/2);
|
| 562 |
|
|
else
|
| 563 |
|
|
b = fillsample(scanline+i-xstep,
|
| 564 |
greg |
2.38 |
zline ? zline+i-xstep : (float *)NULL,
|
| 565 |
greg |
2.3 |
i-xstep, y, xstep, 0, b/2);
|
| 566 |
greg |
2.2 |
if (sd) *sd++ = nc & 1 ? bl : b;
|
| 567 |
|
|
bl = b;
|
| 568 |
greg |
1.1 |
}
|
| 569 |
greg |
2.2 |
if (sd && nc & 1) *sd = bl;
|
| 570 |
greg |
1.1 |
}
|
| 571 |
|
|
|
| 572 |
|
|
|
| 573 |
schorsch |
2.69 |
static void
|
| 574 |
|
|
fillscanbar( /* fill interior */
|
| 575 |
greg |
2.85 |
COLOR *scanbar[],
|
| 576 |
|
|
float *zbar[],
|
| 577 |
schorsch |
2.69 |
int xres,
|
| 578 |
|
|
int y,
|
| 579 |
|
|
int ysize
|
| 580 |
|
|
)
|
| 581 |
greg |
1.1 |
{
|
| 582 |
|
|
COLOR vline[MAXDIV+1];
|
| 583 |
greg |
1.11 |
float zline[MAXDIV+1];
|
| 584 |
greg |
1.1 |
int b = ysize;
|
| 585 |
greg |
2.85 |
int i, j;
|
| 586 |
greg |
2.45 |
|
| 587 |
greg |
1.8 |
for (i = 0; i < xres; i++) {
|
| 588 |
greg |
1.1 |
copycolor(vline[0], scanbar[0][i]);
|
| 589 |
|
|
copycolor(vline[ysize], scanbar[ysize][i]);
|
| 590 |
greg |
1.11 |
if (zbar[0]) {
|
| 591 |
|
|
zline[0] = zbar[0][i];
|
| 592 |
|
|
zline[ysize] = zbar[ysize][i];
|
| 593 |
|
|
}
|
| 594 |
greg |
2.38 |
b = fillsample(vline, zbar[0] ? zline : (float *)NULL,
|
| 595 |
greg |
1.11 |
i, y, 0, ysize, b/2);
|
| 596 |
greg |
2.45 |
|
| 597 |
greg |
1.1 |
for (j = 1; j < ysize; j++)
|
| 598 |
|
|
copycolor(scanbar[j][i], vline[j]);
|
| 599 |
greg |
1.11 |
if (zbar[0])
|
| 600 |
|
|
for (j = 1; j < ysize; j++)
|
| 601 |
|
|
zbar[j][i] = zline[j];
|
| 602 |
greg |
1.1 |
}
|
| 603 |
|
|
}
|
| 604 |
|
|
|
| 605 |
|
|
|
| 606 |
schorsch |
2.69 |
static int
|
| 607 |
|
|
fillsample( /* fill interior points */
|
| 608 |
greg |
2.85 |
COLOR *colline,
|
| 609 |
|
|
float *zline,
|
| 610 |
schorsch |
2.69 |
int x,
|
| 611 |
|
|
int y,
|
| 612 |
|
|
int xlen,
|
| 613 |
|
|
int ylen,
|
| 614 |
|
|
int b
|
| 615 |
|
|
)
|
| 616 |
greg |
1.1 |
{
|
| 617 |
greg |
2.14 |
double ratio;
|
| 618 |
|
|
double z;
|
| 619 |
greg |
1.1 |
COLOR ctmp;
|
| 620 |
|
|
int ncut;
|
| 621 |
greg |
2.85 |
int len;
|
| 622 |
greg |
2.45 |
|
| 623 |
greg |
1.1 |
if (xlen > 0) /* x or y length is zero */
|
| 624 |
|
|
len = xlen;
|
| 625 |
|
|
else
|
| 626 |
|
|
len = ylen;
|
| 627 |
greg |
2.45 |
|
| 628 |
greg |
1.1 |
if (len <= 1) /* limit recursion */
|
| 629 |
|
|
return(0);
|
| 630 |
greg |
2.45 |
|
| 631 |
|
|
if (b > 0 ||
|
| 632 |
|
|
(zline && 2.*fabs(zline[0]-zline[len]) > maxdiff*(zline[0]+zline[len]))
|
| 633 |
greg |
1.11 |
|| bigdiff(colline[0], colline[len], maxdiff)) {
|
| 634 |
greg |
2.45 |
|
| 635 |
greg |
1.11 |
z = pixvalue(colline[len>>1], x + (xlen>>1), y + (ylen>>1));
|
| 636 |
|
|
if (zline) zline[len>>1] = z;
|
| 637 |
greg |
1.1 |
ncut = 1;
|
| 638 |
|
|
} else { /* interpolate */
|
| 639 |
|
|
copycolor(colline[len>>1], colline[len]);
|
| 640 |
|
|
ratio = (double)(len>>1) / len;
|
| 641 |
|
|
scalecolor(colline[len>>1], ratio);
|
| 642 |
greg |
1.11 |
if (zline) zline[len>>1] = zline[len] * ratio;
|
| 643 |
|
|
ratio = 1.0 - ratio;
|
| 644 |
greg |
1.1 |
copycolor(ctmp, colline[0]);
|
| 645 |
|
|
scalecolor(ctmp, ratio);
|
| 646 |
|
|
addcolor(colline[len>>1], ctmp);
|
| 647 |
greg |
1.11 |
if (zline) zline[len>>1] += zline[0] * ratio;
|
| 648 |
greg |
1.1 |
ncut = 0;
|
| 649 |
|
|
}
|
| 650 |
|
|
/* recurse */
|
| 651 |
greg |
1.11 |
ncut += fillsample(colline, zline, x, y, xlen>>1, ylen>>1, (b-1)/2);
|
| 652 |
greg |
2.45 |
|
| 653 |
greg |
2.38 |
ncut += fillsample(colline+(len>>1),
|
| 654 |
|
|
zline ? zline+(len>>1) : (float *)NULL,
|
| 655 |
greg |
1.11 |
x+(xlen>>1), y+(ylen>>1),
|
| 656 |
|
|
xlen-(xlen>>1), ylen-(ylen>>1), b/2);
|
| 657 |
greg |
1.1 |
|
| 658 |
|
|
return(ncut);
|
| 659 |
|
|
}
|
| 660 |
|
|
|
| 661 |
|
|
|
| 662 |
schorsch |
2.69 |
static double
|
| 663 |
|
|
pixvalue( /* compute pixel value */
|
| 664 |
|
|
COLOR col, /* returned color */
|
| 665 |
|
|
int x, /* pixel position */
|
| 666 |
|
|
int y
|
| 667 |
|
|
)
|
| 668 |
greg |
1.1 |
{
|
| 669 |
greg |
2.99 |
static COLORMAT xyz2myrgbmat;
|
| 670 |
gwlarson |
2.49 |
RAY thisray;
|
| 671 |
|
|
FVECT lorg, ldir;
|
| 672 |
greg |
2.97 |
double hpos, vpos, lmax;
|
| 673 |
greg |
2.85 |
int i;
|
| 674 |
gwlarson |
2.49 |
/* compute view ray */
|
| 675 |
greg |
2.85 |
setcolor(col, 0.0, 0.0, 0.0);
|
| 676 |
gwlarson |
2.49 |
hpos = (x+pixjitter())/hres;
|
| 677 |
|
|
vpos = (y+pixjitter())/vres;
|
| 678 |
|
|
if ((thisray.rmax = viewray(thisray.rorg, thisray.rdir,
|
| 679 |
greg |
2.85 |
&ourview, hpos, vpos)) < -FTINY)
|
| 680 |
greg |
1.22 |
return(0.0);
|
| 681 |
greg |
2.77 |
/* set pixel index */
|
| 682 |
|
|
samplendx = pixnumber(x,y,hres,vres);
|
| 683 |
gwlarson |
2.49 |
/* optional motion blur */
|
| 684 |
|
|
if (lastview.type && mblur > FTINY && (lmax = viewray(lorg, ldir,
|
| 685 |
|
|
&lastview, hpos, vpos)) >= -FTINY) {
|
| 686 |
greg |
2.85 |
double d = mblur*(.5-urand(281+samplendx));
|
| 687 |
gwlarson |
2.49 |
|
| 688 |
|
|
thisray.rmax = (1.-d)*thisray.rmax + d*lmax;
|
| 689 |
|
|
for (i = 3; i--; ) {
|
| 690 |
|
|
thisray.rorg[i] = (1.-d)*thisray.rorg[i] + d*lorg[i];
|
| 691 |
|
|
thisray.rdir[i] = (1.-d)*thisray.rdir[i] + d*ldir[i];
|
| 692 |
|
|
}
|
| 693 |
|
|
if (normalize(thisray.rdir) == 0.0)
|
| 694 |
|
|
return(0.0);
|
| 695 |
|
|
}
|
| 696 |
greg |
2.97 |
/* depth-of-field */
|
| 697 |
|
|
if (!jitteraperture(thisray.rorg, thisray.rdir, &ourview, dblur))
|
| 698 |
|
|
return(0.0);
|
| 699 |
gwlarson |
2.49 |
|
| 700 |
greg |
2.75 |
rayorigin(&thisray, PRIMARY, NULL, NULL);
|
| 701 |
greg |
1.25 |
|
| 702 |
greg |
1.1 |
rayvalue(&thisray); /* trace ray */
|
| 703 |
|
|
|
| 704 |
greg |
2.99 |
if (out_prims == stdprims) { /* return color */
|
| 705 |
|
|
scolor_rgb(col, thisray.rcol);
|
| 706 |
|
|
} else if (out_prims == xyzprims) {
|
| 707 |
|
|
scolor_cie(col, thisray.rcol);
|
| 708 |
|
|
scalecolor(col, WHTEFFICACY);
|
| 709 |
|
|
} else if (NCSAMP > 3) {
|
| 710 |
|
|
COLOR xyz;
|
| 711 |
|
|
if (xyz2myrgbmat[0][0] == 0)
|
| 712 |
|
|
compxyz2rgbWBmat(xyz2myrgbmat, out_prims);
|
| 713 |
|
|
scolor_cie(xyz, thisray.rcol);
|
| 714 |
|
|
colortrans(col, xyz2myrgbmat, xyz);
|
| 715 |
|
|
clipgamut(col, xyz[CIEY], CGAMUT_LOWER, cblack, cwhite);
|
| 716 |
|
|
} else
|
| 717 |
|
|
copycolor(col, thisray.rcol);
|
| 718 |
greg |
2.45 |
|
| 719 |
greg |
2.91 |
return(raydistance(&thisray)); /* return distance */
|
| 720 |
greg |
1.1 |
}
|
| 721 |
|
|
|
| 722 |
|
|
|
| 723 |
schorsch |
2.69 |
static int
|
| 724 |
|
|
salvage( /* salvage scanlines from killed program */
|
| 725 |
|
|
char *oldfile
|
| 726 |
|
|
)
|
| 727 |
greg |
1.1 |
{
|
| 728 |
|
|
COLR *scanline;
|
| 729 |
|
|
FILE *fp;
|
| 730 |
|
|
int x, y;
|
| 731 |
|
|
|
| 732 |
|
|
if (oldfile == NULL)
|
| 733 |
greg |
2.37 |
goto gotzip;
|
| 734 |
|
|
|
| 735 |
greg |
1.9 |
if ((fp = fopen(oldfile, "r")) == NULL) {
|
| 736 |
greg |
1.1 |
sprintf(errmsg, "cannot open recover file \"%s\"", oldfile);
|
| 737 |
|
|
error(WARNING, errmsg);
|
| 738 |
greg |
2.37 |
goto gotzip;
|
| 739 |
greg |
1.1 |
}
|
| 740 |
schorsch |
2.55 |
SET_FILE_BINARY(fp);
|
| 741 |
greg |
1.1 |
/* discard header */
|
| 742 |
greg |
2.19 |
getheader(fp, NULL, NULL);
|
| 743 |
greg |
1.1 |
/* get picture size */
|
| 744 |
greg |
1.36 |
if (!fscnresolu(&x, &y, fp)) {
|
| 745 |
greg |
2.29 |
sprintf(errmsg, "bad recover file \"%s\" - not removed",
|
| 746 |
|
|
oldfile);
|
| 747 |
greg |
1.2 |
error(WARNING, errmsg);
|
| 748 |
greg |
1.1 |
fclose(fp);
|
| 749 |
greg |
2.37 |
goto gotzip;
|
| 750 |
greg |
1.1 |
}
|
| 751 |
|
|
|
| 752 |
greg |
2.85 |
if ((x != hres) | (y != vres)) {
|
| 753 |
greg |
1.1 |
sprintf(errmsg, "resolution mismatch in recover file \"%s\"",
|
| 754 |
|
|
oldfile);
|
| 755 |
|
|
error(USER, errmsg);
|
| 756 |
|
|
}
|
| 757 |
|
|
|
| 758 |
greg |
2.8 |
scanline = (COLR *)malloc(hres*sizeof(COLR));
|
| 759 |
greg |
1.1 |
if (scanline == NULL)
|
| 760 |
|
|
error(SYSTEM, "out of memory in salvage");
|
| 761 |
greg |
2.8 |
for (y = 0; y < vres; y++) {
|
| 762 |
|
|
if (freadcolrs(scanline, hres, fp) < 0)
|
| 763 |
greg |
1.1 |
break;
|
| 764 |
greg |
2.8 |
if (fwritecolrs(scanline, hres, stdout) < 0)
|
| 765 |
greg |
1.1 |
goto writerr;
|
| 766 |
|
|
}
|
| 767 |
|
|
if (fflush(stdout) == EOF)
|
| 768 |
|
|
goto writerr;
|
| 769 |
greg |
2.52 |
free((void *)scanline);
|
| 770 |
greg |
1.1 |
fclose(fp);
|
| 771 |
|
|
unlink(oldfile);
|
| 772 |
|
|
return(y);
|
| 773 |
greg |
2.37 |
gotzip:
|
| 774 |
|
|
if (fflush(stdout) == EOF)
|
| 775 |
|
|
error(SYSTEM, "error writing picture header");
|
| 776 |
|
|
return(0);
|
| 777 |
greg |
1.1 |
writerr:
|
| 778 |
greg |
2.9 |
sprintf(errmsg, "write error during recovery of \"%s\"", oldfile);
|
| 779 |
|
|
error(SYSTEM, errmsg);
|
| 780 |
schorsch |
2.69 |
return -1; /* pro forma return */
|
| 781 |
greg |
1.31 |
}
|
| 782 |
|
|
|
| 783 |
schorsch |
2.69 |
static int
|
| 784 |
greg |
2.87 |
pixnumber( /* compute pixel index (screen door) */
|
| 785 |
greg |
2.85 |
int x,
|
| 786 |
|
|
int y,
|
| 787 |
schorsch |
2.69 |
int xres,
|
| 788 |
|
|
int yres
|
| 789 |
|
|
)
|
| 790 |
greg |
1.31 |
{
|
| 791 |
greg |
2.87 |
unsigned nbits = 0;
|
| 792 |
|
|
bitmask_t coord[2];
|
| 793 |
|
|
|
| 794 |
|
|
if (xres < yres) xres = yres;
|
| 795 |
|
|
while (xres > 0) {
|
| 796 |
|
|
xres >>= 1;
|
| 797 |
|
|
++nbits;
|
| 798 |
|
|
}
|
| 799 |
|
|
coord[0] = x; coord[1] = y;
|
| 800 |
|
|
return ((int)hilbert_c2i(2, nbits, coord));
|
| 801 |
greg |
1.1 |
}
|