altos/telelco-v2.0: A bit fancier with the drag-mode LED show
[fw/altos] / src / draw / ao_copy.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 #define bound(val,max,other) do {               \
20                 if (val < 0) {                  \
21                         other -= val;           \
22                         val = 0;                \
23                 }                               \
24                 if (val > max) {                \
25                         other -= (val - max);   \
26                         val = max;              \
27                 }                               \
28         } while (0)
29
30 #define bound2(a, max_a, b, max_b) do {         \
31                 bound(a, max_a, b);             \
32                 bound(b, max_b, a);             \
33         } while (0)
34
35 void
36 ao_copy(const struct ao_bitmap  *dst,
37         int16_t                 dst_x,
38         int16_t                 dst_y,
39         int16_t                 width,
40         int16_t                 height,
41         const struct ao_bitmap  *src,
42         int16_t                 src_x,
43         int16_t                 src_y,
44         uint8_t                 rop)
45 {
46         int16_t         dst_x2 = dst_x + width, dst_y2 = dst_y + height;
47         int16_t         src_x2 = src_x + width, src_y2 = src_y + height;
48         uint8_t         reverse = 0;
49         uint8_t         upsidedown = 0;
50
51         bound2(dst_x, dst->width, src_x, src->width);
52         bound2(dst_x2, dst->width, src_x2, src->width);
53         bound2(dst_y, dst->height, src_y, src->height);
54         bound2(dst_y2, dst->height, src_y2, src->height);
55
56         if (dst == src) {
57                 reverse = (dst_x > src_x);
58                 upsidedown = (dst_y > src_y);
59         }
60
61         if (dst_x < dst_x2 && dst_y < dst_y2) {
62                 ao_blt(src->base + src_y * src->stride,
63                        src->stride,
64                        src_x,
65                        dst->base + dst_y * dst->stride,
66                        dst->stride,
67                        dst_x,
68                        dst_x2 - dst_x,
69                        dst_y2 - dst_y,
70                        rop,
71                        reverse,
72                        upsidedown);
73         }
74 }
75