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