Last active
August 7, 2023 10:36
-
-
Save jarikomppa/ff604dfd057428fb8bb014c5cd736ede to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#define _CRT_SECURE_NO_WARNINGS | |
#define STB_IMAGE_IMPLEMENTATION | |
#include "stb_image.h" | |
#define STB_IMAGE_WRITE_IMPLEMENTATION | |
#include "stb_image_write.h" | |
int tp[10000]; | |
int tps = 0; | |
void fillcell(int* img, int y, int x, int w, int fill) | |
{ | |
tp[tps] = y * w + x; tps++; | |
img[y * w + x] = fill; | |
if (x > 0 && img[y * w + x - 1] == 0xffffffff) fillcell(img, y, x - 1, w, fill); | |
if (y > 0 && img[y * w + x - w] == 0xffffffff) fillcell(img, y - 1, x, w, fill); | |
if (x < w-1 && img[y * w + x + 1] == 0xffffffff) fillcell(img, y, x + 1, w, fill); | |
if (y < w-1 && img[y * w + x + w] == 0xffffffff) fillcell(img, y + 1, x, w, fill); | |
} | |
void avgcell(int * img, int * tex) | |
{ | |
int r = 0, g = 0, b = 0; | |
for (int i = 0; i < tps; i++) | |
{ | |
int c = tex[tp[i]]; | |
r += (c >> 0) & 0xff; | |
g += (c >> 8) & 0xff; | |
b += (c >> 16) & 0xff; | |
} | |
int c = ((r / tps) << 0) | | |
((g / tps) << 8) | | |
((b / tps) << 16) | | |
0xff000000; | |
for (int i = 0; i < tps; i++) | |
img[tp[i]] = c; | |
} | |
int main() | |
{ | |
int x, y, c; | |
int* img = (int*)stbi_load("tilefile.png", &x, &y, &c, 4); | |
int* tex = (int*)stbi_load("tex.png", &x, &y, &c, 4); | |
for (int i = 0; i < y; i++) | |
for (int j = 0; j < x; j++) | |
if (img[i * x + j] == 0xffffffff) | |
{ | |
int fill = tex[i * x + j]; | |
if (fill == 0xffffffff) fill = 0xfffffffe; | |
tps = 0; | |
fillcell(img, i, j, x, fill); | |
avgcell(img, tex); | |
} | |
stbi_write_png("out.png", x, y, 4, img, x * 4); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment