#include <draw.h>
#include <memdraw.h>
+int nflag;
+
static void
resample(Memimage *dst, Rectangle r, Memimage *src, Rectangle sr)
{
for(dp.y=0; dp.y<=dsize.y; dp.y++){
sp.y = _sp.y>>12;
ty = _sp.y&0xFFF;
+ if(nflag)
+ ty = ty << 1 & 0x1000;
pdst = pdst0;
sp.x = sr.min.x;
psrc0 = byteaddr(src, sp);
for(dp.x=0; dp.x<=dsize.x; dp.x++){
sp.x = _sp.x>>12;
tx = _sp.x&0xFFF;
+ if(nflag)
+ tx = tx << 1 & 0x1000;
psrc = psrc0 + sp.x*bpp;
s00 = (0x1000-tx)*(0x1000-ty);
s01 = tx*(0x1000-ty);
}
}
+enum {
+ PERCENT = 0x80000000,
+};
+
+static int
+getsize(char *s)
+{
+ int v;
+
+ v = strtol(s, &s, 10) & ~PERCENT;
+ if(*s == '%')
+ v |= PERCENT;
+ return v;
+}
+
void
usage(void)
{
- sysfatal("Usage: %s [ -x width ] [ -y height ] [image]\n", argv0);
+ sysfatal("Usage: %s [ -x width ] [ -y height ] [ file ]\n", argv0);
}
void
int fd, xsize, ysize;
Memimage *im, *nim;
ulong ochan, tchan;
- char buf[12];
xsize = ysize = 0;
ARGBEGIN{
+ case 'a':
+ xsize = ysize = getsize(EARGF(usage()));
+ break;
case 'x':
- xsize = atoi(EARGF(usage()));
+ xsize = getsize(EARGF(usage()));
break;
case 'y':
- ysize = atoi(EARGF(usage()));
+ ysize = getsize(EARGF(usage()));
+ break;
+ case 'n':
+ nflag++;
break;
default:
usage();
memimageinit();
if((im = readmemimage(fd)) == nil)
sysfatal("readmemimage: %r");
+ if(xsize & PERCENT)
+ xsize = ((xsize & ~PERCENT) * Dx(im->r)) / 100;
+ if(ysize & PERCENT)
+ ysize = ((ysize & ~PERCENT) * Dy(im->r)) / 100;
if(xsize || ysize){
if(ysize == 0)
ysize = (xsize * Dy(im->r)) / Dx(im->r);
ochan = im->chan;
switch(ochan){
default:
- sysfatal("can't handle channel type %s", chantostr(buf, ochan));
+ for(tchan = ochan; tchan; tchan >>= 8)
+ if(TYPE(tchan) == CAlpha){
+ tchan = RGBA32;
+ break;
+ }
+ if(tchan == 0)
+ tchan = RGB24;
+ break;
case GREY8:
case RGB24:
case RGBA32:
case XRGB32:
tchan = ochan;
break;
- case CMAP8:
- case RGB16:
- case RGB15:
- tchan = RGB24;
- break;
case GREY1:
case GREY2:
case GREY4:
freememimage(im);
im = nim;
}
- if((nim = allocmemimage(Rect(im->r.min.x, im->r.min.y, xsize, ysize), tchan)) == nil)
- sysfatal("addocmemimage: %r");
+ if((nim = allocmemimage(
+ Rect(im->r.min.x, im->r.min.y, im->r.min.x+xsize, im->r.min.y+ysize),
+ tchan)) == nil)
+ sysfatal("allocmemimage: %r");
resample(nim, nim->r, im, im->r);
freememimage(im);
im = nim;