png2ff.c - farbfeld - suckless image format with conversion tools
 (HTM) git clone git://git.suckless.org/farbfeld
 (DIR) Log
 (DIR) Files
 (DIR) Refs
 (DIR) README
 (DIR) LICENSE
       ---
       png2ff.c (1996B)
       ---
            1 /* See LICENSE file for copyright and license details. */
            2 #include <arpa/inet.h>
            3 
            4 #include <errno.h>
            5 #include <stdint.h>
            6 #include <stdio.h>
            7 #include <stdlib.h>
            8 #include <string.h>
            9 
           10 #include <png.h>
           11 
           12 #include "util.h"
           13 
           14 static void
           15 png_err(png_struct *pngs, const char *msg)
           16 {
           17         (void)pngs;
           18         die("libpng: %s", msg);
           19 }
           20 
           21 static void
           22 png_setup_reader(png_struct **s, png_info **i, uint32_t *w, uint32_t *h)
           23 {
           24         *s = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, png_err, NULL);
           25         *i = png_create_info_struct(*s);
           26 
           27         if (!*s || !*i) {
           28                 die("Failed to initialize libpng");
           29         }
           30 
           31         png_init_io(*s, stdin);
           32         if (png_get_valid(*s, *i, PNG_INFO_tRNS)) {
           33                 png_set_tRNS_to_alpha(*s);
           34         }
           35         png_set_add_alpha(*s, 255*257, PNG_FILLER_AFTER);
           36         png_set_expand_gray_1_2_4_to_8(*s);
           37         png_set_gray_to_rgb(*s);
           38         png_set_packing(*s);
           39         png_read_png(*s, *i, PNG_TRANSFORM_PACKING | PNG_TRANSFORM_EXPAND, NULL);
           40         *w = png_get_image_width(*s, *i);
           41         *h = png_get_image_height(*s, *i);
           42 }
           43 
           44 static void
           45 usage(void)
           46 {
           47         die("usage: %s", argv0);
           48 }
           49 
           50 int
           51 main(int argc, char *argv[])
           52 {
           53         png_struct *pngs;
           54         png_info *pngi;
           55         uint32_t width, height, rowlen, r, i;
           56         uint16_t *row;
           57         uint8_t **pngrows;
           58 
           59         /* arguments */
           60         argv0 = argv[0], argc--, argv++;
           61 
           62         if (argc) {
           63                 usage();
           64         }
           65 
           66         /* prepare */
           67         png_setup_reader(&pngs, &pngi, &width, &height);
           68         row = ereallocarray(NULL, width, (sizeof("RGBA") - 1) * sizeof(uint16_t));
           69         rowlen = width * (sizeof("RGBA") - 1);
           70         pngrows = png_get_rows(pngs, pngi);
           71 
           72         /* write data */
           73         ff_write_header(width, height);
           74 
           75         switch(png_get_bit_depth(pngs, pngi)) {
           76         case 8:
           77                 for (r = 0; r < height; ++r) {
           78                         for (i = 0; i < rowlen; i++) {
           79                                 row[i] = htons(257 * pngrows[r][i]);
           80                         }
           81                         efwrite(row, sizeof(uint16_t), rowlen, stdout);
           82                 }
           83                 break;
           84         case 16:
           85                 for (r = 0; r < height; ++r) {
           86                         efwrite(pngrows[r], sizeof(uint16_t), rowlen, stdout);
           87                 }
           88                 break;
           89         default:
           90                 die("Invalid bit-depth");
           91         }
           92 
           93         /* clean up */
           94         png_destroy_read_struct(&pngs, &pngi, NULL);
           95 
           96         return fshut(stdout, "<stdout>");
           97 }