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 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298
|
/*
* Copyright (C) 2005-2018 Team Kodi
* This file is part of Kodi - https://kodi.tv
*
* SPDX-License-Identifier: GPL-2.0-or-later
* See LICENSES/README.md for more information.
*/
#ifdef TARGET_WINDOWS
#define _USE_MATH_DEFINES
#endif
#include "ConvolutionKernels.h"
#include "utils/MathUtils.h"
#ifndef M_PI
#define M_PI 3.14159265358979323846
#endif
#define SINC(x) (sin(M_PI * (x)) / (M_PI * (x)))
CConvolutionKernel::CConvolutionKernel(ESCALINGMETHOD method, int size)
{
m_size = size;
m_floatpixels = new float[m_size * 4];
if (method == VS_SCALINGMETHOD_LANCZOS2)
Lanczos2();
else if (method == VS_SCALINGMETHOD_SPLINE36_FAST)
Spline36Fast();
else if (method == VS_SCALINGMETHOD_LANCZOS3_FAST)
Lanczos3Fast();
else if (method == VS_SCALINGMETHOD_SPLINE36)
Spline36();
else if (method == VS_SCALINGMETHOD_LANCZOS3)
Lanczos3();
else if (method == VS_SCALINGMETHOD_CUBIC_B_SPLINE)
Bicubic(1.0, 0.0);
else if (method == VS_SCALINGMETHOD_CUBIC_MITCHELL)
Bicubic(1.0 / 3.0, 1.0 / 3.0);
else if (method == VS_SCALINGMETHOD_CUBIC_CATMULL)
Bicubic(0.0, 0.5);
else if (method == VS_SCALINGMETHOD_CUBIC_0_075)
Bicubic(0.0, 0.75);
else if (method == VS_SCALINGMETHOD_CUBIC_0_1)
Bicubic(0.0, 1.0);
ToIntFract();
ToUint8();
}
CConvolutionKernel::~CConvolutionKernel()
{
delete [] m_floatpixels;
delete [] m_intfractpixels;
delete [] m_uint8pixels;
}
//generate a lanczos2 kernel which can be loaded with RGBA format
//each value of RGBA has one tap, so a shader can load 4 taps with a single pixel lookup
void CConvolutionKernel::Lanczos2()
{
for (int i = 0; i < m_size; i++)
{
double x = (double)i / (double)m_size;
//generate taps
for (int j = 0; j < 4; j++)
m_floatpixels[i * 4 + j] = (float)LanczosWeight(x + (double)(j - 2), 2.0);
//any collection of 4 taps added together needs to be exactly 1.0
//for lanczos this is not always the case, so we take each collection of 4 taps
//and divide those taps by the sum of the taps
float weight = 0.0;
for (int j = 0; j < 4; j++)
weight += m_floatpixels[i * 4 + j];
for (int j = 0; j < 4; j++)
m_floatpixels[i * 4 + j] /= weight;
}
}
//generate a lanczos3 kernel which can be loaded with RGBA format
//each value of RGBA has one tap, so a shader can load 4 taps with a single pixel lookup
//the two outer lobes of the lanczos3 kernel are added to the two lobes one step to the middle
//this basically looks the same as lanczos3, but the kernel only has 4 taps,
//so it can use the 4x4 convolution shader which is twice as fast as the 6x6 one
void CConvolutionKernel::Lanczos3Fast()
{
for (int i = 0; i < m_size; i++)
{
double a = 3.0;
double x = (double)i / (double)m_size;
//generate taps
m_floatpixels[i * 4 + 0] = (float)(LanczosWeight(x - 2.0, a) + LanczosWeight(x - 3.0, a));
m_floatpixels[i * 4 + 1] = (float) LanczosWeight(x - 1.0, a);
m_floatpixels[i * 4 + 2] = (float) LanczosWeight(x , a);
m_floatpixels[i * 4 + 3] = (float)(LanczosWeight(x + 1.0, a) + LanczosWeight(x + 2.0, a));
//any collection of 4 taps added together needs to be exactly 1.0
//for lanczos this is not always the case, so we take each collection of 4 taps
//and divide those taps by the sum of the taps
float weight = 0.0;
for (int j = 0; j < 4; j++)
weight += m_floatpixels[i * 4 + j];
for (int j = 0; j < 4; j++)
m_floatpixels[i * 4 + j] /= weight;
}
}
//generate a lanczos3 kernel which can be loaded with RGBA format
//each value of RGB has one tap, so a shader can load 3 taps with a single pixel lookup
void CConvolutionKernel::Lanczos3()
{
for (int i = 0; i < m_size; i++)
{
double x = (double)i / (double)m_size;
//generate taps
for (int j = 0; j < 3; j++)
m_floatpixels[i * 4 + j] = (float)LanczosWeight(x * 2.0 + (double)(j * 2 - 3), 3.0);
m_floatpixels[i * 4 + 3] = 0.0;
}
//any collection of 6 taps added together needs to be exactly 1.0
//for lanczos this is not always the case, so we take each collection of 6 taps
//and divide those taps by the sum of the taps
for (int i = 0; i < m_size / 2; i++)
{
float weight = 0.0;
for (int j = 0; j < 3; j++)
{
weight += m_floatpixels[i * 4 + j];
weight += m_floatpixels[(i + m_size / 2) * 4 + j];
}
for (int j = 0; j < 3; j++)
{
m_floatpixels[i * 4 + j] /= weight;
m_floatpixels[(i + m_size / 2) * 4 + j] /= weight;
}
}
}
void CConvolutionKernel::Spline36Fast()
{
for (int i = 0; i < m_size; i++)
{
double x = (double)i / (double)m_size;
//generate taps
m_floatpixels[i * 4 + 0] = (float)(Spline36Weight(x - 2.0) + Spline36Weight(x - 3.0));
m_floatpixels[i * 4 + 1] = (float) Spline36Weight(x - 1.0);
m_floatpixels[i * 4 + 2] = (float) Spline36Weight(x );
m_floatpixels[i * 4 + 3] = (float)(Spline36Weight(x + 1.0) + Spline36Weight(x + 2.0));
float weight = 0.0;
for (int j = 0; j < 4; j++)
weight += m_floatpixels[i * 4 + j];
for (int j = 0; j < 4; j++)
m_floatpixels[i * 4 + j] /= weight;
}
}
void CConvolutionKernel::Spline36()
{
for (int i = 0; i < m_size; i++)
{
double x = (double)i / (double)m_size;
//generate taps
for (int j = 0; j < 3; j++)
m_floatpixels[i * 4 + j] = (float)Spline36Weight(x * 2.0 + (double)(j * 2 - 3));
m_floatpixels[i * 4 + 3] = 0.0;
}
for (int i = 0; i < m_size / 2; i++)
{
float weight = 0.0;
for (int j = 0; j < 3; j++)
{
weight += m_floatpixels[i * 4 + j];
weight += m_floatpixels[(i + m_size / 2) * 4 + j];
}
for (int j = 0; j < 3; j++)
{
m_floatpixels[i * 4 + j] /= weight;
m_floatpixels[(i + m_size / 2) * 4 + j] /= weight;
}
}
}
//generate a bicubic kernel which can be loaded with RGBA format
//each value of RGBA has one tap, so a shader can load 4 taps with a single pixel lookup
void CConvolutionKernel::Bicubic(double B, double C)
{
for (int i = 0; i < m_size; i++)
{
double x = (double)i / (double)m_size;
//generate taps
for (int j = 0; j < 4; j++)
m_floatpixels[i * 4 + j] = (float)BicubicWeight(x + (double)(j - 2), B, C);
}
}
double CConvolutionKernel::LanczosWeight(double x, double radius)
{
double ax = fabs(x);
if (ax == 0.0)
return 1.0;
else if (ax < radius)
return SINC(ax) * SINC(ax / radius);
else
return 0.0;
}
double CConvolutionKernel::BicubicWeight(double x, double B, double C)
{
double ax = fabs(x);
if (ax<1.0)
{
return ((12 - 9*B - 6*C) * ax * ax * ax +
(-18 + 12*B + 6*C) * ax * ax +
(6 - 2*B))/6;
}
else if (ax<2.0)
{
return ((-B - 6*C) * ax * ax * ax +
(6*B + 30*C) * ax * ax + (-12*B - 48*C) *
ax + (8*B + 24*C)) / 6;
}
else
{
return 0.0;
}
}
double CConvolutionKernel::Spline36Weight(double x)
{
double ax = fabs(x);
if ( ax < 1.0 )
return ( ( 13.0 / 11.0 * (ax ) - 453.0 / 209.0 ) * (ax ) - 3.0 / 209.0 ) * (ax ) + 1.0;
else if ( ax < 2.0 )
return ( ( -6.0 / 11.0 * (ax - 1.0) + 270.0 / 209.0 ) * (ax - 1.0) - 156.0 / 209.0 ) * (ax - 1.0);
else if ( ax < 3.0 )
return ( ( 1.0 / 11.0 * (ax - 2.0) - 45.0 / 209.0 ) * (ax - 2.0) + 26.0 / 209.0 ) * (ax - 2.0);
return 0.0;
}
//convert float to high byte/low byte, so the kernel can be loaded into an 8 bit texture
//with height 2 and converted back to real float in the shader
//it only works when the kernel texture uses nearest neighbour, but there's almost no difference
//between that and linear interpolation
void CConvolutionKernel::ToIntFract()
{
m_intfractpixels = new uint8_t[m_size * 8];
for (int i = 0; i < m_size * 4; i++)
{
int value = MathUtils::round_int((static_cast<double>(m_floatpixels[i]) + 1.0) / 2.0 * 65535.0);
if (value < 0)
value = 0;
else if (value > 65535)
value = 65535;
int integer = value / 256;
int fract = value % 256;
m_intfractpixels[i] = (uint8_t)integer;
m_intfractpixels[i + m_size * 4] = (uint8_t)fract;
}
}
//convert to 8 bits unsigned
void CConvolutionKernel::ToUint8()
{
m_uint8pixels = new uint8_t[m_size * 4];
for (int i = 0; i < m_size * 4; i++)
{
int value = MathUtils::round_int((static_cast<double>(m_floatpixels[i]) * 0.5 + 0.5) * 255.0);
if (value < 0)
value = 0;
else if (value > 255)
value = 255;
m_uint8pixels[i] = (uint8_t)value;
}
}
|