File size: 2,958 Bytes
3450e6b
 
 
561087f
3450e6b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78

from OpenGL.GL import *

from utils.ColorMatrix import ColorMatrix


def set_uniform(uniform_name, value, type, shader_program):
    uniform_location = glGetUniformLocation(
        shader_program, f"u_{uniform_name}")
    if type == '1f':
        glUniform1f(uniform_location, value)
    elif type == '2fv':
        glUniform2fv(uniform_location, 1, (GLfloat * len(value))(*value))
    elif type == '4fv':
        glUniform4fv(uniform_location, 1, (GLfloat * len(value))(*value))
    elif type == 'mat4':
        glUniformMatrix4fv(uniform_location, 1, GL_FALSE,
                           (GLfloat * len(value))(*value))


def set_color_matrix_uniform(color_matrix, val_name, shader_program):
    t = color_matrix

    set_uniform(
        f"{val_name}Matrix",
        [t.a, t.b, t.c, t.d, t.f, t.g, t.h, t.i,
            t.k, t.l, t.m, t.n, t.p, t.q, t.r, t.s],
        'mat4',
        shader_program
    )

    set_uniform(f"{val_name}Matrix_vec", [
                t.e, t.j, t.o, t.t], '4fv', shader_program)
    set_uniform(f"{val_name}Offset", color_matrix.get_offsets(),
                '4fv', shader_program)


def set_options(exposure, saturation, contrast, brightness, gamma, shadows, highlights, whites, blacks,
                clarity, temperature, sharpness, shader_program, canvas_width, canvas_height):
    if not shader_program:
        print('Shader program not initialized')
        return

    set_uniform('gamma', gamma, '1f', shader_program)
    set_uniform('shadows', shadows, '1f', shader_program)
    set_uniform('highlights', highlights, '1f', shader_program)
    set_uniform('whites', whites, '1f', shader_program)
    set_uniform('blacks', blacks, '1f', shader_program)
    set_uniform('clarity', clarity, '1f', shader_program)
    set_uniform('temperature', temperature, '1f', shader_program)
    set_uniform('sharpness', sharpness, '1f', shader_program)

    color_matrix = ColorMatrix()
    color_matrix.multiply(ColorMatrix.create_exposure_matrix(exposure))
    print(vars(color_matrix))
    color_matrix.multiply(ColorMatrix.create_saturation_matrix(saturation + 1))
    print(vars(color_matrix))
    if contrast > 0:
        contrast = contrast * 2

    color_matrix.multiply(ColorMatrix.create_contrast_matrix(
        contrast + 1))
    print(vars(color_matrix))
    color_matrix.multiply(ColorMatrix.create_brightness_matrix(brightness))
    print(vars(color_matrix))
    set_color_matrix_uniform(color_matrix, 'color', shader_program)

    clarity_matrix = ColorMatrix()
    clarity_matrix.multiply(
        ColorMatrix.create_saturation_matrix(-0.3 * clarity + 1))
    clarity_matrix.multiply(
        ColorMatrix.create_contrast_matrix(0.1 * clarity + 1))
    set_color_matrix_uniform(clarity_matrix, 'clarity', shader_program)
    print('pixelDimension', [
        1 / canvas_width, 1 / canvas_height])
    set_uniform('pixelDimension', [
                1 / canvas_width, 1 / canvas_height], '2fv', shader_program)