summaryrefslogtreecommitdiff
path: root/gui/src/ofxGpuLutBlend.cpp
blob: 4b469edc4c58708691759d57d4667be4e86a9467 (plain)
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
#include "ofxGpuLutBlend.h"

/*
ofxGpuLutBlend::ofxGpuLutBlend(){}
ofxGpuLutBlend::~ofxGpuLutBlend(){}

void ofxGpuLutBlend::load(ofTexture lutTexture){
    
    if(ofIsGLProgrammableRenderer()){
        vertexShader = "#version 150\n";
        vertexShader += STRINGIFY(
                                  uniform mat4 projectionMatrix;
                                  uniform mat4 modelViewMatrix;
                                  uniform mat4 modelViewProjectionMatrix;
                                  
                                  in vec4  position;
                                  in vec2  texcoord;
                                  
                                  out vec2 texCoordVarying;
                                  
                                  void main()
                                  {
                                      texCoordVarying = texcoord;
                                      gl_Position = modelViewProjectionMatrix * position;
                                  }
                                  );
        
        fragmentShader = "#version 150\n";
        fragmentShader += STRINGIFY(
                                    uniform sampler2DRect tex;
                                    uniform sampler2DRect lut;
                                    uniform float blend;
                                    uniform float size = 64.0;
                                    
                                    in vec2 texCoordVarying;
                                    
                                    out vec4 fragColor;
                                    
                                    // Texture coordinates
                                    vec2 texcoord0 = texCoordVarying;
                                    
                                    
                                    
                                    void main( void )
                                    {
                                        vec3 rawColor = texture(tex, texcoord0).rgb;
                                        //float rawAlpha = texture(tex, texcoord0).a;
                                        
                                        if (blend <= 0.0) {
                                            fragColor = vec4(rawColor, 0.0);
                                        }
                                        else {
                                            vec3 originalColor = floor(texture(tex, texcoord0).rgb * vec3(size - 1.0));
                                            vec2 blueIndex = vec2(mod(originalColor.b, sqrt(size)), floor(originalColor.b / sqrt(size)));
                                            vec2 index = vec2((size * blueIndex.x + originalColor.r) + 0.5, (size * blueIndex.y + originalColor.g) + 0.5);
                                            fragColor = vec4(texture(lut, index).rgb, blend);
                                        }
                                    }
                                    );
        
        lutShader.setupShaderFromSource(GL_VERTEX_SHADER, vertexShader);
        lutShader.setupShaderFromSource(GL_FRAGMENT_SHADER, fragmentShader);
        lutShader.bindDefaults();
        lutShader.linkProgram();
    }
    else {
        fragmentShader = "#version 120\n#extension GL_ARB_texture_rectangle : enable\n";
        fragmentShader += STRINGIFY(
                                    uniform sampler2DRect tex;
                                    uniform sampler2DRect lut;
                                    uniform float blend;
                                    uniform float size = 64.0;
                                    
                                    void main( void )
                                    {
                                        vec3 rawColor = texture2DRect(tex, gl_TexCoord[0].st).rgb;
                                        //float rawAlpha = texture2DRect(tex, gl_TexCoord[0].st).a;
                                        
                                        if (blend <= 0.0) {
                                            gl_FragColor = vec4(rawColor, 0.0);
                                        }
                                        else {
                                            vec3 originalColor = floor(texture2DRect(tex, gl_TexCoord[0].st).rgb * vec3(size - 1.0));
                                            vec2 blueIndex = vec2(mod(originalColor.b, sqrt(size)), floor(originalColor.b / sqrt(size)));
                                            vec2 index = vec2((size * blueIndex.x + originalColor.r) + 0.5, (size * blueIndex.y + originalColor.g) + 0.5);
                                            gl_FragColor = vec4(texture2DRect(lut, index).rgb, blend);
                                        }
                                    }
                                    );
        
        
        lutShader.unload();
        lutShader.setupShaderFromSource(GL_FRAGMENT_SHADER, fragmentShader);
        lutShader.linkProgram();
    }
    
    lut.setTextureWrap(GL_CLAMP_TO_EDGE, GL_CLAMP_TO_EDGE);
    lut.setTextureMinMagFilter(GL_NEAREST, GL_NEAREST);
    if(!ofGetUsingArbTex()){
        ofEnableArbTex();
        lut = lutTexture;
        ofDisableArbTex();
    }else{
        lut = lutTexture;
    }
    blendAmt=1.0f;
}
void ofxGpuLutBlend::setBlend(float amt){
    blendAmt=amt;
}
void ofxGpuLutBlend::load(ofImage lutImage){
    load(lutImage.getTexture());
}

void ofxGpuLutBlend::load(string path){
    lutImage.load(path);
    load(lutImage.getTexture());
}

void ofxGpuLutBlend::begin(){
    lutShader.begin();
    lutShader.setUniformTexture("lut", lut, 1);
    lutShader.setUniform1f("blend", blendAmt);
    lutShader.setUniform1f("size", 256.0f);
}

void ofxGpuLutBlend::end(){
    lutShader.end();
}

ofImage oldMakeLut(float gamma,float pedestal,float clamp){
    ofImage image;
    image.allocate(512,512,OF_IMAGE_COLOR);
      uint8_t *pixels=&image.getPixels()[0];
      if (gamma<=0.0f){
            memset(pixels,0,image.getWidth()*image.getHeight()*3);  
      }
      else {
           for (uint32_t b=0;b<64;b++){
                   uint32_t bluecolumn=b%8;
                   uint32_t bluerow=b>>3;
                   for (uint32_t g=0;g<64;g++){
                           for (uint32_t r=0;r<64;r++){
                                   uint8_t *pixel=&pixels[(r+(g<<9)+(bluecolumn<<6)+(bluerow<<15))*3]; //+(b<<12))*3];
                                   pixel[0]=(uint8_t)((pedestal+(pow(r/64.0f,1.0f/gamma)*(clamp-pedestal)))*255); 
                                   pixel[1]=(uint8_t)((pedestal+(pow(g/64.0f,1.0f/gamma)*(clamp-pedestal)))*255); 
                                   pixel[2]=(uint8_t)((pedestal+(pow(b/64.0f,1.0f/gamma)*(clamp-pedestal)))*255); 
                }
            }
        }
    }
    //image.update();
    return image;
}

ofImage simpleMakeLut(float gamma,float pedestal,float clamp,int mapWidthBits){
    ofImage image;
    int mapWidth=pow(2,mapWidthBits);
    int mapDimensionBits=(mapWidthBits*3)/2; //only works if mapWidthBits is even
    int mapDimension=pow(2,mapDimensionBits);
    image.allocate(mapDimension,mapDimension,OF_IMAGE_COLOR);
    uint8_t *pixels=&image.getPixels()[0];  
    int numpixels=image.getWidth()*image.getHeight();   
    for (uint32_t b=0;b<mapWidth;b++){
        uint32_t bluecolumn=b%((int)pow(2,mapWidthBits/2));
        uint32_t bluerow=b>>(mapWidthBits/2);
        if (gamma==0.0f){
            memset(pixels,0,numpixels*4);   
        }
        for (uint32_t g=0;g<mapWidth;g++){
            for (uint32_t r=0;r<mapWidth;r++){
               uint8_t *pixel=&pixels[(r+(g<<mapDimensionBits)+(bluecolumn<<mapWidthBits)+(bluerow<<(mapDimensionBits+mapWidthBits)))*3];
               pixel[0]=(uint8_t)((pedestal+(pow(((float)r)/mapWidth,gamma)*(clamp-pedestal)))*255); 
               pixel[1]=(uint8_t)((pedestal+(pow(((float)g)/mapWidth,gamma)*(clamp-pedestal)))*255); 
               pixel[2]=(uint8_t)((pedestal+(pow(((float)b)/mapWidth,gamma)*(clamp-pedestal)))*255); 
            }
        }
    }
    ofLog()<<"Created LUT: "<<numpixels<<" pixels";
    //image.update();
    return image;
}

ofImage makeLut(float gamma,float pedestal,float clamp,int mapWidthBits){
    //attempt to optimise: starting 2.5fps @ 16mp
    //loop optimisation of gpixel and bPixel make us nothing
    ofImage image;
    int mapWidth=pow(2,mapWidthBits);
    int mapDimensionBits=(mapWidthBits*3)/2; //only works if mapWidthBits is even
    int mapDimension=pow(2,mapDimensionBits);
    image.allocate(mapDimension,mapDimension,OF_IMAGE_COLOR);
    uint8_t *pixels=&image.getPixels()[0];  
    int numpixels=image.getWidth()*image.getHeight();   
    for (uint32_t b=0;b<mapWidth;b++){
        uint32_t bluecolumn=b%((int)pow(2,mapWidthBits/2));
        uint32_t bluerow=b>>(mapWidthBits/2);
        uint8_t bPixel=(uint8_t)((pedestal+(pow(((float)b)/mapWidth,gamma)*(clamp-pedestal)))*255);
        if (gamma==0.0f){
            memset(pixels,0,numpixels*4);   
        }
        for (uint32_t g=0;g<mapWidth;g++){
            uint8_t gPixel=(uint8_t)((pedestal+(pow(((float)g)/mapWidth,gamma)*(clamp-pedestal)))*255);
            for (uint32_t r=0;r<mapWidth;r++){
               uint8_t *pixel=&pixels[(r+(g<<mapDimensionBits)+(bluecolumn<<mapWidthBits)+(bluerow<<(mapDimensionBits+mapWidthBits)))*3];
               pixel[0]=(uint8_t)((pedestal+(pow(((float)r)/mapWidth,gamma)*(clamp-pedestal)))*255); 
               pixel[1]=gPixel;
               pixel[2]=bPixel;
            }
        }
    }
    ofLog()<<"Created LUT: "<<numpixels<<" pixels";
    //image.update();
    return image;
}
*/