WARNING: RENAMED several functions for consistency #1440

This is a BREAKING CHANGE!

To address the linked issue, several functions have been renamed and couterpart functions have been created to free loaded memory:
 - RENAMED: GetImageData() -> LoadImageColors()
 - RENAMED: GetImagePalette() -> LoadImagePalette()
 - RENAMED: GetWaveData() -> LoadWaveSamples()
 - ADDED: UnloadImageColors()
 - ADDED: UnloadImagePalette()
 - ADDED: UnloadWaveSamples()
This commit is contained in:
Ray 2020-12-18 21:03:08 +01:00
parent 5dd142beb6
commit e07bc372a1
4 changed files with 89 additions and 60 deletions

View file

@ -1079,9 +1079,10 @@ void WaveCrop(Wave *wave, int initSample, int finalSample)
else TRACELOG(LOG_WARNING, "WAVE: Crop range out of bounds"); else TRACELOG(LOG_WARNING, "WAVE: Crop range out of bounds");
} }
// Get samples data from wave as a floats array // Load samples data from wave as a floats array
// NOTE: Returned sample values are normalized to range [-1..1] // NOTE 1: Returned sample values are normalized to range [-1..1]
float *GetWaveData(Wave wave) // NOTE 2: Sample data allocated should be freed with UnloadWaveSamples()
float *LoadWaveSamples(Wave wave)
{ {
float *samples = (float *)RL_MALLOC(wave.sampleCount*sizeof(float)); float *samples = (float *)RL_MALLOC(wave.sampleCount*sizeof(float));
@ -1097,6 +1098,12 @@ float *GetWaveData(Wave wave)
return samples; return samples;
} }
// Unload samples data loaded with LoadWaveSamples()
void UnloadWaveSamples(float *samples)
{
RL_FREE(samples);
}
//---------------------------------------------------------------------------------- //----------------------------------------------------------------------------------
// Module Functions Definition - Music loading and stream playing (.OGG) // Module Functions Definition - Music loading and stream playing (.OGG)
//---------------------------------------------------------------------------------- //----------------------------------------------------------------------------------

View file

@ -156,6 +156,7 @@
#define FormatText TextFormat #define FormatText TextFormat
#define LoadText LoadFileText #define LoadText LoadFileText
#define GetExtension GetFileExtension #define GetExtension GetFileExtension
#define GetImageData LoadImageColors
//#define Fade(c, a) ColorAlpha(c, a) //#define Fade(c, a) ColorAlpha(c, a)
//---------------------------------------------------------------------------------- //----------------------------------------------------------------------------------
@ -964,7 +965,7 @@ RLAPI float GetFrameTime(void); // Returns tim
RLAPI double GetTime(void); // Returns elapsed time in seconds since InitWindow() RLAPI double GetTime(void); // Returns elapsed time in seconds since InitWindow()
// Misc. functions // Misc. functions
RLAPI void SetConfigFlags(unsigned int flags); // Setup window configuration flags (view FLAGS) RLAPI void SetConfigFlags(unsigned int flags); // Setup init configuration flags (view FLAGS)
RLAPI void SetTraceLogLevel(int logType); // Set the current threshold (minimum) log level RLAPI void SetTraceLogLevel(int logType); // Set the current threshold (minimum) log level
RLAPI void SetTraceLogExit(int logType); // Set the exit threshold (minimum) log level RLAPI void SetTraceLogExit(int logType); // Set the exit threshold (minimum) log level
@ -1176,9 +1177,10 @@ RLAPI void ImageColorGrayscale(Image *image);
RLAPI void ImageColorContrast(Image *image, float contrast); // Modify image color: contrast (-100 to 100) RLAPI void ImageColorContrast(Image *image, float contrast); // Modify image color: contrast (-100 to 100)
RLAPI void ImageColorBrightness(Image *image, int brightness); // Modify image color: brightness (-255 to 255) RLAPI void ImageColorBrightness(Image *image, int brightness); // Modify image color: brightness (-255 to 255)
RLAPI void ImageColorReplace(Image *image, Color color, Color replace); // Modify image color: replace color RLAPI void ImageColorReplace(Image *image, Color color, Color replace); // Modify image color: replace color
RLAPI Color *LoadImageColors(Image image); // Load color data from image as a Color array (RGBA - 32bit)
RLAPI Color *GetImageData(Image image); // Get pixel data from image as a Color struct array RLAPI Color *LoadImagePalette(Image image, int maxPaletteSize, int *colorsCount); // Load colors palette from image as a Color array (RGBA - 32bit)
RLAPI Color *GetImagePalette(Image image, int maxPaletteSize, int *extractCount); // Get color palette from image to maximum size (memory should be freed) RLAPI void UnloadImageColors(Color *colors); // Unload color data loaded with LoadImageColors()
RLAPI void UnloadImagePalette(Color *colors); // Unload colors palette loaded with LoadImagePalette()
RLAPI Rectangle GetImageAlphaBorder(Image image, float threshold); // Get image alpha border rectangle RLAPI Rectangle GetImageAlphaBorder(Image image, float threshold); // Get image alpha border rectangle
// Image drawing functions // Image drawing functions
@ -1473,7 +1475,9 @@ RLAPI void SetSoundPitch(Sound sound, float pitch); // Set pit
RLAPI void WaveFormat(Wave *wave, int sampleRate, int sampleSize, int channels); // Convert wave data to desired format RLAPI void WaveFormat(Wave *wave, int sampleRate, int sampleSize, int channels); // Convert wave data to desired format
RLAPI Wave WaveCopy(Wave wave); // Copy a wave to a new wave RLAPI Wave WaveCopy(Wave wave); // Copy a wave to a new wave
RLAPI void WaveCrop(Wave *wave, int initSample, int finalSample); // Crop a wave to defined samples range RLAPI void WaveCrop(Wave *wave, int initSample, int finalSample); // Crop a wave to defined samples range
RLAPI float *GetWaveData(Wave wave); // Get samples data from wave as a floats array //RLAPI float *GetWaveData(Wave wave); // Get samples data from wave as a floats array
RLAPI float *LoadWaveSamples(Wave wave); // Load samples data from wave as a floats array
RLAPI void UnloadWaveSamples(float *samples); // Unload samples data loaded with LoadWaveSamples()
// Music management functions // Music management functions
RLAPI Music LoadMusicStream(const char *fileName); // Load music stream from file RLAPI Music LoadMusicStream(const char *fileName); // Load music stream from file

