9 resample(Memimage *dst, Rectangle r, Memimage *src, Rectangle sr)
14 uchar *pdst0, *pdst, *psrc0, *psrc;
15 ulong s00, s01, s10, s11;
18 ssize = subpt(subpt(sr.max, sr.min), Pt(1,1));
19 dsize = subpt(subpt(r.max, r.min), Pt(1,1));
20 pdst0 = byteaddr(dst, r.min);
22 bpl = src->width*sizeof(int);
26 qp.x = (ssize.x<<12)/dsize.x;
28 qp.y = (ssize.y<<12)/dsize.y;
31 for(dp.y=0; dp.y<=dsize.y; dp.y++){
35 ty = ty << 1 & 0x1000;
38 psrc0 = byteaddr(src, sp);
40 for(dp.x=0; dp.x<=dsize.x; dp.x++){
44 tx = tx << 1 & 0x1000;
45 psrc = psrc0 + sp.x*bpp;
46 s00 = (0x1000-tx)*(0x1000-ty);
52 pdst[3] = (s11*psrc[bpl+bpp+3] +
57 pdst[2] = (s11*psrc[bpl+bpp+2] +
61 pdst[1] = (s11*psrc[bpl+bpp+1] +
66 pdst[0] = (s11*psrc[bpl+bpp] +
74 pdst0 += dst->width*sizeof(int);
88 v = strtol(s, &s, 10) & ~PERCENT;
97 sysfatal("Usage: %s [ -x width ] [ -y height ] [ file ]\n", argv0);
101 main(int argc, char **argv)
103 int fd, xsize, ysize;
110 xsize = ysize = getsize(EARGF(usage()));
113 xsize = getsize(EARGF(usage()));
116 ysize = getsize(EARGF(usage()));
126 fd = open(*argv, OREAD);
128 sysfatal("open: %r");
131 if((im = readmemimage(fd)) == nil)
132 sysfatal("readmemimage: %r");
134 xsize = ((xsize & ~PERCENT) * Dx(im->r)) / 100;
136 ysize = ((ysize & ~PERCENT) * Dy(im->r)) / 100;
139 ysize = (xsize * Dy(im->r)) / Dx(im->r);
141 xsize = (ysize * Dx(im->r)) / Dy(im->r);
145 for(tchan = ochan; tchan; tchan >>= 8)
146 if(TYPE(tchan) == CAlpha){
167 if((nim = allocmemimage(im->r, tchan)) == nil)
168 sysfatal("allocimage: %r");
169 memimagedraw(nim, nim->r, im, im->r.min, nil, ZP, S);
173 if((nim = allocmemimage(
174 Rect(im->r.min.x, im->r.min.y, im->r.min.x+xsize, im->r.min.y+ysize),
176 sysfatal("allocmemimage: %r");
177 resample(nim, nim->r, im, im->r);
181 if((im = allocmemimage(nim->r, ochan)) == nil)
182 sysfatal("allocimage: %r");
183 memimagedraw(im, im->r, nim, nim->r.min, nil, ZP, S);
187 if(writememimage(1, im) < 0)
188 sysfatal("writememimage: %r");