4

I'm aware of two methods in order to access to all the pixels in a QImage called img.

Method 1

for (int y = 0; y < img.height(); y++) {
    QRgb *line = (QRgb *) img.scanline(y);
    for (int x = 0; x < img.width(); x++) {
        // line[x] has an individual pixel
        line[x] = QColor(255, 128, 0).rgb();
    }
}

Method 2

QRgb *st = (QRgb *) img.bits();
quint64 pixelCount = img.width() * img.height();

for (quint64 p = 0; p < pixelCount; p++) {
    // st[p] has an individual pixel
    st[p] = QColor(255, 128, 0).rgb();
}

I'm keen on using the second method as it only involves one loop, but am also concerned about any possible overflows on pixelCount if processing a "big enough" image.

Question

What is the most scalable way to iterate over all pixels stored in a QImage? By scalable I mean that it will still work no matter what the image dimensions (width and height) are.

NOTE: I'm already aware there are "physical" limits in terms of memory usage. I just want to know whether both methods are capable of reaching such a limit.

logo_writer
  • 283
  • 2
  • 10
  • "but am also concerned about any possible overflows on pixelCount if processing a "big enough" image". I think when processing an image larger than 4294967296x4294967296, you run into other limitations first :) – Frank Osterfeld Jan 28 '16 at 06:26
  • 1
    Both methods detach from any other QImage instances that implicitly share the data. So it might create an additional copy if you don't take care. Otherwise I don't see a scalability problem (both are O(n) where n is the number of pixels). Do some benchmarks to see if it makes a difference. Use qRgb() to get a QRgb value directly without creating those temporary QColor instances. – Frank Osterfeld Jan 28 '16 at 06:33
  • 1
    As far as I know `QImage` has some limitations. I.e. it is impossible to load an image larger than 32000x32000 px. I don't know what kind of images your application will work with. Are they real, or your question is simply theoretical? – vahancho Jan 28 '16 at 07:50
  • Thank you for all your responses! The question is at the limits of the theoretical/practical. That is: the method by itself *must not add a limit* (other than the stated: memory or `QImage` specific). From the comments I deduce both methods are equal, even though the first one uses two nested loops. – logo_writer Jan 28 '16 at 08:38
  • @FrankOsterfeld I'll make the benchmarks. I thought the second one was better because of using just one loop. – logo_writer Jan 28 '16 at 08:40
  • 1
    Assuming scanLine isn't expensive in any way, I can't see how that would make a noticable difference, given that the total number of operations is about the same. – Frank Osterfeld Jan 28 '16 at 12:46

0 Answers0