// ==============================================================================
// GLOOK v1.0 TRIAL — Solo evaluacion — No para uso profesional
// (c) 2025 GLOOK — Creado por Geovanny Reyes — glook.es
// ==============================================================================

DEFINE_UI_PARAMS(p_Space,      Espacio Color,      DCTLUI_COMBO_BOX, 1, {REC709, DWG_DI, ACES_CCT}, {Rec709 Display, DaVinci WG DI, ACES cct})
DEFINE_UI_PARAMS(p_MidAdj,     Gris Medio Ajuste,  DCTLUI_SLIDER_FLOAT,  0.0, -0.05, 0.05, 0.001)
DEFINE_UI_PARAMS(p_Look,       Look,               DCTLUI_COMBO_BOX, 0, {BLOCKBUSTER, WARM_GOLD, KODAK_35MM, KODAK_GOLD, NORDIC, NEON_CITY, BLEACH}, {Blockbuster, Warm Gold, Kodak 35mm, Kodak Gold, Nordic, Neon City, Bleach})
DEFINE_UI_PARAMS(p_Intensity,  Intensidad,         DCTLUI_SLIDER_FLOAT,  1.0,  0.0, 2.0, 0.05)
DEFINE_UI_PARAMS(p_SkinProt,   Piel Proteccion,    DCTLUI_SLIDER_FLOAT,  0.92, 0.0, 1.0, 0.01)
DEFINE_UI_PARAMS(p_SatMode,    Sat Modo,           DCTLUI_COMBO_BOX, 0, {SAT_NORM, SAT_SUBT}, {Normal, Subtractiva})
DEFINE_UI_PARAMS(p_SatTrim,    Saturacion Trim,    DCTLUI_SLIDER_FLOAT,  0.0, -0.5, 0.5, 0.01)
DEFINE_UI_PARAMS(p_ConTrim,    Contraste Trim,     DCTLUI_SLIDER_FLOAT,  0.0, -0.3, 0.3, 0.01)
DEFINE_UI_PARAMS(p_ShadExp,    Sombras Exp,        DCTLUI_SLIDER_FLOAT,  0.0, -2.0, 2.0, 0.05)
DEFINE_UI_PARAMS(p_HiExp,      Luces Exp,          DCTLUI_SLIDER_FLOAT,  0.0, -2.0, 2.0, 0.05)
DEFINE_UI_PARAMS(p_ShadHue,    Sombras Hue,        DCTLUI_SLIDER_FLOAT,  0.0,  0.0, 360.0, 0.5)
DEFINE_UI_PARAMS(p_ShadHueStr, Sombras Fuerza,     DCTLUI_SLIDER_FLOAT,  0.0,  0.0, 2.0, 0.05)
DEFINE_UI_PARAMS(p_HiHue,      Luces Hue,          DCTLUI_SLIDER_FLOAT,  0.0,  0.0, 360.0, 0.5)
DEFINE_UI_PARAMS(p_HiHueStr,   Luces Fuerza,       DCTLUI_SLIDER_FLOAT,  0.0,  0.0, 2.0, 0.05)
DEFINE_UI_PARAMS(p_Mix,        Mix,                DCTLUI_SLIDER_FLOAT,  1.0,  0.0, 1.0, 0.01)

