add vdp1 fb to png tool
authornotaz <notasas@gmail.com>
Sun, 7 Sep 2014 18:56:06 +0000 (21:56 +0300)
committernotaz <notasas@gmail.com>
Sun, 7 Sep 2014 22:22:21 +0000 (01:22 +0300)
Makefile
vdp1_to_png.c [new file with mode: 0644]

index 60c8312..d73cb81 100644 (file)
--- 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 (file)
index 0000000..6b62437
--- /dev/null
@@ -0,0 +1,106 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <png.h>
+
+#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 <fb_dump> <png>\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;
+}
+