altos/telelco-v2.0: A bit fancier with the drag-mode LED show
[fw/altos] / src / draw / ao_pattern.c
1 /*
2  * Copyright © 2016 Keith Packard <keithp@keithp.com>
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * General Public License for more details.
13  */
14
15 #include "ao.h"
16 #include "ao_draw.h"
17 #include "ao_draw_int.h"
18
19 static inline uint32_t
20 ao_pattern_expand(uint8_t v, uint8_t rot)
21 {
22         uint32_t        r;
23
24         if (rot)
25                 v = ao_left(v, 8-rot) | ao_right(v, rot);
26         r = v;
27         return (r << 24) | (r << 16) | (r << 8) | (r);
28 }
29
30 static inline int
31 min(int a, int b) {
32         return a < b ? a : b;
33 }
34
35 void
36 ao_pattern(const struct ao_bitmap       *dst,
37            int16_t                      x,
38            int16_t                      y,
39            int16_t                      width,
40            int16_t                      height,
41            const struct ao_pattern      *pattern,
42            int16_t                      pat_x,
43            int16_t                      pat_y,
44            uint8_t                      rop)
45 {
46         uint32_t        pat[8];
47
48         int16_t x2 = x + width;
49         int16_t y2 = y + height;
50
51         ao_clip(x, 0, dst->width);
52         ao_clip(x2, 0, dst->width);
53         ao_clip(y, 0, dst->height);
54         ao_clip(y2, 0, dst->height);
55
56         if (x < x2 && y < y2) {
57                 int     xrot = (x - pat_x) & 7;
58                 int     yrot = (y - pat_y) & 7;
59                 int     i;
60                 int16_t dst_x, dst_y;
61
62                 for (i = 0; i < 8; i++)
63                         pat[(i + yrot) & 7] = ao_pattern_expand(pattern->pattern[i], xrot);
64                 for (dst_y = y; dst_y < y2; dst_y += 8) {
65                         int     h = min(y2 - dst_y, 8);
66                         for (dst_x = x; dst_x < x2; dst_x += 8) {
67                                 int     w = min(x2 - dst_x, 8);
68
69                                 ao_blt(pat, 1, 0,
70                                        dst->base + dst_y * dst->stride,
71                                        dst->stride,
72                                        dst_x,
73                                        w, h,
74                                        rop,
75                                        0, 0);
76                         }
77                 }
78         }
79 }
80