#include <u.h>
#include <libc.h>
#include <thread.h>
#include <draw.h>
#include <mouse.h>
#include <keyboard.h>
#include <bio.h>
#include <tiff.h>
static int dopen;
static int idraw;
static Mousectl *m;
ulong
tiffchan(Tiff *t){
int c[3]; // b g r
int n;
IFD *i;
char d[25];
i = lookifdptr(t, Tbitspersample);
switch(lookifd(t, Tphotometric)){
case Prgb:
if(!i)
return rflag ? BGR24 : RGB24;
for(n = 0; n < 3; n++)
c[n] = ifdidx(i, n);
if(rflag)
snprint(d, sizeof d, "b%dg%dr%d", c[0], c[1], c[2]);
else
snprint(d, sizeof d, "r%dg%db%d", c[0], c[1], c[2]);
return strtochan(d);
case Pblackzero:
snprint(d, sizeof d, "k%uld", ifdidx(i, 0));
return strtochan(d);
default:
sysfatal("bad photometric interpretation");
}
return 0;
}
char *
tiffchantostr(Tiff *t, char *s)
{
return chantostr(s, tiffchan(t));
}
void
dodraw(Tiff *tf)
{
static Tiff *t;
static Image *i;
static Rectangle r;
if(tf){
t = tf;
freeimage(i);
r = Rpt(ZP, Pt(lookifd(t, Twidth), lookifd(t, Tlength)));
i = allocimage(display, r, tiffchan(t), 0, DBlack);
if(i == 0)
sysfatal("allocimage: %r");
if(loadimage(i, r, t->rawimg, t->rawimge-t->rawimg) == -1){
fprint(2, "loadimage: %r %ld : bpl = %d ; n = %d\n", t->rawimge-t->rawimg,
bytesperline(r, i->depth), Dy(r)*bytesperline(r, i->depth));
}
}
if(!t)
return;
draw(screen, rectaddpt(r, screen->r.min), i, 0, ZP);
}
void
resizeproc(void *)
{
int d0;
for(d0 = dopen; d0 == dopen && recv(m->resizec, 0) >= 0;){
if(getwindow(display, Refnone) < 0){
fprint(2, "test: can't reattach to window\n");
threadexitsall("resize");
}
dodraw(0);
flushimage(display, 1);
}
threadexits("");
}
static void
drawe(Display*, char *e)
{
fprint(2, "%s\n", e);
threadexitsall("libdraw");
}
int
init(void)
{
if(idraw++ == 0 && initdraw(drawe, 0, 0) < 0){
fprint(2, "test: initdraw failed: %r");
return -1;
}
m = initmouse(0, screen);
dopen++;
return 0;
}
void
cleanup(void)
{
dopen++;
closemouse(m);
// closedisplay(display);
}
void
kbdproc(void*)
{
Keyboardctl *k;
Rune r;
k = initkeyboard(0);
if(k == 0)
return;
if(recv(k->c, &r) >= 0 && r == L'q')
threadexitsall("");
closekeyboard(k);
// threadexits("");
}
void
mouseproc(void*)
{
for(int d0 = dopen; d0 == dopen && readmouse(m) >= 0;)
;
threadexits("");
}
void
dirty(Tiff *t)
{
if(init() < 0)
exits("initdraw");
dodraw(t);
threadcreate(mouseproc, 0, 8192);
threadcreate(resizeproc, 0, 8192);
kbdproc(0);
cleanup();
}
|