FiltersPreview.jsx

Summary

The FiltersPreview.jsx file will contain the functions for FiltersPreview.mxml, an Adobe Flex Project, for the Filters Preview Panel. It will contain functions to execute the proper filter and also utility functions to check if the layer is a smart object and also determine which layer is the correct filter.

Author: John Huan Vu, Photoshop Engineering Intern, Adobe Systems Incorporated


Method Summary
static void changeGaussianFilter(<String> params)
           The function first converts the parameter into a floating values and apply the Gaussian Blur filter to the smart object.
static void changeUnsharpFilter(<String> params)
           The function first splits the parameters into separate values and apply the unsharp mask filter to the smart object.
static void changeZigZagFilter(<String> params)
           The function first splits the parameters into separate values and apply the zig zag filter to the smart object.
static void checkForSmartObject()
           Checks if the active layer is a smart object.
static Array getFilterLayerIndex(<String> fourCharID)
           The function determines all the layers that have the same four character ID of the filter being searched.
static String styleToCharID(<Integer> style)
           The function determines the which style of zig zag to apply

/**************************************************************************
	ADOBE SYSTEMS INCORPORATED
	 Copyright 2008 Adobe Systems Incorporated
	 All Rights Reserved.

	NOTICE:  Adobe permits you to use, modify, and distribute this file
	in accordance with the terms of the Adobe license agreement accompanying
	it.  If you have received this file from a source other than Adobe, then
	your use, modification, or distribution of it requires the prior written
	permission of Adobe.
**************************************************************************/

/**  Name: FiltersPreview.jsx
	@author John Huan Vu, Photoshop Engineering Intern, Adobe Systems Incorporated
	@fileoverview The FiltersPreview.jsx file will contain the functions
			for FiltersPreview.mxml, an Adobe Flex Project, for the
			Filters Preview Panel. It will contain functions to
			execute the proper filter and also utility functions to
			check if the layer is a smart object and also determine
			which layer is the correct filter.
*/

/**
	Checks if the active layer is a smart object. If not, convert the
			layer to a smart object to be used by non-destructive filters.
*/
function checkForSmartObject(){
	if (app.activeDocument.activeLayer.kind != LayerKind.SMARTOBJECT){
		try{
			executeAction(stringIDToTypeID("newPlacedLayer"), undefined, DialogModes.NO);
		} catch(e){
			alert("Problems converting the active layer to a smart layer. " + e);
		}
	}
}

/**
	The function first converts the parameter into a floating
			values and apply the Gaussian Blur filter to the smart object.
	@param {String} params The parameters used for the Gaussian Blur filter
*/
function changeGaussianFilter(params){
	if (app.documents.length != 0){
		var radius = parseFloat(params);
		
		checkForSmartObject();
		
		var gaussianLayerIndexes = getFilterLayerIndex("GsnB");
		
		if(gaussianLayerIndexes.length > 0){
			var idsetd = charIDToTypeID( "setd" );
			var desc8 = new ActionDescriptor();
			var idnull = charIDToTypeID( "null" );
			var ref1 = new ActionReference();
			var idfilterFX = stringIDToTypeID( "filterFX" );
			ref1.putIndex( idfilterFX, gaussianLayerIndexes[0] + 1 );
			var idLyr = charIDToTypeID( "Lyr " );
			var idOrdn = charIDToTypeID( "Ordn" );
			var idTrgt = charIDToTypeID( "Trgt" );
			ref1.putEnumerated( idLyr, idOrdn, idTrgt );
			desc8.putReference( idnull, ref1 );
			var idfilterFX = stringIDToTypeID( "filterFX" );
			var desc9 = new ActionDescriptor();
			var idFltr = charIDToTypeID( "Fltr" );
			var desc10 = new ActionDescriptor();
			var idRds = charIDToTypeID( "Rds " );
			var idPxl = charIDToTypeID( "#Pxl" );
			
			// Apply the radius parameter
			desc10.putUnitDouble( idRds, idPxl, radius);
			var idGsnB = charIDToTypeID( "GsnB" );
			desc9.putObject( idFltr, idGsnB, desc10 );
			var idfilterFX = stringIDToTypeID( "filterFX" );
			desc8.putObject( idfilterFX, idfilterFX, desc9 );
			executeAction( idsetd, desc8, DialogModes.NO );
		} else {
			app.activeDocument.activeLayer.applyGaussianBlur(radius);
		}
	}
}

