More scripts: Raster
Syntax Highlighing:
comments, key words, predefined symbols, class members & methods, functions & classes
# PipelineFilterLookup.sml
# Sample standalone script to demonstrate a simple image pipeline
# application: applying contrast to a grayscale raster.
# If the input raster does not have a contrast table, the script computes
# a standard contrast table for a copy of the raster, then applies this table.
# A default linear contrast table is also computed for the output raster, which
# has lossless JPEG2000 compression applied.
# 21 December 2007
# Randy Smith, MicroImages, Inc.
# Requires Version 2007:73 or later of the TNT products.
numeric err; # error code returned
class RVC_RASTER RastIn, RastOut; # class instances for input and output rasters
# error checking procedure
proc ReportError(numeric linenum, numeric err) {
printf("FAILED -line: %d, error: %d\n", linenum - 1, err);
PopupError(err);
}
# procedure to compute standard contrast table for copy of input raster that has no contrast table
proc computeCon(class CONTRAST con, class RVC_RASTER Rast) {
class RVC_OBJECT tempFile;
class RVC_RASTER tempRast;
# copy source raster to temporary file, then close source raster
tempFile.MakeTempFile(1);
Rast.CopyTo(tempFile, tempRast);
Rast.Close();
# open temporary raster and compute standard contrast table for it
tempRast.OpenAttached("Write");
con.ComputeStandard(tempRast, "normalize");
Rast = tempRast; # reassign temporary raster to original raster variable
tempRast.Close(); # close temporary raster
}
####################################### Main program ####################################
clear();
# set context so script does not exit on error, allowing manual
# handling of errors (primarily used with CONTRAST class method
# to read a contrast table, which returns an error if no table exists).
_context.AbortOnError = 0;
# CHOOSE INPUT RASTER
GetInputRaster(RastIn);
# check to ensure that selected raster is grayscale
if (RastIn.$Info.Type == "binary" or
RastIn.$Info.Type == "16-bit color RGB" or
RastIn.$Info.Type == "16-bit color BGR" or
RastIn.$Info.Type == "24-bit color RGB" or
RastIn.$Info.Type == "24-bit color BGR")
{
print("Binary and composite rasters cannot be used as input for this script. Exiting now.");
Exit();
}
# get last-used contrast table from the input raster; if none, call procedure to
# make a temporary copy of the raster and compute a standard contrast table for it
class CONTRAST riContrast;
err = riContrast.Read(RastIn);
if ( err < 0 ) {
computeCon(riContrast, RastIn);
print("Input raster has no contrast table, setting up normalized contrast.");
}
else
printf("Input contrast type: %s\n", riContrast.Type);
# CHOOSE OUTPUT RASTER
# DlgGetObject creates only an ObjItem for the output; the new raster will then be created
# by the TARGET_RVC with the desired compression. The ObjectPath of the ObjItem is not
# valid until the output raster is actually created by the pipeline.
class RVC_OBJITEM rastOutObjItem;
DlgGetObject("Choose new raster for output", "Raster", rastOutObjItem, "NewOnly");
# PIPELINE SOURCE
# set input raster as pipeline source using its ObjItem
class IMAGE_PIPELINE_SOURCE_RVC source_rvc( RastIn.GetObjItem() );
err = source_rvc.Initialize();
if ( err < 0)
ReportError(_context.CurrentLineNum, err);
else
print("Source raster initialized.");
# PIPELINE FILTER
# set filter to apply a lookup table read from the raster's contrast table
class IMAGE_PIPELINE_FILTER_LOOKUP filter_lookup (source_rvc);
err = filter_lookup.SetContrastParm(riContrast); # set contrast for filter
if ( err < 0) ReportError(_context.CurrentLineNum, err);
err = filter_lookup.Initialize();
if ( err < 0)
ReportError(_context.CurrentLineNum, err);
else
print("Lookup filter initialized.");
# PIPELINE TARGET
# set output raster as pipeline target using its ObjItem
class IMAGE_PIPELINE_TARGET_RVC target_rvc(filter_lookup, rastOutObjItem);
target_rvc.SetCompression("JP2", 0);
err = target_rvc.Initialize();
if ( err < 0)
ReportError(_context.CurrentLineNum, err);
else
print("Pipeline target initialized.");
# EXECUTE the pipeline process
target_rvc.Process();
# open the output raster to compute and save a default linear contrast table for it
# to prevent it from being "recontrasted" in the Display process.
rastOutObjItem = target_rvc.GetObjItem(); # get the real ObjItem of the raster created by the pipeline
err = RastOut.Open(rastOutObjItem, "Write"); # use the ObjItem to open the raster
if ( err < 0) ReportError(_context.CurrentLineNum, err);
print("Computing contrast table for output raster.");
class CONTRAST roContrast; # compute standard linear contrast table
err = roContrast.ComputeStandard(RastOut, "linear", "LINEAR", "Linear contrast");
if ( err < 0) ReportError(_context.CurrentLineNum, err);
RastOut.Close(); # close the output raster
print("Done.");