Syntax Highlighing:
comments, key words, predefined symbols, class members & methods, functions & classes
# TIFF_TO_JP2_MIE_MakeJobs.sml
# Sample script for tutorial Writing Scripts with SML
# Provides interface for TIFF to JP2 file conversion.
# Makes job files that call TIFF_TO_JP2_MIE_FromJob.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.
# Dialog is modeless, allowing selection and processing of multiple sets
# of files in one session.
# The Queue Jobs button makes job files that are immediately added to the Job queue.
# The Save Jobs button makes job files that are held in the Job Manager until
# the user releases them.
# Requires version 2009 or later of the TNT products.
# Script version 2 December 2009.
################# 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
class GUI_CTRL_LISTBOX filelistbox; # handle for file listbox on the dialog
class GUI_CTRL_PUSHBUTTON dirBtn, runJobsBtn, saveJobsBtn; # handles for pushbuttons
numeric ratioEnabled = 1; # flag for tracking whether compression ratio field has been enabled
################# User-Defined Procedures ##########################
# procedure called when the dialog window opens; use to get class handles for controls
proc OnOpen()
{
filelistbox = dlgwin.GetCtrlByID("filelistbox");
dirBtn = dlgwin.GetCtrlByID("dirBtn");
runJobsBtn = dlgwin.GetCtrlByID("runJobsBtn");
saveJobsBtn = dlgwin.GetCtrlByID("saveJobsBtn");
}
# 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 numeric i;
dlgwin.SetCtrlValueStr("msgText", "");
# clear previous input file list if any
if (filepathlist.GetNumItems() > 0 ) then filepathlist.Clear();
if (filelistbox.GetCount() > 0 ) then filelistbox.DeleteAllItems();
# get input GeoTIFF files
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
}
dirBtn.SetEnabled(1); # enable the button to select output directory
# if an output directory has already been selected in a previous run, enable the RunJobs and SaveJobs buttons
if (outputDir$ <> "")
{
runJobsBtn.SetEnabled(1);
saveJobsBtn.SetEnabled(1);
}
}
# 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:");
if (outputDir$ == "") then
PopupMessage("No directory selected. Please choose an output directory.");
else {
outputDir$ = outputDir$ + "/";
dlgwin.SetCtrlValueStr("outDirText", outputDir$); # write directory name to the dialog
runJobsBtn.SetEnabled(1); # enable the Run Jobs and Save Jobs buttons
saveJobsBtn.SetEnabled(1);
}
}
# procedure called by the Cancel button on the dialog
proc OnClose()
{
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() == "LossyRatio") # 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;
dlgwin.SetCtrlValueStr("msgText", sprintf("Processing file %d of %d", i, numfiles));
# set path for the SML script to be called by the job files
local string ScriptPath$;
ScriptPath$ = _context.ScriptDir + "/TIFF_TO_JP2_MIE_FromJob.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);
}
dlgwin.SetCtrlValueStr("msgText", "Done.");
}
######################### 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="" OnOpen="OnOpen()">
<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="LossyRatio" OnSelection="OnCompSelected()">
<item Name="Lossless" Value="Lossless"/>
<item Name="Best Quality" Value="LossyBest"/>
<item Name="User Defined" Value="LossyRatio"/>
</combobox>
<label id="ratioLabel" Enabled="true">Target ratio</label>
<editnumber id="compRatioNum" Width="3" Precision="0" Default="15" Enabled="true"/>
</pane>
<edittext id="msgText" Width="40" ReadOnly="true"/>
<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=" Close " OnPressed="OnClose()"/>
</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.CreateModeless();
dlgwin.Open();
WaitForExit();