From bb1c6aab9e0d8c2fd2979d5cb80ee50395cc3db4 Mon Sep 17 00:00:00 2001 From: Mirco Miranda Date: Mon, 17 Feb 2025 08:54:49 +0100 Subject: [PATCH] Added pixel limit detected by experimental tests --- README.md | 2 +- src/imageformats/jp2.cpp | 12 ++++++++++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index e5c35f4..ee5607c 100644 --- a/README.md +++ b/README.md @@ -213,7 +213,7 @@ plugin ('n/a' means no limit, i.e. the limit depends on the format encoding). - EPS: n/a - HDR: n/a (large image) - HEIF: n/a -- JP2: 300,000 x 300,000 pixels +- JP2: 300,000 x 300,000 pixels, in any case no larger than 2 gigapixels - JXL: 262,144 x 262,144 pixels, in any case no larger than 256 megapixels - JXR: n/a, in any case no larger than 4 GB - KRA: same size as Qt's PNG plugin diff --git a/src/imageformats/jp2.cpp b/src/imageformats/jp2.cpp index 81b346c..7ea1350 100644 --- a/src/imageformats/jp2.cpp +++ b/src/imageformats/jp2.cpp @@ -27,6 +27,13 @@ #define JP2_MAX_IMAGE_HEIGHT JP2_MAX_IMAGE_WIDTH #endif +/* *** JP2_MAX_IMAGE_PIXELS *** + * OpenJPEG seems limited to an image of 2 gigapixel size. + */ +#ifndef JP2_MAX_IMAGE_PIXELS +#define JP2_MAX_IMAGE_PIXELS std::numeric_limits::max() +#endif + /* *** JP2_ENABLE_HDR *** * Enable float image formats. Disabled by default * due to lack of test images. @@ -332,6 +339,11 @@ public: return false; } + if (qint64(width) * qint64(height) > JP2_MAX_IMAGE_PIXELS) { + qCritical() << "Maximum image size is limited to" << JP2_MAX_IMAGE_PIXELS << "pixels"; + return false; + } + // OpenJPEG uses a shadow copy @32-bit/channel so we need to do a check auto maxBytes = qint64(QImageReader::allocationLimit()) * 1024 * 1024; auto neededBytes = qint64(width) * height * nchannels * 4;