Faster way to create images?

P

pedrito

I've got an app that downloads images off the internet using anywhere from
1-20 threads concurrently (though generally around 4 threads).

I have a preview page that shows the images as they're downloading. The
threads that do the downloading trigger an event every 32K (the size of the
chunks I receive), at which point the images are drawn in the Preview page.

To create the images, I create a memory stream from the data and then use
Image.FromStream(). This works fine, but it's VERY cpu intensive. The images
being downloaded vary in size, but figure about 1000x1500 as average.

I tried using FreeImage and the FreeImage.NET wrapper, but that was even
more CPU intensive. Most of the time appears to have been spent in
FreeImage.GetBits() which I use to get the bits to copy into the DibSection,
though it's hard to be sure. pInvoke stuff seems to confuse nProf a bit.

Anyway, I'd love to find a less CPU intensive way to do this. Obviously,
what I'm showing is scaled down copies of these images in the PreviewPage,
so I don't really need the full-size image, but as far as I know, there's no
way to load the image scaled such that uses less CPU.

Anyone have any ideas for how I can improve this? Any open source libraries
that are maybe less CPU intensive than FreeImage?

Thanks.
 
M

Michael Phillips, Jr.

there's no way to load the image scaled such that uses less CPU.

Working with memory streams is the most efficient way to work with image
data.

What you need is a "decimate" algorithm that creates a thumbnail or
decimated image in memory
while the images are downloading 32K at a time. Create a memory stream from
the decimated image
and display it instead. If you are working with bitmaps, then a bitmap's
memory stream consists of:
1) BITMAPFILEHEADER
2) BITMAPINFOHEADER
3) ColorTable or Bitfield array, if any
4) Image bits

It is common to pre-blur the image so that the decimated image looks better.
You can also pick an algorithm that is fast(i.e., less CPU intensive) with a
trade off in image quality.
You could also reduce the bit depth and create a palette to save even more
resources.
 
P

pedrito

Unfortunately, the images can be in any of several formats, including .PNG,
..GIF and .JPG. .BMPs far less often. This is why I need a more general
library since, coding them all from scratch would be, well, re-inventing the
wheel for the millionth time.

One bottleneck is that I'm creating and scaling the images on the GUI thread
and I might do that on separate threads. Threading is already a bit of a
pain as it is, but if I can't come up with a less CPU intensive way of
creating the thumbnails, then that might help for some CPUs.

Pete
 
M

Michael Phillips, Jr.

Image formats such as .PNG, .GIF and .JPG download off the internet quickly
because they are compressed.
They must be decoded before they can be decimated.

There are image libraries that work with memory streams. There may be
existing license free libraries that also
decimate in memory. It is a hot research topic at Universities. As you
said, why reinvent the wheel.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top