How to Load and Save Image to .red File?

HOOPS Luminate comes with a handful built-in file format called .red. This file format can be used to save any RED resources and is quite quick at reloading time. This format can be used to save images as well in a lossless compressed way:

Save an Image

// image2d is a pointer to a RED image implementing the RED::IImage2D interface.
// red_file is a pointer to a previously created RED file.
// resmgr is a pointer to the RED resource manager.

RED::IREDFile* ifile = red_file->As< RED::IREDFile >();

// Save the image to a RED file.
RED::StreamingPolicy policy;
RC_TEST( ifile->Write( image2d, policy, resmgr ) );

Load an Image

// red_file is a pointer to a previously created RED file.
// image2d is a pointer to a 2D HOOPS Luminate image implementing the RED::IImage2D interface.

// Get a pointer to the RED resource manager.
RED::Object* resmgr = RED::Factory::CreateInstance( CID_REDResourceManager );
RED::IResourceManager* iresmgr = resmgr->As< RED::IResourceManager >();

// Load the RED file.
RED::Vector< unsigned int > ctx;
RED::IREDFile* ifile = red_file->As< RED::IREDFile >();

RED::StreamingPolicy policy;
RED::FileHeader file_header;
RED::FileInfo file_info;
RC_TEST( ifile->Load( "./my_file.red", iresmgr->GetState(), policy, file_header, file_info, ctx ) );

// Admitting there was only one context loaded from the file containig a single image, we can access
// to our image through the RED::IDataManager interface:
RED::IDataManager* idatamgr = iresmgr->GetDataManager()->As< RED::IDataManager >();
RC_TEST( idatamgr->GetImage( image2d, ctx[0], 0 ) )

Contrary to other common image file formats (JPEG or PNG for example), the native image pixel format is preserved, whatever it is.

Note

You can also load or save images to common file formats like JPEG, PNG or HDR (see How to Load and Save Image to File? for more details).