+void bitmap_test(int mw_fd)
+{
+ /* a nice checker-board pattern */
+ unsigned char checkbuf[24] = {
+ 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
+ 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
+ 0x55, 0x55, 0x55, 0x55, 0x55, 0x55,
+ 0x55, 0x55, 0x55, 0x55, 0x55, 0x55,
+ };
+ mw_write_buffer(mw_fd, MW_SCREEN_MODE_IDLE, 0, 31, checkbuf, 24);
+ mw_write_buffer(mw_fd, MW_SCREEN_MODE_IDLE, 0, 33, checkbuf, 24);
+ mw_write_buffer(mw_fd, MW_SCREEN_MODE_IDLE, 0, 35, checkbuf, 24);
+ mw_write_buffer(mw_fd, MW_SCREEN_MODE_IDLE, 0, 37, checkbuf, 24);
+
+ mw_update_display(mw_fd, MW_SCREEN_MODE_IDLE, 1);
+}
+
+
+void flip_buffer_bytes(unsigned char invert, unsigned char *buf, int len)
+{
+ int i;
+ unsigned char tmp;
+
+ while (len--) {
+ tmp = 0;
+ for (i=0; i<8; i++)
+ tmp |= ((*buf & (1<<i)) >> i) << (7-i);
+ // fprintf(stderr, "0x%02x -> 0x%02x\n", *buf, tmp);
+ *buf = invert ? ~tmp : tmp;
+ buf++;
+ }
+}
+
+void bitmap_read(int mw_fd)
+{
+ int ffd, ret;
+ char rbuf[256];
+ unsigned int width, height, i, x, y;
+ unsigned int rowlength;
+ unsigned char *bmapbuf;
+ unsigned char mw_buf[24];
+
+ ffd = open("test.pbm", O_RDONLY);
+ if (ffd < 0) {
+ perror("open");
+ return;
+ };
+ ret = read(ffd, rbuf, 3);
+ if (rbuf[0] != 'P' || rbuf[1] != '4') {
+ fprintf(stderr, "not a PBM file\n");
+ return;
+ }
+ memset(rbuf, 0, 256);
+ i = 0;
+ do {
+ ret = read(ffd, (rbuf+i), 1);
+ } while (!isspace(rbuf[i++]));
+ width = atoi(rbuf);
+
+ memset(rbuf, 0, 256);
+ i = 0;
+ do {
+ ret = read(ffd, (rbuf+i), 1);
+ } while (!isspace(rbuf[i++]));
+ height = atoi(rbuf);
+
+ fprintf(stderr, "bitmap resolution is %d x %d\n", width, height);
+
+ rowlength = ((width / 8) + 1);
+ fprintf(stderr, "row length = %d bytes\n", rowlength);
+
+ bmapbuf = malloc(rowlength * height);
+
+ ret = read(ffd, bmapbuf, rowlength * height);
+ close(ffd);
+ fprintf(stderr, "read %d of %d bytes\n", ret, rowlength * height);
+
+#if 0
+ fprintf(stderr, "\n");
+ for (y=0; y<height; y++) {
+ for (x=0; x<rowlength; x++) {
+ for (i=0; i<8; i++)
+ fprintf(stderr, "%c", (bmapbuf[(y*rowlength)+x] & (1<<(7-i))) ? '.' : ' ');
+ }
+ fprintf(stderr, "\n");
+ }
+ fprintf(stderr, "\n");
+#endif
+
+ flip_buffer_bytes(1, bmapbuf, rowlength * height);
+
+ for (y=0; y<height; y+=2) {
+ memset(mw_buf, 0, 24);
+ memcpy(mw_buf, (bmapbuf+(y*rowlength)), (rowlength > 12) ? 12 : rowlength);
+ memcpy((mw_buf+12), (bmapbuf+((y+1)*rowlength)), (rowlength > 12) ? 12 : rowlength);
+ mw_write_buffer(mw_fd, MW_SCREEN_MODE_IDLE, 0, 31+y, mw_buf, 24);
+ }
+ mw_update_display(mw_fd, MW_SCREEN_MODE_IDLE, 1);
+
+ free(bmapbuf);
+}
+
+