Setting XISF compression options from script

themissingcow

New member
Nov 3, 2020
4
0
Hey folks,

Sorry, a somewhat basic Q. Does anyone know of an example of saving a view to an xisf file, with LZ4-HC compression from JavaScript?

Many thanks in advance,
Tom
 

Juan Conejero

PixInsight Staff
Sep 2, 2004
8,056
302
57
Valencia, Spain
pixinsight.com
You have to use format hints to do this. The following simple function (just an example, not verified) allows you to write an image with format hints:

JavaScript:
function writeImage( filePath, image, outputHints )
{
   if ( outputHints === undefined )
      outputHints = "";

   let fileSuffix = File.extractSuffix( filePath );
   let outputFormat = new FileFormat( fileSuffix, false/*toRead*/, true/*toWrite*/ );
   if ( outputFormat.isNull )
      throw new Error( "No installed file format can write \'" + fileSuffix + "\' files." );

   let outputFile = new FileFormatInstance( outputFormat );
   if ( outputFile.isNull )
      throw new Error( "Unable to instantiate file format: " + outputFormat.name );

   if ( !outputFile.create( filePath, outputHints ) )
      throw new Error( "Error creating output file: " + filePath );

   let d = new ImageDescription;
   d.bitsPerSample = 32;
   d.ieeefpSampleFormat = true;
   if ( !outputFile.setOptions( d ) )
      throw new Error( "Unable to set output file options: " + outputFilePath );

   if ( !outputFile.writeImage( image ) )
      throw new Error( "Error writing output file: " + outputFilePath );

   outputFile.close();
}
So if you call it as follows:

writeImage( "/path/to/foo.xisf", myImage, "compression-codec lz4hc+sh" );

it will pass the "compression-codec lz4hc+sh" hint to the XISF format support module. The above example is an *extremely* simplified function to demonstrate the basic concept. In the real world you'll need to take care of metadata such as XISF properties and FITS keywords, as well as ancillary data such as a ICC color profiles, thumbnails, etc.

For a more complete example, see the source code of the BatchFormatConversion script. For complete information on format hints, use the Format Explorer window: select a file format and double click the "implementation" item on the right panel. Let me know if this helps.
 
  • Like
Reactions: themissingcow