| 1 | /* |
| 2 | * Copyright (C) 2018 Matthias Schnöll (matthias.schnoell AT gmail.com) |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or modify |
| 5 | * it under the terms of the GNU General Public License as published by |
| 6 | * the Free Software Foundation; either version 2 of the License, or |
| 7 | * (at your option) any later version. |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with this program; if not, write to the Free Software |
| 16 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
| 17 | */ |
| 18 | |
| 19 | #include "frei0r.hpp" |
| 20 | |
| 21 | // Limits (min/max values) of various data types |
| 22 | #include <limits> |
| 23 | // For the CHAR_BIT constant |
| 24 | #include <climits> |
| 25 | // pow() and other mathematical functions |
| 26 | #include <cmath> |
| 27 | |
| 28 | #define PI 3.141592654 |
| 29 | |
| 30 | /** |
| 31 | This is a frei0r filter which allows one to scale video footage non-linearly. |
| 32 | In combination with a linear scale filter, it allows one to scale 4:3 footage |
| 33 | to 16:9 and maintain the original aspect ratio in the center part of the image. |
| 34 | */ |
| 35 | |
| 36 | typedef struct { |
| 37 | unsigned int higherXPos; |
| 38 | unsigned int lowerXPos; |
| 39 | double lowerWeight; |
| 40 | double higherWeight; |
| 41 | } TransformationElem; |
| 42 | |
| 43 | |
| 44 | class ElasticScale : public frei0r::filter |
| 45 | { |
| 46 | |
| 47 | public: |
| 48 | |
| 49 | ElasticScale(unsigned int width, unsigned int height) |
| 50 | |
| 51 | { |
| 52 | this->width = width; |
| 53 | this->height = height; |
| 54 | |
| 55 | register_param(m_scaleCenter,"Center","Horizontal center position of the linear area"); |
| 56 | register_param(m_linearScaleArea,"Linear Width","Width of the linear area"); |
| 57 | register_param(m_linearScaleFactor,"Linear Scale Factor","Amount how much the linear area is scaled"); |
| 58 | register_param(m_nonLinearScaleFactor,"Non-Linear Scale Factor","Amount how much the outer left and outer right areas are scaled non linearly"); |
| 59 | |
| 60 | // default values for sinus based scaling |
| 61 | m_scaleCenter = 0.5; |
| 62 | m_linearScaleArea = 0.0; |
| 63 | m_linearScaleFactor = 0.7; |
| 64 | m_nonLinearScaleFactor = 0.7125; |
| 65 | |
| 66 | updateScalingFactors(); |
| 67 | calcTransformationFactors(); |
| 68 | } |
| 69 | |
| 70 | ~ElasticScale() |
| 71 | { |
| 72 | // Delete member variables if necessary. |
| 73 | delete[] m_transformationCalculations; |
| 74 | } |
| 75 | |
| 76 | virtual void update(double time, |
| 77 | uint32_t* out, |
| 78 | const uint32_t* in) |
| 79 | { |
| 80 | |
| 81 | if (m_prev_scaleCenter != m_scaleCenter || m_prev_linearScaleArea != m_linearScaleArea || m_prev_linearScaleFactor != m_linearScaleFactor || m_prev_nonLinearScaleFactor != m_nonLinearScaleFactor) |
| 82 | { |
| 83 | updateScalingFactors(); |
| 84 | calcTransformationFactors(); |
| 85 | } |
| 86 | |
| 87 | // pad the rowWidth to be a multiple of 8 |
| 88 | unsigned int paddedRowWidth = width; |
| 89 | if (paddedRowWidth % 8 != 0) |
| 90 | { |
| 91 | paddedRowWidth = (unsigned int)(ceil((double)width/8)*8); |
| 92 | } |
| 93 | |
| 94 | for (unsigned int colIdx = 0; colIdx < width; colIdx++) |
| 95 | { |
| 96 | double lowerWeight = m_transformationCalculations[colIdx].lowerWeight; |
| 97 | double higherWeight = m_transformationCalculations[colIdx].higherWeight; |
| 98 | |
| 99 | for (unsigned int rowIdx = 0; rowIdx < height; rowIdx++) |
| 100 | { |
| 101 | uint32_t newValue = 0; |
| 102 | unsigned int lowerXPos = paddedRowWidth * rowIdx + m_transformationCalculations[colIdx].lowerXPos; |
| 103 | unsigned int higherXPos = paddedRowWidth * rowIdx + m_transformationCalculations[colIdx].higherXPos; |
| 104 | unsigned int curPosDst = paddedRowWidth * rowIdx + colIdx; |
| 105 | |
| 106 | if (higherXPos == lowerXPos) { |
| 107 | newValue = in[higherXPos]; |
| 108 | } else { |
| 109 | for (int i=0; i<4; i++) |
| 110 | { |
| 111 | uint16_t cFold = uint8_t(in[lowerXPos] >> 8*i); |
| 112 | uint16_t cCold = uint8_t(in[higherXPos] >> 8*i); |
| 113 | cFold = (uint16_t)((double)cFold*(1-lowerWeight)); |
| 114 | cCold = (uint16_t)((double)cCold*(1-higherWeight)); |
| 115 | newValue |= ((uint8_t)((cFold + cCold)) << 8*i); |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | out[curPosDst] = newValue; |
| 120 | } |
| 121 | |
| 122 | } |
| 123 | |
| 124 | } |
| 125 | |
| 126 | private: |
| 127 | |
| 128 | // input params |
| 129 | double m_linearScaleArea; |
| 130 | double m_scaleCenter; |
| 131 | double m_linearScaleFactor; |
| 132 | double m_nonLinearScaleFactor; |
| 133 | |
| 134 | // remember the input params to detect any changes during runtime |
| 135 | double m_prev_scaleCenter; |
| 136 | double m_prev_linearScaleArea; |
| 137 | double m_prev_linearScaleFactor; |
| 138 | double m_prev_nonLinearScaleFactor; |
| 139 | |
| 140 | // intern params for mapping the input parameters |
| 141 | double m_intern_scaleCenter; |
| 142 | double m_intern_linearScaleArea; |
| 143 | double m_intern_linearScaleFactor; |
| 144 | double m_intern_nonLinearScaleFactor; |
| 145 | |
| 146 | unsigned int m_borderXAbsLeftSrc; |
| 147 | unsigned int m_borderXAbsRightSrc; |
| 148 | |
| 149 | unsigned int m_borderXAbsLeftDst; |
| 150 | unsigned int m_borderXAbsRightDst; |
| 151 | |
| 152 | TransformationElem* m_transformationCalculations = NULL; |
| 153 | |
| 154 | void updateScalingFactors() |
| 155 | { |
| 156 | // update changed parameters |
| 157 | m_prev_scaleCenter = m_scaleCenter; |
| 158 | m_prev_linearScaleArea = m_linearScaleArea; |
| 159 | m_prev_linearScaleFactor = m_linearScaleFactor; |
| 160 | m_prev_nonLinearScaleFactor = m_nonLinearScaleFactor; |
| 161 | |
| 162 | // write to internal parameters |
| 163 | m_intern_scaleCenter = m_scaleCenter; |
| 164 | m_intern_linearScaleArea = m_linearScaleArea; |
| 165 | m_intern_linearScaleFactor = m_linearScaleFactor; |
| 166 | m_intern_nonLinearScaleFactor = m_nonLinearScaleFactor; |
| 167 | |
| 168 | // limit internal parameters to ranges [0,1] |
| 169 | if (m_intern_scaleCenter <= 0) |
| 170 | { m_intern_scaleCenter = 0; } |
| 171 | else if (m_intern_scaleCenter >= 1) |
| 172 | { m_intern_scaleCenter = 1; } |
| 173 | |
| 174 | if (m_intern_linearScaleArea <= 0) |
| 175 | { m_intern_linearScaleArea = 0; } |
| 176 | else if (m_intern_linearScaleArea >= 1) |
| 177 | { m_intern_linearScaleArea = 1; } |
| 178 | |
| 179 | if (m_intern_linearScaleFactor <= 0) |
| 180 | { m_intern_linearScaleFactor = 0; } |
| 181 | else if (m_intern_linearScaleFactor >= 1) |
| 182 | { m_intern_linearScaleFactor = 1; } |
| 183 | |
| 184 | if (m_intern_nonLinearScaleFactor <= 0) |
| 185 | { m_intern_nonLinearScaleFactor = 0; } |
| 186 | else if (m_intern_nonLinearScaleFactor >= 1) |
| 187 | { m_intern_nonLinearScaleFactor = 1; } |
| 188 | |
| 189 | // adjust internal parameters where necessary |
| 190 | m_intern_nonLinearScaleFactor = m_intern_nonLinearScaleFactor*0.4-0.2; |
| 191 | |
| 192 | // calculate borders based on parameters |
| 193 | // use 'int' instead of 'unsigned int' as possible results can be negative |
| 194 | m_borderXAbsLeftSrc = (int)(m_intern_scaleCenter * width - m_intern_linearScaleArea / 2 * width); |
| 195 | m_borderXAbsLeftDst = (int)(m_intern_scaleCenter * width - m_intern_linearScaleArea / 2 * width * m_intern_linearScaleFactor); |
| 196 | |
| 197 | m_borderXAbsRightSrc = (int)(m_intern_scaleCenter * width + m_intern_linearScaleArea / 2 * width); |
| 198 | m_borderXAbsRightDst = (int)(m_intern_scaleCenter * width + m_intern_linearScaleArea / 2 * width * m_intern_linearScaleFactor); |
| 199 | |
| 200 | // validate if resulting borders are still within the valid range for later calculations [1,width-1] |
| 201 | // negative results are limited |
| 202 | if ((int)m_borderXAbsLeftSrc <= 1) |
| 203 | { m_borderXAbsLeftSrc = 1; } |
| 204 | else if ((int)m_borderXAbsLeftSrc >= (int)width-1) |
| 205 | { m_borderXAbsLeftSrc = width-1; } |
| 206 | |
| 207 | if ((int)m_borderXAbsRightSrc <= 1) |
| 208 | { m_borderXAbsRightSrc = 1; } |
| 209 | else if ((int)m_borderXAbsRightSrc >= (int)width-1) |
| 210 | { m_borderXAbsRightSrc = width-1; } |
| 211 | |
| 212 | if ((int)m_borderXAbsLeftDst <= 1) |
| 213 | { m_borderXAbsLeftDst = 1; } |
| 214 | else if ((int)m_borderXAbsLeftDst >= (int)width-1) |
| 215 | { m_borderXAbsLeftDst = width-1; } |
| 216 | |
| 217 | if ((int)m_borderXAbsRightDst <= 1) |
| 218 | { m_borderXAbsRightDst = 1; } |
| 219 | else if ((int)m_borderXAbsRightDst >= (int)width-1) |
| 220 | { m_borderXAbsRightDst = width-1; } |
| 221 | |
| 222 | } |
| 223 | |
| 224 | void calcTransformationFactors() |
| 225 | { |
| 226 | |
| 227 | if (m_transformationCalculations == NULL) |
| 228 | { |
| 229 | m_transformationCalculations = new TransformationElem[width]; |
| 230 | } |
| 231 | |
| 232 | for (unsigned int colIdx = 0; colIdx < width; colIdx++) |
| 233 | { |
| 234 | double relativeSrcXPos = 0; |
| 235 | unsigned int higherXPos = 0, lowerXPos = 0; |
| 236 | double lowerWeight = 0; |
| 237 | double higherWeight = 0; |
| 238 | |
| 239 | unsigned int offsetSrcX = 0; |
| 240 | unsigned int offsetDstX = 0; |
| 241 | unsigned int lengthSrcSection = m_borderXAbsLeftSrc - offsetSrcX - 1; |
| 242 | unsigned int lengthDstSection = m_borderXAbsLeftDst - offsetDstX - 1; |
| 243 | |
| 244 | // y=SIN(x*pi-pi)*a+x |
| 245 | double linearRatio = (double)(colIdx - offsetDstX) / lengthDstSection; |
| 246 | double ratio = sin(linearRatio * PI - PI) * m_intern_nonLinearScaleFactor + linearRatio; |
| 247 | |
| 248 | if (colIdx > m_borderXAbsLeftDst) |
| 249 | { |
| 250 | offsetSrcX = m_borderXAbsLeftSrc; |
| 251 | offsetDstX = m_borderXAbsLeftDst; |
| 252 | lengthSrcSection = m_borderXAbsRightSrc - offsetSrcX - 1; |
| 253 | lengthDstSection = m_borderXAbsRightDst - offsetDstX - 1; |
| 254 | // linear section |
| 255 | double linearRatio = (double)(colIdx - offsetDstX) / lengthDstSection; |
| 256 | ratio = ((double)(colIdx - offsetDstX) / lengthDstSection); |
| 257 | } |
| 258 | |
| 259 | if (colIdx > m_borderXAbsRightDst) |
| 260 | { |
| 261 | offsetSrcX = m_borderXAbsRightSrc; |
| 262 | offsetDstX = m_borderXAbsRightDst; |
| 263 | lengthSrcSection = width - offsetSrcX - 1; |
| 264 | lengthDstSection = width - offsetDstX - 1; |
| 265 | |
| 266 | // y=SIN(x*pi)*a+x |
| 267 | double linearRatio = (double)(colIdx-offsetDstX) / lengthDstSection; |
| 268 | ratio = sin(linearRatio * PI) * m_intern_nonLinearScaleFactor + linearRatio; |
| 269 | } |
| 270 | |
| 271 | // limit ratio to 0 |
| 272 | ratio = ratio <= 0 ? 0 : ratio; |
| 273 | |
| 274 | relativeSrcXPos = ratio * lengthSrcSection; |
| 275 | |
| 276 | lowerXPos = (unsigned int)floor(relativeSrcXPos); |
| 277 | higherXPos = (unsigned int)ceil(relativeSrcXPos); |
| 278 | if (higherXPos >= lengthSrcSection) |
| 279 | { |
| 280 | higherXPos = lengthSrcSection; |
| 281 | } |
| 282 | if (lowerXPos >= lengthSrcSection) |
| 283 | { |
| 284 | lowerXPos = lengthSrcSection; |
| 285 | } |
| 286 | |
| 287 | if (higherXPos == lowerXPos) |
| 288 | { |
| 289 | lowerWeight = higherWeight = 0.5; |
| 290 | } else { |
| 291 | lowerWeight = relativeSrcXPos - (double)lowerXPos; |
| 292 | higherWeight = (double)higherXPos - relativeSrcXPos; |
| 293 | } |
| 294 | |
| 295 | m_transformationCalculations[colIdx].higherXPos = higherXPos + offsetSrcX; |
| 296 | m_transformationCalculations[colIdx].lowerXPos = lowerXPos + offsetSrcX; |
| 297 | m_transformationCalculations[colIdx].higherWeight = higherWeight; |
| 298 | m_transformationCalculations[colIdx].lowerWeight = lowerWeight; |
| 299 | |
| 300 | } |
| 301 | } |
| 302 | |
| 303 | }; |
| 304 | |
| 305 | frei0r::construct<ElasticScale> plugin("Elastic scale filter", |
| 306 | "This is a frei0r filter which allows one to scale video footage non-linearly.", |
| 307 | "Matthias Schnoell", |
| 308 | 0,2, |
| 309 | F0R_COLOR_MODEL_RGBA8888); |