summaryrefslogtreecommitdiffhomepage
path: root/src
diff options
context:
space:
mode:
authorRay <[email protected]>2023-06-03 19:51:16 +0200
committerRay <[email protected]>2023-06-03 19:51:16 +0200
commitf8b352f6d979bd7bfb960fc776bea0e9d39bf9da (patch)
tree60c29e717e93d3120406bd10295f1fcfa0decbe6 /src
parentba802fdd5e33f0e48406841c5aa9c97fd9fc715b (diff)
downloadraylib-f8b352f6d979bd7bfb960fc776bea0e9d39bf9da.tar.gz
raylib-f8b352f6d979bd7bfb960fc776bea0e9d39bf9da.zip
ADDED: `ExportImageToMemory()`
Only PNG supported for now
Diffstat (limited to 'src')
-rw-r--r--src/raylib.h1
-rw-r--r--src/rtextures.c30
2 files changed, 30 insertions, 1 deletions
diff --git a/src/raylib.h b/src/raylib.h
index 56acd6ad..f6a48bc7 100644
--- a/src/raylib.h
+++ b/src/raylib.h
@@ -1243,6 +1243,7 @@ RLAPI Image LoadImageFromScreen(void);
RLAPI bool IsImageReady(Image image); // Check if an image is ready
RLAPI void UnloadImage(Image image); // Unload image from CPU memory (RAM)
RLAPI bool ExportImage(Image image, const char *fileName); // Export image data to file, returns true on success
+RLAPI unsigned char *ExportImageToMemory(Image image, const char *fileType, int *fileSize); // Export image to memory buffer
RLAPI bool ExportImageAsCode(Image image, const char *fileName); // Export image as code file defining an array of bytes, returns true on success
// Image generation functions
diff --git a/src/rtextures.c b/src/rtextures.c
index a1987ef5..7d76616c 100644
--- a/src/rtextures.c
+++ b/src/rtextures.c
@@ -605,6 +605,34 @@ bool ExportImage(Image image, const char *fileName)
return success;
}
+// Export image to memory buffer
+unsigned char *ExportImageToMemory(Image image, const char *fileType, int *dataSize)
+{
+ unsigned char *fileData = NULL;
+ *dataSize = 0;
+
+ if ((image.width == 0) || (image.height == 0) || (image.data == NULL)) return NULL;
+
+#if defined(SUPPORT_IMAGE_EXPORT)
+ int channels = 4;
+
+ if (image.format == PIXELFORMAT_UNCOMPRESSED_GRAYSCALE) channels = 1;
+ else if (image.format == PIXELFORMAT_UNCOMPRESSED_GRAY_ALPHA) channels = 2;
+ else if (image.format == PIXELFORMAT_UNCOMPRESSED_R8G8B8) channels = 3;
+ else if (image.format == PIXELFORMAT_UNCOMPRESSED_R8G8B8A8) channels = 4;
+
+#if defined(SUPPORT_FILEFORMAT_PNG)
+ if ((strcmp(fileType, ".png") == 0) || (strcmp(fileType, ".PNG") == 0))
+ {
+ fileData = stbi_write_png_to_mem((const unsigned char *)image.data, image.width*channels, image.width, image.height, channels, dataSize);
+ }
+#endif
+
+#endif
+
+ return fileData;
+}
+
// Export image as code file (.h) defining an array of bytes
bool ExportImageAsCode(Image image, const char *fileName)
{
@@ -1013,7 +1041,7 @@ Image ImageCopy(Image image)
if (height < 1) height = 1;
}
- newImage.data = RL_MALLOC(size);
+ newImage.data = RL_CALLOC(size, 1);
if (newImage.data != NULL)
{