| 1 | /* |
| 2 | * normaliz0r.c |
| 3 | * Copyright (C) 2017 Chungzuwalla |
| 4 | * This file is a Frei0r plugin. |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License as published by |
| 8 | * the Free Software Foundation; either version 2 of the License, or |
| 9 | * (at your option) any later version. |
| 10 | * |
| 11 | * This program is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | * GNU General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License |
| 17 | * along with this program; if not, write to the Free Software |
| 18 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
| 19 | */ |
| 20 | |
| 21 | /* |
| 22 | * Normalize video (aka histogram stretching, contrast stretching). |
| 23 | * See: https://en.wikipedia.org/wiki/Normalization_(image_processing) |
| 24 | * |
| 25 | * For each channel of each frame, the filter computes the input range and maps |
| 26 | * it linearly to the user-specified output range. The output range defaults |
| 27 | * to the full dynamic range from pure black to pure white. |
| 28 | * |
| 29 | * The filter can apply temporal smoothing to the input range to reduce rapid |
| 30 | * changes in brightness (flickering) caused by small dark or bright objects |
| 31 | * entering or leaving the scene. This effect is similar to the auto-exposure |
| 32 | * on a video camera. |
| 33 | * |
| 34 | * The filter can normalize the R,G,B channels independently, which may cause |
| 35 | * color shifting, or link them together as a single channel, which prevents |
| 36 | * color shifting. More precisely, linked normalization preserves hues (as |
| 37 | * defined in HSV/HSL color spaces) while independent normalization does not. |
| 38 | * Independent normalization can be used to remove color casts, such as the |
| 39 | * blue cast from underwater video, restoring more natural colors. The filter |
| 40 | * can also combine independent and linked normalization in any ratio. |
| 41 | * |
| 42 | * Finally the overall strength of the filter can be adjusted, from no effect |
| 43 | * to full normalization. |
| 44 | * |
| 45 | * The 5 user parameters are: |
| 46 | * BlackPt, Colors which define the output range. The minimum input value |
| 47 | * WhitePt is mapped to the BlackPt. The maximum input value is mapped to |
| 48 | * the WhitePt. The defaults are black and white respectively. |
| 49 | * Specifying white for BlackPt and black for WhitePt will give |
| 50 | * color-inverted, normalized video. Shades of grey can be used |
| 51 | * to reduce the dynamic range (contrast). Specifying saturated |
| 52 | * colors here can create some interesting effects. |
| 53 | * |
| 54 | * Smoothing The amount of temporal smoothing. The parameter is converted |
| 55 | * into a number of frames N in the range [1,MAX_HISTORY_LEN], and |
| 56 | * the minimum and maximum input value of each channel are then |
| 57 | * smoothed using a rolling average over the last N frames. |
| 58 | * Defaults to 0.0 (no temporal smoothing). |
| 59 | * |
| 60 | * Independence |
| 61 | * Controls the ratio of independent (color shifting) channel |
| 62 | * normalization to linked (color preserving) normalization. 0.0 |
| 63 | * is fully linked, 1.0 is fully independent. Defaults to fully |
| 64 | * independent. |
| 65 | * |
| 66 | * Strength Overall strength of the filter. 1.0 is full strength. 0.0 is |
| 67 | * a rather expensive no-op. Values in between can give a gentle |
| 68 | * boost to low-contrast video without creating an artificial |
| 69 | * over-processed look. The default is full strength. |
| 70 | */ |
| 71 | |
| 72 | #include <assert.h> |
| 73 | #include <stdlib.h> |
| 74 | #include <stdio.h> |
| 75 | |
| 76 | #include "frei0r.h" |
| 77 | #include "frei0r/math.h" |
| 78 | |
| 79 | #define MAX_HISTORY_LEN 128 |
| 80 | |
| 81 | typedef struct |
| 82 | { |
| 83 | int num_pixels; // Number of pixels in a frame. |
| 84 | int frame_num; // Increments on each frame, starting from 0. |
| 85 | |
| 86 | // Per-extrema, per-channel fri0r params |
| 87 | struct |
| 88 | { |
| 89 | uint8_t history[MAX_HISTORY_LEN]; // Param history, for temporal smoothing. |
| 90 | uint16_t history_sum; // Sum of history entries. |
| 91 | float param; // Target output value [0,255] |
| 92 | } min[3], max[3]; // Min and max for each channel in {R,G,B}. |
| 93 | |
| 94 | // Per-instance frei0r params, each a float in the range [0,1]. |
| 95 | int history_len; // Global max history length, which controls amount of |
| 96 | // temporal smoothing. [1,MAX_HISTORY_LEN]. |
| 97 | float independence; // Ratio of independent vs linked normalization [0,1]. |
| 98 | float strength; // Mixing strength for the normalization [0,1]. |
| 99 | } normaliz0r_instance_t; |
| 100 | |
| 101 | int |
| 102 | f0r_init () |
| 103 | { |
| 104 | return 1; |
| 105 | } |
| 106 | |
| 107 | void |
| 108 | f0r_deinit () |
| 109 | { |
| 110 | } |
| 111 | |
| 112 | void |
| 113 | f0r_get_plugin_info (f0r_plugin_info_t* info) |
| 114 | { |
| 115 | info->name = "Normaliz0r"; |
| 116 | info->author = "Chungzuwalla (chungzuwalla\100rling.com)"; |
| 117 | info->plugin_type = F0R_PLUGIN_TYPE_FILTER; |
| 118 | info->color_model = F0R_COLOR_MODEL_RGBA8888; |
| 119 | info->frei0r_version = FREI0R_MAJOR_VERSION; |
| 120 | info->major_version = 0; |
| 121 | info->minor_version = 1; |
| 122 | info->num_params = 5; |
| 123 | info->explanation = "Normalize (aka histogram stretch, contrast stretch)"; |
| 124 | } |
| 125 | |
| 126 | static char *param_infos[] = |
| 127 | { |
| 128 | "BlackPt", "Output color to which darkest input color is mapped (default black)", |
| 129 | "WhitePt", "Output color to which brightest input color is mapped (default white)", |
| 130 | "Smoothing", "Amount of temporal smoothing of the input range, to reduce flicker (default 0.0)", |
| 131 | "Independence", "Proportion of independent to linked channel normalization (default 1.0)", |
| 132 | "Strength", "Strength of filter, from no effect to full normalization (default 1.0)", |
| 133 | }; |
| 134 | |
| 135 | void |
| 136 | f0r_get_param_info (f0r_param_info_t* info, int param_index) |
| 137 | { |
| 138 | info->name = param_infos[param_index * 2]; |
| 139 | info->type = (param_index <= 1) ? F0R_PARAM_COLOR : F0R_PARAM_DOUBLE; |
| 140 | info->explanation = param_infos[param_index * 2 + 1]; |
| 141 | } |
| 142 | |
| 143 | f0r_instance_t |
| 144 | f0r_construct (unsigned int width, unsigned int height) |
| 145 | { |
| 146 | normaliz0r_instance_t* inst = (normaliz0r_instance_t*)calloc(1, sizeof(*inst)); |
| 147 | int c; |
| 148 | inst->num_pixels = width * height; |
| 149 | inst->frame_num = 0; |
| 150 | for (c = 0; c < 3; c++) |
| 151 | { |
| 152 | inst->min[c].history_sum = 0; |
| 153 | inst->max[c].history_sum = 0; |
| 154 | // Set default per-channel frei0r param values. |
| 155 | inst->min[c].param = 0.0; // black |
| 156 | inst->max[c].param = 255.0; // white |
| 157 | } |
| 158 | // Set default per-instance frei0r param values. |
| 159 | inst->history_len = 1; // [1,MAX_HISTORY_LEN]; default is no smoothing |
| 160 | inst->independence = 1.0; // [0,1]; default is fully independent |
| 161 | inst->strength = 1.0; // [0,1]; default is full strength |
| 162 | return (f0r_instance_t)inst; |
| 163 | } |
| 164 | |
| 165 | void |
| 166 | f0r_destruct (f0r_instance_t instance) |
| 167 | { |
| 168 | free (instance); |
| 169 | } |
| 170 | |
| 171 | void |
| 172 | f0r_set_param_value (f0r_instance_t instance, f0r_param_t param, |
| 173 | int param_index) |
| 174 | { |
| 175 | assert(instance); |
| 176 | normaliz0r_instance_t* inst = (normaliz0r_instance_t*)instance; |
| 177 | float val; |
| 178 | |
| 179 | switch (param_index) |
| 180 | { |
| 181 | case 0: |
| 182 | inst->min[0].param = ((f0r_param_color_t*)param)->r * 255.0; |
| 183 | inst->min[1].param = ((f0r_param_color_t*)param)->g * 255.0; |
| 184 | inst->min[2].param = ((f0r_param_color_t*)param)->b * 255.0; |
| 185 | break; |
| 186 | case 1: |
| 187 | inst->max[0].param = ((f0r_param_color_t*)param)->r * 255.0; |
| 188 | inst->max[1].param = ((f0r_param_color_t*)param)->g * 255.0; |
| 189 | inst->max[2].param = ((f0r_param_color_t*)param)->b * 255.0; |
| 190 | break; |
| 191 | case 2: |
| 192 | val = (float)CLAMP(*((double* )param), 0.0, 1.0); |
| 193 | // Map [0,1] <-> [1,MAX_HISTORY_LEN] |
| 194 | inst->history_len = (int)(val * (MAX_HISTORY_LEN - 1)) + 1; |
| 195 | break; |
| 196 | case 3: |
| 197 | val = (float)CLAMP(*((double* )param), 0.0, 1.0); |
| 198 | inst->independence = val; |
| 199 | break; |
| 200 | case 4: |
| 201 | val = (float)CLAMP(*((double* )param), 0.0, 1.0); |
| 202 | inst->strength = val; |
| 203 | break; |
| 204 | } |
| 205 | } |
| 206 | |
| 207 | void |
| 208 | f0r_get_param_value (f0r_instance_t instance, f0r_param_t param, |
| 209 | int param_index) |
| 210 | { |
| 211 | assert(instance); |
| 212 | normaliz0r_instance_t* inst = (normaliz0r_instance_t*)instance; |
| 213 | switch (param_index) |
| 214 | { |
| 215 | case 0: |
| 216 | ((f0r_param_color_t*)param)->r = inst->min[0].param / 255.0; |
| 217 | ((f0r_param_color_t*)param)->g = inst->min[1].param / 255.0; |
| 218 | ((f0r_param_color_t*)param)->b = inst->min[2].param / 255.0; |
| 219 | break; |
| 220 | case 1: |
| 221 | ((f0r_param_color_t*)param)->r = inst->max[0].param / 255.0; |
| 222 | ((f0r_param_color_t*)param)->g = inst->max[1].param / 255.0; |
| 223 | ((f0r_param_color_t*)param)->b = inst->max[2].param / 255.0; |
| 224 | break; |
| 225 | case 2: |
| 226 | // Map [0,1] <-> [1,MAX_HISTORY_LEN] |
| 227 | *((double*)param) = (double)(inst->history_len - 1) / (MAX_HISTORY_LEN - 1); |
| 228 | break; |
| 229 | case 3: |
| 230 | *((double*)param) = inst->independence; |
| 231 | break; |
| 232 | case 4: |
| 233 | *((double*)param) = inst->strength; |
| 234 | break; |
| 235 | } |
| 236 | } |
| 237 | |
| 238 | void |
| 239 | f0r_update (f0r_instance_t instance, double time, const uint32_t* inframe, |
| 240 | uint32_t* outframe) |
| 241 | { |
| 242 | assert(instance); |
| 243 | normaliz0r_instance_t* inst = (normaliz0r_instance_t*)instance; |
| 244 | int c; |
| 245 | |
| 246 | // Per-extrema, per-channel local variables. |
| 247 | struct |
| 248 | { |
| 249 | uint8_t in; // Original input byte value for this frame. |
| 250 | float smoothed; // Smoothed input value [0,255]. |
| 251 | float out; // Output value [0,255]. |
| 252 | } min[3], max[3]; // Min and max for each channel in {R,G,B}. |
| 253 | |
| 254 | // First, scan the input frame to find, for each channel, the minimum |
| 255 | // (min.in) and maximum (max.in) values present in the channel. |
| 256 | { |
| 257 | const uint8_t *in = (const uint8_t*)inframe; |
| 258 | |
| 259 | #define INIT(c) (min[c].in = max[c].in = in[c]) |
| 260 | #define EXTEND(c) (min[c].in = MIN(min[c].in, in[c])), \ |
| 261 | (max[c].in = MAX(max[c].in, in[c])) |
| 262 | INIT(0); |
| 263 | INIT(1); |
| 264 | INIT(2); |
| 265 | in += 4; |
| 266 | // Process (num_pixels - 1), as we used the first pixel to initialize |
| 267 | unsigned int num_pixels = inst->num_pixels - 1; |
| 268 | while (num_pixels--) |
| 269 | { |
| 270 | EXTEND(0); |
| 271 | EXTEND(1); |
| 272 | EXTEND(2); |
| 273 | in += 4; |
| 274 | } |
| 275 | } |
| 276 | |
| 277 | // Next, for each channel, push min.in and max.in into their respective |
| 278 | // histories, to determine the min.smoothed and max.smoothed for this frame. |
| 279 | { |
| 280 | int history_idx = inst->frame_num % inst->history_len; |
| 281 | // Assume the history is not yet full; num_history_vals is the number of |
| 282 | // frames received so far including the current frame. |
| 283 | int num_history_vals = inst->frame_num + 1; |
| 284 | if (inst->frame_num >= inst->history_len) |
| 285 | { |
| 286 | //The history is full; drop oldest value and cap num_history_vals. |
| 287 | for (c = 0; c < 3; c++) |
| 288 | { |
| 289 | inst->min[c].history_sum -= inst->min[c].history[history_idx]; |
| 290 | inst->max[c].history_sum -= inst->max[c].history[history_idx]; |
| 291 | } |
| 292 | num_history_vals = inst->history_len; |
| 293 | } |
| 294 | // For each extremum, update history_sum and calculate smoothed value as |
| 295 | // the rolling average of the history entries. |
| 296 | for (c = 0; c < 3; c++) |
| 297 | { |
| 298 | inst->min[c].history_sum += (inst->min[c].history[history_idx] = min[c].in); |
| 299 | min[c].smoothed = (float)inst->min[c].history_sum / (float)num_history_vals; |
| 300 | inst->max[c].history_sum += (inst->max[c].history[history_idx] = max[c].in); |
| 301 | max[c].smoothed = (float)inst->max[c].history_sum / (float)num_history_vals; |
| 302 | } |
| 303 | } |
| 304 | |
| 305 | // Determine the input range for linked normalization. This is simply the |
| 306 | // minimum of the per-channel minimums, and the maximum of the per-channel |
| 307 | // maximums. |
| 308 | float rgb_min_smoothed = min[0].smoothed; |
| 309 | float rgb_max_smoothed = max[0].smoothed; |
| 310 | rgb_min_smoothed = MIN(rgb_min_smoothed, min[1].smoothed); |
| 311 | rgb_max_smoothed = MAX(rgb_max_smoothed, max[1].smoothed); |
| 312 | rgb_min_smoothed = MIN(rgb_min_smoothed, min[2].smoothed); |
| 313 | rgb_max_smoothed = MAX(rgb_max_smoothed, max[2].smoothed); |
| 314 | |
| 315 | // Now, process each channel to determine the input and output range and |
| 316 | // build the lookup tables. |
| 317 | uint8_t lut[3][256]; |
| 318 | for (c = 0; c < 3; c++) |
| 319 | { |
| 320 | // Adjust the input range for this channel [min.smoothed,max.smoothed] by |
| 321 | // mixing in the correct proportion of the linked normalization input range |
| 322 | // [rgb_min_smoothed,rgb_max_smoothed]. |
| 323 | min[c].smoothed = (min[c].smoothed * inst->independence) |
| 324 | + (rgb_min_smoothed * (1.0 - inst->independence)); |
| 325 | max[c].smoothed = (max[c].smoothed * inst->independence) |
| 326 | + (rgb_max_smoothed * (1.0 - inst->independence)); |
| 327 | |
| 328 | // Calculate the output range [min.out,max.out] as a ratio of the full- |
| 329 | // strength output range [min.param,max.param] and the original input |
| 330 | // range [min.in,max.in], based on the user-specified filter strength. |
| 331 | min[c].out = (inst->min[c].param * inst->strength) |
| 332 | + ((float)min[c].in * (1.0 - inst->strength)); |
| 333 | max[c].out = (inst->max[c].param * inst->strength) |
| 334 | + ((float)max[c].in * (1.0 - inst->strength)); |
| 335 | |
| 336 | // Now, build a lookup table which linearly maps the adjusted input range |
| 337 | // [min.smoothed,max.smoothed] to the output range [min.out,max.out]. |
| 338 | // Perform the linear interpolation for each x: |
| 339 | // lut[x] = (int)(float(x - min.smoothed) * scale + max.out + 0.5) |
| 340 | // where scale = (max.out - min.out) / (max.smoothed - min.smoothed) |
| 341 | if (min[c].smoothed == max[c].smoothed) |
| 342 | { |
| 343 | // There is no dynamic range to expand. No mapping for this channel. |
| 344 | int in_val; |
| 345 | for (in_val = min[c].in; in_val <= max[c].in; in_val++) |
| 346 | lut[c][in_val] = min[c].out; |
| 347 | } |
| 348 | else |
| 349 | { |
| 350 | // We must set lookup values for all values in the original input range |
| 351 | // [min.in,max.in]. Since the original input range may be larger than |
| 352 | // [min.smoothed,max.smoothed], some output values may fall outside the |
| 353 | // [0,255] dynamic range. We need to CLAMP() them. |
| 354 | float scale = (max[c].out - min[c].out) / (max[c].smoothed - min[c].smoothed); |
| 355 | int in_val; |
| 356 | for (in_val = min[c].in; in_val <= max[c].in; in_val++) |
| 357 | { |
| 358 | int out_val = ROUND((in_val - min[c].smoothed) * scale + min[c].out); |
| 359 | lut[c][in_val] = CLAMP(out_val, 0, 255); |
| 360 | } |
| 361 | } |
| 362 | } |
| 363 | |
| 364 | // Finally, process the pixels of the input frame using the lookup tables. |
| 365 | // Copy alpha as-is. |
| 366 | { |
| 367 | const uint8_t *in = (const uint8_t*)inframe; |
| 368 | uint8_t *out = (uint8_t*)outframe; |
| 369 | |
| 370 | #define MAP(c) (out[c] = lut[c][in[c]]) |
| 371 | |
| 372 | unsigned int num_pixels = inst->num_pixels; |
| 373 | while (num_pixels--) |
| 374 | { |
| 375 | MAP(0); |
| 376 | MAP(1); |
| 377 | MAP(2); |
| 378 | out[3] = in[3]; // copy alpha |
| 379 | in += 4; |
| 380 | out += 4; |
| 381 | } |
| 382 | } |
| 383 | |
| 384 | inst->frame_num++; |
| 385 | } |