More scripts: Dialog
Syntax Highlighing:
comments, key words, predefined symbols, class members & methods, functions & classes
# MultRang.sml
#
# SML script to quantize raster cell value to user defined ranges
#
# This script uses a standard display window that shows side by side view of
# the original raster and results of modifications
#
# Buttons:
# 1. "Open" read in raster
# 2. "Define R" define quantizing ranges
# 3. "Save R" save user defined ranges to file
# 4. "Load R" load user defined ranges from file
# 5. "Scale" create and display output raster with cell values quantized to user defined values
# 6. "Undo" undo scale
# 7. "Save Raster" save resulting output raster
# 8. "Exit" exit script
#
# Notes:
# 1. input and output raster must be 8-bit unsigned - this could be modified to work with other types
#
#
# AUTHOR: David Wilson
# MicroImages, Inc.
# REQUESTED BY: Kevin Royal
# CREATION DATE: March 11, 1997
# REVISION LIST: Revision 1.1 March 12, 1997 Added to standard display window
# Revision 1.2 March 17, 1997 Added CreatePyramid() to save file section
# Revision 1.3 Feb 23, 1998 Change class view h1 to class disp h1
#
# procedure and function definitions
#
# must declare any object variables in user defined functions or procedures
raster Rtemp;
class Disp h1;
# procedure to update the screen after modifications to data
proc redrawScreen( string msg$ ) {
CloseRaster(Rtemp); # must close it to flush buffers before displaying it
DispRedraw(h1);
DispSetMessage( h1, msg$ );
}
#
# global data
#
# variables used for showing status of operations
string gStatusBarRight$ = "..........";
string gStatusBarLeft$ = "**********";
numeric gSizeStatusBar = 10;
# variables to store range info
numeric gMaxNumberOfRanges = 20;
numeric gNumRanges = 0;
array numeric gRangeMin[20];
array numeric gRangeMax[20];
# variables to keep track of open status of rasters
numeric rInIsOpen = false;
numeric rOutIsOpen = false;
numeric rTempIsOpen = false;
#
# START OF SCRIPT
#
clear(); # clear the console
# set up range data values - for 8-bit unsigned only
numeric minData = 0;
numeric maxData = 255;
# open a display window - add standard tools - set up buttons and title
h1 = DispOpen(800, 600, 20, 20);
DispAddStandardTools(h1);
DispAddButtons(h1, "Open", "Define R", "Save R", "Load R", "Scale", "Undo", "Save Raster", "Exit" );
DispSetTitle( h1, "SML Script - Scale raster to multiple ranges" );
# set up exitCondition - allows us to break out of while loop
numeric exitCondition = false;
while ( exitCondition == false ) {
# display message showing current range
numeric upperRange, lowerRange;
if ( rInIsOpen ) {
string message$ = sprintf( "Input Range: %3d - %3d Ready for input",
lowerRange, upperRange );
DispSetMessage( h1, message$ );
}
else {
DispSetMessage( h1, "Ready for input" );
}
# wait for button press
string button$ = DispWaitForButtonPress(h1);
DispResetButtons(h1);
if (button$ == "Open") {
if ( rInIsOpen == true ) {
PopupMessage( "Error - File is open" );
}
else {
raster Rin;
GetInputRaster(Rin); # get the input raster
if ( RastType(Rin) <> "8-bit unsigned" ) {
PopupMessage( "This SML script only works with 8 bit unsigned raster types." );
CloseRaster(Rin); # close it
rInIsOpen = false;
}
else {
rInIsOpen = true;
# now create two side by side copies - one for comparison
CreateTempRaster(Rtemp, NumLins(Rin), NumCols(Rin) * 2, RastType(Rin));
rTempIsOpen = true;
numeric numColumns = NumCols(Rin);
numeric numLines = NumLins(Rin);
numeric stepSize = int( numLines / 10 );
string status$;
numeric status = 0; # set up to display status bar
numeric r, c;
for r = 1 to numLines {
for c = 1 to numColumns {
Rtemp[r,c] = Rin[r,c];
Rtemp[r,c + numColumns] = Rin[r,c];
}
if ( !int( r % stepSize) ) { # status bar code
status = status + 1;
status$ = left$(gStatusBarLeft$, status)
+ left$(gStatusBarRight$, gSizeStatusBar - status);
message$ = sprintf( "Making Working Copy of File: %s", status$);
DispSetMessage( h1, message$ );
}
}
# don't use redraw screen - must create layer here
class GRE_LAYER_RASTER layerRtemp;
CloseRaster(Rtemp); # must close it to flush buffers before displaying it
layerRtemp = DispAddRasterVar(h1, Rtemp); # this also opens R1
DispRedrawFull(h1);
DispSetMessage( h1, "Make Working Copy of File: Done" );
lowerRange = GlobalMin( Rin );
upperRange = GlobalMax( Rin );
} # end of else
} # end of else
} # end of "Open"
if (button$ == "Define R") {
# scales output range of raster
if ( rInIsOpen == false ) {
PopupMessage( "Error - No file open" );
}
else {
gNumRanges = PopupNum( "Input number of ranges", 1, 1, gMaxNumberOfRanges, 0 );
numeric i;
numeric defaultLower, defaultUpper, lower, upper;
for i = 1 to gNumRanges {
if ( i == 1 ) {
defaultLower = minData;
defaultUpper = maxData;
lower = minData;
upper = maxData;
}
else {
defaultLower = gRangeMax[i - 1] + 1;
defaultUpper = maxData;
lower = gRangeMax[i - 1] + 1;
upper = maxData;
}
message$ = sprintf( "Input lower limit of range %d", i );
gRangeMin[i] = PopupNum( message$, defaultLower, lower, upper, 0 );
lower = gRangeMin[i];
message$ = sprintf( "Input upper limit of range %d", i );
gRangeMax[i] = PopupNum( message$, defaultUpper, lower, upper, 0 );
} # end of for
} # end of else
} # end of "Define R"
if (button$ == "Save R") {
# write ranges to file
if ( rInIsOpen == false ) {
PopupMessage( "Error - No file open" );
}
else {
class FILE fOutHandle;
string outputFileName$ = PopupString( "Input file name to save ranges: ", "range.txt" );
fOutHandle = fopen( outputFileName$, "w" ); # open for write
# print header text and number of ranges
fprintf( fOutHandle, "%s\n", "SML Script - data ranges" );
fprintf( fOutHandle, "%d\n", gNumRanges );
for i = 1 to gNumRanges {
fprintf( fOutHandle, "%d\n%d\n", gRangeMin[i], gRangeMax[i] );
} # end of for
fclose( fOutHandle ); # close the file
} # end of else
} # end of "Read R"
if (button$ == "Load R") {
# read ranges from file
class FILE fInHandle;
string inputFileName$ = PopupString( "Input file name to save ranges: ", "range.txt" );
fInHandle = fopen( inputFileName$, "r" ); # open for read
# check for valid header
numeric badHeader = false;
string header$ = fgetline$( fInHandle );
if ( header$ <> "SML Script - data ranges" ) {
badHeader = true;
PopupMessage( "Error - Not a valid file" );
}
if ( !badHeader ) {
gNumRanges = fgetnum( fInHandle );
printf( "%s \n", header$ );
printf( "%d \n", gNumRanges );
numeric j;
for j = 1 to gNumRanges {
gRangeMin[j] = fgetnum( fInHandle );
gRangeMax[j] = fgetnum( fInHandle );
printf( "%1d %3d %3d \n", j, gRangeMin[j], gRangeMax[j] );
}
}
fclose( fInHandle ); # close the file
} # end of "Load R"
if (button$ == "Scale") {
# scales output range of raster
if ( rInIsOpen == false ) {
PopupMessage( "Error - No file open" );
}
else {
numLines = NumLins(Rin);
numColumns = NumCols(Rin);
status = 0; # set up to display status bar
numeric foundRange, gMinData;
for r = 1 to numLines {
for c = 1 to numColumns {
foundRange = false;
for j = 1 to gNumRanges {
if ( ( Rtemp[r,c] >= gRangeMin[j] ) and ( Rtemp[r,c] <= gRangeMax[j] ) ) {
Rtemp[r,c + numColumns] = gRangeMax[j];
foundRange = true;
}
}
if ( foundRange == false ) {
# could treat as null cell here
Rtemp[r,c + numColumns] = gMinData;
}
} # end of for c
if ( !int( r % stepSize) ) {
status = status + 1;
status$ = left$(gStatusBarLeft$, status)
+ left$(gStatusBarRight$, gSizeStatusBar - status);
message$ = sprintf( "Scaling output: %s", status$ );
DispSetMessage( h1, message$ );
}
} # end of for r
redrawScreen( "Scale - Done" );
} # end of else
} # end of "Scale"
if (button$ == "Undo") {
# copy left side of temp raster to right side
if ( rInIsOpen == false ) {
PopupMessage( "Error - No file open" );
}
else {
numColumns = NumCols(Rin);
numLines = NumLins(Rin);
stepSize = int( numLines / 10 );
status = 0; # set up to display status bar
for r = 1 to numLines {
for c = 1 to numColumns {
Rtemp[r,c + numColumns] = Rtemp[r,c];
}
if ( !int( r % stepSize) ) {
status = status + 1;
status$ = left$(gStatusBarLeft$, status)
+ left$(gStatusBarRight$, gSizeStatusBar - status);
message$ = sprintf( "Undo: %s", status$ );
DispSetMessage( h1, message$ );
}
}
redrawScreen( "Undo - Done" );
} # end of else
} # end of "Undo"
if (button$ == "Save Raster") {
# copy right side of temp raster to left side
# open dialog for output file to save results to
if ( rInIsOpen == false ) {
PopupMessage( "Error - No file open" );
}
else {
raster Rout;
GetOutputRaster( Rout, NumLins(Rin), NumCols(Rin), RastType(Rin) );
rOutIsOpen = true;
SetScale(Rout, LinScale(Rin), ColScale(Rin));
CopySubobjects(Rin, Rout);
numColumns = NumCols(Rin);
numLines = NumLins(Rin);
stepSize = int( numLines / 10 );
status = 0; # set up to display status bar
for r = 1 to NumLins(Rin) {
for c = 1 to NumCols(Rin) {
Rout[r,c] = Rtemp[r,c + numColumns]; # write new data to output file
}
if ( !int( r % stepSize) ) {
status = status + 1;
status$ = left$(gStatusBarLeft$, status)
+ left$(gStatusBarRight$, gSizeStatusBar - status);
message$ = sprintf( "Saving File: %s", status$ );
DispSetMessage( h1, message$ );
}
}
CreatePyramid(Rout); # Revision 1.2
CloseRaster(Rout); # close the output raster
redrawScreen( "Saving File - Done" );
} # end of else
} # end of "Save"
if (button$ == "Exit") {
exitCondition = true;
}
} # end of main loop
# do clean up
if ( rTempIsOpen ) DeleteTempRaster(Rtemp);
if ( rInIsOpen ) CloseRaster( Rin );
if ( rOutIsOpen ) CloseRaster( Rout );
DispClose(h1);
#
# END OF SCRIPT
#