1 /* 2 * Copyright (C) 2021 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.android.internal.graphics.cam; 18 19 import android.annotation.NonNull; 20 import android.util.MathUtils; 21 22 import com.android.internal.annotations.VisibleForTesting; 23 24 /** 25 * The frame, or viewing conditions, where a color was seen. Used, along with a color, to create a 26 * color appearance model representing the color. 27 * 28 * <p>To convert a traditional color to a color appearance model, it requires knowing what 29 * conditions the color was observed in. Our perception of color depends on, for example, the tone 30 * of the light illuminating the color, how bright that light was, etc. 31 * 32 * <p>This class is modelled separately from the color appearance model itself because there are a 33 * number of calculations during the color => CAM conversion process that depend only on the viewing 34 * conditions. Caching those calculations in a Frame instance saves a significant amount of time. 35 */ 36 public final class Frame { 37 // Standard viewing conditions assumed in RGB specification - Stokes, Anderson, Chandrasekar, 38 // Motta - A Standard Default Color Space for the Internet: sRGB, 1996. 39 // 40 // White point = D65 41 // Luminance of adapting field: 200 / Pi / 5, units are cd/m^2. 42 // sRGB ambient illuminance = 64 lux (per sRGB spec). However, the spec notes this is 43 // artificially low and based on monitors in 1990s. Use 200, the sRGB spec says this is the 44 // real average, and a survey of lux values on Wikipedia confirms this is a comfortable 45 // default: somewhere between a very dark overcast day and office lighting. 46 // Per CAM16 introduction paper (Li et al, 2017) Ew = pi * lw, and La = lw * Yb/Yw 47 // Ew = ambient environment luminance, in lux. 48 // Yb/Yw is taken to be midgray, ~20% relative luminance (XYZ Y 18.4, CIELAB L* 50). 49 // Therefore La = (Ew / pi) * .184 50 // La = 200 / pi * .184 51 // Image surround to 10 degrees = ~20% relative luminance = CIELAB L* 50 52 // 53 // Not from sRGB standard: 54 // Surround = average, 2.0. 55 // Discounting illuminant = false, doesn't occur for self-luminous displays 56 public static final Frame DEFAULT = 57 Frame.make( 58 CamUtils.WHITE_POINT_D65, 59 (float) (200.0f / Math.PI * CamUtils.yFromLstar(50.0f) / 100.f), 50.0f, 2.0f, 60 false); 61 62 private final float mAw; 63 private final float mNbb; 64 private final float mNcb; 65 private final float mC; 66 private final float mNc; 67 private final float mN; 68 private final float[] mRgbD; 69 private final float mFl; 70 private final float mFlRoot; 71 private final float mZ; 72 73 @VisibleForTesting getAw()74 public float getAw() { 75 return mAw; 76 } 77 78 @VisibleForTesting getN()79 public float getN() { 80 return mN; 81 } 82 83 @VisibleForTesting getNbb()84 public float getNbb() { 85 return mNbb; 86 } 87 getNcb()88 float getNcb() { 89 return mNcb; 90 } 91 getC()92 float getC() { 93 return mC; 94 } 95 getNc()96 float getNc() { 97 return mNc; 98 } 99 100 @VisibleForTesting 101 @NonNull getRgbD()102 public float[] getRgbD() { 103 return mRgbD; 104 } 105 getFl()106 float getFl() { 107 return mFl; 108 } 109 110 @VisibleForTesting 111 @NonNull getFlRoot()112 public float getFlRoot() { 113 return mFlRoot; 114 } 115 getZ()116 float getZ() { 117 return mZ; 118 } 119 Frame(float n, float aw, float nbb, float ncb, float c, float nc, float[] rgbD, float fl, float fLRoot, float z)120 private Frame(float n, float aw, float nbb, float ncb, float c, float nc, float[] rgbD, 121 float fl, float fLRoot, float z) { 122 mN = n; 123 mAw = aw; 124 mNbb = nbb; 125 mNcb = ncb; 126 mC = c; 127 mNc = nc; 128 mRgbD = rgbD; 129 mFl = fl; 130 mFlRoot = fLRoot; 131 mZ = z; 132 } 133 134 /** Create a custom frame. */ 135 @NonNull make(@onNull float[] whitepoint, float adaptingLuminance, float backgroundLstar, float surround, boolean discountingIlluminant)136 public static Frame make(@NonNull float[] whitepoint, float adaptingLuminance, 137 float backgroundLstar, float surround, boolean discountingIlluminant) { 138 // Transform white point XYZ to 'cone'/'rgb' responses 139 float[][] matrix = CamUtils.XYZ_TO_CAM16RGB; 140 float[] xyz = whitepoint; 141 float rW = (xyz[0] * matrix[0][0]) + (xyz[1] * matrix[0][1]) + (xyz[2] * matrix[0][2]); 142 float gW = (xyz[0] * matrix[1][0]) + (xyz[1] * matrix[1][1]) + (xyz[2] * matrix[1][2]); 143 float bW = (xyz[0] * matrix[2][0]) + (xyz[1] * matrix[2][1]) + (xyz[2] * matrix[2][2]); 144 145 // Scale input surround, domain (0, 2), to CAM16 surround, domain (0.8, 1.0) 146 float f = 0.8f + (surround / 10.0f); 147 // "Exponential non-linearity" 148 float c = (f >= 0.9) ? MathUtils.lerp(0.59f, 0.69f, ((f - 0.9f) * 10.0f)) : MathUtils.lerp( 149 0.525f, 0.59f, ((f - 0.8f) * 10.0f)); 150 // Calculate degree of adaptation to illuminant 151 float d = discountingIlluminant ? 1.0f : f * (1.0f - ((1.0f / 3.6f) * (float) Math.exp( 152 (-adaptingLuminance - 42.0f) / 92.0f))); 153 // Per Li et al, if D is greater than 1 or less than 0, set it to 1 or 0. 154 d = (d > 1.0) ? 1.0f : (d < 0.0) ? 0.0f : d; 155 // Chromatic induction factor 156 float nc = f; 157 158 // Cone responses to the whitepoint, adjusted for illuminant discounting. 159 // 160 // Why use 100.0 instead of the white point's relative luminance? 161 // 162 // Some papers and implementations, for both CAM02 and CAM16, use the Y 163 // value of the reference white instead of 100. Fairchild's Color Appearance 164 // Models (3rd edition) notes that this is in error: it was included in the 165 // CIE 2004a report on CIECAM02, but, later parts of the conversion process 166 // account for scaling of appearance relative to the white point relative 167 // luminance. This part should simply use 100 as luminance. 168 float[] rgbD = new float[]{d * (100.0f / rW) + 1.0f - d, d * (100.0f / gW) + 1.0f - d, 169 d * (100.0f / bW) + 1.0f - d, }; 170 // Luminance-level adaptation factor 171 float k = 1.0f / (5.0f * adaptingLuminance + 1.0f); 172 float k4 = k * k * k * k; 173 float k4F = 1.0f - k4; 174 float fl = (k4 * adaptingLuminance) + (0.1f * k4F * k4F * (float) Math.cbrt( 175 5.0 * adaptingLuminance)); 176 177 // Intermediate factor, ratio of background relative luminance to white relative luminance 178 float n = (float) CamUtils.yFromLstar(backgroundLstar) / whitepoint[1]; 179 180 // Base exponential nonlinearity 181 // note Schlomer 2018 has a typo and uses 1.58, the correct factor is 1.48 182 float z = 1.48f + (float) Math.sqrt(n); 183 184 // Luminance-level induction factors 185 float nbb = 0.725f / (float) Math.pow(n, 0.2); 186 float ncb = nbb; 187 188 // Discounted cone responses to the white point, adjusted for post-chromatic 189 // adaptation perceptual nonlinearities. 190 float[] rgbAFactors = new float[]{(float) Math.pow(fl * rgbD[0] * rW / 100.0, 0.42), 191 (float) Math.pow(fl * rgbD[1] * gW / 100.0, 0.42), (float) Math.pow( 192 fl * rgbD[2] * bW / 100.0, 0.42)}; 193 194 float[] rgbA = new float[]{(400.0f * rgbAFactors[0]) / (rgbAFactors[0] + 27.13f), 195 (400.0f * rgbAFactors[1]) / (rgbAFactors[1] + 27.13f), 196 (400.0f * rgbAFactors[2]) / (rgbAFactors[2] + 27.13f), }; 197 198 float aw = ((2.0f * rgbA[0]) + rgbA[1] + (0.05f * rgbA[2])) * nbb; 199 200 return new Frame(n, aw, nbb, ncb, c, nc, rgbD, fl, (float) Math.pow(fl, 0.25), z); 201 } 202 } 203