Skip to content

Commit

Permalink
reduced GLSL node footprint for conversions -- sorry if that blew som…
Browse files Browse the repository at this point in the history
…eone's network!

new GLSL COLOR PALETTE node based on cosines
new GLSL HSV ADJUST
  • Loading branch information
Amorano committed Sep 9, 2024
1 parent 7a7ec8a commit 19e3217
Show file tree
Hide file tree
Showing 21 changed files with 352 additions and 299 deletions.
1 change: 0 additions & 1 deletion core/compose.py
Original file line number Diff line number Diff line change
Expand Up @@ -404,7 +404,6 @@ def run(self, **kw) -> Tuple[torch.Tensor, torch.Tensor, torch.Tensor]:
case EnumColorMatchMode.REINHARD:
pA = color_match_reinhard(pA, pB)


if invert == True:
pA = image_invert(pA, 1)

Expand Down
23 changes: 16 additions & 7 deletions core/create_glsl.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@
from Jovimetrix.sup.image import MIN_IMAGE_SIZE, EnumInterpolation, \
EnumScaleMode, cv2tensor_full, image_convert, image_scalefit, tensor2cv

from Jovimetrix.sup.shader import PTYPE, CompileException, EnumEdgeGLSL, \
import Jovimetrix.sup.shader as glsl_enums
from Jovimetrix.sup.shader import PTYPE, CompileException, EnumGLSLEdge, \
GLSLShader, shader_meta

# =============================================================================
Expand Down Expand Up @@ -102,8 +103,8 @@ def INPUT_TYPES(cls) -> dict:
Lexicon.WH: ("VEC2INT", {"default": (512, 512), "mij":MIN_IMAGE_SIZE, "label": [Lexicon.W, Lexicon.H]}),
Lexicon.SAMPLE: (EnumInterpolation._member_names_, {"default": EnumInterpolation.LANCZOS4.name}),
Lexicon.MATTE: ("VEC4INT", {"default": (0, 0, 0, 255), "rgb": True}),
Lexicon.EDGE_X: (EnumEdgeGLSL._member_names_, {"default": EnumEdgeGLSL.CLAMP.name}),
Lexicon.EDGE_Y: (EnumEdgeGLSL._member_names_, {"default": EnumEdgeGLSL.CLAMP.name}),
Lexicon.EDGE_X: (EnumGLSLEdge._member_names_, {"default": EnumGLSLEdge.CLAMP.name}),
Lexicon.EDGE_Y: (EnumGLSLEdge._member_names_, {"default": EnumGLSLEdge.CLAMP.name}),
}
})
return Lexicon._parse(d, cls)
Expand All @@ -124,8 +125,8 @@ def run(self, ident, **kw) -> tuple[torch.Tensor]:
sample = parse_param(kw, Lexicon.SAMPLE, EnumConvertType.STRING, EnumInterpolation.LANCZOS4.name)[0]
sample = EnumInterpolation[sample]
matte = parse_param(kw, Lexicon.MATTE, EnumConvertType.VEC4INT, [(0, 0, 0, 255)], 0, 255)[0]
edge_x = parse_param(kw, Lexicon.EDGE_X, EnumConvertType.STRING, EnumEdgeGLSL.CLAMP.name)[0]
edge_y = parse_param(kw, Lexicon.EDGE_Y, EnumConvertType.STRING, EnumEdgeGLSL.CLAMP.name)[0]
edge_x = parse_param(kw, Lexicon.EDGE_X, EnumConvertType.STRING, EnumGLSLEdge.CLAMP.name)[0]
edge_y = parse_param(kw, Lexicon.EDGE_Y, EnumConvertType.STRING, EnumGLSLEdge.CLAMP.name)[0]
edge = (edge_x, edge_y)

try:
Expand Down Expand Up @@ -239,8 +240,16 @@ def INPUT_TYPES(cls) -> dict:
type_name = typ.name
if glsl_type != 'sampler2D':
if default is not None:
d = default.split(',')
params['default'] = parse_value(d, typ, 0)
if default.startswith('EnumGLSL'):
if (target_enum := getattr(glsl_enums, default.strip(), None)) is not None:
# this be an ENUM....
type_name = target_enum._member_names_
params['default'] = type_name[0]
else:
params['default'] = 0
else:
d = default.split(',')
params['default'] = parse_value(d, typ, 0)

