Debian Codegraph @

rust/icc/FFI.cpp

raw
1 /*
2 * Copyright 2025 Google LLC.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8 #include "rust/icc/FFI.h"
9 #include "rust/icc/FFI.rs.h" // Generated by CXX bridge
10 #include "modules/skcms/skcms.h"
11
12 #include <algorithm>
13 #include <cstddef>
14 #include <cstring>
15 #include <type_traits>
16
17 namespace rust_icc {
18
19 bool ApproximateCurveWrapper(rust::Slice<const uint16_t> table,
20 TransferFunction& out_approx,
21 float& out_max_error) {
22 if (table.empty()) {
23 return false;
24 }
25
26 // Construct skcms_Curve for the table
27 skcms_Curve curve;
28 memset(&curve, 0, sizeof(skcms_Curve));
29 curve.table_entries = static_cast<uint32_t>(table.size());
30 curve.table_8 = nullptr;
31 curve.table_16 = reinterpret_cast<const uint8_t*>(table.data());
32
33 // Call skcms_ApproximateCurve (signature verified at compile time)
34 skcms_TransferFunction skcms_approx;
35 float max_error = 0.0f;
36 bool success = skcms_ApproximateCurve(&curve, &skcms_approx, &max_error);
37
38 if (!success) {
39 return false;
40 }
41
42 // Copy result field-by-field to avoid memcpy of non-trivial type
43 out_approx.g = skcms_approx.g;
44 out_approx.a = skcms_approx.a;
45 out_approx.b = skcms_approx.b;
46 out_approx.c = skcms_approx.c;
47 out_approx.d = skcms_approx.d;
48 out_approx.e = skcms_approx.e;
49 out_approx.f = skcms_approx.f;
50 out_max_error = max_error;
51
52 return true;
53 }
54
55 void ToSkcmsMatrix3x3(const Matrix3x3& rust_matrix, skcms_Matrix3x3* out_skcms) {
56 // Note: std::is_layout_compatible_v (C++20) is not yet implemented in LLVM (P0466R5).
57 // Use sizeof/alignof + standard_layout + trivially_copyable as the next best alternative.
58 static_assert(sizeof(Matrix3x3) == sizeof(skcms_Matrix3x3),
59 "Matrix3x3 must have same size as skcms_Matrix3x3 for memcpy");
60 static_assert(alignof(Matrix3x3) == alignof(skcms_Matrix3x3),
61 "Matrix3x3 must have same alignment as skcms_Matrix3x3 for memcpy");
62 static_assert(std::is_standard_layout_v<Matrix3x3>,
63 "Matrix3x3 must have standard layout for safe memcpy");
64 static_assert(std::is_trivially_copyable_v<Matrix3x3>,
65 "Matrix3x3 must be trivially copyable for memcpy");
66 memcpy(out_skcms, &rust_matrix, sizeof(skcms_Matrix3x3));
67 }
68
69 void ToSkcmsTransferFunction(const TransferFunction& rust_tf,
70 skcms_TransferFunction* out_skcms) {
71 static_assert(sizeof(TransferFunction) == sizeof(skcms_TransferFunction),
72 "TransferFunction must have same size as skcms_TransferFunction for memcpy");
73 static_assert(alignof(TransferFunction) == alignof(skcms_TransferFunction),
74 "TransferFunction must have same alignment as skcms_TransferFunction for memcpy");
75 static_assert(std::is_standard_layout_v<TransferFunction>,
76 "TransferFunction must have standard layout for safe memcpy");
77 static_assert(std::is_trivially_copyable_v<TransferFunction>,
78 "TransferFunction must be trivially copyable for memcpy");
79 // Spot-check field offsets in skcms_TransferFunction to verify the unusual ordering (g, a, b, c, d, e, f)
80 static_assert(offsetof(skcms_TransferFunction, g) == 0,
81 "skcms_TransferFunction::g must be at offset 0");
82 static_assert(offsetof(skcms_TransferFunction, a) == sizeof(float),
83 "skcms_TransferFunction::a must be at offset sizeof(float)");
84 memcpy(out_skcms, &rust_tf, sizeof(skcms_TransferFunction));
85 }
86
87 static void ToSkcmsCurve(const rust_icc::Curve& rust_curve, skcms_Curve* out_skcms) {
88 // Note: Curve table data (table_16) is borrowed from Rust Vec via .data().
89 // See ToSkcmsIccProfile documentation about lifetime requirements.
90 if (rust_curve.table_entries == 0) {
91 out_skcms->table_entries = 0;
92 ToSkcmsTransferFunction(rust_curve.parametric, &out_skcms->parametric);
93 } else {
94 out_skcms->table_entries = rust_curve.table_entries;
95 out_skcms->table_8 = nullptr;
96 out_skcms->table_16 = rust_curve.table_data.data();
97 }
98 }
99
100 static bool ToSkcmsA2B(const rust_icc::A2B& rust_a2b, skcms_A2B* out_skcms) {
101 memset(out_skcms, 0, sizeof(skcms_A2B));
102
103 // Input curves: If input_channels is non-zero, ensure we have enough curves
104 out_skcms->input_channels = rust_a2b.input_channels;
105 if (rust_a2b.input_channels > 0) {
106 if (rust_a2b.input_channels < 1 || rust_a2b.input_channels > 4) {
107 return false;
108 }
109 // Only validate curve count if input_channels is specified
110 if (!rust_a2b.input_curves.empty() && rust_a2b.input_channels > rust_a2b.input_curves.size()) {
111 return false;
112 }
113 for (size_t i = 0; i < rust_a2b.input_curves.size() && i < 4; i++) {
114 ToSkcmsCurve(rust_a2b.input_curves[i], &out_skcms->input_curves[i]);
115 }
116 }
117
118 // Grid data
119 static_assert(std::is_same_v<decltype(out_skcms->grid_points), uint8_t[4]>,
120 "skcms_A2B::grid_points must be uint8_t[4]");
121 static_assert(std::is_trivially_copyable_v<uint8_t>,
122 "uint8_t must be trivially copyable for memcpy");
123 if (rust_a2b.grid_points.size() != 4) {
124 return false;
125 }
126 memcpy(out_skcms->grid_points, rust_a2b.grid_points.data(), 4);
127 if (rust_a2b.is_16bit_grid) {
128 out_skcms->grid_16 = rust_a2b.grid_data.data();
129 } else {
130 out_skcms->grid_8 = rust_a2b.grid_data.data();
131 }
132
133 // Matrix curves and matrix
134 // If matrix_channels is explicitly set, use it. Otherwise, infer from matrix_curves presence.
135 uint32_t effective_matrix_channels = rust_a2b.matrix_channels;
136 if (effective_matrix_channels == 0 && !rust_a2b.matrix_curves.empty()) {
137 // Infer matrix_channels from the number of matrix curves provided
138 effective_matrix_channels = std::min(static_cast<uint32_t>(rust_a2b.matrix_curves.size()), 3u);
139 }
140
141 if (effective_matrix_channels > rust_a2b.matrix_curves.size()) {
142 return false;
143 }
144 if (effective_matrix_channels > 3) {
145 return false;
146 }
147 out_skcms->matrix_channels = effective_matrix_channels;
148 for (size_t i = 0; i < rust_a2b.matrix_curves.size() && i < 3; i++) {
149 ToSkcmsCurve(rust_a2b.matrix_curves[i], &out_skcms->matrix_curves[i]);
150 }
151
152 // Copy 3x3 matrix portion (only if we have matrix curves)
153 if (effective_matrix_channels > 0) {
154 skcms_Matrix3x3 temp_matrix;
155 ToSkcmsMatrix3x3(rust_a2b.matrix, &temp_matrix);
156 for (int row = 0; row < 3; row++) {
157 for (int col = 0; col < 3; col++) {
158 out_skcms->matrix.vals[row][col] = temp_matrix.vals[row][col];
159 }
160 // 4th column is the bias/offset
161 out_skcms->matrix.vals[row][3] = rust_a2b.matrix_bias[row];
162 }
163 }
164
165 // Output curves: If output_channels is non-zero, ensure we have enough curves
166 out_skcms->output_channels = rust_a2b.output_channels;
167 if (rust_a2b.output_channels > 0) {
168 if (rust_a2b.output_channels > 4) {
169 return false;
170 }
171 // Only validate curve count if output_channels is specified
172 if (!rust_a2b.output_curves.empty() && rust_a2b.output_channels > rust_a2b.output_curves.size()) {
173 return false;
174 }
175 for (size_t i = 0; i < rust_a2b.output_curves.size() && i < 4; i++) {
176 ToSkcmsCurve(rust_a2b.output_curves[i], &out_skcms->output_curves[i]);
177 }
178 }
179 return true;
180 }
181
182 // Helper to populate skcms_B2A from Rust B2A data
183 static bool ToSkcmsB2A(const rust_icc::B2A& rust_b2a, skcms_B2A* out_skcms) {
184 memset(out_skcms, 0, sizeof(skcms_B2A));
185
186 // Input curves
187 if (rust_b2a.input_channels != 3) {
188 return false;
189 }
190 if (rust_b2a.input_curves.size() < 3) {
191 return false;
192 }
193 out_skcms->input_channels = rust_b2a.input_channels;
194 for (size_t i = 0; i < 3; i++) {
195 ToSkcmsCurve(rust_b2a.input_curves[i], &out_skcms->input_curves[i]);
196 }
197
198 // Matrix and matrix curves
199 // If matrix_channels is explicitly set, use it. Otherwise, infer from matrix_curves presence.
200 uint32_t effective_matrix_channels = rust_b2a.matrix_channels;
201 if (effective_matrix_channels == 0 && !rust_b2a.matrix_curves.empty()) {
202 // Infer matrix_channels from the number of matrix curves provided
203 effective_matrix_channels = std::min(static_cast<uint32_t>(rust_b2a.matrix_curves.size()), 3u);
204 }
205
206 if (effective_matrix_channels > rust_b2a.matrix_curves.size()) {
207 return false;
208 }
209 if (effective_matrix_channels > 3) {
210 return false;
211 }
212 out_skcms->matrix_channels = effective_matrix_channels;
213
214 // Copy matrix and matrix curves only if matrix stage is active
215 if (effective_matrix_channels > 0) {
216 skcms_Matrix3x3 temp_matrix;
217 ToSkcmsMatrix3x3(rust_b2a.matrix, &temp_matrix);
218 for (int row = 0; row < 3; row++) {
219 for (int col = 0; col < 3; col++) {
220 out_skcms->matrix.vals[row][col] = temp_matrix.vals[row][col];
221 }
222 out_skcms->matrix.vals[row][3] = rust_b2a.matrix_bias[row];
223 }
224
225 for (size_t i = 0; i < rust_b2a.matrix_curves.size() && i < 3; i++) {
226 ToSkcmsCurve(rust_b2a.matrix_curves[i], &out_skcms->matrix_curves[i]);
227 }
228 }
229
230 // Grid data
231 static_assert(std::is_same_v<decltype(out_skcms->grid_points), uint8_t[4]>,
232 "skcms_B2A::grid_points must be uint8_t[4]");
233 static_assert(std::is_trivially_copyable_v<uint8_t>,
234 "uint8_t must be trivially copyable for memcpy");
235 if (rust_b2a.grid_points.size() != 4) {
236 return false;
237 }
238 memcpy(out_skcms->grid_points, rust_b2a.grid_points.data(), 4);
239 if (rust_b2a.is_16bit_grid) {
240 out_skcms->grid_16 = rust_b2a.grid_data.data();
241 } else {
242 out_skcms->grid_8 = rust_b2a.grid_data.data();
243 }
244
245 // Output curves
246 if (rust_b2a.output_channels < 1 || rust_b2a.output_channels > 4) {
247 return false;
248 }
249 if (rust_b2a.output_channels > rust_b2a.output_curves.size()) {
250 return false;
251 }
252 out_skcms->output_channels = rust_b2a.output_channels;
253 for (size_t i = 0; i < rust_b2a.output_channels; i++) {
254 ToSkcmsCurve(rust_b2a.output_curves[i], &out_skcms->output_curves[i]);
255 }
256 return true;
257 }
258
259 bool ToSkcmsIccProfile(const IccProfile& rust_profile, skcms_ICCProfile* out_skcms) {
260 memset(out_skcms, 0, sizeof(skcms_ICCProfile));
261
262 // Copy color space information
263 out_skcms->data_color_space = static_cast<uint32_t>(rust_profile.data_color_space);
264 out_skcms->pcs = static_cast<uint32_t>(rust_profile.connection_space);
265
266 // Copy toXYZD50 matrix if present
267 out_skcms->has_toXYZD50 = rust_profile.has_to_xyzd50;
268 if (rust_profile.has_to_xyzd50) {
269 ToSkcmsMatrix3x3(rust_profile.to_xyzd50, &out_skcms->toXYZD50);
270 }
271
272 // Copy transfer curves if present
273 out_skcms->has_trc = rust_profile.has_trc;
274 if (rust_profile.has_trc) {
275 // Convert each channel's transfer function
276 for (int i = 0; i < 3; i++) {
277 // Set up the skcms_Curve as a parametric function (not a LUT)
278 out_skcms->trc[i].table_entries = 0; // 0 = parametric, not table
279 ToSkcmsTransferFunction(rust_profile.trc[i], &out_skcms->trc[i].parametric);
280 }
281 }
282
283 // Copy CICP data if present
284 out_skcms->has_CICP = rust_profile.has_cicp;
285 if (rust_profile.has_cicp) {
286 out_skcms->CICP.color_primaries = rust_profile.cicp.color_primaries;
287 out_skcms->CICP.transfer_characteristics = rust_profile.cicp.transfer_characteristics;
288 out_skcms->CICP.matrix_coefficients = rust_profile.cicp.matrix_coefficients;
289 out_skcms->CICP.video_full_range_flag = rust_profile.cicp.video_full_range_flag;
290 }
291
292 // Populate A2B and B2A transforms if present
293 out_skcms->has_A2B = rust_profile.has_a2b;
294 if (rust_profile.has_a2b) {
295 if (!ToSkcmsA2B(rust_profile.a2b, &out_skcms->A2B)) {
296 return false;
297 }
298 }
299
300 out_skcms->has_B2A = rust_profile.has_b2a;
301 if (rust_profile.has_b2a) {
302 if (!ToSkcmsB2A(rust_profile.b2a, &out_skcms->B2A)) {
303 return false;
304 }
305 }
306
307 // A profile needs either a matrix, transfer curves, or A2B/B2A to be useful
308 if (!out_skcms->has_toXYZD50 && !out_skcms->has_trc &&
309 !out_skcms->has_A2B && !out_skcms->has_B2A) {
310 return false;
311 }
312
313 return true;
314 }
315
316 } // namespace rust_icc