| 1 | /* |
| 2 | * Copyright (C) 2010-2011 Simon Andreas Eugster ([email protected]) |
| 3 | * This file is a Frei0r plugin. |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License as published by |
| 7 | * the Free Software Foundation; either version 2 of the License, or |
| 8 | * (at your option) any later version. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License |
| 16 | * along with this program; if not, write to the Free Software |
| 17 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
| 18 | */ |
| 19 | |
| 20 | /** |
| 21 | LIGHT GRAFFITI / LIGHT PAINTING / LUMASOL |
| 22 | |
| 23 | |
| 24 | This effect is intended to simulate what happens when you use a shutter speed of |
| 25 | e.g. 10 seconds for your camera and paint with light, like lamps, in the air |
| 26 | -- just with video. It tries to remember bright spots and keeps them in a mask. |
| 27 | Areas that are not very bright (i.e. background) will not sum up. |
| 28 | |
| 29 | Originally I saw this effect in some Ford Kuga commercials on YouTube when a friend |
| 30 | shew me those. One of them was [1]. No information about how this effect works is given |
| 31 | -- only that a guy from the Dutch PIPS:LAB[2] was involved. The technique seems to be |
| 32 | slightly different though; whileas this frei0r effect works in post, the original Lumasol |
| 33 | effect is said to work directly in-camera. |
| 34 | |
| 35 | Since the technique is fascinating I started writing this Open-Source effect.[3] The general |
| 36 | concept is: |
| 37 | 1. Extract the light by using thresholding (absolute brightness and brightness change relative to |
| 38 | the background image fetched from the first frame) |
| 39 | 2. Store the color in a light mask, and the estimated density in an alpha map (increased every time |
| 40 | that a light source hits a pixel to simulate overexposure) |
| 41 | 3. Paint the light mask over the video image |
| 42 | 4. Dim the alpha map, and update the background image (moving average), if desired. |
| 43 | 5. Repeat for the next frame. |
| 44 | |
| 45 | The second approach (LG_ADV) is based on the observation that colour mixing does not work well |
| 46 | with the above one that stores colour values and changes the brightness via an alpha map. Therefore |
| 47 | the new approach directly sums up colour values detected in the light source and does not use an |
| 48 | alpha map. |
| 49 | * Transitions are not very smooth out-of-the-box. This is solved by multiplying the light source's |
| 50 | RGB value by (r+g+b)/3 (after normalizing them to [0,1]); Darker lights will then be even darker |
| 51 | and the transition to the background looks smoother. |
| 52 | * Lights may look a little faint regarding color. Therefore the saturation can be increased by a custom |
| 53 | factor. Saturation depends on the brightness of the light map; the darker the light is, the more |
| 54 | the saturation is increased. This will, within a sensible range, make the lights look more vital. |
| 55 | Dimming works by scaling each color values individually. |
| 56 | |
| 57 | If you write your own Light Graffiti effect (e.g. for After Effects) I'd very much appreciate |
| 58 | to hear about it! |
| 59 | |
| 60 | -- Simon (Granjow) |
| 61 | |
| 62 | [1] http://www.youtube.com/watch?v=WVaxuIKPKvU |
| 63 | [2] http://www.pipslab.org/bio/keez-duyves/ |
| 64 | [3] http://kdenlive.org/users/granjow/writing-light-graffiti-effect |
| 65 | |
| 66 | */ |
| 67 | |
| 68 | #if defined(_MSC_VER) |
| 69 | #define _USE_MATH_DEFINES |
| 70 | #endif /* _MSC_VER */ |
| 71 | #include <cmath> |
| 72 | |
| 73 | #include "frei0r.hpp" |
| 74 | |
| 75 | |
| 76 | #include <cstdio> |
| 77 | #include <climits> |
| 78 | #include <algorithm> |
| 79 | |
| 80 | #define LG_ADV |
| 81 | //#define LG_NO_OVERLAY // Not really working yet |
| 82 | //#define LG_DEBUG |
| 83 | |
| 84 | // Macros to extract color components |
| 85 | #define GETA(abgr) (((abgr) >> (3*CHAR_BIT)) & 0xFF) |
| 86 | #define GETB(abgr) (((abgr) >> (2*CHAR_BIT)) & 0xFF) |
| 87 | #define GETG(abgr) (((abgr) >> (1*CHAR_BIT)) & 0xFF) |
| 88 | #define GETR(abgr) (((abgr) >> (0*CHAR_BIT)) & 0xFF) |
| 89 | |
| 90 | // Macro to assemble a color in RGBA8888 format |
| 91 | #define RGBA(r,g,b,a) ( ((r) << (0*CHAR_BIT)) | ((g) << (1*CHAR_BIT)) | ((b) << (2*CHAR_BIT)) | ((a) << (3*CHAR_BIT)) ) |
| 92 | // Component-wise maximum |
| 93 | #define MAX(a,b) ( (((((a) >> (0*CHAR_BIT)) & 0xFF) > (((b) >> (0*CHAR_BIT)) & 0xFF)) ? ((a) & (0xFF << (0*CHAR_BIT))) : ((b) & (0xFF << (0*CHAR_BIT)))) \ |
| 94 | | (((((a) >> (1*CHAR_BIT)) & 0xFF) > (((b) >> (1*CHAR_BIT)) & 0xFF)) ? ((a) & (0xFF << (1*CHAR_BIT))) : ((b) & (0xFF << (1*CHAR_BIT)))) \ |
| 95 | | (((((a) >> (2*CHAR_BIT)) & 0xFF) > (((b) >> (2*CHAR_BIT)) & 0xFF)) ? ((a) & (0xFF << (2*CHAR_BIT))) : ((b) & (0xFF << (2*CHAR_BIT)))) \ |
| 96 | | (((((a) >> (3*CHAR_BIT)) & 0xFF) > (((b) >> (3*CHAR_BIT)) & 0xFF)) ? ((a) & (0xFF << (3*CHAR_BIT))) : ((b) & (0xFF << (3*CHAR_BIT)))) ) |
| 97 | |
| 98 | #define CLAMP(a) (((a) < 0) ? 0 : (((a) > 255) ? 255 : (a))) |
| 99 | |
| 100 | #define ALPHA(mask,img) \ |
| 101 | ( ( ((uint32_t) ( ((((mask) >> (0*CHAR_BIT)) & 0xFF)/255.0) * ( ((mask) >> (0*CHAR_BIT)) & 0xFF) \ |
| 102 | + (1 - (( ((mask) >> (0*CHAR_BIT)) & 0xFF)/255.0)) * ( ((img) >> (0*CHAR_BIT)) & 0xFF) )) << (0*CHAR_BIT)) \ |
| 103 | | ( ((uint32_t) ( ((((mask) >> (1*CHAR_BIT)) & 0xFF)/255.0) * ( ((mask) >> (1*CHAR_BIT)) & 0xFF) \ |
| 104 | + (1 - (( ((mask) >> (1*CHAR_BIT)) & 0xFF)/255.0)) * ( ((img) >> (1*CHAR_BIT)) & 0xFF) )) << (1*CHAR_BIT)) \ |
| 105 | | ( ((uint32_t) ( ((((mask) >> (2*CHAR_BIT)) & 0xFF)/255.0) * ( ((mask) >> (2*CHAR_BIT)) & 0xFF) \ |
| 106 | + (1 - (( ((mask) >> (2*CHAR_BIT)) & 0xFF)/255.0)) * ( ((img) >> (2*CHAR_BIT)) & 0xFF) )) << (2*CHAR_BIT)) \ |
| 107 | | ( ((uint32_t) ( ((((mask) >> (3*CHAR_BIT)) & 0xFF)/255.0) * ( ((mask) >> (3*CHAR_BIT)) & 0xFF) \ |
| 108 | + (1 - (( ((mask) >> (3*CHAR_BIT)) & 0xFF)/255.0)) * ( ((img) >> (3*CHAR_BIT)) & 0xFF) )) << (3*CHAR_BIT)) ) |
| 109 | |
| 110 | // Screen layer mode |
| 111 | #define SCREEN1(mask,img) ((uint8_t) (255-(255.0-(mask))*(255.0-(img))/255.0)) |
| 112 | |
| 113 | // Luma calculation. Refer to the SOP/Sat filter. |
| 114 | #define REC709Y(r,g,b) (.2126*(r) + .7152*(g) + .0722*(b)) |
| 115 | |
| 116 | struct RGBFloat { |
| 117 | float r; |
| 118 | float g; |
| 119 | float b; |
| 120 | }; |
| 121 | |
| 122 | class LightGraffiti : public frei0r::filter |
| 123 | { |
| 124 | |
| 125 | public: |
| 126 | |
| 127 | LightGraffiti(unsigned int width, unsigned int height) : |
| 128 | m_lightMask(width*height, 0), |
| 129 | m_alphaMap(4*width*height, 0), |
| 130 | m_meanInitialized(false) |
| 131 | |
| 132 | { |
| 133 | m_mode = Graffiti_LongAvgAlphaCumC; |
| 134 | m_dimMode = Dim_Mult; |
| 135 | |
| 136 | #ifdef LG_ADV |
| 137 | RGBFloat rgb0; |
| 138 | rgb0.r = 0; |
| 139 | rgb0.g = 0; |
| 140 | rgb0.b = 0; |
| 141 | m_rgbLightMask = std::vector<RGBFloat>(width*height, rgb0); |
| 142 | |
| 143 | #ifdef LG_DEBUG |
| 144 | for (int i = 0; i < width*height; i++) { |
| 145 | if (m_rgbLightMask[i].r != 0 || m_rgbLightMask[i].g != 0 || m_rgbLightMask[i].b != 0) { |
| 146 | std::cout << "ERROR: " << m_rgbLightMask[i].r; |
| 147 | } |
| 148 | } |
| 149 | #endif |
| 150 | #endif |
| 151 | |
| 152 | #ifdef LG_NO_OVERLAY |
| 153 | m_prevMask = std::vector<RGBFloat>(width*height, rgb0); |
| 154 | #endif |
| 155 | |
| 156 | register_param(m_pSensitivity, "sensitivity", "Sensitivity of the effect for light (higher sensitivity will lead to brighter lights)"); |
| 157 | register_param(m_pBackgroundWeight, "backgroundWeight", "Describes how strong the (accumulated) background should shine through"); |
| 158 | register_param(m_pThresholdBrightness, "thresholdBrightness", "Brightness threshold to distinguish between foreground and background"); |
| 159 | register_param(m_pThresholdDifference, "thresholdDifference", "Threshold: Difference to background to distinguish between fore- and background"); |
| 160 | register_param(m_pThresholdDiffSum, "thresholdDiffSum", "Threshold for sum of differences. Can in most cases be ignored (set to 0)."); |
| 161 | register_param(m_pDim, "dim", "Dimming of the light mask"); |
| 162 | register_param(m_pSaturation, "saturation", "Saturation of lights"); |
| 163 | register_param(m_pLowerOverexposure, "lowerOverexposure", "Prevents some overexposure if the light source stays steady too long (varying speed)"); |
| 164 | register_param(m_pStatsBrightness, "statsBrightness", "Display the brightness and threshold, for adjusting the brightness threshold parameter"); |
| 165 | register_param(m_pStatsDiff, "statsDifference", "Display the background difference and threshold"); |
| 166 | register_param(m_pStatsDiffSum, "statsDiffSum", "Display the sum of the background difference and the threshold"); |
| 167 | register_param(m_pReset, "reset", "Reset filter masks"); |
| 168 | register_param(m_pTransparentBackground, "transparentBackground", "Make the background transparent"); |
| 169 | register_param(m_pBlackReference, "blackReference", "Uses black as background image instead of the first frame."); |
| 170 | register_param(m_pLongAlpha, "longAlpha", "Alpha value for moving average"); |
| 171 | register_param(m_pNonlinearDim, "nonlinearDim", "Nonlinear dimming (may look more natural)"); |
| 172 | m_pLongAlpha = 1/128.0; |
| 173 | m_pSensitivity = 1 / 5.; |
| 174 | m_pBackgroundWeight = 0; |
| 175 | m_pThresholdBrightness = 450 / 765.; |
| 176 | m_pThresholdDifference = 0; |
| 177 | m_pThresholdDiffSum = 0; |
| 178 | m_pDim = 0; |
| 179 | m_pSaturation = 1 / 4.; |
| 180 | m_pLowerOverexposure = 0; |
| 181 | m_pStatsBrightness = false; |
| 182 | m_pStatsDiff = false; |
| 183 | m_pStatsDiffSum = false; |
| 184 | m_pReset = false; |
| 185 | m_pTransparentBackground = false; |
| 186 | m_pBlackReference = false; |
| 187 | m_pLongAlpha = 0; |
| 188 | m_pNonlinearDim = 0; |
| 189 | |
| 190 | } |
| 191 | |
| 192 | ~LightGraffiti() |
| 193 | { |
| 194 | // I was told that a std::vector does not need to be deleted -- |
| 195 | // therefore nothing to do here! |
| 196 | } |
| 197 | |
| 198 | enum GraffitiMode { Graffiti_max, Graffiti_max_sum, Graffiti_Y, Graffiti_Avg, Graffiti_Avg2, |
| 199 | Graffiti_Avg_Stat, Graffiti_AvgTresh_Stat, Graffiti_Max_Stat, Graffiti_Y_Stat, Graffiti_S_Stat, |
| 200 | Graffiti_STresh_Stat, Graffiti_SDiff_Stat, Graffiti_SDiffTresh_Stat, |
| 201 | Graffiti_SSqrt_Stat, |
| 202 | Graffiti_LongAvg, Graffiti_LongAvg_Stat, Graffiti_LongAvgAlpha, Graffiti_LongAvgAlpha_Stat, |
| 203 | Graffiti_LongAvgAlphaCumC }; |
| 204 | enum DimMode { Dim_Mult, Dim_Sin }; |
| 205 | |
| 206 | |
| 207 | |
| 208 | |
| 209 | |
| 210 | virtual void update(double time, |
| 211 | uint32_t* out, |
| 212 | const uint32_t* in) |
| 213 | { |
| 214 | double sensitivity = m_pSensitivity * 5; |
| 215 | double thresholdBrightness = m_pThresholdBrightness * 765; |
| 216 | double thresholdDifference = m_pThresholdDifference * 255; |
| 217 | double thresholdDiffSum = m_pThresholdDiffSum * 765; |
| 218 | double saturation = m_pSaturation * 4; |
| 219 | double lowerOverexposure = m_pLowerOverexposure * 10; |
| 220 | |
| 221 | // Copy everything to the output image. |
| 222 | // Most of the image will very likely not change at all. |
| 223 | std::copy(in, in + width*height, out); |
| 224 | |
| 225 | #ifdef LG_ADV |
| 226 | RGBFloat rgb0; |
| 227 | rgb0.r = 0; |
| 228 | rgb0.g = 0; |
| 229 | rgb0.b = 0; |
| 230 | #endif |
| 231 | |
| 232 | if (m_pNonlinearDim) { |
| 233 | m_dimMode = Dim_Sin; |
| 234 | } else { |
| 235 | m_dimMode = Dim_Mult; |
| 236 | } |
| 237 | |
| 238 | |
| 239 | /* |
| 240 | Refresh the background image |
| 241 | */ |
| 242 | if (!m_meanInitialized || m_pReset) { |
| 243 | if (m_pBlackReference) { |
| 244 | // Do not use the first frame from the movie as background image but plain black |
| 245 | // to calculate the added light. Useful e.g. when dealing with still images. |
| 246 | m_longMeanImage = std::vector<float>(width*height*3, 0); |
| 247 | } else { |
| 248 | m_longMeanImage = std::vector<float>(width*height*3); |
| 249 | for (unsigned int pixel = 0; pixel < width*height; pixel++) { |
| 250 | m_longMeanImage[3*pixel+0] = GETR(in[pixel]); |
| 251 | m_longMeanImage[3*pixel+1] = GETG(in[pixel]); |
| 252 | m_longMeanImage[3*pixel+2] = GETB(in[pixel]); |
| 253 | } |
| 254 | } |
| 255 | m_meanInitialized = true; |
| 256 | } else { |
| 257 | // Calculate the mean image to estimate the background. If alpha is set > 0, bright light sources |
| 258 | // moving into the image and standing still will eventually be treated as background. |
| 259 | if (m_pLongAlpha > 0) { |
| 260 | for (unsigned int pixel = 0; pixel < width*height; pixel++) { |
| 261 | m_longMeanImage[3*pixel+0] = (1-m_pLongAlpha) * m_longMeanImage[3*pixel+0] + m_pLongAlpha * GETR(in[pixel]); |
| 262 | m_longMeanImage[3*pixel+1] = (1-m_pLongAlpha) * m_longMeanImage[3*pixel+1] + m_pLongAlpha * GETG(in[pixel]); |
| 263 | m_longMeanImage[3*pixel+2] = (1-m_pLongAlpha) * m_longMeanImage[3*pixel+2] + m_pLongAlpha * GETB(in[pixel]); |
| 264 | } |
| 265 | } |
| 266 | } |
| 267 | |
| 268 | |
| 269 | /* |
| 270 | Light mask dimming |
| 271 | */ |
| 272 | if (m_pDim > 0) { |
| 273 | // Dims the light mask. Lights will leave fainting trails. |
| 274 | |
| 275 | float factor = 1-m_pDim; |
| 276 | |
| 277 | /* Gnu Octave: |
| 278 | range=linspace(0,1,100); |
| 279 | % Sin |
| 280 | plot(range,sin(range*pi/2).^.5) |
| 281 | plot(range,sin(range*pi/2).^.25) |
| 282 | */ |
| 283 | |
| 284 | switch (m_dimMode) { |
| 285 | |
| 286 | case Dim_Mult: |
| 287 | #ifdef LG_ADV |
| 288 | for (size_t i = 0; i < m_rgbLightMask.size(); i++) { |
| 289 | m_rgbLightMask[i].r *= factor; |
| 290 | m_rgbLightMask[i].g *= factor; |
| 291 | m_rgbLightMask[i].b *= factor; |
| 292 | } |
| 293 | #else |
| 294 | for (unsigned int i = 0; i < width*height; i++) { |
| 295 | m_alphaMap[4*i + 0] *= factor; |
| 296 | m_alphaMap[4*i + 1] *= factor; |
| 297 | m_alphaMap[4*i + 2] *= factor; |
| 298 | m_alphaMap[4*i + 3] *= factor; |
| 299 | } |
| 300 | #endif |
| 301 | break; |
| 302 | |
| 303 | |
| 304 | case Dim_Sin: |
| 305 | #ifdef LG_ADV |
| 306 | for (size_t i = 0; i < m_rgbLightMask.size(); i++) { |
| 307 | // Red |
| 308 | if (m_rgbLightMask[i].r < 1) { |
| 309 | m_rgbLightMask[i].r *= pow(sin(m_rgbLightMask[i].r * M_PI/2), m_pDim) - .01; |
| 310 | } else { |
| 311 | m_rgbLightMask[i].r *= factor; |
| 312 | } |
| 313 | if (m_rgbLightMask[i].r < 0) { m_rgbLightMask[i].r = 0; } |
| 314 | |
| 315 | // Green |
| 316 | if (m_rgbLightMask[i].g < 1) { |
| 317 | m_rgbLightMask[i].g *= pow(sin(m_rgbLightMask[i].g * M_PI/2), m_pDim) - .01; |
| 318 | } else { |
| 319 | m_rgbLightMask[i].g *= factor; |
| 320 | } |
| 321 | if (m_rgbLightMask[i].g < 0) { m_rgbLightMask[i].g = 0; } |
| 322 | |
| 323 | // Blue |
| 324 | if (m_rgbLightMask[i].b < 1) { |
| 325 | m_rgbLightMask[i].b *= pow(sin(m_rgbLightMask[i].b * M_PI/2), m_pDim) - .01; |
| 326 | } else { |
| 327 | m_rgbLightMask[i].b *= factor; |
| 328 | } |
| 329 | if (m_rgbLightMask[i].b < 0) { m_rgbLightMask[i].b = 0; } |
| 330 | } |
| 331 | #else |
| 332 | // Attention: Since Graffiti_LongAvgAlphaCumC only makes use of the first alpha channel |
| 333 | // the other channels are not calculated here due to efficiency reasons. |
| 334 | // May have to be adjusted if required. |
| 335 | for (int i = 0; i < width*height; i++) { |
| 336 | if (m_alphaMap[4*i + 0] < 1) { |
| 337 | m_alphaMap[4*i + 0] *= pow(sin(m_alphaMap[4*i + 0] * M_PI/2), m_pDim) - .01; |
| 338 | } else { |
| 339 | m_alphaMap[4*i + 0] *= factor; |
| 340 | } |
| 341 | if (m_alphaMap[4*i + 0] < 0) { m_alphaMap[4*i + 0] = 0; } |
| 342 | } |
| 343 | #endif |
| 344 | break; |
| 345 | } |
| 346 | |
| 347 | } |
| 348 | |
| 349 | |
| 350 | |
| 351 | /* |
| 352 | Reset all masks if desired |
| 353 | (mainly for parameter adjustments when working in the NLE) |
| 354 | */ |
| 355 | if (m_pReset) { |
| 356 | #ifdef LG_ADV |
| 357 | m_rgbLightMask = std::vector<RGBFloat>(width*height, rgb0); |
| 358 | #else |
| 359 | std::fill(&m_lightMask[0], &m_lightMask[width*height - 1], 0); |
| 360 | std::fill(&m_alphaMap[0], &m_alphaMap[width*height*4 - 1], 0); |
| 361 | #endif |
| 362 | // m_longMeanImage has been handled above already (set to the current image). |
| 363 | } |
| 364 | |
| 365 | |
| 366 | int r, g, b; |
| 367 | int maxDiff, temp, sum; |
| 368 | unsigned int min; |
| 369 | unsigned int max; |
| 370 | float f; |
| 371 | float fr, fg, fb, sr, sg, sb, fy, fsat; |
| 372 | |
| 373 | #ifdef LG_DEBUG |
| 374 | int deCount = 0; |
| 375 | #endif |
| 376 | |
| 377 | |
| 378 | switch (m_mode) { |
| 379 | /* |
| 380 | Lots of testing modes here! |
| 381 | */ |
| 382 | case Graffiti_max: |
| 383 | for (unsigned int pixel = 0; pixel < width*height; pixel++) { |
| 384 | |
| 385 | if ( |
| 386 | (GETR(out[pixel]) == 0xFF |
| 387 | || GETG(out[pixel]) == 0xFF |
| 388 | || GETB(out[pixel]) == 0xFF) |
| 389 | ){ |
| 390 | m_lightMask[pixel] |= out[pixel]; |
| 391 | } |
| 392 | if (m_lightMask[pixel] != 0) { |
| 393 | out[pixel] = m_lightMask[pixel]; |
| 394 | } |
| 395 | } |
| 396 | break; |
| 397 | case Graffiti_max_sum: |
| 398 | for (unsigned int pixel = 0; pixel < width*height; pixel++) { |
| 399 | |
| 400 | if ( |
| 401 | (GETR(out[pixel]) == 0xFF |
| 402 | || GETG(out[pixel]) == 0xFF |
| 403 | || GETB(out[pixel]) == 0xFF) |
| 404 | && |
| 405 | (GETR(out[pixel]) |
| 406 | + GETG(out[pixel]) |
| 407 | + GETB(out[pixel]) |
| 408 | > 0xFF + 0xCC + 0xCC) |
| 409 | ){ |
| 410 | m_lightMask[pixel] |= out[pixel]; |
| 411 | } |
| 412 | if (m_lightMask[pixel] != 0) { |
| 413 | out[pixel] = m_lightMask[pixel]; |
| 414 | } |
| 415 | } |
| 416 | break; |
| 417 | |
| 418 | case Graffiti_Y: |
| 419 | for (unsigned int pixel = 0; pixel < width*height; pixel++) { |
| 420 | if ( |
| 421 | .299*GETR(out[pixel])/255.0 |
| 422 | + .587 * GETG(out[pixel])/255.0 |
| 423 | + .114 * GETB(out[pixel])/255.0 |
| 424 | >= .85 |
| 425 | ){ |
| 426 | m_lightMask[pixel] |= out[pixel]; |
| 427 | } |
| 428 | if (m_lightMask[pixel] != 0) { |
| 429 | out[pixel] = m_lightMask[pixel]; |
| 430 | } |
| 431 | } |
| 432 | break; |
| 433 | |
| 434 | case Graffiti_Max_Stat: |
| 435 | for (unsigned int pixel = 0; pixel < width*height; pixel++) { |
| 436 | if (GETR(out[pixel]) == 0xFF || GETG(out[pixel]) == 0xFF || GETB(out[pixel]) == 0xFF) { |
| 437 | out[pixel] = 0xFFFFFFFF; |
| 438 | } else { |
| 439 | out[pixel] = 0; |
| 440 | } |
| 441 | } |
| 442 | break; |
| 443 | case Graffiti_Y_Stat: |
| 444 | for (unsigned int pixel = 0; pixel < width*height; pixel++) { |
| 445 | temp = .299*GETR(out[pixel]) + .587 * GETG(out[pixel]) + .114 * GETB(out[pixel]); |
| 446 | temp = CLAMP(temp); |
| 447 | out[pixel] = RGBA(temp, temp, temp, 0xFF); |
| 448 | } |
| 449 | break; |
| 450 | case Graffiti_S_Stat: |
| 451 | for (unsigned int pixel = 0; pixel < width*height; pixel++) { |
| 452 | min = GETR(out[pixel]); |
| 453 | max = GETR(out[pixel]); |
| 454 | if (GETG(out[pixel]) < min) min = GETG(out[pixel]); |
| 455 | if (GETG(out[pixel]) > max) max = GETG(out[pixel]); |
| 456 | if (GETB(out[pixel]) < min) min = GETB(out[pixel]); |
| 457 | if (GETB(out[pixel]) > max) max = GETB(out[pixel]); |
| 458 | if (min == 0) { out[pixel] = 0; } |
| 459 | else { |
| 460 | temp = 255.0*(max-min)/(float)max; |
| 461 | temp = CLAMP(temp); |
| 462 | out[pixel] = RGBA(temp, temp, temp, 0xFF); |
| 463 | } |
| 464 | } |
| 465 | break; |
| 466 | case Graffiti_STresh_Stat: |
| 467 | for (unsigned int pixel = 0; pixel < width*height; pixel++) { |
| 468 | min = GETR(out[pixel]); |
| 469 | max = GETR(out[pixel]); |
| 470 | if (GETG(out[pixel]) < min) min = GETG(out[pixel]); |
| 471 | if (GETG(out[pixel]) > max) max = GETG(out[pixel]); |
| 472 | if (GETB(out[pixel]) < min) min = GETB(out[pixel]); |
| 473 | if (GETB(out[pixel]) > max) max = GETB(out[pixel]); |
| 474 | if (min == 0 || max < 0x80) { out[pixel] = 0; } |
| 475 | else { |
| 476 | temp = 255.0*((float)max-min)/max; |
| 477 | temp = CLAMP(temp); |
| 478 | out[pixel] = RGBA(temp, temp, temp, 0xFF); |
| 479 | } |
| 480 | } |
| 481 | break; |
| 482 | case Graffiti_SDiff_Stat: |
| 483 | for (unsigned int pixel = 0; pixel < width*height; pixel++) { |
| 484 | min = GETR(out[pixel]); |
| 485 | max = GETR(out[pixel]); |
| 486 | if (GETG(out[pixel]) < min) min = GETG(out[pixel]); |
| 487 | if (GETG(out[pixel]) > max) max = GETG(out[pixel]); |
| 488 | if (GETB(out[pixel]) < min) min = GETB(out[pixel]); |
| 489 | if (GETB(out[pixel]) > max) max = GETB(out[pixel]); |
| 490 | int sat; |
| 491 | if (min == 0) { sat = 0; } |
| 492 | else { |
| 493 | temp = 255.0*(max-min)/(float)max; |
| 494 | temp = CLAMP(temp); |
| 495 | sat = RGBA(temp, temp, temp, 0xFF); |
| 496 | } |
| 497 | min = m_longMeanImage[3*pixel+0]; |
| 498 | max = m_longMeanImage[3*pixel+0]; |
| 499 | if (m_longMeanImage[3*pixel+1] < min) min = m_longMeanImage[3*pixel+1]; |
| 500 | if (m_longMeanImage[3*pixel+1] > max) max = m_longMeanImage[3*pixel+1]; |
| 501 | if (m_longMeanImage[3*pixel+2] < min) min = m_longMeanImage[3*pixel+2]; |
| 502 | if (m_longMeanImage[3*pixel+2] > max) max = m_longMeanImage[3*pixel+2]; |
| 503 | if (min == 0) { out[pixel] = 0; } |
| 504 | else { |
| 505 | temp = 255.0*(max-min)/(float)max; |
| 506 | temp = CLAMP(temp); |
| 507 | out[pixel] = RGBA(temp, temp, temp, 0xFF); |
| 508 | } |
| 509 | temp = 0x7f + GETR(out[pixel]) - GETR(sat); |
| 510 | temp = CLAMP(temp); |
| 511 | out[pixel] = RGBA(temp, temp, temp, 0xFF); |
| 512 | } |
| 513 | break; |
| 514 | case Graffiti_SDiffTresh_Stat: |
| 515 | for (unsigned int pixel = 0; pixel < width*height; pixel++) { |
| 516 | min = GETR(out[pixel]); |
| 517 | max = GETR(out[pixel]); |
| 518 | if (GETG(out[pixel]) < min) min = GETG(out[pixel]); |
| 519 | if (GETG(out[pixel]) > max) max = GETG(out[pixel]); |
| 520 | if (GETB(out[pixel]) < min) min = GETB(out[pixel]); |
| 521 | if (GETB(out[pixel]) > max) max = GETB(out[pixel]); |
| 522 | int sat; |
| 523 | if (min == 0) { sat = 0; } |
| 524 | else { |
| 525 | temp = 255.0*(max-min)/(float)max; |
| 526 | temp = CLAMP(temp); |
| 527 | sat = RGBA(temp, temp, temp, 0xFF); |
| 528 | } |
| 529 | if (max < 0x80) { |
| 530 | out[pixel] = RGBA(0,0,0,0xFF); |
| 531 | } else { |
| 532 | min = m_longMeanImage[3*pixel+0]; |
| 533 | max = m_longMeanImage[3*pixel+0]; |
| 534 | if (m_longMeanImage[3*pixel+1] < min) min = m_longMeanImage[3*pixel+1]; |
| 535 | if (m_longMeanImage[3*pixel+1] > max) max = m_longMeanImage[3*pixel+1]; |
| 536 | if (m_longMeanImage[3*pixel+2] < min) min = m_longMeanImage[3*pixel+2]; |
| 537 | if (m_longMeanImage[3*pixel+2] > max) max = m_longMeanImage[3*pixel+2]; |
| 538 | if (min == 0) { out[pixel] = 0; } |
| 539 | else { |
| 540 | temp = 255.0*(max-min)/(float)max; |
| 541 | temp = CLAMP(temp); |
| 542 | out[pixel] = RGBA(temp, temp, temp, 0xFF); |
| 543 | } |
| 544 | temp = 0x7f + GETR(out[pixel]) - GETR(sat); |
| 545 | temp = CLAMP(temp); |
| 546 | out[pixel] = RGBA(temp, temp, temp, 0xFF); |
| 547 | } |
| 548 | } |
| 549 | break; |
| 550 | case Graffiti_SSqrt_Stat: |
| 551 | for (unsigned int pixel = 0; pixel < width*height; pixel++) { |
| 552 | min = GETR(out[pixel]); |
| 553 | max = GETR(out[pixel]); |
| 554 | if (GETG(out[pixel]) < min) min = GETG(out[pixel]); |
| 555 | if (GETG(out[pixel]) > max) max = GETG(out[pixel]); |
| 556 | if (GETB(out[pixel]) < min) min = GETB(out[pixel]); |
| 557 | if (GETB(out[pixel]) > max) max = GETB(out[pixel]); |
| 558 | if (min == 0) { out[pixel] = 0; } |
| 559 | else { |
| 560 | temp = 255.0*(max-min)/(float)max/(256.0-max); |
| 561 | temp = CLAMP(temp); |
| 562 | out[pixel] = RGBA(temp, temp, temp, 0xFF); |
| 563 | } |
| 564 | } |
| 565 | break; |
| 566 | case Graffiti_LongAvg_Stat: |
| 567 | maxDiff = 0; |
| 568 | temp = 0; |
| 569 | for (unsigned int pixel = 0; pixel < width*height; pixel++) { |
| 570 | r = 0x7f + (GETR(out[pixel]) - m_longMeanImage[3*pixel+0])/2; |
| 571 | r = CLAMP(r); |
| 572 | g = 0x7f + (GETG(out[pixel]) - m_longMeanImage[3*pixel+1])/2; |
| 573 | g = CLAMP(g); |
| 574 | b = 0x7f + (GETB(out[pixel]) - m_longMeanImage[3*pixel+2])/2; |
| 575 | b = CLAMP(b); |
| 576 | |
| 577 | out[pixel] = RGBA(r,g,b,0xFF); |
| 578 | } |
| 579 | break; |
| 580 | case Graffiti_LongAvg: |
| 581 | for (unsigned int pixel = 0; pixel < width*height; pixel++) { |
| 582 | |
| 583 | r = 0x7f + (GETR(out[pixel]) - m_longMeanImage[3*pixel+0]); |
| 584 | r = CLAMP(r); |
| 585 | max = GETR(out[pixel]); |
| 586 | maxDiff = r; |
| 587 | temp = r; |
| 588 | |
| 589 | g = 0x7f + (GETG(out[pixel]) - m_longMeanImage[3*pixel+1]); |
| 590 | g = CLAMP(g); |
| 591 | if (maxDiff < g) maxDiff = g; |
| 592 | if (max < GETG(out[pixel])) max = GETG(out[pixel]); |
| 593 | temp += g; |
| 594 | |
| 595 | b = 0x7f + (GETB(out[pixel]) - m_longMeanImage[3*pixel+2]); |
| 596 | b = CLAMP(b); |
| 597 | if (maxDiff < b) maxDiff = b; |
| 598 | if (max < GETB(out[pixel])) max = GETB(out[pixel]); |
| 599 | temp += b; |
| 600 | |
| 601 | if (maxDiff > 0xe0 && temp > 0xe0 + 0xd0 + 0x80) { |
| 602 | m_lightMask[pixel] = MAX(m_lightMask[pixel], out[pixel]); |
| 603 | |
| 604 | m_alphaMap[4*pixel+0] = 2*(GETR(out[pixel])-m_longMeanImage[3*pixel+0]); |
| 605 | m_alphaMap[4*pixel+0] = CLAMP(m_alphaMap[4*pixel+0])/255.0; |
| 606 | |
| 607 | m_alphaMap[4*pixel+1] = 2*(GETG(out[pixel])-m_longMeanImage[3*pixel+1]); |
| 608 | m_alphaMap[4*pixel+1] = CLAMP(m_alphaMap[4*pixel+1])/255.0; |
| 609 | |
| 610 | m_alphaMap[4*pixel+2] = 2*(GETB(out[pixel])-m_longMeanImage[3*pixel+2]); |
| 611 | m_alphaMap[4*pixel+2] = CLAMP(m_alphaMap[4*pixel+2])/255.0; |
| 612 | |
| 613 | m_alphaMap[4*pixel+3] = 1; |
| 614 | } |
| 615 | |
| 616 | if (m_lightMask[pixel] != 0) { |
| 617 | r = SCREEN1(GETR(out[pixel]), GETR(m_lightMask[pixel])); |
| 618 | g = SCREEN1(GETG(out[pixel]), GETG(m_lightMask[pixel])); |
| 619 | b = SCREEN1(GETB(out[pixel]), GETB(m_lightMask[pixel])); |
| 620 | r = CLAMP(r); |
| 621 | g = CLAMP(g); |
| 622 | b = CLAMP(b); |
| 623 | out[pixel] = RGBA(r,g,b,0xFF); |
| 624 | } |
| 625 | } |
| 626 | break; |
| 627 | case Graffiti_LongAvgAlpha_Stat: |
| 628 | for (unsigned int pixel = 0; pixel < width*height; pixel++) { |
| 629 | |
| 630 | r = 0x7f + (GETR(out[pixel]) - m_longMeanImage[3*pixel+0]); |
| 631 | r = CLAMP(r); |
| 632 | max = GETR(out[pixel]); |
| 633 | maxDiff = r; |
| 634 | temp = r; |
| 635 | |
| 636 | g = 0x7f + (GETG(out[pixel]) - m_longMeanImage[3*pixel+1]); |
| 637 | g = CLAMP(g); |
| 638 | if (maxDiff < g) maxDiff = g; |
| 639 | if (max < GETG(out[pixel])) max = GETG(out[pixel]); |
| 640 | temp += g; |
| 641 | |
| 642 | b = 0x7f + (GETB(out[pixel]) - m_longMeanImage[3*pixel+2]); |
| 643 | b = CLAMP(b); |
| 644 | if (maxDiff < b) maxDiff = b; |
| 645 | if (max < GETB(out[pixel])) max = GETB(out[pixel]); |
| 646 | temp += b; |
| 647 | |
| 648 | if (maxDiff > 0xe0 && temp > 0xe0 + 0xd0 + 0x80) { |
| 649 | m_lightMask[pixel] = MAX(m_lightMask[pixel], out[pixel]); |
| 650 | |
| 651 | f = 2*(GETR(out[pixel])-m_longMeanImage[3*pixel+0]); |
| 652 | f = CLAMP(f)/255.0; |
| 653 | if (f > m_alphaMap[4*pixel+0]) m_alphaMap[4*pixel+0] = f; |
| 654 | |
| 655 | f = 2*(GETG(out[pixel])-m_longMeanImage[3*pixel+1]); |
| 656 | f = CLAMP(f)/255.0; |
| 657 | if (f > m_alphaMap[4*pixel+1]) m_alphaMap[4*pixel+1] = f; |
| 658 | |
| 659 | f = 2*(GETB(out[pixel])-m_longMeanImage[3*pixel+2]); |
| 660 | f = CLAMP(f)/255.0; |
| 661 | if (f > m_alphaMap[4*pixel+2]) m_alphaMap[4*pixel+2] = f; |
| 662 | |
| 663 | m_alphaMap[4*pixel+3] = 1; |
| 664 | } |
| 665 | r = 255.0*m_alphaMap[4*pixel+0]; |
| 666 | g = 255*m_alphaMap[4*pixel+1]; |
| 667 | b = 255*m_alphaMap[4*pixel+2]; |
| 668 | out[pixel] = RGBA(r,g,b,0xFF); |
| 669 | } |
| 670 | break; |
| 671 | case Graffiti_LongAvgAlpha: |
| 672 | for (unsigned int pixel = 0; pixel < width*height; pixel++) { |
| 673 | |
| 674 | r = 0x7f + (GETR(out[pixel]) - m_longMeanImage[3*pixel+0]); |
| 675 | r = CLAMP(r); |
| 676 | max = GETR(out[pixel]); |
| 677 | maxDiff = r; |
| 678 | temp = r; |
| 679 | |
| 680 | g = 0x7f + (GETG(out[pixel]) - m_longMeanImage[3*pixel+1]); |
| 681 | g = CLAMP(g); |
| 682 | if (maxDiff < g) maxDiff = g; |
| 683 | if (max < GETG(out[pixel])) max = GETG(out[pixel]); |
| 684 | temp += g; |
| 685 | |
| 686 | b = 0x7f + (GETB(out[pixel]) - m_longMeanImage[3*pixel+2]); |
| 687 | b = CLAMP(b); |
| 688 | if (maxDiff < b) maxDiff = b; |
| 689 | if (max < GETB(out[pixel])) max = GETB(out[pixel]); |
| 690 | temp += b; |
| 691 | |
| 692 | if (maxDiff > 0xe0 && temp > 0xe0 + 0xd0 + 0x80) { |
| 693 | m_lightMask[pixel] = MAX(m_lightMask[pixel], out[pixel]); |
| 694 | |
| 695 | f = 2*(GETR(out[pixel])-m_longMeanImage[3*pixel+0]); |
| 696 | f = CLAMP(f)/255.0; |
| 697 | f *= f; |
| 698 | if (f > m_alphaMap[4*pixel+0]) m_alphaMap[4*pixel+0] = f; |
| 699 | |
| 700 | f = 2*(GETG(out[pixel])-m_longMeanImage[3*pixel+1]); |
| 701 | f = CLAMP(f)/255.0; |
| 702 | f *= f; |
| 703 | if (f > m_alphaMap[4*pixel+1]) m_alphaMap[4*pixel+1] = f; |
| 704 | |
| 705 | f = 2*(GETB(out[pixel])-m_longMeanImage[3*pixel+2]); |
| 706 | f = CLAMP(f)/255.0; |
| 707 | f *= f; |
| 708 | if (f > m_alphaMap[4*pixel+2]) m_alphaMap[4*pixel+2] = f; |
| 709 | } |
| 710 | if (m_lightMask[pixel] != 0) { |
| 711 | r = SCREEN1(GETR(out[pixel]), m_alphaMap[4*pixel+0]*GETR(m_lightMask[pixel])); |
| 712 | g = SCREEN1(GETG(out[pixel]), m_alphaMap[4*pixel+1]*GETG(m_lightMask[pixel])); |
| 713 | b = SCREEN1(GETB(out[pixel]), m_alphaMap[4*pixel+2]*GETB(m_lightMask[pixel])); |
| 714 | r = CLAMP(r); |
| 715 | g = CLAMP(g); |
| 716 | b = CLAMP(b); |
| 717 | out[pixel] = RGBA(r,g,b,0xFF); |
| 718 | } |
| 719 | } |
| 720 | break; |
| 721 | case Graffiti_LongAvgAlphaCumC: |
| 722 | |
| 723 | /** |
| 724 | Ideas (partially considered) to get a realistic look: |
| 725 | * Remember Hue if Saturation > 0.1 (below: Close to white, so Hue might be wrong → remember Saturation as well) |
| 726 | * Maximize Saturation for low alpha (opacity) |
| 727 | * Make alpha depend on the light source's brightness |
| 728 | * If alpha > 1: Simulate overexposure by going towards white |
| 729 | * If pixel is bright in another frame: Sum up alpha values (longer exposure) |
| 730 | Maybe: Logarithmic scale? → Overexposure becomes harder |
| 731 | log(alpha/factor + 1) or sqrt(alpha/factor) |
| 732 | */ |
| 733 | for (unsigned int pixel = 0; pixel < width*height; pixel++) { |
| 734 | |
| 735 | /* |
| 736 | Light detection |
| 737 | */ |
| 738 | |
| 739 | // maxDiff: Maximum difference to the mean image |
| 740 | // {-255,...,255} |
| 741 | // max: Maximum pixel value |
| 742 | // {0,...,255} |
| 743 | // temp: Sum of all differences |
| 744 | // {-3*255,...,3*255} |
| 745 | // sum: Sum of all pixel values |
| 746 | // {0,...,3*255} |
| 747 | |
| 748 | r = GETR(out[pixel]) - m_longMeanImage[3*pixel+0]; |
| 749 | maxDiff = r; |
| 750 | max = GETR(out[pixel]); |
| 751 | temp = r; |
| 752 | |
| 753 | g = GETG(out[pixel]) - m_longMeanImage[3*pixel+1]; |
| 754 | if (max < GETG(out[pixel])) { |
| 755 | max = GETG(out[pixel]); |
| 756 | } |
| 757 | if (maxDiff < g) { |
| 758 | maxDiff = g; |
| 759 | } |
| 760 | temp += g; |
| 761 | |
| 762 | b = GETB(out[pixel]) - m_longMeanImage[3*pixel+2]; |
| 763 | if (max < GETB(out[pixel])) { |
| 764 | max = GETB(out[pixel]); |
| 765 | } |
| 766 | if (maxDiff < b) { |
| 767 | maxDiff = b; |
| 768 | } |
| 769 | temp += b; |
| 770 | |
| 771 | sum = GETR(out[pixel]) + GETG(out[pixel]) + GETB(out[pixel]); |
| 772 | |
| 773 | if ( |
| 774 | maxDiff > thresholdDifference |
| 775 | && temp > thresholdDiffSum |
| 776 | && sum > thresholdBrightness |
| 777 | // If all requirements are met, then this should be a light source. |
| 778 | ) |
| 779 | { |
| 780 | #ifdef LG_ADV |
| 781 | // Just add values as float. Overflows are highly unlikely (3.4E38+ frames ...). |
| 782 | fr = CLAMP(r)/255.0; |
| 783 | fg = CLAMP(g)/255.0; |
| 784 | fb = CLAMP(b)/255.0; |
| 785 | |
| 786 | f = (fr + fg + fb) / 3 * sensitivity; |
| 787 | fr *= f; |
| 788 | fg *= f; |
| 789 | fb *= f; |
| 790 | |
| 791 | #ifdef LG_NO_OVERLAY |
| 792 | // std::cout << "fr: " << fr << "; fg: " << fg << "; fb: " << fb << "\n"; |
| 793 | fr -= m_prevMask[pixel].r; |
| 794 | fg -= m_prevMask[pixel].g; |
| 795 | fb -= m_prevMask[pixel].b; |
| 796 | m_prevMask[pixel].r += fr; |
| 797 | m_prevMask[pixel].g += fg; |
| 798 | m_prevMask[pixel].b += fb; |
| 799 | // std::cout << "fr2: " << fr << "; fg2: " << fg << "; fb2: " << fb << "\n"; |
| 800 | if (fr < 0) { fr = 0; } |
| 801 | if (fg < 0) { fg = 0; } |
| 802 | if (fb < 0) { fb = 0; } |
| 803 | #endif |
| 804 | |
| 805 | m_rgbLightMask[pixel].r += fr; |
| 806 | m_rgbLightMask[pixel].g += fg; |
| 807 | m_rgbLightMask[pixel].b += fb; |
| 808 | |
| 809 | #else |
| 810 | // Store the «additional» light delivered by the light source in the light mask. |
| 811 | color = RGBA(CLAMP(r), CLAMP(g), CLAMP(b),0xFF); |
| 812 | m_lightMask[pixel] = MAX(m_lightMask[pixel], color); |
| 813 | |
| 814 | // Add the brightness of the light source to the brightness map (alpha map) |
| 815 | y = REC709Y(CLAMP(r), CLAMP(g), CLAMP(b)) / 255.0; |
| 816 | y = y * sensitivity; |
| 817 | m_alphaMap[4*pixel] += y; |
| 818 | #endif |
| 819 | } else { |
| 820 | #ifdef LG_NO_OVERLAY |
| 821 | m_prevMask[pixel] = rgb0; |
| 822 | #endif |
| 823 | } |
| 824 | |
| 825 | |
| 826 | |
| 827 | /* |
| 828 | Background weight |
| 829 | */ |
| 830 | if (m_pBackgroundWeight > 0) { |
| 831 | // Use part of the background mean. This allows one to have only lights appearing in the video |
| 832 | // if people or other objects walk into the video after the first frame (darker, therefore not in the light mask). |
| 833 | out[pixel] = RGBA((int) (m_pBackgroundWeight*m_longMeanImage[3*pixel+0] + (1-m_pBackgroundWeight)*GETR(out[pixel])), |
| 834 | (int) (m_pBackgroundWeight*m_longMeanImage[3*pixel+1] + (1-m_pBackgroundWeight)*GETG(out[pixel])), |
| 835 | (int) (m_pBackgroundWeight*m_longMeanImage[3*pixel+2] + (1-m_pBackgroundWeight)*GETB(out[pixel])), |
| 836 | 0xFF); |
| 837 | } |
| 838 | |
| 839 | |
| 840 | /* |
| 841 | Adding light mask |
| 842 | */ |
| 843 | #ifdef LG_ADV |
| 844 | if ( |
| 845 | (m_rgbLightMask[pixel].r != 0 || m_rgbLightMask[pixel].g != 0 || m_rgbLightMask[pixel].b != 0) |
| 846 | && !m_pStatsBrightness && !m_pStatsDiff && !m_pStatsDiffSum |
| 847 | ) |
| 848 | { |
| 849 | |
| 850 | fr = m_rgbLightMask[pixel].r; |
| 851 | fg = m_rgbLightMask[pixel].g; |
| 852 | fb = m_rgbLightMask[pixel].b; |
| 853 | |
| 854 | if (lowerOverexposure > 0) { |
| 855 | // Comparison of plots with octave: |
| 856 | // clf;hold on;plot([0 1],[0 1],'k');plot(range,ones(length(range),1),'k');plot(range,sqrt(range));plot(range,log(1+range),'k');plot(range,log(1+range),'g');plot(range,(log(1+range)/3).^.5,'r');axis equal |
| 857 | fr = pow( log(1+fr)/lowerOverexposure, .5 ); |
| 858 | fg = pow( log(1+fg)/lowerOverexposure, .5 ); |
| 859 | fb = pow( log(1+fb)/lowerOverexposure, .5 ); |
| 860 | } |
| 861 | |
| 862 | |
| 863 | // Calculate overflow between different colours: |
| 864 | // A very bright red light source will eventually overflow into other channels. |
| 865 | sr = 0; |
| 866 | sg = 0; |
| 867 | sb = 0; |
| 868 | if (fr > 1) { |
| 869 | sr += fr - 1; |
| 870 | } |
| 871 | if (fg > 1) { |
| 872 | sg += fg - 1; |
| 873 | } |
| 874 | if (fb > 1) { |
| 875 | sb += fb - 1; |
| 876 | } |
| 877 | fr += (sg + sb)/2; |
| 878 | fg += (sr + sb)/2; |
| 879 | fb += (sg + sb)/2; |
| 880 | if (fr > 1) { |
| 881 | fr = 1; |
| 882 | } |
| 883 | if (fg > 1) { |
| 884 | fg = 1; |
| 885 | } |
| 886 | if (fb > 1) { |
| 887 | fb = 1; |
| 888 | } |
| 889 | |
| 890 | // Increase the saturation if the average brightness is below a certain level |
| 891 | // Do not use Rec709 Luma since we want to consider all colours to equal parts. |
| 892 | fy = (fr + fg + fb) / 3; |
| 893 | if (fy < 1 && saturation > 0) { |
| 894 | fsat = 1 + saturation*(1-fy); |
| 895 | |
| 896 | fr = fy + fsat * (fr-fy); |
| 897 | fg = fy + fsat * (fg-fy); |
| 898 | fb = fy + fsat * (fb-fy); |
| 899 | } |
| 900 | |
| 901 | // Paint the light on top of the image using addition |
| 902 | // Since brightness is equidistant in sRGB, this works fine. |
| 903 | r = 255*fr + GETR(out[pixel]); |
| 904 | g = 255*fg + GETG(out[pixel]); |
| 905 | b = 255*fb + GETB(out[pixel]); |
| 906 | r = CLAMP(r); |
| 907 | g = CLAMP(g); |
| 908 | b = CLAMP(b); |
| 909 | out[pixel] = RGBA(r,g,b,0xFF); |
| 910 | |
| 911 | #ifdef LG_DEBUG |
| 912 | deCount++; |
| 913 | if (deCount < 10) { |
| 914 | std::cout << "r: " << m_rgbLightMask[pixel].r << ", fy: " << fy << ", fr: " << fr << ", sr: " << sr << ", R: " << r << ", inR: " << GETR(in[pixel]) << "\n"; |
| 915 | } |
| 916 | #endif |
| 917 | |
| 918 | } else if (m_pTransparentBackground) { |
| 919 | // Transparent background |
| 920 | out[pixel] &= RGBA(0xFF, 0xFF, 0xFF, 0); |
| 921 | } |
| 922 | #else |
| 923 | if ( |
| 924 | m_lightMask[pixel] != 0 && m_alphaMap[4*pixel + 0] != 0 |
| 925 | && !m_pStatsBrightness && !m_pStatsDiff && !m_pStatsDiffSum |
| 926 | ) |
| 927 | { |
| 928 | |
| 929 | f = sqrt(m_alphaMap[4*pixel]); |
| 930 | |
| 931 | r = f * GETR(m_lightMask[pixel]); |
| 932 | g = f * GETG(m_lightMask[pixel]); |
| 933 | b = f * GETB(m_lightMask[pixel]); |
| 934 | |
| 935 | if (f > 1) { |
| 936 | // Simulate overexposure |
| 937 | sum = 0; |
| 938 | if (r > 255) { |
| 939 | sum += r-255; |
| 940 | } |
| 941 | if (g > 255) { |
| 942 | sum += g-255; |
| 943 | } |
| 944 | if (b > 255) { |
| 945 | sum += g-255; |
| 946 | } |
| 947 | |
| 948 | if (sum > 0) { |
| 949 | sum = sum/10.0; |
| 950 | r += sum; |
| 951 | g += sum; |
| 952 | b += sum; |
| 953 | } |
| 954 | } else if (f < 1) { |
| 955 | // Lower exposure: Stronger colors |
| 956 | y = REC709Y(r,g,b); |
| 957 | float sat = 2.0; |
| 958 | |
| 959 | r = y + sat * (r-y); |
| 960 | g = y + sat * (g-y); |
| 961 | b = y + sat * (b-y); |
| 962 | } |
| 963 | |
| 964 | |
| 965 | // Add the light map as additional light to the image |
| 966 | r += GETR(out[pixel]); |
| 967 | g += GETG(out[pixel]); |
| 968 | b += GETB(out[pixel]); |
| 969 | r = CLAMP(r); |
| 970 | g = CLAMP(g); |
| 971 | b = CLAMP(b); |
| 972 | out[pixel] = RGBA(r,g,b,0xFF); |
| 973 | } else if (m_pTransparentBackground) { |
| 974 | // Transparent background |
| 975 | out[pixel] &= RGBA(0xFF, 0xFF, 0xFF, 0); |
| 976 | } |
| 977 | #endif |
| 978 | |
| 979 | |
| 980 | /* |
| 981 | In-video statistics for easier parameter adjustment (thresholds) |
| 982 | */ |
| 983 | if (m_pStatsBrightness) { |
| 984 | // Show the image's brightness and highlight the threshold set by the user |
| 985 | |
| 986 | // Limit maximum brightness to 80% for still being able to distinguish |
| 987 | // between «bright spot» (light grey) and «over the threshold» (blue) |
| 988 | r = .8*sum/3; |
| 989 | g = .8*sum/3; |
| 990 | b = .8*sum/3; |
| 991 | if (sum > thresholdBrightness) { |
| 992 | b = 255; |
| 993 | } |
| 994 | out[pixel] = RGBA(r,g,b,0xFF); |
| 995 | } |
| 996 | |
| 997 | if (m_pStatsDiff) { |
| 998 | // As above, but for the brightness difference relative to the background. |
| 999 | r = .8*CLAMP(maxDiff); |
| 1000 | g = r; |
| 1001 | if (!m_pStatsBrightness) { |
| 1002 | b = r; |
| 1003 | } |
| 1004 | |
| 1005 | if (maxDiff > thresholdDifference) { |
| 1006 | g = 255; |
| 1007 | } |
| 1008 | out[pixel] = RGBA(r,g,b,0xFF); |
| 1009 | } |
| 1010 | |
| 1011 | if (m_pStatsDiffSum) { |
| 1012 | // As above, for the sum of the differences in each color channel. |
| 1013 | r = .8*CLAMP(temp/3.0); |
| 1014 | if (!m_pStatsDiff) { |
| 1015 | g = r; |
| 1016 | } |
| 1017 | if (!m_pStatsBrightness) { |
| 1018 | b = r; |
| 1019 | } |
| 1020 | if (temp > thresholdDiffSum) { |
| 1021 | r = 255; |
| 1022 | } |
| 1023 | out[pixel] = RGBA(r,g,b,0xFF); |
| 1024 | } |
| 1025 | } |
| 1026 | break; |
| 1027 | default: |
| 1028 | break; |
| 1029 | } |
| 1030 | } |
| 1031 | |
| 1032 | private: |
| 1033 | std::vector<uint32_t> m_lightMask; |
| 1034 | std::vector<float> m_longMeanImage; |
| 1035 | std::vector<float> m_alphaMap; |
| 1036 | bool m_meanInitialized; |
| 1037 | GraffitiMode m_mode; |
| 1038 | DimMode m_dimMode; |
| 1039 | |
| 1040 | #ifdef LG_ADV |
| 1041 | std::vector<RGBFloat> m_rgbLightMask; |
| 1042 | #endif |
| 1043 | #ifdef LG_NO_OVERLAY |
| 1044 | std::vector<RGBFloat> m_prevMask; |
| 1045 | #endif |
| 1046 | |
| 1047 | double m_pLongAlpha; |
| 1048 | double m_pSensitivity; |
| 1049 | double m_pBackgroundWeight; |
| 1050 | double m_pThresholdBrightness; |
| 1051 | double m_pThresholdDifference; |
| 1052 | double m_pThresholdDiffSum; |
| 1053 | double m_pDim; |
| 1054 | double m_pSaturation; |
| 1055 | double m_pLowerOverexposure; |
| 1056 | bool m_pStatsBrightness; |
| 1057 | bool m_pStatsDiff; |
| 1058 | bool m_pStatsDiffSum; |
| 1059 | bool m_pTransparentBackground; |
| 1060 | bool m_pBlackReference; |
| 1061 | bool m_pNonlinearDim; |
| 1062 | bool m_pReset; |
| 1063 | |
| 1064 | }; |
| 1065 | |
| 1066 | |
| 1067 | |
| 1068 | frei0r::construct<LightGraffiti> plugin("Light Graffiti", |
| 1069 | "Creates light graffitis from a video by keeping the brightest spots.", |
| 1070 | "Simon A. Eugster (Granjow)", |
| 1071 | 0,3, |
| 1072 | F0R_COLOR_MODEL_RGBA8888); |