Yes, there's a much better way to do this with just four lines of code:
ImageVariant v;
v.CreateUIntImage( 16 );
v.CopyImage( view.Window().MainView().Image() );
UInt16Image& image = *static_cast<UInt16Image*>( v.AnyImage() );
Important: Note that, as a result of invoking a CreateXXXImage() member function, ImageVariant owns the image it transports. So you don't have to care about releasing or deallocating the image; it will be destroyed automatically as soon as v gets out of scope.
Important_1: We say that "ImageVariant owns" because this class implements
class ownership. Contrarily to
object ownership, which means that a particular instance of a class owns some data, an image owned by ImageVariant will be destroyed when no ImageVariant instance references it. For example:
ImageVariant f()
{
ImageVariant a;
a.CreateImage();
ImageVariant b( a );
return b;
}
ImageVariant c = f();
In this example, the image created within f() is not destroyed until c gets out of scope. This mechanism is similar to standard data sharing, but there are important differences in this implementation because ImageVariant can transport both
local images (images created by your module) and
shared images created by the PI Core application and accessible from your module.