Mercurial > cgi-bin > hgwebdir.cgi > PR > Applications > VSs > VSs__jpeg_decoder__Proj
view VSs_tinyjpeg/loadjpeg.c @ 4:62350c40504f
running in sequential mode
author | Nina Engelhardt <nengel@mailbox.tu-berlin.de> |
---|---|
date | Mon, 20 Aug 2012 16:56:27 +0200 |
parents | 42d636fee562 |
children |
line source
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 */
35 #include <stdio.h>
36 #include <stdint.h>
37 #include <stdlib.h>
38 #include <string.h>
39 #include <sys/time.h>
41 #include "tinyjpeg.h"
42 #include "VSs_impl/VSs.h"
44 typedef struct timeval timer;
45 #define TIME(x) gettimeofday(&x, NULL);
47 long timevaldiff(timer *start, timer *finish);
49 static void exitmessage(const char *message)
50 {
51 printf("%s\n", message);
52 exit(0);
53 }
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 }
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];
73 snprintf(temp, sizeof(temp), "%s", filename);
75 memset(targaheader,0,sizeof(targaheader));
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;
86 F = fopen(temp, "wb");
87 fwrite(targaheader, sizeof(targaheader), 1, F);
88 return F;
89 }
91 typedef struct{
92 unsigned char* rgb_data;
93 char* d;
94 FILE* fp;
95 int bufferlen;
96 } write_tga_task_args;
98 VSsTaskType *write_tga_taskType;
100 int32 write_tga_taskArgTypes[2] = {IN, INOUT};
101 int32 write_tga_taskArgSizes[2] = {sizeof(unsigned char), sizeof(char)};
103 //#pragma omp task input(*rgb_data) output(*d) inout(*d)
104 void write_tga_task(void *_data, SlaveVP *animatingSlv ) {
106 write_tga_task_args* args2 = (write_tga_task_args*) _data;
107 FILE* fp = args2->fp;
108 int bufferlen = args2->bufferlen;
109 unsigned char* rgb_data = args2->rgb_data;
110 char* d = args2->d;
112 // To disable ompss warnings
113 d = d;
114 unsigned char *data = rgb_data + bufferlen - RGB_DEPTH;
115 do
116 {
117 unsigned char c = data[0];
118 data[0] = data[2];
119 data[2] = c;
120 data-=RGB_DEPTH;
121 } while (data >= rgb_data);
123 fwrite(rgb_data, 1, bufferlen, fp);
124 VSs__end_task( animatingSlv );
125 }
128 int32 tinyjpegArgTypes[2] = {IN, OUT};
129 int32 tinyjpegArgSizes[2] = {sizeof(struct jdec_private), sizeof(uint8_t)};
131 /**
132 * Load one jpeg image, and decompress it, and save the result.
133 */
134 int convert_one_image(const char *infilename, const char *outfilename)
135 {
136 FILE *fp;
137 unsigned int length_of_file;
138 unsigned int width, height;
139 unsigned char *buf;
140 struct jdec_private *jdec; //for parsing header
141 struct jdec_private **jdec_task; //for decoding mcus
142 uint8_t *rgb_data;
143 int i;
144 int ntasks;
146 /* Load the Jpeg into memory */
147 fp = fopen(infilename, "rb");
148 if (fp == NULL)
149 perror("Cannot open image");//exitmessage("Cannot open filename\n");
150 length_of_file = filesize(fp);
151 buf = (unsigned char *)VMS_App__malloc(length_of_file + 4);
152 if (buf == NULL)
153 exitmessage("Not enough memory for loading file\n");
154 fread(buf, length_of_file, 1, fp);
155 fclose(fp);
157 /* Decompress it */
158 jdec = tinyjpeg_init();
159 if (jdec == NULL)
160 exitmessage("Not enough memory to alloc the structure need for decompressing\n");
162 if (tinyjpeg_parse_header(jdec, buf, length_of_file)<0)
163 exitmessage(tinyjpeg_get_errorstring());
165 /* Get the size of the image */
166 tinyjpeg_get_size(jdec, &width, &height);
168 // RGB stuff
169 rgb_data = (uint8_t *)VMS_App__malloc(width * height * RGB_DEPTH);
170 jdec->components[0] = rgb_data;
172 // this jpeg decoder only supports full MCUs for simplicity
173 ntasks = (jdec->mcus_in_width * jdec->mcus_in_height)/ jdec->restart_interval;
174 jdec_task = (struct jdec_private **) VMS_App__malloc ( ntasks * sizeof(struct jdec_private*));
177 //VSs setup
178 tinyjpegTaskType = VMS_App__malloc( sizeof(VSsTaskType) );
179 tinyjpegTaskType->fn = &tinyjpeg_decode_task;
180 tinyjpegTaskType->numCtldArgs = 2;
181 tinyjpegTaskType->numTotalArgs = 2;
182 tinyjpegTaskType->sizeOfArgs = sizeof(tinyjpeg_decode_task_args);
183 tinyjpegTaskType->argTypes = tinyjpegArgTypes;
184 tinyjpegTaskType->argSizes = tinyjpegArgSizes;
186 tinyjpeg_decode_task_args args;
188 fp = write_tga_header(outfilename, width, height);
189 printf("Decoding JPEG image...\n");
190 for (i=0; i<ntasks; i++){
191 jdec_task[i] = create_jdec_priv_task(jdec, i);
193 args.priv = jdec_task[i];
194 args.context = rgb_data+i*width*RGB_DEPTH*MCU_Y_STRIDE;
195 VSs__submit_task(tinyjpegTaskType, &args, seedSlv);
197 }
199 write_tga_taskType = VMS_App__malloc( sizeof(VSsTaskType) );
200 write_tga_taskType->fn = &write_tga_task;
201 write_tga_taskType->numCtldArgs = 2;
202 write_tga_taskType->numTotalArgs = 4;
203 write_tga_taskType->sizeOfArgs = sizeof(write_tga_task_args);
204 write_tga_taskType->argTypes = write_tga_taskArgTypes;
205 write_tga_taskType->argSizes = write_tga_taskArgSizes;
207 write_tga_task_args args2;
208 char dummy;
209 for(i=0; i<ntasks;i++) {
210 args2.fp = fp;
211 args2.bufferlen = width*RGB_DEPTH*MCU_Y_STRIDE;
212 args2.rgb_data = rgb_data+i*RGB_DEPTH*width*MCU_Y_STRIDE;
213 args2.d = &dummy;
214 VSs__submit_task(write_tga_taskType, &args2, seedSlv);
215 }
217 VSs__taskwait(seedSlv);
218 //#pragma omp barrier
220 tinyjpeg_free(jdec);
221 for(i=0; i < ntasks; i++) {
222 tinyjpeg_free(jdec_task[i]);
223 }
224 fclose(fp);
225 VMS_App__free(buf);
226 VMS_App__free(rgb_data);
227 VMS_App__free(jdec_task);
228 return 0;
229 }
231 /*
232 * Usage information.
233 */
234 static void usage(void)
235 {
236 fprintf(stderr, "Usage: loadjpeg <input_filename.jpeg> <output_filename>\n");
237 exit(1);
238 }
240 /*
241 * Calculates the time difference between start and finish in msecs.
242 */
243 long timevaldiff(timer *start, timer *finish){
244 long msec;
245 msec = (finish->tv_sec - start->tv_sec)*1000;
246 msec += (finish->tv_usec - start->tv_usec)/1000;
247 return msec;
248 }
251 char *output_filename, *input_filename;
252 /**
253 * Benchmark MAIN
254 */
255 int main(int argc, char *argv[])
256 {
258 if (argc < 3)
259 usage();
262 input_filename = argv[1];
263 output_filename = argv[2];
265 VSs__create_seed_slave_and_do_work( &convert_one_image_wrapper,
266 NULL );
270 exit(0);
271 }
275 void convert_one_image_wrapper( void *_params, SlaveVP *animSlv ){
276 seedSlv = animSlv;
278 printf("Input file: %s\nOutput file: %s\n",input_filename,output_filename);
280 convert_one_image(input_filename, output_filename);
282 VSs__end_thread( animSlv );
283 }