Loading and Saving Images

LEADTOOLS provides many options for loading and saving image files. Nevertheless, the code can be as simple as the following, which loads a LEAD compressed file and saves it as a Windows BMP file:

BITMAPHANDLE TmpBitmap = { 0 };  /* Bitmap handle for the temporary bitmap */ 
FILEINFO     FileInfo = { 0 };   /* File Information*/ 
/* Obtain image file information */ 
L_FileInfo(TEXT("image3.cmp"), &FileInfo, sizeof(FILEINFO), 0, NULL); 
/* Load a bitmap at its own bits per pixel  */ 
L_LoadBitmap(TEXT("image3.cmp"), &TmpBitmap, sizeof(BITMAPHANDLE), 0, ORDER_BGR, NULL, NULL); 
/* Save the image as a 24-bit Windows BMP file */ 
L_SaveBitmap(TEXT("TEST.BMP"), &TmpBitmap, FILE_BMP, 24, 0, NULL); 
/* Free the temporary bitmap */ 
L_FreeBitmap(&TmpBitmap); 

Obtaining Image File Information

LEADTOOLS has many functions that provide file information, including the following functions:

FILEINFO - Structure containing file information.

L_FileInfo - Obtains image file information.

L_FileInfoMemory - Obtains image file information from a file in memory.

L_FeedInfo - Supplies image data to the file-info process from a buffer that you provide.

L_GetFilterInfo - Gets the current information for a specific filter.

L_GetFilterListInfo - Gets information for an array of filters used by LEADTOOLS.

L_SetFilterInfo - Changes information for all of the fillers.

L_FreeFilterInfo - Frees the memory allocated by calling the L_GetFilterListInfo or L_GetFilterInfo function.

Whether an image file is on disk or in memory, you can get information about the image before loading it. For more information, see Raster Image Functions: Getting and Setting File Information.

Loading Images

Regardless of the video mode, with LEADTOOLS functions you can load an image at its own color resolution, manipulate it, and display it. You can also handle file-format-specific information, such as the page number or physical resolution.

LEADTOOLS will attempt to load corrupted files (so you can see at least a portion of the image). For such images, the load functions will return SUCCESS, but L_GetLoadStatus will return an error code.

LEADTOOLS supports super compressed images. Only 1-bit and 24-bit bitmaps can be kept super compressed in memory. For more information, refer to Super Compressed Bitmaps.

Support for loading images is provided by the following functions:

L_LoadBitmap - Loads an image from a file into a bitmap (a simpler, higher level version of L_LoadFile).

L_LoadFile - The core function for loading bitmaps. It provides the most flexibility and functionality: all other load functions eventually call this function.

L_LoadFileOffset - Loads an image from a file using a callback function, letting you specify the location of the image data within the file.

L_LoadMemory - Loads in image from a file in memory.

L_FeedLoad - Supplies image data to the file-load process from a buffer that you provide.

L_LoadSvg - Loads a page from an image, document or vector file as an SVG file. Support for this function is available in the Document and Medical Imaging toolkits.

L_LoadBitmapResize - Loads an image from a file into a bitmap, resizing it as it loads.

L_SaveBitmapBuffer or L_SaveFileBuffer - Saves a bitmap to a file in a memory buffer. The L_SaveFileBuffer function uses an optional callback to allow additional processing of the saved material.

L_StartFeedLoad, L_FeedLoad, and L_StopFeedLoad - Controls the execution of a load or file information procedure as you supply transmitted image data.

For more information, refer to Loading and Saving Large TIFF/BigTIFF Files and Using Filter Data to Speed up Loading Large Files.

Converting Images

Conversion from one file format to another can take place when loading or saving a file, or when performing file-to-file conversion. Converting can include the simultaneous resizing of an image. It can also change the image's bits per pixel. Of course, conversion takes longer when resizing or changing the bits per pixel is included. When reducing a file to a 4- or 8-bit format, LEADTOOLS must reduce the number of colors. Therefore, the image cannot be converted back to the original image format and retain its original colors. When converting from 24-bits or less to a 32-bit file, the alpha channel will be filled with zeros.

The LEADTOOLS SDK has its own data type for storing and accessing image data (Each target platform (for example, Windows, Android, etc.), also has its own native image data structure or class.) To make it easy to work with both the LEADTOOLS image data type and the target platform’s native image data type, the LEADTOOLS SDK provides functions for converting between the two.

The following platforms are supported:

LEADTOOLS functions make it possible to perform the following tasks:

If you load a LEAD or JPEG file and convert it to 8 bits per pixel while loading, LEADTOOLS dithers the image using the fixed palette. To use an optimized palette, you should first load it as a 24-bit bitmap; then use L_ColorResBitmap to reduce the image using an optimized palette.

Callbacks

LEADTOOLS can load overlay files, which are usually PTOCA files, by either loading directly from the disk, or by calling an overlay callback function to get the overlay bitmap from the user. To set up the callback function for use, including the methods and the order of those methods LEADTOOLS should use to load an overlay file, call L_SetOverlayCallback. To determine the current overlay callback function (the one last set using L_SetOverlayCallback), use L_GetOverlayCallback. This overlay callback must adhere to the function syntax specified in OVERLAYCALLBACK.

