Optimizes a supported image format buffer using the passed optimization options.


#include "ltwrappr.h"

L_INT LOptimize::OptimizeBuffer(pOrgImgBuffer, uOrgImgBufferSize, phOptImgBuffer, puOptImgBufferSize, pOptImgOptions)


L_UCHAR * pOrgImgBuffer

Pointer to the original image buffer in memory. The image format in memory should be one of the LEAD Image optimizer supported formats.

L_SIZE_T uOrgImgBufferSize

Size, in bytes, of the original image in memory, which is referenced by the pOrgImgBuffer parameter.

HGLOBAL * phOptImgBuffer

Address of a memory handle to be updated by the optimized image buffer. The user is responsible for freeing it by calling the GlobalFree Windows function.

L_SIZE_T * puOptImgBufferSize

Pointer to a variable to be updated with the size of the optimized image buffer in memory, which is referenced by phOptImgBuffer.


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


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


This function optimizes the specified buffer and reduces its size, based on the specified optimization options.

The function will initialize the phOptImgBuffer handle and allocate the storage necessary to hold the optimized image. Since the function allocates storage to hold the optimized image, the user must free the allocated memory by calling the Windows GlobalFree function.

Note that the image buffer to be optimized should be one of the LEAD Image optimizer supported formats otherwise the function will return an Error.

You can optimize an image buffer in memory as follows:

  1. Save the image to be optimized (the original image) in memory, and get a pointer to the first byte of this image. Pass this pointer as the pOrgImgBuffer parameter. Pass the size, in bytes, of the image in memory as the uOrgImgBufferSize parameter.

  2. Declare a memory handle (HGLOBAL) variable and pass its address as the phOptImgBuffer parameter. This function will allocate and unlock the memory needed to save the optimized image buffer.

  3. Declare a long integer (L_UINT32) variable. Pass the address of this variable as the puOptImgBufferSize parameter. This function will update this variable with the size, in bytes, of the optimized image buffer.

  4. Determine the optimization options to use, by doing one of the following:

    Pass NULL in the pOptImgOptions parameter to optimize the image buffer using the default optimization options.

    Get the default optimization options by calling LOptimize::GetDefaultOptions function, and update the structure values as you like. Pass the address of the updated optimization options structure in the pOptImgOptions parameter.

  5. Call this function to optimize the image buffer and update the phOptImgBuffer and uOptImgBufferSize variables with the optimized buffer and buffer size.

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




class LOptimizeChild : public LOptimize 
   virtual L_INT OptimizeBufferCallBack(L_INT nPercent); 
L_INT LOptimizeChild::OptimizeBufferCallBack(L_INT nPercent) 
   L_TCHAR buf[1024]; // Message buffer 
   wsprintf(buf, TEXT("%d"), nPercent); 
   lstrcat(buf, TEXT("% Optimizing ")) ; 
   MessageBox(NULL, buf, TEXT("Optimizing"), MB_OK | MB_ICONINFORMATION); 
   return SUCCESS; 
L_INT LOptimize__OptimizeBufferExample(LBitmapWindow &BtmpWnd)  
   LOptimizeChild Opt; 
   LMemoryFile MemoryFile; 
   LBuffer Buffer; 
   L_INT nRet = 0; 
   L_SIZE_T uOrgImgBufferSize = 0; 
   L_UCHAR * phOrgImgBuffer = NULL; 
   HGLOBAL hOptImgBuffer = NULL; //Handle to the Optimized Buffer in Memory 
   L_SIZE_T uOptImgBufferSize = 0; //size of the optimized buffer in Memory 
   OPTIMIZEIMAGEOPTIONS OptImgOptions; //optimization options structure 
   L_UCHAR * pOptImgBuffer = NULL; 
   nRet = MemoryFile.Save( &Buffer, FILE_GIF,8,0,NULL) ; 
   uOrgImgBufferSize = Buffer.GetSize(); 
   phOrgImgBuffer = (L_UCHAR *)GlobalLock(Buffer.GetHandle()); 
   memset(&OptImgOptions, 0,sizeof(OPTIMIZEIMAGEOPTIONS)) ; 
   nRet = Opt.GetDefaultOptions(&OptImgOptions, sizeof(OPTIMIZEIMAGEOPTIONS)); 
   if(nRet == SUCCESS) 
      //Optimize the image buffer 
      Opt.EnableCallBack(TRUE) ; 
      nRet = Opt.OptimizeBuffer(phOrgImgBuffer, uOrgImgBufferSize, &hOptImgBuffer, // to be updated.  
                                &uOptImgBufferSize, // to be updated.  
      if(nRet == SUCCESS) 
         //To work on the optimized buffer, We have to lock it. 
         pOptImgBuffer = (L_UCHAR *) GlobalLock(hOptImgBuffer); 
            DWORD wWrittenBytes = 0; 
            //Now, pOptImgBuffer is ready to work on, 
            //for example you can create a disk file and dump this buffer to it. 
            L_UINT uChunkSize = 0; 
            while(uOptImgBufferSize > 0) 
               L_UINT uBytesToWrite = (L_UINT)min(uOptImgBufferSize, uChunkSize); 
               DWORD uChunkBytesWritten = 0; 
               if(!WriteFile(hFile, pOptImgBuffer, uBytesToWrite, &wWrittenBytes, NULL)) 
                  return -1; 
               pOptImgBuffer += uChunkBytesWritten; 
               uOptImgBufferSize -= uChunkBytesWritten; 
               wWrittenBytes += uChunkBytesWritten; 
            //Unlock the Memory Buffer. 
            //Free the Memory Buffer allocated by OptimizeBuffer function. 
   return SUCCESS; 
Help Version 22.0.2023.2.2
Products | Support | Contact Us | Intellectual Property Notices
© 1991-2023 LEAD Technologies, Inc. All Rights Reserved.

LEADTOOLS Raster Imaging C++ Class Library Help