173 lines
		
	
	
		
			5.2 KiB
		
	
	
	
		
			GLSL
		
	
	
	
	
	
			
		
		
	
	
			173 lines
		
	
	
		
			5.2 KiB
		
	
	
	
		
			GLSL
		
	
	
	
	
	
#version 130
 | 
						|
 | 
						|
// Fisheye removal for GoPro 11+, 8:7 ratio, without hypersmooth
 | 
						|
// Copyright (C) 2025 Roz K <roz@rozk.net>
 | 
						|
//
 | 
						|
// This program is free software: you can redistribute it and/or modify
 | 
						|
// it under the terms of the GNU Affero General Public License as
 | 
						|
// published by the Free Software Foundation, either version 3 of the
 | 
						|
// License, or (at your option) any later version.
 | 
						|
//
 | 
						|
// This program is distributed in the hope that it will be useful,
 | 
						|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
 | 
						|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 | 
						|
// GNU Affero General Public License for more details.
 | 
						|
//
 | 
						|
// You should have received a copy of the GNU Affero General Public License
 | 
						|
// along with this program.  If not, see <https://www.gnu.org/licenses/>.
 | 
						|
 | 
						|
#extension GL_ARB_texture_rectangle: enable
 | 
						|
 | 
						|
#define color_conversion
 | 
						|
#define texture_filtering
 | 
						|
#define rotate_180
 | 
						|
//#define debug_borders
 | 
						|
 | 
						|
#undef highp // defined by Qt-OpenGl
 | 
						|
precision highp float;
 | 
						|
 | 
						|
// uniforms
 | 
						|
 | 
						|
uniform sampler2DRect myTextureY;
 | 
						|
uniform sampler2DRect myTextureU;
 | 
						|
uniform sampler2DRect myTextureV;
 | 
						|
uniform vec2 myResolution;
 | 
						|
uniform float pts;
 | 
						|
 | 
						|
// parameters
 | 
						|
 | 
						|
const vec2  sensor_size              = vec2(5.949440, 5.205760);
 | 
						|
const vec2  rectilinear_scale        = vec2(1.147370, 1.0);
 | 
						|
const float equidistant_focal_length = 2.920000;
 | 
						|
const float rectilinear_focal_length = 2.102263;
 | 
						|
 | 
						|
// constants
 | 
						|
 | 
						|
const int   subsampling       = 8;
 | 
						|
const vec2  subsampling_step  = vec2(1.0 / float(subsampling));
 | 
						|
const vec2  subsampling_start = subsampling_step * 0.5 - vec2(0.5);
 | 
						|
const float subsampling_scale = 1.0 / float(subsampling * subsampling);
 | 
						|
 | 
						|
// reprojection
 | 
						|
 | 
						|
vec2 texture_center;
 | 
						|
vec2 texture_to_sensor;
 | 
						|
vec2 sensor_to_texture;
 | 
						|
 | 
						|
void initialize() {
 | 
						|
    texture_center = myResolution * 0.5;
 | 
						|
    texture_to_sensor = (sensor_size / myResolution) * rectilinear_scale;
 | 
						|
#ifdef rotate_180
 | 
						|
    texture_to_sensor *= -1.0;
 | 
						|
#endif
 | 
						|
    sensor_to_texture = myResolution / sensor_size;
 | 
						|
}
 | 
						|
 | 
						|
vec2 equidistant_to_rectilinear(const in vec2 equidistant) {
 | 
						|
    float rectilinear_radius = length(equidistant);
 | 
						|
    float rectilinear_angle = atan(rectilinear_radius / rectilinear_focal_length);
 | 
						|
    float equidistant_radius = rectilinear_angle * equidistant_focal_length;
 | 
						|
    return equidistant * (equidistant_radius / (rectilinear_radius > 0.0 ? rectilinear_radius : 1.0));
 | 
						|
}
 | 
						|
 | 
						|
// color space (https://en.wikipedia.org/wiki/Rec._709)
 | 
						|
 | 
						|
#ifdef color_conversion
 | 
						|
 | 
						|
const float luminance_min   =  16.0 / 255.0;
 | 
						|
const float luminance_max   = 235.0 / 255.0;
 | 
						|
const float luminance_range = luminance_max - luminance_min;
 | 
						|
 | 
						|
float decode_luminance(const in float x) {
 | 
						|
    float y = (x - luminance_min) / luminance_range;
 | 
						|
    return (y < 0.081) ? (y / 4.5) : pow((y + 0.099) / 1.099, 1.0 / 0.45);
 | 
						|
}
 | 
						|
 | 
						|