/**
	The function first splits the parameters into separate
			values and apply the unsharp mask filter to the smart object.
	@param {String} params The parameters used for the Unsharp Mask filter
*/
function changeUnsharpFilter(params){
	if (app.documents.length != 0){
		var paramsTokens = params.split(",");
		amt = parseInt(paramsTokens[0]);
		radius = parseFloat(paramsTokens[1]);
		threshold = parseInt(paramsTokens[2]);
		
		checkForSmartObject();
		
		var unsharpLayerIndexes = getFilterLayerIndex("UnsM");
		
		if(unsharpLayerIndexes.length > 0){
			var idsetd = charIDToTypeID( "setd" );
			var desc13 = new ActionDescriptor();
			var idnull = charIDToTypeID( "null" );
			var ref3 = new ActionReference();
			var idfilterFX = stringIDToTypeID( "filterFX" );
			ref3.putIndex( idfilterFX, unsharpLayerIndexes[0] + 1 );
			var idLyr = charIDToTypeID( "Lyr " );
			var idOrdn = charIDToTypeID( "Ordn" );
			var idTrgt = charIDToTypeID( "Trgt" );
			ref3.putEnumerated( idLyr, idOrdn, idTrgt );
			desc13.putReference( idnull, ref3 );
			var idfilterFX = stringIDToTypeID( "filterFX" );
			var desc14 = new ActionDescriptor();
			var idFltr = charIDToTypeID( "Fltr" );
			var desc15 = new ActionDescriptor();
			var idAmnt = charIDToTypeID( "Amnt" );
			var idPrc = charIDToTypeID( "#Prc" );
			
			// Apply the amt parameter
			desc15.putUnitDouble( idAmnt, idPrc, amt );
			var idRds = charIDToTypeID( "Rds " );
			var idPxl = charIDToTypeID( "#Pxl" );
			
			// Apply the radius parameter
			desc15.putUnitDouble( idRds, idPxl, radius );
			var idThsh = charIDToTypeID( "Thsh" );
			
			// Apply the threshold parameter
			desc15.putInteger( idThsh, threshold );
			var idUnsM = charIDToTypeID( "UnsM" );
			desc14.putObject( idFltr, idUnsM, desc15 );
			var idfilterFX = stringIDToTypeID( "filterFX" );
			desc13.putObject( idfilterFX, idfilterFX, desc14 );
			executeAction( idsetd, desc13, DialogModes.NO );
		} else {
			app.activeDocument.activeLayer.applyUnSharpMask(amt,radius,threshold);
		}
	}
}

