scaleras.sml

  Download

More scripts: Raster

Syntax Highlighing:

comments, key words, predefined symbols, class members & methods, functions & classes
            
# ScaleRas.sml
#
# SML script to scale cells of raster to desired range
#
# 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. "Scale"			query user and scale raster cells to desired output range - display results
# 3. "Undo"				undo scale
# 4. "Save"				save resulting output raster
# 5. "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:
# CREATION DATE: 	Dec. 7, 1997
# REVISION LIST: 	Revision 1.1	March 10, 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
class RVC_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 keep track of open status of rasters
numeric rInIsOpen = false;
numeric rOutIsOpen = false;
numeric rTempIsOpen = false;
# set up range data values - for 8-bit unsigned only
numeric minData = 0;
numeric maxData = 255;
# 
# START OF SCRIPT
# 
clear(); # clear the console
# open a display window - add standard tools - set up buttons and title
h1 = DispOpen(800, 600, 20, 20);
DispAddStandardTools(h1);
DispAddButtons(h1, "Open", "Scale", "Undo", "Save", "Exit" );
DispSetTitle( h1, "SML Script - Scale Raster To Range" );
# 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 );
				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;
						string 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$ == "Scale") {
		#  scales output range of raster
		if ( rInIsOpen == false ) {
			PopupMessage( "Error - No file open" );
			}
		else {
			numeric outMinimum, outMaximum, outRange;
			# query user for output range desired - restrict values to acceptable range
			outMinimum = PopupNum( "Input minimum output desired", 0, 0, maxData - 1, 0 );
			outMaximum = PopupNum( "Input maximum output desired", maxData, outMinimum + 1, maxData, 0 );
			outRange = outMaximum - outMinimum;
			# determine range of input values
			numeric inMinimum, inMaximum, inRange;
			inMinimum = GlobalMin( Rin );
			inMaximum = GlobalMax( Rin );
			inRange = inMaximum - inMinimum;
 
			# calculate scale and offset factors to map input range to output range
			# ouput = scale * (input - inMinimum) + offset;
			numeric offset = outMinimum;
			numeric scale = ( outMaximum - outMinimum ) / ( inMaximum - inMinimum );
			numLines = NumLins(Rin);
			numColumns = NumCols(Rin);
			status = 0;	# set up to display status bar
			for r = 1 to numLines {
				for c = 1 to numColumns {
					# do the scaling
					Rtemp[r,c + numColumns] = round( scale * ( Rtemp[r,c] - inMinimum ) + offset );
					}
				if ( !int( r % stepSize) ) {
					status = status + 1;
					status$ = left$(gStatusBarLeft$, status) 
								+ left$(gStatusBarRight$, gSizeStatusBar - status);
					message$ = sprintf( "Scaling output: %s", status$ );
					DispSetMessage( h1, message$ );
					}
				}
			redrawScreen( "Scale Image - 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") {
		# 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
					Rtemp[r,c] = Rtemp[r,c + numColumns];	# update left side of temp 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
#