views:

620

answers:

2

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?

+2  A: 

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.

j_random_hacker
Thanks for the answer,I have already tried this method, it improves performance significantly but it is not yet fast enough.I am not sure that I am doing it correctly, but I ought to add an Application.DoEvents() call to my C# code to make my thumbnail viewer control (filled with thumbnails created in some other thread) invalidated and the whole process still causes scanner to wait for a moment (less than a second) after scanning 5+ papers. Comparing to a commercial application shipped with the device (which I'm trying to clone its speed and add features I need to it) it is not fast enough.
hamid reza
Unfortunately I'm not familiar with C# (or TWAIN for that matter) but I can't understand how queueing results could slow down the scanner. I assume that you have set the priority of the thumbnail conversion thread/process to below that of the acquisition thread/process? That's one possible cause to consider.
j_random_hacker
Thanks again,I'm not sure about it, it seems to me that c# threads other than main thread always have a lower priority than main thread. However, next answer (greyfade) seems to be a more functional solution to what I need.
hamid reza
No problem. In that case I suggest you +1 and/or mark greyfade's answer as correct.
j_random_hacker
+4  A: 

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.

greyfade
Thanks, it seems to be what I wanted.
hamid reza
I've managed to test EPEG, for anybody interested in doing the same, I should mention that it seems EPEG library is now been removed from enlightenment source code, so you should search for it inside its old source codes, for example here: http://download.enlightenment.org/snapshots/2008-01-25/ .
hamid reza
For TIFF images, I used this code: http://www.koders.com/c/fidFAE1882A0596B9D224D831B852AE9891D0154D6D.aspx .It is not as fast as EPEG, but gets the work done.
hamid reza