/**
 *        SP54convert: Simple SP54 codec converter to MJPG.
 *
 *   Testing tool for the spca50x based cams. Made and copyrighted
 *  by Michel Xhaard, Till Adam june in 2003.
 *  
 *  With the help of avilib  Copyright (C) 1999 Rainer Johanni <Rainer@Johanni.de>
 *  
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
**/

#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>

#include "spca50x-jpeg-header.h"


/* Pascal come back */
void
writeln_fatal (char *msg)
{
	printf ("%s \n", msg);
	exit (1);
}

static void
create_jpeg_from_data (u_int8_t * dst, u_int8_t * src, int qIndex, int w,
		       int h, u_int8_t format, int o_size, int *size,
		       int omit_huffman_table, int omit_escape)
{

	int i = 0;
	u_int8_t *start;
	u_int8_t value;

	start = dst;
	/* copy the header from the template */
	memcpy (dst, SPCA50xJPGDefaultHeaderPart1,
		SPCA50X_JPG_DEFAULT_HEADER_PART1_LENGTH);

	/* modify quantization table */
	memcpy (dst + 7, SPCA50xQTable[qIndex * 2], 64);
	memcpy (dst + 72, SPCA50xQTable[qIndex * 2 + 1], 64);

	dst += SPCA50X_JPG_DEFAULT_HEADER_PART1_LENGTH;

	/* copy Huffman table */
	if (!omit_huffman_table) {
		memcpy (dst, SPCA50xJPGDefaultHeaderPart2,
			SPCA50X_JPG_DEFAULT_HEADER_PART2_LENGTH);
		dst += SPCA50X_JPG_DEFAULT_HEADER_PART2_LENGTH;
	}
	memcpy (dst, SPCA50xJPGDefaultHeaderPart3,
		SPCA50X_JPG_DEFAULT_HEADER_PART3_LENGTH);

	/* modify the image width, height */
	*(dst + 8) = w & 0xFF;	//Image width low byte
	*(dst + 7) = w >> 8 & 0xFF;	//Image width high byte
	*(dst + 6) = h & 0xFF;	//Image height low byte
	*(dst + 5) = h >> 8 & 0xFF;	//Image height high byte

	/* set the format */
	*(dst + 11) = format;

	/* point to real JPG compress data start position and copy */
	dst += SPCA50X_JPG_DEFAULT_HEADER_PART3_LENGTH;

	for (i = 0; i < o_size; i++) {
		value = *(src + i) & 0xFF;
		*(dst) = value;
		dst++;

		if (value == 0xFF && !omit_escape) {
			*(dst) = 0x00;
			dst++;
		}
	}
	/* Add end of image marker */
	*(dst++) = 0xFF;
	*(dst++) = 0xD9;

	*size = dst - start;
}

int
main (void)
{
	long width = 160, height = 120;
	long sizein;
	long realsize;
	void *bufferin = NULL;
	void *data = NULL;
	unsigned char *pt_bufferin;
	unsigned char val, probcount;
	int sizeout;


	sizein = 7670;
	bufferin =
		(unsigned char *) realloc (bufferin,
					   (size_t) sizein);

	sizeout = sizein + 8192;
	data = (unsigned char *) realloc (data,
					  (size_t) sizeout);
	printf ("-------------------------------- \n");
	printf (" sizein %d \n", sizein);
	FILE *f = fopen("img.bin", "r");
	if (!f) {
		perror("fopen");
		exit(1);
	}
	fread(bufferin, 1, 7670, f);
	fclose(f);
	/* now compute the real size */
	/* cut the strange bytes at buffer end */
	pt_bufferin = (unsigned char *) bufferin + sizein;
	probcount = 0;
	val = *pt_bufferin;
	while (pt_bufferin > (unsigned char *) bufferin) {
		if (val != 0x00) {
			if (val == 0xFF)
				probcount++;
			else {
				if (probcount == 6
				    || probcount == 2) {
					printf ("skipvalue %0x \n", val);
					pt_bufferin--;
				}
				break;
			}
		}
		val = *pt_bufferin--;
	}
	/* now cut the bad header */
	realsize =
		pt_bufferin - (unsigned char *) bufferin - 14;
	pt_bufferin = bufferin + 14;
	printf ("realsize %d probcount %d \n", realsize,
		probcount);
	printf ("-------------------------------- \n");
	create_jpeg_from_data ((unsigned char *) data,
			       pt_bufferin, 5, width, height,
			       0x22, realsize, &sizeout, 1, 0);
	f = fopen("img.jpg", "w");
	if (!f) {
		perror("fopen");
		exit(1);
	}
	fwrite(data, 1, sizeout, f);
	fclose(f);
}
