Blob


1 #include <u.h>
2 #include <libc.h>
3 #include <draw.h>
4 #include <memdraw.h>
6 int drawdebug;
7 static int tablesbuilt;
9 /* perfect approximation to NTSC = .299r+.587g+.114b when 0 ≤ r,g,b < 256 */
10 #define RGB2K(r,g,b) ((156763*(r)+307758*(g)+59769*(b))>>19)
12 /*
13 * for 0 ≤ x ≤ 255*255, (x*0x0101+0x100)>>16 is a perfect approximation.
14 * for 0 ≤ x < (1<<16), x/255 = ((x+1)*0x0101)>>16 is a perfect approximation.
15 * the last one is perfect for all up to 1<<16, avoids a multiply, but requires a rathole.
16 */
17 /* #define DIV255(x) (((x)*257+256)>>16) */
18 #define DIV255(x) ((((x)+1)*257)>>16)
19 /* #define DIV255(x) (tmp=(x)+1, (tmp+(tmp>>8))>>8) */
21 #define MUL(x, y, t) (t = (x)*(y)+128, (t+(t>>8))>>8)
22 #define MASK13 0xFF00FF00
23 #define MASK02 0x00FF00FF
24 #define MUL13(a, x, t) (t = (a)*(((x)&MASK13)>>8)+128, ((t+((t>>8)&MASK02))>>8)&MASK02)
25 #define MUL02(a, x, t) (t = (a)*(((x)&MASK02)>>0)+128, ((t+((t>>8)&MASK02))>>8)&MASK02)
26 #define MUL0123(a, x, s, t) ((MUL13(a, x, s)<<8)|MUL02(a, x, t))
28 #define MUL2(u, v, x, y) (t = (u)*(v)+(x)*(y)+256, (t+(t>>8))>>8)
30 static void mktables(void);
31 typedef int Subdraw(Memdrawparam*);
32 static Subdraw chardraw, alphadraw, memoptdraw;
34 static Memimage* memones;
35 static Memimage* memzeros;
36 Memimage *memwhite;
37 Memimage *memblack;
38 Memimage *memtransparent;
39 Memimage *memopaque;
41 int __ifmt(Fmt*);
43 void
44 memimageinit(void)
45 {
46 static int didinit = 0;
48 if(didinit)
49 return;
51 didinit = 1;
53 mktables();
54 _memmkcmap();
56 fmtinstall('R', Rfmt);
57 fmtinstall('P', Pfmt);
58 fmtinstall('b', __ifmt);
60 memones = allocmemimage(Rect(0,0,1,1), GREY1);
61 memones->flags |= Frepl;
62 memones->clipr = Rect(-0x3FFFFFF, -0x3FFFFFF, 0x3FFFFFF, 0x3FFFFFF);
63 *byteaddr(memones, ZP) = ~0;
65 memzeros = allocmemimage(Rect(0,0,1,1), GREY1);
66 memzeros->flags |= Frepl;
67 memzeros->clipr = Rect(-0x3FFFFFF, -0x3FFFFFF, 0x3FFFFFF, 0x3FFFFFF);
68 *byteaddr(memzeros, ZP) = 0;
70 if(memones == nil || memzeros == nil)
71 assert(0 /*cannot initialize memimage library */); /* RSC BUG */
73 memwhite = memones;
74 memblack = memzeros;
75 memopaque = memones;
76 memtransparent = memzeros;
77 }
79 u32int _imgtorgba(Memimage*, u32int);
80 u32int _rgbatoimg(Memimage*, u32int);
81 u32int _pixelbits(Memimage*, Point);
83 #define DBG if(0)
84 static Memdrawparam par;
86 Memdrawparam*
87 _memimagedrawsetup(Memimage *dst, Rectangle r, Memimage *src, Point p0, Memimage *mask, Point p1, int op)
88 {
89 if(mask == nil)
90 mask = memopaque;
92 DBG print("memimagedraw %p/%luX %R @ %p %p/%luX %P %p/%luX %P... ", dst, dst->chan, r, dst->data->bdata, src, src->chan, p0, mask, mask->chan, p1);
94 if(drawclip(dst, &r, src, &p0, mask, &p1, &par.sr, &par.mr) == 0){
95 // if(drawdebug)
96 // iprint("empty clipped rectangle\n");
97 return nil;
98 }
100 if(op < Clear || op > SoverD){
101 // if(drawdebug)
102 // iprint("op out of range: %d\n", op);
103 return nil;
106 par.op = op;
107 par.dst = dst;
108 par.r = r;
109 par.src = src;
110 /* par.sr set by drawclip */
111 par.mask = mask;
112 /* par.mr set by drawclip */
114 par.state = 0;
115 if(src->flags&Frepl){
116 par.state |= Replsrc;
117 if(Dx(src->r)==1 && Dy(src->r)==1){
118 par.sval = pixelbits(src, src->r.min);
119 par.state |= Simplesrc;
120 par.srgba = _imgtorgba(src, par.sval);
121 par.sdval = _rgbatoimg(dst, par.srgba);
122 if((par.srgba&0xFF) == 0 && (op&DoutS)){
123 // if (drawdebug) iprint("fill with transparent source\n");
124 return nil; /* no-op successfully handled */
129 if(mask->flags & Frepl){
130 par.state |= Replmask;
131 if(Dx(mask->r)==1 && Dy(mask->r)==1){
132 par.mval = pixelbits(mask, mask->r.min);
133 if(par.mval == 0 && (op&DoutS)){
134 // if(drawdebug) iprint("fill with zero mask\n");
135 return nil; /* no-op successfully handled */
137 par.state |= Simplemask;
138 if(par.mval == ~0)
139 par.state |= Fullmask;
140 par.mrgba = _imgtorgba(mask, par.mval);
144 // if(drawdebug)
145 // iprint("dr %R sr %R mr %R...", r, par.sr, par.mr);
146 DBG print("draw dr %R sr %R mr %R %lux\n", r, par.sr, par.mr, par.state);
148 return &par;
151 void
152 _memimagedraw(Memdrawparam *par)
154 /*
155 * Now that we've clipped the parameters down to be consistent, we
156 * simply try sub-drawing routines in order until we find one that was able
157 * to handle us. If the sub-drawing routine returns zero, it means it was
158 * unable to satisfy the request, so we do not return.
159 */
161 /*
162 * Hardware support. Each video driver provides this function,
163 * which checks to see if there is anything it can help with.
164 * There could be an if around this checking to see if dst is in video memory.
165 */
166 DBG print("test hwdraw\n");
167 if(hwdraw(par)){
168 //if(drawdebug) iprint("hw handled\n");
169 DBG print("hwdraw handled\n");
170 return;
172 /*
173 * Optimizations using memmove and memset.
174 */
175 DBG print("test memoptdraw\n");
176 if(memoptdraw(par)){
177 //if(drawdebug) iprint("memopt handled\n");
178 DBG print("memopt handled\n");
179 return;
182 /*
183 * Character drawing.
184 * Solid source color being painted through a boolean mask onto a high res image.
185 */
186 DBG print("test chardraw\n");
187 if(chardraw(par)){
188 //if(drawdebug) iprint("chardraw handled\n");
189 DBG print("chardraw handled\n");
190 return;
193 /*
194 * General calculation-laden case that does alpha for each pixel.
195 */
196 DBG print("do alphadraw\n");
197 alphadraw(par);
198 //if(drawdebug) iprint("alphadraw handled\n");
199 DBG print("alphadraw handled\n");
201 #undef DBG
203 /*
204 * Clip the destination rectangle further based on the properties of the
205 * source and mask rectangles. Once the destination rectangle is properly
206 * clipped, adjust the source and mask rectangles to be the same size.
207 * Then if source or mask is replicated, move its clipped rectangle
208 * so that its minimum point falls within the repl rectangle.
210 * Return zero if the final rectangle is null.
211 */
212 int
213 drawclip(Memimage *dst, Rectangle *r, Memimage *src, Point *p0, Memimage *mask, Point *p1, Rectangle *sr, Rectangle *mr)
215 Point rmin, delta;
216 int splitcoords;
217 Rectangle omr;
219 if(r->min.x>=r->max.x || r->min.y>=r->max.y)
220 return 0;
221 splitcoords = (p0->x!=p1->x) || (p0->y!=p1->y);
222 /* clip to destination */
223 rmin = r->min;
224 if(!rectclip(r, dst->r) || !rectclip(r, dst->clipr))
225 return 0;
226 /* move mask point */
227 p1->x += r->min.x-rmin.x;
228 p1->y += r->min.y-rmin.y;
229 /* move source point */
230 p0->x += r->min.x-rmin.x;
231 p0->y += r->min.y-rmin.y;
232 /* map destination rectangle into source */
233 sr->min = *p0;
234 sr->max.x = p0->x+Dx(*r);
235 sr->max.y = p0->y+Dy(*r);
236 /* sr is r in source coordinates; clip to source */
237 if(!(src->flags&Frepl) && !rectclip(sr, src->r))
238 return 0;
239 if(!rectclip(sr, src->clipr))
240 return 0;
241 /* compute and clip rectangle in mask */
242 if(splitcoords){
243 /* move mask point with source */
244 p1->x += sr->min.x-p0->x;
245 p1->y += sr->min.y-p0->y;
246 mr->min = *p1;
247 mr->max.x = p1->x+Dx(*sr);
248 mr->max.y = p1->y+Dy(*sr);
249 omr = *mr;
250 /* mr is now rectangle in mask; clip it */
251 if(!(mask->flags&Frepl) && !rectclip(mr, mask->r))
252 return 0;
253 if(!rectclip(mr, mask->clipr))
254 return 0;
255 /* reflect any clips back to source */
256 sr->min.x += mr->min.x-omr.min.x;
257 sr->min.y += mr->min.y-omr.min.y;
258 sr->max.x += mr->max.x-omr.max.x;
259 sr->max.y += mr->max.y-omr.max.y;
260 *p1 = mr->min;
261 }else{
262 if(!(mask->flags&Frepl) && !rectclip(sr, mask->r))
263 return 0;
264 if(!rectclip(sr, mask->clipr))
265 return 0;
266 *p1 = sr->min;
269 /* move source clipping back to destination */
270 delta.x = r->min.x - p0->x;
271 delta.y = r->min.y - p0->y;
272 r->min.x = sr->min.x + delta.x;
273 r->min.y = sr->min.y + delta.y;
274 r->max.x = sr->max.x + delta.x;
275 r->max.y = sr->max.y + delta.y;
277 /* move source rectangle so sr->min is in src->r */
278 if(src->flags&Frepl) {
279 delta.x = drawreplxy(src->r.min.x, src->r.max.x, sr->min.x) - sr->min.x;
280 delta.y = drawreplxy(src->r.min.y, src->r.max.y, sr->min.y) - sr->min.y;
281 sr->min.x += delta.x;
282 sr->min.y += delta.y;
283 sr->max.x += delta.x;
284 sr->max.y += delta.y;
286 *p0 = sr->min;
288 /* move mask point so it is in mask->r */
289 *p1 = drawrepl(mask->r, *p1);
290 mr->min = *p1;
291 mr->max.x = p1->x+Dx(*sr);
292 mr->max.y = p1->y+Dy(*sr);
294 assert(Dx(*sr) == Dx(*mr) && Dx(*mr) == Dx(*r));
295 assert(Dy(*sr) == Dy(*mr) && Dy(*mr) == Dy(*r));
296 assert(ptinrect(*p0, src->r));
297 assert(ptinrect(*p1, mask->r));
298 assert(ptinrect(r->min, dst->r));
300 return 1;
303 /*
304 * Conversion tables.
305 */
306 static uchar replbit[1+8][256]; /* replbit[x][y] is the replication of the x-bit quantity y to 8-bit depth */
307 static uchar conv18[256][8]; /* conv18[x][y] is the yth pixel in the depth-1 pixel x */
308 static uchar conv28[256][4]; /* ... */
309 static uchar conv48[256][2];
311 /*
312 * bitmap of how to replicate n bits to fill 8, for 1 ≤ n ≤ 8.
313 * the X's are where to put the bottom (ones) bit of the n-bit pattern.
314 * only the top 8 bits of the result are actually used.
315 * (the lower 8 bits are needed to get bits in the right place
316 * when n is not a divisor of 8.)
318 * Should check to see if its easier to just refer to replmul than
319 * use the precomputed values in replbit. On PCs it may well
320 * be; on machines with slow multiply instructions it probably isn't.
321 */
322 #define a ((((((((((((((((0
323 #define X *2+1)
324 #define _ *2)
325 static int replmul[1+8] = {
326 0,
327 a X X X X X X X X X X X X X X X X,
328 a _ X _ X _ X _ X _ X _ X _ X _ X,
329 a _ _ X _ _ X _ _ X _ _ X _ _ X _,
330 a _ _ _ X _ _ _ X _ _ _ X _ _ _ X,
331 a _ _ _ _ X _ _ _ _ X _ _ _ _ X _,
332 a _ _ _ _ _ X _ _ _ _ _ X _ _ _ _,
333 a _ _ _ _ _ _ X _ _ _ _ _ _ X _ _,
334 a _ _ _ _ _ _ _ X _ _ _ _ _ _ _ X,
335 };
336 #undef a
337 #undef X
338 #undef _
340 static void
341 mktables(void)
343 int i, j, mask, sh, small;
345 if(tablesbuilt)
346 return;
348 fmtinstall('R', Rfmt);
349 fmtinstall('P', Pfmt);
350 tablesbuilt = 1;
352 /* bit replication up to 8 bits */
353 for(i=0; i<256; i++){
354 for(j=0; j<=8; j++){ /* j <= 8 [sic] */
355 small = i & ((1<<j)-1);
356 replbit[j][i] = (small*replmul[j])>>8;
360 /* bit unpacking up to 8 bits, only powers of 2 */
361 for(i=0; i<256; i++){
362 for(j=0, sh=7, mask=1; j<8; j++, sh--)
363 conv18[i][j] = replbit[1][(i>>sh)&mask];
365 for(j=0, sh=6, mask=3; j<4; j++, sh-=2)
366 conv28[i][j] = replbit[2][(i>>sh)&mask];
368 for(j=0, sh=4, mask=15; j<2; j++, sh-=4)
369 conv48[i][j] = replbit[4][(i>>sh)&mask];
373 static uchar ones = 0xff;
375 /*
376 * General alpha drawing case. Can handle anything.
377 */
378 typedef struct Buffer Buffer;
379 struct Buffer {
380 /* used by most routines */
381 uchar *red;
382 uchar *grn;
383 uchar *blu;
384 uchar *alpha;
385 uchar *grey;
386 u32int *rgba;
387 int delta; /* number of bytes to add to pointer to get next pixel to the right */
389 /* used by boolcalc* for mask data */
390 uchar *m; /* ptr to mask data r.min byte; like p->bytermin */
391 int mskip; /* no. of left bits to skip in *m */
392 uchar *bm; /* ptr to mask data img->r.min byte; like p->bytey0s */
393 int bmskip; /* no. of left bits to skip in *bm */
394 uchar *em; /* ptr to mask data img->r.max.x byte; like p->bytey0e */
395 int emskip; /* no. of right bits to skip in *em */
396 };
398 typedef struct Param Param;
399 typedef Buffer Readfn(Param*, uchar*, int);
400 typedef void Writefn(Param*, uchar*, Buffer);
401 typedef Buffer Calcfn(Buffer, Buffer, Buffer, int, int, int);
403 enum {
404 MAXBCACHE = 16
405 };
407 /* giant rathole to customize functions with */
408 struct Param {
409 Readfn *replcall;
410 Readfn *greymaskcall;
411 Readfn *convreadcall;
412 Writefn *convwritecall;
414 Memimage *img;
415 Rectangle r;
416 int dx; /* of r */
417 int needbuf;
418 int convgrey;
419 int alphaonly;
421 uchar *bytey0s; /* byteaddr(Pt(img->r.min.x, img->r.min.y)) */
422 uchar *bytermin; /* byteaddr(Pt(r.min.x, img->r.min.y)) */
423 uchar *bytey0e; /* byteaddr(Pt(img->r.max.x, img->r.min.y)) */
424 int bwidth;
426 int replcache; /* if set, cache buffers */
427 Buffer bcache[MAXBCACHE];
428 u32int bfilled;
429 uchar *bufbase;
430 int bufoff;
431 int bufdelta;
433 int dir;
435 int convbufoff;
436 uchar *convbuf;
437 Param *convdpar;
438 int convdx;
439 };
441 static uchar *drawbuf;
442 static int ndrawbuf;
443 static int mdrawbuf;
444 static Param spar, mpar, dpar; /* easier on the stacks */
445 static Readfn greymaskread, replread, readptr;
446 static Writefn nullwrite;
447 static Calcfn alphacalc0, alphacalc14, alphacalc2810, alphacalc3679, alphacalc5, alphacalc11, alphacalcS;
448 static Calcfn boolcalc14, boolcalc236789, boolcalc1011;
450 static Readfn* readfn(Memimage*);
451 static Readfn* readalphafn(Memimage*);
452 static Writefn* writefn(Memimage*);
454 static Calcfn* boolcopyfn(Memimage*, Memimage*);
455 static Readfn* convfn(Memimage*, Param*, Memimage*, Param*);
457 static Calcfn *alphacalc[Ncomp] =
459 alphacalc0, /* Clear */
460 alphacalc14, /* DoutS */
461 alphacalc2810, /* SoutD */
462 alphacalc3679, /* DxorS */
463 alphacalc14, /* DinS */
464 alphacalc5, /* D */
465 alphacalc3679, /* DatopS */
466 alphacalc3679, /* DoverS */
467 alphacalc2810, /* SinD */
468 alphacalc3679, /* SatopD */
469 alphacalc2810, /* S */
470 alphacalc11, /* SoverD */
471 };
473 static Calcfn *boolcalc[Ncomp] =
475 alphacalc0, /* Clear */
476 boolcalc14, /* DoutS */
477 boolcalc236789, /* SoutD */
478 boolcalc236789, /* DxorS */
479 boolcalc14, /* DinS */
480 alphacalc5, /* D */
481 boolcalc236789, /* DatopS */
482 boolcalc236789, /* DoverS */
483 boolcalc236789, /* SinD */
484 boolcalc236789, /* SatopD */
485 boolcalc1011, /* S */
486 boolcalc1011, /* SoverD */
487 };
489 static int
490 allocdrawbuf(void)
492 uchar *p;
494 if(ndrawbuf > mdrawbuf){
495 p = realloc(drawbuf, ndrawbuf);
496 if(p == nil){
497 werrstr("memimagedraw out of memory");
498 return -1;
500 drawbuf = p;
501 mdrawbuf = ndrawbuf;
503 return 0;
506 static void
507 getparam(Param *p, Memimage *img, Rectangle r, int convgrey, int needbuf)
509 int nbuf;
511 memset(p, 0, sizeof *p);
513 p->img = img;
514 p->r = r;
515 p->dx = Dx(r);
516 p->needbuf = needbuf;
517 p->convgrey = convgrey;
519 assert(img->r.min.x <= r.min.x && r.min.x < img->r.max.x);
521 p->bytey0s = byteaddr(img, Pt(img->r.min.x, img->r.min.y));
522 p->bytermin = byteaddr(img, Pt(r.min.x, img->r.min.y));
523 p->bytey0e = byteaddr(img, Pt(img->r.max.x, img->r.min.y));
524 p->bwidth = sizeof(u32int)*img->width;
526 assert(p->bytey0s <= p->bytermin && p->bytermin <= p->bytey0e);
528 if(p->r.min.x == p->img->r.min.x)
529 assert(p->bytermin == p->bytey0s);
531 nbuf = 1;
532 if((img->flags&Frepl) && Dy(img->r) <= MAXBCACHE && Dy(img->r) < Dy(r)){
533 p->replcache = 1;
534 nbuf = Dy(img->r);
536 p->bufdelta = 4*p->dx;
537 p->bufoff = ndrawbuf;
538 ndrawbuf += p->bufdelta*nbuf;
541 static void
542 clipy(Memimage *img, int *y)
544 int dy;
546 dy = Dy(img->r);
547 if(*y == dy)
548 *y = 0;
549 else if(*y == -1)
550 *y = dy-1;
551 assert(0 <= *y && *y < dy);
554 static void
555 dumpbuf(char *s, Buffer b, int n)
557 int i;
558 uchar *p;
560 print("%s", s);
561 for(i=0; i<n; i++){
562 print(" ");
563 if(p=b.grey){
564 print(" k%.2uX", *p);
565 b.grey += b.delta;
566 }else{
567 if(p=b.red){
568 print(" r%.2uX", *p);
569 b.red += b.delta;
571 if(p=b.grn){
572 print(" g%.2uX", *p);
573 b.grn += b.delta;
575 if(p=b.blu){
576 print(" b%.2uX", *p);
577 b.blu += b.delta;
580 if((p=b.alpha) != &ones){
581 print(" α%.2uX", *p);
582 b.alpha += b.delta;
585 print("\n");
588 /*
589 * For each scan line, we expand the pixels from source, mask, and destination
590 * into byte-aligned red, green, blue, alpha, and grey channels. If buffering is not
591 * needed and the channels were already byte-aligned (grey8, rgb24, rgba32, rgb32),
592 * the readers need not copy the data: they can simply return pointers to the data.
593 * If the destination image is grey and the source is not, it is converted using the NTSC
594 * formula.
596 * Once we have all the channels, we call either rgbcalc or greycalc, depending on
597 * whether the destination image is color. This is allowed to overwrite the dst buffer (perhaps
598 * the actual data, perhaps a copy) with its result. It should only overwrite the dst buffer
599 * with the same format (i.e. red bytes with red bytes, etc.) A new buffer is returned from
600 * the calculator, and that buffer is passed to a function to write it to the destination.
601 * If the buffer is already pointing at the destination, the writing function is a no-op.
602 */
603 #define DBG if(0)
604 static int
605 alphadraw(Memdrawparam *par)
607 int isgrey, starty, endy, op;
608 int needbuf, dsty, srcy, masky;
609 int y, dir, dx, dy;
610 Buffer bsrc, bdst, bmask;
611 Readfn *rdsrc, *rdmask, *rddst;
612 Calcfn *calc;
613 Writefn *wrdst;
614 Memimage *src, *mask, *dst;
615 Rectangle r, sr, mr;
617 r = par->r;
618 dx = Dx(r);
619 dy = Dy(r);
621 ndrawbuf = 0;
623 src = par->src;
624 mask = par->mask;
625 dst = par->dst;
626 sr = par->sr;
627 mr = par->mr;
628 op = par->op;
630 isgrey = dst->flags&Fgrey;
632 /*
633 * Buffering when src and dst are the same bitmap is sufficient but not
634 * necessary. There are stronger conditions we could use. We could
635 * check to see if the rectangles intersect, and if simply moving in the
636 * correct y direction can avoid the need to buffer.
637 */
638 needbuf = (src->data == dst->data);
640 getparam(&spar, src, sr, isgrey, needbuf);
641 getparam(&dpar, dst, r, isgrey, needbuf);
642 getparam(&mpar, mask, mr, 0, needbuf);
644 dir = (needbuf && byteaddr(dst, r.min) > byteaddr(src, sr.min)) ? -1 : 1;
645 spar.dir = mpar.dir = dpar.dir = dir;
647 /*
648 * If the mask is purely boolean, we can convert from src to dst format
649 * when we read src, and then just copy it to dst where the mask tells us to.
650 * This requires a boolean (1-bit grey) mask and lack of a source alpha channel.
652 * The computation is accomplished by assigning the function pointers as follows:
653 * rdsrc - read and convert source into dst format in a buffer
654 * rdmask - convert mask to bytes, set pointer to it
655 * rddst - fill with pointer to real dst data, but do no reads
656 * calc - copy src onto dst when mask says to.
657 * wrdst - do nothing
658 * This is slightly sleazy, since things aren't doing exactly what their names say,
659 * but it avoids a fair amount of code duplication to make this a case here
660 * rather than have a separate booldraw.
661 */
662 //if(drawdebug) iprint("flag %lud mchan %lux=?%x dd %d\n", src->flags&Falpha, mask->chan, GREY1, dst->depth);
663 if(!(src->flags&Falpha) && mask->chan == GREY1 && dst->depth >= 8 && op == SoverD){
664 //if(drawdebug) iprint("boolcopy...");
665 rdsrc = convfn(dst, &dpar, src, &spar);
666 rddst = readptr;
667 rdmask = readfn(mask);
668 calc = boolcopyfn(dst, mask);
669 wrdst = nullwrite;
670 }else{
671 /* usual alphadraw parameter fetching */
672 rdsrc = readfn(src);
673 rddst = readfn(dst);
674 wrdst = writefn(dst);
675 calc = alphacalc[op];
677 /*
678 * If there is no alpha channel, we'll ask for a grey channel
679 * and pretend it is the alpha.
680 */
681 if(mask->flags&Falpha){
682 rdmask = readalphafn(mask);
683 mpar.alphaonly = 1;
684 }else{
685 mpar.greymaskcall = readfn(mask);
686 mpar.convgrey = 1;
687 rdmask = greymaskread;
689 /*
690 * Should really be above, but then boolcopyfns would have
691 * to deal with bit alignment, and I haven't written that.
693 * This is a common case for things like ellipse drawing.
694 * When there's no alpha involved and the mask is boolean,
695 * we can avoid all the division and multiplication.
696 */
697 if(mask->chan == GREY1 && !(src->flags&Falpha))
698 calc = boolcalc[op];
699 else if(op == SoverD && !(src->flags&Falpha))
700 calc = alphacalcS;
704 /*
705 * If the image has a small enough repl rectangle,
706 * we can just read each line once and cache them.
707 */
708 if(spar.replcache){
709 spar.replcall = rdsrc;
710 rdsrc = replread;
712 if(mpar.replcache){
713 mpar.replcall = rdmask;
714 rdmask = replread;
717 if(allocdrawbuf() < 0)
718 return 0;
720 /*
721 * Before we were saving only offsets from drawbuf in the parameter
722 * structures; now that drawbuf has been grown to accomodate us,
723 * we can fill in the pointers.
724 */
725 spar.bufbase = drawbuf+spar.bufoff;
726 mpar.bufbase = drawbuf+mpar.bufoff;
727 dpar.bufbase = drawbuf+dpar.bufoff;
728 spar.convbuf = drawbuf+spar.convbufoff;
730 if(dir == 1){
731 starty = 0;
732 endy = dy;
733 }else{
734 starty = dy-1;
735 endy = -1;
738 /*
739 * srcy, masky, and dsty are offsets from the top of their
740 * respective Rectangles. they need to be contained within
741 * the rectangles, so clipy can keep them there without division.
742 */
743 srcy = (starty + sr.min.y - src->r.min.y)%Dy(src->r);
744 masky = (starty + mr.min.y - mask->r.min.y)%Dy(mask->r);
745 dsty = starty + r.min.y - dst->r.min.y;
747 assert(0 <= srcy && srcy < Dy(src->r));
748 assert(0 <= masky && masky < Dy(mask->r));
749 assert(0 <= dsty && dsty < Dy(dst->r));
751 for(y=starty; y!=endy; y+=dir, srcy+=dir, masky+=dir, dsty+=dir){
752 clipy(src, &srcy);
753 clipy(dst, &dsty);
754 clipy(mask, &masky);
756 bsrc = rdsrc(&spar, spar.bufbase, srcy);
757 DBG print("[");
758 bmask = rdmask(&mpar, mpar.bufbase, masky);
759 DBG print("]\n");
760 bdst = rddst(&dpar, dpar.bufbase, dsty);
761 DBG dumpbuf("src", bsrc, dx);
762 DBG dumpbuf("mask", bmask, dx);
763 DBG dumpbuf("dst", bdst, dx);
764 bdst = calc(bdst, bsrc, bmask, dx, isgrey, op);
765 wrdst(&dpar, dpar.bytermin+dsty*dpar.bwidth, bdst);
768 return 1;
770 #undef DBG
772 static Buffer
773 alphacalc0(Buffer bdst, Buffer b1, Buffer b2, int dx, int grey, int op)
775 USED(grey);
776 USED(op);
777 memset(bdst.rgba, 0, dx*bdst.delta);
778 return bdst;
781 static Buffer
782 alphacalc14(Buffer bdst, Buffer bsrc, Buffer bmask, int dx, int grey, int op)
784 Buffer obdst;
785 int fd, sadelta;
786 int i, sa, ma, q;
787 u32int s, t;
789 obdst = bdst;
790 sadelta = bsrc.alpha == &ones ? 0 : bsrc.delta;
791 q = bsrc.delta == 4 && bdst.delta == 4;
793 for(i=0; i<dx; i++){
794 sa = *bsrc.alpha;
795 ma = *bmask.alpha;
796 fd = MUL(sa, ma, t);
797 if(op == DoutS)
798 fd = 255-fd;
800 if(grey){
801 *bdst.grey = MUL(fd, *bdst.grey, t);
802 bsrc.grey += bsrc.delta;
803 bdst.grey += bdst.delta;
804 }else{
805 if(q){
806 *bdst.rgba = MUL0123(fd, *bdst.rgba, s, t);
807 bsrc.rgba++;
808 bdst.rgba++;
809 bsrc.alpha += sadelta;
810 bmask.alpha += bmask.delta;
811 continue;
813 *bdst.red = MUL(fd, *bdst.red, t);
814 *bdst.grn = MUL(fd, *bdst.grn, t);
815 *bdst.blu = MUL(fd, *bdst.blu, t);
816 bsrc.red += bsrc.delta;
817 bsrc.blu += bsrc.delta;
818 bsrc.grn += bsrc.delta;
819 bdst.red += bdst.delta;
820 bdst.blu += bdst.delta;
821 bdst.grn += bdst.delta;
823 if(bdst.alpha != &ones){
824 *bdst.alpha = MUL(fd, *bdst.alpha, t);
825 bdst.alpha += bdst.delta;
827 bmask.alpha += bmask.delta;
828 bsrc.alpha += sadelta;
830 return obdst;
833 static Buffer
834 alphacalc2810(Buffer bdst, Buffer bsrc, Buffer bmask, int dx, int grey, int op)
836 Buffer obdst;
837 int fs, sadelta;
838 int i, ma, da, q;
839 u32int s, t;
841 obdst = bdst;
842 sadelta = bsrc.alpha == &ones ? 0 : bsrc.delta;
843 q = bsrc.delta == 4 && bdst.delta == 4;
845 for(i=0; i<dx; i++){
846 ma = *bmask.alpha;
847 da = *bdst.alpha;
848 if(op == SoutD)
849 da = 255-da;
850 fs = ma;
851 if(op != S)
852 fs = MUL(fs, da, t);
854 if(grey){
855 *bdst.grey = MUL(fs, *bsrc.grey, t);
856 bsrc.grey += bsrc.delta;
857 bdst.grey += bdst.delta;
858 }else{
859 if(q){
860 *bdst.rgba = MUL0123(fs, *bsrc.rgba, s, t);
861 bsrc.rgba++;
862 bdst.rgba++;
863 bmask.alpha += bmask.delta;
864 bdst.alpha += bdst.delta;
865 continue;
867 *bdst.red = MUL(fs, *bsrc.red, t);
868 *bdst.grn = MUL(fs, *bsrc.grn, t);
869 *bdst.blu = MUL(fs, *bsrc.blu, t);
870 bsrc.red += bsrc.delta;
871 bsrc.blu += bsrc.delta;
872 bsrc.grn += bsrc.delta;
873 bdst.red += bdst.delta;
874 bdst.blu += bdst.delta;
875 bdst.grn += bdst.delta;
877 if(bdst.alpha != &ones){
878 *bdst.alpha = MUL(fs, *bsrc.alpha, t);
879 bdst.alpha += bdst.delta;
881 bmask.alpha += bmask.delta;
882 bsrc.alpha += sadelta;
884 return obdst;
887 static Buffer
888 alphacalc3679(Buffer bdst, Buffer bsrc, Buffer bmask, int dx, int grey, int op)
890 Buffer obdst;
891 int fs, fd, sadelta;
892 int i, sa, ma, da, q;
893 u32int s, t, u, v;
895 obdst = bdst;
896 sadelta = bsrc.alpha == &ones ? 0 : bsrc.delta;
897 q = bsrc.delta == 4 && bdst.delta == 4;
899 for(i=0; i<dx; i++){
900 sa = *bsrc.alpha;
901 ma = *bmask.alpha;
902 da = *bdst.alpha;
903 if(op == SatopD)
904 fs = MUL(ma, da, t);
905 else
906 fs = MUL(ma, 255-da, t);
907 if(op == DoverS)
908 fd = 255;
909 else{
910 fd = MUL(sa, ma, t);
911 if(op != DatopS)
912 fd = 255-fd;
915 if(grey){
916 *bdst.grey = MUL(fs, *bsrc.grey, s)+MUL(fd, *bdst.grey, t);
917 bsrc.grey += bsrc.delta;
918 bdst.grey += bdst.delta;
919 }else{
920 if(q){
921 *bdst.rgba = MUL0123(fs, *bsrc.rgba, s, t)+MUL0123(fd, *bdst.rgba, u, v);
922 bsrc.rgba++;
923 bdst.rgba++;
924 bsrc.alpha += sadelta;
925 bmask.alpha += bmask.delta;
926 bdst.alpha += bdst.delta;
927 continue;
929 *bdst.red = MUL(fs, *bsrc.red, s)+MUL(fd, *bdst.red, t);
930 *bdst.grn = MUL(fs, *bsrc.grn, s)+MUL(fd, *bdst.grn, t);
931 *bdst.blu = MUL(fs, *bsrc.blu, s)+MUL(fd, *bdst.blu, t);
932 bsrc.red += bsrc.delta;
933 bsrc.blu += bsrc.delta;
934 bsrc.grn += bsrc.delta;
935 bdst.red += bdst.delta;
936 bdst.blu += bdst.delta;
937 bdst.grn += bdst.delta;
939 if(bdst.alpha != &ones){
940 *bdst.alpha = MUL(fs, sa, s)+MUL(fd, da, t);
941 bdst.alpha += bdst.delta;
943 bmask.alpha += bmask.delta;
944 bsrc.alpha += sadelta;
946 return obdst;
949 static Buffer
950 alphacalc5(Buffer bdst, Buffer b1, Buffer b2, int dx, int grey, int op)
952 USED(dx);
953 USED(grey);
954 USED(op);
955 return bdst;
958 static Buffer
959 alphacalc11(Buffer bdst, Buffer bsrc, Buffer bmask, int dx, int grey, int op)
961 Buffer obdst;
962 int fd, sadelta;
963 int i, sa, ma, q;
964 u32int s, t, u, v;
966 USED(op);
967 obdst = bdst;
968 sadelta = bsrc.alpha == &ones ? 0 : bsrc.delta;
969 q = bsrc.delta == 4 && bdst.delta == 4;
971 for(i=0; i<dx; i++){
972 sa = *bsrc.alpha;
973 ma = *bmask.alpha;
974 fd = 255-MUL(sa, ma, t);
976 if(grey){
977 *bdst.grey = MUL(ma, *bsrc.grey, s)+MUL(fd, *bdst.grey, t);
978 bsrc.grey += bsrc.delta;
979 bdst.grey += bdst.delta;
980 }else{
981 if(q){
982 *bdst.rgba = MUL0123(ma, *bsrc.rgba, s, t)+MUL0123(fd, *bdst.rgba, u, v);
983 bsrc.rgba++;
984 bdst.rgba++;
985 bsrc.alpha += sadelta;
986 bmask.alpha += bmask.delta;
987 continue;
989 *bdst.red = MUL(ma, *bsrc.red, s)+MUL(fd, *bdst.red, t);
990 *bdst.grn = MUL(ma, *bsrc.grn, s)+MUL(fd, *bdst.grn, t);
991 *bdst.blu = MUL(ma, *bsrc.blu, s)+MUL(fd, *bdst.blu, t);
992 bsrc.red += bsrc.delta;
993 bsrc.blu += bsrc.delta;
994 bsrc.grn += bsrc.delta;
995 bdst.red += bdst.delta;
996 bdst.blu += bdst.delta;
997 bdst.grn += bdst.delta;
999 if(bdst.alpha != &ones){
1000 *bdst.alpha = MUL(ma, sa, s)+MUL(fd, *bdst.alpha, t);
1001 bdst.alpha += bdst.delta;
1003 bmask.alpha += bmask.delta;
1004 bsrc.alpha += sadelta;
1006 return obdst;
1010 not used yet
1011 source and mask alpha 1
1012 static Buffer
1013 alphacalcS0(Buffer bdst, Buffer bsrc, Buffer bmask, int dx, int grey, int op)
1015 Buffer obdst;
1016 int i;
1018 USED(op);
1019 obdst = bdst;
1020 if(bsrc.delta == bdst.delta){
1021 memmove(bdst.rgba, bsrc.rgba, dx*bdst.delta);
1022 return obdst;
1024 for(i=0; i<dx; i++){
1025 if(grey){
1026 *bdst.grey = *bsrc.grey;
1027 bsrc.grey += bsrc.delta;
1028 bdst.grey += bdst.delta;
1029 }else{
1030 *bdst.red = *bsrc.red;
1031 *bdst.grn = *bsrc.grn;
1032 *bdst.blu = *bsrc.blu;
1033 bsrc.red += bsrc.delta;
1034 bsrc.blu += bsrc.delta;
1035 bsrc.grn += bsrc.delta;
1036 bdst.red += bdst.delta;
1037 bdst.blu += bdst.delta;
1038 bdst.grn += bdst.delta;
1040 if(bdst.alpha != &ones){
1041 *bdst.alpha = 255;
1042 bdst.alpha += bdst.delta;
1045 return obdst;
1049 /* source alpha 1 */
1050 static Buffer
1051 alphacalcS(Buffer bdst, Buffer bsrc, Buffer bmask, int dx, int grey, int op)
1053 Buffer obdst;
1054 int fd;
1055 int i, ma;
1056 u32int s, t;
1058 USED(op);
1059 obdst = bdst;
1061 for(i=0; i<dx; i++){
1062 ma = *bmask.alpha;
1063 fd = 255-ma;
1065 if(grey){
1066 *bdst.grey = MUL(ma, *bsrc.grey, s)+MUL(fd, *bdst.grey, t);
1067 bsrc.grey += bsrc.delta;
1068 bdst.grey += bdst.delta;
1069 }else{
1070 *bdst.red = MUL(ma, *bsrc.red, s)+MUL(fd, *bdst.red, t);
1071 *bdst.grn = MUL(ma, *bsrc.grn, s)+MUL(fd, *bdst.grn, t);
1072 *bdst.blu = MUL(ma, *bsrc.blu, s)+MUL(fd, *bdst.blu, t);
1073 bsrc.red += bsrc.delta;
1074 bsrc.blu += bsrc.delta;
1075 bsrc.grn += bsrc.delta;
1076 bdst.red += bdst.delta;
1077 bdst.blu += bdst.delta;
1078 bdst.grn += bdst.delta;
1080 if(bdst.alpha != &ones){
1081 *bdst.alpha = ma+MUL(fd, *bdst.alpha, t);
1082 bdst.alpha += bdst.delta;
1084 bmask.alpha += bmask.delta;
1086 return obdst;
1089 static Buffer
1090 boolcalc14(Buffer bdst, Buffer b1, Buffer bmask, int dx, int grey, int op)
1092 Buffer obdst;
1093 int i, ma, zero;
1095 obdst = bdst;
1097 for(i=0; i<dx; i++){
1098 ma = *bmask.alpha;
1099 zero = ma ? op == DoutS : op == DinS;
1101 if(grey){
1102 if(zero)
1103 *bdst.grey = 0;
1104 bdst.grey += bdst.delta;
1105 }else{
1106 if(zero)
1107 *bdst.red = *bdst.grn = *bdst.blu = 0;
1108 bdst.red += bdst.delta;
1109 bdst.blu += bdst.delta;
1110 bdst.grn += bdst.delta;
1112 bmask.alpha += bmask.delta;
1113 if(bdst.alpha != &ones){
1114 if(zero)
1115 *bdst.alpha = 0;
1116 bdst.alpha += bdst.delta;
1119 return obdst;
1122 static Buffer
1123 boolcalc236789(Buffer bdst, Buffer bsrc, Buffer bmask, int dx, int grey, int op)
1125 Buffer obdst;
1126 int fs, fd;
1127 int i, ma, da, zero;
1128 u32int s, t;
1130 obdst = bdst;
1131 zero = !(op&1);
1133 for(i=0; i<dx; i++){
1134 ma = *bmask.alpha;
1135 da = *bdst.alpha;
1136 fs = da;
1137 if(op&2)
1138 fs = 255-da;
1139 fd = 0;
1140 if(op&4)
1141 fd = 255;
1143 if(grey){
1144 if(ma)
1145 *bdst.grey = MUL(fs, *bsrc.grey, s)+MUL(fd, *bdst.grey, t);
1146 else if(zero)
1147 *bdst.grey = 0;
1148 bsrc.grey += bsrc.delta;
1149 bdst.grey += bdst.delta;
1150 }else{
1151 if(ma){
1152 *bdst.red = MUL(fs, *bsrc.red, s)+MUL(fd, *bdst.red, t);
1153 *bdst.grn = MUL(fs, *bsrc.grn, s)+MUL(fd, *bdst.grn, t);
1154 *bdst.blu = MUL(fs, *bsrc.blu, s)+MUL(fd, *bdst.blu, t);
1156 else if(zero)
1157 *bdst.red = *bdst.grn = *bdst.blu = 0;
1158 bsrc.red += bsrc.delta;
1159 bsrc.blu += bsrc.delta;
1160 bsrc.grn += bsrc.delta;
1161 bdst.red += bdst.delta;
1162 bdst.blu += bdst.delta;
1163 bdst.grn += bdst.delta;
1165 bmask.alpha += bmask.delta;
1166 if(bdst.alpha != &ones){
1167 if(ma)
1168 *bdst.alpha = fs+MUL(fd, da, t);
1169 else if(zero)
1170 *bdst.alpha = 0;
1171 bdst.alpha += bdst.delta;
1174 return obdst;
1177 static Buffer
1178 boolcalc1011(Buffer bdst, Buffer bsrc, Buffer bmask, int dx, int grey, int op)
1180 Buffer obdst;
1181 int i, ma, zero;
1183 obdst = bdst;
1184 zero = !(op&1);
1186 for(i=0; i<dx; i++){
1187 ma = *bmask.alpha;
1189 if(grey){
1190 if(ma)
1191 *bdst.grey = *bsrc.grey;
1192 else if(zero)
1193 *bdst.grey = 0;
1194 bsrc.grey += bsrc.delta;
1195 bdst.grey += bdst.delta;
1196 }else{
1197 if(ma){
1198 *bdst.red = *bsrc.red;
1199 *bdst.grn = *bsrc.grn;
1200 *bdst.blu = *bsrc.blu;
1202 else if(zero)
1203 *bdst.red = *bdst.grn = *bdst.blu = 0;
1204 bsrc.red += bsrc.delta;
1205 bsrc.blu += bsrc.delta;
1206 bsrc.grn += bsrc.delta;
1207 bdst.red += bdst.delta;
1208 bdst.blu += bdst.delta;
1209 bdst.grn += bdst.delta;
1211 bmask.alpha += bmask.delta;
1212 if(bdst.alpha != &ones){
1213 if(ma)
1214 *bdst.alpha = 255;
1215 else if(zero)
1216 *bdst.alpha = 0;
1217 bdst.alpha += bdst.delta;
1220 return obdst;
1223 * Replicated cached scan line read. Call the function listed in the Param,
1224 * but cache the result so that for replicated images we only do the work once.
1226 static Buffer
1227 replread(Param *p, uchar *s, int y)
1229 Buffer *b;
1231 USED(s);
1232 b = &p->bcache[y];
1233 if((p->bfilled & (1<<y)) == 0){
1234 p->bfilled |= 1<<y;
1235 *b = p->replcall(p, p->bufbase+y*p->bufdelta, y);
1237 return *b;
1241 * Alpha reading function that simply relabels the grey pointer.
1243 static Buffer
1244 greymaskread(Param *p, uchar *buf, int y)
1246 Buffer b;
1248 b = p->greymaskcall(p, buf, y);
1249 b.alpha = b.grey;
1250 return b;
1253 #define DBG if(0)
1254 static Buffer
1255 readnbit(Param *p, uchar *buf, int y)
1257 Buffer b;
1258 Memimage *img;
1259 uchar *repl, *r, *w, *ow, bits;
1260 int i, n, sh, depth, x, dx, npack, nbits;
1262 b.rgba = (u32int*)buf;
1263 b.grey = w = buf;
1264 b.red = b.blu = b.grn = w;
1265 b.alpha = &ones;
1266 b.delta = 1;
1268 dx = p->dx;
1269 img = p->img;
1270 depth = img->depth;
1271 repl = &replbit[depth][0];
1272 npack = 8/depth;
1273 sh = 8-depth;
1275 /* copy from p->r.min.x until end of repl rectangle */
1276 x = p->r.min.x;
1277 n = dx;
1278 if(n > p->img->r.max.x - x)
1279 n = p->img->r.max.x - x;
1281 r = p->bytermin + y*p->bwidth;
1282 DBG print("readnbit dx %d %p=%p+%d*%d, *r=%d fetch %d ", dx, r, p->bytermin, y, p->bwidth, *r, n);
1283 bits = *r++;
1284 nbits = 8;
1285 if(i=x&(npack-1)){
1286 DBG print("throwaway %d...", i);
1287 bits <<= depth*i;
1288 nbits -= depth*i;
1290 for(i=0; i<n; i++){
1291 if(nbits == 0){
1292 DBG print("(%.2ux)...", *r);
1293 bits = *r++;
1294 nbits = 8;
1296 *w++ = repl[bits>>sh];
1297 DBG print("bit %x...", repl[bits>>sh]);
1298 bits <<= depth;
1299 nbits -= depth;
1301 dx -= n;
1302 if(dx == 0)
1303 return b;
1305 assert(x+i == p->img->r.max.x);
1307 /* copy from beginning of repl rectangle until where we were before. */
1308 x = p->img->r.min.x;
1309 n = dx;
1310 if(n > p->r.min.x - x)
1311 n = p->r.min.x - x;
1313 r = p->bytey0s + y*p->bwidth;
1314 DBG print("x=%d r=%p...", x, r);
1315 bits = *r++;
1316 nbits = 8;
1317 if(i=x&(npack-1)){
1318 bits <<= depth*i;
1319 nbits -= depth*i;
1321 DBG print("nbits=%d...", nbits);
1322 for(i=0; i<n; i++){
1323 if(nbits == 0){
1324 bits = *r++;
1325 nbits = 8;
1327 *w++ = repl[bits>>sh];
1328 DBG print("bit %x...", repl[bits>>sh]);
1329 bits <<= depth;
1330 nbits -= depth;
1331 DBG print("bits %x nbits %d...", bits, nbits);
1333 dx -= n;
1334 if(dx == 0)
1335 return b;
1337 assert(dx > 0);
1338 /* now we have exactly one full scan line: just replicate the buffer itself until we are done */
1339 ow = buf;
1340 while(dx--)
1341 *w++ = *ow++;
1343 return b;
1345 #undef DBG
1347 #define DBG if(0)
1348 static void
1349 writenbit(Param *p, uchar *w, Buffer src)
1351 uchar *r;
1352 u32int bits;
1353 int i, sh, depth, npack, nbits, x, ex;
1355 assert(src.grey != nil && src.delta == 1);
1357 x = p->r.min.x;
1358 ex = x+p->dx;
1359 depth = p->img->depth;
1360 npack = 8/depth;
1362 i=x&(npack-1);
1363 bits = i ? (*w >> (8-depth*i)) : 0;
1364 nbits = depth*i;
1365 sh = 8-depth;
1366 r = src.grey;
1368 for(; x<ex; x++){
1369 bits <<= depth;
1370 DBG print(" %x", *r);
1371 bits |= (*r++ >> sh);
1372 nbits += depth;
1373 if(nbits == 8){
1374 *w++ = bits;
1375 nbits = 0;
1379 if(nbits){
1380 sh = 8-nbits;
1381 bits <<= sh;
1382 bits |= *w & ((1<<sh)-1);
1383 *w = bits;
1385 DBG print("\n");
1386 return;
1388 #undef DBG
1390 static Buffer
1391 readcmap(Param *p, uchar *buf, int y)
1393 Buffer b;
1394 int a, convgrey, copyalpha, dx, i, m;
1395 uchar *q, *cmap, *begin, *end, *r, *w;
1397 begin = p->bytey0s + y*p->bwidth;
1398 r = p->bytermin + y*p->bwidth;
1399 end = p->bytey0e + y*p->bwidth;
1400 cmap = p->img->cmap->cmap2rgb;
1401 convgrey = p->convgrey;
1402 copyalpha = (p->img->flags&Falpha) ? 1 : 0;
1404 w = buf;
1405 dx = p->dx;
1406 if(copyalpha){
1407 b.alpha = buf++;
1408 a = p->img->shift[CAlpha]/8;
1409 m = p->img->shift[CMap]/8;
1410 for(i=0; i<dx; i++){
1411 *w++ = r[a];
1412 q = cmap+r[m]*3;
1413 r += 2;
1414 if(r == end)
1415 r = begin;
1416 if(convgrey){
1417 *w++ = RGB2K(q[0], q[1], q[2]);
1418 }else{
1419 *w++ = q[2]; /* blue */
1420 *w++ = q[1]; /* green */
1421 *w++ = q[0]; /* red */
1424 }else{
1425 b.alpha = &ones;
1426 for(i=0; i<dx; i++){
1427 q = cmap+*r++*3;
1428 if(r == end)
1429 r = begin;
1430 if(convgrey){
1431 *w++ = RGB2K(q[0], q[1], q[2]);
1432 }else{
1433 *w++ = q[2]; /* blue */
1434 *w++ = q[1]; /* green */
1435 *w++ = q[0]; /* red */
1440 b.rgba = (u32int*)(buf-copyalpha);
1442 if(convgrey){
1443 b.grey = buf;
1444 b.red = b.blu = b.grn = buf;
1445 b.delta = 1+copyalpha;
1446 }else{
1447 b.blu = buf;
1448 b.grn = buf+1;
1449 b.red = buf+2;
1450 b.grey = nil;
1451 b.delta = 3+copyalpha;
1453 return b;
1456 static void
1457 writecmap(Param *p, uchar *w, Buffer src)
1459 uchar *cmap, *red, *grn, *blu;
1460 int i, dx, delta;
1462 cmap = p->img->cmap->rgb2cmap;
1464 delta = src.delta;
1465 red= src.red;
1466 grn = src.grn;
1467 blu = src.blu;
1469 dx = p->dx;
1470 for(i=0; i<dx; i++, red+=delta, grn+=delta, blu+=delta)
1471 *w++ = cmap[(*red>>4)*256+(*grn>>4)*16+(*blu>>4)];
1474 #define DBG if(0)
1475 static Buffer
1476 readbyte(Param *p, uchar *buf, int y)
1478 Buffer b;
1479 Memimage *img;
1480 int dx, isgrey, convgrey, alphaonly, copyalpha, i, nb;
1481 uchar *begin, *end, *r, *w, *rrepl, *grepl, *brepl, *arepl, *krepl;
1482 uchar ured, ugrn, ublu;
1483 u32int u;
1485 img = p->img;
1486 begin = p->bytey0s + y*p->bwidth;
1487 r = p->bytermin + y*p->bwidth;
1488 end = p->bytey0e + y*p->bwidth;
1490 w = buf;
1491 dx = p->dx;
1492 nb = img->depth/8;
1494 convgrey = p->convgrey; /* convert rgb to grey */
1495 isgrey = img->flags&Fgrey;
1496 alphaonly = p->alphaonly;
1497 copyalpha = (img->flags&Falpha) ? 1 : 0;
1499 DBG print("copyalpha %d alphaonly %d convgrey %d isgrey %d\n", copyalpha, alphaonly, convgrey, isgrey);
1500 /* if we can, avoid processing everything */
1501 if(!(img->flags&Frepl) && !convgrey && (img->flags&Fbytes)){
1502 memset(&b, 0, sizeof b);
1503 if(p->needbuf){
1504 memmove(buf, r, dx*nb);
1505 r = buf;
1507 b.rgba = (u32int*)r;
1508 if(copyalpha)
1509 b.alpha = r+img->shift[CAlpha]/8;
1510 else
1511 b.alpha = &ones;
1512 if(isgrey){
1513 b.grey = r+img->shift[CGrey]/8;
1514 b.red = b.grn = b.blu = b.grey;
1515 }else{
1516 b.red = r+img->shift[CRed]/8;
1517 b.grn = r+img->shift[CGreen]/8;
1518 b.blu = r+img->shift[CBlue]/8;
1520 b.delta = nb;
1521 return b;
1524 DBG print("2\n");
1525 rrepl = replbit[img->nbits[CRed]];
1526 grepl = replbit[img->nbits[CGreen]];
1527 brepl = replbit[img->nbits[CBlue]];
1528 arepl = replbit[img->nbits[CAlpha]];
1529 krepl = replbit[img->nbits[CGrey]];
1531 for(i=0; i<dx; i++){
1532 u = r[0] | (r[1]<<8) | (r[2]<<16) | (r[3]<<24);
1533 if(copyalpha) {
1534 *w++ = arepl[(u>>img->shift[CAlpha]) & img->mask[CAlpha]];
1535 DBG print("a %x\n", w[-1]);
1538 if(isgrey)
1539 *w++ = krepl[(u >> img->shift[CGrey]) & img->mask[CGrey]];
1540 else if(!alphaonly){
1541 ured = rrepl[(u >> img->shift[CRed]) & img->mask[CRed]];
1542 ugrn = grepl[(u >> img->shift[CGreen]) & img->mask[CGreen]];
1543 ublu = brepl[(u >> img->shift[CBlue]) & img->mask[CBlue]];
1544 if(convgrey){
1545 DBG print("g %x %x %x\n", ured, ugrn, ublu);
1546 *w++ = RGB2K(ured, ugrn, ublu);
1547 DBG print("%x\n", w[-1]);
1548 }else{
1549 *w++ = brepl[(u >> img->shift[CBlue]) & img->mask[CBlue]];
1550 *w++ = grepl[(u >> img->shift[CGreen]) & img->mask[CGreen]];
1551 *w++ = rrepl[(u >> img->shift[CRed]) & img->mask[CRed]];
1554 r += nb;
1555 if(r == end)
1556 r = begin;
1559 b.alpha = copyalpha ? buf : &ones;
1560 b.rgba = (u32int*)buf;
1561 if(alphaonly){
1562 b.red = b.grn = b.blu = b.grey = nil;
1563 if(!copyalpha)
1564 b.rgba = nil;
1565 b.delta = 1;
1566 }else if(isgrey || convgrey){
1567 b.grey = buf+copyalpha;
1568 b.red = b.grn = b.blu = buf+copyalpha;
1569 b.delta = copyalpha+1;
1570 DBG print("alpha %x grey %x\n", b.alpha ? *b.alpha : 0xFF, *b.grey);
1571 }else{
1572 b.blu = buf+copyalpha;
1573 b.grn = buf+copyalpha+1;
1574 b.grey = nil;
1575 b.red = buf+copyalpha+2;
1576 b.delta = copyalpha+3;
1578 return b;
1580 #undef DBG
1582 #define DBG if(0)
1583 static void
1584 writebyte(Param *p, uchar *w, Buffer src)
1586 Memimage *img;
1587 int i, isalpha, isgrey, nb, delta, dx, adelta;
1588 uchar ff, *red, *grn, *blu, *grey, *alpha;
1589 u32int u, mask;
1591 img = p->img;
1593 red = src.red;
1594 grn = src.grn;
1595 blu = src.blu;
1596 alpha = src.alpha;
1597 delta = src.delta;
1598 grey = src.grey;
1599 dx = p->dx;
1601 nb = img->depth/8;
1602 mask = (nb==4) ? 0 : ~((1<<img->depth)-1);
1604 isalpha = img->flags&Falpha;
1605 isgrey = img->flags&Fgrey;
1606 adelta = src.delta;
1608 if(isalpha && (alpha == nil || alpha == &ones)){
1609 ff = 0xFF;
1610 alpha = &ff;
1611 adelta = 0;
1614 for(i=0; i<dx; i++){
1615 u = w[0] | (w[1]<<8) | (w[2]<<16) | (w[3]<<24);
1616 DBG print("u %.8lux...", u);
1617 u &= mask;
1618 DBG print("&mask %.8lux...", u);
1619 if(isgrey){
1620 u |= ((*grey >> (8-img->nbits[CGrey])) & img->mask[CGrey]) << img->shift[CGrey];
1621 DBG print("|grey %.8lux...", u);
1622 grey += delta;
1623 }else{
1624 u |= ((*red >> (8-img->nbits[CRed])) & img->mask[CRed]) << img->shift[CRed];
1625 u |= ((*grn >> (8-img->nbits[CGreen])) & img->mask[CGreen]) << img->shift[CGreen];
1626 u |= ((*blu >> (8-img->nbits[CBlue])) & img->mask[CBlue]) << img->shift[CBlue];
1627 red += delta;
1628 grn += delta;
1629 blu += delta;
1630 DBG print("|rgb %.8lux...", u);
1633 if(isalpha){
1634 u |= ((*alpha >> (8-img->nbits[CAlpha])) & img->mask[CAlpha]) << img->shift[CAlpha];
1635 alpha += adelta;
1636 DBG print("|alpha %.8lux...", u);
1639 w[0] = u;
1640 w[1] = u>>8;
1641 w[2] = u>>16;
1642 w[3] = u>>24;
1643 w += nb;
1646 #undef DBG
1648 static Readfn*
1649 readfn(Memimage *img)
1651 if(img->depth < 8)
1652 return readnbit;
1653 if(img->nbits[CMap] == 8)
1654 return readcmap;
1655 return readbyte;
1658 static Readfn*
1659 readalphafn(Memimage *m)
1661 USED(m);
1662 return readbyte;
1665 static Writefn*
1666 writefn(Memimage *img)
1668 if(img->depth < 8)
1669 return writenbit;
1670 if(img->chan == CMAP8)
1671 return writecmap;
1672 return writebyte;
1675 static void
1676 nullwrite(Param *p, uchar *s, Buffer b)
1678 USED(p);
1679 USED(s);
1682 static Buffer
1683 readptr(Param *p, uchar *s, int y)
1685 Buffer b;
1686 uchar *q;
1688 USED(s);
1689 q = p->bytermin + y*p->bwidth;
1690 b.red = q; /* ptr to data */
1691 b.grn = b.blu = b.grey = b.alpha = nil;
1692 b.rgba = (u32int*)q;
1693 b.delta = p->img->depth/8;
1694 return b;
1697 static Buffer
1698 boolmemmove(Buffer bdst, Buffer bsrc, Buffer b1, int dx, int i, int o)
1700 USED(i);
1701 USED(o);
1702 memmove(bdst.red, bsrc.red, dx*bdst.delta);
1703 return bdst;
1706 static Buffer
1707 boolcopy8(Buffer bdst, Buffer bsrc, Buffer bmask, int dx, int i, int o)
1709 uchar *m, *r, *w, *ew;
1711 USED(i);
1712 USED(o);
1713 m = bmask.grey;
1714 w = bdst.red;
1715 r = bsrc.red;
1716 ew = w+dx;
1717 for(; w < ew; w++,r++)
1718 if(*m++)
1719 *w = *r;
1720 return bdst; /* not used */
1723 static Buffer
1724 boolcopy16(Buffer bdst, Buffer bsrc, Buffer bmask, int dx, int i, int o)
1726 uchar *m;
1727 ushort *r, *w, *ew;
1729 USED(i);
1730 USED(o);
1731 m = bmask.grey;
1732 w = (ushort*)bdst.red;
1733 r = (ushort*)bsrc.red;
1734 ew = w+dx;
1735 for(; w < ew; w++,r++)
1736 if(*m++)
1737 *w = *r;
1738 return bdst; /* not used */
1741 static Buffer
1742 boolcopy24(Buffer bdst, Buffer bsrc, Buffer bmask, int dx, int i, int o)
1744 uchar *m;
1745 uchar *r, *w, *ew;
1747 USED(i);
1748 USED(o);
1749 m = bmask.grey;
1750 w = bdst.red;
1751 r = bsrc.red;
1752 ew = w+dx*3;
1753 while(w < ew){
1754 if(*m++){
1755 *w++ = *r++;
1756 *w++ = *r++;
1757 *w++ = *r++;
1758 }else{
1759 w += 3;
1760 r += 3;
1763 return bdst; /* not used */
1766 static Buffer
1767 boolcopy32(Buffer bdst, Buffer bsrc, Buffer bmask, int dx, int i, int o)
1769 uchar *m;
1770 u32int *r, *w, *ew;
1772 USED(i);
1773 USED(o);
1774 m = bmask.grey;
1775 w = (u32int*)bdst.red;
1776 r = (u32int*)bsrc.red;
1777 ew = w+dx;
1778 for(; w < ew; w++,r++)
1779 if(*m++)
1780 *w = *r;
1781 return bdst; /* not used */
1784 static Buffer
1785 genconv(Param *p, uchar *buf, int y)
1787 Buffer b;
1788 int nb;
1789 uchar *r, *w, *ew;
1791 /* read from source into RGB format in convbuf */
1792 b = p->convreadcall(p, p->convbuf, y);
1794 /* write RGB format into dst format in buf */
1795 p->convwritecall(p->convdpar, buf, b);
1797 if(p->convdx){
1798 nb = p->convdpar->img->depth/8;
1799 r = buf;
1800 w = buf+nb*p->dx;
1801 ew = buf+nb*p->convdx;
1802 while(w<ew)
1803 *w++ = *r++;
1806 b.red = buf;
1807 b.blu = b.grn = b.grey = b.alpha = nil;
1808 b.rgba = (u32int*)buf;
1809 b.delta = 0;
1811 return b;
1814 static Readfn*
1815 convfn(Memimage *dst, Param *dpar, Memimage *src, Param *spar)
1817 if(dst->chan == src->chan && !(src->flags&Frepl)){
1818 //if(drawdebug) iprint("readptr...");
1819 return readptr;
1822 if(dst->chan==CMAP8 && (src->chan==GREY1||src->chan==GREY2||src->chan==GREY4)){
1823 /* cheat because we know the replicated value is exactly the color map entry. */
1824 //if(drawdebug) iprint("Readnbit...");
1825 return readnbit;
1828 spar->convreadcall = readfn(src);
1829 spar->convwritecall = writefn(dst);
1830 spar->convdpar = dpar;
1832 /* allocate a conversion buffer */
1833 spar->convbufoff = ndrawbuf;
1834 ndrawbuf += spar->dx*4;
1836 if(spar->dx > Dx(spar->img->r)){
1837 spar->convdx = spar->dx;
1838 spar->dx = Dx(spar->img->r);
1841 //if(drawdebug) iprint("genconv...");
1842 return genconv;
1846 * Do NOT call this directly. pixelbits is a wrapper
1847 * around this that fetches the bits from the X server
1848 * when necessary.
1850 u32int
1851 _pixelbits(Memimage *i, Point pt)
1853 uchar *p;
1854 u32int val;
1855 int off, bpp, npack;
1857 val = 0;
1858 p = byteaddr(i, pt);
1859 switch(bpp=i->depth){
1860 case 1:
1861 case 2:
1862 case 4:
1863 npack = 8/bpp;
1864 off = pt.x%npack;
1865 val = p[0] >> bpp*(npack-1-off);
1866 val &= (1<<bpp)-1;
1867 break;
1868 case 8:
1869 val = p[0];
1870 break;
1871 case 16:
1872 val = p[0]|(p[1]<<8);
1873 break;
1874 case 24:
1875 val = p[0]|(p[1]<<8)|(p[2]<<16);
1876 break;
1877 case 32:
1878 val = p[0]|(p[1]<<8)|(p[2]<<16)|(p[3]<<24);
1879 break;
1881 while(bpp<32){
1882 val |= val<<bpp;
1883 bpp *= 2;
1885 return val;
1888 static Calcfn*
1889 boolcopyfn(Memimage *img, Memimage *mask)
1891 if(mask->flags&Frepl && Dx(mask->r)==1 && Dy(mask->r)==1 && pixelbits(mask, mask->r.min)==~0)
1892 return boolmemmove;
1894 switch(img->depth){
1895 case 8:
1896 return boolcopy8;
1897 case 16:
1898 return boolcopy16;
1899 case 24:
1900 return boolcopy24;
1901 case 32:
1902 return boolcopy32;
1903 default:
1904 assert(0 /* boolcopyfn */);
1906 return 0;
1910 * Optimized draw for filling and scrolling; uses memset and memmove.
1912 static void
1913 memsets(void *vp, ushort val, int n)
1915 ushort *p, *ep;
1917 p = vp;
1918 ep = p+n;
1919 while(p<ep)
1920 *p++ = val;
1923 static void
1924 memsetl(void *vp, u32int val, int n)
1926 u32int *p, *ep;
1928 p = vp;
1929 ep = p+n;
1930 while(p<ep)
1931 *p++ = val;
1934 static void
1935 memset24(void *vp, u32int val, int n)
1937 uchar *p, *ep;
1938 uchar a,b,c;
1940 p = vp;
1941 ep = p+3*n;
1942 a = val;
1943 b = val>>8;
1944 c = val>>16;
1945 while(p<ep){
1946 *p++ = a;
1947 *p++ = b;
1948 *p++ = c;
1952 u32int
1953 _imgtorgba(Memimage *img, u32int val)
1955 uchar r, g, b, a;
1956 int nb, ov, v;
1957 u32int chan;
1958 uchar *p;
1960 a = 0xFF;
1961 r = g = b = 0xAA; /* garbage */
1962 for(chan=img->chan; chan; chan>>=8){
1963 nb = NBITS(chan);
1964 ov = v = val&((1<<nb)-1);
1965 val >>= nb;
1967 while(nb < 8){
1968 v |= v<<nb;
1969 nb *= 2;
1971 v >>= (nb-8);
1973 switch(TYPE(chan)){
1974 case CRed:
1975 r = v;
1976 break;
1977 case CGreen:
1978 g = v;
1979 break;
1980 case CBlue:
1981 b = v;
1982 break;
1983 case CAlpha:
1984 a = v;
1985 break;
1986 case CGrey:
1987 r = g = b = v;
1988 break;
1989 case CMap:
1990 p = img->cmap->cmap2rgb+3*ov;
1991 r = *p++;
1992 g = *p++;
1993 b = *p;
1994 break;
1997 return (r<<24)|(g<<16)|(b<<8)|a;
2000 u32int
2001 _rgbatoimg(Memimage *img, u32int rgba)
2003 u32int chan;
2004 int d, nb;
2005 u32int v;
2006 uchar *p, r, g, b, a, m;
2008 v = 0;
2009 r = rgba>>24;
2010 g = rgba>>16;
2011 b = rgba>>8;
2012 a = rgba;
2013 d = 0;
2014 for(chan=img->chan; chan; chan>>=8){
2015 nb = NBITS(chan);
2016 switch(TYPE(chan)){
2017 case CRed:
2018 v |= (r>>(8-nb))<<d;
2019 break;
2020 case CGreen:
2021 v |= (g>>(8-nb))<<d;
2022 break;
2023 case CBlue:
2024 v |= (b>>(8-nb))<<d;
2025 break;
2026 case CAlpha:
2027 v |= (a>>(8-nb))<<d;
2028 break;
2029 case CMap:
2030 p = img->cmap->rgb2cmap;
2031 m = p[(r>>4)*256+(g>>4)*16+(b>>4)];
2032 v |= (m>>(8-nb))<<d;
2033 break;
2034 case CGrey:
2035 m = RGB2K(r,g,b);
2036 v |= (m>>(8-nb))<<d;
2037 break;
2039 d += nb;
2041 // print("rgba2img %.8lux = %.*lux\n", rgba, 2*d/8, v);
2042 return v;
2045 #define DBG if(0)
2046 static int
2047 memoptdraw(Memdrawparam *par)
2049 int m, y, dy, dx, op;
2050 u32int v;
2051 Memimage *src;
2052 Memimage *dst;
2054 dx = Dx(par->r);
2055 dy = Dy(par->r);
2056 src = par->src;
2057 dst = par->dst;
2058 op = par->op;
2060 DBG print("state %lux mval %lux dd %d\n", par->state, par->mval, dst->depth);
2062 * If we have an opaque mask and source is one opaque pixel we can convert to the
2063 * destination format and just replicate with memset.
2065 m = Simplesrc|Simplemask|Fullmask;
2066 if((par->state&m)==m && (par->srgba&0xFF) == 0xFF && (op ==S || op == SoverD)){
2067 uchar *dp, p[4];
2068 int d, dwid, ppb, np, nb;
2069 uchar lm, rm;
2071 DBG print("memopt, dst %p, dst->data->bdata %p\n", dst, dst->data->bdata);
2072 dwid = dst->width*sizeof(u32int);
2073 dp = byteaddr(dst, par->r.min);
2074 v = par->sdval;
2075 DBG print("sdval %lud, depth %d\n", v, dst->depth);
2076 switch(dst->depth){
2077 case 1:
2078 case 2:
2079 case 4:
2080 for(d=dst->depth; d<8; d*=2)
2081 v |= (v<<d);
2082 ppb = 8/dst->depth; /* pixels per byte */
2083 m = ppb-1;
2084 /* left edge */
2085 np = par->r.min.x&m; /* no. pixels unused on left side of word */
2086 dx -= (ppb-np);
2087 nb = 8 - np * dst->depth; /* no. bits used on right side of word */
2088 lm = (1<<nb)-1;
2089 DBG print("np %d x %d nb %d lm %ux ppb %d m %ux\n", np, par->r.min.x, nb, lm, ppb, m);
2091 /* right edge */
2092 np = par->r.max.x&m; /* no. pixels used on left side of word */
2093 dx -= np;
2094 nb = 8 - np * dst->depth; /* no. bits unused on right side of word */
2095 rm = ~((1<<nb)-1);
2096 DBG print("np %d x %d nb %d rm %ux ppb %d m %ux\n", np, par->r.max.x, nb, rm, ppb, m);
2098 DBG print("dx %d Dx %d\n", dx, Dx(par->r));
2099 /* lm, rm are masks that are 1 where we should touch the bits */
2100 if(dx < 0){ /* just one byte */
2101 lm &= rm;
2102 for(y=0; y<dy; y++, dp+=dwid)
2103 *dp ^= (v ^ *dp) & lm;
2104 }else if(dx == 0){ /* no full bytes */
2105 if(lm)
2106 dwid--;
2108 for(y=0; y<dy; y++, dp+=dwid){
2109 if(lm){
2110 DBG print("dp %p v %lux lm %ux (v ^ *dp) & lm %lux\n", dp, v, lm, (v^*dp)&lm);
2111 *dp ^= (v ^ *dp) & lm;
2112 dp++;
2114 *dp ^= (v ^ *dp) & rm;
2116 }else{ /* full bytes in middle */
2117 dx /= ppb;
2118 if(lm)
2119 dwid--;
2120 dwid -= dx;
2122 for(y=0; y<dy; y++, dp+=dwid){
2123 if(lm){
2124 *dp ^= (v ^ *dp) & lm;
2125 dp++;
2127 memset(dp, v, dx);
2128 dp += dx;
2129 *dp ^= (v ^ *dp) & rm;
2132 return 1;
2133 case 8:
2134 for(y=0; y<dy; y++, dp+=dwid)
2135 memset(dp, v, dx);
2136 return 1;
2137 case 16:
2138 p[0] = v; /* make little endian */
2139 p[1] = v>>8;
2140 v = *(ushort*)p;
2141 DBG print("dp=%p; dx=%d; for(y=0; y<%d; y++, dp+=%d)\nmemsets(dp, v, dx);\n",
2142 dp, dx, dy, dwid);
2143 for(y=0; y<dy; y++, dp+=dwid)
2144 memsets(dp, v, dx);
2145 return 1;
2146 case 24:
2147 for(y=0; y<dy; y++, dp+=dwid)
2148 memset24(dp, v, dx);
2149 return 1;
2150 case 32:
2151 p[0] = v; /* make little endian */
2152 p[1] = v>>8;
2153 p[2] = v>>16;
2154 p[3] = v>>24;
2155 v = *(u32int*)p;
2156 for(y=0; y<dy; y++, dp+=dwid)
2157 memsetl(dp, v, dx);
2158 return 1;
2159 default:
2160 assert(0 /* bad dest depth in memoptdraw */);
2165 * If no source alpha, an opaque mask, we can just copy the
2166 * source onto the destination. If the channels are the same and
2167 * the source is not replicated, memmove suffices.
2169 m = Simplemask|Fullmask;
2170 if((par->state&(m|Replsrc))==m && src->depth >= 8
2171 && src->chan == dst->chan && !(src->flags&Falpha) && (op == S || op == SoverD)){
2172 uchar *sp, *dp;
2173 long swid, dwid, nb;
2174 int dir;
2176 if(src->data == dst->data && byteaddr(dst, par->r.min) > byteaddr(src, par->sr.min))
2177 dir = -1;
2178 else
2179 dir = 1;
2181 swid = src->width*sizeof(u32int);
2182 dwid = dst->width*sizeof(u32int);
2183 sp = byteaddr(src, par->sr.min);
2184 dp = byteaddr(dst, par->r.min);
2185 if(dir == -1){
2186 sp += (dy-1)*swid;
2187 dp += (dy-1)*dwid;
2188 swid = -swid;
2189 dwid = -dwid;
2191 nb = (dx*src->depth)/8;
2192 for(y=0; y<dy; y++, sp+=swid, dp+=dwid)
2193 memmove(dp, sp, nb);
2194 return 1;
2198 * If we have a 1-bit mask, 1-bit source, and 1-bit destination, and
2199 * they're all bit aligned, we can just use bit operators. This happens
2200 * when we're manipulating boolean masks, e.g. in the arc code.
2202 if((par->state&(Simplemask|Simplesrc|Replmask|Replsrc))==0
2203 && dst->chan==GREY1 && src->chan==GREY1 && par->mask->chan==GREY1
2204 && (par->r.min.x&7)==(par->sr.min.x&7) && (par->r.min.x&7)==(par->mr.min.x&7)){
2205 uchar *sp, *dp, *mp;
2206 uchar lm, rm;
2207 long swid, dwid, mwid;
2208 int i, x, dir;
2210 sp = byteaddr(src, par->sr.min);
2211 dp = byteaddr(dst, par->r.min);
2212 mp = byteaddr(par->mask, par->mr.min);
2213 swid = src->width*sizeof(u32int);
2214 dwid = dst->width*sizeof(u32int);
2215 mwid = par->mask->width*sizeof(u32int);
2217 if(src->data == dst->data && byteaddr(dst, par->r.min) > byteaddr(src, par->sr.min)){
2218 dir = -1;
2219 }else
2220 dir = 1;
2222 lm = 0xFF>>(par->r.min.x&7);
2223 rm = 0xFF<<(8-(par->r.max.x&7));
2224 dx -= (8-(par->r.min.x&7)) + (par->r.max.x&7);
2226 if(dx < 0){ /* one byte wide */
2227 lm &= rm;
2228 if(dir == -1){
2229 dp += dwid*(dy-1);
2230 sp += swid*(dy-1);
2231 mp += mwid*(dy-1);
2232 dwid = -dwid;
2233 swid = -swid;
2234 mwid = -mwid;
2236 for(y=0; y<dy; y++){
2237 *dp ^= (*dp ^ *sp) & *mp & lm;
2238 dp += dwid;
2239 sp += swid;
2240 mp += mwid;
2242 return 1;
2245 dx /= 8;
2246 if(dir == 1){
2247 i = (lm!=0)+dx+(rm!=0);
2248 mwid -= i;
2249 swid -= i;
2250 dwid -= i;
2251 for(y=0; y<dy; y++, dp+=dwid, sp+=swid, mp+=mwid){
2252 if(lm){
2253 *dp ^= (*dp ^ *sp++) & *mp++ & lm;
2254 dp++;
2256 for(x=0; x<dx; x++){
2257 *dp ^= (*dp ^ *sp++) & *mp++;
2258 dp++;
2260 if(rm){
2261 *dp ^= (*dp ^ *sp++) & *mp++ & rm;
2262 dp++;
2265 return 1;
2266 }else{
2267 /* dir == -1 */
2268 i = (lm!=0)+dx+(rm!=0);
2269 dp += dwid*(dy-1)+i-1;
2270 sp += swid*(dy-1)+i-1;
2271 mp += mwid*(dy-1)+i-1;
2272 dwid = -dwid+i;
2273 swid = -swid+i;
2274 mwid = -mwid+i;
2275 for(y=0; y<dy; y++, dp+=dwid, sp+=swid, mp+=mwid){
2276 if(rm){
2277 *dp ^= (*dp ^ *sp--) & *mp-- & rm;
2278 dp--;
2280 for(x=0; x<dx; x++){
2281 *dp ^= (*dp ^ *sp--) & *mp--;
2282 dp--;
2284 if(lm){
2285 *dp ^= (*dp ^ *sp--) & *mp-- & lm;
2286 dp--;
2290 return 1;
2292 return 0;
2294 #undef DBG
2297 * Boolean character drawing.
2298 * Solid opaque color through a 1-bit greyscale mask.
2300 #define DBG if(0)
2301 static int
2302 chardraw(Memdrawparam *par)
2304 u32int bits;
2305 int i, ddepth, dy, dx, x, bx, ex, y, npack, bsh, depth, op;
2306 u32int v, maskwid, dstwid;
2307 uchar *wp, *rp, *q, *wc;
2308 ushort *ws;
2309 u32int *wl;
2310 uchar sp[4];
2311 Rectangle r, mr;
2312 Memimage *mask, *src, *dst;
2314 if(0) if(drawdebug) iprint("chardraw? mf %lux md %d sf %lux dxs %d dys %d dd %d ddat %p sdat %p\n",
2315 par->mask->flags, par->mask->depth, par->src->flags,
2316 Dx(par->src->r), Dy(par->src->r), par->dst->depth, par->dst->data, par->src->data);
2318 mask = par->mask;
2319 src = par->src;
2320 dst = par->dst;
2321 r = par->r;
2322 mr = par->mr;
2323 op = par->op;
2325 if((par->state&(Replsrc|Simplesrc|Replmask)) != (Replsrc|Simplesrc)
2326 || mask->depth != 1 || src->flags&Falpha || dst->depth<8 || dst->data==src->data
2327 || op != SoverD)
2328 return 0;
2330 //if(drawdebug) iprint("chardraw...");
2332 depth = mask->depth;
2333 maskwid = mask->width*sizeof(u32int);
2334 rp = byteaddr(mask, mr.min);
2335 npack = 8/depth;
2336 bsh = (mr.min.x % npack) * depth;
2338 wp = byteaddr(dst, r.min);
2339 dstwid = dst->width*sizeof(u32int);
2340 DBG print("bsh %d\n", bsh);
2341 dy = Dy(r);
2342 dx = Dx(r);
2344 ddepth = dst->depth;
2347 * for loop counts from bsh to bsh+dx
2349 * we want the bottom bits to be the amount
2350 * to shift the pixels down, so for n≡0 (mod 8) we want
2351 * bottom bits 7. for n≡1, 6, etc.
2352 * the bits come from -n-1.
2355 bx = -bsh-1;
2356 ex = -bsh-1-dx;
2357 SET(bits);
2358 v = par->sdval;
2360 /* make little endian */
2361 sp[0] = v;
2362 sp[1] = v>>8;
2363 sp[2] = v>>16;
2364 sp[3] = v>>24;
2366 //print("sp %x %x %x %x\n", sp[0], sp[1], sp[2], sp[3]);
2367 for(y=0; y<dy; y++, rp+=maskwid, wp+=dstwid){
2368 q = rp;
2369 if(bsh)
2370 bits = *q++;
2371 switch(ddepth){
2372 case 8:
2373 //if(drawdebug) iprint("8loop...");
2374 wc = wp;
2375 for(x=bx; x>ex; x--, wc++){
2376 i = x&7;
2377 if(i == 8-1)
2378 bits = *q++;
2379 DBG print("bits %lux sh %d...", bits, i);
2380 if((bits>>i)&1)
2381 *wc = v;
2383 break;
2384 case 16:
2385 ws = (ushort*)wp;
2386 v = *(ushort*)sp;
2387 for(x=bx; x>ex; x--, ws++){
2388 i = x&7;
2389 if(i == 8-1)
2390 bits = *q++;
2391 DBG print("bits %lux sh %d...", bits, i);
2392 if((bits>>i)&1)
2393 *ws = v;
2395 break;
2396 case 24:
2397 wc = wp;
2398 for(x=bx; x>ex; x--, wc+=3){
2399 i = x&7;
2400 if(i == 8-1)
2401 bits = *q++;
2402 DBG print("bits %lux sh %d...", bits, i);
2403 if((bits>>i)&1){
2404 wc[0] = sp[0];
2405 wc[1] = sp[1];
2406 wc[2] = sp[2];
2409 break;
2410 case 32:
2411 wl = (u32int*)wp;
2412 v = *(u32int*)sp;
2413 for(x=bx; x>ex; x--, wl++){
2414 i = x&7;
2415 if(i == 8-1)
2416 bits = *q++;
2417 DBG iprint("bits %lux sh %d...", bits, i);
2418 if((bits>>i)&1)
2419 *wl = v;
2421 break;
2425 DBG print("\n");
2426 return 1;
2428 #undef DBG
2432 * Fill entire byte with replicated (if necessary) copy of source pixel,
2433 * assuming destination ldepth is >= source ldepth.
2435 * This code is just plain wrong for >8bpp.
2437 u32int
2438 membyteval(Memimage *src)
2440 int i, val, bpp;
2441 uchar uc;
2443 unloadmemimage(src, src->r, &uc, 1);
2444 bpp = src->depth;
2445 uc <<= (src->r.min.x&(7/src->depth))*src->depth;
2446 uc &= ~(0xFF>>bpp);
2447 * pixel value is now in high part of byte. repeat throughout byte
2448 val = uc;
2449 for(i=bpp; i<8; i<<=1)
2450 val |= val>>i;
2451 return val;
2456 void
2457 _memfillcolor(Memimage *i, u32int val)
2459 u32int bits;
2460 int d, y;
2461 uchar p[4];
2463 if(val == DNofill)
2464 return;
2466 bits = _rgbatoimg(i, val);
2467 switch(i->depth){
2468 case 24: /* 24-bit images suck */
2469 for(y=i->r.min.y; y<i->r.max.y; y++)
2470 memset24(byteaddr(i, Pt(i->r.min.x, y)), bits, Dx(i->r));
2471 break;
2472 default: /* 1, 2, 4, 8, 16, 32 */
2473 for(d=i->depth; d<32; d*=2)
2474 bits = (bits << d) | bits;
2475 p[0] = bits; /* make little endian */
2476 p[1] = bits>>8;
2477 p[2] = bits>>16;
2478 p[3] = bits>>24;
2479 bits = *(u32int*)p;
2480 memsetl(wordaddr(i, i->r.min), bits, i->width*Dy(i->r));
2481 break;