float encode_luminance(const in float l) {
 | 
						|
    float x = (l < 0.018) ? (l * 4.5) : (pow(l, 0.45) * 1.099 - 0.099);
 | 
						|
    return x * luminance_range + luminance_min;
 | 
						|
}
 | 
						|
 | 
						|
vec3 decode_ycbcr(const in vec3 pixel) {
 | 
						|
    return vec3(decode_luminance(pixel.x), pixel.yz);
 | 
						|
}
 | 
						|
 | 
						|
vec3 encode_ycbcr(const in vec3 color) {
 | 
						|
    return vec3(encode_luminance(color.x), color.yz);
 | 
						|
}
 | 
						|
 | 
						|
#else
 | 
						|
 | 
						|
#define decode_luminance(_x) (_x)
 | 
						|
#define encode_luminance(_l) (_l)
 | 
						|
#define decode_ycbcr(_pixel) (_pixel)
 | 
						|
#define encode_ycbcr(_color) (_color)
 | 
						|
 | 
						|
#endif
 | 
						|
 | 
						|
// texture filtering
 | 
						|
 | 
						|
#ifdef texture_filtering
 | 
						|
 | 
						|
const vec2 half_pixel = vec2(0.5);
 | 
						|
 | 
						|
const vec2 corner_a = vec2(0.0, 0.0) + half_pixel;
 | 
						|
const vec2 corner_b = vec2(1.0, 0.0) + half_pixel;
 | 
						|
const vec2 corner_c = vec2(0.0, 1.0) + half_pixel;
 | 
						|
const vec2 corner_d = vec2(1.0, 1.0) + half_pixel;
 | 
						|
 | 
						|
vec3 texture_filter(const in vec2 y_coord) {
 | 
						|
    vec2 i, f = modf(y_coord - half_pixel, i);
 | 
						|
    float luminance = mix(
 | 
						|
        mix(
 | 
						|
            decode_luminance(texture2DRect(myTextureY, i + corner_a).x),
 | 
						|
            decode_luminance(texture2DRect(myTextureY, i + corner_b).x),
 | 
						|
            f.x),
 | 
						|
        mix(
 | 
						|
            decode_luminance(texture2DRect(myTextureY, i + corner_c).x),
 | 
						|
            decode_luminance(texture2DRect(myTextureY, i + corner_d).x),
 | 
						|
            f.x),
 | 
						|
        f.y);
 | 
						|
    vec2 uv_coord = y_coord * 0.5;
 | 
						|
    return vec3(luminance, texture2DRect(myTextureU, uv_coord).x, texture2DRect(myTextureV, uv_coord).x);
 | 
						|
}
 | 
						|
 | 
						|
#else
 | 
						|
 | 
						|
vec3 texture_filter(const in vec2 y_coord) {
 | 
						|
    vec2 uv_coord = y_coord * 0.5;
 | 
						|
    return decode_ycbcr(vec3(
 | 
						|
        texture2DRect(myTextureY, y_coord).x,
 | 
						|
        texture2DRect(myTextureU, uv_coord).x,
 | 
						|
        texture2DRect(myTextureV, uv_coord).x));
 | 
						|
}
 | 
						|
 | 
						|
#endif
 | 
						|
 | 
						|
vec3 texture_sample(const in vec2 equidistant) {
 | 
						|
    vec2 rectilinear = texture_center + sensor_to_texture *
 | 
						|
        equidistant_to_rectilinear((equidistant - texture_center) * texture_to_sensor);
 | 
						|
#ifdef debug_borders
 | 
						|
    if (rectilinear.x < 0.0 || rectilinear.y < 0.0 ||
 | 
						|
        rectilinear.x > myResolution.x || rectilinear.y > myResolution.y) {
 | 
						|
        return vec3(235.0 / 255.0, 240.0 / 255.0, 240.0 / 255.0);
 | 
						|
    }
 | 
						|
#endif
 | 
						|
    return texture_filter(rectilinear);
 | 
						|
}
 | 
						|
 | 
						|
// main
 | 
						|
 | 
						|
void main() {
 | 
						|
    initialize();
 | 
						|
    vec2 coord = gl_TexCoord[0].xy;
 | 
						|
    vec3 color = vec3(0.0);
 | 
						|
    for (int y = 0; y < subsampling; y++) {
 | 
						|
        for (int x = 0; x < subsampling; x++) {
 | 
						|
            vec2 offset = subsampling_start + subsampling_step * vec2(x, y);
 | 
						|
            color += texture_sample(coord + offset);
 | 
						|
        }
 | 
						|
    }
 | 
						|
    gl_FragColor = vec4(encode_ycbcr(color * subsampling_scale), 1.0);
 | 
						|
}
 |