2011-11-01 00:39:52 +00:00
|
|
|
#include <u.h>
|
|
|
|
#include <libc.h>
|
|
|
|
#include <draw.h>
|
|
|
|
#include <event.h>
|
|
|
|
|
2012-03-02 04:54:45 +00:00
|
|
|
#define NCOLORS 6
|
|
|
|
|
|
|
|
Image *colors[NCOLORS];
|
|
|
|
|
2011-11-01 00:39:52 +00:00
|
|
|
void
|
|
|
|
eresized(int)
|
|
|
|
{
|
|
|
|
if(getwindow(display, Refnone) < 0)
|
|
|
|
sysfatal("resize failed");
|
|
|
|
}
|
|
|
|
|
2012-02-29 00:47:26 +00:00
|
|
|
int
|
2012-02-28 02:15:37 +00:00
|
|
|
loadimg(char *name)
|
|
|
|
{
|
|
|
|
Image *b;
|
|
|
|
int fd;
|
|
|
|
|
2012-02-29 00:47:26 +00:00
|
|
|
if((fd = open(name, OREAD)) < 0)
|
|
|
|
return -1;
|
2012-03-02 04:54:45 +00:00
|
|
|
if((b = readimage(display, fd, 0)) == nil){
|
2012-02-29 00:47:26 +00:00
|
|
|
close(fd);
|
2012-03-02 04:54:45 +00:00
|
|
|
return -1;
|
2012-02-29 00:47:26 +00:00
|
|
|
}
|
2012-03-02 04:54:45 +00:00
|
|
|
draw(screen, screen->r, b, 0, b->r.min);
|
|
|
|
flushimage(display, 1);
|
|
|
|
freeimage(b);
|
|
|
|
close(fd);
|
|
|
|
return 0;
|
2012-02-28 02:15:37 +00:00
|
|
|
}
|
|
|
|
|
2012-02-29 00:47:26 +00:00
|
|
|
int
|
|
|
|
saveimg(char *name)
|
2011-11-01 00:39:52 +00:00
|
|
|
{
|
|
|
|
int fd;
|
|
|
|
|
2012-02-29 00:47:26 +00:00
|
|
|
if((fd = create(name, OWRITE|OTRUNC, 0666)) < 0)
|
|
|
|
return -1;
|
|
|
|
writeimage(fd, screen, 0);
|
2011-11-01 00:39:52 +00:00
|
|
|
close(fd);
|
2012-03-02 04:54:45 +00:00
|
|
|
return 0;
|
2011-11-01 00:39:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2012-02-28 02:15:37 +00:00
|
|
|
main(int argc, char *argv[])
|
2011-11-01 00:39:52 +00:00
|
|
|
{
|
|
|
|
Event e;
|
|
|
|
Point last;
|
2012-03-02 04:54:45 +00:00
|
|
|
int b = 1;
|
|
|
|
int c = 0;
|
|
|
|
int cn, f;
|
|
|
|
int haslast = 0;
|
2011-12-10 03:18:20 +00:00
|
|
|
char brush[128];
|
2012-03-02 04:54:45 +00:00
|
|
|
char color[NCOLORS];
|
2011-11-01 00:39:52 +00:00
|
|
|
char file[128];
|
2012-03-02 04:54:45 +00:00
|
|
|
char fill[NCOLORS];
|
2011-11-01 00:39:52 +00:00
|
|
|
|
2011-12-10 22:01:15 +00:00
|
|
|
if(initdraw(0, 0, "paint") < 0){
|
|
|
|
fprint(2, "paint: initdraw failed: %r\n");
|
|
|
|
exits("initdraw");
|
|
|
|
}
|
2011-11-01 00:39:52 +00:00
|
|
|
einit(Emouse | Ekeyboard);
|
|
|
|
draw(screen, screen->r, display->white, 0, ZP);
|
|
|
|
flushimage(display, 1);
|
2012-02-28 02:15:37 +00:00
|
|
|
|
2012-03-02 04:54:45 +00:00
|
|
|
colors[0] = display->black;
|
|
|
|
colors[1] = display->white;
|
|
|
|
colors[2] = allocimage(display, Rect(0,0,1,1), CMAP8, 1, DRed);
|
|
|
|
colors[3] = allocimage(display, Rect(0,0,1,1), CMAP8, 1, DGreen);
|
|
|
|
colors[4] = allocimage(display, Rect(0,0,1,1), CMAP8, 1, DBlue);
|
|
|
|
colors[5] = allocimage(display, Rect(0,0,1,1), CMAP8, 1, DYellow);
|
|
|
|
|
2012-02-28 02:15:37 +00:00
|
|
|
ARGBEGIN{
|
|
|
|
default:
|
|
|
|
goto Usage;
|
|
|
|
}ARGEND
|
|
|
|
switch(argc){
|
|
|
|
default:
|
|
|
|
Usage:
|
|
|
|
fprint(2, "Usage: [file]\n");
|
|
|
|
exits("usage");
|
|
|
|
case 0:
|
|
|
|
break;
|
|
|
|
case 1:
|
2012-07-09 22:05:59 +00:00
|
|
|
strncpy(file, argv[0], sizeof(file)-1);
|
2012-03-01 14:32:05 +00:00
|
|
|
if(loadimg(file) < 0)
|
2012-02-29 00:47:26 +00:00
|
|
|
sysfatal("%r");
|
2012-02-28 02:15:37 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2011-11-01 00:39:52 +00:00
|
|
|
while(1){
|
|
|
|
switch(event(&e)){
|
|
|
|
case Emouse:
|
|
|
|
if(e.mouse.buttons & 1){
|
|
|
|
if(haslast)
|
2012-03-02 04:54:45 +00:00
|
|
|
line(screen, last, e.mouse.xy, Enddisc, Enddisc, b, colors[c], ZP);
|
2011-11-01 00:39:52 +00:00
|
|
|
else
|
2012-03-02 04:54:45 +00:00
|
|
|
fillellipse(screen, e.mouse.xy, b, b, colors[c], ZP);
|
2011-11-01 00:39:52 +00:00
|
|
|
last = e.mouse.xy;
|
|
|
|
haslast = 1;
|
|
|
|
flushimage(display, 1);
|
|
|
|
} else
|
|
|
|
haslast = 0;
|
|
|
|
break;
|
|
|
|
case Ekeyboard:
|
2011-12-10 03:18:20 +00:00
|
|
|
if(e.kbdc == 'b'){
|
|
|
|
if(eenter("Brush", brush, sizeof(brush), &e.mouse) <= 0)
|
|
|
|
break;
|
2012-03-02 04:54:45 +00:00
|
|
|
b = atoi(brush);
|
|
|
|
}
|
|
|
|
if(e.kbdc == 'c'){
|
|
|
|
if(eenter("Color", color, sizeof(color), &e.mouse) <= 0)
|
|
|
|
break;
|
|
|
|
cn = atoi(color);
|
|
|
|
if(cn >= 0 && cn < NCOLORS)
|
|
|
|
c = cn;
|
|
|
|
}
|
|
|
|
if(e.kbdc == 'f'){
|
|
|
|
if(eenter("Fill", fill, sizeof(fill), &e.mouse) <= 0)
|
|
|
|
break;
|
|
|
|
f = atoi(fill);
|
|
|
|
if(f >= 0 && f < NCOLORS)
|
|
|
|
draw(screen, screen->r, colors[f], 0, ZP);
|
2011-12-10 03:18:20 +00:00
|
|
|
}
|
2012-02-28 02:15:37 +00:00
|
|
|
if(e.kbdc == 'o'){
|
|
|
|
if(eenter("Open file", file, sizeof(file), &e.mouse) <= 0)
|
|
|
|
break;
|
2012-02-29 00:47:26 +00:00
|
|
|
if(loadimg(file) < 0){
|
|
|
|
rerrstr(file, sizeof(file));
|
|
|
|
eenter(file, 0, 0, &e.mouse);
|
|
|
|
}
|
2012-02-28 02:15:37 +00:00
|
|
|
}
|
2011-12-10 03:18:20 +00:00
|
|
|
if(e.kbdc == 'q')
|
|
|
|
exits(nil);
|
2011-11-01 00:39:52 +00:00
|
|
|
if(e.kbdc == 's'){
|
|
|
|
if(eenter("Save to", file, sizeof(file), &e.mouse) <= 0)
|
|
|
|
break;
|
2012-02-29 00:47:26 +00:00
|
|
|
if(saveimg(file) < 0){
|
|
|
|
rerrstr(file, sizeof(file));
|
|
|
|
eenter(file, 0, 0, &e.mouse);
|
|
|
|
}
|
2011-11-01 00:39:52 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|