6 #define K2 7 /* from -.7 to +.7 inclusive, meaning .2 into each adjacent pixel */
22 * i0(x) is the modified Bessel function, Σ (x/2)^2L / (L!)²
23 * There are faster ways to calculate this, but we precompute
24 * into a table so let's keep it simple.
34 v += pow(x/2., 2*L)/pow(fac(L), 2);
39 kaiser(double x, double τ, double α)
43 return i0(α*sqrt(1-(x*x/(τ*τ))))/i0(α);
49 fprint(2, "usage: resample [-x xsize] [-y ysize] [imagefile]\n");
50 fprint(2, "\twhere size is an integer or a percentage in the form 25%%\n");
55 getint(char *s, int *percent)
67 resamplex(uchar *in, int off, int d, int inx, uchar *out, int outx)
73 rat = (double)inx/(double)outx;
74 for(x=0; x<outx; x++){
76 /* don't resample if size unchanged */
77 out[off+x*d] = in[off+x*d];
82 for(k=-K2; k<=K2; k++){
89 v += in[off+i*d] * K[K2+k];
96 resampley(uchar **in, int off, int iny, uchar **out, int outy)
101 rat = (double)iny/(double)outy;
102 for(y=0; y<outy; y++){
104 /* don't resample if size unchanged */
105 out[y][off] = in[y][off];
110 for(k=-K2; k<=K2; k++){
117 v += in[i][off] * K[K2+k];
133 resample(int xsize, int ysize, Memimage *m)
135 int i, j, d, bpl, nchan;
137 uchar **oscan, **nscan;
139 new = allocmemimage(Rect(0, 0, xsize, ysize), m->chan);
141 sysfatal("can't allocate new image: %r");
143 oscan = malloc(Dy(m->r)*sizeof(uchar*));
144 nscan = malloc(max(ysize, Dy(m->r))*sizeof(uchar*));
145 if(oscan == nil || nscan == nil)
146 sysfatal("can't allocate: %r");
148 /* unload original image into scan lines */
149 bpl = bytesperline(m->r, m->depth);
150 for(i=0; i<Dy(m->r); i++){
151 oscan[i] = malloc(bpl);
153 sysfatal("can't allocate: %r");
154 j = unloadmemimage(m, Rect(m->r.min.x, m->r.min.y+i, m->r.max.x, m->r.min.y+i+1), oscan[i], bpl);
156 sysfatal("unloadmemimage");
159 /* allocate scan lines for destination. we do y first, so need at least Dy(m->r) lines */
160 bpl = bytesperline(Rect(0, 0, xsize, Dy(m->r)), m->depth);
161 for(i=0; i<max(ysize, Dy(m->r)); i++){
162 nscan[i] = malloc(bpl);
164 sysfatal("can't allocate: %r");
168 nchan = d = m->depth/8;
169 if(m->chan == XRGB32)
171 for(i=0; i<Dy(m->r); i++){
172 for(j=0; j<nchan; j++)
173 resamplex(oscan[i], j, d, Dx(m->r), nscan[i], xsize);
176 nscan[i] = malloc(bpl);
178 sysfatal("can't allocate: %r");
182 for(i=0; i<xsize; i++)
183 for(j=0; j<nchan; j++)
184 resampley(oscan, d*i+j, Dy(m->r), nscan, ysize);
186 /* pack data into destination */
187 bpl = bytesperline(new->r, m->depth);
188 for(i=0; i<ysize; i++){
189 j = loadmemimage(new, Rect(0, i, xsize, i+1), nscan[i], bpl);
191 sysfatal("loadmemimage: %r");
197 main(int argc, char *argv[])
199 int i, fd, xsize, ysize, xpercent, ypercent;
201 Memimage *m, *new, *t1, *t2;
206 for(i=-K2; i<=K2; i++){
207 K[K2+i] = kaiser(i/10., K2/10., 4.);
208 // print("%g %g\n", i/10., K[K2+i]);
219 memset(&rparam, 0, sizeof rparam);
221 xpercent = ypercent = 0;
224 case 'a': /* compatibility; equivalent to just -x or -y */
225 if(xsize != 0 || ysize != 0)
227 xsize = getint(ARGF(), &xpercent);
236 xsize = getint(ARGF(), &xpercent);
243 ysize = getint(ARGF(), &ypercent);
251 if(xsize == 0 && ysize == 0)
260 fd = open(file, OREAD);
262 sysfatal("can't open %s: %r", file);
265 m = readmemimage(fd);
267 sysfatal("can't read %s: %r", file);
270 xsize = Dx(m->r)*xsize/100;
272 ysize = Dy(m->r)*ysize/100;
274 ysize = (xsize * Dy(m->r)) / Dx(m->r);
276 xsize = (ysize * Dx(m->r)) / Dy(m->r);
280 for(tchan = m->chan; tchan; tchan >>= 8)
281 if(TYPE(tchan) == CAlpha){
293 new = resample(xsize, ysize, m);
301 /* use library to convert to byte-per-chan form, then convert back */
302 t1 = allocmemimage(m->r, tchan);
304 sysfatal("can't allocate temporary image: %r");
305 memimagedraw(t1, t1->r, m, m->r.min, nil, ZP, S);
306 t2 = resample(xsize, ysize, t1);
308 new = allocmemimage(Rect(0, 0, xsize, ysize), m->chan);
310 sysfatal("can't allocate new image: %r");
311 /* should do error diffusion here */
312 memimagedraw(new, new->r, t2, t2->r.min, nil, ZP, S);
318 if(writememimage(1, new) < 0)
319 sysfatal("write error on output: %r");