Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Having an image file buffer in memory, what is the fastest way to create its thumbnail?

Trying to create a an image acquiring application optimized for a fast scanner (which can provide up to 6 compressed images [color+gray+binary][front+rear] for each paper at speed of 150 ppm) I have some speed issues. Using TWAIN technology and memory buffer transfer mode (TWSX_MEMORY) I receive image buffer (as JPEG or TIFF file loaded in memory) from scanner and save it to my application destination path. If I do not want to create thumbnails, my application causes no speed loss for the scanner, but if I want to, due the way I do it (saving buffer into a file in my C++ TWAIN handling dll, notifying my .NET host application with destination file path using a function pointer, opening the image file in C# and creating the thumbnail image), my application causes extreme speed loss to scanning speed. I tried some optimizations such as performing loading phase in a separate thread and sending unmanaged image file buffer to .NET host and trying to load it in an unsafe context (UnmanagedMemoryStream) and creating thumbnail. But it did not improve the speed significantly. So my question is :

Having an image file buffer in memory (e.g. 24 bit JPEG compressed without embeded thumbnail), is there a fast direct way to create a thumbnail image from it? What do you suggest as fastest method for creating thumbnails in this case?

like image 713
hamid reza Avatar asked Aug 22 '09 15:08

hamid reza


2 Answers

If it's a JPEG image, you can simply discard most of the DCT data, and create a thumbnail at a power of two size, using only the DCT coefficients.

If you can find the sources for it, take a look at EPEG from the Enlightenment project. It does exactly what you're looking for with JPEG files, entirely without decoding or decompressing the image. The source code would be very instructive.

For other image formats, it's not so simple - you'll need to decode and render the image to a memory buffer, then perform your own scaling. The CImg and boost::GIL libraries can assist with that.

like image 79
greyfade Avatar answered Sep 16 '22 21:09

greyfade


I take it that the problem is that it takes longer to convert an image to a thumbnail than to acquire the image in the first place, correct?

Although a faster thumbnail conversion program might fix the problem for you, it might not be sufficient for someone with a slower computer. Instead, I suggest creating a queue of images to be converted to thumbnails -- i.e. you have one thread (or process) which adds scanned images to the queue, and another thread/process that removes images from that queue and creates thumbnails from them. This way the relative speeds of the two operations don't matter.

like image 25
j_random_hacker Avatar answered Sep 20 '22 21:09

j_random_hacker