Saves a bitmap to a file in any supported format, using a callback function, and letting you specify an offset within the file to begin saving. This makes it possible to embed an image file in another file.
#include "l_bitmap.h"
L_LTFIL_API L_INT L_SaveFileOffset(fd, nOffsetBegin, nSizeWritten, pBitmap, nFormat, nBitsPerPixel, nQFactor, uFlags, pfnCallback, pUserData, pSaveOptions)
The Windows file handle of the file to save.
The offset within the specified file to embed the saved image file. For example, if you specify 5, then 5 bytes of other data will precede the embedded file.
Address of a variable to be updated with the size of the embedded image file.
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 your 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.)
Output file format. For valid values, refer to Files To Be Included With Your Application.
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 closet 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.
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.
Binary flags that determine the behavior of L_SaveFile. 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). Note that in order to support multipage files, the file must be opened with both READ and WRITE access. |
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.
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.
Pointer to optional extended save options. Pass NULL to use the default save options.
Value | Meaning |
---|---|
SUCCESS | The function was successful. |
< 1 | An error occurred. Refer to Return Codes. |
This function overwrites existing data at the specified location in the file (unless you are appending to a multipage file, in which case it will add the new page after the existing pages).
Use this function to update an image that was loaded with the L_LoadFileOffset function.
The offset is specified in bytes. You must open the file and get a Windows file handle before calling this function. You must also declare a variable to receive the resulting size of the embedded image file (in the nSizeWritten
parameter).
If you use a callback and save the bitmap as a MacPaint (MAC) file, the image to be saved must have a width of 576 and a height of 720 (the required dimensions of that format).
Note: In order to support multipage files, the file must be opened with both READ and WRITE access.
This function supports signed data images, but only DICOM and TIFF formats support signed data. This function will return an error if you attempt to save a signed image to a format other than DICOM or TIFF.
For information on saving bitmaps that have been window leveled, refer to Saving Window-Leveled Bitmaps.
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.
Note: More options are available in the SAVEFILEOPTION structure.
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 resulting 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
// Set up 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)
Win32, x64, Linux.
For complete sample code, refer to the OFFSET example.
This example saves an image file with a 30-byte offset.
It then loads the file that it just saved. Note that the zSizeWritten
variable from the save is reused when loading. A real application would
have to get this information another way when loading.
L_INT SaveFileOffsetExample(pBITMAPHANDLE LeadBitmap)
{
L_INT nRet; /* Return value */
L_TCHAR szMessage[80]; /* Buffer for the MessageBox string */
HANDLE OffsetFile=NULL; /* File handle */
L_OFFSET zSizeWritten; /* Variable to be updated with the size written */
DWORD dwSizeWrite;
/* Create a file */
OffsetFile = CreateFile(MAKE_IMAGE_PATH(TEXT("TOFFSET.TST")), GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0);
/* Write some text in the file -- 29 characters and a terminator */
WriteFile(OffsetFile, TEXT("This is a 29-character string"), 30, &dwSizeWrite, NULL);
/* Save the image file with an offset inside the TOFFSET.TST file */
nRet = L_SaveFileOffset((L_HFILE)OffsetFile, 30, &zSizeWritten, LeadBitmap, FILE_BMP, 8, 0, SAVEFILE_FIXEDPALETTE, NULL, NULL, NULL);
if(nRet != SUCCESS)
return nRet;
/* Close the file */
CloseHandle(OffsetFile);
/* Notify the user with a message box */
if (nRet == SUCCESS)
{
MessageBox (NULL, TEXT("File was saved"), TEXT("Notice"), MB_OK);
}
else
{
wsprintf (szMessage, TEXT("Error %d saving the file"), nRet);
MessageBox (NULL, szMessage, TEXT("Error"), MB_OK);
return nRet;
}
/* Free the bitmap */
L_FreeBitmap (LeadBitmap);
/* Now load the file we just saved */
/* Open the file and get the file handle */
OffsetFile = CreateFile (MAKE_IMAGE_PATH(TEXT("TOFFSET.TST")), GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
/* Load the file with a 30-byte offset */
nRet = L_LoadFileOffset((L_HFILE) OffsetFile, 30, zSizeWritten,
LeadBitmap, sizeof(BITMAPHANDLE), 0, ORDER_BGR,
LOADFILE_ALLOCATE | LOADFILE_STORE,
NULL, NULL, NULL, NULL);
/* Close the file */
CloseHandle(OffsetFile);
/* Notify the user with a message box. The BPS in the message confirms that it is a new image. */
if (nRet == SUCCESS)
{
wsprintf (szMessage, TEXT("New file loaded at %d BPS"), LeadBitmap->BitsPerPixel);
MessageBox (NULL, szMessage, TEXT("Notice"), MB_OK);
}
else
{
wsprintf (szMessage, TEXT("Error %d loading the file"), nRet);
MessageBox (NULL, szMessage, TEXT("Error"), MB_OK);
return nRet;
}
return SUCCESS;
}