L_SaveFileBuffer

#include "l_bitmap.h"

L_LTFIL_API L_INT L_SaveFileBuffer(pBuffer, uInitialBufferSize, puFinalFileSize, pBitmap, nFormat, nBitsPerPixel, nQFactor, uFlags, pfnFileSaveCB, pfnSaveBufferCB, pUserData, pSaveOptions)

Saves a bitmap to a file in a memory buffer that the user must allocate, using input data that the FILESAVECALLBACK callback function supplies. The output can be in any of the supported compressed or uncompressed file formats.

Parameters

L_UCHAR* pBuffer

Pointer to a buffer allocated by the user. Note that if this buffer is not large enough to accommodate the file in memory, the SAVEBUFFERCALLBACK is called to allow the user to reallocate the buffer.

L_SIZE_T uInitialBufferSize

Size of pBuffer in bytes. Note that the buffer size can be reallocated in the pfnSaveBufferCB callback.

L_SIZE_T* puFinalFileSize

Pointer to a variable that will contain the final size of the file in memory. This is not the final size of the allocated buffer, which may be larger than the memory file.

pBITMAPHANDLE pBitmap

Pointer to the bitmap handle that describes the data to be saved. The bitmap handle can contain the actual data, but does not have to, since the callback function can supply the data.

However, the bitmap handle must contain values for the following fields: Width, Height, BitsPerPixel, BytesPerLine, nColors, ViewPerspective, Order, and DitheringMethod. (The BytesPerLine value must be a multiple of four.)

L_INT nFormat

Output file format. For valid values, refer to Files To Be Included With Your Application.

L_INT nBitsPerPixel

Resulting file's pixel depth. Note that not all bits per pixel are available to all file formats. For valid values, refer to Files To Be Included With Your Application. If nBitsPerPixel is 0, the file will be stored using the closest BitsPerPixel value supported by that format. For example, if a file format supports 1, 4, and 24 BitsPerPixel, and the pBitmap->BitsPerPixel is 5, the file will be stored as 24 bit. Likewise, if the pBitmap->BitsPerPixel is 2, the file will be stored as 4 bit.

L_INT nQFactor

This parameter is used when saving an image to file format that supports quality factor (QFactor). QFactor is a number that determines the degree of loss in the compression process.

For possible values, refer to Compression Quality Factors.

L_UINT uFlags

Binary flags that determine the behavior of L_SaveFileBuffer. You can specify one of the following values:

Value Meaning
SAVEFILE_FIXEDPALETTE [0x0001] The function uses the fixed palette for images that are saved as 8 bits per pixel or less.
SAVEFILE_OPTIMIZEDPALETTE [0x0002] The function uses the individual image's optimized palette for images that are saved as 8 bits per pixel or less. The optimized palette must be included in the bitmap handle.
SAVEFILE_MULTIPAGE [0x0004] The function saves the image in a multipage file. It appends the image as the last one in the file. You can save multipage images in PCX, GIF, and most TIFF file formats (including JTIF, but excluding EXIF).

FILESAVECALLBACK pfnFileSaveCB

Optional callback function for additional processing.

If you do not provide a callback function, use NULL as the value of this parameter.

If you do provide a callback function, use the function pointer as the value of this parameter.

The callback function must adhere to the syntax described in FILESAVECALLBACK Function.

SAVEBUFFERCALLBACK pfnSaveBufferCB

Optional callback function for reallocating the buffer if it is not large enough to accommodate the save. NULL can be passed for this parameter. However, if NULL is passed for this parameter and the buffer is not large enough, the function will fail.

The callback function must adhere to the syntax described in SAVEBUFFERCALLBACK 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.

pSAVEFILEOPTION pSaveOptions

Pointer to optional extended save options. Pass NULL to use the default save options.

Returns

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

Comments

L_SaveFileBuffer saves a bitmap to a file in a memory buffer that the user must allocate. The output can be in any of the supported compressed or uncompressed file formats. This function is similar to L_SaveFileMemory, except the user is responsible for allocating/reallocating buffers. The SAVEBUFFERCALLBACK notifies the user if the buffer is not large enough. The user can then reallocate the buffer. For more information, see SAVEBUFFERCALLBACK.

