14

Are there any known size/space limitation of QPixmap and/or QImage objects documented? I did not find any useful information regarding this. I'm currently using Qt 4.7.3 on OSX and Windows. Particulary I'm interested in:

  • Width/Height limits?
  • Limits depending on color format?
  • Difference between 32/64 bit machines?
  • Difference regarding OS?

I would naively suspect that memory is the only limitation, so one could calculate max size by

width x height x byte_per_pixel

I assume that there is a more elaborate rule of thumb; also 32 bit machines may have addressing problems when you run into GB dimensions.

In the end I want to store multiple RGBA images of about 16000x16000 pixel in size and render them using transparency onto each other within a QGraphicsScene. The workstation available can have a lot of RAM, let's say 16GB.

tl;dr: What size limits of QImage/QPixmap are you aware of, or where can I find such information?

Edit: I'm aware of the tiling approach and I'm fine with that. Still it would be great to know the things described above.

Thanks!

Angie Quijano
  • 4,167
  • 3
  • 25
  • 30
grefab
  • 1,997
  • 2
  • 22
  • 32

5 Answers5

14

Both are limited to 32767x32767 pixels. That is, you can think of them as using a signed 16-bit value for both the X and Y resolution.

No axis can ever exceed 32767 pixels, even if the other axis is only 1 pixel. Operating system "bitness" does not affect the limitation. The underlying system may run into other limits, such as memory as you mentioned, before such a huge image can be created.

You can see an example of this limitation in the following source code: http://git.zx2c4.com/qt/plain/src/gui/image/qpixmap_x11.cpp

if (uint(w) >= 32768 || uint(h) >= 32768) {
    w = h = 0;
    is_null = true;
    return;
}
Charles Burns
  • 10,310
  • 7
  • 64
  • 81
  • This may be correct for `QPixmap`, but for the limitations of `QImage` see @povman's answer. – Thorbjørn Lindeijer Apr 06 '15 at 20:30
  • Could somebody elaborate on what are "axis"? I have a rendering issue when scaling an image that is caused by this but my results as to when the tearing starts are not consistent with 32767. I can scale a 1000000x200 image 41000x before it fails to correctly render the qpixmap. – quimnuss May 07 '18 at 13:13
  • Also, I've seen the snippet comes from the `QPixmap::fromImage` function; Where is the limitation set if the pixmap is created with the constructor that takes a filename? – quimnuss May 07 '18 at 13:36
8

Building on the answer by @charles-burns, here is relevant source code for QImage:

QImageData *d = 0;

if (format == QImage::Format_Invalid)
    return d;

const int depth = qt_depthForFormat(format);
const int calc_bytes_per_line = ((width * depth + 31)/32) * 4;
const int min_bytes_per_line = (width * depth + 7)/8;

if (bpl <= 0)
    bpl = calc_bytes_per_line;

if (width <= 0 || height <= 0 || !data
    || INT_MAX/sizeof(uchar *) < uint(height)
    || INT_MAX/uint(depth) < uint(width)
    || bpl <= 0
    || height <= 0
    || bpl < min_bytes_per_line
    || INT_MAX/uint(bpl) < uint(height))
    return d;                                        // invalid parameter(s)

So here, bpl is the number of bytes per line, which is effectively width * depth_in_bytes. Using algebra on that final invalid test:

  • INT_MAX/uint(bpl) < uint(height)
  • INT_MAX < uint(height) * uint(bpl)
  • INT_MAX < height * width * depth_in_bytes

So, your image size in total must be less than 2147483647 (for 32-bit ints).

Luke Worth
  • 570
  • 3
  • 18
2

I actually had occasion to look into this at one time. Do a search in the source code of qimage.cpp for "sanity check for potential overflows" and you can see the checks that Qt is doing. Basically,

  • The number of bytes required (width * height * depth_for_format) must be less than INT_MAX.
  • It must be able to malloc those bytes at the point you are creating the QImage instance.
Dave Mateer
  • 17,608
  • 15
  • 96
  • 149
1

Are you building a 64 bit app? If not, you are going to run into memory issues very quickly. On Windows, even if the machine has 16GB ram, a 32 bit process will be limited to 2GB (Unless it is LARGEADDRESSAWARE then 3GB). A 16000x16000 image will be just under 1 GB, so you'll only be able to allocate enough memory for 1, maybe 2 if you are very lucky.

With a 64 bit app you should be able to allocate enough memory for several images.

Roland Rabien
  • 8,750
  • 7
  • 50
  • 67
0

When I try to load JPEG with size 6160x4120 to QPixmap I get this warning: "qt.gui.imageio: QImageIOHandler: Rejecting image as it exceeds the current allocation limit of 128 megabytes" and returns empty QPixmap.

This seems to be the most strict constraint I have found so far.

There is however an option to increase this limit with void QImageReader::setAllocationLimit(int mbLimit).