You are here: User's Guide > SPIP Plug-ins > File Importer Plug-in

File Importer Plug-in

A file Importer plug-in enables you to write import filters for file formats not supported by SPIP. Images or curves read by file importer plug-ins will be handled in the same way as files read by SPIP's internal file importers and you will for example be able to browse the file in the ImageMet Explorer using thumbnail view.

To create a Microsoft Visual Studio 2005 C++ File Importer project click

Plug-ins > Programming Tools > Project Creator and select Visual Studio 2005 C++ File Importer as Project Type:


 

When the project has been generated you will be asked if you want to open it and you may find it convenient to do so. Otherwise, you may later open its solution file (e.g., MyFileImporter.sln) in Visual Studio.

The Project is now ready to be build: In Visual Studio click Build > Build Solution and the resulting .dll file will be put into the SPIP\UserDLL\FileImport\ folder where it is automatically found by SPIP next time SPIP is started. Note, that File Importers should always be placed in the SPIP\PLug-ins\FileImport\ folder of the personal Documents directory. When opening the solution using a newer version of Visual Studio you will have the possibility to convert the solution to the newer format.

 

extern "C" _declspec(dllexport) bool ImFileImportFirstPriority( wchar_t* FileName)

{

 // This function attempts to read the file as a SPIP(TM) .asc file

 // The reader has been simplified to increase readability; for example,

 // it does not read void pixel information from the file.

 

 // Handle only files with extension = ".asc"

 if (!wcsstr(FileName, L".asc" ))

  return false;

 

 // Open file for reading

 ifstream File( FileName, ios::binary);

 

 // Read header

 vector<char> Buffer(2000,0);

 File.read( &Buffer[0], Buffer.size()-1);

 

 // Check format and read values

 int Nx, Ny;

 float XRange, YRange, XOffset, YOffset;

 float ZScale;

 vector<char> ZUnit(100,0);

 if ( !strstr(&Buffer[0],"# File Format = ASCII") )

  return false;

 if ( !ReadVal(&Buffer[0],"# x-pixels =","%d",Nx) ||

   !ReadVal(&Buffer[0],"# y-pixels =","%d",Ny) ||

   !ReadVal(&Buffer[0],"# x-length =","%f",XRange) ||

   !ReadVal(&Buffer[0],"# y-length =","%f",YRange) ||

   !ReadVal(&Buffer[0],"# x-offset =","%f",XOffset) ||

   !ReadVal(&Buffer[0],"# y-offset =","%f",YOffset) )

  return false;

 if ( !ReadVal(&Buffer[0],"#bit2nm =","%f",ZScale) )

  ZScale = 1;

 if ( !ReadString(&Buffer[0],"# z-unit =",&ZUnit[0],ZUnit.size()-1) )

  strcpy_s( &ZUnit[0], ZUnit.size(), "nm");

 

 // Find image data start

 char StartOfDataMarker[] = "# Start of Data:";

 char* ImgDataPos = strstr( &Buffer[0], StartOfDataMarker);

 if ( !ImgDataPos )

  return false;

 ImgDataPos += strlen(StartOfDataMarker);

  

 // Create image

 CSpipExchange Img;

 if ( !Img.Create_ImageData(Nx,Ny) )

  return false;

 

 // Set image information

 Img.Put_XRange( XRange);

 Img.Put_YRange( YRange);

 Img.Put_XOffset( XOffset);

 Img.Put_YOffset( YOffset);

 Img.Put_Xunit( "nm");

 Img.Put_Yunit( "nm");

 Img.Put_Zunit( &ZUnit[0]);

 Img.Put_Filename( FileName);

 Img.Put_Desc( "Loaded by FileImporter sample plug-in");

 

 // Read image data

 int ImageDataOffset = ImgDataPos - &Buffer[0];

 File.seekg( ImageDataOffset);

 if ( !File.good() )

  return false;

 for ( int i=0; i<Nx*Ny; ++i)

 {

  File >> Img.Data[i];

  if ( !File.good() )

   return false;  

  Img.Data[i] *= ZScale;

 }

 

 // Send image to SPIP

 Img.Add_ImageDataToInputList("");

 // if the file contains more images (and/or curves) then you may call
// Add_ImageDataToInputList for each image

 return true;

}

 

 

 

 

The important functions to have in mind when constructing a file importer plug-in are:

bool ImFileImport( wchar_t* FileName), which is the function SPIP will be looking for and call in case none of SPIP's own file importers are able to read the file.

bool ImFileImportFirstPriority( wchar_t* FileName) If you want to give your file importer first priority above the internal SPIP file importers you should call it ImFileImportFirstPriority instead of ImFileImport. If the file importer reads the file successfully no other file importers will be called.

This function will add images to SPIP's internal list of images and curves found in the file. SPIP will check this list when the plug-in returns, so that they can be displayed in the SPIP program window or the ImageMet Explorer. Note, that an importer plug-in can this way return more images or curves from the same input file.

of