summaryrefslogtreecommitdiff
path: root/png2ff.c
diff options
context:
space:
mode:
authorFRIGN <dev@frign.de>2015-11-09 23:39:29 +0100
committerFRIGN <dev@frign.de>2015-11-09 23:40:46 +0100
commitd5f6f70d351239ee37b3d864c9a94dc49d66c052 (patch)
tree47993f137fd273df86a4f24fe7fe22098c435e0a /png2ff.c
parent0b0fcaa7136d7de2e1bc5dc05a576f5fe5608995 (diff)
imagefile -> farbfeld
- Rename the format - Change the format specification - Drop old tools waiting to be fixed on a later date, just keep fixed png for now - Simplify other stuff This is a direct consequence of my slcon2-talk on this topic. At first I planned to have 64 bits per channel, but this is overkill.
Diffstat (limited to 'png2ff.c')
-rw-r--r--png2ff.c75
1 files changed, 75 insertions, 0 deletions
diff --git a/png2ff.c b/png2ff.c
new file mode 100644
index 0000000..52f1781
--- /dev/null
+++ b/png2ff.c
@@ -0,0 +1,75 @@
+/* See LICENSE file for copyright and license details. */
+#include <endian.h>
+#include <stdint.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <png.h>
+
+#include "arg.h"
+
+char *argv0;
+
+static void
+usage(void)
+{
+ fprintf(stderr, "usage:%s\n", argv0);
+ exit(1);
+}
+
+int
+main(int argc, char *argv[])
+{
+ png_structp png_struct_p;
+ png_infop png_info_p;
+ png_bytepp png_row_p;
+ int depth, color, interlace;
+ uint32_t width, height, png_row_len, tmp32, r, i;
+ uint16_t tmp16;
+
+ ARGBEGIN {
+ default:
+ usage();
+ } ARGEND
+
+ if (argc)
+ usage();
+
+ /* load png */
+ png_struct_p = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL,
+ NULL, NULL);
+ png_info_p = png_create_info_struct(png_struct_p);
+
+ if (!png_struct_p || !png_info_p || setjmp(png_jmpbuf(png_struct_p))) {
+ fprintf(stderr, "failed to initialize libpng\n");
+ return 1;
+ }
+ png_init_io(png_struct_p, stdin);
+ png_set_add_alpha(png_struct_p, 255, PNG_FILLER_AFTER);
+ png_set_gray_to_rgb(png_struct_p);
+ png_read_png(png_struct_p, png_info_p, PNG_TRANSFORM_STRIP_16 |
+ PNG_TRANSFORM_PACKING | PNG_TRANSFORM_EXPAND, NULL);
+ png_get_IHDR(png_struct_p, png_info_p, &width, &height, &depth,
+ &color, &interlace, NULL, NULL);
+ png_row_len = png_get_rowbytes(png_struct_p, png_info_p);
+ png_row_p = png_get_rows(png_struct_p, png_info_p);
+
+ /* write header */
+ fprintf(stdout, "farbfeld");
+ tmp32 = htobe32(width);
+ fwrite(&tmp32, sizeof(uint32_t), 1, stdout);
+ tmp32 = htobe32(height);
+ fwrite(&tmp32, sizeof(uint32_t), 1, stdout);
+
+ /* write data */
+ /* TODO: allow 16 bit PNGs to be converted losslessly */
+ for (r = 0; r < height; ++r) {
+ for (i = 0; i < png_row_len; i++) {
+ tmp16 = htobe16((uint16_t)png_row_p[r][i]);
+ fwrite(&tmp16, sizeof(uint16_t), 1, stdout);
+ }
+ }
+
+ /* cleanup */
+ png_destroy_read_struct(&png_struct_p, &png_info_p, NULL);
+ return 0;
+}