diff --git a/include/Refresh_Image.h b/include/Refresh_Image.h index fa1593a..c3240eb 100644 --- a/include/Refresh_Image.h +++ b/include/Refresh_Image.h @@ -68,17 +68,19 @@ REFRESHAPI void Refresh_Image_Free(uint8_t *mem); /* Image Write API */ -/* Encodes RGBA8 image data into PNG data. +/* Encodes 32-bit color data into PNG data. * * filename: The filename that the image will be written to. * w: The width of the PNG data. * h: The height of the PNG data. - * data: The raw RGBA8 image data. + * bgra: Whether the data is in BGRA8 format. Otherwise will assume RBGA8. + * data: The raw color data. */ REFRESHAPI void Refresh_Image_SavePNG( char const *filename, int32_t w, int32_t h, + uint8_t bgra, uint8_t *data ); diff --git a/src/Refresh_Image.c b/src/Refresh_Image.c index 17f59fd..950a0e1 100644 --- a/src/Refresh_Image.c +++ b/src/Refresh_Image.c @@ -209,9 +209,32 @@ void Refresh_Image_SavePNG( const char *filename, int32_t w, int32_t h, + uint8_t bgra, uint8_t *data ) { - stbi_write_png(filename, w, h, 4, data, w * 4); + uint32_t i; + uint8_t *bgraData; + + if (bgra) + { + bgraData = SDL_malloc(w * h * 4); + + for (i = 0; i < w * h * 4; i += 4) + { + bgraData[i] = data[i + 2]; + bgraData[i + 1] = data[i + 1]; + bgraData[i + 2] = data[i]; + bgraData[i + 3] = data[i + 3]; + } + + stbi_write_png(filename, w, h, 4, bgraData, w * 4); + + SDL_free(bgraData); + } + else + { + stbi_write_png(filename, w, h, 4, data, w * 4); + } } /* vim: set noexpandtab shiftwidth=8 tabstop=8: */