From: "Peter A. Cejchan" <pac@next.gli.cas.cz>
To: 9fans@cse.psu.edu
Subject: [9fans] just pipe your img thru this ;-)
Date: Mon, 1 Jul 2002 09:42:30 +0200 [thread overview]
Message-ID: <20020701094230.B6596@next.gli.cas.cz> (raw)
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
reply other threads:[~2002-07-01 7:42 UTC|newest]
Thread overview: [no followups] expand[flat|nested] mbox.gz Atom feed
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20020701094230.B6596@next.gli.cas.cz \
--to=pac@next.gli.cas.cz \
--cc=9fans@cse.psu.edu \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
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).