I have been trying to render an image from raw decompressed RGB image data, but the images that result tend to have a striped pattern to them.
Here is a standard stock image that has ended up with this effect when I try to render it (sorry for the low quality, apparently the full screenshot is somehow over 2MB?): the image, it is meant to be a fox sitting in snow
this image is in a raw RGB format in the BGR byte order.
I have another image, it’s just a standard coloured square image like those used to test renderers & it comes out like this (this image is an RGB format in RGB byte order): the test image
This is my current code to render the image:
BITMAPINFOHEADER bmp_header = {};
bmp_header.biSize = sizeof(BITMAPINFOHEADER);
bmp_header.biWidth = image_props.width;
bmp_header.biHeight = -image_props.height;
bmp_header.biPlanes = 1;
bmp_header.biBitCount = image_props.bits_per_component * 3;
bmp_header.biCompression = BI_RGB;
bmp_header.biSizeImage = 0;
BITMAPINFO bmi = {};
bmi.bmiHeader = bmp_header;
void* raw_bits = malloc(image_raw.size());
if (raw_bits != nullptr) {
memcpy(raw_bits, image_raw.data(), image_raw.size());
// this switches a BGR byte order image back to an RGB byte order if needed, so far I have no way to detect which byte order an image is in
uint8_t* pixel_data = static_cast<uint8_t*>(raw_bits);
for (size_t i = 0; i < image_raw.size(); i += 3) {
std::swap(pixel_data[i], pixel_data[i + 2]); // Swap red and blue bytes
}
}
StretchDIBits(hdc, 0, 0, 1500, 1500,
0, 0, image_props.width, image_props.height,
raw_bits, &bmi,
BI_RGB, // uint_8 not compat at cur
SRCCOPY);
free(raw_bits);
HDC hdc = GetDC(hwnd);
ReleaseDC(hwnd, hdc);
break;
I have no clue what could be going wrong. Another thing I need help with is to find a way to have the application be able to determine which byte order of RGB the image uses, RGB or BGR, since right now I have no way of doing this. My application needs to stay as small as possible so an excess amount of 3rd-party libs are out of the question, which is why I am using winGDI & not something like Direct2D. Any help is appreciated, thank you
Ezek is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.