PDA

View Full Version : Image reading and writing line by line



Astrologer
28th April 2010, 12:52
Hi there. Would you please, guys help me out? I have got an image and sometimes a huge one. In order to avoid memory problems I would like to read an image line by line and after having processed each line I would want to write it to an output image.
The problem is I need to use QImage::scaled() function (or similar which can provide efficient scaling procedure which I need use) that suits me fine.
First guess is to divide input image into number of QImages (let's say, number of lines of input image) and accomplish scaling procedure on each of them. But how will I write all of them into single output image? There may be another solution or more suitable Q-class I'm not aware of. Anyway you're welcome to give any input on this. Thank you so much in advance.

Kumosan
28th April 2010, 13:12
I doubt you can do that. Images are usually compressed so there is no simple way to read just a line. I might be wrong, I am not an expert on images, but I think to get a line of a jpeg there is no other way to fully load it into memory and uncompress it there resulting in an even larger image in memory.

Astrologer
28th April 2010, 13:17
Thank you for help. Actually I can use QImageReader to split it as:


QImageReader imagereader(input_jpg_name);
imagereader.setScaledSize(QSize(500,500));
QImage img1 = imagereader.read();

avoiding memory wasting. Any other suggestions, guys? :)

Kumosan
28th April 2010, 13:24
From the QImageReader docs:
Depending on the underlying support in the image format, this can save memory and speed up loading of images.

So even if you can get a scaled image from QImageReader, it does not necessarily mean that you can save memory.

Astrologer
28th April 2010, 13:33
I see your point. That's exactly the problem that I am longing for solution which will help me. I have an image let's say 20000x20000. If not up to QImageReader I can't see a way QT can handle it. If I feed the image directly to QImage it returns zero width and zero height purpotedly because of the size. If I do it with example written above, it works and saves a thumbnail size of 500x500. Memory can be saved as I pointed out by cropping the big one into slices size of one pixel, for example.

Astrologer
28th April 2010, 20:11
Hey, guys. Ain't there any idea? :confused:

SixDegrees
28th April 2010, 21:48
Qt doesn't really give you access to the primitive graphics and image file functions you need to do this properly. If your solution works for your purposes, then you're done. But as noted, there are potential problems with it.

Most modern image formats use some sort of compression. In JPEG, images are processed in 8x8 blocks, so you would need access to the raw JPEG library to read strips, which would decompress into more than a single line. Other formats, like JPEG2000, use wavelet transforms for compression, and the spatial information in the image is scattered throughout the compressed data, making retrieval of individual lines or blocks problematic.

For similar reasons, your procedure of removing blocks, reducing them and then stitching them back together to produce a single, reduced image may produce "seams" around the edges of your individual tiles, because the compression algorithm relies on a broad "neighborhood" around each pixel and behaves differently near edges. You'll want to inspect your images carefully after reassembly to see if these are visible, or if they are objectionable.

A better solution would be to use a tool like ImageMagick, an image processing toolkit that can be used from either the command line or through a C++ interface. It allows you to set the maximum amount of memory an operation will consume, and is careful not to exceed that limit, although more memory results in greater execution speed. But when dealing with really huge images, it is often the best solution available. It's scaling algorithms are top-notch, as well, and their advantages and shortcomings are carefully documented. Plus, it's free.

Astrologer
29th April 2010, 08:15
Yes, it seems to be a great library, though I just skimmed over it. I am going to plough on with the library. Thank you all, guys.