Writing an image as it's generated

  • Hi,

    I'm doing some image processing using Qt. I have a data source that provides, from (0,0) to (width, height), the necessary data to write an image: the ordered pixels and their R,G,B values.

    My code currently uses libpng, that does the following tasks:

    1. Create a png file
    2. Write its header
    3. As each full line is received from my data source, it's appended to my output file
    4. Closes the file if there's no more data to be received.

    I want to remove libpng from my project and use only Qt, in order to avoid dependencies. But I'm unable to find a way, using something like a QImage, to write every image line as it's read. I tried to use QImage, but it stores image data in memory and only writes the image data when I call QImage::save(). If I call save() every time I read a new line, the process will be slow because all the lines before will be written again, right?

    So, is there some way to append a new line to an image file when I get the values for its pixels?

  • You could just use a plain vanilla QFile, I suppose. But I would stop and ask why. Your scenario is strange, and I wonder if there's a better approach for what you're trying to accomplish, perhaps. What is the benefit to having the data saved as it's being created, rather that it being buffered first?

  • Removing libpng won't help you. Libpng is already linked into Qt itself by default, so you can use that version of it directly as well.

  • @mlong, I just want to parallelize the task.

    @Andre, I know that Qt uses libpng, but I'd like to use QImage so I can use different output formats in a easier way, not only PNG files. And I know that Qt uses libpng and that libpng is linked into Qt, but there isn't a way to access the libpng functions (like png_write_row()) that are linked to Qt, there is?

  • If you want to parallelize, you should just create your full QImage and then pass that to a thread that will do the writing, hence leaving your main thread available to process a new image.
    Your case is indeed strange and I can't see how you can expect a real performance gain in such a design.

Log in to reply