1

In my application I have to convert an image (image could be in any of the format - PNG, JPEG, BMP etc) to DIB.

This is what I am doing right now,

  1. Create a movable global memory using GlobalAlloc(GHND,0)
  2. Create a stream on this global memory using CreateStreamOnHGlobal() API
  3. Save the given image on to the stream using the encoder for BITMAP
  4. Then I create another buffer and copy the contents from global memory to the newly created buffer skipping the bitmap file header. This makes the newly allocated buffer a DIB data
  5. Finally release global memory using GlobalFree() and release the stream.

What I am not comfortable here is, there are a lot of memory being allocated here to convert an image to DIB. I would like to know if there are any other way to achieve this using only minimum amount of memory allocations and de-allocations.

  • If your images are all the same size, allocate a static buffer instead of a dynamic one. – Thomas Matthews Feb 28 '14 at 14:04
  • @ThomasMatthews Unfortunately the images are not of same size always. User selects the image and user can select any image he/she wish. – Karthik Kalyanasundaram Feb 28 '14 at 15:44
  • 2
    No, that's never going to work, you actually have to *decode* the data in the image file. Not ever code you should write yourself, GDI+ and WIC are standard on Windows. They hand you the dib on a silver platter, take care of memory management as well. – Hans Passant Feb 28 '14 at 16:59
  • Why are you worrying about memory? Does your system run in real mode? – stark Feb 28 '14 at 17:32
  • Why do you need a memory buffer containing a packed DIB anyway? There's almost certainly a better way to do this. – Esme Povirk Mar 01 '14 at 01:37
  • @stark this code is part of the runtime of a scripting engine. So I should ensure it consumes minimum amount of memory or in otherwords, it should be as much optimized as possible. – Karthik Kalyanasundaram Mar 01 '14 at 01:57
  • @VincentPovirk The complete application is already built and it used third party imaging libraries. I had to remove 3rd party lib dependency with this GDI+ implementation. Existing code needs DIB in memory buffer. – Karthik Kalyanasundaram Mar 01 '14 at 01:58
  • @HansPassant I already looked up in GDI+ couldn't find any useful function which gets me decoded data. I don't know if I overlooked anything. Will have look in to WIC and GDI+ again. – Karthik Kalyanasundaram Mar 01 '14 at 02:00
  • @KarthikKalyanasundaram Any reasonable lib will mmap the files. The process will show much virtual mem allocated, but it will only use physical mem when it touches the data. So again, why are you worrying about memory? – stark Mar 01 '14 at 23:47

0 Answers0