View file

@ -376,7 +376,7 @@ Font LoadFontFromImage(Image image, Color key, int firstChar)
int tempCharValues[MAX_GLYPHS_FROM_IMAGE]; int tempCharValues[MAX_GLYPHS_FROM_IMAGE];
Rectangle tempCharRecs[MAX_GLYPHS_FROM_IMAGE]; Rectangle tempCharRecs[MAX_GLYPHS_FROM_IMAGE];
Color *pixels = GetImageData(image); Color *pixels = LoadImageColors(image);
// Parse image data to get charSpacing and lineSpacing // Parse image data to get charSpacing and lineSpacing
for (y = 0; y < image.height; y++) for (y = 0; y < image.height; y++)

View file

@ -186,7 +186,7 @@ static Image LoadPVR(const unsigned char *fileData, unsigned int fileSize); //
#if defined(SUPPORT_FILEFORMAT_ASTC) #if defined(SUPPORT_FILEFORMAT_ASTC)
static Image LoadASTC(const unsigned char *fileData, unsigned int fileSize); // Load ASTC file data static Image LoadASTC(const unsigned char *fileData, unsigned int fileSize); // Load ASTC file data
#endif #endif
static Vector4 *GetImageDataNormalized(Image image); // Get pixel data from image as Vector4 array (float normalized) static Vector4 *LoadImageDataNormalized(Image image); // Load pixel data from image as Vector4 array (float normalized)
//---------------------------------------------------------------------------------- //----------------------------------------------------------------------------------
// Module Functions Definition // Module Functions Definition
@ -412,7 +412,7 @@ bool ExportImage(Image image, const char *fileName)
else else
{ {
// NOTE: Getting Color array as RGBA unsigned char values // NOTE: Getting Color array as RGBA unsigned char values
imgData = (unsigned char *)GetImageData(image); imgData = (unsigned char *)LoadImageColors(image);
allocatedData = true; allocatedData = true;
} }
@ -888,7 +888,7 @@ void ImageFormat(Image *image, int newFormat)
{ {
if ((image->format < COMPRESSED_DXT1_RGB) && (newFormat < COMPRESSED_DXT1_RGB)) if ((image->format < COMPRESSED_DXT1_RGB) && (newFormat < COMPRESSED_DXT1_RGB))
{ {
Vector4 *pixels = GetImageDataNormalized(*image); // Supports 8 to 32 bit per channel Vector4 *pixels = LoadImageDataNormalized(*image); // Supports 8 to 32 bit per channel
RL_FREE(image->data); // WARNING! We loose mipmaps data --> Regenerated at the end... RL_FREE(image->data); // WARNING! We loose mipmaps data --> Regenerated at the end...
image->data = NULL; image->data = NULL;
@ -1306,7 +1306,7 @@ void ImageAlphaPremultiply(Image *image)
if ((image->data == NULL) || (image->width == 0) || (image->height == 0)) return; if ((image->data == NULL) || (image->width == 0) || (image->height == 0)) return;
float alpha = 0.0f; float alpha = 0.0f;
Color *pixels = GetImageData(*image); Color *pixels = LoadImageColors(*image);
for (int i = 0; i < image->width*image->height; i++) for (int i = 0; i < image->width*image->height; i++)
{ {
@ -1368,7 +1368,7 @@ void ImageResize(Image *image, int newWidth, int newHeight)
else else
{ {
// Get data as Color pixels array to work with it // Get data as Color pixels array to work with it
Color *pixels = GetImageData(*image); Color *pixels = LoadImageColors(*image);
Color *output = (Color *)RL_MALLOC(newWidth*newHeight*sizeof(Color)); Color *output = (Color *)RL_MALLOC(newWidth*newHeight*sizeof(Color));
// NOTE: Color data is casted to (unsigned char *), there shouldn't been any problem... // NOTE: Color data is casted to (unsigned char *), there shouldn't been any problem...
@ -1376,7 +1376,7 @@ void ImageResize(Image *image, int newWidth, int newHeight)
int format = image->format; int format = image->format;
RL_FREE(pixels); UnloadImageColors(pixels);
RL_FREE(image->data); RL_FREE(image->data);
image->data = output; image->data = output;
@ -1394,7 +1394,7 @@ void ImageResizeNN(Image *image,int newWidth,int newHeight)
// Security check to avoid program crash // Security check to avoid program crash
if ((image->data == NULL) || (image->width == 0) || (image->height == 0)) return; if ((image->data == NULL) || (image->width == 0) || (image->height == 0)) return;
Color *pixels = GetImageData(*image); Color *pixels = LoadImageColors(*image);
Color *output = (Color *)RL_MALLOC(newWidth*newHeight*sizeof(Color)); Color *output = (Color *)RL_MALLOC(newWidth*newHeight*sizeof(Color));
// EDIT: added +1 to account for an early rounding problem // EDIT: added +1 to account for an early rounding problem
@ -1424,7 +1424,7 @@ void ImageResizeNN(Image *image,int newWidth,int newHeight)
ImageFormat(image, format); // Reformat 32bit RGBA image to original format ImageFormat(image, format); // Reformat 32bit RGBA image to original format
RL_FREE(pixels); UnloadImageColors(pixels);
} }
// Resize canvas and fill with color // Resize canvas and fill with color
@ -1570,7 +1570,7 @@ void ImageDither(Image *image, int rBpp, int gBpp, int bBpp, int aBpp)
} }
else else
{ {
Color *pixels = GetImageData(*image); Color *pixels = LoadImageColors(*image);
RL_FREE(image->data); // free old image data RL_FREE(image->data); // free old image data
@ -1658,7 +1658,7 @@ void ImageDither(Image *image, int rBpp, int gBpp, int bBpp, int aBpp)
} }
} }
RL_FREE(pixels); UnloadImageColors(pixels);
} }
} }
@ -1801,7 +1801,7 @@ void ImageColorTint(Image *image, Color color)
// Security check to avoid program crash // Security check to avoid program crash
if ((image->data == NULL) || (image->width == 0) || (image->height == 0)) return; if ((image->data == NULL) || (image->width == 0) || (image->height == 0)) return;
Color *pixels = GetImageData(*image); Color *pixels = LoadImageColors(*image);
float cR = (float)color.r/255; float cR = (float)color.r/255;
float cG = (float)color.g/255; float cG = (float)color.g/255;
@ -1840,7 +1840,7 @@ void ImageColorInvert(Image *image)
// Security check to avoid program crash // Security check to avoid program crash
if ((image->data == NULL) || (image->width == 0) || (image->height == 0)) return; if ((image->data == NULL) || (image->width == 0) || (image->height == 0)) return;
Color *pixels = GetImageData(*image); Color *pixels = LoadImageColors(*image);
for (int y = 0; y < image->height; y++) for (int y = 0; y < image->height; y++)
{ {
@ -1880,7 +1880,7 @@ void ImageColorContrast(Image *image, float contrast)
contrast = (100.0f + contrast)/100.0f; contrast = (100.0f + contrast)/100.0f;
contrast *= contrast; contrast *= contrast;
Color *pixels = GetImageData(*image); Color *pixels = LoadImageColors(*image);
for (int y = 0; y < image->height; y++) for (int y = 0; y < image->height; y++)
{ {
@ -1935,7 +1935,7 @@ void ImageColorBrightness(Image *image, int brightness)
if (brightness < -255) brightness = -255; if (brightness < -255) brightness = -255;
if (brightness > 255) brightness = 255; if (brightness > 255) brightness = 255;
Color *pixels = GetImageData(*image); Color *pixels = LoadImageColors(*image);
for (int y = 0; y < image->height; y++) for (int y = 0; y < image->height; y++)
{ {
@ -1975,7 +1975,7 @@ void ImageColorReplace(Image *image, Color color, Color replace)
// Security check to avoid program crash // Security check to avoid program crash
if ((image->data == NULL) || (image->width == 0) || (image->height == 0)) return; if ((image->data == NULL) || (image->width == 0) || (image->height == 0)) return;
Color *pixels = GetImageData(*image); Color *pixels = LoadImageColors(*image);
for (int y = 0; y < image->height; y++) for (int y = 0; y < image->height; y++)
{ {
@ -2004,8 +2004,9 @@ void ImageColorReplace(Image *image, Color color, Color replace)
} }
#endif // SUPPORT_IMAGE_MANIPULATION #endif // SUPPORT_IMAGE_MANIPULATION
// Get pixel data from image in the form of Color struct array // Load color data from image as a Color array (RGBA - 32bit)
Color *GetImageData(Image image) // NOTE: Memory allocated should be freed using UnloadImageColors();
Color *LoadImageColors(Image image)
{ {
if ((image.width == 0) || (image.height == 0)) return NULL; if ((image.width == 0) || (image.height == 0)) return NULL;
@ -2121,16 +2122,20 @@ Color *GetImageData(Image image)
return pixels; return pixels;
} }
// Get color palette from image to maximum size // Load colors palette from image as a Color array (RGBA - 32bit)
// NOTE: Memory allocated should be freed manually! // NOTE: Memory allocated should be freed using UnloadImagePalette()
Color *GetImagePalette(Image image, int maxPaletteSize, int *extractCount) Color *LoadImagePalette(Image image, int maxPaletteSize, int *colorsCount)
{ {
#define COLOR_EQUAL(col1, col2) ((col1.r == col2.r)&&(col1.g == col2.g)&&(col1.b == col2.b)&&(col1.a == col2.a)) #define COLOR_EQUAL(col1, col2) ((col1.r == col2.r)&&(col1.g == col2.g)&&(col1.b == col2.b)&&(col1.a == col2.a))
Color *pixels = GetImageData(image);
Color *palette = (Color *)RL_MALLOC(maxPaletteSize*sizeof(Color));
int palCount = 0; int palCount = 0;
Color *palette = NULL;
Color *pixels = LoadImageColors(image);
if (pixels != NULL)
{
palette = (Color *)RL_MALLOC(maxPaletteSize*sizeof(Color));
for (int i = 0; i < maxPaletteSize; i++) palette[i] = BLANK; // Set all colors to BLANK for (int i = 0; i < maxPaletteSize; i++) palette[i] = BLANK; // Set all colors to BLANK
for (int i = 0; i < image.width*image.height; i++) for (int i = 0; i < image.width*image.height; i++)
@ -2165,15 +2170,28 @@ Color *GetImagePalette(Image image, int maxPaletteSize, int *extractCount)
} }
} }
RL_FREE(pixels); UnloadImagePixels(pixels);
}
*extractCount = palCount; *colorsCount = palCount;
return palette; return palette;
} }
// Unload color data loaded with LoadImageColors()
void UnloadImageColors(Color *colors)
{
RL_FREE(colors);
}
// Unload colors palette loaded with LoadImagePalette()
void UnloadImagePalette(Color *colors)
{
RL_FREE(palette);
}
// Get pixel data from image as Vector4 array (float normalized) // Get pixel data from image as Vector4 array (float normalized)
static Vector4 *GetImageDataNormalized(Image image) static Vector4 *LoadImageDataNormalized(Image image)
{ {
Vector4 *pixels = (Vector4 *)RL_MALLOC(image.width*image.height*sizeof(Vector4)); Vector4 *pixels = (Vector4 *)RL_MALLOC(image.width*image.height*sizeof(Vector4));
@ -2289,7 +2307,7 @@ Rectangle GetImageAlphaBorder(Image image, float threshold)
{ {
Rectangle crop = { 0 }; Rectangle crop = { 0 };
Color *pixels = GetImageData(image); Color *pixels = LoadImageColors(image);
if (pixels != NULL) if (pixels != NULL)
{ {
@ -2318,7 +2336,7 @@ Rectangle GetImageAlphaBorder(Image image, float threshold)
crop = (Rectangle){ (float)xMin, (float)yMin, (float)((xMax + 1) - xMin), (float)((yMax + 1) - yMin) }; crop = (Rectangle){ (float)xMin, (float)yMin, (float)((xMax + 1) - xMin), (float)((yMax + 1) - yMin) };
} }
RL_FREE(pixels); UnloadImageColors(pixels);
} }
return crop; return crop;