OLD | NEW |
(Empty) | |
| 1 /* |
| 2 * Small jpeg decoder library - testing application |
| 3 * |
| 4 * Copyright (c) 2006, Luc Saillard <luc@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 |
| 41 static void exitmessage(const char *message) __attribute__((noreturn)); |
| 42 static void exitmessage(const char *message) |
| 43 { |
| 44 printf("%s\n", message); |
| 45 exit(0); |
| 46 } |
| 47 |
| 48 static int filesize(FILE *fp) |
| 49 { |
| 50 long pos; |
| 51 fseek(fp, 0, SEEK_END); |
| 52 pos = ftell(fp); |
| 53 fseek(fp, 0, SEEK_SET); |
| 54 return pos; |
| 55 } |
| 56 |
| 57 /** |
| 58 * Load one jpeg image, and decompress it, and save the result. |
| 59 */ |
| 60 int convert_one_image(const char *infilename) |
| 61 { |
| 62 FILE *fp; |
| 63 unsigned int length_of_file; |
| 64 unsigned int width, height; |
| 65 unsigned char *buf; |
| 66 struct jdec_private *jdec; |
| 67 |
| 68 /* Load the Jpeg into memory */ |
| 69 fp = fopen(infilename, "rb"); |
| 70 if (fp == NULL) |
| 71 exitmessage("Cannot open filename\n"); |
| 72 length_of_file = filesize(fp); |
| 73 buf = (unsigned char *)malloc(length_of_file + 4); |
| 74 if (buf == NULL) |
| 75 exitmessage("Not enough memory for loading file\n"); |
| 76 fread(buf, length_of_file, 1, fp); |
| 77 fclose(fp); |
| 78 |
| 79 /* Decompress it */ |
| 80 jdec = tinyjpeg_init(); |
| 81 if (jdec == NULL) |
| 82 exitmessage("Not enough memory to alloc the structure need for decompressing
\n"); |
| 83 |
| 84 if (tinyjpeg_parse_header(jdec, buf, length_of_file)<0) |
| 85 exitmessage(tinyjpeg_get_errorstring(jdec)); |
| 86 |
| 87 /* Get the size of the image */ |
| 88 tinyjpeg_get_size(jdec, &width, &height); |
| 89 |
| 90 printf("Decoding JPEG image %xx%x...\n", width, height); |
| 91 if (tinyjpeg_decode(jdec) < 0) |
| 92 exitmessage(tinyjpeg_get_errorstring(jdec)); |
| 93 |
| 94 tinyjpeg_free(jdec); |
| 95 |
| 96 free(buf); |
| 97 return 0; |
| 98 } |
| 99 |
| 100 static void usage(void) |
| 101 { |
| 102 fprintf(stderr, "Usage: loadjpeg <input_filename.jpeg> \n"); |
| 103 exit(1); |
| 104 } |
| 105 |
| 106 /** |
| 107 * main |
| 108 * |
| 109 */ |
| 110 int main(int argc, char *argv[]) |
| 111 { |
| 112 char *input_filename; |
| 113 clock_t start_time, finish_time; |
| 114 unsigned int duration; |
| 115 int current_argument; |
| 116 |
| 117 if (argc < 2) |
| 118 usage(); |
| 119 |
| 120 current_argument = 1; |
| 121 input_filename = argv[current_argument]; |
| 122 |
| 123 start_time = clock(); |
| 124 convert_one_image(input_filename); |
| 125 finish_time = clock(); |
| 126 duration = finish_time - start_time; |
| 127 printf("Decoding finished in %u ticks\n", duration); |
| 128 |
| 129 return 0; |
| 130 } |
| 131 |
| 132 |
| 133 |
| 134 |
OLD | NEW |