?
Solved

Memory Management in JPEG to BMP conversion using Intel IJL Library

Posted on 2005-04-04
4
Medium Priority
?
1,344 Views
Last Modified: 2013-12-03
Iam using Intel IPP library 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<BITMAPFILEHEADER*>(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<BITMAPINFOHEADER*>(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<BYTE*>(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
Comment
Question by:sendhil_an
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 48

Expert Comment

by:AlexFM
ID: 13704267
First try this function with unmanaged C++ client.
0
 
LVL 96

Assisted Solution

by:Bob Learned
Bob Learned earned 150 total points
ID: 13706401
Is there a reason why you can just convert a Bitmap to JPEG natively:

public class Graphics_Library
{
 
public enum ImageType
 {
   Unknown,
   Bmp,
   Emf,
   Exif,
   Gif,
   Jpg,
   Icon,
   Png,
   Tiff,
   Wmf
 }

 public static void ConvertImage(string file, ImageType type)
 {
   Image imageCurrent = Image.FromFile(file);
   Imaging.ImageFormat format;
   if (type == ImageType.Bmp)
   {
     format = Imaging.ImageFormat.Bmp;
   } else if (type == ImageType.Emf) {
     format = Imaging.ImageFormat.Emf;
   } else if (type == ImageType.Exif) {
     format = Imaging.ImageFormat.Exif;
   } else if (type == ImageType.Gif) {
     format = Imaging.ImageFormat.Gif;
   } else if (type == ImageType.Jpg) {
     format = Imaging.ImageFormat.Jpeg;
   } else if (type == ImageType.Png) {
     format = Imaging.ImageFormat.Png;
   } else if (type == ImageType.Tiff) {
     format = Imaging.ImageFormat.Tiff;
   } else if (type == ImageType.Wmf) {
     format = Imaging.ImageFormat.Wmf;
   }
   string fileName = Path.GetFileNameWithoutExtension(file);
   string filePath = Path.GetDirectoryName(file);
   file = Path.Combine(filePath, fileName) + "." + type.ToString.ToLower;
   imageCurrent.Save(file, format);
 }
}

Bob

0
 

Author Comment

by:sendhil_an
ID: 13707313
yeah, i could convert JPEG to BMP without using native code. But it takes more CPU to perform the operation. I need to do this conversion many times a second and there will be multiple instances of it. Intel JPEG library has been optimized to do it.

I will try the function from an  unmanged C++ client

sendhil
0
 
LVL 48

Accepted Solution

by:
AlexFM earned 1350 total points
ID: 13707576
If your unmanaged test will be successful, post here unmanaged C++ code which calls JpegToBitmap function. Having working unmanaged version it is always possible to translate it to C#.

Loking at your code now, I wonder what is ipJpegPtr which is passed directly to JpegToBitmap?
Also,
int *ptrBmpSize = &bmpSize;
looks strange. You need ref parameter instead of it. There is no need in unsafe prigramming. Be careful with bool return type. It is char in MS VC++ and can have other size in other compilers. It's better to use int.

int JpegToBitmap(BYTE *ipJpegPtr, DWORD ipJpegBufferSize, BYTE *ipBitmapPtr, int *ipPtrBmpSize, int ipDestWidth, int ipDestHeight)

should be translated to C# as:

public static extern int JpegToBitmap(IntPtr ipJpegPtr, Int32 ipJpegBufferSize, IntPtr ipBitmapPtr, ref int BmpSize, int destWidth, int destHeight);



0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

IP addresses can be stored in a database in any of several ways.  These ways may vary based on the volume of the data.  I was dealing with quite a large amount of data for user authentication purpose, and needed a way to minimize the storage.   …
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
Suggested Courses

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question