If the user wants to allocate a buffer that is large enough to hold the file in memory, an upper bound on the file size can be estimated by the following formula:

RequiredBytes = (Image Width) * (Image Height) * (BitsPerPixel) / 8

Note that this is an estimatethis does not include the size of the file header, and it does not take into account any compression. It is best to use a SAVEBUFFERCALLBACK so that the user can be notified if the buffer is not large enough.

Support for 12 and 16-bit grayscale images is only available in the Document and Medical Imaging toolkits.

This function cannot be used in combination with L_RedirectIO.

Notes:

This function supports signed data images, but only DICOM and TIFF formats support signed data. This function will return an error code if you attempt to save a signed image to a format other than DICOM or TIFF.

If the bitmap has a region, the region stored in the bitmap will be saved, if the image is saved as one of the TIFF file formats.

You can convert one file format to another when you save a file. You can also use the high-level function, L_FileConvert, to convert any possible format to any other possible format.

In LEADTOOLS version 17 and up, when saving a colored image (such as a 24-bits per pixel image) to bitonal (1-bit per pixel), the toolkit will not use any dithering when converting the image data. This is done because dithering is not the recommended when converting colored images containing text for document processing such as OCR and Barcode. The result text will be fuzzy and hard for a recognition engine to process. To save a colored image as bitonal with Floyd-Stein dithering (the behavior of LEADTOOLS 16.5 and earlier) use the ESO_USEDITHERINGMETHOD along with BITMAPHANDLE.DitheringMethod as illustrated below:

// 'pBitmap' is a colored BITMAPHANDLE 
// Setup FloydStein dithering: 
bitmapHandle.DitheringMethod = FLOYD_STEIN_DITHERING; 
SAVEFILEOPTION saveOptions = {0}; 
L_GetDefaultSaveFileOption(&saveOptions, sizeof(SAVEFILEOPTION)); 
saveOptions.Flags |= ESO_USEDITHERINGMETHOD; 
// Save the bitmap as 1-bpp with auto-dithering: 
L_SaveBitmap(fileName, &bitmapHandle, FILE_CCITT_GROUP4, 1, 0, &saveOptions); 
// or any other L_SaveBitmapXyz or L_SaveFileXyz functions such as: 
// L_SaveFile(fileName, &bitmapHandle, FILE_CCITT_GROUP4, 1, 0, 0, NULL, NULL, &saveOptions) 

Required DLLs and Libraries

Platforms

Win32, x64, Linux.

See Also

Functions

Topics

Example

This example loads the file 'pszFile', and then saves it to a memory buffer that the caller allocates. Note that when the buffer becomes filled, the ExampleSaveBufferCB callback is called to notify the caller to reallocate the buffer. After the save is completed, the size of the file in memory is displayed. The image 'pszFile' is flipped, and saved as the second page of the memory buffered TIF Finally, the second page of the memory buffer containing the file is loaded into pBitmap

typedef struct _tagMYBUFFER 
{ 
   L_UCHAR   *pBuffer; 
   L_SIZE_T   dwBufferSize; 
} MYBUFFER,  *pMYBUFFER; 
 
static L_INT EXT_CALLBACK ExampleSaveBufferCB (L_SIZE_T   uRequiredSize, 
                                               L_UCHAR ** ppBuffer, 
                                               L_SIZE_T * pdwBufferSize, 
                                               L_VOID   * pUserData) 
{ 
   L_UCHAR *pTempBuffer; 
   L_INT nRet; 
   L_TCHAR szMsg[400]; 
   pMYBUFFER pMyBuffer = (pMYBUFFER)pUserData; 
    
   pTempBuffer = (L_UCHAR *)realloc(pMyBuffer->pBuffer, uRequiredSize); 
   if (pTempBuffer) 
   { 
      wsprintf(szMsg, TEXT("** Reallocate from\n\t[0x%x, %d] to\n\t[0x%x, %d]\n"),  
               pMyBuffer->pBuffer, 
               pMyBuffer->dwBufferSize, 
               pTempBuffer, 
               uRequiredSize); 
       
      //MessageBox(NULL, szMsg, TEXT(""), MB_OK); 
      OutputDebugString(szMsg); 
             
      pMyBuffer->pBuffer = pTempBuffer; 
      pMyBuffer->dwBufferSize = uRequiredSize; 
       
      *ppBuffer = pMyBuffer->pBuffer; 
      *pdwBufferSize = uRequiredSize; 
      nRet = SUCCESS; 
   } 
   else 
      nRet = FAILURE; 
 
   return nRet; 
} 
 
