Example n.4 - Multiple Output
Previous  Top  Next

In this example the aim is to show how generating multiple output files.

//test if image is monochrome  
nBits:=ImgGetBitsPixel(_CurrentImage);  
 
//if it's not monochrome,   
if nBits=8 then  
begin  
   // copying  the whole image  
   BinaryImage:=ImgCopy(_CurrentImage,0,0,0,0);  
     
   //thresholding  
   ImgThreshold(BinaryImage,127);  
     
   //saving  
   NewFileName:=ExtractFilePath(_CurrentOutputFile)+'\Mono'+ExtractFileName(_CurrentOutputFile);  
   tipo:=ExtractFileExt(_CurrentOutputFile);  
   if tipo='.tif' then ImgSaveAsTif(BinaryImage,NewFileName)  
   else if tipo='.jpg' then ImgSaveAsJpg(BinaryImage,NewFileName)  
        else if tipo='.png' then ImgSaveAsPng(BinaryImage,NewFileName)  
         else if tipo='.bmp' then ImgSaveAsBmp(BinaryImage,NewFileName)  
          else ApplicationLog(_CurrentAgent,'Error: '+tipo+' format not supported in output.');  
     
   //deleting  
   ImgDelete(BinaryImage);  
   end;



Looking at the above code, the first thing that we need explain is the use of another image handle called BinaryImage, in fact we generate a copy the current image using ImgCopy; this function needs the handle of the source image (_CurrentImage, in this case) and four other parameters for specifying the rectangle area we want to copy (using four ' 0 ', we'll copy the whole source image).
It's very important to stress that every time we generate (copying, creating, opening from file, etc.) a new image different from that handled by _CurrentImage, after having processed it and, if necessary, saved it, before closing the script we must release system resources used by the image; for this purpose we use ImgDelete. This function mustn't be used with _CurrentImage or with any handle that contains its same value: current image resources are directly manage by the run-time environment, so if we use ImgDelete, we'll generate an error!

In the above script, if image is grayscale, we'll have in output two files: the first one will be the same as the input image; the second one will be the result of a thresholding process of the copy of the input source.
For saving files, we have used specific functions for each kind of image format (tif, jpeg, bmp, png): through ExtractFileExt we identify the format and later we use either ImgSaveAsTif or ImgSaveAsJpg or ImgSaveAsPng or ImgsaveAsBmp. We note one important detail: we needn't to save _CurrentImage, because it is automatically saved by run-time environment!