Author Topic: PCL: ViewToImage or ImageFromView method?  (Read 269 times)

NKV

  • PixInsight Old Hand
  • ****
  • Posts: 561
    • View Profile
PCL: ViewToImage or ImageFromView method?
« on: 2012 February 18 06:38:33 »
Hi,
For example: I want to take image from some View and converted it to UInt16Image.
I can do it via:
Code: [Select]
UInt16Image image; // target

ImageVariant v = view.Window().MainView().Image(); // source

if ( v.IsComplexSample() )throw Error( "Complex images not supported" );

AbstractImage* i = v.AnyImage();

if ( v.IsFloatSample() ) switch ( v.BitsPerSample() )
{
case 32 : image.Assign( *static_cast<Image*>( i ) ); break;
case 64 : image.Assign( *static_cast<DImage*>( i ) ); break;
}
else switch ( v.BitsPerSample() )
{
case  8 : image.Assign( *static_cast<UInt8Image*>( i ) ); break;
case 16 : image.Assign( *static_cast<UInt16Image*>( i ) ); break;
case 32 : image.Assign( *static_cast<UInt32Image*>( i ) ); break;
}

Please answer: Is it best method or there are more easy way?

Best regards,
Nikolay.

Juan Conejero

  • PTeam Member
  • PixInsight Jedi Grand Master
  • ********
  • Posts: 3895
    • View Profile
    • http://pixinsight.com/
Re: PCL: ViewToImage or ImageFromView method?
« Reply #1 on: 2012 February 19 11:15:29 »
Yes, there's a much better way to do this with just four lines of code:

Code: [Select]
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.
« Last Edit: 2012 February 19 11:28:38 by Juan Conejero »
Juan Conejero
PixInsight Development Team
http://pixinsight.com/

NKV

  • PixInsight Old Hand
  • ****
  • Posts: 561
    • View Profile
Re: PCL: ViewToImage or ImageFromView method?
« Reply #2 on: 2012 February 19 14:39:10 »
Thank you!