You can supply the input for a load operation or the output of a save operation, and you can add processing, such as a paint-while-load feature. Using Callback Functions describes these features. Callbacks lists the functions that you can supply.

You can also supply a callback that will be called whenever your application is trying to load an encrypted file. See L_SetDecryptOptions for more information.

Encrypted Files

LEADTOOLS can load encrypted files if you supply the correct password.

You can load encrypted files by providing a decryption password or a callback using L_SetDecryptOptions prior calling the information or load functions.

The password for encrypted PDF files can also be loaded using L_SetPDFOptions prior to calling the information or load functions.

For more information, refer to Loading Encrypted Files.

Multipage Files

Work with multipage files by using the following functions:

L_LoadBitmapList - Creates a bitmap list and loads bitmaps from a multipage file into the list.

L_SaveBitmapList - Saves a list of bitmaps in a multipage file.

L_DeletePage - Deletes individual pages within a multipage file.

The bitmap list functions can also be used:

L_GetBitmapListItem

L_SetBitmapListItem

L_InsertBitmapListItem

L_CopyBitmapListItems

L_GetBitmapListCount

L_DestroyBitmapList

L_RemoveBitmapListItem

L_DeleteBitmapListItems

For more information, refer to Loading and Saving Large TIFF/BigTIFF Files and Using Filter Data to Speed up Loading Large Files.

Specific File Format Support

LEADTOOLS supports a vast number of image formats. For the most current listing of all supported file formats, see Supported File Formats.

The following topics provide more information about specific file formats:

Controlling Progressive Loads and Saves

Implementing GIF Features

Implementing PhotoCD and FlashPix Features

Implementing FlashPix Extension

Implementing JBIG Features

Implementing Exif Features

Implementing TIFF Comments and Tags

Working with Markers

Multipage File Formats

Working with the RAW File Filter

The LEADTOOLS toolkits have many functions to support specific file formats. They include:

Document Formats

LEADTOOLS provides support for loading a document as a raster image. Document formats such as PDF, XPS, XLS, PST, RTF and Text do not contain physical width, height or resolution. It is up to the loader to specify the transformation from logical coordinates to physical pixels through a process called rasterization. Rasterization is the process of converting a document to a raster image. To determine whether a certain file on disk or memory contains a document file rather than a regular raster image, call L_FileInfo and look at the bIsDocFile member of the resulting FILEINFO structure. When rasterizing document files, use the L_GetRasterizeDocOptions to get current values for the control options used by LEADTOOLS. Use L_SetRasterizeDocOptions to set control option values before rasterizing document files.

NOTE: To save a region inside a TIFF file, you must have an unlocked Document or Medical Imaging license.

Vector

When loading vector files, use the L_GetVectorOptions function to get the current values for the control options used by LEADTOOLS. Use the L_SetVectorOptions function to change the values for the control options before loading the vector files.

Saving Colored Images as Bitonal

LEADTOOLS can automatically save a colored image (an image that has more than 1 bit/pixel) as bitonal (an image that has 1 bit/pixel). This is accomplished by passing "1" as the nBitsPerPixel parameter of L_SaveBitmap or L_SaveFile.

Whenever you reduce an image's color resolution to 8 bits per pixel or less, a dithering method comes into play. One alternative is to use a nearest-color match (no dithering), which means that the color of each pixel is changed to the palette color that most closely matches it. If the original image contains subtle color details, the result of a nearest-color match may have large blotches of color that are not very pleasing.

Dithering methods create the appearance of more subtle shades by mixing in pixels of different colors. This is similar to the way newspaper pictures produce the appearance of shades of gray, even though the only actual colors are black and white.

Starting with v17, LEADTOOLS does not use dithering when saving a colored image as bitonal (1-bit/pixel) by default. This will guarantee that the final bitonal image contains the clearest representation of the original text in the image and provides the best input for document-based recognition methods such as OCR and Barcode.

To enable dithering, you must set the dithering method inside the BITMAPHANDLE to the required value and then instruct the save functions to use this value when saving the image. The following example will load a 24bpp image and save it as a bitonal image, both with and without dithering.

static L_VOID Test(L_TCHAR* coloredImageFileName) 
{ 
   // SAVEFILEOPTION does not use ESO_USEDITHERINGMETHOD by default 
   BITMAPHANDLE bitmapHandle = {0}; 
   L_LoadBitmap(coloredImageFileName, &bitmapHandle, sizeof(BITMAPHANDLE), 0, ORDER_BGRORGRAY, NULL, NULL); 
   // Save it with no dithering options 
   L_SaveBitmap(L"C:\\NotDithered.tif", &bitmapHandle, FILE_CCITT_GROUP4, 1, 1, NULL); 
   // Change the bitmap dithering method to FloydStein 
   bitmapHandle.DitheringMethod = FLOYD_STEIN_DITHERING; 
   // Use the bitmap dithering method when saving 
   SAVEFILEOPTION saveOptions = {0}; 
   L_GetDefaultSaveFileOption(&saveOptions, sizeof(SAVEFILEOPTION)); 
   saveOptions.Flags |= ESO_USEDITHERINGMETHOD; 
   // Save it again 
   L_SaveBitmap(L"C:\\Dithered.tif", &bitmapHandle, FILE_CCITT_GROUP4, 1, 1, &saveOptions); 
   L_FreeBitmap(&bitmapHandle); 
} 

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.