/**
 * Copyright (c) 2001-2008 by PDFTron Systems Inc. All Rights Reserved.
 */
#include "pdf2image.h"
#include <iostream>
#include <sstream>
#include <vector>

using namespace std;

/** 
 * A custom callback that can be used to report errors and other messages.
 */ 
char* PDF2IMAGE_API_CALL MyCallback(int mode, char* msg, void* user_data)
{
	if (mode == PDF2IMAGE_ERROR) {
		cout << "Error: " << msg << endl;
	}
	else if (mode == PDF2IMAGE_MSG) {
		cout << msg;
	}
	else if (mode == PDF2IMAGE_GETPASS) {
		static string gl_pass;
		cin >> gl_pass;
		return (char*)gl_pass.c_str();
	}
	else if (mode == PDF2IMAGE_OUT_FILENAME)
	{
		if(user_data != 0)
		{
			//stores the filename in the given vector 
			vector<string>* saved_files=(vector<string>*)user_data;
			saved_files->push_back(string(msg));
		}
	}

	return 0;
}


/**
 * You can modify the following lines with your registration information.
 */
char* username = "John Doe";
char* company = "My Company";
char* lic_key = "my license key";

/* the path to the resource file 
 * can be NULL in Windows 
 */
#ifdef _WIN32
char* resource_path =NULL;
#else
char* resource_path ="../../lib";
#endif

/**
 * A macro that can be used to switch between different code samples.
 */
#define EXAMPLE 1

/**
 * The simplest PDF2Image program: Pass an explicit command-line string.
 *
 * This example converts 'tiger.pdf' to PNG image 'tiger.png'
 * To convert all PDF documents in 'TestFiles' folder simply delete 'tiger.pdf'
 * from the command string.
 * 
 * For a detailed explanation of all commands, please refer to PDF2Image SDK User 
 * Manual. Also, PDF2Image Command-Line application is a great tool to experiment 
 * with the available options.
 */
#if EXAMPLE == 1
int main() 
{
	PDF2ImageInit(username, company, lic_key, resource_path);
	PDF2ImageRun("-f png -o ../TestOutput/test1 ../../samples/TestFiles/tiger.pdf", MyCallback, 0);
	cout << "Done!" << endl;
	return 0;
}
#endif

/**
 * An example of how to use PDF2Image SDK to implement a command-line application
 * for converting PDF pages to images.
 */
#if EXAMPLE == 2
int main(int argc, char** argv) 
{
	PDF2ImageInit(username, company, lic_key, resource_path);
	stringstream s;
	for (int i=1; i<argc; ++i) {
		s << argv[i] << ' ';
	}

	PDF2ImageRun((char*)s.str().c_str(), MyCallback, 0);
	cout << "Done!" << endl;
	return 0;
}
#endif

/** 
 * An example of how to dynamically build a command string based on 
 * user settings...
 *
 * Please note that this sample does not cover all possible options offered by 
 * PDF2Image. For a detailed explanation of all commands, please refer to PDF2Image 
 * SDK User Manual.
 */
#if EXAMPLE == 3
int main() 
{
	PDF2ImageInit(username, company, lic_key, resource_path);

	string output_folder = "../TestOutput/test2";
	string open_password = "secret";
	string output_format = "png"; // or png, png8, gif, tif, bmp, raw, etc.

	// Controls for the output name of rasterized/converted images...
	string filename_prefix = "";
	int filename_digits = 4;

	bool multipage = false; // used if the output format is 'tiff'.
	bool grayscale = false;

	// the output image resolution (DPI := Dots Pet Inch).
	int dpi = 96; 

	// dimensions of the output image in pixels
	// Used if dpi is 0.	
	int hres = 500, vres=500;

	// compression_quality is used to control lossy compression 
	// algorithms (e.g. jpeg)
	int compression_quality = 60;
	
	int rotate = 0;  // rotates the page 90/180/270 degrees
	bool antialias = true;
	bool printmode = true;

	// --------------------------------------------------
	// Given the above settings build a command string.
	stringstream s;
	if (!output_folder.empty()) s << "-o " << output_folder << " ";
	if (!open_password.empty()) s << "-p " << open_password << " ";
	if (!output_format.empty()) s << "-f " << output_format << " ";

	if (!filename_prefix.empty()) s << "--prefix " << filename_prefix << " ";
	if (filename_digits>0)  s << "--digits " << filename_digits << " ";

	if (multipage) s << "--multipage ";
	if (grayscale) s << "--gray ";

	if (dpi>0) {
		s << "--dpi " << dpi << " ";
	}
	else {  // use explicit image resolution
		s << "--hres " << hres << " --vres " << vres << " ";
	}

	if (compression_quality > 0) {
		s << "-q " << compression_quality << " ";
	}

	if (antialias == false) s << "--nosmooth ";
	if (printmode) s << "--printmode ";

	if (rotate == 90) s << "--rotate 90 ";
	if (rotate == 180) s << "--rotate 180 ";
	if (rotate == 270) s << "--rotate 270 ";

	// Specify input PDF files and folders...
	s << "../../samples/TestFiles/tiger.pdf ";
	s << "../../samples/TestFiles/tai962.pdf "; 
	// s << "c:/my_pdf_folder "; etc ...

	// Execute the command string.
	vector<string> saved_files;
	PDF2ImageRun((char*)s.str().c_str(), MyCallback, &saved_files);

	//print the saved file(s)
	cout << "The Saved Files Are:" << endl;
	for(unsigned int i=0; i<saved_files.size(); i++)
	{
		cout << saved_files[i] << endl;
	}



	cout << "Done!" << endl;
	return 0;
}
#endif

