1
Vote

QuantizeImage uses up to 120 mb in memory

description

When i ran a small test on a 4 mb bitmap image, QuantizeImage function used up to 120 mb in memory.
why is that ? how can it be fixed ?

Thanks for this fast and easy to use library.


UPDATE
CalculateMoments used up to 42 mb
SplitData used 78 mb which makes the total 120 mb

comments

mwrock wrote Aug 13, 2014 at 5:33 AM

Hi there,

Thanks for asking. nquant was designed to optimize for quality and not performance or efficiency although I tried to make nquant as performant and efficient as possible. What you are seeing in memory are indexed data sructures for all bytes and colors held in the original image. The reason that the memory footprint is so much lager than the original image file is:
  1. A bitmap is compressed and can be several times smaller than the bytes that make up the actual image and nquant deals with the raw uncompressed data.
  2. nquant effectively creates indexes into the image data. So just like adding indexes to a database table will cause the physical table to grow, nquant's indexed views of the data and its color maps are likely to be much larger than the actual index. This is impacted by the number of colors in the image. A huge image with 2 colors wont take much space but a 1000 pixel image with 1000 different colors could be quite large.
The problem I had tht I designed nquant to solve was that there was no free quantizer available that could optimize my images without distorting them to an unacceptable degree. Some of the C based tools were OK but not good enough for me to publish and the .net sample code produced horrible looking images.

So I did not care so much if memory was relatively high. I only had to convert a relatively sized batch once. I just needed them to look good. The WU algorithm nuant uses is pretty memory intensive and for my purposes I was fine with that.

That all said, if someone can optimize the algorithm without sacrificing image quality (and I'm sure there is room for optimization) I'd be delighted to take the changes.

I hope that all makes sense and addresses your question.

HMVC wrote Aug 13, 2014 at 10:41 AM

Thanks for the reply.
I will run some tests to see if it will crash under certain conditions.

CaptCrunch wrote Nov 9, 2015 at 12:01 PM

I can get a Memory exception quite repeatedly. It uses around 1GB of memory.

Exception of type 'System.OutOfMemoryException' was thrown.

at nQuant.ColorData..ctor(Int32 dataGranularity, Int32 bitmapWidth, Int32 bitmapHeight) in c:\dev\nquant\nQuant.Core\ColorData.cs:line 13
at nQuant.WuQuantizerBase.BuildHistogram(Bitmap sourceImage, Int32 alphaThreshold, Int32 alphaFader) in c:\dev\nquant\nQuant.Core\WuQuantizerBase.cs:line 94
at nQuant.WuQuantizerBase.QuantizeImage(Bitmap image, Int32 alphaThreshold, Int32 alphaFader) in c:\dev\nquant\nQuant.Core\WuQuantizerBase.cs:line 28
at nQuant.WuQuantizerBase.QuantizeImage(Bitmap image) in c:\dev\nquant\nQuant.Core\WuQuantizerBase.cs:line 22