1
0
Files
avidemux_shaders/gopro_8:7/unfish_gopro_8:7.glsl

172 lines
5.2 KiB
Plaintext
Raw Normal View History

2025-09-18 07:37:25 +02:00
#version 130
2025-09-27 04:58:23 +02:00
2025-06-15 16:49:25 +02:00
// Fisheye removal for GoPro 11+, 8:7 ratio, without hypersmooth
2025-09-27 04:58:23 +02:00
// Copyright (C) 2025 Roz K <roz@rozk.net>
2025-09-20 12:53:45 +02:00
//
// 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
2025-09-27 04:58:23 +02:00
#define color_conversion
#define texture_filtering
2025-10-09 03:52:59 +02:00
//#define rotate_180
2025-09-18 07:37:25 +02:00
//#define debug_borders
2025-09-10 13:56:31 +02:00
2025-09-20 12:53:45 +02:00
#undef highp // defined by Qt-OpenGl
2025-09-18 07:37:25 +02:00
precision highp float;
2025-09-10 13:56:31 +02:00
// uniforms
2025-09-27 04:58:23 +02:00
uniform sampler2DRect myTextureY;
uniform sampler2DRect myTextureU;
uniform sampler2DRect myTextureV;
uniform vec2 myResolution;
uniform float pts;
2025-09-10 13:56:31 +02:00
// parameters
2025-09-27 04:58:23 +02:00
const vec2 sensor_size = vec2(5.949440, 5.205760);
2025-10-01 00:11:30 +02:00
const float equidistant_focal_length = 2.920000;
const float rectilinear_focal_length = 2.102263;
2025-09-10 13:56:31 +02:00
// constants
2025-09-27 04:58:23 +02:00
const int subsampling = 8;
const vec2 subsampling_step = vec2(1.0 / float(subsampling));
const vec2 subsampling_start = subsampling_step * 0.5 - vec2(0.5);
2025-09-18 06:08:05 +02:00
const float subsampling_scale = 1.0 / float(subsampling * subsampling);
2025-09-10 13:56:31 +02:00
2025-09-27 04:58:23 +02:00
// reprojection
vec2 texture_center;
vec2 texture_to_sensor;
vec2 sensor_to_texture;
void initialize() {
texture_center = myResolution * 0.5;
2025-10-09 03:46:59 +02:00
texture_to_sensor = (sensor_size / myResolution);
2025-09-18 07:37:25 +02:00
#ifdef rotate_180
texture_to_sensor *= -1.0;
#endif
2025-09-20 12:53:45 +02:00
sensor_to_texture = myResolution / sensor_size;
}
2025-09-27 04:58:23 +02:00
vec2 equidistant_to_rectilinear(const in vec2 equidistant) {
float rectilinear_radius = length(equidistant);
2025-09-18 06:08:05 +02:00
float rectilinear_angle = atan(rectilinear_radius / rectilinear_focal_length);
float equidistant_radius = rectilinear_angle * equidistant_focal_length;
2025-09-27 05:26:34 +02:00
return equidistant * (equidistant_radius / (rectilinear_radius > 0.0 ? rectilinear_radius : 1.0));
}
2025-09-27 04:58:23 +02:00
// color space (https://en.wikipedia.org/wiki/Rec._709)
#ifdef color_conversion
2025-09-27 04:58:23 +02:00
const float luminance_min = 16.0 / 255.0;
const float luminance_max = 235.0 / 255.0;
const float luminance_range = luminance_max - luminance_min;
2025-09-21 03:35:56 +02:00
2025-09-27 04:58:23 +02:00
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);
}
2025-09-21 03:35:56 +02:00
2025-10-01 00:13:06 +02:00
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;
2025-09-27 04:58:23 +02:00
}
2025-09-21 03:35:56 +02:00
2025-10-01 00:13:06 +02:00
vec3 decode_ycbcr(const in vec3 pixel) {
return vec3(decode_luminance(pixel.x), pixel.yz);
2025-09-21 03:35:56 +02:00
}
2025-10-01 00:13:06 +02:00
vec3 encode_ycbcr(const in vec3 color) {
return vec3(encode_luminance(color.x), color.yz);
2025-09-21 03:35:56 +02:00
}
#else
2025-09-27 04:58:23 +02:00
#define decode_luminance(_x) (_x)
2025-10-01 00:13:06 +02:00
#define encode_luminance(_l) (_l)
#define decode_ycbcr(_pixel) (_pixel)
#define encode_ycbcr(_color) (_color)
2025-09-27 04:58:23 +02:00
#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) {
2025-10-01 00:13:06 +02:00
vec2 i, f = modf(y_coord - half_pixel, i);
2025-09-27 04:58:23 +02:00
float luminance = mix(
mix(
2025-10-01 00:13:06 +02:00
decode_luminance(texture2DRect(myTextureY, i + corner_a).x),
decode_luminance(texture2DRect(myTextureY, i + corner_b).x),
2025-09-27 04:58:23 +02:00
f.x),
mix(
2025-10-01 00:13:06 +02:00
decode_luminance(texture2DRect(myTextureY, i + corner_c).x),
decode_luminance(texture2DRect(myTextureY, i + corner_d).x),
2025-09-27 04:58:23 +02:00
f.x),
f.y);
2025-10-01 00:13:06 +02:00
vec2 uv_coord = y_coord * 0.5;
return vec3(luminance, texture2DRect(myTextureU, uv_coord).x, texture2DRect(myTextureV, uv_coord).x);
2025-09-27 04:58:23 +02:00
}
#else
vec3 texture_filter(const in vec2 y_coord) {
vec2 uv_coord = y_coord * 0.5;
2025-10-01 00:13:06 +02:00
return decode_ycbcr(vec3(
2025-09-27 04:58:23 +02:00
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);
}
2025-09-21 03:35:56 +02:00
2025-09-27 04:58:23 +02:00
// main
2025-09-21 03:35:56 +02:00
void main() {
initialize();
vec2 coord = gl_TexCoord[0].xy;
2025-09-21 03:35:56 +02:00
vec3 color = vec3(0.0);
2025-09-18 06:08:05 +02:00
for (int y = 0; y < subsampling; y++) {
for (int x = 0; x < subsampling; x++) {
vec2 offset = subsampling_start + subsampling_step * vec2(x, y);
2025-09-27 04:58:23 +02:00
color += texture_sample(coord + offset);
2025-09-10 13:56:31 +02:00
}
}
2025-10-01 00:13:06 +02:00
gl_FragColor = vec4(encode_ycbcr(color * subsampling_scale), 1.0);
}