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

[rtextures] LoadImageDataNormalized() broken for some pixel formats due to missing increment #4737

Closed
henrikglass opened this issue Jan 29, 2025 · 3 comments · Fixed by #4745

Comments

@henrikglass
Copy link
Contributor

  • [ YES] I checked there is no similar issue already reported
  • [ YES] I checked the documentation on the wiki
  • [ YES] My code has no errors or misuse of raylib
  • [ TECNICALLY YES] I tested it on latest raylib version from master branch

Issue description

I was experimenting a bit with GenMeshHeightmap and noticed that it simply refused to work for Images with the pixelformat PIXELFORMAT_UNCOMPRESSED_R32. Long story short, I tracked it down:

LoadImageDataNormalized in rtextures.c is broken for both PIXELFORMAT_UNCOMPRESSED_R32 and PIXELFORMAT_UNCOMPRESSED_R16 due to k not being incremented for each pixel. See snippet below:

case PIXELFORMAT_UNCOMPRESSED_R32:
{
    pixels[i].x = ((float *)image.data)[k];
    pixels[i].y = 0.0f;
    pixels[i].z = 0.0f;
    pixels[i].w = 1.0f;
} break;

     /* ... */

case PIXELFORMAT_UNCOMPRESSED_R16:
{
    pixels[i].x = HalfToFloat(((unsigned short *)image.data)[k]);
    pixels[i].y = 0.0f;
    pixels[i].z = 0.0f;
    pixels[i].w = 1.0f;
} break;

This should be:

case PIXELFORMAT_UNCOMPRESSED_R32:
{
    pixels[i].x = ((float *)image.data)[k];
    pixels[i].y = 0.0f;
    pixels[i].z = 0.0f;
    pixels[i].w = 1.0f;
    k += 1;
} break;

     /* ... */

case PIXELFORMAT_UNCOMPRESSED_R16:
{
    pixels[i].x = HalfToFloat(((unsigned short *)image.data)[k]);
    pixels[i].y = 0.0f;
    pixels[i].z = 0.0f;
    pixels[i].w = 1.0f;
    k += 1;
} break;

I made these changes to the source of raylib 5.0* and tested it with my own little raylib project. It seemed to produce the result I expected. However, with the latest version of raylib (master), I was unable to get my program to run even without the changes. My program would segfault when calling DrawMesh. I don't know what that is about. I'll leave that one for someone else to figure out.

I might make a pull request if I can make the latest version of raylib work, but if anyone else is tempted to do so before me please go ahead.

* I also had fix LoadImageColors, which had the same problem. It seems to be fixed in the latest version (master).

@henrikglass henrikglass changed the title [rtextures] LoadImageDataNormalized broken for some pixelformats due to missing increment of k [rtextures] LoadImageDataNormalized broken for some pixel formats due to missing increment Jan 29, 2025
@henrikglass
Copy link
Contributor Author

I did a very quick test. 53ea275 still causes my program to crash. I'm about to leave my computer, but I'll see if I can reproduce the problem later on a different machine, and, if so, produce a minimal example.

@raysan5 raysan5 changed the title [rtextures] LoadImageDataNormalized broken for some pixel formats due to missing increment [rtextures] LoadImageDataNormalized() broken for some pixel formats due to missing increment Jan 30, 2025
@raysan5
Copy link
Owner

raysan5 commented Feb 1, 2025

@henrikglass Please, could you send a PR with the review? Thanks!

@henrikglass
Copy link
Contributor Author

Good news, I couldn't replicate the crash on my home computer, which probably means I originally did something silly.

I'll post a PR later today with the fixes.

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 a pull request may close this issue.

2 participants