cscale.c
2.03 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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
/*
* cscale -
* Multiply or divide the colors in an image by a color
* vector.
*
* Paul Haeberli - 1988
*/
#include "image.h"
#include "math.h"
short rbuf[8192];
short gbuf[8192];
short bbuf[8192];
int divide;
int balance;
main(argc,argv)
int argc;
char **argv;
{
IMAGE *iimage, *oimage;
unsigned int xsize, ysize, zsize;
unsigned int y, z;
int r, g, b;
int max;
if(argc<6) {
fprintf(stderr,"usage: cscale inimage.rgb outimage.rgb r g b [-d] [-b]\n");
exit(1);
}
r = atoi(argv[3]);
g = atoi(argv[4]);
b = atoi(argv[5]);
if(argc>6) {
switch(argv[6][1]) {
case 'd':
divide = 1;
break;
case 'b':
max = r;
if(g>max)
max = g;
if(b<max)
max = b;
r = ((r*255)/max)+0.5;
g = ((g*255)/max)+0.5;
b = ((b*255)/max)+0.5;
divide = 1;
break;
}
}
if( (iimage=iopen(argv[1],"r")) == NULL ) {
fprintf(stderr,"%s: can't open input file %s\n",argv[0],argv[1]);
exit(1);
}
xsize = iimage->xsize;
ysize = iimage->ysize;
zsize = iimage->zsize;
oimage = iopen(argv[2],"w",RLE(1),3,xsize,ysize,3);
for(y=0; y<ysize; y++) {
if(zsize>=3) {
getrow(iimage,rbuf,y,0);
getrow(iimage,gbuf,y,1);
getrow(iimage,bbuf,y,2);
} else {
getrow(iimage,rbuf,y,0);
getrow(iimage,gbuf,y,0);
getrow(iimage,bbuf,y,0);
}
cscale(rbuf,gbuf,bbuf,xsize,r,g,b,divide);
putrow(oimage,rbuf,y,0);
putrow(oimage,gbuf,y,1);
putrow(oimage,bbuf,y,2);
}
iclose(oimage);
exit(0);
}
cscale(rbuf,gbuf,bbuf,n,r,g,b,div)
register unsigned short *rbuf, *gbuf, *bbuf;
register int n, r, g, b, div;
{
if(div) {
while(n--) {
*rbuf = (*rbuf * 255)/r;
*gbuf = (*gbuf * 255)/g;
*bbuf = (*bbuf * 255)/b;
if(*rbuf>255)
*rbuf = 255;
if(*gbuf>255)
*gbuf = 255;
if(*bbuf>255)
*bbuf = 255;
rbuf++;
gbuf++;
bbuf++;
}
} else {
while(n--) {
*rbuf = (*rbuf * r)/255;
*gbuf = (*gbuf * g)/255;
*bbuf = (*bbuf * b)/255;
rbuf++;
gbuf++;
bbuf++;
}
}
}