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

bugfix: prevent image loading crash when actual channel is not three #87

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

r1ckhu
Copy link
Contributor

@r1ckhu r1ckhu commented Jun 25, 2024

Fix an issue in the stbi_loadf and stbi_load functions where the last parameter was incorrectly set to three. This caused the "data" array to always have three channels, rather than the expected "c" channels.

Fix an issue in the stbi_loadf and stbi_load functions where the last parameter was incorrectly set to three. This caused the "data" array to always have three channels, rather than the expected "c" channels.
@r1ckhu
Copy link
Contributor Author

r1ckhu commented Jun 25, 2024

The stb_image documentation for reference.

// Basic usage (see HDR discussion below for HDR usage):
//    int x,y,n;
//    unsigned char *data = stbi_load(filename, &x, &y, &n, 0);
//    // ... process data if not NULL ...
//    // ... x = width, y = height, n = # 8-bit components per pixel ...
//    // ... replace '0' with '1'..'4' to force that many components per pixel
//    // ... but 'n' will always be the number that it would have been if you said 0
//    stbi_image_free(data);
//
// Standard parameters:
//    int *x                 -- outputs image width in pixels
//    int *y                 -- outputs image height in pixels
//    int *channels_in_file  -- outputs # of image components in image file
//    int desired_channels   -- if non-zero, # of image components requested in result
//
// The return value from an image loader is an 'unsigned char *' which points
// to the pixel data, or NULL on an allocation failure or if the image is
// corrupt or invalid. The pixel data consists of *y scanlines of *x pixels,
// with each pixel consisting of N interleaved 8-bit components; the first
// pixel pointed to is top-left-most in the image. There is no padding between
// image scanlines or between pixels, regardless of format. The number of
// components N is 'desired_channels' if desired_channels is non-zero, or
// *channels_in_file otherwise. If desired_channels is non-zero,
// *channels_in_file has the number of components that _would_ have been
// output otherwise. E.g. if you set desired_channels to 4, you will always
// get RGBA output, but you can check *channels_in_file to see if it's trivially
// opaque because e.g. there were only 3 channels in the source image.

@mchenwang mchenwang requested a review from ChenxiZhou0619 July 22, 2024 11:22
@r1ckhu
Copy link
Contributor Author

r1ckhu commented Jul 22, 2024

Example scene that fails in the original code: https://njucg.github.io/Moer/files/bathroom.zip

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

Successfully merging this pull request may close these issues.

1 participant