L_INT SaveFileBufferExample(L_TCHAR * pszFile, pBITMAPHANDLE pBitmap) 
{ 
   L_INT nRet; 
   BITMAPHANDLE Bitmap; 
   L_SIZE_T dwFinalFileSize; 
   L_TCHAR szMsg[100]; 
   SAVEFILEOPTION SaveFileOption; 
   LOADFILEOPTION LoadFileOption; 
   MYBUFFER        MyBuffer; 
 
   MyBuffer.dwBufferSize = 100; 
   MyBuffer.dwBufferSize = 0; 
   MyBuffer.pBuffer = NULL; 
 
   nRet = L_LoadBitmap(pszFile, &Bitmap, sizeof(BITMAPHANDLE), 0, ORDER_BGR, NULL, NULL); 
   if (nRet != SUCCESS) 
      return nRet; 
 
   MyBuffer.pBuffer = (L_UCHAR *)malloc(MyBuffer.dwBufferSize);  //large enough to hold rgbw.bmp 
   if (MyBuffer.pBuffer == NULL) 
      return FAILURE; 
 
   L_GetDefaultSaveFileOption(&SaveFileOption, sizeof(SAVEFILEOPTION)); 
   L_GetDefaultLoadFileOption(&LoadFileOption, sizeof(LOADFILEOPTION)); 
   SaveFileOption.PageNumber = 1; 
 
   nRet = L_SaveFileBuffer(MyBuffer.pBuffer,  
                    MyBuffer.dwBufferSize, 
                    &dwFinalFileSize, 
                    &Bitmap, 
                    FILE_TIF, 
                    0, 
                    0, 
                    0, 
                    NULL, 
                    ExampleSaveBufferCB, 
                    &MyBuffer, 
                    &SaveFileOption); 
   if(nRet != SUCCESS) 
      return nRet; 
 
   wsprintf(szMsg, TEXT("Buffer Size After First Save: %d"), dwFinalFileSize); 
   MessageBox(NULL, szMsg, TEXT(""), MB_OK); 
 
   nRet = L_FlipBitmap(&Bitmap); 
   if(nRet != SUCCESS) 
      return nRet; 
   SaveFileOption.PageNumber = 2; 
 
   nRet = L_SaveFileBuffer(MyBuffer.pBuffer,  
                           MyBuffer.dwBufferSize, 
                           &dwFinalFileSize, 
                           &Bitmap, 
                           FILE_TIF, 
                           0, 
                           0, 
                           0, 
                           NULL, 
                           ExampleSaveBufferCB, 
                           &MyBuffer, 
                           &SaveFileOption); 
   if(nRet != SUCCESS) 
      return nRet; 
 
   wsprintf(szMsg, TEXT("Final Buffer Size: %d"), dwFinalFileSize); 
   MessageBox(NULL, szMsg, TEXT(""), MB_OK); 
 
   LoadFileOption.PageNumber = 2; 
   if(pBitmap->Flags.Allocated) 
      L_FreeBitmap(pBitmap); 
   nRet = L_LoadBitmapMemory (MyBuffer.pBuffer, pBitmap, sizeof(BITMAPHANDLE), 0, ORDER_BGR, dwFinalFileSize, &LoadFileOption, NULL); 
   if(nRet != SUCCESS) 
      return nRet; 
 
   free(MyBuffer.pBuffer); 
   L_FreeBitmap(&Bitmap); 
   return SUCCESS; 
} 

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

LEADTOOLS Raster Imaging C API Help