Home | History | Annotate | Download | only in decode
      1 /*
      2  * Small jpeg decoder library - testing application
      3  *
      4  * Copyright (c) 2006, Luc Saillard <luc (at) saillard.org>
      5  * Copyright (c) 2012 Intel Corporation.
      6  * All rights reserved.
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions are met:
      9  *
     10  * - Redistributions of source code must retain the above copyright notice,
     11  *  this list of conditions and the following disclaimer.
     12  *
     13  * - Redistributions in binary form must reproduce the above copyright notice,
     14  *  this list of conditions and the following disclaimer in the documentation
     15  *  and/or other materials provided with the distribution.
     16  *
     17  * - Neither the name of the author nor the names of its contributors may be
     18  *  used to endorse or promote products derived from this software without
     19  *  specific prior written permission.
     20  *
     21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
     22  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     24  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
     25  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     26  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     27  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     28  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     29  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     30  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     31  * POSSIBILITY OF SUCH DAMAGE.
     32  *
     33  */
     34 
     35 #include "tinyjpeg.h"
     36 #include <stdio.h>
     37 #include <stdlib.h>
     38 #include <string.h>
     39 #include <time.h>
     40 #include "va_display.h"
     41 
     42 static void exitmessage(const char *message) __attribute__((noreturn));
     43 static void exitmessage(const char *message)
     44 {
     45   printf("%s\n", message);
     46   exit(0);
     47 }
     48 
     49 static int filesize(FILE *fp)
     50 {
     51   long pos;
     52   fseek(fp, 0, SEEK_END);
     53   pos = ftell(fp);
     54   fseek(fp, 0, SEEK_SET);
     55   return pos;
     56 }
     57 
     58 /**
     59  * Load one jpeg image, and decompress it, and save the result.
     60  */
     61 int convert_one_image(const char *infilename)
     62 {
     63   FILE *fp;
     64   unsigned int length_of_file;
     65   unsigned int width, height;
     66   unsigned char *buf;
     67   struct jdec_private *jdec;
     68 
     69   /* Load the Jpeg into memory */
     70   fp = fopen(infilename, "rb");
     71   if (fp == NULL)
     72     exitmessage("Cannot open filename\n");
     73   length_of_file = filesize(fp);
     74   buf = (unsigned char *)malloc(length_of_file + 4);
     75   if (buf == NULL)
     76     exitmessage("Not enough memory for loading file\n");
     77   fread(buf, length_of_file, 1, fp);
     78   fclose(fp);
     79 
     80   /* Decompress it */
     81   jdec = tinyjpeg_init();
     82   if (jdec == NULL)
     83     exitmessage("Not enough memory to alloc the structure need for decompressing\n");
     84 
     85   if (tinyjpeg_parse_header(jdec, buf, length_of_file)<0)
     86     exitmessage(tinyjpeg_get_errorstring(jdec));
     87 
     88   /* Get the size of the image */
     89   tinyjpeg_get_size(jdec, &width, &height);
     90 
     91   printf("Decoding JPEG image %dx%d...\n", width, height);
     92   if (tinyjpeg_decode(jdec) < 0)
     93     exitmessage(tinyjpeg_get_errorstring(jdec));
     94 
     95   tinyjpeg_free(jdec);
     96 
     97   free(buf);
     98   return 0;
     99 }
    100 
    101 static void usage(void)
    102 {
    103     fprintf(stderr, "Usage: loadjpeg <input_filename.jpeg> \n");
    104     exit(1);
    105 }
    106 
    107 /**
    108  * main
    109  *
    110  */
    111 int main(int argc, char *argv[])
    112 {
    113   char *input_filename;
    114   clock_t start_time, finish_time;
    115   unsigned int duration;
    116   int current_argument;
    117 
    118   va_init_display_args(&argc, argv);
    119 
    120   if (argc < 2)
    121     usage();
    122 
    123   current_argument = 1;
    124   input_filename = argv[current_argument];
    125 
    126   start_time = clock();
    127   convert_one_image(input_filename);
    128   finish_time = clock();
    129   duration = finish_time - start_time;
    130   printf("Decoding finished in %u ticks\n", duration);
    131 
    132   return 0;
    133 }
    134 
    135 
    136 
    137 
    138