Mercurial > cgi-bin > hgwebdir.cgi > PR > Applications > VSs > VSs__jpeg_decoder__Proj
comparison VSs_tinyjpeg/loadjpeg.c @ 0:a8af8b3fc99d
initial commit
| author | Nina Engelhardt <nengel@mailbox.tu-berlin.de> |
|---|---|
| date | Thu, 05 Jul 2012 11:35:03 +0200 |
| parents | |
| children | 7e13c9ecc89c |
comparison
equal
deleted
inserted
replaced
| -1:000000000000 | 0:f853b84b39ae |
|---|---|
| 1 /* | |
| 2 * Small jpeg decoder library - testing application | |
| 3 * | |
| 4 * Copyright (c) 2006, Luc Saillard <luc@saillard.org> | |
| 5 * All rights reserved. | |
| 6 * Redistribution and use in source and binary forms, with or without | |
| 7 * modification, are permitted provided that the following conditions are met: | |
| 8 * | |
| 9 * - Redistributions of source code must retain the above copyright notice, | |
| 10 * this list of conditions and the following disclaimer. | |
| 11 * | |
| 12 * - Redistributions in binary form must reproduce the above copyright notice, | |
| 13 * this list of conditions and the following disclaimer in the documentation | |
| 14 * and/or other materials provided with the distribution. | |
| 15 * | |
| 16 * - Neither the name of the author nor the names of its contributors may be | |
| 17 * used to endorse or promote products derived from this software without | |
| 18 * specific prior written permission. | |
| 19 * | |
| 20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" | |
| 21 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
| 22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
| 23 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE | |
| 24 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | |
| 25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | |
| 26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | |
| 27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | |
| 28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | |
| 29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | |
| 30 * POSSIBILITY OF SUCH DAMAGE. | |
| 31 * | |
| 32 */ | |
| 33 | |
| 34 | |
| 35 #include <stdio.h> | |
| 36 #include <stdint.h> | |
| 37 #include <stdlib.h> | |
| 38 #include <string.h> | |
| 39 #include <sys/time.h> | |
| 40 | |
| 41 #include "tinyjpeg.h" | |
| 42 #include "VSs_impl/VSs.h" | |
| 43 | |
| 44 typedef struct timeval timer; | |
| 45 #define TIME(x) gettimeofday(&x, NULL); | |
| 46 | |
| 47 long timevaldiff(timer *start, timer *finish); | |
| 48 | |
| 49 static void exitmessage(const char *message) | |
| 50 { | |
| 51 printf("%s\n", message); | |
| 52 exit(0); | |
| 53 } | |
| 54 | |
| 55 static int filesize(FILE *fp) | |
| 56 { | |
| 57 long pos; | |
| 58 fseek(fp, 0, SEEK_END); | |
| 59 pos = ftell(fp); | |
| 60 fseek(fp, 0, SEEK_SET); | |
| 61 return pos; | |
| 62 } | |
| 63 | |
| 64 /** | |
| 65 * Save a buffer in 24bits Targa format | |
| 66 * (BGR byte order) | |
| 67 */ | |
| 68 FILE* write_tga_header(const char* filename, int width, int height) { | |
| 69 unsigned char targaheader[18]; | |
| 70 FILE *F; | |
| 71 char temp[1024]; | |
| 72 | |
| 73 snprintf(temp, sizeof(temp), "%s", filename); | |
| 74 | |
| 75 memset(targaheader,0,sizeof(targaheader)); | |
| 76 | |
| 77 targaheader[12] = (unsigned char) (width & 0xFF); | |
| 78 targaheader[13] = (unsigned char) (width >> 8); | |
| 79 targaheader[14] = (unsigned char) (height & 0xFF); | |
| 80 targaheader[15] = (unsigned char) (height >> 8); | |
| 81 targaheader[17] = 0x20; /* Top-down, non-interlaced */ | |
| 82 targaheader[2] = 2; /* image type = uncompressed RGB */ | |
| 83 targaheader[16] = 24; | |
| 84 | |
| 85 | |
| 86 F = fopen(temp, "wb"); | |
| 87 fwrite(targaheader, sizeof(targaheader), 1, F); | |
| 88 return F; | |
| 89 } | |
| 90 | |
| 91 //todo | |
| 92 //#pragma omp task input(*rgb_data) output(*d) inout(*d) | |
| 93 void write_tga_task(FILE* fp, int bufferlen, unsigned char* rgb_data, char* d) { | |
| 94 | |
| 95 // To disable ompss warnings | |
| 96 d = d; | |
| 97 unsigned char *data = rgb_data + bufferlen - RGB_DEPTH; | |
| 98 do | |
| 99 { | |
| 100 unsigned char c = data[0]; | |
| 101 data[0] = data[2]; | |
| 102 data[2] = c; | |
| 103 data-=RGB_DEPTH; | |
| 104 } while (data >= rgb_data); | |
| 105 | |
| 106 fwrite(rgb_data, 1, bufferlen, fp); | |
| 107 } | |
| 108 | |
| 109 | |
| 110 int32 tinyjpegArgTypes[2] = {IN, OUT}; | |
| 111 int32 tinyjpegArgSizes[2] = {sizeof(struct jdec_private), sizeof(uint8_t)}; | |
| 112 | |
| 113 /** | |
| 114 * Load one jpeg image, and decompress it, and save the result. | |
| 115 */ | |
| 116 int convert_one_image(const char *infilename, const char *outfilename) | |
| 117 { | |
| 118 FILE *fp; | |
| 119 unsigned int length_of_file; | |
| 120 unsigned int width, height; | |
| 121 unsigned char *buf; | |
| 122 struct jdec_private *jdec; //for parsing header | |
| 123 struct jdec_private **jdec_task; //for decoding mcus | |
| 124 uint8_t *rgb_data; | |
| 125 int i; | |
| 126 int ntasks; | |
| 127 | |
| 128 /* Load the Jpeg into memory */ | |
| 129 fp = fopen(infilename, "rb"); | |
| 130 if (fp == NULL) | |
| 131 perror("Cannot open image");//exitmessage("Cannot open filename\n"); | |
| 132 length_of_file = filesize(fp); | |
| 133 buf = (unsigned char *)malloc(length_of_file + 4); | |
| 134 if (buf == NULL) | |
| 135 exitmessage("Not enough memory for loading file\n"); | |
| 136 fread(buf, length_of_file, 1, fp); | |
| 137 fclose(fp); | |
| 138 | |
| 139 /* Decompress it */ | |
| 140 jdec = tinyjpeg_init(); | |
| 141 if (jdec == NULL) | |
| 142 exitmessage("Not enough memory to alloc the structure need for decompressing\n"); | |
| 143 | |
| 144 if (tinyjpeg_parse_header(jdec, buf, length_of_file)<0) | |
| 145 exitmessage(tinyjpeg_get_errorstring()); | |
| 146 | |
| 147 /* Get the size of the image */ | |
| 148 tinyjpeg_get_size(jdec, &width, &height); | |
| 149 | |
| 150 // RGB stuff | |
| 151 rgb_data = (uint8_t *)malloc(width * height * RGB_DEPTH); | |
| 152 jdec->components[0] = rgb_data; | |
| 153 | |
| 154 // this jpeg decoder only supports full MCUs for simplicity | |
| 155 ntasks = (jdec->mcus_in_width * jdec->mcus_in_height)/ jdec->restart_interval; | |
| 156 jdec_task = (struct jdec_private **) malloc ( ntasks * sizeof(struct jdec_private*)); | |
| 157 | |
| 158 | |
| 159 //VSs setup | |
| 160 tinyjpegTaskType = VMS_App__malloc( sizeof(VSsTaskType) ); | |
| 161 tinyjpegTaskType->fn = &tinyjpeg_decode_task; | |
| 162 tinyjpegTaskType->numCtldArgs = 2; | |
| 163 tinyjpegTaskType->numTotalArgs = 2; | |
| 164 tinyjpegTaskType->sizeOfArgs = sizeof(tinyjpeg_decode_task_args); | |
| 165 tinyjpegTaskType->argTypes = tinyjpegArgTypes; | |
| 166 tinyjpegTaskType->argSizes = tinyjpegArgSizes; | |
| 167 | |
| 168 tinyjpeg_decode_task_args args; | |
| 169 | |
| 170 fp = write_tga_header(outfilename, width, height); | |
| 171 printf("Decoding JPEG image...\n"); | |
| 172 for (i=0; i<ntasks; i++){ | |
| 173 jdec_task[i] = create_jdec_priv_task(jdec, i); | |
| 174 | |
| 175 args.priv = jdec_task[i]; | |
| 176 args.context = rgb_data+i*width*RGB_DEPTH*MCU_Y_STRIDE; | |
| 177 VSs__submit_task(tinyjpegTaskType, &args, master); | |
| 178 | |
| 179 } | |
| 180 | |
| 181 char dummy; | |
| 182 for(i=0; i<ntasks;i++) { | |
| 183 write_tga_task(fp, width*RGB_DEPTH*MCU_Y_STRIDE, rgb_data+i*RGB_DEPTH*width*MCU_Y_STRIDE, &dummy); | |
| 184 } | |
| 185 | |
| 186 //VSs__wait_for_all_tasks_to_complete(); | |
| 187 //#pragma omp barrier | |
| 188 | |
| 189 tinyjpeg_free(jdec); | |
| 190 for(i=0; i < ntasks; i++) { | |
| 191 tinyjpeg_free(jdec_task[i]); | |
| 192 } | |
| 193 fclose(fp); | |
| 194 free(buf); | |
| 195 free(rgb_data); | |
| 196 free(jdec_task); | |
| 197 return 0; | |
| 198 } | |
| 199 | |
| 200 /* | |
| 201 * Usage information. | |
| 202 */ | |
| 203 static void usage(void) | |
| 204 { | |
| 205 fprintf(stderr, "Usage: loadjpeg <input_filename.jpeg> <output_filename>\n"); | |
| 206 exit(1); | |
| 207 } | |
| 208 | |
| 209 /* | |
| 210 * Calculates the time difference between start and finish in msecs. | |
| 211 */ | |
| 212 long timevaldiff(timer *start, timer *finish){ | |
| 213 long msec; | |
| 214 msec = (finish->tv_sec - start->tv_sec)*1000; | |
| 215 msec += (finish->tv_usec - start->tv_usec)/1000; | |
| 216 return msec; | |
| 217 } | |
| 218 | |
| 219 | |
| 220 char *output_filename, *input_filename; | |
| 221 /** | |
| 222 * Benchmark MAIN | |
| 223 */ | |
| 224 int main(int argc, char *argv[]) | |
| 225 { | |
| 226 | |
| 227 if (argc < 3) | |
| 228 usage(); | |
| 229 | |
| 230 | |
| 231 input_filename = argv[1]; | |
| 232 output_filename = argv[2]; | |
| 233 | |
| 234 VSs__create_seed_slave_and_do_work( &convert_one_image_wrapper, | |
| 235 NULL ); | |
| 236 | |
| 237 | |
| 238 | |
| 239 return 0; | |
| 240 } | |
| 241 | |
| 242 | |
| 243 | |
| 244 void convert_one_image_wrapper( void *_params, SlaveVP *animSlv ){ | |
| 245 master = animSlv; | |
| 246 | |
| 247 printf("Input file: %s\nOutput file: %s\n",input_filename,output_filename); | |
| 248 | |
| 249 convert_one_image(input_filename, output_filename); | |
| 250 } | |
| 251 |
