aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorraysan5 <raysan5@gmail.com>2016-03-02 19:22:55 +0100
committerraysan5 <raysan5@gmail.com>2016-03-02 19:22:55 +0100
commitfffbf48dec317d55262de62f8bab757a31d0eebd (patch)
treec9488bd0c9daf8341d0e800363226d8c8249eebd /src
parenta167067cbd1a330d435dbe652c2ecbeda5e1f35d (diff)
downloadraylib-fffbf48dec317d55262de62f8bab757a31d0eebd.tar.gz
raylib-fffbf48dec317d55262de62f8bab757a31d0eebd.zip
Added support for Nearest-Neighbor image scaling
Specially useful on default font scaling
Diffstat (limited to 'src')
-rw-r--r--src/raylib.h1
-rw-r--r--src/textures.c38
2 files changed, 37 insertions, 2 deletions
diff --git a/src/raylib.h b/src/raylib.h
index 4f36e203..f8f1683e 100644
--- a/src/raylib.h
+++ b/src/raylib.h
@@ -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)
diff --git a/src/textures.c b/src/textures.c
index 36819daf..cb3113dc 100644
--- a/src/textures.c
+++ b/src/textures.c
@@ -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);