if val_min is not None:
params['mij'] = parse_value(val_min, EnumConvertType.FLOAT, -sys.maxsize)
Expand Down
15 changes: 4 additions & 11 deletions node_list.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,23 +15,16 @@
"FLATTEN (JOV) \u2b07\ufe0f": "Combine multiple input images into a single image by summing their pixel values",
"GLSL (JOV) \ud83c\udf69": "Execute custom GLSL (OpenGL Shading Language) fragment shaders to generate images or apply effects",
"GLSL BLEND LINEAR (JOV) \ud83e\uddd9\ud83c\udffd": "Simple linear blend between two images",
"GLSL COLOR CONVERSION (JOV) \ud83e\uddd9\ud83c\udffd": "Convert an image from one color space (RGB, HSV, LAB, XYZ) to another",
"GLSL COLOR PALETTE (JOV) \ud83e\uddd9\ud83c\udffd": "COLOR PALETTE",
"GLSL CONICAL GRADIENT (JOV) \ud83e\uddd9\ud83c\udffd": "Generate a conical gradient from black to white",
"GLSL DIRECTIONAL WARP (JOV) \ud83e\uddd9\ud83c\udffd": "Domain warp an image with a direction and distortion map",
"GLSL GRAYSCALE (JOV) \ud83e\uddd9\ud83c\udffd": "Convert input to grayscale",
"GLSL HSV-2-LAB (JOV) \ud83e\uddd9\ud83c\udffd": "Convert HSV image into LAB color space",
"GLSL HSV-2-RGB (JOV) \ud83e\uddd9\ud83c\udffd": "Convert HSV image into RGB color space",
"GLSL LAB-2-HSV (JOV) \ud83e\uddd9\ud83c\udffd": "Convert LAB image into HSV color space",
"GLSL LAB-2-RGB (JOV) \ud83e\uddd9\ud83c\udffd": "Convert LAB image into RGB color space",
"GLSL LAB-2-XYZ (JOV) \ud83e\uddd9\ud83c\udffd": "Convert LAB image into XYZ color space",
"GLSL HSV ADJUST (JOV) \ud83e\uddd9\ud83c\udffd": "Hue, Saturation and Value adjustment control",
"GLSL NORMAL (JOV) \ud83e\uddd9\ud83c\udffd": "Convert input into a Normal map",
"GLSL NORMAL BLEND (JOV) \ud83e\uddd9\ud83c\udffd": "Blend two Normal maps",
"GLSL POSTERIZE (JOV) \ud83e\uddd9\ud83c\udffd": "Reduce the pixel color data range",
"GLSL RGB-2-HSV (JOV) \ud83e\uddd9\ud83c\udffd": "Convert RGB(A) input into HSV color space",
"GLSL RGB-2-LAB (JOV) \ud83e\uddd9\ud83c\udffd": "Convert RGB(A) image into LAB color space",
"GLSL RGB-2-XYZ (JOV) \ud83e\uddd9\ud83c\udffd": "Convert RGB(A) input into XYZ color space",
"GLSL TRANSFORM (JOV) \ud83e\uddd9\ud83c\udffd": "TRANSFORM",
"GLSL XYZ-2-LAB (JOV) \ud83e\uddd9\ud83c\udffd": "Convert XYZ(W) image into LAB color space",
"GLSL XYZ-2-RGB (JOV) \ud83e\uddd9\ud83c\udffd": "Convert XYZ(W) image into RGB color space",
"GLSL TRANSFORM (JOV) \ud83e\uddd9\ud83c\udffd": "Abuse UV space to create repetitions",
"GRADIENT MAP (JOV) \ud83c\uddf2\ud83c\uddfa": "Remaps an input image using a gradient lookup table (LUT)",
"GRAPH (JOV) \ud83d\udcc8": "Visualize a series of data points over time",
"IMAGE INFO (JOV) \ud83d\udcda": "Exports and Displays immediate information about images",
Expand Down
178 changes: 178 additions & 0 deletions res/glsl/color/color-convert.frag
Original file line number Diff line number Diff line change
@@ -0,0 +1,178 @@
// name: COLOR CONVERSION
// desc: Convert an image from one color space (RGB, HSV, LAB, XYZ) to another.
// category: COLOR

uniform sampler2D image; // | Image to convert
uniform int operator; // EnumGLSLColorConvert | conversion operation to perform.

const vec3 D65 = vec3(95.047, 100.0, 108.883);

// =============================================================================
// PROTOTYPES
// =============================================================================

vec3 rgb2xyz(vec3 rgb);
vec3 xyz2rgb(vec3 xyz);
vec3 xyz2lab(vec3 xyz);

