Josh Posted September 11, 2024 Posted September 11, 2024 The idea is to build a color LUT and apply it to textures to try to normalize colors. Some success. #include "UltraEngine.h" using namespace UltraEngine; int main(int argc, const char* argv[]) { //------------------------------------ // Build the color palette //------------------------------------ auto plug = LoadPlugin("Plugins/FITextureLoader"); auto palette = LoadPixmap(GetPath(PATH_DESKTOP) + "/hl2_brick.png"); int res = 32; float m = 256.0f / float(res); Vec3* colors = new Vec3[res * res * res]; float* colordistance = new float[res * res * res]; int r, g, b, a, pr, pg, pb; int x, y; float d; uint32_t rgba; Vec4 color4; Vec3 color3; Vec3 current; if (FileType(GetPath(PATH_DESKTOP) + "/lut.png") != 1) { auto samples = CreateBuffer(palette->size.x * sizeof(Vec4)); Print("Building palette"); for (x = 0; x < palette->size.x; ++x) { color4 = palette->Sample(iVec2(x, 0)); memcpy(samples->Data() + sizeof(Vec4) * x, &color4, sizeof(Vec4)); } for (r = 0; r < res; ++r) { Print(r); for (g = 0; g < res; ++g) { for (b = 0; b < res; ++b) { current = Vec3(float(r) / 255.0f, float(g) / 255.0f, float(b) / 255.0f) * m; for (x = 0; x < palette->size.x; ++x) { for (y = 0; y < palette->size.y; ++y) { //memcpy(&color3, samples->Data() + sizeof(Vec4) * x, sizeof(Vec3)); color4 = palette->Sample(iVec2(x, y)); color3 = color4.xyz(); d = current.DistanceToPoint(color3); if (x == 0) { colors[r * res * res + g * res + b] = color3; colordistance[r * res * res + g * res + b] = d; } else { if (d < colordistance[r * res * res + g * res + b]) { colors[r * res * res + g * res + b] = color3; colordistance[r * res * res + g * res + b] = d; } } } } } } } auto out = CreatePixmap(res * res, res); for (r = 0; r < res; ++r) { for (g = 0; g < res; ++g) { for (b = 0; b < res; ++b) { //out->WritePixel(r + b * res, g, Rgba(r * m, g * m, b * m, 255)); out->WritePixel(r + b * res, g, Vec4(colors[r * res * res + g * res + b], 1.0f)); } } } out->Save(GetPath(PATH_DESKTOP) + "/lut.png"); } Print("Processing image"); //------------------------------------ // Apply color palette to an image //------------------------------------ auto out = LoadPixmap(GetPath(PATH_DESKTOP) + "/lut.png"); auto pixmap = LoadPixmap(GetPath(PATH_DESKTOP) + "/pexels-chetanvlad-3373620.jpg"); std::vector<shared_ptr<Pixmap> > frames(out->size.y); for (int y = 0; y < out->size.y; ++y) { frames[y] = CreatePixmap(out->size.y, out->size.y); out->CopyRect(y * out->size.y, 0, out->size.y, out->size.y, frames[y], 0, 0); } for (int x = 0; x < pixmap->size.x; ++x) { for (int y = 0; y < pixmap->size.y; ++y) { rgba = pixmap->ReadPixel(x, y); r = Red(rgba); g = Green(rgba); b = Blue(rgba); a = Alpha(rgba); float unitsize = float(out->size.y) / float(out->size.x); float u = float(r) / 255.0f; float v = float(g) / 255.0f; int frame = Floor(float(b) / 255.0f * float(out->size.y)); frame = Clamp(frame, 0, frames.size() - 1); //Print(u); //Print(v); color4 = frames[frame]->Sample(u, v); color4.a = float(a) / 255.0f; pixmap->WritePixel(x, y, color4); } } pixmap->Save(StripExt(pixmap->path) + "_out.png"); return 0; } 1 Quote My job is to make tools you love, with the features you want, and performance you can't live without.
Josh Posted September 11, 2024 Author Posted September 11, 2024 Using a downsampled seed image works better because it gives you more of an average texture color: Before: After: Quote My job is to make tools you love, with the features you want, and performance you can't live without.
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.