I changed your program to draw a colored ramp,
default yellow, but there are options -r (red), -g (green), -b (blue).
When writing raw bytes in preparation for loadimage,
you must always prepare them in little-endian order,
even on big-endian machines. That's just the definition
(it coincides with what most VGA cards do).
So if you are using ARGB32 then you write B, G, R, A,
in that order.
Russ
#include <u.h>
#include <libc.h>
#include <draw.h>
#include <event.h>
enum {
B = 0,
G = 1,
R = 2,
A = 3,
};
static Image *image = nil;
enum{
Border = 2,
Edge = 5
};
void
eresized (int new) {
Rectangle r;
if (new && getwindow (display, Refnone) < 0) {
sysfatal ("png: can't reattach to window");
}
if(image == nil)
return;
r = insetrect (screen->clipr, Edge + Border);
r.max.x = r.min.x + Dx (image->r);
r.max.y = r.min.y + Dy (image->r);
border (screen, r, -Border, nil, ZP);
draw (screen, r, image, nil, image->r.min);
flushimage (display, 1);
}
int
blend(int a, int b, int p, int q)
{
/* f = p/q; return a*(1-f) + b*f */
return (a*(q-p)+b*p)/q;
}
void
main (int argc, char **argv) {
Rectangle r;
Image *m, *m2;
int ch, size;
char pixel[4];
unsigned long outchan = ARGB32;
unsigned char *buf, *bp;
int x, y, rgb;
rgb = 0xFFFF00; /* yellow */
ARGBEGIN{
case 'r':
rgb = 0xFF0000;
break;
case 'g':
rgb = 0x00FF00;
break;
case 'b':
rgb = 0x0000FF;
break;
}ARGEND
r.min = Pt (0, 0);
r.max = Pt (160, 160);
size = Dx (r) * Dy(r) * sizeof (pixel);
bp = buf = (unsigned char *) malloc (size);
for(y=0; y<Dy(r); y++){
bp = buf+y*bytesperline(r, chantodepth(outchan));
for(x=0; x<Dx(r); x++){
*bp++ = blend(rgb, 255, x, Dx(r)); /* B */
*bp++ = blend(rgb>>8, 255, x, Dx(r)); /* G */
*bp++ = blend(rgb>>16, 255, x, Dx(r)); /* R */
*bp++ = 0xFF; /* A */
}
}
initdraw (0, 0, 0);
einit (Ekeyboard | Emouse);
if ((m = allocimage (display, r, outchan, 1, 0)) == 0) {
sysfatal ("No image");
}
if (loadimage (m, m->r, buf, size) < 0) {
sysfatal ("Image load failed: %r");
}
if ((m2 = allocimage (display, r, outchan, 1, 0)) == 0) {
sysfatal ("No backup image");
}
draw (m2, r, display->white, nil, ZP);
draw (m2, m2->r, m, nil, m->r.min);
image = m2;
eresized(0);
if ((ch = ekbd()) == 'q' || ch == 0x7F || ch == 0x04)
return;
draw (screen, screen->clipr, display->white, nil, ZP);
image = nil;
freeimage (m);
}