// a custom script for processing images in two directories

// create generic dialog for getting parameters
// NOTE: the uberdialog numbers all elements including statics by the order they are added.
// the indexes shown are also returned by the corresponding functions.  We use them later to later retrieve values.

var dlg = new Dialog("Panoramic resample script", 700, 200);

var types = new Array("bmp","jpg","ppm","tga","tif", "hdr", "pfm");

dlg.AddStatic("INPUT SETTINGS"); // 0
dlg.AddString("Directory", "c:\\"); // 1
dlg.AddChoice("File type", 4, types[0] + "|" + types[1] + "|" + types[2] + "|" + types[3] + "|" + types[4] + "|" + types[5] + "|" + types[6]); // 2
dlg.AddPanoramicFormat() // 3

dlg.AddStatic("OUTPUT SETTINGS"); // 4
dlg.AddString("Directory", "c:\\"); // 5

// this last line adds error checking so that the output panoramic format has the proper width and height.
dlg.EnsurePanoramicAspectRatio( dlg.AddPanoramicFormat(), dlg.AddInt("Width", 1024, 1), dlg.AddInt("Height", 512, 1), dlg.AddCheck("Keep Aspect Ratio", 1)); // 6, 7, 8, 9
dlg.AddChoice("Sampling", 2, "Nearest neighbor|Bilinear|Bicubic"); // 10
dlg.AddInt("Supersample", 3); // 11

var result = dlg.DoModal(); // create the window (similar to the MFC style DoModal function)

if (result == 1) // if the user clicked OK, process images
{
	// get all files ending with desired extension
	var path = dlg.GetString(1) + "*." + types[dlg.GetChoice(2)];
	var filelist = System.Files(path);

	// get other parameters from dialog
	var newpath = dlg.GetString(5);
	var inputformat = dlg.GetPanoramicFormat(3);
	var outputformat = dlg.GetPanoramicFormat(6);
	var width = dlg.GetInt(7);
	var height = dlg.GetInt(8);
	var sampling = dlg.GetChoice(10);
	var supersample = dlg.GetInt(11);
	
	Alert(sampling);
	Alert(supersample);
	
	// iterate through the file names	
	var processed = 0;
	while(processed < filelist.length)
	{
		var filename = filelist[processed];
		var img = LoadImage(filename);

		img.Rotate90CW(); // or use Rotate90CCW();
		img.FlipHorizontal(); // or use FlipVertical();

		// the optional matrix parameter allows you to apply a custom rotation during resampling (I only include it so you know its there)
		var img2 = img.PanoramicResample(inputformat, outputformat, width, height, sampling, supersample, { matrix:[1, 0, 0, 0, 1, 0, 0, 0, 1] }, {} );
		
		// change the path to the new output directory
		var newname = filename.replace(/^.*\\/, newpath);
		
		img2.Save(newname);
		
		Sleep(100);
		img.Close(1); // force the image to close
		img2.Close(1);
				
		processed++;
	}
	
	Alert("Processed " + processed + "  files ");
	
}
