Intel® Integrated Performance Primitives
Deliberate problems developing high-performance vision, signal, security, and storage applications.
6719 Discussions

Memory Management in JPEG to BMP conversion using Intel IJL Library

sendhil
Beginner
251 Views

Iam using Intellibrary for converting a JPEG to BMP. This function is in a VC++ dll and it is called from C#. Iam passing a chunk of memory for this conversion to the unmanaged code. Each time the JpegToBitmap function is called, the page file/memory usage keeps on increasing and crashes the application. How do i take care of this.

The call to the function looks like this.
C# Code
************************
[System.Runtime.InteropServices.DllImport("**.dll")]
public unsafe static extern bool JpegToBitmap(
byte *ipJpegPtr,
UInt32 ipJpegBufferSize,
byte *ipBitmapPtr,
int *ipPtrBmpSize,
int destWidth,
int destHeight);

.....
.....
byte[] bmpBytes = new byte[1024 * 1024*5];
GCHandle gcbmpBytes = GCHandle.Alloc(bmpBytes, GCHandleType.Pinned);
byte *bmpPtr = (byte *)gcbmpBytes.AddrOfPinnedObject().ToPointer();
int *ptrBmpSize = &bmpSize;
JpegToBitmap(jpegPtr, (uint)localImageBytes.Length, bmpPtr, ptrBmpSize, this.Width, this.Height);
************************


The VC++ function which does the conversion
*******************************
bool JpegToBitmap(
BYTE *ipJpegPtr,
DWORD ipJpegBufferSize,
BYTE *ipBitmapPtr,
int *ipPtrBmpSize,
int ipDestWidth,
int ipDestHeight) {


int scaleFactor;


IJLERR jerr;
JPEG_CORE_PROPERTIES jcprops;
DWORD dwWholeImageSize;
DWORD dib_line_width;
DWORD dib_pad_bytes;

IJLIOTYPE readMode;

if(IJL_OK == (jerr = ijlInit(&jcprops))) {

jcprops.JPGFile = NULL;
jcprops.JPGBytes = ipJpegPtr;
jcprops.JPGSizeBytes = ipJpegBufferSize;


if(IJL_OK == (jerr = ijlRead(&jcprops, IJL_JBUFF_READPARAMS)))
{
switch(jcprops.JPGChannels) {
case 1:
{
jcprops.JPGColor = IJL_G;
jcprops.DIBColor = IJL_RGB;
jcprops.DIBChannels = 3;
break;
} // case 1

case 3:
{
jcprops.JPGColor = IJL_YCBCR;
jcprops.DIBColor = IJL_RGB;
jcprops.DIBChannels = 3;
break;
} // case 3

default:
{
// This catches everything else, but no
// color twist will be performed by the IJL.
jcprops.JPGColor = IJL_OTHER;
jcprops.DIBColor = IJL_OTHER;
jcprops.DIBChannels = jcprops.JPGChannels;
break;
} // default
} // switch

readMode = IJL_JBUFF_READWHOLEIMAGE;


scaleFactor = 1;

// Compute DIB padding
dib_line_width = jcprops.JPGWidth * 3;
dib_pad_bytes = IJL_DIB_PAD_BYTES(jcprops.JPGWidth /* / scaleFactor*/, 3);
dwWholeImageSize = ( dib_line_width + dib_pad_bytes ) * jcprops.JPGHeight /* / scaleFactor*/;

// Allocate memory to hold the decompressed image data.
if(1) {
// Set up the info on the desired DIB properties.
jcprops.DIBWidth = jcprops.JPGWidth /* / scaleFactor*/;
jcprops.DIBHeight = jcprops.JPGHeight /* / scaleFactor*/;
jcprops.DIBPadBytes = 0;

if(1) {
BITMAPINFOHEADER* memBMIH;
BITMAPFILEHEADER* memBMFH;

memBMFH = reinterpret_cast(ipBitmapPtr);
memBMFH->bfType = 'MB';
memBMFH->bfSize = dwWholeImageSize + sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER);
memBMFH->bfReserved1 = 0;
memBMFH->bfReserved2 = 0;
memBMFH->bfOffBits = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER);


memBMIH = reinterpret_cast(ipBitmapPtr + sizeof(BITMAPFILEHEADER));
memBMIH->biSize = sizeof(BITMAPINFOHEADER);
memBMIH->biWidth = jcprops.JPGWidth /* / scaleFactor*/;
memBMIH->biHeight = -jcprops.JPGHeight /* / scaleFactor*/;
memBMIH->biPlanes = 1;

memBMIH->biBitCount = 24;
memBMIH->biCompression = BI_RGB;
memBMIH->biSizeImage = 0;
memBMIH->biXPelsPerMeter = 0;
memBMIH->biYPelsPerMeter = 0;
memBMIH->biClrUsed = 0;
memBMIH->biClrImportant = 0;

// Set up the info on the desired DIB properties.
jcprops.DIBWidth = jcprops.JPGWidth /* / scaleFactor*/;
jcprops.DIBHeight = jcprops.JPGHeight /* / scaleFactor*/; // Implies a bottom-up DIB.
jcprops.DIBChannels = 3;
jcprops.DIBColor = IJL_BGR;
jcprops.DIBPadBytes = dib_pad_bytes;
jcprops.DIBBytes = reinterpret_cast(ipBitmapPtr + sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER));

if(IJL_OK == (jerr = ijlRead(&jcprops, readMode)))
{
*ipPtrBmpSize = memBMFH->bfSize;
} // ijlread whole image
}// create compatible DC
else
{
//logerror("create Compatible DC failed");
} // else create compatible dc
} // else lptemp create
} // ijl read params
ijlFree( & jcprops);
}
return true;
}

0 Kudos
1 Reply
Vladimir_Dudnik
Employee
251 Views
Hi,
there is recomendation from our expert regarding:
JpegToBitmap(jpegPtr, (uint)localImageBytes.Length, bmpPtr, ptrBmpSize, this.Width, this.Height);
it is complicated code, you should not forget to make gcbmpBytes.Free().
To make it simple, you can use

byte[] bmpBytes = new byte[1024*1024*5];

fixed( byte* bmpPtr = bmpBytes )
{
JpegToBitmap(, bmpPtr,);
}

Regards,
Vladimir

0 Kudos
Reply