From mboxrd@z Thu Jan 1 00:00:00 1970 To: 9fans@cse.psu.edu Message-ID: <20020701094230.B6596@next.gli.cas.cz> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline User-Agent: Mutt/1.2.5i From: "Peter A. Cejchan" Subject: [9fans] just pipe your img thru this ;-) Date: Mon, 1 Jul 2002 09:42:30 +0200 Topicbox-Message-UUID: bee69af6-eaca-11e9-9e20-41e7f4b1d025 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 [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 #include #include #include #include #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 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; ir); 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; ir)); 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; ir), nscan, ysize, nchan); /* pack data into destination */ bpl = bytesperline(new->r, m->depth); for(i=0; i 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 [http | ftp]://next.gli.cas.cz ----- End forwarded message ----- -- ++pac. Peter A. Cejchan Paleobiology Lab, GLU Acad. Sci. CZ [http | ftp]://next.gli.cas.cz