blob: ec4cf84987e840a6c608d4706273113bd59fcf7a [file]
/*
* Copyright 2013 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
// TODO(b/129481165): remove the #pragma below and fix conversion issues
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wconversion"
#include "Daltonizer.h"
#include <array>
#include <math/mat3.h>
#include <math/mat4.h>
namespace android {
namespace {
//
// Parameters for simulating color vision changes.
// Adapted from the color correction logic used by Chrome OS in chromium/src:
// ash/color_enhancement/color_enhancement_controller.cc
// These values were generated by doing a polynomial regression on tabulated
// values from the initial source:
// http://www.inf.ufrgs.br/~oliveira/pubs_files/CVD_Simulation/CVD_Simulation.html
// Original Research Paper:
// http://www.inf.ufrgs.br/~oliveira/pubs_files/CVD_Simulation/Machado_Oliveira_Fernandes_CVD_Vis2009_final.pdf
//
// The first index is ColorBlindnessType enum - 1, so this must be kept in
// that order.
constexpr std::array<std::array<float, 3>, 27> kSimulationParams = {{
// ColorBlindnessType::kProtanomaly:
{{0.4720, -1.2946, 0.9857}},
{{-0.6128, 1.6326, 0.0187}},
{{0.1407, -0.3380, -0.0044}},
{{-0.1420, 0.2488, 0.0044}},
{{0.1872, -0.3908, 0.9942}},
{{-0.0451, 0.1420, 0.0013}},
{{0.0222, -0.0253, -0.0004}},
{{-0.0290, -0.0201, 0.0006}},
{{0.0068, 0.0454, 0.9990}},
// ColorBlindnessType::kDeuteranomaly:
{{0.5442, -1.1454, 0.9818}},
{{-0.7091, 1.5287, 0.0238}},
{{0.1650, -0.3833, -0.0055}},
{{-0.1664, 0.4368, 0.0056}},
{{0.2178, -0.5327, 0.9927}},
{{-0.0514, 0.0958, 0.0017}},
{{0.0180, -0.0288, -0.0006}},
{{-0.0232, -0.0649, 0.0007}},
{{0.0052, 0.0360, 0.9998}},
// ColorBlindnessType::kTritanomaly:
{{0.4275, -0.0181, 0.9307}},
{{-0.2454, 0.0013, 0.0827}},
{{-0.1821, 0.0168, -0.0134}},
{{-0.1280, 0.0047, 0.0202}},
{{0.0233, -0.0398, 0.9728}},
{{0.1048, 0.0352, 0.0070}},
{{-0.0156, 0.0061, 0.0071}},
{{0.3841, 0.2947, 0.0151}},
{{-0.3685, -0.3008, 0.9778}}}};
// Returns a 3x3 matrix for simulating the given type of CVD with the given
// severity.
// Calculation adapted from GetCvdSimulationMatrix_ in chromium/src's
// ash/color_enhancement/color_enhancement_controller.cc.
void GetCvdSimulationMatrix(ColorBlindnessType type, float severity, mat3& result) {
float severity_squared = severity * severity;
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
int type_start_row = (static_cast<int>(type) - 1) * 9;
int param_row = i * 3 + j;
// mat3 is in column-major order. Transpose i and j when setting.
result[j][i] = kSimulationParams[type_start_row + param_row][0] * severity_squared +
kSimulationParams[type_start_row + param_row][1] * severity +
kSimulationParams[type_start_row + param_row][2];
}
}
}
} // namespace
void Daltonizer::setType(ColorBlindnessType type) {
if (type != mType) {
mDirty = true;
mType = type;
}
}
void Daltonizer::setMode(ColorBlindnessMode mode) {
if (mode != mMode) {
mDirty = true;
mMode = mode;
}
}
void Daltonizer::setLevel(int32_t level) {
if (level < 0 || level > 10) {
return;
}
float newLevel = level / 10.0f;
if (std::fabs(mLevel - newLevel) > 0.09f) {
mDirty = true;
}
mLevel = newLevel;
}
void Daltonizer::setUseUpdatedAlgorithm(bool useUpdatedAlgorithm) {
if (useUpdatedAlgorithm != mUseUpdatedAlgorithm) {
mUseUpdatedAlgorithm = useUpdatedAlgorithm;
mDirty = true;
}
}
const mat4& Daltonizer::operator()() {
if (mDirty) {
mDirty = false;
update();
}
return mColorTransform;
}
void Daltonizer::update() {
if (mType == ColorBlindnessType::None) {
mColorTransform = mat4();
return;
}
if (mUseUpdatedAlgorithm) {
updateColorTransformNew();
} else {
updateColorTransform();
}
}
void Daltonizer::updateColorTransform() {
// converts a linear RGB color to the XYZ space
const mat4 rgb2xyz( 0.4124, 0.2126, 0.0193, 0,
0.3576, 0.7152, 0.1192, 0,
0.1805, 0.0722, 0.9505, 0,
0 , 0 , 0 , 1);
// converts a XYZ color to the LMS space.
const mat4 xyz2lms( 0.7328,-0.7036, 0.0030, 0,
0.4296, 1.6975, 0.0136, 0,
-0.1624, 0.0061, 0.9834, 0,
0 , 0 , 0 , 1);
// Direct conversion from linear RGB to LMS
const mat4 rgb2lms(xyz2lms*rgb2xyz);
// And back from LMS to linear RGB
const mat4 lms2rgb(inverse(rgb2lms));
// To simulate color blindness we need to "remove" the data lost by the absence of
// a cone. This cannot be done by just zeroing out the corresponding LMS component
// because it would create a color outside of the RGB gammut.
// Instead we project the color along the axis of the missing component onto a plane
// within the RGB gammut:
// - since the projection happens along the axis of the missing component, a
// color blind viewer perceives the projected color the same.
// - We use the plane defined by 3 points in LMS space: black, white and
// blue and red for protanopia/deuteranopia and tritanopia respectively.
// LMS space red
const vec3& lms_r(rgb2lms[0].rgb);
// LMS space blue
const vec3& lms_b(rgb2lms[2].rgb);
// LMS space white
const vec3 lms_w((rgb2lms * vec4(1)).rgb);
// To find the planes we solve the a*L + b*M + c*S = 0 equation for the LMS values
// of the three known points. This equation is trivially solved, and has for
// solution the following cross-products:
const vec3 p0 = cross(lms_w, lms_b); // protanopia/deuteranopia
const vec3 p1 = cross(lms_w, lms_r); // tritanopia
// The following 3 matrices perform the projection of a LMS color onto the given plane
// along the selected axis
// projection for protanopia (L = 0)
const mat4 lms2lmsp( 0.0000, 0.0000, 0.0000, 0,
-p0.y / p0.x, 1.0000, 0.0000, 0,
-p0.z / p0.x, 0.0000, 1.0000, 0,
0 , 0 , 0 , 1);
// projection for deuteranopia (M = 0)
const mat4 lms2lmsd( 1.0000, -p0.x / p0.y, 0.0000, 0,
0.0000, 0.0000, 0.0000, 0,
0.0000, -p0.z / p0.y, 1.0000, 0,
0 , 0 , 0 , 1);
// projection for tritanopia (S = 0)
const mat4 lms2lmst( 1.0000, 0.0000, -p1.x / p1.z, 0,
0.0000, 1.0000, -p1.y / p1.z, 0,
0.0000, 0.0000, 0.0000, 0,
0 , 0 , 0 , 1);
// We will calculate the error between the color and the color viewed by
// a color blind user and "spread" this error onto the healthy cones.
// The matrices below perform this last step and have been chosen arbitrarily.
// Scale 0 represents no change (mColorTransform is identical matrix).
// error spread for protanopia
const mat4 errp(1.0, mLevel, mLevel, 0.0,
0.0, 1.0, 0.0, 0.0,
0.0, 0.0, 1.0, 0.0,
0.0, 0.0, 0.0, 1.0);
// error spread for deuteranopia
const mat4 errd( 1.0, 0.0, 0.0, 0.0,
mLevel, 1.0, mLevel, 0.0,
0.0, 0.0, 1.0, 0.0,
0.0, 0.0, 0.0, 1.0);
// error spread for tritanopia
const mat4 errt( 1.0, 0.0, 0.0, 0.0,
0.0, 1.0, 0.0, 0.0,
mLevel, mLevel, 1.0, 0.0,
0.0, 0.0, 0.0, 1.0);
// And the magic happens here...
// We construct the matrix that will perform the whole correction.
// simulation: type of color blindness to simulate:
// set to either lms2lmsp, lms2lmsd, lms2lmst
mat4 simulation;
// correction: type of color blindness correction (should match the simulation above):
// set to identity, errp, errd, errt ([0] for simulation only)
mat4 correction(0);
switch (mType) {
case ColorBlindnessType::Protanomaly:
simulation = lms2lmsp;
if (mMode == ColorBlindnessMode::Correction)
correction = errp;
break;
case ColorBlindnessType::Deuteranomaly:
simulation = lms2lmsd;
if (mMode == ColorBlindnessMode::Correction)
correction = errd;
break;
case ColorBlindnessType::Tritanomaly:
simulation = lms2lmst;
if (mMode == ColorBlindnessMode::Correction)
correction = errt;
break;
case ColorBlindnessType::None:
// We already caught this at the beginning of the method, but the
// compiler doesn't know that
break;
}
mColorTransform = lms2rgb *
(simulation * rgb2lms + correction * (rgb2lms - simulation * rgb2lms));
}
void Daltonizer::updateColorTransformNew() {
// In simulation mode, always use a level of 1.0.
float level = mMode == ColorBlindnessMode::Correction ? mLevel : 1.0;
if (level <= 0.0f) {
// Use exactly the identity for level 0. No need to calculate the simulation matrix.
mColorTransform = mat4();
return;
}
// Compute the matrix that could be used to simulate an image as seen by someone with
// this type of color vision deficiency.
mat3 simulation_matrix = mat3(0);
GetCvdSimulationMatrix(mType, level, simulation_matrix);
if (mMode == ColorBlindnessMode::Simulation) {
// We are only simulating, no need to do anything beyond getting the simulation matrix.
mColorTransform = mat4(simulation_matrix);
return;
}
// Now use the simulation to calculate a correction matrix. This process is
// called Daltonizing.
// "Daltonizing" an image consists of calculating the error, which is the
// original image minus the simulation and represents the information lost for
// the user, and linearly transforming that error to a color space the user
// can see, then adding it back onto the original image (Fidaner, Lin and
// Ozguven, 2006). The correction matrix is used to map the error between the
// initial image and the simulated image into a color space that can be seen
// by the user based on the type of color deficiency. So for example someone
// with Protanopia can see less of the red channel, so the correction matrix
// could be:
// | 0.0, 0.0, 0.0, |
// | 0.7, 1.0, 0.0, |
// | 0.7, 0.0, 1.0 |
// Multiplying this correction matrix by the error and adding it back to the
// original will have the effect of shifting more of image information lost to
// protanopes back into the image in a part of the spectrum they can see.
// Similarly, for Deuteranopia we correct on the green axis, and for
// Tritanopia we correct on the blue axis.
mat3 correction_matrix = mat3(0);
if (mMode == ColorBlindnessMode::Correction) {
switch (mType) {
// Note that mat3 is column-major, so the first column will multiply with
// the red channel, etc.
case ColorBlindnessType::Protanomaly:
// Correct on red axis: Shift colors in the red channel to the other
// channels.
correction_matrix = mat3(0.0, 0.7, 0.7,
0.0, 1.0, 0.0,
0.0, 0.0, 1.0);
break;
case ColorBlindnessType::Deuteranomaly:
// Correct on green axis: Shift colors in the green channel to the other
// channels.
correction_matrix = mat3(1.0, 0.0, 0.0,
0.7, 0.0, 0.7,
0.0, 0.0, 1.0);
break;
case ColorBlindnessType::Tritanomaly:
// Correct on blue axis: Shift colors in the blue channel into the other
// channels.
correction_matrix = mat3(1.0, 0.0, 0.0,
0.0, 1.0, 0.0,
0.7, 0.7, 0.0);
break;
case ColorBlindnessType::None:
// We already checked this at the beginning of the method, but the compiler doesn't
// know that.
return;
}
}
// For Daltonization of an image `original_img`, we would calculate the
// Daltonized version based on the `simulated_img` image and the
// `correction_matrix` as:
//
// result_img = original_img + correction_matrix x (original_img -
// simulated_img)
//
// We know that simulation_matrix x original_img = simulated_img, and can
// substitute:
//
// result_image = original_img + correction_matrix x (original_img -
// simulation_matrix x original_img)
//
// We can factor out `original_img` because matrix multiplication distributes:
//
// result_image = (Identity + correction_matrix x
// (Identity - simulation_matrix)) x original_img
//
// This method should return the matrix that multiplies by `original_img` to
// get the result, i.e.
//
// result_matrix = Identity + correction_matrix x (Identity -
// simulation_matrix)
//
// Distributing the `correction_matrix` gives us:
//
// result_matrix = Identity + correction_matrix - correction_matrix x
// simulation_matrix
//
// Compute this and return it as a 4x4 with a 1 in the bottom right.
const mat3 identity;
mColorTransform = mat4(identity + correction_matrix - correction_matrix * simulation_matrix);
}
} /* namespace android */
// TODO(b/129481165): remove the #pragma below and fix conversion issues
#pragma clang diagnostic pop // ignored "-Wconversion"