From cb9a1e3ee0d67b104e04e0125c85b061037319cf Mon Sep 17 00:00:00 2001 From: notaz Date: Sun, 7 Sep 2014 21:56:06 +0300 Subject: [PATCH] add vdp1 fb to png tool --- Makefile | 6 ++- vdp1_to_png.c | 106 ++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 110 insertions(+), 2 deletions(-) create mode 100644 vdp1_to_png.c diff --git a/Makefile b/Makefile index 60c8312..d73cb81 100644 --- a/Makefile +++ b/Makefile @@ -3,9 +3,11 @@ ifndef DEBUG CFLAGS += -O2 endif -TARGET = datalink +TARGETS = datalink vdp1_to_png -all: $(TARGET) +all: $(TARGETS) + +vdp1_to_png: LDLIBS += -lpng clean: $(RM) $(TARGET) diff --git a/vdp1_to_png.c b/vdp1_to_png.c new file mode 100644 index 0000000..6b62437 --- /dev/null +++ b/vdp1_to_png.c @@ -0,0 +1,106 @@ +#include +#include +#include +#include + +#define WIDTH 512 +#define HEIGHT 256 + +static unsigned short work_buf[WIDTH * HEIGHT]; + +static int writepng(const char *fname, const unsigned short *src) +{ + png_structp png_ptr = NULL; + png_infop info_ptr = NULL; + png_bytepp row_pointers; + int i, j, ret = -1; + FILE *f; + + f = fopen(fname, "wb"); + if (f == NULL) { + printf(__FILE__ ": failed to open \"%s\"\n", fname); + return -1; + } + + row_pointers = calloc(HEIGHT, sizeof(row_pointers[0])); + if (row_pointers == NULL) + goto end1; + + for (i = 0; i < HEIGHT; i++) { + unsigned char *dst = malloc(WIDTH * 3); + if (dst == NULL) + goto end2; + row_pointers[i] = dst; + for (j = 0; j < WIDTH; j++, src++, dst += 3) { + unsigned short v = (*src >> 8) | (*src << 8); + dst[0] = (v & 0x7c00) >> 7; + dst[1] = (v & 0x03e0) >> 2; + dst[2] = (v & 0x001f) << 3; + } + } + + /* initialize stuff */ + png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL); + if (png_ptr == NULL) { + fprintf(stderr, "png_create_write_struct() failed"); + goto end2; + } + + info_ptr = png_create_info_struct(png_ptr); + if (info_ptr == NULL) { + fprintf(stderr, "png_create_info_struct() failed"); + goto end3; + } + + if (setjmp(png_jmpbuf(png_ptr)) != 0) { + fprintf(stderr, "error in png code\n"); + goto end4; + } + + png_init_io(png_ptr, f); + + png_set_IHDR(png_ptr, info_ptr, WIDTH, HEIGHT, + 8, PNG_COLOR_TYPE_RGB, PNG_INTERLACE_NONE, + PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT); + + png_write_info(png_ptr, info_ptr); + png_write_image(png_ptr, row_pointers); + png_write_end(png_ptr, NULL); + + ret = 0; + +end4: +// png_destroy_info_struct(png_ptr, &info_ptr); // freed below +end3: + png_destroy_write_struct(&png_ptr, &info_ptr); +end2: + for (i = 0; i < HEIGHT; i++) + free(row_pointers[i]); + free(row_pointers); +end1: + fclose(f); + return ret; +} + +int main(int argc, char *argv[]) +{ + FILE *f; + + if (argc != 3) { + printf("usage:\n%s \n", argv[0]); + return 1; + } + + f = fopen(argv[1], "rb"); + if (!f) { + perror("fopen fb_dump"); + return 1; + } + + if (fread(work_buf, 1, sizeof(work_buf), f) != sizeof(work_buf)) + printf("read error?\n"); + + writepng(argv[2], work_buf); + return 0; +} + -- 2.39.5