From da7d77dd7ed01ceb858554269d0748addbacb969 Mon Sep 17 00:00:00 2001 From: Zack Buhman Date: Wed, 2 Jul 2025 20:56:51 -0500 Subject: [PATCH] add twiddle_data_4bpp --- example/software_ta_two_volumes.cpp | 3 +- holly/region_array.cpp | 2 - tools/twiddle_data_4bpp.cpp | 104 ++++++++++++++++++++++++++++ 3 files changed, 106 insertions(+), 3 deletions(-) create mode 100644 tools/twiddle_data_4bpp.cpp diff --git a/example/software_ta_two_volumes.cpp b/example/software_ta_two_volumes.cpp index 3603a4b..b3e2e43 100644 --- a/example/software_ta_two_volumes.cpp +++ b/example/software_ta_two_volumes.cpp @@ -48,7 +48,8 @@ void init_texture_memory() texture_memory_alloc.region_array.start, texture_memory_alloc.object_list.start); - background_parameter(0xff222200); + background_parameter2(texture_memory_alloc.background[0].start, + 0xff000000); } constexpr float half_degree = 0.01745329f / 2.f; diff --git a/holly/region_array.cpp b/holly/region_array.cpp index cb69675..5cf8705 100644 --- a/holly/region_array.cpp +++ b/holly/region_array.cpp @@ -117,8 +117,6 @@ void region_array2(const uint32_t width, // in tile units (1 tile unit = 32 pix } } -#include "printf/printf.h" - void region_array_multipass(const uint32_t width, // in tile units (1 tile unit = 32 pixels) const uint32_t height, // in tile units (1 tile unit = 32 pixels) const struct opb_size * opb_size, diff --git a/tools/twiddle_data_4bpp.cpp b/tools/twiddle_data_4bpp.cpp new file mode 100644 index 0000000..53dde3b --- /dev/null +++ b/tools/twiddle_data_4bpp.cpp @@ -0,0 +1,104 @@ +#include +#include +#include +#include +#include +#include +#include + +#include "../twiddle.hpp" + +int read_file(const char * filename, uint8_t ** buf, uint32_t * size_out) +{ + FILE * file = fopen(filename, "rb"); + if (file == NULL) { + fprintf(stderr, "fopen(\"%s\", \"rb\"): %s\n", filename, strerror(errno)); + return -1; + } + + int ret; + ret = fseek(file, 0L, SEEK_END); + if (ret < 0) { + fprintf(stderr, "fseek(SEEK_END)"); + return -1; + } + + long offset = ftell(file); + if (offset < 0) { + fprintf(stderr, "ftell"); + return -1; + } + size_t size = offset; + + ret = fseek(file, 0L, SEEK_SET); + if (ret < 0) { + fprintf(stderr, "fseek(SEEK_SET)"); + return -1; + } + + fprintf(stderr, "read_file: %s size %ld\n", filename, size); + *buf = (uint8_t *)malloc(size); + size_t fread_size = fread(*buf, 1, size, file); + if (fread_size != size) { + fprintf(stderr, "fread `%s` short read: %" PRIu64 " ; expected: %" PRIu64 "\n", filename, fread_size, size); + return -1; + } + + ret = fclose(file); + if (ret < 0) { + fprintf(stderr, "fclose"); + return -1; + } + + *size_out = size; + + return 0; +} + +int write_file(const char * filename, const uint8_t * buf, uint32_t size) +{ + FILE * file = fopen(filename, "wb"); + if (file == NULL) { + fprintf(stderr, "fopen(\"%s\", \"wb\"): %s\n", filename, strerror(errno)); + return -1; + } + + size_t write_size = fwrite(buf, 1, size, file); + if (write_size != size) { + fprintf(stderr, "fwrite: %d %ld %s\n", size, write_size, strerror(errno)); + return -1; + } + + int ret = fclose(file); + if (ret < 0) { + fprintf(stderr, "fclose"); + return -1; + } + + return 0; +} + +int main(int argc, const char * argv[]) +{ + if (argc != 3) { + fprintf(stderr, "argc != 3\n"); + return -1; + } + + uint8_t * buf; + uint32_t size; + int ret = read_file(argv[1], &buf, &size); + assert(ret == 0); + + // fixme: add pgm support + + for (int i = 0; i < size; i++) { + buf[i] = (buf[i] == 255) ? 1 : 0; + } + + uint8_t * twiddle = (uint8_t *)malloc(size / 2); + twiddle::texture_4bpp(twiddle, buf, 64, 32); + + ret = write_file(argv[2], twiddle, size / 2); + assert(ret == 0); +}