Jump to content

LUT experiment for textures


Josh
 Share

Recommended Posts

The idea is to build a color LUT and apply it to textures to try to normalize colors. Some success.

lut.thumb.png.372c3ba13f2ef41321f0cafe53347601.pngneutral.thumb.png.8df3eb4d68e51104e1adb2e37132b794.png

image.png.0b08546d132354ad9cc1e9ff7c9a8723.png

#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;
}

grass_big_green.thumb.jpg.8bd01e5044542139e2be99a7b8ebdb2d.jpg

grass_big_green_out.thumb.png.c32a82f39a664293217450ff8df5765a.png

  • Like 1

My job is to make tools you love, with the features you want, and performance you can't live without.

Link to comment
Share on other sites

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.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...