summaryrefslogtreecommitdiffhomepage
path: root/src
diff options
context:
space:
mode:
authorraysan5 <[email protected]>2018-01-06 02:50:20 +0100
committerraysan5 <[email protected]>2018-01-06 02:50:20 +0100
commit7fa28611607c210789054056095588b17c5bdaac (patch)
treeee470bf077b1f45ece7494387341d5fd623d62e1 /src
parent0e48396369f7cb3c41a69c9d7dba01328e0dd51c (diff)
downloadraylib-7fa28611607c210789054056095588b17c5bdaac.tar.gz
raylib-7fa28611607c210789054056095588b17c5bdaac.zip
Added function: GetPixelDataSize()
Just found I need that function...
Diffstat (limited to 'src')
-rw-r--r--src/raylib.h1
-rw-r--r--src/textures.c37
2 files changed, 38 insertions, 0 deletions
diff --git a/src/raylib.h b/src/raylib.h
index 75bd883d..01699ae7 100644
--- a/src/raylib.h
+++ b/src/raylib.h
@@ -869,6 +869,7 @@ RLAPI void UnloadImage(Image image);
RLAPI void UnloadTexture(Texture2D texture); // Unload texture from GPU memory (VRAM)
RLAPI void UnloadRenderTexture(RenderTexture2D target); // Unload render texture from GPU memory (VRAM)
RLAPI Color *GetImageData(Image image); // Get pixel data from image as a Color struct array
+RLAPI int GetImageDataSize(Image image); // Get image data size in bytes
RLAPI Image GetTextureData(Texture2D texture); // Get pixel data from GPU texture and return an Image
RLAPI void UpdateTexture(Texture2D texture, const void *pixels); // Update GPU texture with new data
RLAPI void SaveImageAs(const char *fileName, Image image); // Save image to a PNG file
diff --git a/src/textures.c b/src/textures.c
index fd2fae0d..9abee62b 100644
--- a/src/textures.c
+++ b/src/textures.c
@@ -515,6 +515,43 @@ Color *GetImageData(Image image)
return pixels;
}
+// Get pixel data size in bytes (image or texture)
+// NOTE: Size depends on pixel format
+int GetPixelDataSize(int width, int height, int format)
+{
+ int dataSize = 0; // Size in bytes
+ int bpp = 0; // Bits per pixel
+
+ switch (format)
+ {
+ case UNCOMPRESSED_GRAYSCALE: bpp = 8; break;
+ case UNCOMPRESSED_GRAY_ALPHA:
+ case UNCOMPRESSED_R5G6B5:
+ case UNCOMPRESSED_R5G5B5A1:
+ case UNCOMPRESSED_R4G4B4A4: bpp = 16; break;
+ case UNCOMPRESSED_R8G8B8A8: bpp = 32; break;
+ case UNCOMPRESSED_R8G8B8: bpp = 24; break;
+ case UNCOMPRESSED_R32G32B32: bpp = 32*3; break;
+ case UNCOMPRESSED_R32G32B32A32: bpp = 32*4; break;
+ case COMPRESSED_DXT1_RGB:
+ case COMPRESSED_DXT1_RGBA:
+ case COMPRESSED_ETC1_RGB:
+ case COMPRESSED_ETC2_RGB:
+ case COMPRESSED_PVRT_RGB:
+ case COMPRESSED_PVRT_RGBA: bpp = 4; break;
+ case COMPRESSED_DXT3_RGBA:
+ case COMPRESSED_DXT5_RGBA:
+ case COMPRESSED_ETC2_EAC_RGBA:
+ case COMPRESSED_ASTC_4x4_RGBA: bpp = 8; break;
+ case COMPRESSED_ASTC_8x8_RGBA: bpp = 2; break;
+ default: break;
+ }
+
+ dataSize = width*height*bpp/8; // Total data size in bytes
+
+ return dataSize;
+}
+
// Get pixel data from GPU texture and return an Image
// NOTE: Compressed texture formats not supported
Image GetTextureData(Texture2D texture)