|
| 1 | +/////////////////////////////////////////////////////////////////////////// |
| 2 | +// Copyright (c) 2013 Academy of Motion Picture Arts and Sciences |
| 3 | +// ("A.M.P.A.S."). Portions contributed by others as indicated. |
| 4 | +// All rights reserved. |
| 5 | +// |
| 6 | +// A worldwide, royalty-free, non-exclusive right to copy, modify, create |
| 7 | +// derivatives, and use, in source and binary forms, is hereby granted, |
| 8 | +// subject to acceptance of this license. Performance of any of the |
| 9 | +// aforementioned acts indicates acceptance to be bound by the following |
| 10 | +// terms and conditions: |
| 11 | +// |
| 12 | +// * Copies of source code, in whole or in part, must retain the |
| 13 | +// above copyright notice, this list of conditions and the |
| 14 | +// Disclaimer of Warranty. |
| 15 | +// |
| 16 | +// * Use in binary form must retain the above copyright notice, |
| 17 | +// this list of conditions and the Disclaimer of Warranty in the |
| 18 | +// documentation and/or other materials provided with the distribution. |
| 19 | +// |
| 20 | +// * Nothing in this license shall be deemed to grant any rights to |
| 21 | +// trademarks, copyrights, patents, trade secrets or any other |
| 22 | +// intellectual property of A.M.P.A.S. or any contributors, except |
| 23 | +// as expressly stated herein. |
| 24 | +// |
| 25 | +// * Neither the name "A.M.P.A.S." nor the name of any other |
| 26 | +// contributors to this software may be used to endorse or promote |
| 27 | +// products derivative of or based on this software without express |
| 28 | +// prior written permission of A.M.P.A.S. or the contributors, as |
| 29 | +// appropriate. |
| 30 | +// |
| 31 | +// This license shall be construed pursuant to the laws of the State of |
| 32 | +// California, and any disputes related thereto shall be subject to the |
| 33 | +// jurisdiction of the courts therein. |
| 34 | +// |
| 35 | +// Disclaimer of Warranty: THIS SOFTWARE IS PROVIDED BY A.M.P.A.S. AND |
| 36 | +// CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, |
| 37 | +// BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS |
| 38 | +// FOR A PARTICULAR PURPOSE, AND NON-INFRINGEMENT ARE DISCLAIMED. IN NO |
| 39 | +// EVENT SHALL A.M.P.A.S., OR ANY CONTRIBUTORS OR DISTRIBUTORS, BE LIABLE |
| 40 | +// FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, RESITUTIONARY, |
| 41 | +// OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 42 | +// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 43 | +// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 44 | +// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 45 | +// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF |
| 46 | +// THE POSSIBILITY OF SUCH DAMAGE. |
| 47 | +// |
| 48 | +// WITHOUT LIMITING THE GENERALITY OF THE FOREGOING, THE ACADEMY |
| 49 | +// SPECIFICALLY DISCLAIMS ANY REPRESENTATIONS OR WARRANTIES WHATSOEVER |
| 50 | +// RELATED TO PATENT OR OTHER INTELLECTUAL PROPERTY RIGHTS IN THE ACADEMY |
| 51 | +// COLOR ENCODING SYSTEM, OR APPLICATIONS THEREOF, HELD BY PARTIES OTHER |
| 52 | +// THAN A.M.P.A.S., WHETHER DISCLOSED OR UNDISCLOSED. |
| 53 | +/////////////////////////////////////////////////////////////////////////// |
| 54 | + |
| 55 | +// |
| 56 | +// Function change_saturation() saturates or desaturates an RGB image by |
| 57 | +// converting the pixels to CIELAB, multiplying the a* and b* components |
| 58 | +// by a caller-supplied factor, sScale, and converting back to RGB. |
| 59 | +// |
| 60 | +// Parameters: |
| 61 | +// |
| 62 | +// R, G, B Input pixel value |
| 63 | +// |
| 64 | +// ROut, GOut, BOut Output pixel value |
| 65 | +// |
| 66 | +// sScale Saturation scale factor: |
| 67 | +// sScale > 1 increases saturation, |
| 68 | +// 0 <= sScale < 1 decreases saturation, |
| 69 | +// |
| 70 | +// chromaticities CIE (x,y) coordinates of the primaries |
| 71 | +// and white point for the input and output |
| 72 | +// pixels |
| 73 | +// |
| 74 | +// whiteLuminance Luminance of pixels with R = G = B = 1 |
| 75 | +// |
| 76 | +// adoptedNeutral CIE (x,y) coordinates of the white stimulus |
| 77 | +// for RGB-to-LAB and LAB-to-RGB conversion. |
| 78 | +// Decreasing or increasing saturation makes |
| 79 | +// the pixel colors move towards or away from |
| 80 | +// the adoptedNeutral value. |
| 81 | +// |
| 82 | + |
| 83 | +void |
| 84 | +change_saturation |
| 85 | + (output varying half ROut, |
| 86 | + output varying half GOut, |
| 87 | + output varying half BOut, |
| 88 | + varying half R, |
| 89 | + varying half G, |
| 90 | + varying half B, |
| 91 | + float adoptedNeutral[2], |
| 92 | + Chromaticities chromaticities, |
| 93 | + float whiteLuminance = 1.0, |
| 94 | + float sScale = 1.0) |
| 95 | +{ |
| 96 | + // |
| 97 | + // Compute the XYZ coordinates of the white stimulus. |
| 98 | + // |
| 99 | + |
| 100 | + float XYZn[3]; |
| 101 | + XYZn[0] = adoptedNeutral[0] * whiteLuminance / adoptedNeutral[1]; |
| 102 | + XYZn[1] = whiteLuminance; |
| 103 | + XYZn[2] = whiteLuminance / adoptedNeutral[1] - XYZn[0] - whiteLuminance; |
| 104 | + |
| 105 | + // |
| 106 | + // Convert input RGB value first to XYZ, then to LAB |
| 107 | + // |
| 108 | + |
| 109 | + float toXYZ[4][4] = RGBtoXYZ (chromaticities, whiteLuminance); |
| 110 | + float RGB[3] = {R, G, B}; |
| 111 | + float XYZ[3] = mult_f3_f44 (RGB, toXYZ); |
| 112 | + float Lab[3] = XYZtoLab (XYZ, XYZn); |
| 113 | + |
| 114 | + // |
| 115 | + // Change saturation by scaling the a* and b* coordinates. |
| 116 | + // |
| 117 | + |
| 118 | + Lab[1] = Lab[1] * sScale; |
| 119 | + Lab[2] = Lab[2] * sScale; |
| 120 | + |
| 121 | + // |
| 122 | + // Convert back to XYZ and then to RGB |
| 123 | + // |
| 124 | + |
| 125 | + XYZ = LabtoXYZ (Lab, XYZn); |
| 126 | + float toRGB[4][4] = XYZtoRGB (chromaticities, whiteLuminance); |
| 127 | + RGB = mult_f3_f44 (XYZ, toRGB); |
| 128 | + ROut = RGB[0]; |
| 129 | + GOut = RGB[1]; |
| 130 | + BOut = RGB[2]; |
| 131 | +} |
0 commit comments