Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fail to save large AVIF image #8

Open
vinlyx opened this issue May 17, 2022 · 6 comments
Open

Fail to save large AVIF image #8

vinlyx opened this issue May 17, 2022 · 6 comments

Comments

@vinlyx
Copy link

vinlyx commented May 17, 2022

"rgb data is incorrect size: %u * %u (%u) != %zd",

Hi @fdintino,
I am trying to use avif plugin to convert a large TIFF image to AVIF format. Unfortunately, I got a RuntimeError:
File "/xxx/Python-3.9.4/lib/python3.9/site-packages/pillow_avif/AvifImagePlugin.py", line 240, in _save
enc.add(
RuntimeError: rgb data is incorrect size: 119151 * 49716 (1628743820) != 5923711116

The raw TIFF image is 39717 x 49716 in RGB mode. I have tried to debug the error and find out it may due to an uint32 overflow issue. The width and height of avifRGBImage are defined as uint32_t. In this case 39717 x 49716 is larage than 4,294,967,295, so it generate a overflowed result (1628743820).

if (rgb.rowBytes * rgb.height != size) {
Convert rgb.rowBytes or rgb.height uint64_t during this operation may fix the issue, but I am not sure whether it will cause any issue in the following step.

Hope this analysis will help to fix this error. Thanks!

@fdintino
Copy link
Owner

rgb.rowBytes and rgb.height are uint32_t in libavif, and I believe the width and height themselves have limits well below 232. But we can cast them to Py_ssize_t in that multiplication expression to fix this specific overflow.

@fdintino
Copy link
Owner

fdintino commented May 17, 2022

So...I can fix the issue in this plugin. But it appears that libavif has a limit of 16384x16384 (228 pixels). So although encoding your image would no longer fail with this particular error, you still would not be able to encode the image as AVIF.

@cdgriffith
Copy link

Just adding a detail that the specifications don't allow for huge images with AV1.

NOTE: Following [AV1] level definitions, coded image items compliant to the AVIF Advanced profile may not have a number of pixels greater than 35651584, a width greater than 16384 or a height greater than 8704. It is still possible to use the Advanced profile to create larger images using grid derivation.

I do not see further reference to how to achieve a "grid derivation" AVIF image.

@fdintino
Copy link
Owner

libavif supports image grids...I had been meaning to look into exposing that in the codec. I'll see what I can do.

@vinlyx
Copy link
Author

vinlyx commented Jun 27, 2022

libavif supports image grids...I had been meaning to look into exposing that in the codec. I'll see what I can do.

That would be amazing~!
But I know little about image grids feature. Is there any documentation or example about that? Thanks!

@vsdfrf
Copy link

vsdfrf commented Jul 29, 2024

Hello, I find that libavif may support large images, Does that mean that using the avif plugin to convert a large TIFF image to AVIF format is possible?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants