aboutsummaryrefslogtreecommitdiff
path: root/src/xbm.c
diff options
context:
space:
mode:
authorFrançois Cartegnie <281376+fcartegnie@users.noreply.github.com>2024-06-11 11:29:21 +0700
committerGitHub <noreply@github.com>2024-06-11 11:29:21 +0700
commit1ae34de744aac664df8780365a549ea31c57ec13 (patch)
tree2cef86d7bb4e057a40c1d39c909910f96bc68056 /src/xbm.c
parent0d4a46593ce616f8a7a9eb93648b279f5dbee70d (diff)
parent8815700322c0c10cb0528b047b2c7604c2eeb053 (diff)
Merge pull request #13 from kienvo/xbm-support
Add support for .xbm bitmap file
Diffstat (limited to 'src/xbm.c')
-rw-r--r--src/xbm.c67
1 files changed, 67 insertions, 0 deletions
diff --git a/src/xbm.c b/src/xbm.c
new file mode 100644
index 0000000..9c16811
--- /dev/null
+++ b/src/xbm.c
@@ -0,0 +1,67 @@
+#include "xbm.h"
+#include <stdlib.h>
+#include <memory.h>
+
+#define ALIGNBIT(x) (((x % 8)>0)*8 + (x / 8) * 8)
+
+/**
+ * Draw bitmap file to fb at (col, row)
+ * if bitmap file larger than fb, it will be stripped out
+ */
+void xbm2fb(xbm_t *xbm, uint16_t *fb, int col, int row)
+{
+ int W = ALIGNBIT(xbm->w);
+ uint16_t *tmpfb = malloc(W * sizeof(uint16_t));
+ memset(tmpfb, 0, W * sizeof(uint16_t));
+
+ if ((xbm->h + row) >= 0 && col >= 0) {
+
+ for (int h = 0; h < xbm->h; h++) {
+ for (int w = 0; w < W; w++) {
+ tmpfb[w] |= ((xbm->bits[h*(W/8) + w/8] >> (w % 8)) & 0x01)
+ << (h+row);
+ }
+ }
+ }
+ for (int i=0; i < xbm->w; i++) {
+ fb[col+i] = tmpfb[i];
+ }
+}
+
+void xbm2fb_dirty(xbm_t *xbm, uint16_t *fb, int col, int row)
+{
+ // Byte align for xbm bytes
+ int W = ALIGNBIT(xbm->w);
+
+ for (int h = 0; h < xbm->h; h++) {
+ for (int w = 0; w < W; w++) {
+ fb[w] |= ((xbm->bits[h*(W/8) + w/8] >> (w % 8)) & 0x01) << (h+row);
+ }
+ }
+}
+
+/**
+ * Crop image horizontaly
+ * TODO: vertical crop
+ *
+ */
+xbm_t *xbm_croph(xbm_t *xbm, xbm_t *frame, int from_row, int to_row)
+{
+ int rb = xbm->w / 8 + ((xbm->w % 8)>0); // Number of bytes in a row
+ if (to_row * rb > rb * xbm->h)
+ return NULL;
+
+ frame->bits = &xbm->bits[rb * from_row];
+ frame->w = xbm->w;
+ frame->h = to_row - from_row;
+ frame->fh = xbm->fh;
+
+ return frame;
+}
+
+xbm_t *extract_frame(xbm_t *xbm, xbm_t *frame, int frame_index)
+{
+ int from = frame_index * xbm->fh;
+ int to = from + xbm->fh;
+ return xbm_croph(xbm, frame, from, to);
+} \ No newline at end of file