jpg2ff.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
       ---
       jpg2ff.c (1925B)
       ---
            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 <jpeglib.h>
           11 
           12 #include "util.h"
           13 
           14 static void
           15 jpeg_error(j_common_ptr js)
           16 {
           17         fprintf(stderr, "%s: libjpeg: ", argv0);
           18         (*js->err->output_message)(js);
           19         exit(1);
           20 }
           21 
           22 static void
           23 jpeg_setup_reader(struct jpeg_decompress_struct *s, struct jpeg_error_mgr *e,
           24                   uint32_t *w, uint32_t *h)
           25 {
           26         jpeg_create_decompress(s);
           27         e->error_exit = jpeg_error;
           28         s->err = jpeg_std_error(e);
           29 
           30         jpeg_stdio_src(s, stdin);
           31         jpeg_read_header(s, 1);
           32         *w = s->image_width;
           33         *h = s->image_height;
           34         s->output_components = 3;     /* color components per pixel */
           35         s->out_color_space = JCS_RGB; /* input color space */
           36 
           37         jpeg_start_decompress(s);
           38 }
           39 
           40 static void
           41 usage(void)
           42 {
           43         die("usage: %s", argv0);
           44 }
           45 
           46 int
           47 main(int argc, char *argv[])
           48 {
           49         struct jpeg_decompress_struct js;
           50         struct jpeg_error_mgr jerr;
           51         uint32_t width, height;
           52         uint16_t *row;
           53         uint8_t *rowin;
           54         size_t rowlen, i;
           55 
           56         /* arguments */
           57         argv0 = argv[0], argc--, argv++;
           58 
           59         if (argc) {
           60                 usage();
           61         }
           62 
           63         /* prepare */
           64         jpeg_setup_reader(&js, &jerr, &width, &height);
           65         row = ereallocarray(NULL, width, (sizeof("RGBA") - 1) * sizeof(uint16_t));
           66         rowlen = width * (sizeof("RGBA") - 1);
           67         rowin = ereallocarray(NULL, width, (sizeof("RGB") - 1) * sizeof(uint8_t));
           68 
           69         /* write data */
           70         ff_write_header(width, height);
           71 
           72         while (js.output_scanline < js.output_height) {
           73                 jpeg_read_scanlines(&js, &rowin, 1);
           74 
           75                 for (i = 0; i < width; ++i) {
           76                         row[4 * i + 0] = htons(rowin[3 * i + 0] * 257);
           77                         row[4 * i + 1] = htons(rowin[3 * i + 1] * 257);
           78                         row[4 * i + 2] = htons(rowin[3 * i + 2] * 257);
           79                         row[4 * i + 3] = htons(65535);
           80                 }
           81 
           82                 efwrite(row, sizeof(uint16_t), rowlen, stdout);
           83         }
           84 
           85         /* clean up */
           86         jpeg_finish_decompress(&js);
           87         jpeg_destroy_decompress(&js);
           88 
           89         return fshut(stdout, "<stdout>");
           90 }