Home > OS >  STB Image Writing JPEG Incorrectly
STB Image Writing JPEG Incorrectly

Time:08-29

i was working on my pixel art editor in C/C which was a fork of Image trying to write

and this was the Image that was written:

Image Written

This is my WriteJpgFromCanvas function:

void WriteJpgFromCanvas(const char *filepath, int *canvas_dims, Uint32* data) {
    unsigned char* pixels = (unsigned char*)malloc(canvas_dims[0] * canvas_dims[1] * 4 * sizeof(unsigned char));
    memset(pixels, 0, canvas_dims[0] * canvas_dims[1] * 4 * sizeof(unsigned char));

    unsigned char* ptr;
    for (int y = 0; y < canvas_dims[1]; y  ) {
        for (int x = 0; x < canvas_dims[0]; x  ) {
            Uint32* pixel = GetPixel(x, y, data);
            ptr = pixels   ((y * canvas_dims[0]   x) * 4);
            *(ptr 0) = (*pixel & 0xFF000000) >> 24;  // R
            *(ptr 1) = (*pixel & 0x00FF0000) >> 16;  // G
            *(ptr 2) = (*pixel & 0x0000FF00) >> 8;   // B
            *(ptr 3) = (*pixel & 0x000000FF);        // A
        }
    }

    stbi_write_jpg(filepath, canvas_dims[0], canvas_dims[1], 4, data, 100);
    free(pixels);
}

Remarks:

CodePudding user response:

The displayed runtime left shift of a negative value indicates that the data is in an unexpected format. And indeed the function stbi_write_jpg is called with the unconverted data when looking at the presented code.

You need to call stbi_write_jpg with the converted data, which is called pixel in your code shown.

So the correct call should look like this:

stbi_write_jpg(filepath, canvas_dims[0], canvas_dims[1], 4, pixel, 100);
  • Related