/*
 * Small jpeg decoder library - testing application
 *
 * Copyright (c) 2006, Luc Saillard <luc@saillard.org>
 * All rights reserved.
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * - Redistributions of source code must retain the above copyright notice,
 *  this list of conditions and the following disclaimer.
 *
 * - Redistributions in binary form must reproduce the above copyright notice,
 *  this list of conditions and the following disclaimer in the documentation
 *  and/or other materials provided with the distribution.
 *
 * - Neither the name of the author nor the names of its contributors may be
 *  used to endorse or promote products derived from this software without
 *  specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 *
 */


#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <sys/time.h>

#include "tinyjpeg.h"
#include "VSs_impl/VSs.h"

typedef struct timeval timer;
#define TIME(x) gettimeofday(&x, NULL);

long timevaldiff(timer *start, timer *finish);

static void exitmessage(const char *message)
{
	printf("%s\n", message);
	exit(0);
}

static int filesize(FILE *fp)
{
	long pos;
	fseek(fp, 0, SEEK_END);
	pos = ftell(fp);
	fseek(fp, 0, SEEK_SET);
	return pos;
}

/**
 * Save a buffer in 24bits Targa format
 * (BGR byte order)
 */
FILE* write_tga_header(const char* filename, int width, int height) {
	unsigned char targaheader[18];
	FILE *F;
	char temp[1024];

	snprintf(temp, sizeof(temp), "%s", filename);

	memset(targaheader,0,sizeof(targaheader));

	targaheader[12] = (unsigned char) (width & 0xFF);
	targaheader[13] = (unsigned char) (width >> 8);
	targaheader[14] = (unsigned char) (height & 0xFF);
	targaheader[15] = (unsigned char) (height >> 8);
	targaheader[17] = 0x20;    /* Top-down, non-interlaced */
	targaheader[2]  = 2;       /* image type = uncompressed RGB */
	targaheader[16] = 24;

	
	F = fopen(temp, "wb");
	fwrite(targaheader, sizeof(targaheader), 1, F);
	return F;
}

typedef struct{
    unsigned char* rgb_data;
    char* d;
    FILE* fp;
    int bufferlen;
} write_tga_task_args;

VSsTaskType *write_tga_taskType;

int32 write_tga_taskArgTypes[2] = {IN, INOUT};
int32 write_tga_taskArgSizes[2] = {sizeof(unsigned char), sizeof(char)};

//#pragma omp task input(*rgb_data) output(*d) inout(*d)
void write_tga_task(void *_data, SlaveVP *animatingSlv ) {

    write_tga_task_args* args2 = (write_tga_task_args*) _data;
    FILE* fp = args2->fp;
    int bufferlen = args2->bufferlen;
    unsigned char* rgb_data = args2->rgb_data;
    char* d = args2->d;
    
	// To disable ompss warnings
	d = d;  
	unsigned char *data = rgb_data + bufferlen - RGB_DEPTH;
	do
	{
		unsigned char c = data[0];
		data[0] = data[2];
		data[2] = c;
		data-=RGB_DEPTH;
	} while (data >= rgb_data);
	
	fwrite(rgb_data, 1, bufferlen, fp);
        VSs__end_task( animatingSlv );
}


int32 tinyjpegArgTypes[2] = {IN, OUT};
int32 tinyjpegArgSizes[2] = {sizeof(struct jdec_private), sizeof(uint8_t)};

/**
 * Load one jpeg image, and decompress it, and save the result.
 */
