| 1 | /* |
| 2 | * Copyright (C) 2010 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 | #include <math.h> |
| 21 | #include <stdlib.h> |
| 22 | #include "frei0r.hpp" |
| 23 | #include "frei0r/math.h" |
| 24 | |
| 25 | /** |
| 26 | This filter implements a standard way of color correction proposed by |
| 27 | the American Society of Cinematographers: The Color Decision List, also |
| 28 | known as the |
| 29 | |
| 30 | ASC CDL |
| 31 | |
| 32 | More information about the ASC CDL can be found on Wikipedia[1], and |
| 33 | the current revision of the specification, including example code, can |
| 34 | be obtained by sending a mail to asc-cdl at theasc dot com. (Really works.) |
| 35 | |
| 36 | The ASC CDL is a standard format for basic primary color correction (primary |
| 37 | meaning affecting the whole image and not only selected parts). Even big |
| 38 | editing systems use it :) This filter only obtains the values; Importing and |
| 39 | exporting to one of the possible ASC CDL exchange files must be done elsewhere. |
| 40 | |
| 41 | Basically there are two stages in the correction: |
| 42 | 1. SOP correction for each channel separately |
| 43 | 2. Overall saturation correction |
| 44 | All corrections work on [0,1], so the RGB(A) values need to be transposed |
| 45 | from {0,...,255} to [0,1]. |
| 46 | 1. SOP correction |
| 47 | * Slope: out = in * slope; 0 <= slope < \infty |
| 48 | * Offset: out = in + offset; -\infty < offset < \infty |
| 49 | * Power: out = in^power; 0 < power < \infty |
| 50 | 2. Saturation |
| 51 | * Luma: Y = 0.2126 R + 0.7152 G + 0.0722 B (according to Rec. 709) |
| 52 | * Forall channels: |
| 53 | out = luma + sat * (in-luma) |
| 54 | As the values may exceed 1 (or 0), they need to be clamped where necessary. |
| 55 | |
| 56 | |
| 57 | [1] http://en.wikipedia.org/wiki/Color_Decision_List |
| 58 | */ |
| 59 | |
| 60 | class SOPSat : public frei0r::filter |
| 61 | { |
| 62 | |
| 63 | public: |
| 64 | |
| 65 | double rSlope; |
| 66 | double gSlope; |
| 67 | double bSlope; |
| 68 | double aSlope; |
| 69 | double rOffset; |
| 70 | double gOffset; |
| 71 | double bOffset; |
| 72 | double aOffset; |
| 73 | double rPower; |
| 74 | double gPower; |
| 75 | double bPower; |
| 76 | double aPower; |
| 77 | double saturation; |
| 78 | |
| 79 | SOPSat(unsigned int, unsigned int) |
| 80 | { |
| 81 | |
| 82 | register_param(rSlope, "rSlope", "Slope of the red color component"); |
| 83 | register_param(gSlope, "gSlope", "Slope of the green color component"); |
| 84 | register_param(bSlope, "bSlope", "Slope of the blue color component"); |
| 85 | register_param(aSlope, "aSlope", "Slope of the alpha component"); |
| 86 | register_param(rOffset, "rOffset", "Offset of the red color component"); |
| 87 | register_param(gOffset, "gOffset", "Offset of the green color component"); |
| 88 | register_param(bOffset, "bOffset", "Offset of the blue color component"); |
| 89 | register_param(aOffset, "aOffset", "Offset of the alpha component"); |
| 90 | register_param(rPower, "rPower", "Power (Gamma) of the red color component"); |
| 91 | register_param(gPower, "gPower", "Power (Gamma) of the green color component"); |
| 92 | register_param(bPower, "bPower", "Power (Gamma) of the blue color component"); |
| 93 | register_param(aPower, "aPower", "Power (Gamma) of the alpha component"); |
| 94 | register_param(saturation, "saturation", "Overall saturation"); |
| 95 | rSlope = 1 / 20.; |
| 96 | gSlope = 1 / 20.; |
| 97 | bSlope = 1 / 20.; |
| 98 | aSlope = 1 / 20.; |
| 99 | rOffset = (0 - (-4)) / (double)(4 - (-4)); |
| 100 | gOffset = (0 - (-4)) / (double)(4 - (-4)); |
| 101 | bOffset = (0 - (-4)) / (double)(4 - (-4)); |
| 102 | aOffset = (0 - (-4)) / (double)(4 - (-4)); |
| 103 | rPower = 1 / 20.; |
| 104 | gPower = 1 / 20.; |
| 105 | bPower = 1 / 20.; |
| 106 | aPower = 1 / 20.; |
| 107 | saturation = 1 / 10.; |
| 108 | |
| 109 | // Pre-build the lookup table. |
| 110 | // For 1080p, rendering a 5-second video took |
| 111 | // * 37 s without the LUT |
| 112 | // * 7 s with the LUT |
| 113 | // * 5 s without any effect applied (plain rendering). |
| 114 | // So the LUT brings about 15x speedup. |
| 115 | m_lutR = (unsigned char *) malloc(256*sizeof(char)); |
| 116 | m_lutG = (unsigned char *) malloc(256*sizeof(char)); |
| 117 | m_lutB = (unsigned char *) malloc(256*sizeof(char)); |
| 118 | m_lutA = (unsigned char *) malloc(256*sizeof(char)); |
| 119 | updateLUT(); |
| 120 | |
| 121 | } |
| 122 | |
| 123 | ~SOPSat() |
| 124 | { |
| 125 | free(m_lutR); |
| 126 | free(m_lutG); |
| 127 | free(m_lutB); |
| 128 | free(m_lutA); |
| 129 | } |
| 130 | |
| 131 | virtual void update(double time, |
| 132 | uint32_t* out, |
| 133 | const uint32_t* in) |
| 134 | { |
| 135 | // Rebuild the lookup table in case the parameters have changed. |
| 136 | updateLUT(); |
| 137 | |
| 138 | unsigned char *pixel = (unsigned char *) in; |
| 139 | unsigned char *dest = (unsigned char *) out; |
| 140 | |
| 141 | if (fabs(m_sat-1) < 0.001) { |
| 142 | // Calculating the saturation is expensive. So first check whether |
| 143 | // we really need to do it. |
| 144 | // Keeping the if/else outside of the loop gives a little speed gain. |
| 145 | // Worth the duplicate code, as only 4 lines so far :) |
| 146 | |
| 147 | for (unsigned int i = 0; i < size; i++) { |
| 148 | *dest++ = m_lutR[*pixel++]; |
| 149 | *dest++ = m_lutG[*pixel++]; |
| 150 | *dest++ = m_lutB[*pixel++]; |
| 151 | *dest++ = m_lutA[*pixel++]; |
| 152 | } |
| 153 | } else { |
| 154 | double luma; |
| 155 | for (unsigned int i = 0; i < size; i++) { |
| 156 | luma = 0.2126 * m_lutR[*(pixel+0)] |
| 157 | + 0.7152 * m_lutG[*(pixel+1)] |
| 158 | + 0.0722 * m_lutB[*(pixel+2)]; |
| 159 | *dest++ = CLAMP0255(luma + m_sat*(m_lutR[*pixel++]-luma)); |
| 160 | *dest++ = CLAMP0255(luma + m_sat*(m_lutG[*pixel++]-luma)); |
| 161 | *dest++ = CLAMP0255(luma + m_sat*(m_lutB[*pixel++]-luma)); |
| 162 | *dest++ = m_lutA[*pixel++]; |
| 163 | } |
| 164 | } |
| 165 | } |
| 166 | |
| 167 | private: |
| 168 | unsigned char *m_lutR; |
| 169 | unsigned char *m_lutG; |
| 170 | unsigned char *m_lutB; |
| 171 | unsigned char *m_lutA; |
| 172 | |
| 173 | double m_sat; |
| 174 | |
| 175 | void updateLUT() { |
| 176 | double rS = rSlope * 20; |
| 177 | double gS = gSlope * 20; |
| 178 | double bS = bSlope * 20; |
| 179 | double aS = aSlope * 20; |
| 180 | |
| 181 | double rO = -4 + rOffset * (4 - (-4)); |
| 182 | double gO = -4 + gOffset * (4 - (-4)); |
| 183 | double bO = -4 + bOffset * (4 - (-4)); |
| 184 | double aO = -4 + aOffset * (4 - (-4)); |
| 185 | |
| 186 | double rP = rPower * 20; |
| 187 | double gP = gPower * 20; |
| 188 | double bP = bPower * 20; |
| 189 | double aP = aPower * 20; |
| 190 | |
| 191 | m_sat = saturation * 10; |
| 192 | |
| 193 | for (int i = 0; i < 256; i++) { |
| 194 | // above0 avoids overflows for negative numbers. |
| 195 | m_lutR[i] = CLAMP0255((int) (pow(above0((float)i/255 * rS + rO), rP)*255)); |
| 196 | m_lutG[i] = CLAMP0255((int) (pow(above0((float)i/255 * gS + gO), gP)*255)); |
| 197 | m_lutB[i] = CLAMP0255((int) (pow(above0((float)i/255 * bS + bO), bP)*255)); |
| 198 | m_lutA[i] = CLAMP0255((int) (pow(above0((float)i/255 * aS + aO), aP)*255)); |
| 199 | } |
| 200 | } |
| 201 | |
| 202 | double above0(double f) { |
| 203 | if (f < 0) { |
| 204 | return 0; |
| 205 | } else { |
| 206 | return f; |
| 207 | } |
| 208 | } |
| 209 | |
| 210 | }; |
| 211 | |
| 212 | |
| 213 | |
| 214 | frei0r::construct<SOPSat> plugin("SOP/Sat", |
| 215 | "Slope/Offset/Power and Saturation color corrections according to the ASC CDL (Color Decision List)", |
| 216 | "Simon A. Eugster (Granjow)", |
| 217 | 0,3, |
| 218 | F0R_COLOR_MODEL_RGBA8888); |