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


#include "l_bitmap.h"

L_LTIMGOPT_API L_INT L_OptOptimizeDir(pszOrgDirPath, pszOptDirPath, pOptImgOptions, pszFilesExt, bIncludeSubDirs, pfnOptImgDirCB, pUserData)


L_TCHAR * pszOrgDirPath

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

L_TCHAR * pszOptDirPath

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


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

L_TCHAR * 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 ".".

L_BOOL 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.


Optional callback function for additional processing.

The callback function must adhere to the function prototype described in OPTIMIZEIMAGEDIRCALLBACK Function.

L_VOID * pUserData

Void pointer that you can use to pass one or more additional parameters that the callback function needs.

To use this feature, assign a value to a variable or create a structure that contains as many fields as you need. Then, in this parameter, pass the address of the variable or structure, casting it to L_VOID *. The callback function, which receives the address in its own pUserData parameter, can cast it to a pointer of the appropriate data type to access your variable or structure.

If the additional parameters are not needed, you can pass NULL in this parameter.


Value Meaning
SUCCESS The function was successful.
< 1 An error occurred. Refer to Return Codes.


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


Win32, x64.

See Also




This example is going to optimizes all files with extensions jpg, bmp and tmp.

   // Check if pCBData is a valid pointer 
   if (pImgDirInfo) 
      L_TCHAR buf[300]; // Message buffer 
      switch (pImgDirInfo->nStatusCode) 
      case SUCCESS: // Optimizing operation Successfully done 
         _tprintf("Optimizing: Operation Successfully Done."); 
      case OPTIMIZE_DIR_PRE_OPTIMIZINGIMAGE: // Pre Optimizing Image 
         _tprintf("NOTE: Pre Optimize Image."); 
         if ((pImgDirInfo->pFileInfo->Format == FILE_JPEG) || 
            (pImgDirInfo->pFileInfo->Format == FILE_JPEG_411) || 
            (pImgDirInfo->pFileInfo->Format == FILE_JPEG_422) || 
            (pImgDirInfo->pFileInfo->Format == FILE_EXIF_JPEG_411) || 
            (pImgDirInfo->pFileInfo->Format == FILE_EXIF_JPEG_422)) 
            pImgDirInfo->pOptImgOptions->JPEGColorSpace = JPEG_COLORSPACE_422; 
      case OPTIMIZE_DIR_OPTIMIZINGIMAGE: // Optimizing 
         static int i = 1;     // Optimized files counter 
         // display the current file and total percent 
         wsprintf(buf, TEXT("Optimizing: %d %% of optimizing %s to %s \nFiles %d of %d"), pImgDirInfo->nFilePercent, pImgDirInfo->szOrgFileName, pImgDirInfo->szOptFileName, i, pImgDirInfo->nTotalFolderFilesCount); 
         _tprintf(_T("%s"), buf); 
         // if nFilePercent = 100 increment the counter by 1 
         if ((pImgDirInfo->nFilePercent == 100) && ((i + 1) <= pImgDirInfo->nTotalFolderFilesCount)) 
         // 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 'Y' to skip this file and optimize the next one; 'N' for optimization abortion."), pImgDirInfo->nStatusCode, pImgDirInfo->szOrgFileName); 
         _tprintf(_T("%s"), buf); 
         char chr; 
         scanf("%c", &chr); 
         if (chr == 'Y') 
         else if (chr == 'N') 
   return SUCCESS; 
extern "C" L_INT OptOptimizeDirExample(OPTIMIZEIMAGEOPTIONS OptImgOptions, L_BOOL bIncludeSubDir, L_VOID * pUserData) 
   return L_OptOptimizeDir(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. 
      &OptImgOptions,                                 // Optimization options. 
      TEXT("*.jpg;*.bmp"),                            // optimize all images with ext jpg, bmp and tmp only. 
      bIncludeSubDir,                                 // Optimize or not the sub-dirs. 
      fnOptDirCB,                                     // Callback 
      pUserData);                                     // User data 

Help Version 22.0.2023.7.11
Products | Support | Contact Us | Intellectual Property Notices
© 1991-2023 LEAD Technologies, Inc. All Rights Reserved.

LEADTOOLS Raster Imaging C API Help

Products | Support | Contact Us | Intellectual Property Notices
© 1991-2023 LEAD Technologies, Inc. All Rights Reserved.