#include "l_bitmap.h"
L_LTFIL_API L_INT L_StartDecompressBuffer(phDecompress, pStartDecompressData)
L_HGLOBAL *phDecompress; |
pointer to the decompression handle |
pSTARTDECOMPRESSDATA pStartDecompressData; |
pointer to a structure |
Initializes the buffered decompression engine. The decompression is then carried out using the L_DecompressBuffer function. It is ended by the L_StopDecompressBuffer function.
Parameter |
Description |
phDecompress |
Pointer to the handle that identifies the decompression process. |
pStartDecompressData |
Pointer to a structure that contains information about the decompression process. |
SUCCESS |
The function was successful. |
< 1 |
An error occurred. Refer to Return Codes. |
You must declare a handle of type HGLOBAL before calling this function. Pass the address of this handle to this function and the function will initialize the handle.
If this function is called for an unsupported format, it will return ERROR_INV_PARAMETER.
Currently, raw JPEG must contain all the Huffman tables encoded. That is, it must be a readable JPEG file.
Required DLLs and Libraries
LTFIL For a listing of the exact DLLs and Libraries needed, based on the toolkit version, refer to Files To Be Included With Your Application. |
Win32, x64, Linux.
Functions: |
L_DecompressBuffer, L_StopDecompressBuffer, L_StartCompressBuffer, L_CompressBuffer, L_EndCompressBuffer |
Topics: |
|
|
#define MAKE_IMAGE_PATH(pFileName) TEXT("C:\\Users\\Public\\Documents\\LEADTOOLS Images\\")pFileName
#include <fcntl.h>
#include <io.h>
#include <Tchar.h>
#define TAG_STRIPOFFSETS 0x111
#define TAG_STRIPBYTECOUNTS 0x117
#define TAG_ROWSPERSTRIP 0x116
#define MAX_STRIPS 1000
// Returns maximum index
L_INT ReadTag(L_TCHAR *pszFileName, L_UINT16 uTag, L_INT StripArray[])
{
L_UINT16 uType;
L_UINT32 uCount;
L_ReadFileTag(pszFileName, uTag, &uType, &uCount, StripArray, NULL);
return uCount;
}
// Example 1
// This sample loads raw data from packbits.tif
// packbits.tif is a 24-bit tif packbits compressed file
// packbits.tif has 46 strips of packbits data
// The strip information is obtained by reading the TIF tags TAG_STRIPOFFSETS and TAG_STRIPBYTECOUNTS
// The strips are directly read and fed to the L_Compress APIs
L_INT StartDecompressBufferFirstExample(L_VOID)
{
L_INT nRet;
L_TCHAR *szFileName = MAKE_IMAGE_PATH(TEXT("packbits.tif"));
L_HANDLE fd;
L_UCHAR *pBuf = NULL;
BITMAPHANDLE Bitmap;
FILEINFO TempFileInfo;
L_INT nRowsPerStrip=0;
STARTDECOMPRESSDATA StartDecompressData;
DECOMPRESSDATA DecompressData;
L_INT nIndex = 0;
HGLOBAL hDecompress;
L_INT StripOffsets[MAX_STRIPS];
L_INT StripSizes[MAX_STRIPS];
L_INT nMaxIndex;
DWORD wReadBytes;
memset(&TempFileInfo, 0, sizeof(FILEINFO));
TempFileInfo.uStructSize = sizeof(TempFileInfo);
nRet = L_FileInfo (szFileName, &TempFileInfo, sizeof(FILEINFO), 0, NULL);
if(nRet != SUCCESS)
return nRet;
//******************************
//*** L_StartDecompressBuffer
//******************************
memset(&StartDecompressData, 0, sizeof(STARTDECOMPRESSDATA) );
memset(&DecompressData, 0, sizeof(DECOMPRESSDATA) );
StartDecompressData.uStructSize = sizeof(STARTDECOMPRESSDATA);
StartDecompressData.pBitmap = &Bitmap;
StartDecompressData.uBitmapStructSize = sizeof(BITMAPHANDLE);
StartDecompressData.uStripsOrTiles = DECOMPRESS_STRIPS;
StartDecompressData.uFormat = FILE_RAW_PACKBITS;
StartDecompressData.nWidth = TempFileInfo.Width;
StartDecompressData.nHeight = TempFileInfo.Height;
StartDecompressData.nBitsPerPixel = TempFileInfo.BitsPerPixel;
StartDecompressData.nViewPerspective = TempFileInfo.ViewPerspective;
StartDecompressData.nRawOrder = TempFileInfo.Order;
StartDecompressData.nLoadOrder = ORDER_BGRORGRAY;
StartDecompressData.nXResolution = TempFileInfo.XResolution;
StartDecompressData.nYResolution = TempFileInfo.YResolution;
StartDecompressData.pfnReadCallback = NULL;
StartDecompressData.uFlags = 0;
StartDecompressData.pUserData = NULL;
StartDecompressData.nPhotoInt = 2; // TIFF tag 0-6 WhiteIsZero(0) BlackIsZero(1) RGB(2)
nRet = L_StartDecompressBuffer(&hDecompress, &StartDecompressData);
if(nRet != SUCCESS)
return nRet;
//******************************
//*** L_DecompressBuffer
//******************************
nMaxIndex = ReadTag(szFileName, TAG_STRIPOFFSETS, StripOffsets);
ReadTag(szFileName, TAG_STRIPBYTECOUNTS, StripSizes);
ReadTag(szFileName, TAG_ROWSPERSTRIP, &nRowsPerStrip);
fd = CreateFile(szFileName, GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
DecompressData.nRow = 0;//Note--this field is ignored for strips
for (nIndex = 0; nIndex < nMaxIndex; nIndex++)
{
//seek to the first strip
SetFilePointer(fd,StripOffsets[nIndex] , 0, FILE_BEGIN);
pBuf = (L_UCHAR *)malloc(StripSizes[nIndex]);
ReadFile (fd, pBuf, StripSizes[nIndex], &wReadBytes, NULL);
DecompressData.uStructSize = sizeof(DECOMPRESSDATA);
DecompressData.pBuffer = pBuf; //pointer to raw data
DecompressData.nWidth = TempFileInfo.Width; //Width of uncompressed strip/tile
DecompressData.nHeight = nRowsPerStrip; //Height of uncompressed strip/tile
if (nIndex == (nMaxIndex - 1 ))
{
//fewer rows per strip
DecompressData.nHeight = TempFileInfo.Height - (nMaxIndex - 1) * nRowsPerStrip;
}
DecompressData.uOffset = 0; //Offset of strip relative to buffer
DecompressData.nBufferSize = StripSizes[nIndex];//Size of strip after compression
DecompressData.nCol = 0; //Col offset of tile
DecompressData.uFlags = DECOMPRESS_CHUNK_COMPLETE;
DecompressData.nReserved1 = 0; //Reserved for future use
nRet = L_DecompressBuffer ( hDecompress, &DecompressData);
if(nRet != SUCCESS)
return nRet;
free(pBuf);
}
CloseHandle(fd);
//******************************
//*** L_StopDecompressBuffer
//******************************
L_StopDecompressBuffer (hDecompress);
// Bitmap contains the uncompressed bitmap
nRet = L_SaveBitmap (MAKE_IMAGE_PATH(TEXT("Uncompressed.bmp")), &Bitmap, FILE_BMP, 24, 0, NULL);
L_FreeBitmap(&Bitmap);
return nRet;
}
// Example 2
// This sample is similar to LoadRawPackbitsStrips(), except the data
// is fed to L_Decompress APIs as tiles instead of strips and the image
// created is 4x the size of the original image, consisting of four of the original images
// This sample loads raw data from packbits.tif
// packbits.tif is a 24-bit tif packbits compressed file
// packbits.tif has 46 strips of packbits data
// The strip information is obtained by reading the TIF tags TAG_STRIPOFFSETS and TAG_STRIPBYTECOUNTS
// The strips are directly read and fed to the L_Compress APIs
L_INT StartDecompressBufferSecondExample()
{
L_INT nRet;
L_TCHAR *szFileName = MAKE_IMAGE_PATH(TEXT("packbits.tif"));
L_HANDLE fd;
L_UCHAR *pBuf = NULL;
BITMAPHANDLE Bitmap;
FILEINFO TempFileInfo;
L_INT nRowsPerStrip=0;
STARTDECOMPRESSDATA StartDecompressData;
DECOMPRESSDATA DecompressData;
L_INT nIndex = 0;
HGLOBAL hDecompress;
L_INT nCurrentRow = 0;
L_INT StripOffsets[MAX_STRIPS];
L_INT StripSizes[MAX_STRIPS];
L_INT nMaxIndex;
DWORD wReadBytes;
memset(&TempFileInfo, 0, sizeof(FILEINFO));
TempFileInfo.uStructSize = sizeof(TempFileInfo);
nRet = L_FileInfo (szFileName, &TempFileInfo, sizeof(FILEINFO), 0, NULL);
if(nRet != SUCCESS)
return nRet;
//******************************
//*** nRet = L_StartDecompressBuffer
//******************************
memset(&StartDecompressData, 0, sizeof(STARTDECOMPRESSDATA) );
memset(&DecompressData, 0, sizeof(DECOMPRESSDATA) );
StartDecompressData.uStructSize = sizeof(STARTDECOMPRESSDATA); // For versioning
StartDecompressData.pBitmap = &Bitmap;
StartDecompressData.uBitmapStructSize = sizeof(BITMAPHANDLE);
StartDecompressData.uStripsOrTiles = DECOMPRESS_TILES;
StartDecompressData.uFormat = FILE_RAW_PACKBITS;
StartDecompressData.nWidth = TempFileInfo.Width * 2; //
StartDecompressData.nHeight = TempFileInfo.Height * 2; //
StartDecompressData.nBitsPerPixel = TempFileInfo.BitsPerPixel; //
StartDecompressData.nViewPerspective = TempFileInfo.ViewPerspective; //
StartDecompressData.nRawOrder = TempFileInfo.Order; // ORDER_RGB, ORDER_BGR
StartDecompressData.nLoadOrder = ORDER_BGRORGRAY; // ORDER_RGB, ORDER_BGR
StartDecompressData.nXResolution = TempFileInfo.XResolution;
StartDecompressData.nYResolution = TempFileInfo.YResolution;
StartDecompressData.pfnReadCallback = NULL; // optional callback function
StartDecompressData.uFlags = 0; // DECOMPRESS_LSB, DECOMPRESS_PAD4, DECOMPRESS_WHITEONBLACK, DECOMPRESS_PALETTE
StartDecompressData.pUserData = NULL; //
StartDecompressData.nPhotoInt = 2; // TIFF tag 0-6 WhiteIsZero(0) BlackIsZero(1) RGB(2)
//StartDecompressData.nPlanarConfig; // TIFF tag 1 = Chunky, 2 = Planar format
nRet = L_StartDecompressBuffer(&hDecompress, &StartDecompressData);
if(nRet != SUCCESS)
return nRet;
//******************************
//*** L_DecompressBuffer
//******************************
nMaxIndex = ReadTag(szFileName, TAG_STRIPOFFSETS, StripOffsets);
ReadTag(szFileName, TAG_STRIPBYTECOUNTS, StripSizes);
ReadTag(szFileName, TAG_ROWSPERSTRIP, &nRowsPerStrip);
fd= CreateFile(szFileName, GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
nCurrentRow = 0;
for (nIndex = 0; nIndex < nMaxIndex; nIndex++)
{
//seek to the first strip
SetFilePointer(fd, StripOffsets[nIndex], 0, FILE_BEGIN);
pBuf = (L_UCHAR *)malloc(StripSizes[nIndex]);
ReadFile(fd, pBuf, StripSizes[nIndex], &wReadBytes, NULL);
DecompressData.uStructSize = sizeof(DECOMPRESSDATA);
DecompressData.pBuffer = pBuf; //pointer to raw data
DecompressData.nWidth = TempFileInfo.Width; //Width of uncompressed strip/tile
DecompressData.nHeight = nRowsPerStrip; //Height of uncompressed strip/tile
if (nIndex == (nMaxIndex - 1 ))
{
//fewer rows per strip
DecompressData.nHeight = TempFileInfo.Height - (nMaxIndex - 1) * nRowsPerStrip;
}
DecompressData.uOffset = 0; //Offset of strip relative to buffer
DecompressData.nBufferSize = StripSizes[nIndex]; //Size of strip after compression
DecompressData.nRow = nCurrentRow; //Row offset of tile
DecompressData.nCol = 0; //Col offset of tile
DecompressData.uFlags = DECOMPRESS_CHUNK_COMPLETE;
DecompressData.nReserved1 = 0; //Reserved for future use
//Strips in upper left corner
nRet = L_DecompressBuffer ( hDecompress, &DecompressData);
if(nRet != SUCCESS)
return nRet;
//Strips in upper right corner
DecompressData.nRow = nCurrentRow;
DecompressData.nCol = TempFileInfo.Width;
L_DecompressBuffer( hDecompress, &DecompressData);
//Strips in lower left corner
DecompressData.nRow = TempFileInfo.Height + nCurrentRow;
DecompressData.nCol = 0;
L_DecompressBuffer( hDecompress, &DecompressData);
//Strips in lower right corner
DecompressData.nRow = TempFileInfo.Height + nCurrentRow;
DecompressData.nCol = TempFileInfo.Width;
nRet = L_DecompressBuffer( hDecompress, &DecompressData);
if(nRet != SUCCESS)
return nRet;
free(pBuf);
nCurrentRow += DecompressData.nHeight;
}
CloseHandle(fd);
//******************************
//*** L_StopDecompressBuffer
//******************************
L_StopDecompressBuffer(hDecompress);
// Bitmap contains the uncompressed bitmap
nRet = L_SaveBitmap(MAKE_IMAGE_PATH(TEXT("Uncompressed.bmp")), &Bitmap, FILE_BMP, 24, 0, NULL);
L_FreeBitmap(&Bitmap);
return nRet;
}
// Example 3
// This sample loads raw data from OCR1.tif
// This file is a tif ccitt group4 compressed with a single strip
// The strip information is obtained by reading the TIF tags TAG_STRIPOFFSETS and TAG_STRIPBYTECOUNTS
// The strip is directly read and fed to the L_Decompress APIs
L_INT StartDecompressBufferThirdExample()
{
L_INT nRet;
L_TCHAR *szFileName = MAKE_IMAGE_PATH(TEXT("OCR1.tif"));
L_HANDLE fd;
L_UCHAR *pBuf = NULL;
BITMAPHANDLE Bitmap;
FILEINFO TempFileInfo;
L_INT nRowsPerStrip=0;
STARTDECOMPRESSDATA StartDecompressData;
DECOMPRESSDATA DecompressData;
L_INT nIndex = 0;
HGLOBAL hDecompress;
L_INT StripOffsets[MAX_STRIPS];
L_INT StripSizes[MAX_STRIPS];
L_INT nMaxIndex;
DWORD wReadBytes;
memset(&TempFileInfo, 0, sizeof(FILEINFO));
TempFileInfo.uStructSize = sizeof(TempFileInfo);
nRet = L_FileInfo(szFileName, &TempFileInfo, sizeof(FILEINFO), 0, NULL);
if(nRet != SUCCESS)
return nRet;
//******************************
//*** L_StartDecompressBuffer
//******************************
memset(&StartDecompressData, 0, sizeof(STARTDECOMPRESSDATA) );
memset(&DecompressData, 0, sizeof(DECOMPRESSDATA) );
StartDecompressData.uStructSize = sizeof(STARTDECOMPRESSDATA);
StartDecompressData.pBitmap = &Bitmap;
StartDecompressData.uBitmapStructSize = sizeof(BITMAPHANDLE);
StartDecompressData.uStripsOrTiles = DECOMPRESS_TILES;
StartDecompressData.uFormat = FILE_FAX_G3_1D;
StartDecompressData.nWidth = TempFileInfo.Width;
StartDecompressData.nHeight = TempFileInfo.Height;
StartDecompressData.nBitsPerPixel = TempFileInfo.BitsPerPixel;
StartDecompressData.nViewPerspective = TempFileInfo.ViewPerspective;
StartDecompressData.nRawOrder = TempFileInfo.Order; // ORDER_RGB, ORDER_BGR
StartDecompressData.nLoadOrder = ORDER_BGRORGRAY;
StartDecompressData.nXResolution = TempFileInfo.XResolution;
StartDecompressData.nYResolution = TempFileInfo.YResolution;
StartDecompressData.pfnReadCallback = NULL;
StartDecompressData.uFlags = 0;
StartDecompressData.pUserData = NULL; //
StartDecompressData.nPhotoInt = 0; // TIFF tag 0-6 TIFF tag 0=WhiteIsZero, 1=BlackIsZero
nRet = L_StartDecompressBuffer(&hDecompress, &StartDecompressData);
if(nRet != SUCCESS)
return nRet;
//******************************
//*** L_DecompressBuffer
//******************************
nMaxIndex = ReadTag(szFileName, TAG_STRIPOFFSETS, StripOffsets);
nMaxIndex = ReadTag(szFileName, TAG_STRIPBYTECOUNTS, StripSizes);
ReadTag(szFileName, TAG_ROWSPERSTRIP, &nRowsPerStrip);
fd= CreateFile(szFileName, GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
DecompressData.nRow = 0; //This field is ignored with strips
for (nIndex = 0; nIndex < nMaxIndex; nIndex++)
{
//seek to the first strip
SetFilePointer(fd, StripOffsets[nIndex], 0, FILE_BEGIN);
pBuf = (L_UCHAR *)malloc(StripSizes[nIndex]);
ReadFile(fd, pBuf, StripSizes[nIndex], &wReadBytes, NULL);
DecompressData.uStructSize = sizeof(DECOMPRESSDATA);
DecompressData.pBuffer = pBuf; //pointer to raw data
DecompressData.nWidth = TempFileInfo.Width; //Width of uncompressed strip/tile
DecompressData.nHeight = nRowsPerStrip; //Height of uncompressed strip/tile
if (nIndex == (nMaxIndex - 1 ))
{
//fewer rows per strip
DecompressData.nHeight = TempFileInfo.Height - (nMaxIndex - 1) * nRowsPerStrip;
}
DecompressData.uOffset = 0; //Offset of strip relative to buffer
DecompressData.nBufferSize = StripSizes[nIndex];//Size of strip after compression
DecompressData.nCol = 0; //Col offset of tile
DecompressData.uFlags = DECOMPRESS_CHUNK_COMPLETE;
DecompressData.nReserved1 = 0; //Reserved for future use
nRet = L_DecompressBuffer( hDecompress, &DecompressData);
if(nRet != SUCCESS)
return nRet;
free(pBuf);
}
CloseHandle(fd);
//******************************
//*** L_StopDecompressBuffer
//******************************
L_StopDecompressBuffer(hDecompress);
// Bitmap contains the uncompressed bitmap
nRet = L_SaveBitmap(MAKE_IMAGE_PATH(TEXT("Uncompressed.bmp")), &Bitmap, FILE_BMP, 24, 0, NULL);
L_FreeBitmap(&Bitmap);
return nRet;
}