aboutsummaryrefslogtreecommitdiff
path: root/src/xbm.c
diff options
context:
space:
mode:
authorDien-Nhung Nguyen-Phu <kein@kienlab.com>2024-06-09 12:59:34 +0700
committerDien-Nhung Nguyen-Phu <kein@kienlab.com>2024-06-10 12:34:37 +0700
commit8815700322c0c10cb0528b047b2c7604c2eeb053 (patch)
treef1ba04d1c18ac01cb671618309f015599e9da782 /src/xbm.c
parentf6d84a1ead1a57574c45bb80b6fd450c5b784e09 (diff)
Add support for .xbm bitmap file
.xbm is easy to include in C as source file. It could be viewed and edited directly without need to be converted between images and C array.
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