Reviewed ImageConvertToPOT()
This commit is contained in:
parent
fd2b0c7c38
commit
07858c3a1f
1 changed files with 135 additions and 128 deletions
233
src/textures.c
233
src/textures.c
|
@ -330,7 +330,6 @@ Texture2D LoadTexture(const char *fileName)
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
TraceLog(WARNING, "Texture could not be created");
|
TraceLog(WARNING, "Texture could not be created");
|
||||||
|
|
||||||
texture.id = 0;
|
texture.id = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -391,7 +390,8 @@ void UnloadImage(Image image)
|
||||||
{
|
{
|
||||||
free(image.data);
|
free(image.data);
|
||||||
|
|
||||||
TraceLog(INFO, "Unloaded image data");
|
// NOTE: It becomes anoying every time a texture is loaded
|
||||||
|
//TraceLog(INFO, "Unloaded image data");
|
||||||
}
|
}
|
||||||
|
|
||||||
// Unload texture from GPU memory
|
// Unload texture from GPU memory
|
||||||
|
@ -519,136 +519,139 @@ Image GetTextureData(Texture2D texture)
|
||||||
// Convert image data to desired format
|
// Convert image data to desired format
|
||||||
void ImageConvertFormat(Image *image, int newFormat)
|
void ImageConvertFormat(Image *image, int newFormat)
|
||||||
{
|
{
|
||||||
if ((image->format != newFormat) && (image->format < 8) && (newFormat < 8))
|
if (image->format != newFormat)
|
||||||
{
|
{
|
||||||
Color *pixels = GetImageData(*image);
|
if ((image->format < 8) && (newFormat < 8))
|
||||||
|
|
||||||
free(image->data);
|
|
||||||
|
|
||||||
image->format = newFormat;
|
|
||||||
|
|
||||||
int k = 0;
|
|
||||||
|
|
||||||
switch (image->format)
|
|
||||||
{
|
{
|
||||||
case UNCOMPRESSED_GRAYSCALE:
|
Color *pixels = GetImageData(*image);
|
||||||
|
|
||||||
|
free(image->data);
|
||||||
|
|
||||||
|
image->format = newFormat;
|
||||||
|
|
||||||
|
int k = 0;
|
||||||
|
|
||||||
|
switch (image->format)
|
||||||
{
|
{
|
||||||
image->data = (unsigned char *)malloc(image->width*image->height*sizeof(unsigned char));
|
case UNCOMPRESSED_GRAYSCALE:
|
||||||
|
|
||||||
for (int i = 0; i < image->width*image->height; i++)
|
|
||||||
{
|
{
|
||||||
((unsigned char *)image->data)[i] = (unsigned char)((float)pixels[k].r*0.299f + (float)pixels[k].g*0.587f + (float)pixels[k].b*0.114f);
|
image->data = (unsigned char *)malloc(image->width*image->height*sizeof(unsigned char));
|
||||||
k++;
|
|
||||||
}
|
|
||||||
|
|
||||||
} break;
|
for (int i = 0; i < image->width*image->height; i++)
|
||||||
case UNCOMPRESSED_GRAY_ALPHA:
|
{
|
||||||
{
|
((unsigned char *)image->data)[i] = (unsigned char)((float)pixels[k].r*0.299f + (float)pixels[k].g*0.587f + (float)pixels[k].b*0.114f);
|
||||||
image->data = (unsigned char *)malloc(image->width*image->height*2*sizeof(unsigned char));
|
k++;
|
||||||
|
}
|
||||||
|
|
||||||
for (int i = 0; i < image->width*image->height*2; i += 2)
|
} break;
|
||||||
|
case UNCOMPRESSED_GRAY_ALPHA:
|
||||||
{
|
{
|
||||||
((unsigned char *)image->data)[i] = (unsigned char)((float)pixels[k].r*0.299f + (float)pixels[k].g*0.587f + (float)pixels[k].b*0.114f);
|
image->data = (unsigned char *)malloc(image->width*image->height*2*sizeof(unsigned char));
|
||||||
((unsigned char *)image->data)[i + 1] = pixels[k].a;
|
|
||||||
k++;
|
|
||||||
}
|
|
||||||
|
|
||||||
} break;
|
for (int i = 0; i < image->width*image->height*2; i += 2)
|
||||||
case UNCOMPRESSED_R5G6B5:
|
{
|
||||||
{
|
((unsigned char *)image->data)[i] = (unsigned char)((float)pixels[k].r*0.299f + (float)pixels[k].g*0.587f + (float)pixels[k].b*0.114f);
|
||||||
image->data = (unsigned short *)malloc(image->width*image->height*sizeof(unsigned short));
|
((unsigned char *)image->data)[i + 1] = pixels[k].a;
|
||||||
|
k++;
|
||||||
|
}
|
||||||
|
|
||||||
unsigned char r;
|
} break;
|
||||||
unsigned char g;
|
case UNCOMPRESSED_R5G6B5:
|
||||||
unsigned char b;
|
|
||||||
|
|
||||||
for (int i = 0; i < image->width*image->height; i++)
|
|
||||||
{
|
{
|
||||||
r = (unsigned char)(round((float)pixels[k].r*31/255));
|
image->data = (unsigned short *)malloc(image->width*image->height*sizeof(unsigned short));
|
||||||
g = (unsigned char)(round((float)pixels[k].g*63/255));
|
|
||||||
b = (unsigned char)(round((float)pixels[k].b*31/255));
|
|
||||||
|
|
||||||
((unsigned short *)image->data)[i] = (unsigned short)r << 11 | (unsigned short)g << 5 | (unsigned short)b;
|
unsigned char r;
|
||||||
|
unsigned char g;
|
||||||
|
unsigned char b;
|
||||||
|
|
||||||
k++;
|
for (int i = 0; i < image->width*image->height; i++)
|
||||||
}
|
{
|
||||||
|
r = (unsigned char)(round((float)pixels[k].r*31/255));
|
||||||
|
g = (unsigned char)(round((float)pixels[k].g*63/255));
|
||||||
|
b = (unsigned char)(round((float)pixels[k].b*31/255));
|
||||||
|
|
||||||
} break;
|
((unsigned short *)image->data)[i] = (unsigned short)r << 11 | (unsigned short)g << 5 | (unsigned short)b;
|
||||||
case UNCOMPRESSED_R8G8B8:
|
|
||||||
{
|
|
||||||
image->data = (unsigned char *)malloc(image->width*image->height*3*sizeof(unsigned char));
|
|
||||||
|
|
||||||
for (int i = 0; i < image->width*image->height*3; i += 3)
|
k++;
|
||||||
|
}
|
||||||
|
|
||||||
|
} break;
|
||||||
|
case UNCOMPRESSED_R8G8B8:
|
||||||
{
|
{
|
||||||
((unsigned char *)image->data)[i] = pixels[k].r;
|
image->data = (unsigned char *)malloc(image->width*image->height*3*sizeof(unsigned char));
|
||||||
((unsigned char *)image->data)[i + 1] = pixels[k].g;
|
|
||||||
((unsigned char *)image->data)[i + 2] = pixels[k].b;
|
|
||||||
k++;
|
|
||||||
}
|
|
||||||
} break;
|
|
||||||
case UNCOMPRESSED_R5G5B5A1:
|
|
||||||
{
|
|
||||||
image->data = (unsigned short *)malloc(image->width*image->height*sizeof(unsigned short));
|
|
||||||
|
|
||||||
unsigned char r;
|
for (int i = 0; i < image->width*image->height*3; i += 3)
|
||||||
unsigned char g;
|
{
|
||||||
unsigned char b;
|
((unsigned char *)image->data)[i] = pixels[k].r;
|
||||||
unsigned char a = 1;
|
((unsigned char *)image->data)[i + 1] = pixels[k].g;
|
||||||
|
((unsigned char *)image->data)[i + 2] = pixels[k].b;
|
||||||
for (int i = 0; i < image->width*image->height; i++)
|
k++;
|
||||||
|
}
|
||||||
|
} break;
|
||||||
|
case UNCOMPRESSED_R5G5B5A1:
|
||||||
{
|
{
|
||||||
r = (unsigned char)(round((float)pixels[k].r*31/255));
|
image->data = (unsigned short *)malloc(image->width*image->height*sizeof(unsigned short));
|
||||||
g = (unsigned char)(round((float)pixels[k].g*31/255));
|
|
||||||
b = (unsigned char)(round((float)pixels[k].b*31/255));
|
|
||||||
a = (pixels[k].a > 50) ? 1 : 0;
|
|
||||||
|
|
||||||
((unsigned short *)image->data)[i] = (unsigned short)r << 11 | (unsigned short)g << 6 | (unsigned short)b << 1| (unsigned short)a;
|
unsigned char r;
|
||||||
|
unsigned char g;
|
||||||
|
unsigned char b;
|
||||||
|
unsigned char a = 1;
|
||||||
|
|
||||||
k++;
|
for (int i = 0; i < image->width*image->height; i++)
|
||||||
}
|
{
|
||||||
|
r = (unsigned char)(round((float)pixels[k].r*31/255));
|
||||||
|
g = (unsigned char)(round((float)pixels[k].g*31/255));
|
||||||
|
b = (unsigned char)(round((float)pixels[k].b*31/255));
|
||||||
|
a = (pixels[k].a > 50) ? 1 : 0;
|
||||||
|
|
||||||
} break;
|
((unsigned short *)image->data)[i] = (unsigned short)r << 11 | (unsigned short)g << 6 | (unsigned short)b << 1| (unsigned short)a;
|
||||||
case UNCOMPRESSED_R4G4B4A4:
|
|
||||||
{
|
|
||||||
image->data = (unsigned short *)malloc(image->width*image->height*sizeof(unsigned short));
|
|
||||||
|
|
||||||
unsigned char r;
|
k++;
|
||||||
unsigned char g;
|
}
|
||||||
unsigned char b;
|
|
||||||
unsigned char a;
|
|
||||||
|
|
||||||
for (int i = 0; i < image->width*image->height; i++)
|
} break;
|
||||||
|
case UNCOMPRESSED_R4G4B4A4:
|
||||||
{
|
{
|
||||||
r = (unsigned char)(round((float)pixels[k].r*15/255));
|
image->data = (unsigned short *)malloc(image->width*image->height*sizeof(unsigned short));
|
||||||
g = (unsigned char)(round((float)pixels[k].g*15/255));
|
|
||||||
b = (unsigned char)(round((float)pixels[k].b*15/255));
|
|
||||||
a = (unsigned char)(round((float)pixels[k].a*15/255));
|
|
||||||
|
|
||||||
((unsigned short *)image->data)[i] = (unsigned short)r << 12 | (unsigned short)g << 8| (unsigned short)b << 4| (unsigned short)a;
|
unsigned char r;
|
||||||
|
unsigned char g;
|
||||||
|
unsigned char b;
|
||||||
|
unsigned char a;
|
||||||
|
|
||||||
k++;
|
for (int i = 0; i < image->width*image->height; i++)
|
||||||
}
|
{
|
||||||
|
r = (unsigned char)(round((float)pixels[k].r*15/255));
|
||||||
|
g = (unsigned char)(round((float)pixels[k].g*15/255));
|
||||||
|
b = (unsigned char)(round((float)pixels[k].b*15/255));
|
||||||
|
a = (unsigned char)(round((float)pixels[k].a*15/255));
|
||||||
|
|
||||||
} break;
|
((unsigned short *)image->data)[i] = (unsigned short)r << 12 | (unsigned short)g << 8| (unsigned short)b << 4| (unsigned short)a;
|
||||||
case UNCOMPRESSED_R8G8B8A8:
|
|
||||||
{
|
|
||||||
image->data = (unsigned char *)malloc(image->width*image->height*4*sizeof(unsigned char));
|
|
||||||
|
|
||||||
for (int i = 0; i < image->width*image->height*4; i += 4)
|
k++;
|
||||||
|
}
|
||||||
|
|
||||||
|
} break;
|
||||||
|
case UNCOMPRESSED_R8G8B8A8:
|
||||||
{
|
{
|
||||||
((unsigned char *)image->data)[i] = pixels[k].r;
|
image->data = (unsigned char *)malloc(image->width*image->height*4*sizeof(unsigned char));
|
||||||
((unsigned char *)image->data)[i + 1] = pixels[k].g;
|
|
||||||
((unsigned char *)image->data)[i + 2] = pixels[k].b;
|
for (int i = 0; i < image->width*image->height*4; i += 4)
|
||||||
((unsigned char *)image->data)[i + 3] = pixels[k].a;
|
{
|
||||||
k++;
|
((unsigned char *)image->data)[i] = pixels[k].r;
|
||||||
}
|
((unsigned char *)image->data)[i + 1] = pixels[k].g;
|
||||||
} break;
|
((unsigned char *)image->data)[i + 2] = pixels[k].b;
|
||||||
default: break;
|
((unsigned char *)image->data)[i + 3] = pixels[k].a;
|
||||||
|
k++;
|
||||||
|
}
|
||||||
|
} break;
|
||||||
|
default: break;
|
||||||
|
}
|
||||||
|
|
||||||
|
free(pixels);
|
||||||
}
|
}
|
||||||
|
else TraceLog(WARNING, "Image data format is compressed, can not be converted");
|
||||||
free(pixels);
|
|
||||||
}
|
}
|
||||||
else TraceLog(WARNING, "Image data format is compressed, can not be converted");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -656,8 +659,8 @@ void ImageConvertFormat(Image *image, int newFormat)
|
||||||
// NOTE: Requirement on OpenGL ES 2.0 (RPI, HTML5)
|
// NOTE: Requirement on OpenGL ES 2.0 (RPI, HTML5)
|
||||||
void ImageConvertToPOT(Image *image, Color fillColor)
|
void ImageConvertToPOT(Image *image, Color fillColor)
|
||||||
{
|
{
|
||||||
// TODO: Review for new image struct
|
Color *pixels = GetImageData(*image); // Get pixels data
|
||||||
/*
|
|
||||||
// Just add the required amount of pixels at the right and bottom sides of image...
|
// Just add the required amount of pixels at the right and bottom sides of image...
|
||||||
int potWidth = GetNextPOT(image->width);
|
int potWidth = GetNextPOT(image->width);
|
||||||
int potHeight = GetNextPOT(image->height);
|
int potHeight = GetNextPOT(image->height);
|
||||||
|
@ -665,29 +668,33 @@ void ImageConvertToPOT(Image *image, Color fillColor)
|
||||||
// Check if POT texture generation is required (if texture is not already POT)
|
// Check if POT texture generation is required (if texture is not already POT)
|
||||||
if ((potWidth != image->width) || (potHeight != image->height))
|
if ((potWidth != image->width) || (potHeight != image->height))
|
||||||
{
|
{
|
||||||
Color *imgDataPixelPOT = NULL;
|
Color *pixelsPOT = NULL;
|
||||||
|
|
||||||
// Generate POT array from NPOT data
|
// Generate POT array from NPOT data
|
||||||
imgDataPixelPOT = (Color *)malloc(potWidth * potHeight * sizeof(Color));
|
pixelsPOT = (Color *)malloc(potWidth * potHeight * sizeof(Color));
|
||||||
|
|
||||||
for (int j = 0; j < potHeight; j++)
|
for (int j = 0; j < potHeight; j++)
|
||||||
{
|
{
|
||||||
for (int i = 0; i < potWidth; i++)
|
for (int i = 0; i < potWidth; i++)
|
||||||
{
|
{
|
||||||
if ((j < image->height) && (i < image->width)) imgDataPixelPOT[j*potWidth + i] = image->data[j*image->width + i];
|
if ((j < image->height) && (i < image->width)) pixelsPOT[j*potWidth + i] = pixels[j*image->width + i];
|
||||||
else imgDataPixelPOT[j*potWidth + i] = fillColor;
|
else pixelsPOT[j*potWidth + i] = fillColor;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
TraceLog(WARNING, "Image converted to POT: (%ix%i) -> (%ix%i)", image->width, image->height, potWidth, potHeight);
|
TraceLog(WARNING, "Image converted to POT: (%ix%i) -> (%ix%i)", image->width, image->height, potWidth, potHeight);
|
||||||
|
|
||||||
free(image->pixels);
|
free(pixels); // Free pixels data
|
||||||
|
free(image->data); // Free old image data
|
||||||
|
|
||||||
image->pixels = imgDataPixelPOT;
|
int format = image->format; // Store image data format to reconvert later
|
||||||
image->width = potWidth;
|
|
||||||
image->height = potHeight;
|
*image = LoadImageEx(pixelsPOT, potWidth, potHeight);
|
||||||
|
|
||||||
|
free(pixelsPOT); // Free POT pixels data
|
||||||
|
|
||||||
|
ImageConvertFormat(image, format); // Reconvert image to previous format
|
||||||
}
|
}
|
||||||
*/
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Copy an image to a new image
|
// Copy an image to a new image
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue