mirror of
https://github.com/raysan5/raylib.git
synced 2025-09-11 22:08:15 +00:00
Improved data export capabilities!
REVIEWED: ExportImage() REVIEWED: ExportMesh() ADDED: ExportWave() REMOVED: Internal funcs: SavePNG(), SaveBMP() NOTE: These changes break the API (parameters order)
This commit is contained in:
@@ -19,6 +19,9 @@
|
||||
* Selecte desired fileformats to be supported for image data loading. Some of those formats are
|
||||
* supported by default, to remove support, just comment unrequired #define in this module
|
||||
*
|
||||
* #define SUPPORT_IMAGE_EXPORT
|
||||
* Support image export in multiple file formats
|
||||
*
|
||||
* #define SUPPORT_IMAGE_MANIPULATION
|
||||
* Support multiple image editing functions to scale, adjust colors, flip, draw on images, crop...
|
||||
* If not defined only three image editing functions supported: ImageFormat(), ImageAlphaMask(), ImageToPOT()
|
||||
@@ -103,6 +106,11 @@
|
||||
// NOTE: Used to read image data (multiple formats support)
|
||||
#endif
|
||||
|
||||
#if defined(SUPPORT_IMAGE_EXPORT)
|
||||
#define STB_IMAGE_WRITE_IMPLEMENTATION
|
||||
#include "external/stb_image_write.h" // Required for: stbi_write_bmp(), stbi_write_png()
|
||||
#endif
|
||||
|
||||
#if defined(SUPPORT_IMAGE_MANIPULATION)
|
||||
#define STB_IMAGE_RESIZE_IMPLEMENTATION
|
||||
#include "external/stb_image_resize.h" // Required for: stbir_resize_uint8()
|
||||
@@ -706,15 +714,25 @@ void UpdateTexture(Texture2D texture, const void *pixels)
|
||||
rlUpdateTexture(texture.id, texture.width, texture.height, texture.format, pixels);
|
||||
}
|
||||
|
||||
// Export image as a PNG file
|
||||
void ExportImage(const char *fileName, Image image)
|
||||
// Export image data to file
|
||||
// NOTE: File format depends on fileName extension
|
||||
void ExportImage(Image image, const char *fileName)
|
||||
{
|
||||
int success = 0;
|
||||
|
||||
// NOTE: Getting Color array as RGBA unsigned char values
|
||||
unsigned char *imgData = (unsigned char *)GetImageData(image);
|
||||
|
||||
// NOTE: SavePNG() not supported by some platforms: PLATFORM_WEB, PLATFORM_ANDROID
|
||||
SavePNG(fileName, imgData, image.width, image.height, 4);
|
||||
|
||||
if (IsFileExtension(fileName, ".png")) success = stbi_write_png(fileName, image.width, image.height, 4, imgData, image.width*4);
|
||||
else if (IsFileExtension(fileName, ".bmp")) success = stbi_write_bmp(fileName, image.width, image.height, 4, imgData);
|
||||
else if (IsFileExtension(fileName, ".tga")) success = stbi_write_tga(fileName, image.width, image.height, 4, imgData);
|
||||
else if (IsFileExtension(fileName, ".jpg")) success = stbi_write_jpg(fileName, image.width, image.height, 4, imgData, 80); // Between 1 and 100
|
||||
else if (IsFileExtension(fileName, ".raw")) { } // TODO: Export raw pixel data
|
||||
else if (IsFileExtension(fileName, ".h")) { } // TODO: Export pixel data as an array of bytes
|
||||
|
||||
if (success != 0) TraceLog(LOG_INFO, "Image exported successfully: %s", fileName);
|
||||
else TraceLog(LOG_WARNING, "Image could not be exported.");
|
||||
|
||||
free(imgData);
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user