TiffToJP2getJobParms.sml

  Download

More scripts: Job

Syntax Highlighing:

comments, key words, predefined symbols, class members & methods, functions & classes
            
# TiffToJP2getJobParms.sml
# Provides interface for TIFF to JP2 file conversion.
# Makes job files that call TiffToJP2fromJob.sml, which must be in the
# same directory as this SML script.
# Creates and opens a dialog allowing user to select one or more
# TIFF files, an output directory, and set JPEG2000 compression options.
# The Run Jobs button makes job files that are run immediately,
# The Save Jobs button makes job files that are held in the Job Manager until 
# the user releases them.
# Requires version 2009 of the TNT products.
# Script version 28 April 2009.
# Run Jobs button replaced by Queue Jobs button.
#################  Variables to be written to Job File:  ###############
class STRING outputDir$;			# global variable
# Job File variables declared later as local variables in MakeJobs() procedure:
# string inputPath$;
#	string compType$;
#	numeric compRatio;
#################  Other Global Variables  #########################
class STRINGLIST filepathlist;			# list of filepaths of the TIFF files selected
numeric numfiles; 									# number of TIFF files selected
numeric err;												# value returned for error checking
class GUI_DLG dlgwin;							# the dialog window created by script
numeric ratioEnabled = 1;			# flag for tracking whether compression ratio field has been enabled
# Procedure called by the Select TIFF Files button on the dialog.
# Gets a stringlist with the filepath strings for the selected files, 
# writes the filepath strings to the file listbox on the dialog, and
# enables the Get Directory button
proc GetInputFiles() 
	{
	local class GUI_CTRL_LISTBOX filelistbox;			# handle for file listbox on the dialog
	local numeric i;
	filelistbox = dlgwin.GetCtrlByID("filelistbox");
	numfiles = GetInputFileNames(_context.ScriptDir, "Select TIFF file(s) for conversion to JP2:", "tif", filepathlist);
	printf("numfiles = %d\n", numfiles);
	for i = 1 to numfiles
		{
		filelistbox.AddItem( filepathlist[i-1] );			# add filepath string to the listbox
		}
	dlgwin.GetCtrlByID("dirBtn").SetEnabled(1);		# enable the button to select output directory
	}
# Procedure called by the Select Output Directory button on the dialog
# Gets the output directory path and enables the Run Jobs and Save Jobs buttons
proc GetOutputDirectory()
	{
	outputDir$ = GetDirectory("", "Choose directory for JP2 files:");
	outputDir$ = outputDir$ + "/";
	dlgwin.SetCtrlValueStr("outDirText", outputDir$);		# write directory name to the dialog
	dlgwin.GetCtrlByID("runJobsBtn").SetEnabled(1);			# enable the Run Jobs and Save Jobs buttons
	dlgwin.GetCtrlByID("saveJobsBtn").SetEnabled(1);
	}
# procedure called by the Cancel button on the dialog
proc OnCancel()
	{
	dlgwin.Close(0);
	Exit();
	}
# Procedure called when JPEG2000 compression option is selected.
# Changes the state of associated controls on the dialog
proc OnCompSelected()
	{
	if (dlgwin.GetCtrlByID("jp2compOptions").GetValueStr() == "user")		# user-defined compression is set
		{
		if (ratioEnabled <> 1)		# if ratio label and field are currently disabled, enable them
			{
			dlgwin.GetCtrlByID("ratioLabel").SetEnabled(1);
			dlgwin.GetCtrlByID("compRatioNum").SetEnabled(1);
			ratioEnabled = 1;
			}
		}
	else		#  compression option = lossless or best quality
		{
		if (ratioEnabled == 1)		# if ratio label and field are enabled, disable them
			{
			dlgwin.GetCtrlByID("ratioLabel").SetEnabled(0);
			dlgwin.GetCtrlByID("compRatioNum").SetEnabled(0);
			ratioEnabled = 0;
			}
		}
	}
