diff 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
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/VSs_tinyjpeg/loadjpeg.c	Thu Jul 05 11:35:03 2012 +0200
     1.3 @@ -0,0 +1,251 @@
     1.4 +/*
     1.5 + * Small jpeg decoder library - testing application
     1.6 + *
     1.7 + * Copyright (c) 2006, Luc Saillard <luc@saillard.org>
     1.8 + * All rights reserved.
     1.9 + * Redistribution and use in source and binary forms, with or without
    1.10 + * modification, are permitted provided that the following conditions are met:
    1.11 + *
    1.12 + * - Redistributions of source code must retain the above copyright notice,
    1.13 + *  this list of conditions and the following disclaimer.
    1.14 + *
    1.15 + * - Redistributions in binary form must reproduce the above copyright notice,
    1.16 + *  this list of conditions and the following disclaimer in the documentation
    1.17 + *  and/or other materials provided with the distribution.
    1.18 + *
    1.19 + * - Neither the name of the author nor the names of its contributors may be
    1.20 + *  used to endorse or promote products derived from this software without
    1.21 + *  specific prior written permission.
    1.22 + *
    1.23 + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
    1.24 + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
    1.25 + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
    1.26 + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
    1.27 + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
    1.28 + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
    1.29 + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
    1.30 + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
    1.31 + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
    1.32 + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
    1.33 + * POSSIBILITY OF SUCH DAMAGE.
    1.34 + *
    1.35 + */
    1.36 +
    1.37 +
    1.38 +#include <stdio.h>
    1.39 +#include <stdint.h>
    1.40 +#include <stdlib.h>
    1.41 +#include <string.h>
    1.42 +#include <sys/time.h>
    1.43 +
    1.44 +#include "tinyjpeg.h"
    1.45 +#include "VSs_impl/VSs.h"
    1.46 +
    1.47 +typedef struct timeval timer;
    1.48 +#define TIME(x) gettimeofday(&x, NULL);
    1.49 +
    1.50 +long timevaldiff(timer *start, timer *finish);
    1.51 +
    1.52 +static void exitmessage(const char *message)
    1.53 +{
    1.54 +	printf("%s\n", message);
    1.55 +	exit(0);
    1.56 +}
    1.57 +
    1.58 +static int filesize(FILE *fp)
    1.59 +{
    1.60 +	long pos;
    1.61 +	fseek(fp, 0, SEEK_END);
    1.62 +	pos = ftell(fp);
    1.63 +	fseek(fp, 0, SEEK_SET);
    1.64 +	return pos;
    1.65 +}
    1.66 +
    1.67 +/**
    1.68 + * Save a buffer in 24bits Targa format
    1.69 + * (BGR byte order)
    1.70 + */
    1.71 +FILE* write_tga_header(const char* filename, int width, int height) {
    1.72 +	unsigned char targaheader[18];
    1.73 +	FILE *F;
    1.74 +	char temp[1024];
    1.75 +
    1.76 +	snprintf(temp, sizeof(temp), "%s", filename);
    1.77 +
    1.78 +	memset(targaheader,0,sizeof(targaheader));
    1.79 +
    1.80 +	targaheader[12] = (unsigned char) (width & 0xFF);
    1.81 +	targaheader[13] = (unsigned char) (width >> 8);
    1.82 +	targaheader[14] = (unsigned char) (height & 0xFF);
    1.83 +	targaheader[15] = (unsigned char) (height >> 8);
    1.84 +	targaheader[17] = 0x20;    /* Top-down, non-interlaced */
    1.85 +	targaheader[2]  = 2;       /* image type = uncompressed RGB */
    1.86 +	targaheader[16] = 24;
    1.87 +
    1.88 +	
    1.89 +	F = fopen(temp, "wb");
    1.90 +	fwrite(targaheader, sizeof(targaheader), 1, F);
    1.91 +	return F;
    1.92 +}
    1.93 +
    1.94 +//todo
    1.95 +//#pragma omp task input(*rgb_data) output(*d) inout(*d)
    1.96 +void write_tga_task(FILE* fp, int bufferlen, unsigned char* rgb_data, char* d) {
    1.97 +  
    1.98 +	// To disable ompss warnings
    1.99 +	d = d;  
   1.100 +	unsigned char *data = rgb_data + bufferlen - RGB_DEPTH;
   1.101 +	do
   1.102 +	{
   1.103 +		unsigned char c = data[0];
   1.104 +		data[0] = data[2];
   1.105 +		data[2] = c;
   1.106 +		data-=RGB_DEPTH;
   1.107 +	} while (data >= rgb_data);
   1.108 +	
   1.109 +	fwrite(rgb_data, 1, bufferlen, fp);
   1.110 +}
   1.111 +
   1.112 +
   1.113 +int32 tinyjpegArgTypes[2] = {IN, OUT};
   1.114 +int32 tinyjpegArgSizes[2] = {sizeof(struct jdec_private), sizeof(uint8_t)};
   1.115 +
   1.116 +/**
   1.117 + * Load one jpeg image, and decompress it, and save the result.
   1.118 + */
   1.119 +int convert_one_image(const char *infilename, const char *outfilename)
   1.120 +{
   1.121 +	FILE *fp;
   1.122 +	unsigned int length_of_file;
   1.123 +	unsigned int width, height;
   1.124 +	unsigned char *buf;
   1.125 +	struct jdec_private *jdec;	//for parsing header
   1.126 +	struct jdec_private **jdec_task;	//for decoding mcus
   1.127 +	uint8_t *rgb_data;
   1.128 +	int i;
   1.129 +	int ntasks;
   1.130 +
   1.131 +	/* Load the Jpeg into memory */
   1.132 +	fp = fopen(infilename, "rb");
   1.133 +	if (fp == NULL)
   1.134 +		perror("Cannot open image");//exitmessage("Cannot open filename\n");
   1.135 +	length_of_file = filesize(fp);
   1.136 +	buf = (unsigned char *)malloc(length_of_file + 4);
   1.137 +	if (buf == NULL)
   1.138 +		exitmessage("Not enough memory for loading file\n");
   1.139 +	fread(buf, length_of_file, 1, fp);
   1.140 +	fclose(fp);
   1.141 +
   1.142 +	/* Decompress it */
   1.143 +	jdec = tinyjpeg_init();
   1.144 +	if (jdec == NULL)
   1.145 +		exitmessage("Not enough memory to alloc the structure need for decompressing\n");
   1.146 +
   1.147 +	if (tinyjpeg_parse_header(jdec, buf, length_of_file)<0)
   1.148 +		exitmessage(tinyjpeg_get_errorstring());
   1.149 +
   1.150 +	/* Get the size of the image */
   1.151 +	tinyjpeg_get_size(jdec, &width, &height);
   1.152 +
   1.153 +	// RGB stuff
   1.154 +	rgb_data = (uint8_t *)malloc(width * height * RGB_DEPTH);
   1.155 +	jdec->components[0] = rgb_data;
   1.156 +
   1.157 +	// this jpeg decoder only supports full MCUs for simplicity
   1.158 +	ntasks = (jdec->mcus_in_width * jdec->mcus_in_height)/ jdec->restart_interval;
   1.159 +	jdec_task = (struct jdec_private **) malloc ( ntasks * sizeof(struct jdec_private*));
   1.160 +	
   1.161 +
   1.162 +        //VSs setup
   1.163 +        tinyjpegTaskType = VMS_App__malloc( sizeof(VSsTaskType) );
   1.164 +        tinyjpegTaskType->fn = &tinyjpeg_decode_task;
   1.165 +        tinyjpegTaskType->numCtldArgs = 2;
   1.166 +        tinyjpegTaskType->numTotalArgs = 2;
   1.167 +        tinyjpegTaskType->sizeOfArgs = sizeof(tinyjpeg_decode_task_args);
   1.168 +        tinyjpegTaskType->argTypes = tinyjpegArgTypes;
   1.169 +        tinyjpegTaskType->argSizes = tinyjpegArgSizes;
   1.170 +                
   1.171 +        tinyjpeg_decode_task_args args;
   1.172 +        
   1.173 +	fp = write_tga_header(outfilename, width, height);
   1.174 +	printf("Decoding JPEG image...\n");
   1.175 +	for (i=0; i<ntasks; i++){
   1.176 +		jdec_task[i] = create_jdec_priv_task(jdec, i);
   1.177 +                
   1.178 +                args.priv = jdec_task[i];
   1.179 +                args.context = rgb_data+i*width*RGB_DEPTH*MCU_Y_STRIDE;
   1.180 +                VSs__submit_task(tinyjpegTaskType, &args, master);
   1.181 +                        	
   1.182 +	}
   1.183 +	
   1.184 +	char dummy;
   1.185 +	for(i=0; i<ntasks;i++) {		
   1.186 +		write_tga_task(fp, width*RGB_DEPTH*MCU_Y_STRIDE, rgb_data+i*RGB_DEPTH*width*MCU_Y_STRIDE, &dummy);
   1.187 +	}
   1.188 +        
   1.189 +        //VSs__wait_for_all_tasks_to_complete();
   1.190 +	//#pragma omp barrier
   1.191 +
   1.192 +	tinyjpeg_free(jdec);
   1.193 +	for(i=0; i < ntasks; i++) {
   1.194 +	    tinyjpeg_free(jdec_task[i]);
   1.195 +	}
   1.196 +	fclose(fp);		
   1.197 +	free(buf);
   1.198 +	free(rgb_data);
   1.199 +	free(jdec_task);
   1.200 +	return 0;
   1.201 +}
   1.202 +
   1.203 +/*
   1.204 + *   Usage information.
   1.205 + */
   1.206 +static void usage(void)
   1.207 +{
   1.208 +	fprintf(stderr, "Usage: loadjpeg <input_filename.jpeg> <output_filename>\n");
   1.209 +	exit(1);
   1.210 +}
   1.211 +
   1.212 +/*
   1.213 + *   Calculates the time difference between start and finish in msecs.
   1.214 + */
   1.215 +long timevaldiff(timer *start, timer *finish){
   1.216 +	long msec;
   1.217 +	msec = (finish->tv_sec - start->tv_sec)*1000;
   1.218 +	msec += (finish->tv_usec - start->tv_usec)/1000;
   1.219 +	return msec;
   1.220 +}
   1.221 +
   1.222 +
   1.223 +    char *output_filename, *input_filename;
   1.224 +/**
   1.225 + * Benchmark MAIN
   1.226 + */
   1.227 +int main(int argc, char *argv[])
   1.228 +{
   1.229 +
   1.230 +	if (argc < 3)
   1.231 +		usage();
   1.232 +
   1.233 +        
   1.234 +        input_filename = argv[1];
   1.235 +        output_filename = argv[2];
   1.236 +
   1.237 +        VSs__create_seed_slave_and_do_work( &convert_one_image_wrapper,
   1.238 +                                       NULL );
   1.239 +        
   1.240 +
   1.241 +
   1.242 +	return 0;
   1.243 +}
   1.244 +
   1.245 +
   1.246 +
   1.247 +void convert_one_image_wrapper( void *_params, SlaveVP *animSlv ){
   1.248 +    master = animSlv;
   1.249 +
   1.250 +    printf("Input file: %s\nOutput file: %s\n",input_filename,output_filename);
   1.251 +    
   1.252 +    convert_one_image(input_filename, output_filename);
   1.253 +}
   1.254 +