



#include <stdio.h>
#include <stdlib.h>
#include <string.h>



extern "C" 
{
#include "jpeglib.h"
#include <setjmp.h>
}





struct scol_error_mgr
{
	struct jpeg_error_mgr pub;
	jmp_buf setjmp_buffer;
};
typedef struct scol_error_mgr * scol_error_ptr;



METHODDEF(void) scol_error_exit (j_common_ptr cinfo)
{
	scol_error_ptr scolerr = (scol_error_ptr) cinfo->err;
	longjmp(scolerr->setjmp_buffer, 1);
}


void scol_output(j_common_ptr cinfo)
{
	
}



char*	LoadJpgFromFile(char *filename, int *w, int *h)
{
	struct jpeg_decompress_struct	cinfo;
	struct scol_error_mgr			jerr;

	JSAMPARRAY	buffer;				// Output row buffer
	int			row_stride;			// physical row width in output buffer
	FILE		*infile;

	char*		bitmap;

	int			i, j;

	if ((infile=fopen(filename,"rb"))==NULL)	return NULL;
	
	cinfo.err = jpeg_std_error(&jerr.pub);
	jerr.pub.error_exit = scol_error_exit;
	jerr.pub.output_message = scol_output;
	
	if (setjmp(jerr.setjmp_buffer)) 
	{
		jpeg_destroy_decompress(&cinfo);
		fclose(infile);
		return NULL;
	}
	
	jpeg_create_decompress(&cinfo);
	
	jpeg_stdio_src(&cinfo, infile);
	
	jpeg_read_header(&cinfo, TRUE);
	jpeg_start_decompress(&cinfo);
	
	i = cinfo.output_width;
	j = cinfo.output_height;
	
	*w = i;
	*h = j;

	bitmap  = (char*) malloc(i*j*4);
	memset(bitmap, 0, i*j*4);
	
	if(bitmap==NULL)
	{
		fclose(infile);
		return NULL;
	}
	
	row_stride = cinfo.output_width * cinfo.output_components;
	
	buffer = (*cinfo.mem->alloc_sarray)	((j_common_ptr) &cinfo, JPOOL_IMAGE, row_stride, 1);
	
	
	int line = j-1;
	while (cinfo.output_scanline < cinfo.output_height)
	{
		jpeg_read_scanlines(&cinfo, buffer, 1);
		
		char *buffer2;
		buffer2 = (char*) buffer[0];
		
		for(int k=0; k<i; k++)
		{
			bitmap[(line*i+k)*4+0] = (char)	*(buffer2++);
			bitmap[(line*i+k)*4+1] = (char)	*(buffer2++);
			bitmap[(line*i+k)*4+2] = (char)	*(buffer2++);
			bitmap[(line*i+k)*4+3] = (char) 255;
		}
		
		line--;
	}
	
	
	jpeg_finish_decompress(&cinfo);
	jpeg_destroy_decompress(&cinfo);
	fclose(infile);
	
	return bitmap;
}




/*
char *stdloadJpg(char *name, int *w, int *h)
{
	struct jpeg_decompress_struct cinfo;
	struct scol_error_mgr jerr;
	
	JSAMPARRAY buffer;
	int row_stride;	
	short *dst;
	FILE *infile;
	char *bitmap;
	
	int i,j,length;
	
	if ((infile=fopen(name,"rb"))==NULL) return NULL;
	
	cinfo.err = jpeg_std_error(&jerr.pub);
	jerr.pub.error_exit = scol_error_exit;
	
	if (setjmp(jerr.setjmp_buffer)) 
	{
		jpeg_destroy_decompress(&cinfo);
		fclose(infile);
		return NULL;
	}
	
	jpeg_create_decompress(&cinfo);
	
	jpeg_stdio_src(&cinfo, infile);
	
	jpeg_read_header(&cinfo, TRUE);
	jpeg_start_decompress(&cinfo);
	//  MMechostr(MSKDEBUG,"jpeg : %dx%dx%d\n",cinfo.output_width,
	//		cinfo.output_height,cinfo.output_components);
	
	i=cinfo.output_width;
	j=cinfo.output_height;
	
	*w=i;
	*h=j;
	
	length=i*j*2;
	bitmap=(char*)malloc(length);
	if (bitmap==NULL)
    {
		fclose(infile);
		return NULL;
    }

	row_stride = cinfo.output_width * cinfo.output_components;
	
	buffer = (*cinfo.mem->alloc_sarray)
		((j_common_ptr) &cinfo, JPOOL_IMAGE, row_stride, 1);
	
	dst=(short*)bitmap;
	dst+=i*(cinfo.output_height-1);
	
	while (cinfo.output_scanline < cinfo.output_height)
	{
		jpeg_read_scanlines(&cinfo, buffer, 1);
		dst-=i;
	}
	jpeg_finish_decompress(&cinfo);
	jpeg_destroy_decompress(&cinfo);
	fclose(infile);
	
	return bitmap;
}
*/