ff2png_rgb.c - randomcrap - random crap programs of varying quality
 (HTM) git clone git://git.codemadness.org/randomcrap
 (DIR) Log
 (DIR) Files
 (DIR) Refs
 (DIR) README
 (DIR) LICENSE
       ---
       ff2png_rgb.c (1746B)
       ---
            1 /* farbfeld: ff to png 8-bit RGB (no alpha). */
            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_writer(png_struct **s, png_info **i, uint32_t w, uint32_t h)
           23 {
           24         *s = png_create_write_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, stdout);
           32         png_set_IHDR(*s, *i, w, h, 8, PNG_COLOR_TYPE_RGB,
           33                      PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_BASE,
           34                      PNG_FILTER_TYPE_BASE);
           35         png_write_info(*s, *i);
           36 }
           37 
           38 static void
           39 usage(void)
           40 {
           41         die("usage: %s", argv0);
           42 }
           43 
           44 int
           45 main(int argc, char *argv[])
           46 {
           47         png_struct *pngs;
           48         png_info *pngi;
           49         size_t rowlen;
           50         uint32_t width, height, i, j, k;
           51         uint16_t *row;
           52         uint8_t *orow;
           53 
           54         /* arguments */
           55         argv0 = argv[0], argc--, argv++;
           56 
           57         if (argc) {
           58                 usage();
           59         }
           60 
           61         /* prepare */
           62         ff_read_header(&width, &height);
           63         png_setup_writer(&pngs, &pngi, width, height);
           64         row = ereallocarray(NULL, width, (sizeof("RGBA") - 1) * sizeof(uint16_t));
           65         orow = ereallocarray(NULL, width, (sizeof("RGB") - 1));
           66         rowlen = width * (sizeof("RGBA") - 1);
           67 
           68         /* write data */
           69         for (i = 0; i < height; ++i) {
           70                 efread(row, sizeof(uint16_t), rowlen, stdin);
           71                 for (j = 0, k = 0; j < rowlen; j += 4, k += 3) {
           72                         orow[k]   = row[j] / 257;
           73                         orow[k+1] = row[j+1] / 257;
           74                         orow[k+2] = row[j+2] / 257;
           75                         /* ignore alpha */
           76                 }
           77                 png_write_row(pngs, orow);
           78         }
           79 
           80         /* clean up */
           81         png_write_end(pngs, NULL);
           82         png_destroy_write_struct(&pngs, NULL);
           83 
           84         return fshut(stdout, "<stdout>");
           85 }