0

I am newbie in Qt & image storage & manipulation. Please bear with me if my question is silly.

Problem:
I am required to store some QImages on the RAM while my Qt application is running. My app runs only on android. Say that I am required to store 100 QImages of size 1000*1000. But the RAM might not support such a huge memory requirement since there is a heap memory limit for apps on Android.

Attempted solution:
Now that I have to store 100 QImages which a huge requirement given the size, I am trying to do something like this to reduce the main memory footprint of these QImages.

SetQImageToList(QImage & img) {

    QByteArray theByteArray;
    QBuffer buffer(& theByteArray);
    buffer.open(QIODevice::WriteOnly);
    img.save(&buffer, "JPG");
}

I can get the same QImage back doing following as I learn:

QImage::fromData(theByteArray, "JPG")

I can store & read back the QImages. Code works fine for storing & retrieval of the QImages.

Question:
Does this technique guarantee that theByteArray stored is a lower footprint than the actual QImage? What are the downsides of doing this? Will I incur a performance loss while I read & write into theByteArray then QImage & vice versa?

TheWaterProgrammer
  • 7,055
  • 12
  • 70
  • 159
  • Memory or CPU? Use on buffer per (de)compress worker thread. Use worker thread(s) to mitigate the performance loss/ responsiveness problems and signals/slots to communicate with UI and other threads. How many worker threads to use is something to explore. Find out about how many CPU threads/heads available on the platform. – Alexander V Dec 19 '17 at 18:48
  • @AlexanderVX My concern here is "memory" or you can call it "RAM usage". why do you bring the CPU discussion into it? I have clearly mentioned the aspect of heap memory limitation which of course concerns RAM usage – TheWaterProgrammer Dec 19 '17 at 19:42
  • @AlexanderVX also I understand that I might increase CPU instructions heavily by doing this `byteArray` to `QImage` & back. But questions seeks to answer one important question : **Does using QByteArray from each QImage reduce memory footprint of my app at runtime?** – TheWaterProgrammer Dec 19 '17 at 19:49
  • I deal with image processing for quite a few time. Even with Android platform now we need to prevent object from being multiplied in memory more than avoiding individual buffer. It does not really matter whether QByteArray was used as an intermediary especially if required. You don't have enough RAM? Make many images being processed via one buffer one after another. And if that is too slow, make more threads. You talk about many big images? BTW Qt in many cases uses same buffer for different objects. Look at Qt::bits() doc article. – Alexander V Dec 19 '17 at 19:56
  • If so, I meant you need to decide between memory hit and CPU hit. So, I did not even try to help answer the question. Your task seems to be more doable with CPU updating images in memory from some storage when requested. And your code should not request more than currently visible. Maybe consider some image cache as well. – Alexander V Dec 20 '17 at 20:10

0 Answers0