18 |
|
|
19 |
|
#include <signal.h> |
20 |
|
|
21 |
+ |
#include <setjmp.h> |
22 |
+ |
|
23 |
|
#include <ctype.h> |
24 |
|
|
25 |
< |
VIEW ourview = STDVIEW(470); /* viewing parameters */ |
25 |
> |
VIEW ourview = STDVIEW; /* viewing parameters */ |
26 |
> |
int hresolu, vresolu; /* image resolution */ |
27 |
|
|
28 |
|
int psample = 8; /* pixel sample size */ |
29 |
|
double maxdiff = .15; /* max. sample difference */ |
47 |
|
int ambincl = -1; /* include == 1, exclude == 0 */ |
48 |
|
|
49 |
|
int greyscale = 0; /* map colors to brightness? */ |
50 |
< |
char *devname = "X"; /* output device name */ |
50 |
> |
char *devname = dev_default; /* output device name */ |
51 |
|
|
52 |
|
struct driver *dev = NULL; /* driver functions */ |
53 |
|
|
57 |
|
RECT pframe; /* current frame boundaries */ |
58 |
|
int pdepth; /* image depth in current frame */ |
59 |
|
|
60 |
+ |
static jmp_buf mainloop; /* longjmp back to main loop */ |
61 |
+ |
static char *reserve_mem = NULL; /* pre-allocated reserve memory */ |
62 |
+ |
|
63 |
+ |
#define RESERVE_AMT 8192 /* amount of memory to reserve */ |
64 |
+ |
|
65 |
|
#define CTRL(c) ('c'-'@') |
66 |
|
|
67 |
|
|
120 |
|
|
121 |
|
devopen(devname); /* open device */ |
122 |
|
newimage(); /* set up image */ |
123 |
< |
|
123 |
> |
setjmp(mainloop); |
124 |
|
for ( ; ; ) { /* quit in command() */ |
125 |
< |
while (ourview.hresolu <= 1<<pdepth && |
126 |
< |
ourview.vresolu <= 1<<pdepth) |
125 |
> |
while (hresolu <= 1<<pdepth && |
126 |
> |
vresolu <= 1<<pdepth) |
127 |
|
command("done: "); |
128 |
|
|
129 |
< |
if (ourview.hresolu <= psample<<pdepth && |
130 |
< |
ourview.vresolu <= psample<<pdepth) { |
129 |
> |
if (hresolu <= psample<<pdepth && |
130 |
> |
vresolu <= psample<<pdepth) { |
131 |
|
sprintf(buf, "%d sampling...\n", 1<<pdepth); |
132 |
|
(*dev->comout)(buf); |
133 |
|
rsample(); |
134 |
|
} else { |
135 |
|
sprintf(buf, "%d refining...\n", 1<<pdepth); |
136 |
|
(*dev->comout)(buf); |
137 |
< |
refine(&ptrunk, 0, 0, ourview.hresolu, |
130 |
< |
ourview.vresolu, pdepth+1); |
137 |
> |
refine(&ptrunk, 0, 0, hresolu, vresolu, pdepth+1); |
138 |
|
} |
139 |
|
if (dev->inpready) |
140 |
|
command(": "); |
144 |
|
} |
145 |
|
|
146 |
|
|
147 |
+ |
memreserve() /* fill memory reserves */ |
148 |
+ |
{ |
149 |
+ |
if (reserve_mem != NULL) |
150 |
+ |
return; /* got some already */ |
151 |
+ |
reserve_mem = malloc(RESERVE_AMT); |
152 |
+ |
} |
153 |
+ |
|
154 |
+ |
|
155 |
+ |
memerror(detail) /* try and rescue a memory error */ |
156 |
+ |
char *detail; |
157 |
+ |
{ |
158 |
+ |
if (reserve_mem == NULL) { |
159 |
+ |
sprintf(errmsg, "out of memory %s", detail); |
160 |
+ |
error(SYSTEM, errmsg); |
161 |
+ |
} |
162 |
+ |
free(reserve_mem); |
163 |
+ |
reserve_mem = NULL; |
164 |
+ |
do |
165 |
+ |
command("out of memory: "); |
166 |
+ |
while (reserve_mem == NULL); |
167 |
+ |
longjmp(mainloop, 1); |
168 |
+ |
} |
169 |
+ |
|
170 |
+ |
|
171 |
|
command(prompt) /* get/execute command */ |
172 |
|
char *prompt; |
173 |
|
{ |
176 |
|
char inpbuf[256]; |
177 |
|
char *args; |
178 |
|
again: |
179 |
< |
(*dev->comout)(prompt); /* get command + arguments */ |
149 |
< |
(*dev->comin)(inpbuf); |
179 |
> |
(*dev->comin)(inpbuf, prompt); /* get command + arguments */ |
180 |
|
for (args = inpbuf; *args && *args != ' '; args++) |
181 |
|
; |
182 |
|
if (*args) *args++ = '\0'; |
296 |
|
* difference, we subsample the super-pixels. The testing process |
297 |
|
* includes initialization of the next row. |
298 |
|
*/ |
299 |
< |
xsiz = (((pframe.r-pframe.l)<<pdepth)+ourview.hresolu-1) / |
300 |
< |
ourview.hresolu; |
271 |
< |
ysiz = (((pframe.u-pframe.d)<<pdepth)+ourview.vresolu-1) / |
272 |
< |
ourview.vresolu; |
299 |
> |
xsiz = (((pframe.r-pframe.l)<<pdepth)+hresolu-1) / hresolu; |
300 |
> |
ysiz = (((pframe.u-pframe.d)<<pdepth)+vresolu-1) / vresolu; |
301 |
|
rl = (RECT *)malloc(xsiz*sizeof(RECT)); |
302 |
+ |
if (rl == NULL) |
303 |
+ |
memerror("in rsample"); |
304 |
|
pl = (PNODE **)malloc(xsiz*sizeof(PNODE *)); |
305 |
< |
if (rl == NULL || pl == NULL) |
306 |
< |
error(SYSTEM, "out of memory in rsample"); |
305 |
> |
if (pl == NULL) |
306 |
> |
memerror("in rsample"); |
307 |
|
/* |
308 |
|
* Initialize the bottom row. |
309 |
|
*/ |
310 |
|
rl[0].l = rl[0].d = 0; |
311 |
< |
rl[0].r = ourview.hresolu; rl[0].u = ourview.vresolu; |
311 |
> |
rl[0].r = hresolu; rl[0].u = vresolu; |
312 |
|
pl[0] = findrect(pframe.l, pframe.d, &ptrunk, rl, pdepth); |
313 |
|
for (x = 1; x < xsiz; x++) { |
314 |
|
rl[x].l = rl[x].d = 0; |
315 |
< |
rl[x].r = ourview.hresolu; rl[x].u = ourview.vresolu; |
316 |
< |
pl[x] = findrect(pframe.l+((x*ourview.hresolu)>>pdepth), |
315 |
> |
rl[x].r = hresolu; rl[x].u = vresolu; |
316 |
> |
pl[x] = findrect(pframe.l+((x*hresolu)>>pdepth), |
317 |
|
pframe.d, &ptrunk, rl+x, pdepth); |
318 |
|
} |
319 |
|
/* sample the image */ |
341 |
|
* Find super-pixel at this position in next row. |
342 |
|
*/ |
343 |
|
r.l = r.d = 0; |
344 |
< |
r.r = ourview.hresolu; r.u = ourview.vresolu; |
345 |
< |
p = findrect(pframe.l+((x*ourview.hresolu)>>pdepth), |
346 |
< |
pframe.d+(((y+1)*ourview.vresolu)>>pdepth), |
344 |
> |
r.r = hresolu; r.u = vresolu; |
345 |
> |
p = findrect(pframe.l+((x*hresolu)>>pdepth), |
346 |
> |
pframe.d+(((y+1)*vresolu)>>pdepth), |
347 |
|
&ptrunk, &r, pdepth); |
348 |
|
/* |
349 |
|
* Test super-pixel in next row. |
390 |
|
if (p->kid == NULL) { /* subdivide */ |
391 |
|
|
392 |
|
if ((p->kid = newptree()) == NULL) |
393 |
< |
error(SYSTEM, "out of memory in refine"); |
393 |
> |
memerror("in refine"); |
394 |
|
/* |
395 |
|
* The following paint order can leave a black pixel |
396 |
|
* when redraw() is called in (*dev->paintr)(). |