#include "png_encoder.h" #include "common.h" #define STB_IMAGE_WRITE_IMPLEMENTATION #include "stb_image_write.h" static void stbi_write_to_vector(void* context, void* data, int size) { auto* vec = static_cast*>(context); auto* bytes = static_cast(data); vec->insert(vec->end(), bytes, bytes + size); } std::vector EncodeBgraToPng(const uint8_t* bgra, int width, int height, int stride) { // BGRA -> RGBA channel swap (D-14) std::vector rgba(static_cast(width) * height * 4); for (int y = 0; y < height; y++) { const uint8_t* src = bgra + y * stride; uint8_t* dst = rgba.data() + y * width * 4; for (int x = 0; x < width; x++) { dst[x * 4 + 0] = src[x * 4 + 2]; // R <- B dst[x * 4 + 1] = src[x * 4 + 1]; // G <- G dst[x * 4 + 2] = src[x * 4 + 0]; // B <- R dst[x * 4 + 3] = src[x * 4 + 3]; // A <- A } } std::vector png; stbi_write_png_to_func(stbi_write_to_vector, &png, width, height, 4, rgba.data(), width * 4); if (png.empty()) { DWM_LOG("ERROR: stbi_write_png_to_func returned empty output"); } return png; }