__DEVICE__ float luma_f(float r, float g, float b, int sp) {
    if(sp==2) return 0.2722f*r+0.6741f*g+0.0537f*b;
    return 0.2126f*r+0.7152f*g+0.0722f*b;
}
__DEVICE__ float sst(float e0, float e1, float x) {
    float d=e1-e0; if(d<0.00001f&&d>-0.00001f) return 0.5f;
    float t=(x-e0)/d; t=t<0.0f?0.0f:(t>1.0f?1.0f:t);
    return t*t*(3.0f-2.0f*t);
}
__DEVICE__ float3 hue_vec(float deg) {
    float hh=deg/360.0f; if(hh<0.0f)hh+=1.0f; if(hh>1.0f)hh-=1.0f;
    float tr=hh+0.333333f; if(tr>1.0f)tr-=1.0f;
    float tg=hh, tb=hh-0.333333f; if(tb<0.0f)tb+=1.0f;
    float rv,gv,bv;
    if(tr<0.166667f)rv=6.0f*tr; else if(tr<0.5f)rv=1.0f; else if(tr<0.666667f)rv=(0.666667f-tr)*6.0f; else rv=0.0f;
    if(tg<0.166667f)gv=6.0f*tg; else if(tg<0.5f)gv=1.0f; else if(tg<0.666667f)gv=(0.666667f-tg)*6.0f; else gv=0.0f;
    if(tb<0.166667f)bv=6.0f*tb; else if(tb<0.5f)bv=1.0f; else if(tb<0.666667f)bv=(0.666667f-tb)*6.0f; else bv=0.0f;
    return make_float3(rv-0.5f,gv-0.5f,bv-0.5f);
}
__DEVICE__ float skin_fast(float r, float g, float b) {
    if(r<g||r<b) return 0.0f;
    float mn=_fminf(g,b), d=r-mn; if(d<0.001f) return 0.0f;
    float hue=(g-b)/d; if(hue<0.0f)hue+=6.0f; hue*=60.0f;
    if(hue>55.0f) return 0.0f;
    float sat=d/(r+0.0001f); if(sat<0.04f||sat>0.90f) return 0.0f;
    return (1.0f-sst(32.0f,55.0f,hue))*sst(0.04f,0.14f,sat)*(1.0f-sst(0.70f,0.90f,sat));
}
__DEVICE__ float sh_mask_f(float lm, float mg, float sh_rng, int sp) {
    if(sp==0) return sst(0.28f, 0.05f, lm);
    return sst(mg, _fmaxf(mg-sh_rng,0.001f), lm);
}
__DEVICE__ float hi_mask_f(float lm, float mg, float hi_rng, int sp) {
    if(sp==0) return sst(0.72f, 0.95f, lm);
    return sst(mg, _fminf(mg+hi_rng,0.999f), lm);
}

