91 |
|
static int |
92 |
|
binary_copyf(FILE *fp, int asize) |
93 |
|
{ |
94 |
+ |
const int skip_thresh = 1024; |
95 |
|
const size_t elsiz = asize*ncomp; |
96 |
|
const int width = scanlen(&res); |
97 |
< |
char *buf = (char *)malloc(elsiz*width); |
97 |
> |
const long skip_len = (width-ncols)*elsiz; |
98 |
> |
char *buf; |
99 |
|
int y; |
100 |
< |
|
101 |
< |
if (!buf) { |
102 |
< |
fputs(progname, stderr); |
103 |
< |
fputs(": out of memory!\n", stderr); |
104 |
< |
return(0); |
105 |
< |
} |
100 |
> |
/* check if fseek() useful */ |
101 |
> |
if (skip_len > skip_thresh && |
102 |
> |
fseek(fp, (rmin*width + cmin)*elsiz, SEEK_CUR) == 0) { |
103 |
> |
buf = (char *)malloc(ncols*elsiz); |
104 |
> |
if (!buf) |
105 |
> |
goto memerr; |
106 |
> |
for (y = nrows; y-- > 0; ) { |
107 |
> |
if (getbinary(buf, elsiz, ncols, fp) != ncols) |
108 |
> |
goto readerr; |
109 |
> |
if (putbinary(buf, elsiz, ncols, stdout) != ncols) |
110 |
> |
goto writerr; |
111 |
> |
if (y && fseek(fp, skip_len, SEEK_CUR) < 0) { |
112 |
> |
fputs(progname, stderr); |
113 |
> |
fputs(": unexpected seek error on input\n", stderr); |
114 |
> |
return(0); |
115 |
> |
} |
116 |
> |
} |
117 |
> |
free(buf); /* success! */ |
118 |
> |
return(1); |
119 |
> |
} /* else need to read it all... */ |
120 |
> |
buf = (char *)malloc(width*elsiz); |
121 |
> |
if (!buf) |
122 |
> |
goto memerr; |
123 |
|
/* skip rows as requested */ |
124 |
< |
if (rmin && fseek(fp, rmin*width*elsiz, SEEK_CUR) < 0) { |
124 |
> |
if (skip_len > skip_thresh || |
125 |
> |
(rmin && fseek(fp, rmin*width*elsiz, SEEK_CUR) < 0)) |
126 |
|
for (y = 0; y < rmin; y++) |
127 |
|
if (getbinary(buf, elsiz, width, fp) != width) |
128 |
|
goto readerr; |
109 |
– |
} |
129 |
|
for (y = 0; y < nrows; y++) { /* copy portion */ |
130 |
|
if (getbinary(buf, elsiz, width, fp) != width) |
131 |
|
goto readerr; |
142 |
|
readerr: |
143 |
|
fputs(progname, stderr); |
144 |
|
fputs(": error reading binary data\n", stderr); |
145 |
+ |
return(0); |
146 |
+ |
memerr: |
147 |
+ |
fputs(progname, stderr); |
148 |
+ |
fputs(": out of memory!\n", stderr); |
149 |
|
return(0); |
150 |
|
} |
151 |
|
|