Exemplo n.º 1
0
void
main(int argc, char *argv[])
{
	Memimage *m;
	int fd, r;
	char f;

	f = 0;
	r = 0;
	fd = 0;
	ARGBEGIN {
	case 'u':
		f = 'u';
		break;
	case 'l':
		f = 'l';
		break;
	case 'r':
		r = atoi(EARGF(usage()));
		break;
	default:
		usage();
	} ARGEND;
	if(*argv){
		fd = open(*argv, OREAD);
		if(fd < 0)
			sysfatal("open: %r");
	}
	memimageinit();
	if((m = readmemimage(fd)) == nil)
		sysfatal("readmemimage: %r");
	if(f == 'u' || f == 'l'){
		m = upsidedown(m);
		if(f == 'l')
			r = 180;
	}
	switch(r % 360){
	case 270:
		m = rot90(m);
	case 180:
		m = rot90(m);
	case 90:
		m = rot90(m);
		break;
	}
	if(writememimage(1, m) < 0)
		sysfatal("writememimage: %r");
	exits(0);
}
Exemplo n.º 2
0
void
main(int argc, char **argv)
{
	int fd, xsize, ysize;
	Memimage *im, *nim;
	ulong ochan, tchan;

	xsize = ysize = 0;
	ARGBEGIN{
	case 'a':
		xsize = ysize = getsize(EARGF(usage()));
		break;
	case 'x':
		xsize = getsize(EARGF(usage()));
		break;
	case 'y':
		ysize = getsize(EARGF(usage()));
		break;
	default:
		usage();
	}ARGEND
	fd = 0;
	if(*argv){
		fd = open(*argv, OREAD);
		if(fd < 0)
			sysfatal("open: %r");
	}
	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);
		if(xsize == 0)
			xsize = (ysize * Dx(im->r)) / Dy(im->r);
		ochan = im->chan;
		switch(ochan){
		default:
			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 ARGB32:
		case XRGB32:
			tchan = ochan;
			break;
		case GREY1:
		case GREY2:
		case GREY4:
			tchan = GREY8;
			break;
		}
		if(tchan != ochan){
			if((nim = allocmemimage(im->r, tchan)) == nil)
				sysfatal("allocimage: %r");
			memimagedraw(nim, nim->r, im, im->r.min, nil, ZP, S);
			freememimage(im);
			im = nim;
		}
		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;
		if(tchan != ochan){
			if((im = allocmemimage(nim->r, ochan)) == nil)
				sysfatal("allocimage: %r");
			memimagedraw(im, im->r, nim, nim->r.min, nil, ZP, S);
			freememimage(nim);
		}
	}
	if(writememimage(1, im) < 0)
		sysfatal("writememimage: %r");
	exits(0);
}
Exemplo n.º 3
0
void
main(int argc, char *argv[])
{
	char *tostr, *file;
	int fd, uncompressed;
	ulong tochan;
	Memimage *m, *n;

	tostr = nil;
	uncompressed = 0;
	ARGBEGIN{
	case 'c':
		tostr = EARGF(usage());
		break;
	case 'u':
		uncompressed = 1;
		break;
	default:
		usage();
	}ARGEND

	memimageinit();

	file = "<stdin>";
	m = nil;

	switch(argc){
	case 0:
		m = readmemimage(0);
		break;
	case 1:
		file = argv[0];
		fd = open(file, OREAD);
		if(fd < 0)
			sysfatal("can't open %s: %r", file);
		m = readmemimage(fd);
		close(fd);
		break;
	default:
		usage();
	}

	if(m == nil)
		sysfatal("can't read %s: %r", file);

	if(tostr == nil)
		tochan = m->chan;
	else{
		tochan = strtochan(tostr);
		if(tochan == 0)
			sysfatal("bad channel descriptor '%s'", tostr);
	}

	n = allocmemimage(m->r, tochan);
	if(n == nil)
		sysfatal("can't allocate new image: %r");

	memimagedraw(n, n->r, m, m->r.min, nil, ZP, S);
	if(uncompressed)
		writeuncompressed(1, n);
	else
		writememimage(1, n);
	exits(nil);
}