aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorRay <raysan5@gmail.com>2017-06-28 13:39:18 +0200
committerGitHub <noreply@github.com>2017-06-28 13:39:18 +0200
commit92ca68aac8e593b3df15a18ba86f8af0acff8921 (patch)
tree9397f2a6a93e6681d98508e3fbc36763848b1619 /src
parentc46abd34d4fb86ffd08c5e25ded52ac6e52efbf0 (diff)
parentfcd13fd5d22219d8ed69b88e9cc1138b96b9a16d (diff)
downloadraylib-92ca68aac8e593b3df15a18ba86f8af0acff8921.tar.gz
raylib-92ca68aac8e593b3df15a18ba86f8af0acff8921.zip
Merge pull request #309 from nounoursheureux/image_gen
Add some image generation functions
Diffstat (limited to 'src')
-rw-r--r--src/raylib.h7
-rw-r--r--src/textures.c144
2 files changed, 150 insertions, 1 deletions
diff --git a/src/raylib.h b/src/raylib.h
index 6d597fef..29cc5728 100644
--- a/src/raylib.h
+++ b/src/raylib.h
@@ -873,6 +873,13 @@ RLAPI void ImageColorGrayscale(Image *image);
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)
+// Image generation functions
+RLAPI Image GenImageGradientV(int width, int height, Color top, Color bottom); // Generate image: vertical gradient
+RLAPI Image GenImageGradientH(int width, int height, Color left, Color right); // Generate image: horizontal gradient
+RLAPI Image GenImageChecked(int width, int height, int checksX, int checksY, Color col1, Color col2); // Generate image: checked
+RLAPI Image GenImageWhiteNoise(int width, int height, float factor); // Generate image: white noise
+RLAPI Image GenImageCellular(int width, int height, int tileSize); // Generate image: cellular algorithm. Bigger tileSize means bigger cells
+
// Texture2D configuration functions
RLAPI void GenTextureMipmaps(Texture2D *texture); // Generate GPU mipmaps for a texture
RLAPI void SetTextureFilter(Texture2D texture, int filterMode); // Set texture scaling filter mode
diff --git a/src/textures.c b/src/textures.c
index d249d2be..99392516 100644
--- a/src/textures.c
+++ b/src/textures.c
@@ -1431,6 +1431,148 @@ void ImageColorBrightness(Image *image, int brightness)
}
#endif // SUPPORT_IMAGE_MANIPULATION
+// Generate image: vertical gradient
+Image GenImageGradientV(int width, int height, Color top, Color bottom)
+{
+ Color *pixels = (Color *)malloc(width*height*sizeof(Color));
+
+ for (int j = 0; j < height; j++)
+ {
+ float factor = (float)j / (float)height;
+ for (int i = 0; i < width; i++)
+ {
+ pixels[j*width + i].r = (int)((float)bottom.r * factor + (float)top.r * (1.f - factor));
+ pixels[j*width + i].g = (int)((float)bottom.g * factor + (float)top.g * (1.f - factor));
+ pixels[j*width + i].b = (int)((float)bottom.b * factor + (float)top.b * (1.f - factor));
+ pixels[j*width + i].a = (int)((float)bottom.a * factor + (float)top.a * (1.f - factor));
+ }
+ }
+
+ Image image = LoadImageEx(pixels, width, height);
+ free(pixels);
+
+ return image;
+}
+
+// Generate image: horizontal gradient
+Image GenImageGradientH(int width, int height, Color left, Color right)
+{
+ Color *pixels = (Color *)malloc(width*height*sizeof(Color));
+
+ for (int i = 0; i < width; i++)
+ {
+ float factor = (float)i / (float)width;
+ for (int j = 0; j < height; j++)
+ {
+ pixels[j*width + i].r = (int)((float)right.r * factor + (float)left.r * (1.f - factor));
+ pixels[j*width + i].g = (int)((float)right.g * factor + (float)left.g * (1.f - factor));
+ pixels[j*width + i].b = (int)((float)right.b * factor + (float)left.b * (1.f - factor));
+ pixels[j*width + i].a = (int)((float)right.a * factor + (float)left.a * (1.f - factor));
+ }
+ }
+
+ Image image = LoadImageEx(pixels, width, height);
+ free(pixels);
+
+ return image;
+}
+
+// Generate image: checked
+Image GenImageChecked(int width, int height, int checksX, int checksY, Color col1, Color col2)
+{
+ Color *pixels = (Color *)malloc(width*height*sizeof(Color));
+
+ for (int y = 0; y < height; y++)
+ {
+ for (int x = 0; x < width; x++)
+ {
+ if ((x/checksX + y/checksY) % 2 == 0) pixels[y*width + x] = col1;
+ else pixels[y*width + x] = col2;
+ }
+ }
+
+ Image image = LoadImageEx(pixels, width, height);
+ free(pixels);
+
+ return image;
+}
+
+// Generate image: white noise
+Image GenImageWhiteNoise(int width, int height, float factor)
+{
+ Color *pixels = (Color *)malloc(width*height*sizeof(Color));
+
+ for (int i = 0; i < width*height; i++)
+ {
+ if (GetRandomValue(0, 99) < (int)(factor * 100.f)) pixels[i] = WHITE;
+ else pixels[i] = BLACK;
+ }
+
+ Image image = LoadImageEx(pixels, width, height);
+ free(pixels);
+
+ return image;
+}
+
+// Generate image: cellular algorithm. Bigger tileSize means bigger cells
+Image GenImageCellular(int width, int height, int tileSize)
+{
+ Color *pixels = (Color*)malloc(width*height*sizeof(Color));
+
+ int seeds_per_row = width / tileSize;
+ int seeds_per_col = height / tileSize;
+ int seeds_count = seeds_per_row * seeds_per_col;
+
+ Vector2* seeds = (Vector2*)malloc(seeds_count * sizeof(Vector2));
+
+ for (int i = 0; i < seeds_count; i++)
+ {
+ int y = (i / seeds_per_row) * tileSize + GetRandomValue(0, tileSize-1);
+ int x = (i % seeds_per_row) * tileSize + GetRandomValue(0, tileSize-1);
+ seeds[i] = (Vector2){x, y};
+ }
+
+ for (int y = 0; y < height; y++)
+ {
+ int tile_y = y / tileSize;
+ for (int x = 0; x < width; x++)
+ {
+ int tile_x = x / tileSize;
+
+ float min_distance = strtod("Inf", NULL);
+
+ // Check all adjacent tiles
+ for (int i = -1; i < 2; i++)
+ {
+ if (tile_x + i < 0 || tile_x + i >= seeds_per_row) continue;
+
+ for (int j = -1; j < 2; j++)
+ {
+ if (tile_y + j < 0 || tile_y + j >= seeds_per_col) continue;
+
+ Vector2 neighbor_seed = seeds[(tile_y+j) * seeds_per_row + tile_x+i];
+
+ float dist = hypot(x - (int)neighbor_seed.x, y - (int)neighbor_seed.y);
+ min_distance = fmin(min_distance, dist);
+ }
+ }
+
+ // I made this up but it seems to give good results at all tile sizes
+ int intensity = (int)(min_distance * 256.f / tileSize);
+ if (intensity > 255) intensity = 255;
+
+ Color c = {intensity, intensity, intensity, 255};
+ pixels[y*width + x] = c;
+ }
+ }
+ free(seeds);
+
+ Image image = LoadImageEx(pixels, width, height);
+ free(pixels);
+
+ return image;
+}
+
// Generate GPU mipmaps for a texture
void GenTextureMipmaps(Texture2D *texture)
{
@@ -2250,4 +2392,4 @@ static Image LoadASTC(const char *fileName)
return image;
}
-#endif \ No newline at end of file
+#endif