From c16029551852fe1574087715bf759b3d25da49b8 Mon Sep 17 00:00:00 2001 From: limzykenneth Date: Mon, 7 Sep 2026 09:24:22 +0800 Subject: [PATCH 1/2] Add maximum allowed gif dimension --- src/image/loading_displaying.js | 41 +++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) diff --git a/src/image/loading_displaying.js b/src/image/loading_displaying.js index 0aae6c324a..c8aff7726c 100644 --- a/src/image/loading_displaying.js +++ b/src/image/loading_displaying.js @@ -11,6 +11,40 @@ import * as omggif from 'omggif'; import { GIFEncoder, quantize, nearestColorIndex } from 'gifenc'; function loadingDisplaying(p5, fn) { + /** + * The largest possible number of pixels a gif frame can contain. + * + * This static property defines how large, in terms of dimension, a gif image + * is allowed to be loaded into a p5 sketch. The default value is + * 100,000,000. This means an image's width multiplied by its height must not + * exceed 100,000,000. For example, an image with width 10,000 and height + * 10,000 is just enough, as well as an image with width 5000 and height + * 20,000. An image with width 20,000 and height 20,000 is not allowed and + * will cause an error when it is loaded. + * + * To avoid this error, you should try to reduce the image dimension of the + * gif. If that is not possible or not desirable, you can set + * `MAX_GIF_PIXELS` to a higher value instead. + * + * @static + * @property {Boolean} MAX_GIF_PIXELS + * + * @example + * // META: norender + * // Increase the maximum pixel counts to 200,000,000 + * p5.MAX_GIF_PIXELS = 200_000_000; + * + * let img; + * async function setup() { + * createCanvas(100, 100); + * + * background(200); + * + * img = await loadImage('./a-large-animated.gif'); + * } + */ + p5.MAX_GIF_PIXELS = 4000 * 4000; // 4 Channel per pixels for total of 64MB + /** * Loads an image to create a p5.Image object. * @@ -623,6 +657,13 @@ function loadingDisplaying(p5, fn) { pImg.height = pImg.canvas.height = gifReader.height; const frames = []; const numFrames = gifReader.numFrames(); + + if (pImg.width * pImg.height > p5.MAX_GIF_PIXELS) { + // GIF is too big, refuse to proceed + p5.FES.log`The GIF is over the maximum allowed dimension. Try to shrink the image or set p5.MAX_GIF_PIXELS to a higher value.`(); + throw new Error("The GIF is over the maximum allowed dimension."); + } + let framePixels = new Uint8ClampedArray(pImg.width * pImg.height * 4); const loadGIFFrameIntoImage = (frameNum, gifReader) => { From 083e47e963388ecd23145cbf52b9382b10334b38 Mon Sep 17 00:00:00 2001 From: limzykenneth Date: Mon, 7 Sep 2026 09:26:56 +0800 Subject: [PATCH 2/2] Fix documentation indicating incorrect default value of MAX_GIF_PIXELS --- src/image/loading_displaying.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/image/loading_displaying.js b/src/image/loading_displaying.js index c8aff7726c..47c1c03133 100644 --- a/src/image/loading_displaying.js +++ b/src/image/loading_displaying.js @@ -16,7 +16,7 @@ function loadingDisplaying(p5, fn) { * * This static property defines how large, in terms of dimension, a gif image * is allowed to be loaded into a p5 sketch. The default value is - * 100,000,000. This means an image's width multiplied by its height must not + * 16,000,000. This means an image's width multiplied by its height must not * exceed 100,000,000. For example, an image with width 10,000 and height * 10,000 is just enough, as well as an image with width 5000 and height * 20,000. An image with width 20,000 and height 20,000 is not allowed and