subimg.c
1.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
/*
* subimg -
* Extract a region from an image.
*
* Paul Haeberli - 1984
*
*/
#include "image.h"
short row[8192];
main(argc,argv)
int argc;
char **argv;
{
register IMAGE *iimage, *oimage;
register int iy, iz, oy;
int xsize, ysize;
int temp;
int xmin, xmax;
int ymin, ymax;
if( argc<7 ) {
fprintf(stderr,
"usage: subimg inimage outimage x1 x2 y1 y2\n");
exit(1);
}
if( (iimage=iopen(argv[1],"r")) == NULL ) {
fprintf(stderr,"subimg: can't open input file %s\n",argv[1]);
exit(1);
}
xmin = atoi(argv[3]);
xmax = atoi(argv[4]);
ymin = atoi(argv[5]);
ymax = atoi(argv[6]);
xmin = negmap(xmin,iimage->xsize);
xmax = negmap(xmax,iimage->xsize);
ymin = negmap(ymin,iimage->ysize);
ymax = negmap(ymax,iimage->ysize);
if(xmin>xmax) {
temp = xmax;
xmax = xmin;
xmin = temp;
}
if(ymin>ymax) {
temp = ymax;
ymax = ymin;
ymin = temp;
}
xsize = xmax-xmin+1;
ysize = ymax-ymin+1;
if(xsize>iimage->xsize) {
fprintf(stderr,"subimg: xsize too big\n");
exit(1);
}
if(ysize>iimage->ysize) {
fprintf(stderr,"subimg: ysize too big\n");
exit(1);
}
oimage = iopen(argv[2],"w",iimage->type,iimage->dim,
xsize,ysize,iimage->zsize);
isetname(oimage,iimage->name);
oimage->colormap = iimage->colormap;
for(iz=0; iz<iimage->zsize; iz++) {
oy = 0;
for(iy=ymin; iy<=ymax; iy++) {
getrow(iimage,row,iy,iz);
putrow(oimage,row+xmin,oy++,iz);
}
}
iclose(oimage);
exit(0);
}
negmap(val,size)
int val, size;
{
if(val<0)
return size-1+val;
else
return val;
}