9fans - fans of the OS Plan 9 from Bell Labs
 help / color / mirror / Atom feed
* [9fans] just pipe your img thru this ;-)
@ 2002-07-01  7:42 Peter A. Cejchan
  0 siblings, 0 replies; only message in thread
From: Peter A. Cejchan @ 2002-07-01  7:42 UTC (permalink / raw)
  To: 9fans


Hi all,
my first attempt to write sthg in native code ... just wanted to tell it to sb.
Comments more than welcome!!!
Cheers,
-- 
++pac.

Peter A. Cejchan
Paleobiology Lab, GLU Acad. Sci. CZ
<pac@next.gli.cas.cz>
[http | ftp]://next.gli.cas.cz

--OgqxwSJOaUobr8KG
Content-Type: text/x-csrc; charset=us-ascii
Content-Disposition: attachment; filename="cnl.c"

// cnl.c
// extracts red (green, blue) channel from an image
// first step to RG anaglyphs (stereo images)
// heavily, and in a dirty(?) way, reuses code from resample.c
// sorry,
// Tue Jun 25 14:04:30 EDT 2002  <pac@next.gli.cas.cz>


#include <u.h>
#include <libc.h>
#include <draw.h>
#include <memdraw.h>

#define ALPHA 3
#define RED 2
#define GREEN 1
#define BLUE 0

int color=RED;	// default channel to extract

void
usage(void)
{
	fprint(2, "usage: cnl [-r||-g|-b] [imagefile]\n");
	fprint(2, "           jpg -c testimg.jpg | cnl -r  | page  \n");
	exits("usage");
}

void
extractchannel(uchar **in, int off, int iny, uchar **out, int outy, int n)
{
	int y, i, k, z;	
	z=off-n*(off/n);
	switch(n){
	case 3:
		for(y=0; y<outy; y++){
			if (z == color)
				out[y][off] = in[y][off];
			else
				out[y][off] =  0;
		}
		break;
	case 4:
		for(y=0; y<outy; y++){
			if ((z == color) || (z == ALPHA))
				out[y][off] = in[y][off];
			else
				out[y][off] =  0;
		}
		break;
	default:
		sysfatal("can't handle  %d-channel images", n);	
	}
}

int
max(int a, int b)
{
	if(a > b)
		return a;
	return b;
}

Memimage*
resample(int xsize, int ysize, Memimage *m)
{
	int i, j, bpl, nchan;
	Memimage *new;
	uchar **oscan, **nscan;

	new = allocmemimage(Rect(0, 0, xsize, ysize), m->chan);
	if(new == nil)
		sysfatal("can't allocate new image: %r");

	oscan = malloc(Dy(m->r)*sizeof(uchar*));
	nscan = malloc(max(ysize, Dy(m->r))*sizeof(uchar*));
	if(oscan == nil || nscan == nil)
		sysfatal("can't allocate: %r");

	/* unload original image into scan lines */
	bpl = bytesperline(m->r, m->depth);
	for(i=0; i<Dy(m->r); i++){
		oscan[i] = malloc(bpl);
		if(oscan[i] == nil)
			sysfatal("can't allocate: %r");
		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);
		if(j != bpl)
			sysfatal("unloadmemimage");
	}

	/* allocate scan lines for destination. we do y first, so need at least Dy(m->r) lines */
	bpl = bytesperline(Rect(0, 0, xsize, Dy(m->r)), m->depth);
	for(i=0; i<max(ysize, Dy(m->r)); i++){
		nscan[i] = malloc(bpl);
		if(nscan[i] == nil)
			sysfatal("can't allocate: %r");
	}

	nchan = m->depth/8;

	/* extract channel in Y direction*/
	for(i=0; i<xsize; i++)
		for(j=0; j<nchan; j++)
			extractchannel(oscan, nchan*i+j, Dy(m->r), nscan, ysize, nchan);

	/* pack data into destination */
	bpl = bytesperline(new->r, m->depth);
	for(i=0; i<ysize; i++){
		j = loadmemimage(new, Rect(0, i, xsize, i+1), nscan[i], bpl);
		if(j != bpl)
			sysfatal("loadmemimage: %r");
	}
	return new;
}

void
main(int argc, char *argv[])
{
	int i, fd, xsize, ysize;
	Rectangle rparam;
	Memimage *m, *new, *t1, *t2;
	char *file;
	ulong tchan;
	char tmp[100];
	double v;

	memimageinit();
	memset(&rparam, 0, sizeof rparam);
	xsize = ysize = 0;

	ARGBEGIN{
	case 'r':
		color=RED;
		break;
	case 'g':
		color=GREEN;
		break;
	case 'b':
		color=BLUE;
		break;
	default:
		exits("usage");
	}ARGEND

	file = "<stdin>";
	fd = 0;
	if(argc > 1)
		usage();
	else if(argc == 1){
		file = argv[0];
		fd = open(file, OREAD);
		if(fd < 0)
			sysfatal("can't open %s: %r", file);
	}

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

	xsize = Dx(m->r);
	ysize = Dy(m->r);

	new = nil;
	switch(m->chan){

	case RGB24:
	case RGBA32:
	case ARGB32:
	case XRGB32:
		new = resample(xsize, ysize, m);
		break;

	case GREY1:
	case GREY2:
	case GREY4:
	case GREY8:
	case CMAP8:
	case RGB15:
	case RGB16:
		tchan = RGB24;
		goto Convert;

	Convert:
		/* use library to convert to byte-per-chan form, then convert back */
		t1 = allocmemimage(m->r, tchan);
		if(t1 == nil)
			sysfatal("can't allocate temporary image: %r");
		memimagedraw(t1, t1->r, m, m->r.min, nil, ZP);
		t2 = resample(xsize, ysize, t1);
		freememimage(t1);
		new = allocmemimage(Rect(0, 0, xsize, ysize), m->chan);
		if(new == nil)
			sysfatal("can't allocate new image: %r");
		/* should do error diffusion here */
		memimagedraw(new, new->r, t2, t2->r.min, nil, ZP);
		freememimage(t2);
		break;

	default:
		sysfatal("can't handle channel type %s", chantostr(tmp, m->chan));
	}

	assert(new);
	if(writememimage(1, new) < 0)
		sysfatal("write error on output: %r");

	exits(nil);
}

--OgqxwSJOaUobr8KG--

----- End forwarded message -----

-- 
++pac.

Peter A. Cejchan
Paleobiology Lab, GLU Acad. Sci. CZ
<pac@next.gli.cas.cz>
[http | ftp]://next.gli.cas.cz

----- End forwarded message -----

-- 
++pac.

Peter A. Cejchan
Paleobiology Lab, GLU Acad. Sci. CZ
<pac@next.gli.cas.cz>
[http | ftp]://next.gli.cas.cz


^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2002-07-01  7:42 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2002-07-01  7:42 [9fans] just pipe your img thru this ;-) Peter A. Cejchan

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).