aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDaniel Eklöf <daniel@ekloef.se>2020-12-29 18:42:56 +0000
committerDaniel Eklöf <daniel@ekloef.se>2021-01-01 20:09:37 +0000
commit878adb970e1532224bf25eb25e1ecc86b6331262 (patch)
tree289ea257e136d25651435ab2aeeb34e11e6f514d
parentbox-drawing: wrap {h,v}hline() functions in macros, to shorten argument list (diff)
box-drawing: make sure we never walk outside the (image) buffer
-rw-r--r--box-drawing.c27
1 files changed, 14 insertions, 13 deletions
diff --git a/box-drawing.c b/box-drawing.c
index 86cc00f7..b368210a 100644
--- a/box-drawing.c
+++ b/box-drawing.c
@@ -22,10 +22,10 @@ thickness(float pts, int dpi)
}
static void
-_hline(uint8_t *buf, int x1, int x2, int y, int thick, int stride)
+_hline(uint8_t *buf, int x1, int x2, int y, int thick, int width, int height, int stride)
{
- for (size_t row = y; row < y + thick; row++) {
- for (size_t col = x1; col < x2; col++) {
+ for (size_t row = max(y, 0); row < min(y + thick, height); row++) {
+ for (size_t col = max(x1, 0); col < min(x2, width); col++) {
size_t idx = col / 8;
size_t bit_no = col % 8;
buf[row * stride + idx] |= 1 << bit_no;
@@ -33,13 +33,13 @@ _hline(uint8_t *buf, int x1, int x2, int y, int thick, int stride)
}
}
-#define hline(x1, x2, y, thick) _hline(buf, x1, x2, y, thick, stride)
+#define hline(x1, x2, y, thick) _hline(buf, x1, x2, y, thick, width, height, stride)
static void
-_vline(uint8_t *buf, int y1, int y2, int x, int thick, int stride)
+_vline(uint8_t *buf, int y1, int y2, int x, int thick, int width, int height, int stride)
{
- for (size_t row = y1; row < y2; row++) {
- for (size_t col = x; col < x + thick; col++) {
+ for (size_t row = max(y1, 0); row < min(y2, height); row++) {
+ for (size_t col = max(x, 0); col < min(x + thick, width); col++) {
size_t idx = col / 8;
size_t bit_no = col % 8;
buf[row * stride + idx] |= 1 << bit_no;
@@ -47,13 +47,13 @@ _vline(uint8_t *buf, int y1, int y2, int x, int thick, int stride)
}
}
-#define vline(y1, y2, x, thick) _vline(buf, y1, y2, x, thick, stride)
+#define vline(y1, y2, x, thick) _vline(buf, y1, y2, x, thick, width, height, stride)
static void
-rectangle(uint8_t *buf, int x1, int y1, int x2, int y2, int stride)
+_rect(uint8_t *buf, int x1, int y1, int x2, int y2, int width, int height, int stride)
{
- for (size_t row = y1; row < y2; row++) {
- for (size_t col = x1; col < x2; col++) {
+ for (size_t row = max(y1, 0); row < min(y2, height); row++) {
+ for (size_t col = max(x1, 0); col < min(x2, width); col++) {
size_t idx = col / 8;
size_t bit_no = col % 8;
buf[row * stride + idx] |= 1 << bit_no;
@@ -61,6 +61,9 @@ rectangle(uint8_t *buf, int x1, int y1, int x2, int y2, int stride)
}
}
+#define rect(x1, y1, x2, y2) _rect(buf, x1, y1, x2, y2, width, height, stride)
+
+
#define _hline_middle_left(_vthick, _hthick) \
do { \
int vthick = thickness(_vthick, dpi); \
@@ -94,8 +97,6 @@ rectangle(uint8_t *buf, int x1, int y1, int x2, int y2, int stride)
#define vline_middle_up(thick) _vline_middle_up(thick, thick)
#define vline_middle_down(thick) _vline_middle_down(thick, thick)
-#define rect(x1, y1, x2, y2) rectangle(buf, x1, y1, x2, y2, stride)
-
#define quad_upper_left() rect(0, 0, ceil(width / 2.), ceil(height / 2.))
#define quad_upper_right() rect(floor(width / 2.), 0, width, ceil(height / 2.))
#define quad_lower_left() rect(0, floor(height / 2.), ceil(width / 2.), height)