// =============================================================================
// RGB
// =============================================================================

vec3 rgb2hsv(vec3 rgb) {
vec4 K = vec4(0.0, -1.0 / 3.0, 2.0 / 3.0, -1.0);
vec4 p = mix(vec4(rgb.bg, K.wz), vec4(rgb.gb, K.xy), step(rgb.b, rgb.g));
vec4 q = mix(vec4(p.xyw, rgb.r), vec4(rgb.r, p.yzx), step(p.x, rgb.r));
float d = q.x - min(q.w, q.y);
return vec3(abs(q.z + (q.w - q.y) / (6.0 * d + M_EPSILON)), d / (q.x + M_EPSILON), q.x);
}

vec3 rgb2lab(vec3 rgb) {
vec3 xyz = rgb2xyz(rgb);
return xyz2lab(xyz);
}

vec3 rgb2xyz(vec3 rgb) {
vec3 tmp;
tmp.x = (rgb.r > 0.04045) ? pow((rgb.r + 0.055) / 1.055, 2.4) : rgb.r / 12.92;
tmp.y = (rgb.g > 0.04045) ? pow((rgb.g + 0.055) / 1.055, 2.4) : rgb.g / 12.92;
tmp.z = (rgb.b > 0.04045) ? pow((rgb.b + 0.055) / 1.055, 2.4) : rgb.b / 12.92;
return 100.0 * tmp * mat3(
0.4124, 0.3576, 0.1805,
0.2126, 0.7152, 0.0722,
0.0193, 0.1192, 0.9505
);
}

// =============================================================================
// HSV
// =============================================================================

vec3 hsv2rgb(vec3 hsv) {
hsv = vec3(hsv.x, clamp(hsv.yz, 0.0, 1.0));
vec4 K = vec4(1.0, 2.0 / 3.0, 1.0 / 3.0, 3.0);
vec3 p = abs(fract(hsv.xxx + K.xyz) * 6.0 - K.www);
return hsv.z * mix(K.xxx, clamp(p - K.xxx, 0.0, 1.0), hsv.y);
}

vec3 hsv2lab(vec3 hsv) {
float H = hsv.x * 360.0;
float S = hsv.y;
float V = hsv.z;

// Convert to LAB
float L = V * 100.0;
float C = S * L;

float h = H * M_PI / 180.0;
float a = C * cos(h);
float b = C * sin(h);

// Normalize LAB
return vec3(L / 100.0, (a + 128.0) / 255.0, (b + 128.0) / 255.0);
}

vec3 hsv2xyz(vec3 hsv) {
vec3 rgb = hsv2rgb(hsv);
return rgb2xyz(rgb);
}

// =============================================================================
// LAB
// =============================================================================

vec3 lab2xyz(vec3 lab) {
float fy = (lab.x + 16.0) / 116.0;
float fx = lab.y / 500.0 + fy;
float fz = fy - lab.z / 200.0;
return vec3(
95.047 * ((fx > 0.206897) ? fx * fx * fx : (fx - 16.0 / 116.0) / 7.787),
100.000 * ((fy > 0.206897) ? fy * fy * fy : (fy - 16.0 / 116.0) / 7.787),
108.883 * ((fz > 0.206897) ? fz * fz * fz : (fz - 16.0 / 116.0) / 7.787)
);
}

vec3 lab2rgb(vec3 lab) {
vec3 xyz = lab2xyz(lab);
return xyz2rgb(xyz);
}

vec3 lab2hsv(vec3 lab) {
vec3 rgb = lab2rgb(lab);
return rgb2hsv(rgb);
}

// =============================================================================
// XYZ
// =============================================================================

vec3 xyz2rgb(vec3 xyz) {
vec3 v = xyz / 100.0 * mat3(
3.2406, -1.5372, -0.4986,
-0.9689, 1.8758, 0.0415,
0.0557, -0.2040, 1.0570
);
vec3 r;
r.x = ( v.r > 0.0031308 ) ? (( 1.055 * pow( v.r, ( 1.0 / 2.4 ))) - 0.055 ) : 12.92 * v.r;
r.y = ( v.g > 0.0031308 ) ? (( 1.055 * pow( v.g, ( 1.0 / 2.4 ))) - 0.055 ) : 12.92 * v.g;
r.z = ( v.b > 0.0031308 ) ? (( 1.055 * pow( v.b, ( 1.0 / 2.4 ))) - 0.055 ) : 12.92 * v.b;
return r;
}