/**
	The function first splits the parameters into separate
			values and apply the zig zag filter to the smart object.
	@param {String} params The parameters used for the Zig Zag filter
*/
function changeZigZagFilter(params){
	if (app.documents.length != 0){
		var paramsTokens = params.split(",");
		amt = parseInt(paramsTokens[0]);
		ridges = parseInt(paramsTokens[1]);
		style = parseInt(paramsTokens[2]);

		checkForSmartObject();
		
		var zigZagLayerIndexes = getFilterLayerIndex("ZgZg");
		
		if(zigZagLayerIndexes.length > 0){
			var idsetd = charIDToTypeID( "setd" );
			var desc13 = new ActionDescriptor();
			var idnull = charIDToTypeID( "null" );
			var ref5 = new ActionReference();
			var idfilterFX = stringIDToTypeID( "filterFX" );
			ref5.putIndex( idfilterFX, zigZagLayerIndexes[0] + 1 );
			var idLyr = charIDToTypeID( "Lyr " );
			var idOrdn = charIDToTypeID( "Ordn" );
			var idTrgt = charIDToTypeID( "Trgt" );
			ref5.putEnumerated( idLyr, idOrdn, idTrgt );
			desc13.putReference( idnull, ref5 );
			var idfilterFX = stringIDToTypeID( "filterFX" );
			var desc14 = new ActionDescriptor();
			var idFltr = charIDToTypeID( "Fltr" );
			var desc15 = new ActionDescriptor();
			var idAmnt = charIDToTypeID( "Amnt" );
			
			// Apply the amt parameter
			desc15.putInteger( idAmnt, amt );
			var idNmbR = charIDToTypeID( "NmbR" );
			
			// Apply the ridges parameter
			desc15.putInteger( idNmbR, ridges );
			var idZZTy = charIDToTypeID( "ZZTy" );
			var idZZTy = charIDToTypeID( "ZZTy" );
			
			// Apply the style parameter
			var idOtFr = charIDToTypeID( styleToCharID(style));
			desc15.putEnumerated( idZZTy, idZZTy, idOtFr );
			var idZgZg = charIDToTypeID( "ZgZg" );
			desc14.putObject( idFltr, idZgZg, desc15 );
			var idfilterFX = stringIDToTypeID( "filterFX" );
			desc13.putObject( idfilterFX, idfilterFX, desc14 );
			executeAction( idsetd, desc13, DialogModes.NO);
		} else {
			if(style == 0){
				app.activeDocument.activeLayer.applyZigZag(amt,ridges,ZigZagType.AROUNDCENTER);
			} else if(style == 1){
				app.activeDocument.activeLayer.applyZigZag(amt,ridges,ZigZagType.OUTFROMCENTER);
			} else {
				app.activeDocument.activeLayer.applyZigZag(amt,ridges,ZigZagType.PONDRIPPLES);
			}
		}
	}
}

/**
	The function determines the which style of zig zag to apply
	@param {Integer} style The style of zig zag filter to apply
	@returns Four character ID of the style the zig zag filter will apply
	@type String
*/
function styleToCharID(style){
	if(style == 0) return "ArnC";
	else if(style == 1) return "OtFr";
	else return "PndR";
}

/**
	The function determines all the layers that have the same
			four character ID of the filter being searched.
	@param {String} fourCharID The four character ID of the filter
	@returns An array of the indexes of where the layer is located
	@type Array
*/
function getFilterLayerIndex(fourCharID) {
	var filterIndexes = new Array();
	
	var filterRuntimeID = charIDToTypeID( fourCharID );
	var smartObjectID = stringIDToTypeID( "smartObject" );
	var filterFXID = stringIDToTypeID( "filterFX" );
	var fltrID = charIDToTypeID( 'Fltr' );
	
	var ref = new ActionReference();
	ref.putProperty(charIDToTypeID('Prpr'), smartObjectID);
	ref.putEnumerated(charIDToTypeID('Lyr '), charIDToTypeID('Ordn'), charIDToTypeID('Trgt'));

	var resultDesc = executeActionGet( ref );
	if (resultDesc.hasKey(smartObjectID)){
		var smartDesc = resultDesc.getObjectValue(smartObjectID);
		
		if (smartDesc.hasKey(filterFXID)) {
			var resultList = smartDesc.getList(filterFXID);
			
			for (var i=0; i < resultList.count; i++ ) {
				var filterFXDesc = resultList.getObjectValue( i );
				if ( filterFXDesc.hasKey( fltrID ) ) {
					var filterObjID = filterFXDesc.getObjectType( fltrID );
					if ( filterObjID == filterRuntimeID ) {
						filterIndexes.push( i );
					}
				}
			}
		}
	}	
	return filterIndexes;
}


Documentation generated by JSDoc on Wed Sep 3 16:09:11 2008