Added support for Nearest-Neighbor image scaling
Specially useful on default font scaling
This commit is contained in:
parent
a167067cbd
commit
fffbf48dec
@ -691,6 +691,7 @@ void ImageDither(Image *image, int rBpp, int gBpp, int bBpp, int aBpp);
|
||||
Image ImageCopy(Image image); // Create an image duplicate (useful for transformations)
|
||||
void ImageCrop(Image *image, Rectangle crop); // Crop an image to a defined rectangle
|
||||
void ImageResize(Image *image, int newWidth, int newHeight); // Resize and image (bilinear filtering)
|
||||
void ImageResizeNN(Image *image,int newWidth,int newHeight); // Resize and image (Nearest-Neighbor scaling algorithm)
|
||||
void ImageDraw(Image *dst, Image src, Rectangle srcRec, Rectangle dstRec); // Draw a source image within a destination image
|
||||
Image ImageText(const char *text, int fontSize, Color color); // Create an image from text (default font)
|
||||
Image ImageTextEx(SpriteFont font, const char *text, int fontSize, int spacing, Color tint); // Create an image from text (custom sprite font)
|
||||
|
@ -919,6 +919,39 @@ void ImageResize(Image *image, int newWidth, int newHeight)
|
||||
free(pixels);
|
||||
}
|
||||
|
||||
// Resize and image to new size using Nearest-Neighbor scaling algorithm
|
||||
void ImageResizeNN(Image *image,int newWidth,int newHeight)
|
||||
{
|
||||
Color *pixels = GetImageData(*image);
|
||||
Color *output = (Color *)malloc(newWidth*newHeight*sizeof(Color));
|
||||
|
||||
// EDIT: added +1 to account for an early rounding problem
|
||||
int x_ratio = (int)((image->width<<16)/newWidth) + 1;
|
||||
int y_ratio = (int)((image->height<<16)/newHeight) + 1;
|
||||
|
||||
int x2, y2;
|
||||
for (int i = 0; i < newHeight; i++)
|
||||
{
|
||||
for (int j = 0; j < newWidth; j++)
|
||||
{
|
||||
x2 = ((j*x_ratio) >> 16);
|
||||
y2 = ((i*y_ratio) >> 16);
|
||||
|
||||
output[(i*newWidth) + j] = pixels[(y2*image->width) + x2] ;
|
||||
}
|
||||
}
|
||||
|
||||
int format = image->format;
|
||||
|
||||
UnloadImage(*image);
|
||||
|
||||
*image = LoadImageEx(output, newWidth, newHeight);
|
||||
ImageFormat(image, format); // Reformat 32bit RGBA image to original format
|
||||
|
||||
free(output);
|
||||
free(pixels);
|
||||
}
|
||||
|
||||
// Draw an image (source) within an image (destination)
|
||||
void ImageDraw(Image *dst, Image src, Rectangle srcRec, Rectangle dstRec)
|
||||
{
|
||||
@ -1046,8 +1079,9 @@ Image ImageTextEx(SpriteFont font, const char *text, int fontSize, int spacing,
|
||||
float scaleFactor = (float)fontSize/imSize.y;
|
||||
TraceLog(INFO, "Scalefactor: %f", scaleFactor);
|
||||
|
||||
// TODO: Allow nearest-neighbor scaling algorithm
|
||||
ImageResize(&imText, (int)(imSize.x*scaleFactor), (int)(imSize.y*scaleFactor));
|
||||
// Using nearest-neighbor scaling algorithm for default font
|
||||
if (font.texture.id == GetDefaultFont().texture.id) ImageResizeNN(&imText, (int)(imSize.x*scaleFactor), (int)(imSize.y*scaleFactor));
|
||||
else ImageResize(&imText, (int)(imSize.x*scaleFactor), (int)(imSize.y*scaleFactor));
|
||||
}
|
||||
|
||||
free(pixels);
|
||||
|
Loading…
Reference in New Issue
Block a user