
//
// Copyright ©2001 Cisco Systems Inc. All rights reserved.
//
// File:    cip.js
// Author:  DAD
// Purpose: Uses the CipImage ActiveX Library to convert images to and from the supported types.
//          
//


try
	{
	var fn1; 
	var fn2;
	var fn1e;
	var fn2e;
	var tempArray = new Array();

	var cip = new ActiveXObject("CIPIMAGE.ImageProcessor"); // Load the CipImage object
  var fso = new ActiveXObject("Scripting.FileSystemObject"); // File System object comes in handy for validation

	if (WScript.arguments.length == 2) // Do I have a From and a To file name?
		{
		fn1 = WScript.arguments(0); // From File
		fn2 = WScript.arguments(1); // To File

		if (!fso.FileExists(fn1))
			{
			throw {number:1, description:"Source File Not Found: " + fn1 + "\r\n"}; // Something was wrong with the file name - Throw an error
			}
		
		tempArray = String(fn1).split("."); // Split the name into an Array using the "." as a delimeter
		fn1e = String(tempArray[tempArray.length - 1]).toUpperCase(); // The last element should have the file extension
		tempArray = String(fn2).split(".");
		fn2e = String(tempArray[tempArray.length - 1]).toUpperCase();		
		
		switch (fn1e) // Load based on the file extension
			{
			case "CIP":
				cip.LoadCIP(fn1);
				break;
			case "GIF":
				cip.LoadGIF(fn1);
				break;
			case "JPG":
			case "JPEG":
				cip.LoadJPG(fn1);
				break;
			case "PNG":
				cip.LoadPNG(fn1);
				break;
			case "BMP":
				cip.LoadBMP(fn1);
				break;
			default:
				throw {number:2, description:"Invalid file extention: " + fnel + "\r\n"}; // Something was wrong with the file name - Throw an error
				break;
			}

		if (cip.colorCount != 0) // If there are colors it is using RGB and not a palette. Convert it incase you go to a non-RGB capable format.
			{
			cip.RGBToPalette();
			}

		switch (fn2e) // Save based on the file extension
			{
			case "CIP":
				cip.SaveCIP(fn2);
				break;
			case "GIF":
				cip.SaveGIF(fn2);
				break;
			case "JPG":
			case "JPEG":
				cip.SaveJPG(fn2);
				break;
			case "PNG":
				cip.SavePNG(fn2);
				break;
			case "BMP":
				cip.SaveBMP(fn2);
				break;
			default:
				throw {number:2, description:"Invalid file extention: " + fne2 + "\r\n"};
				break;
			}
		WScript.echo ("\r\nSuccessfully converted " + fn1 + " to " + fn2 + "\r\n");
		}
	else
		{
		WScript.echo ("\r\nYou must specify a Source and a Destination file name.\r\nExample - \"cip test.gif test.cip\"\r\n");
		}
	}
catch	(err)
	{
	WScript.echo ("\r\nAn error occurred.\r\n\r\nError number: " + err.number.toString(16) + "\r\nError description: " + err.description);
	}
