about summary refs log tree commit diff
diff options
context:
space:
mode:
authorsternenseemann <0rpkxez4ksa01gb3typccl0i@systemli.org>2020-12-16 23:10:45 +0100
committersternenseemann <0rpkxez4ksa01gb3typccl0i@systemli.org>2020-12-16 23:10:45 +0100
commit6676dc51345b5a4a7005ebd6bfe25486fcf7b4a8 (patch)
tree9a60f34041f9541f0ed9bf96443123be68008729
parent97c6e06ef335b7f87cfadf0607136322fc2ad14b (diff)
feat(bitmap): allow to specify fallback color
This changes the prototype of bs_bitmap_get and bs_view_bitarray, but
errno is still updated accordingly.

fix(bs-renderflipdot): Background is all white using -S or -P with -i
-rw-r--r--bitmap.c8
-rw-r--r--bs-renderflipdot.c4
-rw-r--r--include/buchstabensuppe/bitmap.h10
3 files changed, 13 insertions, 9 deletions
diff --git a/bitmap.c b/bitmap.c
index ab20def..98140e3 100644
--- a/bitmap.c
+++ b/bitmap.c
@@ -103,11 +103,11 @@ void bs_bitmap_free(bs_bitmap_t *b) {
   }
 }
 
-unsigned char bs_bitmap_get(bs_bitmap_t b, int x, int y) {
+unsigned char bs_bitmap_get(bs_bitmap_t b, int x, int y, unsigned char def) {
   if(x < 0 || y < 0 ||
       x >= b.bs_bitmap_width || y >= b.bs_bitmap_height) {
     errno = EINVAL;
-    return 0;
+    return def;
   }
 
   return b.bs_bitmap[y * b.bs_bitmap_width + x];
@@ -150,7 +150,7 @@ void bs_bitmap_print(bs_bitmap_t bitmap, bool binary) {
   }
 }
 
-uint8_t *bs_view_bitarray(bs_view_t view, size_t *size) {
+uint8_t *bs_view_bitarray(bs_view_t view, size_t *size, unsigned char def) {
   int view_max_y = view.bs_view_offset_y + view.bs_view_height;
   int view_max_x = view.bs_view_offset_x + view.bs_view_width;
 
@@ -172,7 +172,7 @@ uint8_t *bs_view_bitarray(bs_view_t view, size_t *size) {
         // reduce pixel to a single bit works regardless of monospace and
         // grayscale bitmaps -- however grayscale bitmaps are not converted
         // on the fly TODO
-        uint8_t pixel_val = bs_bitmap_get(view.bs_view_bitmap, x + i, y) > 0;
+        uint8_t pixel_val = bs_bitmap_get(view.bs_view_bitmap, x + i, y, def) > 0;
         byte |= pixel_val << (7 - i);
       }
 
diff --git a/bs-renderflipdot.c b/bs-renderflipdot.c
index 03ea185..19a66ac 100644
--- a/bs-renderflipdot.c
+++ b/bs-renderflipdot.c
@@ -141,7 +141,7 @@ bool render_flipdot(const char *host, const char *port, int family, const char *
     // render first frame immediately
 
     size_t bits_size;
-    uint8_t *bits = bs_view_bitarray(view, &bits_size);
+    uint8_t *bits = bs_view_bitarray(view, &bits_size, invert);
 
     if(multiple_frames) {
       // TODO use sigaction
@@ -176,7 +176,7 @@ bool render_flipdot(const char *host, const char *port, int family, const char *
       if(!multiple_frames) {
         finished = true;
       } else if(!finished) {
-        bits = bs_view_bitarray(view, &bits_size);
+        bits = bs_view_bitarray(view, &bits_size, invert);
         pause();
       }
     }
diff --git a/include/buchstabensuppe/bitmap.h b/include/buchstabensuppe/bitmap.h
index 5b4af8d..c6a47e1 100644
--- a/include/buchstabensuppe/bitmap.h
+++ b/include/buchstabensuppe/bitmap.h
@@ -84,9 +84,9 @@ void bs_bitmap_set(bs_bitmap_t bitmap, int x, int y, unsigned char value);
  * @brief Get a pixel value
  *
  * Gets the value at the specified location. If the location
- * is out of bounds it returns 0 and sets `errno` to `EINVAL`.
+ * is out of bounds it returns `def` and sets `errno` to `EINVAL`.
  */
-unsigned char bs_bitmap_get(bs_bitmap_t bitmap, int x, int y);
+unsigned char bs_bitmap_get(bs_bitmap_t bitmap, int x, int y, unsigned char def);
 
 /*!
  * @brief Copy a bitmap into another one
@@ -124,10 +124,14 @@ void bs_bitmap_print(bs_bitmap_t bitmap, bool binary_image);
  * `view` describes the bitmap to be used and the area
  * of it. `size` will hold the length of the returned array.
  *
+ * If the `view` contains areas which are not covered by its
+ * bitmap, these pixels are treated as if they had the value
+ * `def`.
+ *
  * On error `NULL` is returned and `size` is 0. The allocated
  * memory must be freed by the caller.
  */
-uint8_t *bs_view_bitarray(bs_view_t view, size_t *size);
+uint8_t *bs_view_bitarray(bs_view_t view, size_t *size, unsigned char def);
 
 /*!
  * @name Bitmap Processing