__DEVICE__ int fp(int c, int px, int py) {
    if(px<0||px>4||py<0||py>6) return 0;
    int v=0;
    if(c==0){v=(py==0)?14:(py==1)?16:(py==2)?16:(py==3)?23:(py==4)?17:(py==5)?17:14;}
    else if(c==1){v=(py==0)?16:(py==1)?16:(py==2)?16:(py==3)?16:(py==4)?16:(py==5)?16:31;}
    else if(c==2||c==3){v=(py==0)?14:(py==1)?17:(py==2)?17:(py==3)?17:(py==4)?17:(py==5)?17:14;}
    else if(c==4){v=(py==0)?17:(py==1)?18:(py==2)?20:(py==3)?24:(py==4)?20:(py==5)?18:17;}
    else if(c==5){v=0;}
    else if(c==6){v=(py==0)?0:(py==1)?0:(py==2)?17:(py==3)?17:(py==4)?10:(py==5)?4:0;}
    else if(c==7){v=(py==0)?4:(py==1)?12:(py==2)?4:(py==3)?4:(py==4)?4:(py==5)?4:14;}
    return (v>>(4-px))&1;
}
__DEVICE__ int wm_at(int px, int py, int ax, int ay, int sc) {
    int lx=px-ax,ly=py-ay,th=7*sc,tw=48*sc;
    if(lx<0||lx>=tw||ly<0||ly>=th) return 0;
    int stride=6*sc,ci=lx/stride,cpx=(lx%stride)/sc,cpy=ly/sc;
    if(ci<0||ci>=8||cpx>=5) return 0;
    return fp(ci,cpx,cpy);
}
// (c) 2025 GLOOK — Creado por Geovanny Reyes — Todos los derechos reservados
__DEVICE__ float3 transform(int p_Width, int p_Height, int p_X, int p_Y, float p_R, float p_G, float p_B) {
    float or_=p_R,og=p_G,ob=p_B,r=p_R,g=p_G,b=p_B;
    float sk=skin_fast(r,g,b);
    float mg=0.435f;
    if(p_Space==1)mg=0.336f; else if(p_Space==2)mg=0.414f;
    mg+=p_MidAdj;
    float sh_hue=188.0f,sh_str=1.0f,hi_hue=32.0f,hi_str=0.9f;
    float sat_v=1.0f,con_v=0.0f,lift_v=0.0f,hi_desat=1.0f,sh_rng=0.28f,hi_rng=0.28f;
    float rl=0.0f,gl=0.0f,bl_=0.0f;
    if(p_Look==0){sh_hue=188.0f;sh_str=1.00f;hi_hue=32.0f;hi_str=0.88f;sat_v=1.00f;con_v=0.035f;lift_v=0.00f;hi_desat=1.00f;sh_rng=0.28f;hi_rng=0.28f;
    }else if(p_Look==1){sh_hue=38.0f;sh_str=0.55f;hi_hue=38.0f;hi_str=0.52f;sat_v=1.06f;con_v=0.022f;lift_v=0.010f;hi_desat=0.88f;sh_rng=0.26f;hi_rng=0.26f;rl=0.018f;gl=0.012f;bl_=0.006f;
    }else if(p_Look==2){sh_hue=182.0f;sh_str=0.20f;hi_hue=36.0f;hi_str=0.32f;sat_v=0.94f;con_v=0.015f;lift_v=0.00f;hi_desat=0.84f;sh_rng=0.28f;hi_rng=0.28f;rl=0.016f;gl=0.012f;bl_=0.008f;
    }else if(p_Look==3){sh_hue=42.0f;sh_str=0.35f;hi_hue=42.0f;hi_str=0.50f;sat_v=0.95f;con_v=0.015f;lift_v=0.00f;hi_desat=0.80f;sh_rng=0.28f;hi_rng=0.26f;rl=0.024f;gl=0.018f;bl_=0.010f;
    }else if(p_Look==4){sh_hue=225.0f;sh_str=0.85f;hi_hue=38.0f;hi_str=0.40f;sat_v=0.82f;con_v=0.07f;lift_v=0.00f;hi_desat=1.00f;sh_rng=0.28f;hi_rng=0.26f;
    }else if(p_Look==5){sh_hue=195.0f;sh_str=1.05f;hi_hue=315.0f;hi_str=0.85f;sat_v=1.10f;con_v=0.08f;lift_v=0.00f;hi_desat=1.00f;sh_rng=0.28f;hi_rng=0.26f;
    }else{sh_hue=0.0f;sh_str=0.0f;hi_hue=0.0f;hi_str=0.0f;sat_v=0.50f;con_v=0.15f;lift_v=0.020f;hi_desat=1.00f;sh_rng=0.28f;hi_rng=0.28f;}
    float eff_sh=sh_str*p_Intensity,eff_hi=hi_str*p_Intensity,eff_con=con_v+p_ConTrim;
    r+=rl*p_Intensity; g+=gl*p_Intensity; b+=bl_*p_Intensity;
    float lm=luma_f(r,g,b,p_Space);
    float shmk=sh_mask_f(lm,mg,sh_rng,p_Space)*eff_sh;
    float himk=hi_mask_f(lm,mg,hi_rng,p_Space)*eff_hi;
    float3 sho=hue_vec(sh_hue),hio=hue_vec(hi_hue); float sc=0.08f;
    r+=shmk*sho.x*sc+himk*hio.x*sc; g+=shmk*sho.y*sc+himk*hio.y*sc; b+=shmk*sho.z*sc+himk*hio.z*sc;
    r=_fmaxf(r,0.0f); g=_fmaxf(g,0.0f); b=_fmaxf(b,0.0f);
    float lm2=luma_f(r,g,b,p_Space);
    if(p_SatMode==1){
        float bs=_fmaxf(sat_v,0.0f); r=lm2+(r-lm2)*bs; g=lm2+(g-lm2)*bs; b=lm2+(b-lm2)*bs;
        float ds=_fmaxf(p_SatTrim,0.0f); if(ds>0.001f){float av=(r+g+b)*0.333333f;ds=_fminf(ds,1.0f);r=r+(av-r)*ds;g=g+(av-g)*ds;b=b+(av-b)*ds;}
    }else{float es=_fmaxf(sat_v+p_SatTrim,0.0f);r=lm2+(r-lm2)*es;g=lm2+(g-lm2)*es;b=lm2+(b-lm2)*es;}
    float cs2=1.0f+eff_con*p_Intensity;
    r=(r-mg)*cs2+mg; g=(g-mg)*cs2+mg; b=(b-mg)*cs2+mg;
    float lft=lift_v*p_Intensity;
    if(lft>0.0f){r=r+lft*(1.0f-r);g=g+lft*(1.0f-g);b=b+lft*(1.0f-b);}
    float lm3=luma_f(r,g,b,p_Space);
    if(lm3>hi_desat&&hi_desat<1.0f){float amt=(lm3-hi_desat)/(1.0f-hi_desat+0.001f);amt=_fminf(amt*amt*0.45f,0.45f)*p_Intensity;r=r+(lm3-r)*amt;g=g+(lm3-g)*amt;b=b+(lm3-b)*amt;}
    float lm4=luma_f(r,g,b,p_Space);
    float shm=sh_mask_f(lm4,mg,0.999f,p_Space),him=hi_mask_f(lm4,mg,0.999f,p_Space);
    float sd=p_ShadExp*0.18f*p_Intensity*shm,hd=p_HiExp*0.18f*p_Intensity*him;
    r+=sd+hd; g+=sd+hd; b+=sd+hd;
    if(p_ShadHueStr>0.001f){float cl=luma_f(r,g,b,p_Space),sm2=sh_mask_f(cl,mg,0.999f,p_Space);float3 sv=hue_vec(p_ShadHue);float sc2=0.10f*p_ShadHueStr*p_Intensity;r+=sm2*sv.x*sc2;g+=sm2*sv.y*sc2;b+=sm2*sv.z*sc2;r=_fmaxf(r,0.0f);g=_fmaxf(g,0.0f);b=_fmaxf(b,0.0f);}
    if(p_HiHueStr>0.001f){float cl=luma_f(r,g,b,p_Space),hm2=hi_mask_f(cl,mg,0.999f,p_Space);float3 hv=hue_vec(p_HiHue);float sc3=0.10f*p_HiHueStr*p_Intensity;r+=hm2*hv.x*sc3;g+=hm2*hv.y*sc3;b+=hm2*hv.z*sc3;}

    float prot=sk*p_SkinProt;
    r=r+(or_-r)*prot;g=g+(og-g)*prot;b=b+(ob-b)*prot;
    r=or_+(r-or_)*p_Mix;g=og+(g-og)*p_Mix;b=ob+(b-ob)*p_Mix;
    {
        int ph=p_Height>1?p_Height:1,pw=p_Width>1?p_Width:1;
        int wsc=ph/90; if(wsc<6)wsc=6;
        int wth=7*wsc,wtw=48*wsc,wmg=wsc*8;
        int ax0=wmg,ay0=wmg,ax1=pw-wmg-wtw,ay1=wmg,ax2=wmg,ay2=ph-wmg-wth;
        int ax3=pw-wmg-wtw,ay3=ph-wmg-wth,ax4=(pw-wtw)/2,ay4=(ph-wth)/2;
        int in_wm=wm_at(p_X,p_Y,ax0,ay0,wsc)||wm_at(p_X,p_Y,ax1,ay1,wsc)||
                  wm_at(p_X,p_Y,ax2,ay2,wsc)||wm_at(p_X,p_Y,ax3,ay3,wsc)||
                  wm_at(p_X,p_Y,ax4,ay4,wsc);
        if(in_wm){float wma=0.78f;r=r*(1.0f-wma)+1.0f*wma;g=g*(1.0f-wma)+1.0f*wma;b=b*(1.0f-wma)+1.0f*wma;}
    }
    return make_float3(r,g,b);
}