892fc49fb58e5ceca084e402a5f96b1e31070bc7
[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_draw.h"
16 #include "ao_draw_int.h"
17
18 static inline uint32_t
19 ao_pattern_expand(uint8_t v, uint8_t rot)
20 {
21         uint32_t        r;
22
23         if (rot)
24                 v = (uint8_t) ao_left(v, 8-rot) | (uint8_t) ao_right(v, rot);
25         r = v;
26         return (r << 24) | (r << 16) | (r << 8) | (r);
27 }
28
29 static inline int
30 min(int a, int b) {
31         return a < b ? a : b;
32 }
33
34 void
35 ao_pattern(const struct ao_bitmap       *dst,
36            int16_t                      x,
37            int16_t                      y,
38            int16_t                      width,
39            int16_t                      height,
40            const struct ao_pattern      *pattern,
41            int16_t                      pat_x,
42            int16_t                      pat_y,
43            uint8_t                      rop)
44 {
45         uint32_t        pat[8];
46
47         int16_t x2 = x + width;
48         int16_t y2 = y + height;
49
50         ao_clip(x, 0, dst->width);
51         ao_clip(x2, 0, dst->width);
52         ao_clip(y, 0, dst->height);
53         ao_clip(y2, 0, dst->height);
54
55         if (x < x2 && y < y2) {
56                 uint8_t xrot = (x - pat_x) & 7;
57                 uint8_t yrot = (y - pat_y) & 7;
58                 uint8_t i;
59                 int16_t dst_x, dst_y;
60
61                 for (i = 0; i < 8; i++)
62                         pat[(i + yrot) & 7] = ao_pattern_expand(pattern->pattern[i], xrot);
63                 for (dst_y = y; dst_y < y2; dst_y += 8) {
64                         int16_t h = (int16_t) min(y2 - dst_y, 8);
65                         for (dst_x = x; dst_x < x2; dst_x += 8) {
66                                 int16_t w = (int16_t) min(x2 - dst_x, 8);
67
68                                 ao_blt(pat, 1, 0,
69                                        dst->base + dst_y * dst->stride,
70                                        dst->stride,
71                                        dst_x,
72                                        w, h,
73                                        rop,
74                                        0, 0);
75                         }
76                 }
77         }
78 }
79