LOptimize::OptimizeDir

#include "ltwrappr.h"

L_INT LOptimize::OptimizeDir(pszOrgDirPath, pszOptDirPath, pOptImgOptions, pszFilesExt, bIncludeSubDirs)

L_TCHAR * pszOrgDirPath;

/* full path directory to optimize */

L_TCHAR * pszOptDirPath;

/* full path directory to save optimized images */

pOPTIMIZEIMAGEOPTIONS pOptImgOptions;

/* pointer to the OPTIMIZEIMAGEOPTIONS structure */

L_TCHAR * pszFilesExt;

/* files extension(s) to optimize */

L_BOOL bIncludeSubDirs;

/* flag that indicates whether to include sub-directories */

Optimizes a directory of images using the specified optimization options, and saves the optimized images to a new directory with the same hierarchy.

Parameter

Description

pszOrgDirPath

Character string that contains the full path directory of the supported images to optimize.

pszOptDirPath

Character string that contains the full path directory to save the optimized image(s).

pOptImgOptions

Pointer to an OPTIMIZEIMAGEOPTIONS structure that contains the options used in optimization. Pass NULL to use the default optimization options.

pszFilesExt

Pointer to a character string that contains the extensions of the files to optimize. This is a NULL terminated string and can have a maximum length of MAX_PATH. For example:

To optimize all "gif" and "jpeg" files, pszFilesExt should be set to "*.gif;*.jpeg".

To optimize all the supported files found in the pszOrgDirPath directory, regardless of the their extensions, pszFilesExt should be set to "*.*".

bIncludeSubDirs

Flag that indicates whether to include sub-directories when optimizing. Possible values are:

 

Value

Meaning

 

TRUE

Include sub-directories when optimizing.

 

FALSE

Exclude sub-directories when optimizing.

Returns

SUCCESS

The function was successful.

< 1

An error occurred. Refer to Return Codes.

Comments

This function looks for all the selected and supported files in the pszOrgDirPath directory and optimizes them using the optimization options passed in the pOptImgOptions parameter. It then saves the optimized images to the pszOptDirPath directory.

Both the pszOrgDirPath and pszOptDirPath parameters should contain a full path, complete with drive name. The pszOrgDirPath parameter should contain the full path to the input (original) directory, and the pszOptDirPath parameter should contain the full path to the output (optimized) directory.

This function does not support 32-bit grayscale images. It returns the error code ERROR_GRAY32_UNSUPPORTED if a 32-bit grayscale image is passed to this function.

Required DLLs and Libraries

LTIMGOPT

For a listing of the exact DLLs and Libraries needed, based on the toolkit version, refer to Files To Be Included With Your Application.

Platforms

Win32, x64.

See Also

Functions:

LOptimize::GetDefaultOptions, LOptimize::OptimizeBuffer, LOptimize::OptimizeDirCallBack, LOptimize::OptimizeBufferCallBack, Class Members

Topics:

Image Optimization

 

Raster Image Functions: Optimization Functions

Example

#define MAKE_IMAGE_PATH(pFileName) TEXT("C:\\Users\\Public\\Documents\\LEADTOOLS Images\\")pFileName
class LOptimizeDirChild : public LOptimize
{
public:
   LOptimizeDirChild();
   ~LOptimizeDirChild();
   virtual L_INT OptimizeDirCallBack(pOPTIMIZEIMAGEDIRINFO pOptImgDirCBInfo);
};
LOptimizeDirChild::LOptimizeDirChild()
{
}
LOptimizeDirChild::~LOptimizeDirChild()
{
}
L_INT LOptimizeDirChild::OptimizeDirCallBack(pOPTIMIZEIMAGEDIRINFO pOptImgDirCBInfo)
{
   // Check if pOPTIMIZEIMAGEDIRINFO data is a valid pointer
   if(pOptImgDirCBInfo)
   {
      L_TCHAR buf[200]; // Message buffer
      switch(pOptImgDirCBInfo->nStatusCode)
      {
         case OPTIMIZE_DIR_PRE_OPTIMIZINGIMAGE: /* Pre Optimizing Image */
         {
            MessageBox(NULL, TEXT("Pre Optimize Image"), TEXT("Note"), MB_OK);
            if((pOptImgDirCBInfo->pFileInfo->Format == FILE_JPEG) ||
               (pOptImgDirCBInfo->pFileInfo->Format == FILE_JPEG_411) ||
               (pOptImgDirCBInfo->pFileInfo->Format == FILE_JPEG_422) ||
               (pOptImgDirCBInfo->pFileInfo->Format == FILE_EXIF_JPEG_411) ||
               (pOptImgDirCBInfo->pFileInfo->Format == FILE_EXIF_JPEG_422))
               pOptImgDirCBInfo->pOptImgOptions->JPEGColorSpace = JPEG_COLORSPACE_422;
         }
         break;
         case OPTIMIZE_DIR_OPTIMIZINGIMAGE: /* Optimizing ... */
         {
            static int i = 1; // Optimized files counter
            // display the current file and total percent
            wsprintf(buf, TEXT("%d %% of optimizing %s to %s \nFiles %d of %d"), pOptImgDirCBInfo->nFilePercent, pOptImgDirCBInfo->szOrgFileName, pOptImgDirCBInfo->szOptFileName, i, pOptImgDirCBInfo->nTotalFolderFilesCount);
            MessageBox(NULL, buf, TEXT("Optimizing"), MB_OK);
            // if nFilePercent = 100 increment the counter by 1
            if((pOptImgDirCBInfo->nFilePercent == 100) && ((i + 1) <= pOptImgDirCBInfo->nTotalFolderFilesCount))
            i++;
         }
         break;
         case SUCCESS: /* Optimizing operation Successfully done */
         {
            MessageBox(NULL, TEXT("Operation Successfully Done"), TEXT("Optimizing"), MB_OK);
         }
         break;
         default:
         {
            /* report error to user */
            /* Give the user the ability to skip the current image only or cancel the whole operation*/
            wsprintf(buf, TEXT("Error: %d - Optimizing Image!\n%s\nPress OK to skip this file and optimize the next one!"), pOptImgDirCBInfo->nStatusCode, pOptImgDirCBInfo->szOrgFileName);
            if(MessageBox(NULL, buf, TEXT("Error"), MB_OKCANCEL|MB_ICONERROR) == IDOK)
               return(ERROR_OPT_SKIPIMAGE);
            else
               return(ERROR_OPT_ABORT);
         }
         break;
      }
   }
   return SUCCESS;
}
L_INT LOptimize__OptimizeDirExample() 
{
   pOPTIMIZEIMAGEOPTIONS pOptImgOptions = NULL ;
   L_INT nRet ;
   LOptimizeDirChild opt ;
   opt.EnableCallBack(TRUE) ;
   nRet = opt.OptimizeDir(MAKE_IMAGE_PATH(TEXT("")), /* The original Dir to be optimized. */
                           MAKE_IMAGE_PATH(TEXT("OptimizedImages")),/* The New Dir to save the Optimized image(s) to.*/
                           pOptImgOptions, /* Optimization options.*/
                           TEXT("*.jpg;*.gif;*.bmp;"), /* optimize all images with ext jpg, bmp and bmp only.*/
                           TRUE /* Optimize or not the sub-dirs.*/);
   if(nRet != SUCCESS)
      return nRet;
   return SUCCESS;
}