add bmp_to_1bit.c

This commit is contained in:
olebeck 2022-05-29 13:42:41 +02:00
parent e1f4bca05b
commit 7c8fa4048c
9 changed files with 96 additions and 13 deletions

View File

@ -49,12 +49,10 @@ void display() {
#include "pocketlib.h"
static void display() {
// ????
VSync(VSYNC_COUNT);
uint32_t *src = (uint32_t*)screen;
volatile unsigned int *dst = LCD_VRAM_ADRS;
int i = 32;
while (i--) {*dst++ = *src++;}
for (int i=32; i--;) {*dst++ = *src++;}
}
uint8_t ROMByteAccess(unsigned char *addr)

View File

@ -1,9 +1,11 @@
all: ascii2sjis bin2mcs bmp2ps1b mcpad
all: ascii2sjis bin2mcs bmp2ps1b mcpad bmp_to_1bit
ascii2sjis: ascii2sjis.c
bin2mcs: bin2mcs.c
bmp2ps1b: bmp2ps1b.c
mcpad: mcpad.c
bmp_to_1bit: bmp_to_1bit.c
clean:
rm -f ascii2sjis bin2mcs bmp2ps1b mcpad
rm -f ascii2sjis bin2mcs bmp2ps1b mcpad bmp_to_1bit
rm -f ascii2sjis.exe bin2mcs.exe bmp2ps1b.exe mcpad.exe bmp_to_1bit.exe

Binary file not shown.

Binary file not shown.

Binary file not shown.

91
tools/bmp_to_1bit.c Normal file
View File

@ -0,0 +1,91 @@
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
typedef struct __attribute__((__packed__)) BMPHeader {
uint16_t type; // Magic identifier: 0x4d42
uint32_t size; // File size in bytes
uint16_t reserved1; // Not used
uint16_t reserved2; // Not used
uint32_t offset; // Offset to image data in bytes from beginning of file
uint32_t dib_header_size; // DIB Header size in bytes
int32_t width_px; // Width of the image
int32_t height_px; // Height of image
uint16_t num_planes; // Number of color planes
uint16_t bits_per_pixel; // Bits per pixel
uint32_t compression; // Compression type
uint32_t image_size_bytes; // Image size in bytes
int32_t x_resolution_ppm; // Pixels per meter
int32_t y_resolution_ppm; // Pixels per meter
uint32_t num_colors; // Number of colors
uint32_t important_colors; // Important colors
} BMPHeader;
int main (int argc, char* argv[])
{
FILE* in, *out;
if(argc < 3) {
printf("Usage: %s <input.bmp> <output.bmp>\n", argv[0]);
return 1;
}
in = fopen(argv[1], "rb");
if(!in) {
printf("Could not open input file.\n");
return 1;
}
out = fopen(argv[2], "wb");
if(!out) {
printf("Could not open output file.\n");
return 1;
}
BMPHeader in_header = {0};
fread(&in_header, sizeof(BMPHeader), 1, in);
fseek(in, in_header.offset, SEEK_SET);
uint8_t* px = malloc(in_header.image_size_bytes);
fread(px, in_header.image_size_bytes, 1, in);
fclose(in);
printf("%d x %d\n", in_header.width_px, in_header.height_px);
printf("bpp: %d\n", in_header.bits_per_pixel);
uint8_t* out_buffer = malloc(32 * 32 / 8);
for(int out_y = 0; out_y < 32; out_y++) {
for(int out_x = 0; out_x < 32; out_x++) {
// input is bigger than output
// so we need to skip some pixels
int in_x = out_x * in_header.width_px / 32;
int in_y = out_y * in_header.height_px / 32;
int in_offset = (in_y * in_header.width_px + in_x) * 3;
uint8_t v = (px[in_offset + 2] + px[in_offset + 1] + px[in_offset + 0]) / 3;
int out_offset = out_y * 32 / 8 + out_x / 8;
if(v > 127) {
out_buffer[out_offset] |= 1 << (7 - out_x % 8);
} else {
out_buffer[out_offset] &= ~(1 << (7 - out_x % 8));
}
}
}
free(px);
BMPHeader out_header = {0};
out_header.type = 0x4d42;
out_header.size = sizeof(BMPHeader) + 32 * 32 / 8;
out_header.offset = sizeof(BMPHeader);
out_header.dib_header_size = sizeof(BMPHeader) - 14;
out_header.width_px = 32;
out_header.height_px = 32;
out_header.bits_per_pixel = 1;
out_header.image_size_bytes = 32 * 32 / 8;
out_header.num_colors = 2;
out_header.important_colors = 2;
fwrite(&out_header, sizeof(BMPHeader), 1, out);
fwrite(out_buffer, out_header.image_size_bytes, 1, out);
fclose(out);
free(out_buffer);
}

Binary file not shown.

View File

@ -1,8 +0,0 @@
#!/bin/python3
import sys
from PIL import Image
img = Image.open(sys.argv[1])
img = img.convert("1", dither=Image.Dither.NONE)
img = img.resize((32,32), Image.Resampling.NEAREST)
img.save(sys.argv[2])

Binary file not shown.