//Copyright (c)2008 Sachin Garg. All Rights Reserved. 
//http://www.rawzor.com/        sachingarg@rawzor.com

//lets first disable a few (not so useful) warnings in msvc :-)
#define _CRT_SECURE_NO_WARNINGS

#include <stdio.h>
#include <stdlib.h>
#include "rwz_sdk.h"

int main(int argc, char* argv[]) {

	FILE *in, *out;
	char *infile, *outfile;
	int infile_size, outfile_size, status;

printf("\n\
RAWZOR - LOSSLESS CAMERA RAW COMPRESSION                           \n\
Copyright 2008, All rights reserved.                               \n\
http://www.rawzor.com                        sachingarg@rawzor.com \n\
                                                                   \n\
NOTE: Sample code and executable to test rwz sdk, doesn't performs \n\
full error checking or parameter validation, use only for testing  \n\
etc... (using SDK core version %.3d)                               \n\
                                                                   \n",
rwz_sdk_get_max_version());

if(argc!=4) {
printf("\n\
Usage: rwz_sdk_test d|m|t input_file_name.rwz output_file_name.raw \n\
                                                                   \n\
   d : Decompress a rzw file to get the uncompressed raw image file\n\
   m : Decompress only the raw image's meta data, does not         \n\
       decompresses the raw pixel data or the embedded thumbnail.  \n\
   t : Decompress only the raw image's meta data and embedded      \n\
       thumbnail (if any), does't decompresses the raw pixel data. \n\
                                                                   \n\
example: rwz_sdk_test d birthdaycake.nef.rwz birthdaycake.nef      \n\n");
return 1;
}

	//open the input file
	if(argv[1][0]=='d' || argv[1][0]=='m' || argv[1][0]=='t') {
		if (!(in = fopen(argv[2],"rb"))) {
			printf("Error opening input file\n");
			return 1;
		}

		//get size of input file and load in buffer
		fseek(in,0,SEEK_END);
		infile_size=ftell(in);
		fseek(in,0,SEEK_SET);
		infile=malloc(infile_size);
		if(!infile) { 
			printf("Error alocating memory for input file\n");
			return 1;
		}
		fread(infile,1,infile_size,in);
		fclose(in);
	}
	else {
		printf("Invalid option (try d|m|t)\n");
		return 1;
	}

	//check if input file is a rwz file, and get its uncompressed size
	status=m_rwz_check(infile,infile_size, &outfile_size);
	if(status==2) {
		printf("The input rwz file needs a newer version of Rawzor SDK\n");
		return 1;
	}
	if(status) {
		printf("Input file is not a valid rwz file\n");
		return 1;
	}

	//allocate buffer for output file
	outfile=malloc(outfile_size);
	if(!outfile) { 
		printf("Error alocating memory for output file\n");
		return 1;
	}

	//do what we want to do :-)
	if(argv[1][0]=='d')
		status=m_rwz_decompress(infile,infile_size,outfile,outfile_size);
	if(argv[1][0]=='m')
		status=m_rwz_get_meta_only(infile,infile_size,outfile,outfile_size);
	if(argv[1][0]=='t')
		status=m_rwz_get_meta_and_thumbnail(infile,infile_size,outfile,outfile_size);

	if(status) {
		printf("Failed to decompress file\n");
		return 1;
	}

	//write output file
	if ((out = fopen(argv[3],"rb"))) {
		char ch;
		printf("Output file already exists, replace it? (y/n) : ");
		scanf("%c",&ch);
		if(!(ch=='y' || ch=='Y')) {
			printf("Failed to open output file, file already exists.\n");
			fclose(out);
			return 1;
		}
		fclose(out);
	}
	if (!(out = fopen(argv[3],"wb"))) {
		printf("Error opening output file\n");
		return 1;
	}
	fwrite(outfile,1,outfile_size,out);
	fclose(out);

printf("\n\
Please report any bugs or unexpected errors. Your feedback        \n\
and suggestions will help a lot. Don't forget to check            \n\
for updates.                                                      \n\
                                                                  \n\
http://www.rawzor.com                        sachingarg@rawzor.com\n");

	return 0;
}