vec3 xyz2hsv(vec3 xyz) {
vec3 rgb = xyz2rgb(xyz);
return rgb2hsv(rgb);
}

vec3 xyz2lab(vec3 xyz) {
vec3 n = xyz / D65;
vec3 v;
v.x = ( n.x > 0.008856 ) ? pow( n.x, 1.0 / 3.0 ) : ( 7.787 * n.x ) + ( 16.0 / 116.0 );
v.y = ( n.y > 0.008856 ) ? pow( n.y, 1.0 / 3.0 ) : ( 7.787 * n.y ) + ( 16.0 / 116.0 );
v.z = ( n.z > 0.008856 ) ? pow( n.z, 1.0 / 3.0 ) : ( 7.787 * n.z ) + ( 16.0 / 116.0 );
return vec3(( 116.0 * v.y ) - 16.0, 500.0 * ( v.x - v.y ), 200.0 * ( v.y - v.z ));
}

// =============================================================================
// SELECTOR
// =============================================================================

vec3 convertColor(vec3 color, int operator) {
// RGB
if (operator == 0) {
return rgb2hsv(color);
} else if (operator == 1) {
return rgb2lab(color);
} else if (operator == 2) {
return rgb2xyz(color);
// HSV
} else if (operator == 10) {
return hsv2rgb(color);
} else if (operator == 11) {
return hsv2lab(color);
} else if (operator == 12) {
return hsv2xyz(color);
// LAB
} else if (operator == 20) {
return lab2rgb(color);
} else if (operator == 21) {
return lab2hsv(color);
} else if (operator == 22) {
return lab2xyz(color);
// XYZ
} else if (operator == 30) {
return xyz2rgb(color);
} else if (operator == 31) {
return xyz2hsv(color);
} else if (operator == 32) {
return xyz2lab(color);
}
return color;
}

void mainImage(out vec4 fragColor, vec2 fragCoord) {
vec2 uv = fragCoord / iResolution.xy;
vec4 color = texture(image, uv);
vec3 rgb = convertColor(color.rgb, operator);
fragColor = vec4(rgb, color.a);
}
38 changes: 38 additions & 0 deletions res/glsl/color/color-hsv.frag
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// name: HSV ADJUST
// desc: Hue, Saturation and Value adjustment control. Maintains alpha/mask.
// category: COLOR

uniform sampler2D image; // | RGB(A) image
uniform vec3 HSV; // 0.,1.,1.;-1;2;0.01 | Adjust the Hue, Saturation or Value

vec3 rgb2hsv(vec3 c) {
vec4 K = vec4(0.0, -1.0 / 3.0, 2.0 / 3.0, -1.0);
vec4 p = mix(vec4(c.bg, K.wz), vec4(c.gb, K.xy), step(c.b, c.g));
vec4 q = mix(vec4(p.xyw, c.r), vec4(c.r, p.yzx), step(p.x, c.r));
float d = q.x - min(q.w, q.y);
return vec3(abs(q.z + (q.w - q.y) / (6.0 * d + M_EPSILON)), d / (q.x + M_EPSILON), q.x);
}

vec3 hsv2rgb(vec3 c) {
vec3 rgb = clamp(abs(mod(c.x * 6.0 + vec3(0.0, 4.0, 2.0), 6.0) - 3.0) - 1.0, 0.0, 1.0);
return c.z * mix(vec3(1.0), rgb, c.y);
}

void mainImage(out vec4 fragColor, vec2 fragCoord) {
vec2 uv = fragCoord.xy / iResolution.xy;
vec4 color = texture(image, uv);
vec3 rgb = color.rgb;
vec3 hsv = rgb2hsv(rgb);

// Adjust hue
hsv.x = mod(hsv.x + HSV.x, 1.0);

// Adjust saturation
hsv.y = clamp(hsv.y * HSV.y, 0.0, 1.0);

// Adjust value
hsv.z = clamp(hsv.z * HSV.z, 0.0, 1.0);

rgb = hsv2rgb(hsv);
fragColor = vec4(rgb, color.a);
}
32 changes: 0 additions & 32 deletions res/glsl/color/convert/color-convert_hsv2lab.frag

This file was deleted.

18 changes: 0 additions & 18 deletions res/glsl/color/convert/color-convert_hsv2rgb.frag

This file was deleted.

32 changes: 0 additions & 32 deletions res/glsl/color/convert/color-convert_lab2hsv.frag

This file was deleted.

Loading

0 comments on commit 19e3217

Please sign in to comment.