// Script Name: autocrop.js
// Version 1.0b 9 December 2003
// Copyright (c) Paul Jaruszewski and Roger Cavanagh. All rights reserved.
// www.melor.com

// This script is provided free for personal use.
// No liability will be accepted for any consequences arising from the use of this script.
// This script may be copied IN ITS ENTIRETY to others provided and no charge is made other
// than for media. 

// Description: crops the active document after image rotation

// Installation: copy the file autocrop.js to the ..\Presets|Scripts folder

// ===================================================================================================
// Main script starts here
// ===================================================================================================

if (documents.length ==0 ) { // Issue warning if no open image
  alert("No open images!");
} else { // Proceed with main script

// Save preferences

	var	originalUnits = preferences.rulerUnits;
	if (originalUnits != Units.PIXELS) {
  	preferences.rulerUnits = Units.PIXELS;
	}

// Declarations

	var docRef = activeDocument;

// Main

	doCrop();

// Reset Preferences

	preferences.rulerUnits = originalUnits;
	purge(PurgeTarget.ALLCACHES);

} // end of if documents

// ===================================================================================================
function doCrop() {
// ===================================================================================================

// Declare and Initialise

	var picrgb = new Array(3);
	var backrgb = new Array(3);
	var bounds = new Array(4);
	var j = 0;
	var jInc = 10;
	var maxi = docRef.width;
	if (maxi > docRef.height) {
  	maxi = docRef.height;
	}
	
	backrgb=eyeDropper(0,0); //Find color of upper left pixel
	j = 1;
loopOne:	
	while (j < maxi) {
    picrgb=eyeDropper(j,j);
  	if (backrgb[0] != picrgb[0] || backrgb[1] != picrgb[1] || backrgb[2] != picrgb[2]) {
  		if (jInc == 1) {
      	bounds = [j, j, docRef.width - j, docRef.height - j];
      	docRef.crop(bounds);
      	bounds = null;
      	break loopOne;
      } else {
      	j = j - jInc;
		if (j < 0) {
      		j = 0;
      	}
      	jInc = 1 ;
      }
    }
		purge(PurgeTarget.ALLCACHES);
		j = j + jInc;
	} // end while
	
	maxi = docRef.width;
	if (maxi > docRef.height) {
  	maxi = docRef.height;
	}
	jInc = 5;
	j = 1;
loopTwo:	
	while (j < maxi) {
    picrgb=eyeDropper(docRef.width - j,j);
  	if (backrgb[0] != picrgb[0] || backrgb[1] != picrgb[1] || backrgb[2] != picrgb[2]) {
  		if (jInc == 1) {
				bounds = [j, j, docRef.width - j, docRef.height - j];
	    	docRef.crop(bounds);
  	  	bounds = null;
      	break loopTwo;
      } else {
      	j = j - jInc;
      	if (j < 0) {
      		j = 0;
      	}
      	jInc = 1;
      }
    }
		purge(PurgeTarget.ALLCACHES);
		j = j + jInc;
	} // end while
} // end function doCrop

// ===================================================================================================
// This function returns the three bytes of the RGB color of any given pixel
// ===================================================================================================

function eyeDropper(x,y) { 
    var x2 = x + 1; 
    var y2 = y + 1; 
    var out = new Array(3);
    docRef.selection.select([[x,y], [x2,y], [x2,y2], [x, y2]], SelectionType.REPLACE, 0, false); 
    for(ch in list = ["Red", "Green", "Blue"]) { 
        histogram = docRef.channels[list[ch]].histogram; 
        
        for (i = 0; i <= 255; i++) { 
            if (histogram[i]) {
            	out[ch] = i; 
            	break; 
            }
        } // end for
        
    } // end for
    return out; 
} // end function

