#include <stdio.h>
#include <stdlib.h>

#include <jpeglib.h>
#include <jerror.h>


/* Following code from /usr/share/doc/libjpeg-turbo8-dev/libjpeg.txt.gz and example.c from libjpeg-turbo distribution. */


int main (int argc, char** argv)
{
  int return_code = 0;

  struct jpeg_decompress_struct cinfo;
  struct jpeg_error_mgr jerr;

  JSAMPARRAY buffer; 
  int row_stride; 

  cinfo.err = jpeg_std_error (&jerr);
  jpeg_create_decompress (&cinfo);

  FILE* input = stdin;

  jpeg_stdio_src (&cinfo, input);

  jpeg_read_header (&cinfo, TRUE);

  jpeg_start_decompress (&cinfo);

  row_stride = cinfo.output_width * cinfo.output_components;
 	buffer = (*cinfo.mem->alloc_sarray) ((j_common_ptr) &cinfo, JPOOL_IMAGE, row_stride, 1);

  while (cinfo.output_scanline < cinfo.output_height) {
    (void) jpeg_read_scanlines (&cinfo, buffer, 1);
  }

  jpeg_finish_decompress (&cinfo);

  jpeg_destroy_decompress (&cinfo);

  if (fgetc (input) != EOF) {
    return_code = -1;
  }

  exit (return_code);
}

