David,
I am glad to answer these little but important things. As long as I can answer in a reasonable amount of time, I'll try to answer as quick and extensively as possible. Otherwise I'll delay a bit, but anyway please don't stop asking! ;^)
Masks are always image windows in PixInsight. This is because every masking relation acts exclusively from a mask window to a masked window. So you can't use any Image object as a mask for an existing ImageWindow. You have to create an ImageWindow object, put your image inside it, then activate that ImageWindow as a mask. Here's a little code snippet:
// Get access to the active image window (or you can find
// any window with ImageWindow.windowById()).
var window = ImageWindow.activeWindow;
// Always handle situations where there are no active image window.
if ( window.isNull )
throw Error( "I want a window to play..." );
// Get access to the active image.
var image = window.mainView.image;
// Our mask image must have the same dimensions as the target image.
// However, it can be a grayscale image of any sample type.
var maskImage = new Image( image.width, image.height, 1,
ColorSpace_Gray, 16, SampleType_Integer );
// ... Do something with your mask image here ...
// Create a one-pixel grayscale image (1x1x1).
var maskWindow = new ImageWindow( 1, 1, 1, 16, false, false, "FooMask" );
// The main view of our mask window.
var maskView = maskWindow.mainView;
// Inform the core application that we are going to modify this view's
// image. UndoFlag_NoSwapFile avoids generation of a swap file. Since we
// are working with a new image, a swap file is not needed because we
// don't want to undo anything for this window at this point.
maskView.beginProcess( UndoFlag_NoSwapFile );
// Transfer the mask image.
// ### Important: Note that after this transfer, maskImage is no longer
// a reference to a valid Image object.
maskView.image.transfer( maskImage );
// Inform the core application that we have finished processing the view.
maskView.endProcess();
// Show the mask window, or your users will hate you...
maskWindow.show();
// Now activate the mask for the active window.
window.mask = maskWindow;
Hope this clarifies.