home products news downloads documentation support gallery online maps resellers search
TNTmips Downloads Menu

HOME

CONTACT US

CURRENT RELEASE
  TNT 2013

DEVELOPMENT VERSION
  TNT 2014

TNTmips Pro
PRIOR RELEASES
  TNT 2012

FREE SOFTWARE
  TNTmips Free
  TNTatlas
  TNTsdk

MORE DOWNLOADS
  HASP Key Driver
  Screen Recorder
  TNT Language Kits
  Sample Geodata
  TNT Scripts

DOCUMENTATION
  TNTmips Tutorials
  Tutorial Datasets
  Technical Guides
  Scripts
  Quick Guides

MORE INFO
  Download FAQs
  FTP
  Download Managers
  Find Reseller

SITE MAP


gamma.sml


# Gamma.sml
#
# SML script to adjust gamma of raster
#
# 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. "Range"			set range to apply gamma adjustment to
# 3. "Auto Range"		set range to range of input raster
# 4. "Gamma"			adjust gamma of raster and display result
# 5. "Undo"				undo gamma
# 6. "Save"				save resulting output raster
# 7. "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. 5, 1996
# REVISION LIST: 	March 10, 1997, Added to standard display window
#

#
# procedure and function definitions
#

# procedure to update the screen after modifications to data
class DISP h1;
class RVC_RASTER Rtemp;

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;
numeric lowerRange = minData;
numeric upperRange = maxData;

# 
# 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", "Range", "Auto Range", "Gamma", "Undo", "Save", "Exit" );
DispSetTitle( h1, "SML Script - adjust image gamma" );

# set up exitCondition - allows us to break out of while loop
numeric exitCondition = false;

while ( exitCondition == false ) {

	# display message showing current range
	string message$ = sprintf( "%s%3d - %3d%s", "Range:", lowerRange, upperRange, "  Ready for input" );
	DispSetMessage( h1, message$ );

	# wait for button press
	string button$ = DispWaitForButtonPress(h1);
	DispResetButtons(h1);

	if (button$ == "Open") {

		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 = true;

		if ( rInIsOpen ) {

 			#	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
			string status$;
			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" );
			} # end of if ( rInIsOpen )

		}	# end of "Open"

	if (button$ == "Range") {
		# define input range to operate on
		if ( rInIsOpen == false ) {
			PopupMessage( "Error - No file open" );
			}
		else {
			# query user for output range desired - restrict values to acceptable range
			lowerRange = PopupNum( "Input lower range limit", 0, 0, maxData - 1, 0 );
			upperRange = PopupNum( "Input upper range limit", maxData, lowerRange + 1, maxData, 0 );
			}

		}	# end of "Range"

	if (button$ == "Auto Range") {
		# define input range to operate on - default to range of data
		if ( rInIsOpen == false ) {
			PopupMessage( "Error - No file open" );
			}
		else {
			DispSetMessage( h1, "Setting Range to GlobalMin and GlobalMax" );
			lowerRange = GlobalMin(Rin);
			upperRange = GlobalMax(Rin);
			}
		}	# end of "Range"

	if (button$ == "Gamma") {
		# do gamma adjust over defined range
		if ( rInIsOpen == false ) {
			PopupMessage( "Error - No file open" );
			}
		else {
			numeric newGamma = PopupNum("Adjust Gamma of Range - Input Gamma Value", 1.00, .1, 10.0, 2);

			numeric gMax = lowerRange;
			numeric gMin = upperRange;
			numeric gRange = gMax - gMin;
			numeric oneOverGamma = 1 / newGamma;
			numColumns = NumCols(Rin);
			numLines = NumLins(Rin);
			stepSize = int( numLines / 10 );
			numeric inV, inScale, norm, outV;

			status = 0;	# set up to display status bar
			for r = 1 to numLines {
				for c = 1 to numColumns {
					inV = Rin[r,c];
					if ( (inV >= lowerRange) and (inV <= upperRange) ) {
						inScale = ( inV - gMin ) / gRange;
						norm = pow( oneOverGamma, inScale );	# pow(x,y) is y to x power!
						outV = ( inV * ( norm - inScale ) + inV );
						Rtemp[r,c + numColumns] = outV;
						}
					else {
						Rtemp[r,c + numColumns] = inV;
						}
					}
				if ( !int( r % stepSize) ) {
					status = status + 1;
					status$ = left$(gStatusBarLeft$, status) 
								+ left$(gStatusBarRight$, gSizeStatusBar - status);
					message$ = sprintf( "Adjusting Gamma: %s", status$ );
					DispSetMessage( h1, message$ );
					}
				}
			redrawScreen( "Adjusting Gamma - Done" );
			}	 # end of else
		}	# end of "Gamma"

	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 Gamma: %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$ );
					}
				}
			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
# 


Back Home ©MicroImages, Inc. 2013 Published in the United States of America
11th Floor - Sharp Tower, 206 South 13th Street, Lincoln NE 68508-2010   USA
Business & Sales: (402)477-9554  Support: (402)477-9562  Fax: (402)477-9559
Business info@microimages.com  Support support@microimages.com  Web webmaster@microimages.com

25 March 2009

page update: 26 May 11