oxedyne/fe2o3/fe2o3_graphics/tests/icc.rs
25.5 KiB, 1 run
created by r1870400018:68837, which is this file's identity for as long as the history lasts, whatever it is later renamed to
download · who wrote it · its history
| 1 | //! The ICC reader, the matrix path and the built-in spaces, against LittleCMS. |
| 2 | //! |
| 3 | //! Every expected number below came from `transicc` (LittleCMS 2.17, lcms2-utils) run once over the |
| 4 | //! swatch set by `tools/icc/fixtures.sh`, which is how to make them again. LittleCMS shares nothing |
| 5 | //! with this reader, so agreement is agreement with an independent implementation. The numbers are |
| 6 | //! printed to four places, in percent for XYZ and in the 0..255 scale for grey. |
| 7 | //! |
| 8 | //! The Ghostscript profiles are AGPL and are read from their system path only, by tests marked |
| 9 | //! `#[ignore]`; none is copied into the tree. Run them with `--ignored`. FOGRA39L Coated is bundled |
| 10 | //! (CC0) and its tests always run. |
| 11 | //! |
| 12 | //! The profiles refused are built here, byte by byte, so that what is wrong with each is on the page. |
| 13 | //! |
| 14 | //! [Written with AI entirely](https://need2know.ai/entirely-ai/code)\ |
| 15 | //! Anthropic Claude |
| 16 | |
| 17 | use oxedyne_fe2o3_core::prelude::*; |
| 18 | use oxedyne_fe2o3_graphics::icc::{ |
| 19 | builtin, |
| 20 | matrix::{ |
| 21 | Grey, |
| 22 | RgbMatrix, |
| 23 | rgb_to_grey, |
| 24 | }, |
| 25 | read::{ |
| 26 | Class, |
| 27 | Curve, |
| 28 | Lut, |
| 29 | Profile, |
| 30 | Sig, |
| 31 | Space, |
| 32 | }, |
| 33 | }; |
| 34 | |
| 35 | use std::{ |
| 36 | fs, |
| 37 | path::PathBuf, |
| 38 | }; |
| 39 | |
| 40 | #[path = "icc/xform.rs"] |
| 41 | mod xform; |
| 42 | |
| 43 | const GS: &str = "/usr/share/color/icc/ghostscript"; |
| 44 | const COLORD: &str = "/usr/share/color/icc/colord"; |
| 45 | |
| 46 | const CUBE_GREY: &[f64] = &[ |
| 47 | 0.0000, |
| 48 | 10.2685, |
| 49 | 30.1323, |
| 50 | 49.7276, |
| 51 | 69.6342, |
| 52 | 53.9027, |
| 53 | 56.2335, |
| 54 | 63.0817, |
| 55 | 73.9222, |
| 56 | 87.9027, |
| 57 | 109.6109, |
| 58 | 110.6342, |
| 59 | 113.8638, |
| 60 | 119.5564, |
| 61 | 127.8677, |
| 62 | 164.4514, |
| 63 | 165.0700, |
| 64 | 167.0311, |
| 65 | 170.5914, |
| 66 | 175.9961, |
| 67 | 220.1634, |
| 68 | 220.5875, |
| 69 | 221.9377, |
| 70 | 224.4086, |
| 71 | 228.2179, |
| 72 | 27.7004, |
| 73 | 32.0895, |
| 74 | 43.3268, |
| 75 | 58.4747, |
| 76 | 75.8716, |
| 77 | 61.9844, |
| 78 | 64.0000, |
| 79 | 70.0000, |
| 80 | 79.7665, |
| 81 | 92.7160, |
| 82 | 113.3269, |
| 83 | 114.3152, |
| 84 | 117.4125, |
| 85 | 122.9066, |
| 86 | 130.9455, |
| 87 | 166.7004, |
| 88 | 167.3113, |
| 89 | 169.2412, |
| 90 | 172.7432, |
| 91 | 178.0545, |
| 92 | 221.7121, |
| 93 | 222.1323, |
| 94 | 223.4630, |
| 95 | 225.9183, |
| 96 | 229.6926, |
| 97 | 61.8949, |
| 98 | 63.9105, |
| 99 | 69.9222, |
| 100 | 79.7004, |
| 101 | 92.6615, |
| 102 | 82.1907, |
| 103 | 83.6498, |
| 104 | 88.1206, |
| 105 | 95.7626, |
| 106 | 106.4358, |
| 107 | 124.3502, |
| 108 | 125.2296, |
| 109 | 128.0000, |
| 110 | 132.9455, |
| 111 | 140.2763, |
| 112 | 173.6770, |
| 113 | 174.2529, |
| 114 | 176.0817, |
| 115 | 179.4124, |
| 116 | 184.4825, |
| 117 | 226.5798, |
| 118 | 226.9844, |
| 119 | 228.2879, |
| 120 | 230.6693, |
| 121 | 234.3463, |
| 122 | 95.5798, |
| 123 | 96.7977, |
| 124 | 100.5798, |
| 125 | 107.1595, |
| 126 | 116.5759, |
| 127 | 108.9144, |
| 128 | 109.9494, |
| 129 | 113.2023, |
| 130 | 118.9339, |
| 131 | 127.2879, |
| 132 | 142.0428, |
| 133 | 142.7860, |
| 134 | 145.1362, |
| 135 | 149.3813, |
| 136 | 155.7471, |
| 137 | 185.7276, |
| 138 | 186.2568, |
| 139 | 187.9339, |
| 140 | 190.9961, |
| 141 | 195.6848, |
| 142 | 235.2568, |
| 143 | 235.6420, |
| 144 | 236.8794, |
| 145 | 239.1517, |
| 146 | 242.6615, |
| 147 | 129.8054, |
| 148 | 130.6303, |
| 149 | 133.2646, |
| 150 | 137.9767, |
| 151 | 144.9883, |
| 152 | 139.2607, |
| 153 | 140.0272, |
| 154 | 142.4397, |
| 155 | 146.7821, |
| 156 | 153.2840, |
| 157 | 165.1907, |
| 158 | 165.8054, |
| 159 | 167.7626, |
| 160 | 171.2957, |
| 161 | 176.6731, |
| 162 | 202.8171, |
| 163 | 203.2879, |
| 164 | 204.7899, |
| 165 | 207.5292, |
| 166 | 211.7471, |
| 167 | 248.0817, |
| 168 | 248.4436, |
| 169 | 249.5953, |
| 170 | 251.7198, |
| 171 | 255.0000, |
| 172 | ]; |
| 173 | const RAMP_GREY: &[f64] = &[ |
| 174 | 0.0000, |
| 175 | 14.9961, |
| 176 | 30.0000, |
| 177 | 45.0000, |
| 178 | 60.0000, |
| 179 | 74.9961, |
| 180 | 90.0000, |
| 181 | 105.0000, |
| 182 | 120.0039, |
| 183 | 135.0000, |
| 184 | 150.0000, |
| 185 | 165.0000, |
| 186 | 180.0000, |
| 187 | 195.0000, |
| 188 | 210.0000, |
| 189 | 225.0000, |
| 190 | 240.0000, |
| 191 | 255.0000, |
| 192 | ]; |
| 193 | const CORNER_XYZ: &[[f64; 3]] = &[ |
| 194 | [0.0000, 0.0000, 0.0000], |
| 195 | [14.3046, 6.0610, 71.3913], |
| 196 | [38.5113, 71.6905, 9.7067], |
| 197 | [52.8159, 77.7515, 81.0980], |
| 198 | [43.6041, 22.2485, 1.3920], |
| 199 | [57.9087, 28.3095, 72.7833], |
| 200 | [82.1154, 93.9390, 11.0987], |
| 201 | [96.4200, 100.0000, 82.4900], |
| 202 | [20.8133, 21.5861, 17.8063], |
| 203 | ]; |
| 204 | const RAMP_XYZ: &[[f64; 3]] = &[ |
| 205 | [0.0000, 0.0000, 0.0000], |
| 206 | [0.4605, 0.4776, 0.3940], |
| 207 | [1.2521, 1.2985, 1.0712], |
| 208 | [2.5306, 2.6246, 2.1650], |
| 209 | [4.3564, 4.5182, 3.7271], |
| 210 | [6.7840, 7.0359, 5.8039], |
| 211 | [9.8575, 10.2235, 8.4334], |
| 212 | [13.6211, 14.1268, 11.6532], |
| 213 | [18.1099, 18.7823, 15.4935], |
| 214 | [23.3609, 24.2283, 19.9859], |
| 215 | [29.4078, 30.4997, 25.1592], |
| 216 | [36.2787, 37.6257, 31.0374], |
| 217 | [44.0073, 45.6413, 37.6495], |
| 218 | [52.6187, 54.5724, 45.0167], |
| 219 | [62.1408, 64.4480, 53.1632], |
| 220 | [72.5986, 75.2941, 62.1101], |
| 221 | [84.0172, 87.1366, 71.8790], |
| 222 | [96.4200, 100.0000, 82.4900], |
| 223 | ]; |
| 224 | const CUBE_GREY_GS: &[f64] = &[ |
| 225 | 0.0000, |
| 226 | 10.2685, |
| 227 | 30.1323, |
| 228 | 49.7276, |
| 229 | 69.6342, |
| 230 | 53.9027, |
| 231 | 56.2335, |
| 232 | 63.0817, |
| 233 | 73.9222, |
| 234 | 87.9027, |
| 235 | 109.6070, |
| 236 | 110.6342, |
| 237 | 113.8560, |
| 238 | 119.5525, |
| 239 | 127.8599, |
| 240 | 164.4514, |
| 241 | 165.0700, |
| 242 | 167.0272, |
| 243 | 170.5875, |
| 244 | 175.9922, |
| 245 | 220.1595, |
| 246 | 220.5837, |
| 247 | 221.9339, |
| 248 | 224.4047, |
| 249 | 228.2140, |
| 250 | 27.7004, |
| 251 | 32.0895, |
| 252 | 43.3268, |
| 253 | 58.4747, |
| 254 | 75.8716, |
| 255 | 61.9844, |
| 256 | 64.0000, |
| 257 | 70.0000, |
| 258 | 79.7665, |
| 259 | 92.7160, |
| 260 | 113.3230, |
| 261 | 114.3113, |
| 262 | 117.4086, |
| 263 | 122.8988, |
| 264 | 130.9416, |
| 265 | 166.7004, |
| 266 | 167.3113, |
| 267 | 169.2374, |
| 268 | 172.7393, |
| 269 | 178.0545, |
| 270 | 221.7082, |
| 271 | 222.1245, |
| 272 | 223.4591, |
| 273 | 225.9105, |
| 274 | 229.6887, |
| 275 | 61.8949, |
| 276 | 63.9105, |
| 277 | 69.9222, |
| 278 | 79.7004, |
| 279 | 92.6615, |
| 280 | 82.1907, |
| 281 | 83.6498, |
| 282 | 88.1128, |
| 283 | 95.7549, |
| 284 | 106.4358, |
| 285 | 124.3463, |
| 286 | 125.2257, |
| 287 | 128.0000, |
| 288 | 132.9455, |
| 289 | 140.2763, |
| 290 | 173.6731, |
| 291 | 174.2490, |
| 292 | 176.0817, |
| 293 | 179.4124, |
| 294 | 184.4786, |
| 295 | 226.5759, |
| 296 | 226.9805, |
| 297 | 228.2802, |
| 298 | 230.6654, |
| 299 | 234.3424, |
| 300 | 95.5875, |
| 301 | 96.7977, |
| 302 | 100.5798, |
| 303 | 107.1595, |
| 304 | 116.5798, |
| 305 | 108.9144, |
| 306 | 109.9494, |
| 307 | 113.2023, |
| 308 | 118.9339, |
| 309 | 127.2879, |
| 310 | 142.0428, |
| 311 | 142.7821, |
| 312 | 145.1362, |
| 313 | 149.3813, |
| 314 | 155.7471, |
| 315 | 185.7276, |
| 316 | 186.2529, |
| 317 | 187.9339, |
| 318 | 190.9961, |
| 319 | 195.6809, |
| 320 | 235.2529, |
| 321 | 235.6381, |
| 322 | 236.8755, |
| 323 | 239.1479, |
| 324 | 242.6576, |
| 325 | 129.8054, |
| 326 | 130.6303, |
| 327 | 133.2646, |
| 328 | 137.9767, |
| 329 | 144.9883, |
| 330 | 139.2607, |
| 331 | 140.0272, |
| 332 | 142.4397, |
| 333 | 146.7821, |
| 334 | 153.2840, |
| 335 | 165.1907, |
| 336 | 165.8015, |
| 337 | 167.7587, |
| 338 | 171.2957, |
| 339 | 176.6731, |
| 340 | 202.8171, |
| 341 | 203.2840, |
| 342 | 204.7860, |
| 343 | 207.5292, |
| 344 | 211.7471, |
| 345 | 248.0778, |
| 346 | 248.4397, |
| 347 | 249.5914, |
| 348 | 251.7160, |
| 349 | 254.9961, |
| 350 | ]; |
| 351 | const CORNER_XYZ_GS: &[[f64; 3]] = &[ |
| 352 | [0.0000, 0.0000, 0.0000], |
| 353 | [14.3066, 6.0608, 71.4096], |
| 354 | [38.5147, 71.6873, 9.7076], |
| 355 | [52.8213, 77.7481, 81.1173], |
| 356 | [43.6066, 22.2488, 1.3916], |
| 357 | [57.9132, 28.3096, 72.8012], |
| 358 | [82.1213, 93.9362, 11.0992], |
| 359 | [96.4279, 99.9969, 82.5088], |
| 360 | [20.8144, 21.5848, 17.8099], |
| 361 | ]; |
| 362 | const FOGRA_LAB: &[[f64; 3]] = &[ |
| 363 | [100.0000, -0.0000, -0.0000], |
| 364 | [17.4249, 0.0117, 0.6094], |
| 365 | [93.7837, -4.6914, 97.8867], |
| 366 | [17.0757, -3.1328, 12.5977], |
| 367 | [50.9252, 77.4102, -1.7813], |
| 368 | [11.5748, 14.5000, 1.9375], |
| 369 | [49.8851, 71.3164, 50.9961], |
| 370 | [12.5169, 8.9570, 8.0313], |
| 371 | [58.1189, -39.7109, -50.4766], |
| 372 | [11.5303, -8.6914, -10.0391], |
| 373 | [52.9289, -67.7461, 29.1055], |
| 374 | [12.4908, -13.3438, 4.1250], |
| 375 | [25.6771, 22.5508, -46.9023], |
| 376 | [8.9185, 5.9688, -5.5781], |
| 377 | [24.7151, 0.1016, 0.6797], |
| 378 | [9.8192, -0.0664, 2.6328], |
| 379 | ]; |
| 380 | const GS_CMYK_LAB: &[[f64; 3]] = &[ |
| 381 | [100.0000, -0.0000, -0.0000], |
| 382 | [22.3529, 1.0703, 0.0586], |
| 383 | [95.0812, -6.2969, 90.3516], |
| 384 | [20.4856, -2.2578, 11.7109], |
| 385 | [53.9537, 76.1406, -6.5625], |
| 386 | [13.9767, 15.8789, -0.3906], |
| 387 | [53.6045, 69.8125, 45.1953], |
| 388 | [15.0398, 11.5742, 6.3789], |
| 389 | [63.6106, -41.3945, -48.3359], |
| 390 | [16.7754, -8.0195, -9.9844], |
| 391 | [58.8848, -67.5430, 27.1289], |
| 392 | [16.8413, -13.4687, 4.3789], |
| 393 | [30.9191, 19.9883, -48.3633], |
| 394 | [10.6265, 7.7031, -9.0977], |
| 395 | [29.0119, 0.4844, -1.3555], |
| 396 | [11.7724, 0.7656, 0.3281], |
| 397 | ]; |
| 398 | const GS_LAB_CMYK: &[[f64; 4]] = &[ |
| 399 | [100.0000, 94.1176, 0.0000, 10.5882], |
| 400 | [74.9020, 67.8431, 65.4902, 90.1961], |
| 401 | [87.8431, 98.4314, 0.0000, 14.5098], |
| 402 | [56.4706, 80.3922, 62.3529, 76.0784], |
| 403 | [100.0000, 0.0000, 0.0000, 0.0000], |
| 404 | [62.3529, 0.0000, 100.0000, 0.0000], |
| 405 | [6.6667, 34.9020, 0.0000, 0.0000], |
| 406 | [0.0000, 100.0000, 81.1765, 0.0000], |
| 407 | ]; |
| 408 | |
| 409 | // The first nine tags of FOGRA39L Coated are those every later reader needs; the rest are listed |
| 410 | // so that a change to the tag table is seen. |
| 411 | const FOGRA_TAGS: &[&[u8; 4]] = &[ |
| 412 | b"desc", b"cprt", b"wtpt", b"bkpt", b"A2B1", b"B2A1", b"A2B0", b"A2B2", b"B2A0", b"B2A2", |
| 413 | b"gamt", b"arts", b"meta", b"dscm", b"dmnd", b"dmdd", |
| 414 | ]; |
| 415 | |
| 416 | fn fogra_bytes() -> Outcome<Vec<u8>> { |
| 417 | let p = PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("data/icc/FOGRA39L_coated.icc"); |
| 418 | Ok(res!(fs::read(&p), IO, File)) |
| 419 | } |
| 420 | |
| 421 | fn system(dir: &str, name: &str) -> Outcome<Profile> { |
| 422 | let p = PathBuf::from(dir).join(name); |
| 423 | let b = res!(fs::read(&p), IO, File); |
| 424 | Profile::read(name, &b) |
| 425 | } |
| 426 | |
| 427 | fn sigs(p: &Profile) -> Vec<Sig> { |
| 428 | p.tags.iter().map(|e| e.sig).collect() |
| 429 | } |
| 430 | |
| 431 | fn near(got: f64, want: f64, tol: f64, what: &str) { |
| 432 | assert!((got - want).abs() <= tol, "{}: got {}, want {} (tolerance {})", what, got, want, tol); |
| 433 | } |
| 434 | |
| 435 | // Linear interpolation of a 16-bit table at x in 0..1, as a lookup table's curves are applied. |
| 436 | fn table(t: &[u16], x: f64) -> f64 { |
| 437 | let at = x.clamp(0.0, 1.0) * (t.len() - 1) as f64; |
| 438 | let i = (at.floor() as usize).min(t.len() - 2); |
| 439 | let f = at - i as f64; |
| 440 | (t[i] as f64 + (t[i + 1] as f64 - t[i] as f64) * f) / 65535.0 |
| 441 | } |
| 442 | |
| 443 | // A lattice node's values after the table's output curves, as fractions of full scale. The |
| 444 | // swatches sit on the lattice's corners, where the input curves return 0 and 1, which is asserted. |
| 445 | fn corner(lut: &Lut, idx: &[usize]) -> Outcome<Vec<f64>> { |
| 446 | let top = lut.max(); |
| 447 | for c in 0..lut.nin { |
| 448 | let t = &lut.ins[c * lut.ine..(c + 1) * lut.ine]; |
| 449 | assert_eq!((t[0] as f64, t[lut.ine - 1] as f64), (0.0, top), "input curve {}", c); |
| 450 | } |
| 451 | let node = res!(lut.node(idx)); |
| 452 | let mut out = Vec::new(); |
| 453 | for c in 0..lut.nout { |
| 454 | let t = &lut.outs[c * lut.oute..(c + 1) * lut.oute]; |
| 455 | let t16: Vec<u16> = if lut.bits == 8 { |
| 456 | t.iter().map(|v| v * 257).collect() |
| 457 | } else { |
| 458 | t.to_vec() |
| 459 | }; |
| 460 | out.push(table(&t16, node[c] as f64 / top)); |
| 461 | } |
| 462 | Ok(out) |
| 463 | } |
| 464 | |
| 465 | // The index of corner `bits` (the first input slowest, as the fixtures count) of a lattice. |
| 466 | fn corner_idx(n: usize, grid: usize, k: usize) -> Vec<usize> { |
| 467 | (0..n).map(|i| if (k >> (n - 1 - i)) & 1 == 1 { grid - 1 } else { 0 }).collect() |
| 468 | } |
| 469 | |
| 470 | // A profile built byte by byte: the header, a tag table, and the tags laid end to end. |
| 471 | fn build(version: u32, class: &[u8; 4], space: &[u8; 4], pcs: &[u8; 4], tags: &[(Sig, Vec<u8>)]) -> Vec<u8> { |
| 472 | let table_len = 4 + 12 * tags.len(); |
| 473 | let mut body = Vec::new(); |
| 474 | let mut dir = Vec::new(); |
| 475 | for (sig, data) in tags { |
| 476 | let off = 128 + table_len + body.len(); |
| 477 | dir.extend_from_slice(sig); |
| 478 | dir.extend_from_slice(&(off as u32).to_be_bytes()); |
| 479 | dir.extend_from_slice(&(data.len() as u32).to_be_bytes()); |
| 480 | body.extend_from_slice(data); |
| 481 | while body.len() % 4 != 0 { |
| 482 | body.push(0); |
| 483 | } |
| 484 | } |
| 485 | let size = 128 + table_len + body.len(); |
| 486 | let mut b = vec![0u8; 128]; |
| 487 | b[0..4].copy_from_slice(&(size as u32).to_be_bytes()); |
| 488 | b[8..12].copy_from_slice(&version.to_be_bytes()); |
| 489 | b[12..16].copy_from_slice(class); |
| 490 | b[16..20].copy_from_slice(space); |
| 491 | b[20..24].copy_from_slice(pcs); |
| 492 | b[36..40].copy_from_slice(b"acsp"); |
| 493 | for (i, v) in [0.964_202_88f64, 1.0, 0.824_905_4].iter().enumerate() { |
| 494 | b[68 + 4 * i..72 + 4 * i].copy_from_slice(&((v * 65536.0).round() as i32).to_be_bytes()); |
| 495 | } |
| 496 | b.extend_from_slice(&(tags.len() as u32).to_be_bytes()); |
| 497 | b.extend_from_slice(&dir); |
| 498 | b.extend_from_slice(&body); |
| 499 | b |
| 500 | } |
| 501 | |
| 502 | fn s15(v: f64) -> [u8; 4] { |
| 503 | ((v * 65536.0).round() as i32).to_be_bytes() |
| 504 | } |
| 505 | |
| 506 | fn xyz_tag(v: [f64; 3]) -> Vec<u8> { |
| 507 | let mut t = b"XYZ \0\0\0\0".to_vec(); |
| 508 | for x in v { |
| 509 | t.extend_from_slice(&s15(x)); |
| 510 | } |
| 511 | t |
| 512 | } |
| 513 | |
| 514 | // The sRGB curve as the version 4 parametric curve of function type 3. |
| 515 | fn para_srgb() -> Vec<u8> { |
| 516 | let mut t = b"para\0\0\0\0\0\x03\0\0".to_vec(); |
| 517 | for v in [2.4, 1.0 / 1.055, 0.055 / 1.055, 1.0 / 12.92, 0.04045] { |
| 518 | t.extend_from_slice(&s15(v)); |
| 519 | } |
| 520 | t |
| 521 | } |
| 522 | |
| 523 | // A text description in the version 4 form, one English record. |
| 524 | fn mluc(text: &str) -> Vec<u8> { |
| 525 | let units: Vec<u8> = text.encode_utf16().flat_map(|u| u.to_be_bytes()).collect(); |
| 526 | let mut t = b"mluc\0\0\0\0".to_vec(); |
| 527 | t.extend_from_slice(&1u32.to_be_bytes()); |
| 528 | t.extend_from_slice(&12u32.to_be_bytes()); |
| 529 | t.extend_from_slice(b"enUS"); |
| 530 | t.extend_from_slice(&(units.len() as u32).to_be_bytes()); |
| 531 | t.extend_from_slice(&28u32.to_be_bytes()); |
| 532 | t.extend_from_slice(&units); |
| 533 | t |
| 534 | } |
| 535 | |
| 536 | // A matrix profile of sRGB, version 4, the colorants those of sRGB adapted to D50. |
| 537 | fn srgb_v4() -> Vec<u8> { |
| 538 | let cols = [[0.43607, 0.22249, 0.01392], [0.38515, 0.71687, 0.09708], [0.14307, 0.06061, 0.71410]]; |
| 539 | build(0x0420_0000, b"mntr", b"RGB ", b"XYZ ", &[ |
| 540 | (*b"desc", mluc("Hand-built sRGB")), |
| 541 | (*b"rXYZ", xyz_tag(cols[0])), |
| 542 | (*b"gXYZ", xyz_tag(cols[1])), |
| 543 | (*b"bXYZ", xyz_tag(cols[2])), |
| 544 | (*b"rTRC", para_srgb()), |
| 545 | (*b"gTRC", para_srgb()), |
| 546 | (*b"bTRC", para_srgb()), |
| 547 | ]) |
| 548 | } |
| 549 | |
| 550 | fn cube() -> Vec<[f64; 3]> { |
| 551 | let mut v = Vec::new(); |
| 552 | for r in [0.0, 64.0, 128.0, 191.0, 255.0] { |
| 553 | for g in [0.0, 64.0, 128.0, 191.0, 255.0] { |
| 554 | for b in [0.0, 64.0, 128.0, 191.0, 255.0] { |
| 555 | v.push([r / 255.0, g / 255.0, b / 255.0]); |
| 556 | } |
| 557 | } |
| 558 | } |
| 559 | v |
| 560 | } |
| 561 | |
| 562 | fn corners() -> Vec<[f64; 3]> { |
| 563 | let mut v = Vec::new(); |
| 564 | for r in [0.0, 1.0] { |
| 565 | for g in [0.0, 1.0] { |
| 566 | for b in [0.0, 1.0] { |
| 567 | v.push([r, g, b]); |
| 568 | } |
| 569 | } |
| 570 | } |
| 571 | v.push([128.0 / 255.0, 128.0 / 255.0, 128.0 / 255.0]); |
| 572 | v |
| 573 | } |
| 574 | |
| 575 | // RGB to grey within one level of eight bits. |
| 576 | fn check_grey(src: &RgbMatrix, dst: &Grey, cube_want: &[f64], ramp_want: &[f64], tag: &str) { |
| 577 | let c = cube(); |
| 578 | assert_eq!(c.len(), cube_want.len()); |
| 579 | for (i, rgb) in c.iter().enumerate() { |
| 580 | near(rgb_to_grey(src, dst, *rgb) * 255.0, cube_want[i], 1.0, &fmt!("{} cube {:?}", tag, rgb)); |
| 581 | } |
| 582 | assert_eq!(ramp_want.len(), 18); |
| 583 | for (i, want) in ramp_want.iter().enumerate() { |
| 584 | let v = (i * 15) as f64 / 255.0; |
| 585 | near(rgb_to_grey(src, dst, [v, v, v]) * 255.0, *want, 1.0, &fmt!("{} ramp {}", tag, i)); |
| 586 | } |
| 587 | } |
| 588 | |
| 589 | fn check_xyz(src: &RgbMatrix, want: &[[f64; 3]], tol: f64, tag: &str) { |
| 590 | let c = corners(); |
| 591 | assert_eq!(c.len(), want.len()); |
| 592 | for (i, rgb) in c.iter().enumerate() { |
| 593 | let got = src.xyz(*rgb); |
| 594 | for k in 0..3 { |
| 595 | near(got[k] * 100.0, want[i][k], tol, &fmt!("{} corner {} xyz[{}]", tag, i, k)); |
| 596 | } |
| 597 | } |
| 598 | } |
| 599 | |
| 600 | #[test] |
| 601 | fn test_fogra39l_header_tags_and_description_read_as_lcms_reports_them() -> Outcome<()> { |
| 602 | let p = res!(Profile::read("FOGRA39L_coated.icc", &res!(fogra_bytes()))); |
| 603 | assert_eq!((p.head.major, p.head.minor), (2, 2)); |
| 604 | assert_eq!(p.head.class, Class::Output); |
| 605 | assert_eq!((p.head.space, p.head.pcs), (Space::Cmyk, Space::Lab)); |
| 606 | assert_eq!(p.head.intent, 1); |
| 607 | assert_eq!(p.head.size as usize, res!(fogra_bytes()).len()); |
| 608 | near(p.head.illum[0], 0.9642, 1e-4, "illuminant X"); |
| 609 | assert_eq!(sigs(&p), FOGRA_TAGS.iter().map(|s| **s).collect::<Vec<Sig>>()); |
| 610 | assert_eq!(p.desc.as_deref(), Some("FOGRA39L Coated")); |
| 611 | let w = res!(p.wtpt.ok_or_else(|| err!("No wtpt."; Missing))); |
| 612 | // The media white point of a printing condition is the paper, not the D50 of the connection space. |
| 613 | near(w[0], 0.8448, 1e-4, "white point X"); |
| 614 | near(w[1], 0.8763, 1e-4, "white point Y"); |
| 615 | near(w[2], 0.7462, 1e-4, "white point Z"); |
| 616 | Ok(()) |
| 617 | } |
| 618 | |
| 619 | #[test] |
| 620 | fn test_fogra39l_lookup_tables_have_the_shape_lcms_walks() -> Outcome<()> { |
| 621 | let p = res!(Profile::read("FOGRA39L_coated.icc", &res!(fogra_bytes()))); |
| 622 | for i in 0..3 { |
| 623 | let t = res!(p.a2b[i].as_ref().ok_or_else(|| err!("No A2B{}.", i; Missing))); |
| 624 | assert_eq!((t.bits, t.nin, t.nout, t.grid, t.ine, t.oute), (16, 4, 3, 9, 1024, 1024)); |
| 625 | assert_eq!((t.ins.len(), t.clut.len(), t.outs.len()), (4 * 1024, 9 * 9 * 9 * 9 * 3, 3 * 1024)); |
| 626 | } |
| 627 | for i in 0..3 { |
| 628 | let t = res!(p.b2a[i].as_ref().ok_or_else(|| err!("No B2A{}.", i; Missing))); |
| 629 | assert_eq!((t.bits, t.nin, t.nout, t.grid, t.ine, t.oute), (16, 3, 4, 17, 1024, 1024)); |
| 630 | assert_eq!(t.clut.len(), 17 * 17 * 17 * 4); |
| 631 | } |
| 632 | // No matrix path in a printer's profile. |
| 633 | assert!(p.cols.iter().all(|c| c.is_none()) && p.ktrc.is_none()); |
| 634 | Ok(()) |
| 635 | } |
| 636 | |
| 637 | #[test] |
| 638 | fn test_fogra39l_corner_nodes_give_the_lab_lcms_gives() -> Outcome<()> { |
| 639 | let p = res!(Profile::read("FOGRA39L_coated.icc", &res!(fogra_bytes()))); |
| 640 | let t = res!(p.a2b[1].as_ref().ok_or_else(|| err!("No A2B1."; Missing))); |
| 641 | assert_eq!(FOGRA_LAB.len(), 16); |
| 642 | for (k, want) in FOGRA_LAB.iter().enumerate() { |
| 643 | let out = res!(corner(t, &corner_idx(4, t.grid, k))); |
| 644 | let v = [out[0] * 65535.0, out[1] * 65535.0, out[2] * 65535.0]; |
| 645 | let lab = t.lab([v[0].round() as u16, v[1].round() as u16, v[2].round() as u16]); |
| 646 | for c in 0..3 { |
| 647 | near(lab[c], want[c], 0.01, &fmt!("FOGRA corner {} Lab[{}]", k, c)); |
| 648 | } |
| 649 | } |
| 650 | Ok(()) |
| 651 | } |
| 652 | |
| 653 | #[test] |
| 654 | fn test_a_truncated_profile_is_refused_by_name() -> Outcome<()> { |
| 655 | let whole = res!(fogra_bytes()); |
| 656 | // Cut short with its length field left alone: the header says more than is there. |
| 657 | let e = match Profile::read("cut.icc", &whole[..60_000]) { |
| 658 | Ok(_) => return Err(err!("A profile cut to 60000 bytes was read."; Test, Bug)), |
| 659 | Err(e) => fmt!("{}", e), |
| 660 | }; |
| 661 | assert!(e.contains("cut.icc") && e.contains("truncated") && e.contains("122152"), "{}", e); |
| 662 | // Cut short with the length field made to agree: the tag table runs off the end, and says which tag. |
| 663 | let mut short = whole[..60_000].to_vec(); |
| 664 | short[0..4].copy_from_slice(&60_000u32.to_be_bytes()); |
| 665 | let e = match Profile::read("cut.icc", &short) { |
| 666 | Ok(_) => return Err(err!("A profile cut to 60000 bytes was read."; Test, Bug)), |
| 667 | Err(e) => fmt!("{}", e), |
| 668 | }; |
| 669 | assert!(e.contains("'B2A1'") && e.contains("cut.icc") && e.contains("truncated"), "{}", e); |
| 670 | // Not even a header. |
| 671 | let e = match Profile::read("tiny.icc", &whole[..100]) { |
| 672 | Ok(_) => return Err(err!("A profile of 100 bytes was read."; Test, Bug)), |
| 673 | Err(e) => fmt!("{}", e), |
| 674 | }; |
| 675 | assert!(e.contains("tiny.icc") && e.contains("header"), "{}", e); |
| 676 | // A tag cut short inside its own declared size: a lut16 whose table is shorter than its header says. |
| 677 | let mut p = whole.clone(); |
| 678 | let at = 132 + 12 * 4; // A2B1 is the fifth entry; its size is the last word of the entry |
| 679 | let size = u32::from_be_bytes([p[at + 8], p[at + 9], p[at + 10], p[at + 11]]); |
| 680 | p[at + 8..at + 12].copy_from_slice(&(size - 4000).to_be_bytes()); |
| 681 | let e = match Profile::read("short_tag.icc", &p) { |
| 682 | Ok(_) => return Err(err!("A table with its tail removed was read."; Test, Bug)), |
| 683 | Err(e) => fmt!("{}", e), |
| 684 | }; |
| 685 | assert!(e.contains("'A2B1'") && e.contains("short_tag.icc") && e.contains("truncated"), "{}", e); |
| 686 | Ok(()) |
| 687 | } |
| 688 | |
| 689 | #[test] |
| 690 | fn test_a_version_4_mab_table_is_refused_by_name() -> Outcome<()> { |
| 691 | let mut mab = b"mAB \0\0\0\0".to_vec(); |
| 692 | mab.extend_from_slice(&[3, 3, 0, 0]); |
| 693 | mab.resize(32, 0); |
| 694 | let b = build(0x0420_0000, b"prtr", b"RGB ", b"Lab ", &[(*b"A2B0", mab)]); |
| 695 | let e = match Profile::read("v4_mab.icc", &b) { |
| 696 | Ok(_) => return Err(err!("A profile with an mAB table was read."; Test, Bug)), |
| 697 | Err(e) => fmt!("{}", e), |
| 698 | }; |
| 699 | assert!(e.contains("'A2B0'") && e.contains("'mAB '") && e.contains("v4_mab.icc"), "{}", e); |
| 700 | assert!(e.contains("No other profile is substituted"), "{}", e); |
| 701 | // The same profile with no table in the tag position reads, so it is the table that is refused. |
| 702 | let ok = build(0x0420_0000, b"prtr", b"RGB ", b"Lab ", &[(*b"cprt", b"text\0\0\0\0x\0".to_vec())]); |
| 703 | let p = res!(Profile::read("v4_plain.icc", &ok)); |
| 704 | assert!(p.a2b.iter().all(|t| t.is_none())); |
| 705 | // mBA is refused alike. |
| 706 | let mut mba = b"mBA \0\0\0\0".to_vec(); |
| 707 | mba.resize(32, 0); |
| 708 | let b = build(0x0420_0000, b"prtr", b"RGB ", b"Lab ", &[(*b"B2A1", mba)]); |
| 709 | let e = match Profile::read("v4_mba.icc", &b) { |
| 710 | Ok(_) => return Err(err!("A profile with an mBA table was read."; Test, Bug)), |
| 711 | Err(e) => fmt!("{}", e), |
| 712 | }; |
| 713 | assert!(e.contains("'B2A1'") && e.contains("'mBA '"), "{}", e); |
| 714 | Ok(()) |
| 715 | } |
| 716 | |
| 717 | #[test] |
| 718 | fn test_a_profile_not_made_for_the_reader_is_refused() -> Outcome<()> { |
| 719 | // Not an ICC profile at all. |
| 720 | let mut b = srgb_v4(); |
| 721 | b[36..40].copy_from_slice(b"nope"); |
| 722 | assert!(Profile::read("noticc.icc", &b).is_err()); |
| 723 | // A version this reader does not read. |
| 724 | let b = build(0x0300_0000, b"mntr", b"RGB ", b"XYZ ", &[]); |
| 725 | let e = match Profile::read("v3.icc", &b) { |
| 726 | Ok(_) => return Err(err!("A version 3 profile was read."; Test, Bug)), |
| 727 | Err(e) => fmt!("{}", e), |
| 728 | }; |
| 729 | assert!(e.contains("v3.icc") && e.contains("version 3"), "{}", e); |
| 730 | // A lookup table whose channels do not fit its profile: an RGB profile holding a four-input table. |
| 731 | let mut lut = b"mft2\0\0\0\0".to_vec(); |
| 732 | lut.extend_from_slice(&[4, 3, 2, 0]); |
| 733 | for v in [1.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 1.0] { |
| 734 | lut.extend_from_slice(&s15(v)); |
| 735 | } |
| 736 | lut.extend_from_slice(&[0, 2, 0, 2]); |
| 737 | lut.resize(52 + 2 * (4 * 2 + 16 * 3 + 3 * 2), 0); |
| 738 | let b = build(0x0220_0000, b"prtr", b"RGB ", b"Lab ", &[(*b"A2B0", lut)]); |
| 739 | let e = match Profile::read("mismatch.icc", &b) { |
| 740 | Ok(_) => return Err(err!("A table of the wrong shape was read."; Test, Bug)), |
| 741 | Err(e) => fmt!("{}", e), |
| 742 | }; |
| 743 | assert!(e.contains("'A2B0'") && e.contains("mismatch.icc"), "{}", e); |
| 744 | Ok(()) |
| 745 | } |
| 746 | |
| 747 | #[test] |
| 748 | fn test_a_grey_profile_with_a_lab_connection_space_is_refused_by_name() -> Outcome<()> { |
| 749 | // A kTRC of no entries is the identity; it is the connection space that the grey path cannot take. |
| 750 | let curv = b"curv\0\0\0\0\0\0\0\0".to_vec(); |
| 751 | let b = build(0x0220_0000, b"mntr", b"GRAY", b"Lab ", &[(*b"kTRC", curv)]); |
| 752 | let p = res!(Profile::read("labgrey.icc", &b)); |
| 753 | let e = match Grey::read(&p) { |
| 754 | Ok(_) => return Err(err!("A grey profile with a Lab connection space was read as a grey curve."; Test, Bug)), |
| 755 | Err(e) => fmt!("{}", e), |
| 756 | }; |
| 757 | assert!(e.contains("labgrey.icc") && e.contains("Gray") && e.contains("Lab"), "{}", e); |
| 758 | Ok(()) |
| 759 | } |
| 760 | |
| 761 | #[test] |
| 762 | fn test_a_hand_built_version_4_srgb_profile_matches_the_built_in_space() -> Outcome<()> { |
| 763 | let p = res!(Profile::read("hand_srgb.icc", &srgb_v4())); |
| 764 | assert_eq!(p.desc.as_deref(), Some("Hand-built sRGB")); |
| 765 | assert_eq!((p.head.major, p.head.minor), (4, 2)); |
| 766 | assert!(matches!(p.trcs[0], Some(Curve::Para { func: 3, .. }))); |
| 767 | let m = res!(RgbMatrix::read(&p)); |
| 768 | let b = res!(builtin::srgb()); |
| 769 | for rgb in cube() { |
| 770 | let (x, y) = (m.xyz(rgb), b.xyz(rgb)); |
| 771 | for k in 0..3 { |
| 772 | near(x[k], y[k], 2e-4, &fmt!("hand-built vs built-in {:?} [{}]", rgb, k)); |
| 773 | } |
| 774 | } |
| 775 | Ok(()) |
| 776 | } |
| 777 | |
| 778 | #[test] |
| 779 | fn test_rgb_to_xyz_through_the_built_in_srgb_matches_lcms() -> Outcome<()> { |
| 780 | check_xyz(&res!(builtin::srgb()), CORNER_XYZ, 5e-3, "built-in sRGB"); |
| 781 | Ok(()) |
| 782 | } |
| 783 | |
| 784 | #[test] |
| 785 | fn test_rgb_to_grey_through_the_built_in_spaces_is_within_one_level_of_lcms() -> Outcome<()> { |
| 786 | check_grey(&res!(builtin::srgb()), &builtin::sgray(), CUBE_GREY, RAMP_GREY, "built-in"); |
| 787 | Ok(()) |
| 788 | } |
| 789 | |
| 790 | #[test] |
| 791 | fn test_grey_level_to_y_through_the_built_in_sgray_matches_lcms() { |
| 792 | let g = builtin::sgray(); |
| 793 | for (i, want) in RAMP_XYZ.iter().enumerate() { |
| 794 | near(g.y((i * 15) as f64 / 255.0) * 100.0, want[1], 0.05, &fmt!("sGray Y at {}", i * 15)); |
| 795 | } |
| 796 | } |
| 797 | |
| 798 | #[test] |
| 799 | fn test_the_bundled_bytes_are_the_fogra39l_file() -> Outcome<()> { |
| 800 | // Present only with the feature on; the build without it carries none of the bytes. |
| 801 | #[cfg(feature = "fogra39")] |
| 802 | { |
| 803 | assert_eq!(oxedyne_fe2o3_graphics::icc::FOGRA39L, &res!(fogra_bytes())[..]); |
| 804 | let p = res!(oxedyne_fe2o3_graphics::icc::fogra39l()); |
| 805 | assert_eq!(p.desc.as_deref(), Some("FOGRA39L Coated")); |
| 806 | } |
| 807 | Ok(()) |
| 808 | } |
| 809 | |
| 810 | #[test] |
| 811 | #[ignore = "reads Ghostscript's AGPL profiles from /usr/share/color/icc/ghostscript"] |
| 812 | fn test_ghostscript_default_rgb_reads_and_converts_as_lcms_does() -> Outcome<()> { |
| 813 | let p = res!(system(GS, "default_rgb.icc")); |
| 814 | assert_eq!((p.head.major, p.head.minor), (2, 1)); |
| 815 | assert_eq!(p.head.class, Class::Display); |
| 816 | assert_eq!((p.head.space, p.head.pcs), (Space::Rgb, Space::Xyz)); |
| 817 | assert_eq!(sigs(&p), [b"desc", b"cprt", b"wtpt", b"bkpt", b"rXYZ", b"gXYZ", b"bXYZ", b"rTRC", b"gTRC", b"bTRC"].iter().map(|s| **s).collect::<Vec<Sig>>()); |
| 818 | assert_eq!(p.desc.as_deref(), Some("Artifex Software sRGB ICC Profile")); |
| 819 | assert!(matches!(&p.trcs[0], Some(Curve::Table(t)) if t.len() == 1024)); |
| 820 | let m = res!(RgbMatrix::read(&p)); |
| 821 | check_xyz(&m, CORNER_XYZ_GS, 5e-3, "default_rgb"); |
| 822 | let g = res!(Grey::read(&res!(system(GS, "default_gray.icc")))); |
| 823 | check_grey(&m, &g, CUBE_GREY_GS, RAMP_GREY, "default_rgb to default_gray"); |
| 824 | Ok(()) |
| 825 | } |
| 826 | |
| 827 | #[test] |
| 828 | #[ignore = "reads Ghostscript's AGPL profiles from /usr/share/color/icc/ghostscript"] |
| 829 | fn test_ghostscript_default_gray_reads_as_lcms_reports_it() -> Outcome<()> { |
| 830 | let p = res!(system(GS, "default_gray.icc")); |
| 831 | assert_eq!(p.head.class, Class::Display); |
| 832 | assert_eq!((p.head.space, p.head.pcs), (Space::Gray, Space::Xyz)); |
| 833 | assert_eq!(sigs(&p), [b"desc", b"cprt", b"wtpt", b"bkpt", b"kTRC"].iter().map(|s| **s).collect::<Vec<Sig>>()); |
| 834 | assert_eq!(p.desc.as_deref(), Some("Artifex Software sGray ICC Profile")); |
| 835 | let g = res!(Grey::read(&p)); |
| 836 | for (i, want) in RAMP_XYZ.iter().enumerate() { |
| 837 | near(g.y((i * 15) as f64 / 255.0) * 100.0, want[1], 5e-3, &fmt!("default_gray Y at {}", i * 15)); |
| 838 | } |
| 839 | Ok(()) |
| 840 | } |
| 841 | |
| 842 | #[test] |
| 843 | #[ignore = "reads Ghostscript's AGPL profiles from /usr/share/color/icc/ghostscript"] |
| 844 | fn test_ghostscript_default_cmyk_reads_both_lut_widths_as_lcms_does() -> Outcome<()> { |
| 845 | let p = res!(system(GS, "default_cmyk.icc")); |
| 846 | assert_eq!((p.head.major, p.head.minor), (2, 1)); |
| 847 | assert_eq!(p.head.class, Class::Output); |
| 848 | assert_eq!((p.head.space, p.head.pcs), (Space::Cmyk, Space::Lab)); |
| 849 | assert_eq!(sigs(&p), [b"desc", b"cprt", b"wtpt", b"A2B0", b"B2A0", b"A2B1", b"B2A1", b"A2B2", b"B2A2"].iter().map(|s| **s).collect::<Vec<Sig>>()); |
| 850 | assert_eq!(p.desc.as_deref(), Some("Artifex CMYK SWOP Profile")); |
| 851 | // A2B0 is a lut16, B2A0 a lut8. The three intents share one table, so the relative colorimetric |
| 852 | // fixtures (no black point compensation) are the nodes of A2B0 and B2A0 too. |
| 853 | for i in 1..3 { |
| 854 | let (a0, ai) = (res!(p.a2b[0].as_ref().ok_or_else(|| err!("No A2B0."; Missing))), res!(p.a2b[i].as_ref().ok_or_else(|| err!("No A2B{}.", i; Missing)))); |
| 855 | assert_eq!((&a0.clut, &a0.ins, &a0.outs), (&ai.clut, &ai.ins, &ai.outs)); |
| 856 | } |
| 857 | let a = res!(p.a2b[0].as_ref().ok_or_else(|| err!("No A2B0."; Missing))); |
| 858 | assert_eq!((a.bits, a.nin, a.nout, a.grid, a.ine, a.oute), (16, 4, 3, 9, 256, 2)); |
| 859 | let b = res!(p.b2a[0].as_ref().ok_or_else(|| err!("No B2A0."; Missing))); |
| 860 | assert_eq!((b.bits, b.nin, b.nout, b.grid, b.ine, b.oute), (8, 3, 4, 33, 256, 256)); |
| 861 | assert_eq!(b.clut.len(), 33 * 33 * 33 * 4); |
| 862 | for (k, want) in GS_CMYK_LAB.iter().enumerate() { |
| 863 | let out = res!(corner(a, &corner_idx(4, a.grid, k))); |
| 864 | let v = [out[0] * 65535.0, out[1] * 65535.0, out[2] * 65535.0]; |
| 865 | let lab = a.lab([v[0].round() as u16, v[1].round() as u16, v[2].round() as u16]); |
| 866 | for c in 0..3 { |
| 867 | near(lab[c], want[c], 0.01, &fmt!("default_cmyk A2B0 corner {} Lab[{}]", k, c)); |
| 868 | } |
| 869 | } |
| 870 | for (k, want) in GS_LAB_CMYK.iter().enumerate() { |
| 871 | let out = res!(corner(b, &corner_idx(3, b.grid, k))); |
| 872 | for c in 0..4 { |
| 873 | near(out[c] * 100.0, want[c], 0.05, &fmt!("default_cmyk B2A0 corner {} [{}]", k, c)); |
| 874 | } |
| 875 | } |
| 876 | Ok(()) |
| 877 | } |
| 878 | |
| 879 | #[test] |
| 880 | #[ignore = "reads colord's profiles from /usr/share/color/icc/colord"] |
| 881 | fn test_colord_srgb_v4_reads_its_parametric_curves() -> Outcome<()> { |
| 882 | let p = res!(system(COLORD, "sRGB.icc")); |
| 883 | assert_eq!(p.head.major, 4); |
| 884 | assert!(p.chad.is_some() && p.wtpt.is_some()); |
| 885 | assert!(matches!(p.trcs[1], Some(Curve::Para { .. }))); |
| 886 | assert!(p.desc.is_some()); |
| 887 | let m = res!(RgbMatrix::read(&p)); |
| 888 | let b = res!(builtin::srgb()); |
| 889 | for rgb in cube() { |
| 890 | let (x, y) = (m.xyz(rgb), b.xyz(rgb)); |
| 891 | for k in 0..3 { |
| 892 | near(x[k], y[k], 2e-3, &fmt!("colord sRGB vs built-in {:?} [{}]", rgb, k)); |
| 893 | } |
| 894 | } |
| 895 | Ok(()) |
| 896 | } |