int convert_one_image(const char *infilename, const char *outfilename)
{
	FILE *fp;
	unsigned int length_of_file;
	unsigned int width, height;
	unsigned char *buf;
	struct jdec_private *jdec;	//for parsing header
	struct jdec_private **jdec_task;	//for decoding mcus
	uint8_t *rgb_data;
	int i;
	int ntasks;

	/* Load the Jpeg into memory */
	fp = fopen(infilename, "rb");
	if (fp == NULL)
		perror("Cannot open image");//exitmessage("Cannot open filename\n");
	length_of_file = filesize(fp);
	buf = (unsigned char *)VMS_App__malloc(length_of_file + 4);
	if (buf == NULL)
		exitmessage("Not enough memory for loading file\n");
	fread(buf, length_of_file, 1, fp);
	fclose(fp);

	/* Decompress it */
	jdec = tinyjpeg_init();
	if (jdec == NULL)
		exitmessage("Not enough memory to alloc the structure need for decompressing\n");

	if (tinyjpeg_parse_header(jdec, buf, length_of_file)<0)
		exitmessage(tinyjpeg_get_errorstring());

	/* Get the size of the image */
	tinyjpeg_get_size(jdec, &width, &height);

	// RGB stuff
	rgb_data = (uint8_t *)VMS_App__malloc(width * height * RGB_DEPTH);
	jdec->components[0] = rgb_data;

	// this jpeg decoder only supports full MCUs for simplicity
	ntasks = (jdec->mcus_in_width * jdec->mcus_in_height)/ jdec->restart_interval;
	jdec_task = (struct jdec_private **) VMS_App__malloc ( ntasks * sizeof(struct jdec_private*));
	

        //VSs setup
        tinyjpegTaskType = VMS_App__malloc( sizeof(VSsTaskType) );
        tinyjpegTaskType->fn = &tinyjpeg_decode_task;
        tinyjpegTaskType->numCtldArgs = 2;
        tinyjpegTaskType->numTotalArgs = 2;
        tinyjpegTaskType->sizeOfArgs = sizeof(tinyjpeg_decode_task_args);
        tinyjpegTaskType->argTypes = tinyjpegArgTypes;
        tinyjpegTaskType->argSizes = tinyjpegArgSizes;
                
        tinyjpeg_decode_task_args args;
        
	fp = write_tga_header(outfilename, width, height);
	printf("Decoding JPEG image...\n");
	for (i=0; i<ntasks; i++){
		jdec_task[i] = create_jdec_priv_task(jdec, i);
                
                args.priv = jdec_task[i];
                args.context = rgb_data+i*width*RGB_DEPTH*MCU_Y_STRIDE;
                VSs__submit_task(tinyjpegTaskType, &args, seedSlv);
                        	
	}
	
        write_tga_taskType = VMS_App__malloc( sizeof(VSsTaskType) );
        write_tga_taskType->fn = &write_tga_task;
        write_tga_taskType->numCtldArgs = 2;
        write_tga_taskType->numTotalArgs = 4;
        write_tga_taskType->sizeOfArgs = sizeof(write_tga_task_args);
        write_tga_taskType->argTypes = write_tga_taskArgTypes;
        write_tga_taskType->argSizes = write_tga_taskArgSizes;
        
        write_tga_task_args args2;
	char dummy;
	for(i=0; i<ntasks;i++) {
            args2.fp = fp;
            args2.bufferlen = width*RGB_DEPTH*MCU_Y_STRIDE;
            args2.rgb_data = rgb_data+i*RGB_DEPTH*width*MCU_Y_STRIDE;
            args2.d = &dummy;
                VSs__submit_task(write_tga_taskType, &args2, seedSlv);
	}
        
        VSs__taskwait(seedSlv);
	//#pragma omp barrier

	tinyjpeg_free(jdec);
	for(i=0; i < ntasks; i++) {
	    tinyjpeg_free(jdec_task[i]);
	}
	fclose(fp);		
	VMS_App__free(buf);
	VMS_App__free(rgb_data);
	VMS_App__free(jdec_task);
	return 0;
}

/*
 *   Usage information.
 */
static void usage(void)
{
	fprintf(stderr, "Usage: loadjpeg <input_filename.jpeg> <output_filename>\n");
	exit(1);
}

/*
 *   Calculates the time difference between start and finish in msecs.
 */
long timevaldiff(timer *start, timer *finish){
	long msec;
	msec = (finish->tv_sec - start->tv_sec)*1000;
	msec += (finish->tv_usec - start->tv_usec)/1000;
	return msec;
}


    char *output_filename, *input_filename;
/**
 * Benchmark MAIN
 */
int main(int argc, char *argv[])
{

	if (argc < 3)
		usage();

        
        input_filename = argv[1];
        output_filename = argv[2];

        VSs__create_seed_slave_and_do_work( &convert_one_image_wrapper,
                                       NULL );
        


	exit(0);
}



void convert_one_image_wrapper( void *_params, SlaveVP *animSlv ){
    seedSlv = animSlv;

    printf("Input file: %s\nOutput file: %s\n",input_filename,output_filename);
    
    convert_one_image(input_filename, output_filename);
    
    VSs__end_thread( animSlv );
}

