view VSs_tinyjpeg/loadjpeg.c @ 3:42d636fee562

added second type of task
author Nina Engelhardt <nengel@mailbox.tu-berlin.de>
date Fri, 13 Jul 2012 17:59:00 +0200
parents a52de05d2e2b
children 62350c40504f
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* args = (write_tga_task_args*) _data;
107 FILE* fp = args->fp;
108 int bufferlen = args->bufferlen;
109 unsigned char* rgb_data = args->rgb_data;
110 char* d = args->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 }
127 int32 tinyjpegArgTypes[2] = {IN, OUT};
128 int32 tinyjpegArgSizes[2] = {sizeof(struct jdec_private), sizeof(uint8_t)};
130 /**
131 * Load one jpeg image, and decompress it, and save the result.
132 */
133 int convert_one_image(const char *infilename, const char *outfilename)
134 {
135 FILE *fp;
136 unsigned int length_of_file;
137 unsigned int width, height;
138 unsigned char *buf;
139 struct jdec_private *jdec; //for parsing header
140 struct jdec_private **jdec_task; //for decoding mcus
141 uint8_t *rgb_data;
142 int i;
143 int ntasks;
145 /* Load the Jpeg into memory */
146 fp = fopen(infilename, "rb");
147 if (fp == NULL)
148 perror("Cannot open image");//exitmessage("Cannot open filename\n");
149 length_of_file = filesize(fp);
150 buf = (unsigned char *)VMS_App__malloc(length_of_file + 4);
151 if (buf == NULL)
152 exitmessage("Not enough memory for loading file\n");
153 fread(buf, length_of_file, 1, fp);
154 fclose(fp);
156 /* Decompress it */
157 jdec = tinyjpeg_init();
158 if (jdec == NULL)
159 exitmessage("Not enough memory to alloc the structure need for decompressing\n");
161 if (tinyjpeg_parse_header(jdec, buf, length_of_file)<0)
162 exitmessage(tinyjpeg_get_errorstring());
164 /* Get the size of the image */
165 tinyjpeg_get_size(jdec, &width, &height);
167 // RGB stuff
168 rgb_data = (uint8_t *)VMS_App__malloc(width * height * RGB_DEPTH);
169 jdec->components[0] = rgb_data;
171 // this jpeg decoder only supports full MCUs for simplicity
172 ntasks = (jdec->mcus_in_width * jdec->mcus_in_height)/ jdec->restart_interval;
173 jdec_task = (struct jdec_private **) VMS_App__malloc ( ntasks * sizeof(struct jdec_private*));
176 //VSs setup
177 tinyjpegTaskType = VMS_App__malloc( sizeof(VSsTaskType) );
178 tinyjpegTaskType->fn = &tinyjpeg_decode_task;
179 tinyjpegTaskType->numCtldArgs = 2;
180 tinyjpegTaskType->numTotalArgs = 2;
181 tinyjpegTaskType->sizeOfArgs = sizeof(tinyjpeg_decode_task_args);
182 tinyjpegTaskType->argTypes = tinyjpegArgTypes;
183 tinyjpegTaskType->argSizes = tinyjpegArgSizes;
185 tinyjpeg_decode_task_args args;
187 fp = write_tga_header(outfilename, width, height);
188 printf("Decoding JPEG image...\n");
189 for (i=0; i<ntasks; i++){
190 jdec_task[i] = create_jdec_priv_task(jdec, i);
192 args.priv = jdec_task[i];
193 args.context = rgb_data+i*width*RGB_DEPTH*MCU_Y_STRIDE;
194 VSs__submit_task(tinyjpegTaskType, &args, seedSlv);
196 }
198 write_tga_taskType = VMS_App__malloc( sizeof(VSsTaskType) );
199 write_tga_taskType->fn = &write_tga_task;
200 write_tga_taskType->numCtldArgs = 2;
201 write_tga_taskType->numTotalArgs = 4;
202 write_tga_taskType->sizeOfArgs = sizeof(write_tga_task_args);
203 write_tga_taskType->argTypes = write_tga_taskArgTypes;
204 write_tga_taskType->argSizes = write_tga_taskArgSizes;
206 write_tga_task_args args2;
207 char dummy;
208 for(i=0; i<ntasks;i++) {
209 args2.fp = fp;
210 args2.bufferlen = width*RGB_DEPTH*MCU_Y_STRIDE;
211 args2.rgb_data = rgb_data+i*RGB_DEPTH*width*MCU_Y_STRIDE;
212 args2.d = &dummy;
213 VSs__submit_task(write_tga_taskType, &args2, seedSlv);
214 }
216 VSs__taskwait(seedSlv);
217 //#pragma omp barrier
219 tinyjpeg_free(jdec);
220 for(i=0; i < ntasks; i++) {
221 tinyjpeg_free(jdec_task[i]);
222 }
223 fclose(fp);
224 VMS_App__free(buf);
225 VMS_App__free(rgb_data);
226 VMS_App__free(jdec_task);
227 return 0;
228 }
230 /*
231 * Usage information.
232 */
233 static void usage(void)
234 {
235 fprintf(stderr, "Usage: loadjpeg <input_filename.jpeg> <output_filename>\n");
236 exit(1);
237 }
239 /*
240 * Calculates the time difference between start and finish in msecs.
241 */
242 long timevaldiff(timer *start, timer *finish){
243 long msec;
244 msec = (finish->tv_sec - start->tv_sec)*1000;
245 msec += (finish->tv_usec - start->tv_usec)/1000;
246 return msec;
247 }
250 char *output_filename, *input_filename;
251 /**
252 * Benchmark MAIN
253 */
254 int main(int argc, char *argv[])
255 {
257 if (argc < 3)
258 usage();
261 input_filename = argv[1];
262 output_filename = argv[2];
264 VSs__create_seed_slave_and_do_work( &convert_one_image_wrapper,
265 NULL );
269 return 0;
270 }
274 void convert_one_image_wrapper( void *_params, SlaveVP *animSlv ){
275 seedSlv = animSlv;
277 printf("Input file: %s\nOutput file: %s\n",input_filename,output_filename);
279 convert_one_image(input_filename, output_filename);
281 VSs__dissipate_slave( animSlv );
282 }