|
| 1 | +/* Copyright (C) 2003-2013 Runtime Revolution Ltd. |
| 2 | + |
| 3 | + This file is part of LiveCode. |
| 4 | + |
| 5 | + LiveCode is free software; you can redistribute it and/or modify it under |
| 6 | + the terms of the GNU General Public License v3 as published by the Free |
| 7 | + Software Foundation. |
| 8 | + |
| 9 | + LiveCode is distributed in the hope that it will be useful, but WITHOUT ANY |
| 10 | + WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 11 | + FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 12 | + for more details. |
| 13 | + |
| 14 | + You should have received a copy of the GNU General Public License |
| 15 | + along with LiveCode. If not see <http://www.gnu.org/licenses/>. */ |
| 16 | + |
| 17 | +// IM-2014-09-24: [[ Bug 13208 ]] Shared code to convert from Linear RGB color space to CIE 1931 XYZ |
| 18 | + |
| 19 | +#include "core.h" |
| 20 | +#include "graphics.h" |
| 21 | +#include "color.h" |
| 22 | + |
| 23 | +//////////////////////////////////////////////////////////////////////////////// |
| 24 | + |
| 25 | +MCColorVector3 MCColorVector3ScalarMultiply(const MCColorVector3 &p_vector, MCGFloat p_scalar) |
| 26 | +{ |
| 27 | + return MCColorVector3Make(p_vector.x * p_scalar, p_vector.y * p_scalar, p_vector.z * p_scalar); |
| 28 | +} |
| 29 | + |
| 30 | +void MCColorMatrix3x3Set(MCColorMatrix3x3 &x_matrix, |
| 31 | + MCGFloat a, MCGFloat b, MCGFloat c, |
| 32 | + MCGFloat d, MCGFloat e, MCGFloat f, |
| 33 | + MCGFloat g, MCGFloat h, MCGFloat i) |
| 34 | +{ |
| 35 | + x_matrix.m[0][0] = a; |
| 36 | + x_matrix.m[1][0] = b; |
| 37 | + x_matrix.m[2][0] = c; |
| 38 | + x_matrix.m[0][1] = d; |
| 39 | + x_matrix.m[1][1] = e; |
| 40 | + x_matrix.m[2][1] = f; |
| 41 | + x_matrix.m[0][2] = g; |
| 42 | + x_matrix.m[1][2] = h; |
| 43 | + x_matrix.m[2][2] = i; |
| 44 | +} |
| 45 | + |
| 46 | +void MCColorMatrix3x3Copy(const MCColorMatrix3x3 &p_matrix, MCColorMatrix3x3 &r_copy) |
| 47 | +{ |
| 48 | + MCMemoryCopy(&r_copy, &p_matrix, sizeof(MCColorMatrix3x3)); |
| 49 | +} |
| 50 | + |
| 51 | +bool MCColorMatrix3x3Inverse(const MCColorMatrix3x3 &p_matrix, MCColorMatrix3x3 &r_inverse) |
| 52 | +{ |
| 53 | + MCGFloat a, b, c; |
| 54 | + a = p_matrix.m[1][1] * p_matrix.m[2][2] - p_matrix.m[1][2] * p_matrix.m[2][1]; |
| 55 | + b = p_matrix.m[1][2] * p_matrix.m[2][0] - p_matrix.m[1][0] * p_matrix.m[2][2]; |
| 56 | + c = p_matrix.m[1][0] * p_matrix.m[2][1] - p_matrix.m[1][1] * p_matrix.m[2][0]; |
| 57 | + |
| 58 | + MCGFloat t_det; |
| 59 | + t_det = p_matrix.m[0][0] * a + p_matrix.m[0][1] * b + p_matrix.m[0][2] * c; |
| 60 | + |
| 61 | + if (t_det == 0.0) |
| 62 | + return false; |
| 63 | + |
| 64 | + MCGFloat t_inv_det; |
| 65 | + t_inv_det = 1.0 / t_det; |
| 66 | + |
| 67 | + r_inverse.m[0][0] = t_inv_det * a; |
| 68 | + r_inverse.m[0][1] = t_inv_det * (p_matrix.m[2][1] * p_matrix.m[0][2] - p_matrix.m[2][2] * p_matrix.m[0][1]); |
| 69 | + r_inverse.m[0][2] = t_inv_det * (p_matrix.m[0][1] * p_matrix.m[1][2] - p_matrix.m[0][2] * p_matrix.m[1][1]); |
| 70 | + r_inverse.m[1][0] = t_inv_det * b; |
| 71 | + r_inverse.m[1][1] = t_inv_det * (p_matrix.m[2][2] * p_matrix.m[0][0] - p_matrix.m[2][0] * p_matrix.m[0][2]); |
| 72 | + r_inverse.m[1][2] = t_inv_det * (p_matrix.m[0][2] * p_matrix.m[1][0] - p_matrix.m[0][0] * p_matrix.m[1][2]); |
| 73 | + r_inverse.m[2][0] = t_inv_det * c; |
| 74 | + r_inverse.m[2][1] = t_inv_det * (p_matrix.m[2][0] * p_matrix.m[0][1] - p_matrix.m[2][1] * p_matrix.m[0][0]); |
| 75 | + r_inverse.m[2][2] = t_inv_det * (p_matrix.m[0][0] * p_matrix.m[1][1] - p_matrix.m[0][1] * p_matrix.m[1][0]); |
| 76 | + |
| 77 | + return true; |
| 78 | +} |
| 79 | + |
| 80 | +MCColorVector3 MCColorMatrix3x3MultiplyVector(const MCColorMatrix3x3 &p_matrix, const MCColorVector3 &p_vector) |
| 81 | +{ |
| 82 | + return MCColorVector3Make(p_matrix.m[0][0] * p_vector.x + p_matrix.m[0][1] * p_vector.y + p_matrix.m[0][2] * p_vector.z, |
| 83 | + p_matrix.m[1][0] * p_vector.x + p_matrix.m[1][1] * p_vector.y + p_matrix.m[1][2] * p_vector.z, |
| 84 | + p_matrix.m[2][0] * p_vector.x + p_matrix.m[2][1] * p_vector.y + p_matrix.m[2][2] * p_vector.z); |
| 85 | +} |
| 86 | + |
| 87 | +MCColorVector3 xy_to_xyz(const MCColorVector2 &p_xy) |
| 88 | +{ |
| 89 | + return MCColorVector3Make(p_xy.x, p_xy.y, 1.0 - (p_xy.x + p_xy.y)); |
| 90 | +} |
| 91 | + |
| 92 | +bool MCColorTransformLinearRGBToXYZ(const MCColorVector2 &p_white, const MCColorVector2 &p_red, const MCColorVector2 &p_green, const MCColorVector2 &p_blue, |
| 93 | + MCColorVector3 &r_white, MCColorMatrix3x3 &r_matrix) |
| 94 | +{ |
| 95 | + MCColorVector3 t_r, t_g, t_b, t_w; |
| 96 | + t_r = xy_to_xyz(p_red); |
| 97 | + t_g = xy_to_xyz(p_green); |
| 98 | + t_b = xy_to_xyz(p_blue); |
| 99 | + |
| 100 | + t_w = xy_to_xyz(p_white); |
| 101 | + t_w = MCColorVector3ScalarMultiply(t_w, 1.0 / t_w.y); |
| 102 | + |
| 103 | + MCColorMatrix3x3 t_matrix; |
| 104 | + MCColorMatrix3x3Set(t_matrix, |
| 105 | + t_r.x, t_r.y, t_r.z, |
| 106 | + t_g.x, t_g.y, t_g.z, |
| 107 | + t_b.x, t_b.y, t_b.z); |
| 108 | + |
| 109 | + MCColorMatrix3x3 t_inverse; |
| 110 | + if (!MCColorMatrix3x3Inverse(t_matrix, t_inverse)) |
| 111 | + return false; |
| 112 | + |
| 113 | + MCColorVector3 t_scale; |
| 114 | + t_scale = MCColorMatrix3x3MultiplyVector(t_inverse, t_w); |
| 115 | + |
| 116 | + t_matrix.m[0][0] *= t_scale.x; |
| 117 | + t_matrix.m[1][0] *= t_scale.x; |
| 118 | + t_matrix.m[2][0] *= t_scale.x; |
| 119 | + t_matrix.m[0][1] *= t_scale.y; |
| 120 | + t_matrix.m[1][1] *= t_scale.y; |
| 121 | + t_matrix.m[2][1] *= t_scale.y; |
| 122 | + t_matrix.m[0][2] *= t_scale.z; |
| 123 | + t_matrix.m[1][2] *= t_scale.z; |
| 124 | + t_matrix.m[2][2] *= t_scale.z; |
| 125 | + |
| 126 | +// MCColorMatrix3x3Copy(t_matrix, r_matrix); |
| 127 | + r_matrix = t_matrix; |
| 128 | + r_white = t_w; |
| 129 | + |
| 130 | + return true; |
| 131 | +} |
| 132 | + |
| 133 | +//////////////////////////////////////////////////////////////////////////////// |
0 commit comments