# Procedure called by the Run Jobs and Save Jobs buttons.
# Makes a job file for each of the input TIFF files to be converted.
# The "hold" parameter value determines whether job is run immediately (0) or held (1)
proc MakeJobs(numeric hold)
	{
	local numeric i;									# loop counter
	local string inputPath$;					# string with the filepath of the current input TIFF file
	local string compType$;				# type of JPEG2000 compression to set
	local numeric compRatio;			# target compression ratio for lossy compression
	local class FILEPATH inFilepath, outFilepath;
	local string description$;				# description of job to be shown in the Job Manager
	# get compression options from the dialog
	compType$ = dlgwin.GetCtrlValueStr("jp2compOptions");
	if (compType$ == "user") then				# get target ratio for user-defined compression
		compRatio = dlgwin.GetCtrlValueNum("compRatioNum");
	else
		compRatio = 0;
	# set path for the SML script to be called by the job files
	local string ScriptPath$; 
	ScriptPath$ = _context.ScriptDir + "/TiffToJP2fromJob.sml";
	local class FILEPATH scriptPath(ScriptPath$);
	for i = 1 to numfiles			# loop through input TIFF files to make job file for each
		{
		local class MIJOB job;		# class for setting up and writing a job file
		inputPath$ = filepathlist[i-1];
		inFilepath = inputPath$;
		# string with the job description to be shown in the Job Manager 
		description$ = sprintf("Convert %s to GeoJP2", inFilepath.GetName() );
		printf("file %d, description= %s\n", i, description$);	 
		# create job file structure with job description and path to the SML script to be called
		job.CreateJob(description$, scriptPath, 2);
		# add variable names and values to the job file structure 
		job.AddValue("inputPath$", inputPath$);
		job.AddValue("outputDir$", outputDir$);
		job.AddValue("compType$", compType$);
		job.AddValue("compRatio", compRatio);
		# write completed job file to the established TNT Jobs directory
		job.Write(hold);											
		}
	OnCancel();			# call procedure to close dialog and exit the script
	}
#########################   Main Program  ###################################
clear();
# dialog specification
class STRING xml$ = '<?xml version="1.0"?>
	<!DOCTYPE root SYSTEM "smlforms.dtd">
	<root>
		<dialog id="dlg" Title="TIFF to JP2 Conversion" Buttons="">
			<pushbutton Name=" Select TIFF Files " OnPressed="GetInputFiles()"/>
			<listbox id="filelistbox" Width="40" />
			<pushbutton id="dirBtn" Name=" Select Output Directory " Enabled="false" OnPressed="GetOutputDirectory()"/>
			<edittext id="outDirText" ReadOnly="true" Width="40"/>
			<pane Orientation="horizontal" HorizResize="Fixed">
				<label>JPEG2000 compression</label>
				<combobox id="jp2compOptions" Default="user" OnSelection="OnCompSelected()">
					<item Name="Lossless" Value="lossless"/>
					<item Name="Best Quality" Value="best"/>
					<item Name="User Defined" Value="user"/>
				</combobox>
				<label id="ratioLabel" Enabled="true">Target ratio</label>
				<editnumber id="compRatioNum" Width="3" Precision="0" Default="15" Enabled="true"/>
			</pane>
			<pane Orientation="horizontal" HorizResize="Fixed" HorizAlign="Right">
				<pushbutton id="runJobsBtn" Name=" Queue Jobs " Enabled="false" OnPressed="MakeJobs(0)"/>
				<pushbutton id="saveJobsBtn" Name=" Save Jobs " Enabled="false" OnPressed="MakeJobs(1)"/>
				<pushbutton Name=" Cancel " OnPressed="OnCancel()"/>
			</pane>
		</dialog>
	</root>';
##############################################
### parse XML text for the dialog into memory; 
### return an error code (number < 0 ) if there are syntax errors
class XMLDOC dlgdoc;
err = dlgdoc.Parse(xml$);
if ( err < 0 ) {
	PopupError( err ); 	# Popup an error dialog. "Details" button shows syntax errors.
	Exit();
	}
################################################
# get the dialog element from the parsed XML document and
# show error message if the dialog element can't be found
class XMLNODE dlgnode;
dlgnode = dlgdoc.GetElementByID("dlg");
if ( dlgnode == 0 ) {
	PopupMessage("Could not find dialog node in XML document");
	Exit();
	}
################################################
# Set the XML dialog element as the source for the GUI_DLG class 
# instance we are using for the dialog window.
err = dlgwin.SetXMLNode(dlgnode);
if ( err < 0 ) {
	PopupError( err ); 	# Popup an error dialog. 
	Exit();
	}
#############################
# open the dialog window
dlgwin.DoModal();