# lincombo.sml

#### Syntax Highlighing:

```comments, key words, predefined symbols, class members & methods, functions & classes
```
```# lincombo.sml
# Creates Raster objects that are linear combinations of any
# number of input Raster objects.
# creating and filling in the transform matrix
#
# the matrix size is determined by the number
# of input and output rasters:
# number of columns = number of input rasters + 1
# number of rows = number of output rasters
# column 1 has offset coefficients
# columns 2 to (N + 1) hold the coeffients for the N output rasters
# in general if:
# N = number of output rasters, M = number of input rasters
#
# outRasterN[r,c] =   transformMatrix[N-1,0]
#                   + transformMatrix[N-1,1] * inRaster1[r,c]
#                   + transformMatrix[N-1,2] * inRaster2[r,c]
#                   + ...
#                   + transformMatrix[N-1,M] * inRasterM[r,c]
#
# our matrix will be:
# | 0.0  .213  .715  .072 |  - coeff. for the first raster
# | 0.0  .333  .333  .333 |  - coeff. for the second raster
#
# our output rasters are created using these formulas:
#
# Lcombo1[r,c] = 0.0 + (.213 * Rred[r,c])
# 	                  + (.715 * Rgreen[r,c])
#                    + (.072 + Rblue[r,c])
#
# Lcombo2[r,c] = 0.0 + (.333 * Rred[r,c])
# 	                  + (.333 * Rgreen[r,c])
#                    + (.333 + Rblue[r,c])
#
# Compute brightness Raster from rgb rasters
# the coefficents are only approximate, correct for NTSC(1953)
raster Rred, Rgreen, Rblue;
class MATRIX transform;
# set up variables to hold values for flags
numeric flagNoAutoscale = 0;
numeric flagAutoscale = 1;
GetInputRaster(Rred);
GetInputRaster(Rgreen);
GetInputRaster(Rblue);
numeric numInputRasters = 3;
numeric numOutputRasters = 2;
transform = CreateMatrix(numOutputRasters, numInputRasters + 1);
# output Raster one
# set matrix items (coefficients) equal to correct
# red, green, and blue contributions to brightness
SetMatrixItem(transform, 0, 0, 0.0);  # sets offset to zero
SetMatrixItem(transform, 0, 1, .212); # red
SetMatrixItem(transform, 0, 2, .715); # green
SetMatrixItem(transform, 0, 3, .072); # blue
# output Raster two
# other method to compute brightness - not as good
# set coefficients for average of red, green, and blue
SetMatrixItem(transform, 1, 0, 0.0);  # sets offset to zero
SetMatrixItem(transform, 1, 1, .333); # red
SetMatrixItem(transform, 1, 2, .333); # green
SetMatrixItem(transform, 1, 3, .333); # blue
# get output RVC file name
string outRVCFile\$ = GetOutputFileName("", "Select output RVC file", "rvc");
numeric flags = 0; # no auto scale
RasterLinearCombination(transform, outRVCFile\$, "Lcombo",
"Linear Combinations Test :", flags, Rred, Rgreen, Rblue);
DestroyMatrix(transform);
CloseRaster(Rred);
CloseRaster(Rgreen);
CloseRaster(Rblue);```