// Copyright 2005.  Reindeer Graphics, Inc.  All rights reserved.
// Round up image size to next square power-of-two

if (app.documents.length == 0)
{
  alert ("There are no documents");
}
else
{
  	var docRef = app.activeDocument;					//current document
	var PowerOfTwo = -1;
	var startRulerUnits = preferences.rulerUnits;
	var startTypeUnits = preferences.typeUnits;
	//switch to pixels to get dimensions
	preferences.rulerUnits = Units.PIXELS;
	preferences.typeUnits = TypeUnits.PIXELS;
	//find the maximum dimension
	var w = docRef.width;
	var h = docRef.height;
	//switch units back to user's type
	preferences.rulerUnits = startRulerUnits;
	preferences.typeUnits = startTypeUnits;
	var max = w;
	if (h > w) max = h;
	
	if (max < 64) PowerOfTwo = 64;
	else if (max < 128) PowerOfTwo = 128;
	else if (max < 256) PowerOfTwo = 256;
	else if (max < 512) PowerOfTwo = 512;
	else if (max < 1024) PowerOfTwo = 1024;
	else if (max < 2048) PowerOfTwo = 2048;
	else if (max < 4096) PowerOfTwo = 4096;
	else if (max < 8192) PowerOfTwo = 8192;
	else if (max < 16384) PowerOfTwo = 16384;
	else
	{
 		alert ("The largest supported size for FFTs is 16384x16384");
 		PowerOfTwo = -1;
	}

	if (PowerOfTwo > 0)
	{
		//resize to PowerOfTwo x PowerOfTwo
		var id539 = charIDToTypeID( "CnvS" );
	    var desc90 = new ActionDescriptor();
	    var id540 = charIDToTypeID( "Wdth" );
	    var id541 = charIDToTypeID( "#Pxl" );
	    desc90.putUnitDouble( id540, id541, PowerOfTwo );
	    var id542 = charIDToTypeID( "Hght" );
	    var id543 = charIDToTypeID( "#Pxl" );
	    desc90.putUnitDouble( id542, id543, PowerOfTwo );
	    var id544 = charIDToTypeID( "Hrzn" );
	    var id545 = charIDToTypeID( "HrzL" );
	    var id546 = charIDToTypeID( "Left" );
	    desc90.putEnumerated( id544, id545, id546 );
	    var id547 = charIDToTypeID( "Vrtc" );
	    var id548 = charIDToTypeID( "VrtL" );
	    var id549 = charIDToTypeID( "Top " );
	    desc90.putEnumerated( id547, id548, id549 );
	    var id550 = stringIDToTypeID( "canvasExtensionColorType" );
	    var id551 = stringIDToTypeID( "canvasExtensionColorType" );
	    var id552 = charIDToTypeID( "Gry " );
	    desc90.putEnumerated( id550, id551, id552 );
		executeAction( id539, desc90, DialogModes.NO );
	}
}
