lvandeve / lodepng

PNG encoder and decoder in C and C++.
zlib License
2.03k stars 420 forks source link

lodepng crashes ntdll.dll #188

Open mankrip opened 2 months ago

mankrip commented 2 months ago

lodepng crashes the function ntdll!RtlUserThreadStart() from ntdll.dll when I try to create a PNG image.

This is my C code to save 8-bit indexed color screenshots. Lodepng.cpp was also renamed to .c to ensure C compatibility:

#include "lodepng.h"
void WritePNGfile (const char *filename, const byte *image, unsigned width, unsigned height)
{
    LodePNGState pngstate;
    byte *png;
    size_t pngsize;
    unsigned error;

    // initialize state data
    lodepng_state_init (&pngstate);

    // generate PNG palette
    for(int i = 0; i < 256; i++)
    {
        byte r = palette_screentint[i].r; // palette_screenbase
        byte g = palette_screentint[i].g;
        byte b = palette_screentint[i].b;
        byte a = (i == TRANSPARENT_COLOR) ? 0 : 255;

        // palette must be added both to input and output color mode, because
        // in this sample both the raw image and the expected PNG image use that palette.
        lodepng_palette_add (&pngstate.info_raw      , r, g, b, a);
        lodepng_palette_add (&pngstate.info_png.color, r, g, b, a);
    }

    // customize state
    // both the raw image and the encoded image must get colorType 3 (palette).
    pngstate.info_raw.colortype = LCT_PALETTE;
    pngstate.info_raw.bitdepth = 8;
    pngstate.encoder.auto_convert = 0; // we specify ourselves exactly what output PNG color mode we want
    pngstate.info_png.color.colortype = LCT_PALETTE; // if you comment this line, and create the above palette in info_raw instead, then you get the same image in a RGBA PNG.
    pngstate.info_png.color.bitdepth = 8;

    // encode and save
    error = lodepng_encode (&png, &pngsize, image, width, height, &pngstate);
    if (error)
        Con_Printf ("error %u: %s\n", error, lodepng_error_text (error));
    else
        lodepng_save_file (png, pngsize, filename);

    // finish
    lodepng_state_cleanup (&pngstate);
    free (png);
}

This code compiles without warnings, but crashes. When looking at the debug info, the only thing I got was the mention of ntdll!RtlUserThreadStart().

My program is single-threaded, and compiled with the TDM-GCC 32-bit x86 compiler.