1use crate::WarningSinkFn;
4use crate::cache::{Cache, CacheKey};
5use crate::function::Function;
6use crate::util::decode_or_warn;
7use log::warn;
8use moxcms::{
9 ColorProfile, DataColorSpace, Layout, RenderingIntent, Transform8BitExecutor,
10 TransformF32BitExecutor, TransformOptions, Xyzd,
11};
12use pdf_syntax::object;
13use pdf_syntax::object::Array;
14use pdf_syntax::object::Dict;
15use pdf_syntax::object::Name;
16use pdf_syntax::object::Object;
17use pdf_syntax::object::Stream;
18use pdf_syntax::object::dict::keys::*;
19use smallvec::{SmallVec, ToSmallVec, smallvec};
20use std::fmt::{Debug, Formatter};
21use std::ops::Deref;
22use std::sync::{Arc, OnceLock};
23
24static DEFAULT_CMYK_PROFILE: OnceLock<Option<ICCProfile>> = OnceLock::new();
27
28fn default_cmyk_profile() -> Option<&'static ICCProfile> {
29 DEFAULT_CMYK_PROFILE
30 .get_or_init(|| ICCProfile::new(include_bytes!("../assets/CGATS001Compat-v2-micro.icc"), 4))
31 .as_ref()
32}
33
34pub type ColorComponents = SmallVec<[f32; 4]>;
36
37#[derive(Debug, Copy, Clone)]
39pub struct AlphaColor {
40 components: [f32; 4],
41}
42
43impl AlphaColor {
44 pub const BLACK: Self = Self::new([0., 0., 0., 1.]);
46
47 pub const TRANSPARENT: Self = Self::new([0., 0., 0., 0.]);
49
50 pub const WHITE: Self = Self::new([1., 1., 1., 1.]);
52
53 pub const fn new(components: [f32; 4]) -> Self {
55 Self { components }
56 }
57
58 pub const fn from_rgb8(r: u8, g: u8, b: u8) -> Self {
60 let components = [u8_to_f32(r), u8_to_f32(g), u8_to_f32(b), 1.];
61 Self::new(components)
62 }
63
64 pub fn premultiplied(&self) -> [f32; 4] {
66 [
67 self.components[0] * self.components[3],
68 self.components[1] * self.components[3],
69 self.components[2] * self.components[3],
70 self.components[3],
71 ]
72 }
73
74 pub const fn from_rgba8(r: u8, g: u8, b: u8, a: u8) -> Self {
76 let components = [u8_to_f32(r), u8_to_f32(g), u8_to_f32(b), u8_to_f32(a)];
77 Self::new(components)
78 }
79
80 pub fn to_rgba8(&self) -> [u8; 4] {
82 [
83 (self.components[0] * 255.0 + 0.5) as u8,
84 (self.components[1] * 255.0 + 0.5) as u8,
85 (self.components[2] * 255.0 + 0.5) as u8,
86 (self.components[3] * 255.0 + 0.5) as u8,
87 ]
88 }
89
90 pub fn components(&self) -> [f32; 4] {
92 self.components
93 }
94}
95
96const fn u8_to_f32(x: u8) -> f32 {
97 x as f32 * (1.0 / 255.0)
98}
99
100#[derive(Debug, Clone)]
101pub(crate) enum ColorSpaceType {
102 DeviceCmyk,
103 DeviceGray,
104 DeviceRgb,
105 Pattern(ColorSpace),
106 Indexed(Indexed),
107 ICCBased(ICCProfile),
108 CalGray(CalGray),
109 CalRgb(CalRgb),
110 Lab(Lab),
111 Separation(Separation),
112 DeviceN(DeviceN),
113}
114
115impl ColorSpaceType {
116 fn new(object: Object<'_>, cache: &Cache, warning_sink: &WarningSinkFn) -> Option<Self> {
117 Self::new_inner(object, cache, warning_sink)
118 }
119
120 fn new_inner(object: Object<'_>, cache: &Cache, warning_sink: &WarningSinkFn) -> Option<Self> {
121 if let Some(name) = object.clone().into_name() {
122 return Self::new_from_name(name.clone());
123 } else if let Some(color_array) = object.clone().into_array() {
124 let mut iter = color_array.clone().flex_iter();
125 let name = iter.next::<Name>()?;
126
127 match name.deref() {
128 ICC_BASED => {
129 let icc_stream = iter.next::<Stream<'_>>()?;
130 let dict = icc_stream.dict();
131 let num_components = dict.get::<usize>(N);
136
137 return cache.get_or_insert_with(icc_stream.cache_key(), || {
138 let from_icc = num_components.and_then(|n| {
142 decode_or_warn(&icc_stream, warning_sink)
143 .as_ref()
144 .and_then(|decoded| {
145 ICCProfile::new(decoded, n).map(|icc| {
146 if icc.is_srgb() {
151 Self::DeviceRgb
152 } else {
153 Self::ICCBased(icc)
154 }
155 })
156 })
157 });
158
159 from_icc
160 .or_else(|| {
161 dict.get::<Object<'_>>(ALTERNATE)
162 .and_then(|o| Self::new(o, cache, warning_sink))
163 })
164 .or_else(|| match num_components {
165 Some(1) => Some(Self::DeviceGray),
166 Some(3) => Some(Self::DeviceRgb),
167 Some(4) => Some(Self::DeviceCmyk),
168 _ => None,
169 })
170 });
171 }
172 CALCMYK => return Some(Self::DeviceCmyk),
173 CALGRAY => {
174 let cal_dict = iter.next::<Dict<'_>>()?;
175 return Some(Self::CalGray(CalGray::new(&cal_dict)?));
176 }
177 CALRGB => {
178 let cal_dict = iter.next::<Dict<'_>>()?;
179 return Some(Self::CalRgb(CalRgb::new(&cal_dict)?));
180 }
181 DEVICE_RGB | RGB => return Some(Self::DeviceRgb),
182 DEVICE_GRAY | G => return Some(Self::DeviceGray),
183 DEVICE_CMYK | CMYK => return Some(Self::DeviceCmyk),
184 LAB => {
185 let lab_dict = iter.next::<Dict<'_>>()?;
186 return Some(Self::Lab(Lab::new(&lab_dict)?));
187 }
188 INDEXED | I => {
189 return Some(Self::Indexed(Indexed::new(
190 &color_array,
191 cache,
192 warning_sink,
193 )?));
194 }
195 SEPARATION => {
196 return Some(Self::Separation(Separation::new(
197 &color_array,
198 cache,
199 warning_sink,
200 )?));
201 }
202 DEVICE_N => {
203 return Some(Self::DeviceN(DeviceN::new(
204 &color_array,
205 cache,
206 warning_sink,
207 )?));
208 }
209 PATTERN => {
210 let cs = iter
213 .next::<Object<'_>>()
214 .and_then(|o| ColorSpace::new(o, cache, warning_sink))
215 .unwrap_or(ColorSpace::device_rgb());
216 return Some(Self::Pattern(cs));
217 }
218 _ => {
219 warn!("unsupported color space: {}", name.as_str());
220 return None;
221 }
222 }
223 }
224
225 None
226 }
227
228 fn new_from_name(name: Name) -> Option<Self> {
229 match name.deref() {
230 DEVICE_RGB | RGB => Some(Self::DeviceRgb),
231 DEVICE_GRAY | G => Some(Self::DeviceGray),
232 DEVICE_CMYK | CMYK => Some(Self::DeviceCmyk),
233 CALCMYK => Some(Self::DeviceCmyk),
234 PATTERN => Some(Self::Pattern(ColorSpace::device_rgb())),
235 _ => None,
236 }
237 }
238}
239
240#[derive(Debug, Clone)]
242pub struct ColorSpace(Arc<ColorSpaceType>);
243
244impl ColorSpace {
245 pub(crate) fn new(
247 object: Object<'_>,
248 cache: &Cache,
249 warning_sink: &WarningSinkFn,
250 ) -> Option<Self> {
251 Some(Self(Arc::new(ColorSpaceType::new(
252 object,
253 cache,
254 warning_sink,
255 )?)))
256 }
257
258 pub(crate) fn new_from_name(name: Name) -> Option<Self> {
260 ColorSpaceType::new_from_name(name).map(|c| Self(Arc::new(c)))
261 }
262
263 pub(crate) fn device_gray() -> Self {
265 Self(Arc::new(ColorSpaceType::DeviceGray))
266 }
267
268 pub(crate) fn device_rgb() -> Self {
270 Self(Arc::new(ColorSpaceType::DeviceRgb))
271 }
272
273 pub(crate) fn device_cmyk() -> Self {
275 Self(Arc::new(ColorSpaceType::DeviceCmyk))
276 }
277
278 pub fn is_device_rgb(&self) -> bool {
280 matches!(*self.0, ColorSpaceType::DeviceRgb)
281 }
282
283 pub fn is_device_cmyk(&self) -> bool {
285 matches!(*self.0, ColorSpaceType::DeviceCmyk)
286 }
287
288 pub(crate) fn pattern() -> Self {
290 Self(Arc::new(ColorSpaceType::Pattern(Self::device_gray())))
291 }
292
293 pub(crate) fn pattern_cs(&self) -> Option<Self> {
294 match self.0.as_ref() {
295 ColorSpaceType::Pattern(cs) => Some(cs.clone()),
296 _ => None,
297 }
298 }
299
300 pub(crate) fn is_pattern(&self) -> bool {
302 matches!(self.0.as_ref(), ColorSpaceType::Pattern(_))
303 }
304
305 pub(crate) fn is_indexed(&self) -> bool {
307 matches!(self.0.as_ref(), ColorSpaceType::Indexed(_))
308 }
309
310 pub(crate) fn default_decode_arr(&self, n: f32) -> SmallVec<[(f32, f32); 4]> {
312 match self.0.as_ref() {
313 ColorSpaceType::DeviceCmyk => smallvec![(0.0, 1.0), (0.0, 1.0), (0.0, 1.0), (0.0, 1.0)],
314 ColorSpaceType::DeviceGray => smallvec![(0.0, 1.0)],
315 ColorSpaceType::DeviceRgb => smallvec![(0.0, 1.0), (0.0, 1.0), (0.0, 1.0)],
316 ColorSpaceType::ICCBased(i) => smallvec![(0.0, 1.0); i.0.number_components],
317 ColorSpaceType::CalGray(_) => smallvec![(0.0, 1.0)],
318 ColorSpaceType::CalRgb(_) => smallvec![(0.0, 1.0), (0.0, 1.0), (0.0, 1.0)],
319 ColorSpaceType::Lab(l) => smallvec![
320 (0.0, 100.0),
321 (l.range[0], l.range[1]),
322 (l.range[2], l.range[3]),
323 ],
324 ColorSpaceType::Indexed(_) => smallvec![(0.0, 2.0_f32.powf(n) - 1.0)],
325 ColorSpaceType::Separation(_) => smallvec![(0.0, 1.0)],
326 ColorSpaceType::DeviceN(d) => smallvec![(0.0, 1.0); d.num_components as usize],
327 ColorSpaceType::Pattern(_) => smallvec![(0.0, 1.0)],
329 }
330 }
331
332 pub(crate) fn initial_color(&self) -> ColorComponents {
334 match self.0.as_ref() {
335 ColorSpaceType::DeviceCmyk => smallvec![0.0, 0.0, 0.0, 1.0],
336 ColorSpaceType::DeviceGray => smallvec![0.0],
337 ColorSpaceType::DeviceRgb => smallvec![0.0, 0.0, 0.0],
338 ColorSpaceType::ICCBased(icc) => match icc.0.number_components {
339 1 => smallvec![0.0],
340 3 => smallvec![0.0, 0.0, 0.0],
341 4 => smallvec![0.0, 0.0, 0.0, 1.0],
342 _ => unreachable!(),
343 },
344 ColorSpaceType::CalGray(_) => smallvec![0.0],
345 ColorSpaceType::CalRgb(_) => smallvec![0.0, 0.0, 0.0],
346 ColorSpaceType::Lab(_) => smallvec![0.0, 0.0, 0.0],
347 ColorSpaceType::Indexed(_) => smallvec![0.0],
348 ColorSpaceType::Separation(_) => smallvec![1.0],
349 ColorSpaceType::Pattern(c) => c.initial_color(),
350 ColorSpaceType::DeviceN(d) => smallvec![1.0; d.num_components as usize],
351 }
352 }
353
354 pub(crate) fn num_components(&self) -> u8 {
356 match self.0.as_ref() {
357 ColorSpaceType::DeviceCmyk => 4,
358 ColorSpaceType::DeviceGray => 1,
359 ColorSpaceType::DeviceRgb => 3,
360 ColorSpaceType::ICCBased(icc) => icc.0.number_components as u8,
361 ColorSpaceType::CalGray(_) => 1,
362 ColorSpaceType::CalRgb(_) => 3,
363 ColorSpaceType::Lab(_) => 3,
364 ColorSpaceType::Indexed(_) => 1,
365 ColorSpaceType::Separation(_) => 1,
366 ColorSpaceType::Pattern(p) => p.num_components(),
367 ColorSpaceType::DeviceN(d) => d.num_components,
368 }
369 }
370
371 pub fn to_rgba(&self, c: &[f32], opacity: f32, manual_scale: bool) -> AlphaColor {
373 self.to_alpha_color(c, opacity, manual_scale)
374 .unwrap_or(AlphaColor::BLACK)
375 }
376}
377
378impl ToRgb for ColorSpace {
379 fn convert_f32(&self, input: &[f32], output: &mut [u8], manual_scale: bool) -> Option<()> {
380 match self.0.as_ref() {
381 ColorSpaceType::DeviceCmyk => {
382 if let Some(profile) = default_cmyk_profile() {
388 return profile.convert_f32(input, output, manual_scale);
389 }
390 for (input, output) in input.chunks_exact(4).zip(output.chunks_exact_mut(3)) {
392 let (c, m, y, k) = (input[0], input[1], input[2], input[3]);
393 output[0] = f32_to_u8(1.0 - (c + k).min(1.0));
394 output[1] = f32_to_u8(1.0 - (m + k).min(1.0));
395 output[2] = f32_to_u8(1.0 - (y + k).min(1.0));
396 }
397 Some(())
398 }
399 ColorSpaceType::DeviceGray => {
400 let converted = input.iter().copied().map(f32_to_u8).collect::<Vec<_>>();
401
402 for (input, output) in converted.iter().zip(output.chunks_exact_mut(3)) {
403 output.copy_from_slice(&[*input, *input, *input]);
404 }
405
406 Some(())
407 }
408 ColorSpaceType::DeviceRgb => {
409 for (input, output) in input.iter().copied().zip(output) {
410 *output = f32_to_u8(input);
411 }
412
413 Some(())
414 }
415 ColorSpaceType::Pattern(i) => i.convert_f32(input, output, manual_scale),
416 ColorSpaceType::Indexed(i) => i.convert_f32(input, output, manual_scale),
417 ColorSpaceType::ICCBased(i) => i.convert_f32(input, output, manual_scale),
418 ColorSpaceType::CalGray(i) => i.convert_f32(input, output, manual_scale),
419 ColorSpaceType::CalRgb(i) => i.convert_f32(input, output, manual_scale),
420 ColorSpaceType::Lab(i) => i.convert_f32(input, output, manual_scale),
421 ColorSpaceType::Separation(i) => i.convert_f32(input, output, manual_scale),
422 ColorSpaceType::DeviceN(i) => i.convert_f32(input, output, manual_scale),
423 }
424 }
425
426 fn supports_u8(&self) -> bool {
427 match self.0.as_ref() {
428 ColorSpaceType::DeviceCmyk => true,
429 ColorSpaceType::DeviceGray => true,
430 ColorSpaceType::DeviceRgb => true,
431 ColorSpaceType::Pattern(i) => i.supports_u8(),
432 ColorSpaceType::Indexed(i) => i.supports_u8(),
433 ColorSpaceType::ICCBased(i) => i.supports_u8(),
434 ColorSpaceType::CalGray(i) => i.supports_u8(),
435 ColorSpaceType::CalRgb(i) => i.supports_u8(),
436 ColorSpaceType::Lab(i) => i.supports_u8(),
437 ColorSpaceType::Separation(i) => i.supports_u8(),
438 ColorSpaceType::DeviceN(i) => i.supports_u8(),
439 }
440 }
441
442 fn convert_u8(&self, input: &[u8], output: &mut [u8]) -> Option<()> {
443 match self.0.as_ref() {
444 ColorSpaceType::DeviceCmyk => {
445 if let Some(profile) = default_cmyk_profile() {
447 return profile.convert_u8(input, output);
448 }
449 for (input, output) in input.chunks_exact(4).zip(output.chunks_exact_mut(3)) {
451 let (c, m, y, k) = (
452 input[0] as u16,
453 input[1] as u16,
454 input[2] as u16,
455 input[3] as u16,
456 );
457 output[0] = (255u16.saturating_sub(c + k)) as u8;
458 output[1] = (255u16.saturating_sub(m + k)) as u8;
459 output[2] = (255u16.saturating_sub(y + k)) as u8;
460 }
461 Some(())
462 }
463 ColorSpaceType::DeviceGray => {
464 for (input, output) in input.iter().zip(output.chunks_exact_mut(3)) {
465 output.copy_from_slice(&[*input, *input, *input]);
466 }
467
468 Some(())
469 }
470 ColorSpaceType::DeviceRgb => {
471 output.copy_from_slice(input);
472
473 Some(())
474 }
475 ColorSpaceType::Pattern(i) => i.convert_u8(input, output),
476 ColorSpaceType::Indexed(i) => i.convert_u8(input, output),
477 ColorSpaceType::ICCBased(i) => i.convert_u8(input, output),
478 ColorSpaceType::CalGray(i) => i.convert_u8(input, output),
479 ColorSpaceType::CalRgb(i) => i.convert_u8(input, output),
480 ColorSpaceType::Lab(i) => i.convert_u8(input, output),
481 ColorSpaceType::Separation(i) => i.convert_u8(input, output),
482 ColorSpaceType::DeviceN(i) => i.convert_u8(input, output),
483 }
484 }
485
486 fn is_none(&self) -> bool {
487 match self.0.as_ref() {
488 ColorSpaceType::Separation(s) => s.is_none(),
489 ColorSpaceType::DeviceN(d) => d.is_none(),
490 _ => false,
491 }
492 }
493}
494
495#[derive(Debug, Clone)]
496pub(crate) struct CalGray {
497 white_point: [f32; 3],
498 black_point: [f32; 3],
499 gamma: f32,
500}
501
502impl CalGray {
504 fn new(dict: &Dict<'_>) -> Option<Self> {
505 let white_point = dict.get::<[f32; 3]>(WHITE_POINT).unwrap_or([1.0, 1.0, 1.0]);
506 let black_point = dict.get::<[f32; 3]>(BLACK_POINT).unwrap_or([0.0, 0.0, 0.0]);
507 let gamma = dict.get::<f32>(GAMMA).unwrap_or(1.0);
508
509 Some(Self {
510 white_point,
511 black_point,
512 gamma,
513 })
514 }
515}
516
517impl ToRgb for CalGray {
518 fn convert_f32(&self, input: &[f32], output: &mut [u8], _: bool) -> Option<()> {
519 for (input, output) in input.iter().copied().zip(output.chunks_exact_mut(3)) {
520 let g = self.gamma;
521 let (_xw, yw, _zw) = {
522 let wp = self.white_point;
523 (wp[0], wp[1], wp[2])
524 };
525 let (_xb, _yb, _zb) = {
526 let bp = self.black_point;
527 (bp[0], bp[1], bp[2])
528 };
529
530 let a = input;
531 let ag = a.powf(g);
532 let l = yw * ag;
533 let val = (0.0_f32.max(295.8 * l.powf(0.333_333_34) - 40.8) + 0.5) as u8;
534
535 output.copy_from_slice(&[val, val, val]);
536 }
537
538 Some(())
539 }
540}
541
542#[derive(Debug, Clone)]
543pub(crate) struct CalRgb {
544 white_point: [f32; 3],
545 black_point: [f32; 3],
546 matrix: [f32; 9],
547 gamma: [f32; 3],
548}
549
550impl CalRgb {
555 fn new(dict: &Dict<'_>) -> Option<Self> {
556 let white_point = dict.get::<[f32; 3]>(WHITE_POINT).unwrap_or([1.0, 1.0, 1.0]);
557 let black_point = dict.get::<[f32; 3]>(BLACK_POINT).unwrap_or([0.0, 0.0, 0.0]);
558 let matrix = dict
559 .get::<[f32; 9]>(MATRIX)
560 .unwrap_or([1.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 1.0]);
561 let gamma = dict.get::<[f32; 3]>(GAMMA).unwrap_or([1.0, 1.0, 1.0]);
562
563 Some(Self {
564 white_point,
565 black_point,
566 matrix,
567 gamma,
568 })
569 }
570
571 const BRADFORD_SCALE_MATRIX: [f32; 9] = [
572 0.8951, 0.2664, -0.1614, -0.7502, 1.7135, 0.0367, 0.0389, -0.0685, 1.0296,
573 ];
574
575 const BRADFORD_SCALE_INVERSE_MATRIX: [f32; 9] = [
576 0.9869929, -0.1470543, 0.1599627, 0.4323053, 0.5183603, 0.0492912, -0.0085287, 0.0400428,
577 0.9684867,
578 ];
579
580 const SRGB_D65_XYZ_TO_RGB_MATRIX: [f32; 9] = [
581 3.2404542, -1.5371385, -0.4985314, -0.969_266, 1.8760108, 0.0415560, 0.0556434, -0.2040259,
582 1.0572252,
583 ];
584
585 const FLAT_WHITEPOINT: [f32; 3] = [1.0, 1.0, 1.0];
586 const D65_WHITEPOINT: [f32; 3] = [0.95047, 1.0, 1.08883];
587
588 fn decode_l_constant() -> f32 {
589 ((8.0_f32 + 16.0) / 116.0).powi(3) / 8.0
590 }
591
592 fn srgb_transfer_function(color: f32) -> f32 {
593 if color <= 0.0031308 {
594 (12.92 * color).clamp(0.0, 1.0)
595 } else if color >= 0.99554525 {
596 1.0
597 } else {
598 ((1.0 + 0.055) * color.powf(1.0 / 2.4) - 0.055).clamp(0.0, 1.0)
599 }
600 }
601
602 fn matrix_product(a: &[f32; 9], b: &[f32; 3]) -> [f32; 3] {
603 [
604 a[0] * b[0] + a[1] * b[1] + a[2] * b[2],
605 a[3] * b[0] + a[4] * b[1] + a[5] * b[2],
606 a[6] * b[0] + a[7] * b[1] + a[8] * b[2],
607 ]
608 }
609
610 fn to_flat(source_white_point: &[f32; 3], lms: &[f32; 3]) -> [f32; 3] {
611 [
612 lms[0] / source_white_point[0],
613 lms[1] / source_white_point[1],
614 lms[2] / source_white_point[2],
615 ]
616 }
617
618 fn to_d65(source_white_point: &[f32; 3], lms: &[f32; 3]) -> [f32; 3] {
619 [
620 lms[0] * Self::D65_WHITEPOINT[0] / source_white_point[0],
621 lms[1] * Self::D65_WHITEPOINT[1] / source_white_point[1],
622 lms[2] * Self::D65_WHITEPOINT[2] / source_white_point[2],
623 ]
624 }
625
626 fn decode_l(l: f32) -> f32 {
627 if l < 0.0 {
628 -Self::decode_l(-l)
629 } else if l > 8.0 {
630 ((l + 16.0) / 116.0).powi(3)
631 } else {
632 l * Self::decode_l_constant()
633 }
634 }
635
636 fn compensate_black_point(source_bp: &[f32; 3], xyz_flat: &[f32; 3]) -> [f32; 3] {
637 if source_bp == &[0.0, 0.0, 0.0] {
638 return *xyz_flat;
639 }
640
641 let zero_decode_l = Self::decode_l(0.0);
642
643 let mut out = [0.0; 3];
644 for i in 0..3 {
645 let src = Self::decode_l(source_bp[i]);
646 let scale = (1.0 - zero_decode_l) / (1.0 - src);
647 let offset = 1.0 - scale;
648 out[i] = xyz_flat[i] * scale + offset;
649 }
650
651 out
652 }
653
654 fn normalize_white_point_to_flat(
655 &self,
656 source_white_point: &[f32; 3],
657 xyz: &[f32; 3],
658 ) -> [f32; 3] {
659 if source_white_point[0] == 1.0 && source_white_point[2] == 1.0 {
660 return *xyz;
661 }
662 let lms = Self::matrix_product(&Self::BRADFORD_SCALE_MATRIX, xyz);
663 let lms_flat = Self::to_flat(source_white_point, &lms);
664 Self::matrix_product(&Self::BRADFORD_SCALE_INVERSE_MATRIX, &lms_flat)
665 }
666
667 fn normalize_white_point_to_d65(
668 &self,
669 source_white_point: &[f32; 3],
670 xyz: &[f32; 3],
671 ) -> [f32; 3] {
672 let lms = Self::matrix_product(&Self::BRADFORD_SCALE_MATRIX, xyz);
673 let lms_d65 = Self::to_d65(source_white_point, &lms);
674 Self::matrix_product(&Self::BRADFORD_SCALE_INVERSE_MATRIX, &lms_d65)
675 }
676}
677
678impl ToRgb for CalRgb {
679 fn convert_f32(&self, input: &[f32], output: &mut [u8], _: bool) -> Option<()> {
680 for (input, output) in input.chunks_exact(3).zip(output.chunks_exact_mut(3)) {
681 let input = [
682 input[0].clamp(0.0, 1.0),
683 input[1].clamp(0.0, 1.0),
684 input[2].clamp(0.0, 1.0),
685 ];
686
687 let [r, g, b] = input;
688 let [gr, gg, gb] = self.gamma;
689 let [agr, bgg, cgb] = [
690 if r == 1.0 { 1.0 } else { r.powf(gr) },
691 if g == 1.0 { 1.0 } else { g.powf(gg) },
692 if b == 1.0 { 1.0 } else { b.powf(gb) },
693 ];
694
695 let m = &self.matrix;
696 let x = m[0] * agr + m[3] * bgg + m[6] * cgb;
697 let y = m[1] * agr + m[4] * bgg + m[7] * cgb;
698 let z = m[2] * agr + m[5] * bgg + m[8] * cgb;
699 let xyz = [x, y, z];
700
701 let xyz_flat = self.normalize_white_point_to_flat(&self.white_point, &xyz);
702 let xyz_black = Self::compensate_black_point(&self.black_point, &xyz_flat);
703 let xyz_d65 = self.normalize_white_point_to_d65(&Self::FLAT_WHITEPOINT, &xyz_black);
704 let srgb_xyz = Self::matrix_product(&Self::SRGB_D65_XYZ_TO_RGB_MATRIX, &xyz_d65);
705
706 output.copy_from_slice(&[
707 (Self::srgb_transfer_function(srgb_xyz[0]) * 255.0 + 0.5) as u8,
708 (Self::srgb_transfer_function(srgb_xyz[1]) * 255.0 + 0.5) as u8,
709 (Self::srgb_transfer_function(srgb_xyz[2]) * 255.0 + 0.5) as u8,
710 ]);
711 }
712
713 Some(())
714 }
715}
716
717#[derive(Debug, Clone)]
718pub(crate) struct Lab {
719 range: [f32; 4],
720 profile: ICCProfile,
721}
722
723impl Lab {
724 fn new(dict: &Dict<'_>) -> Option<Self> {
725 let white_point = dict.get::<[f32; 3]>(WHITE_POINT).unwrap_or([1.0, 1.0, 1.0]);
726 let _black_point = dict.get::<[f32; 3]>(BLACK_POINT).unwrap_or([0.0, 0.0, 0.0]);
728 let range = dict
729 .get::<[f32; 4]>(RANGE)
730 .unwrap_or([-100.0, 100.0, -100.0, 100.0]);
731
732 let mut profile = ColorProfile::new_from_slice(include_bytes!("../assets/LAB.icc")).ok()?;
733 profile.white_point = Xyzd::new(
734 white_point[0] as f64,
735 white_point[1] as f64,
736 white_point[2] as f64,
737 );
738
739 let profile = ICCProfile::new_from_src_profile(
740 profile, false,
741 false, 3,
745 )?;
746
747 Some(Self { range, profile })
748 }
749}
750
751impl ToRgb for Lab {
752 fn convert_f32(&self, input: &[f32], output: &mut [u8], manual_scale: bool) -> Option<()> {
753 if !manual_scale {
754 let input = input
758 .chunks_exact(3)
759 .flat_map(|i| {
760 let l = i[0] / 100.0;
761 let a = (i[1] + 128.0) / 255.0;
762 let b = (i[2] + 128.0) / 255.0;
763
764 [l, a, b]
765 })
766 .collect::<Vec<_>>();
767
768 self.profile.convert_f32(&input, output, manual_scale)
769 } else {
770 self.profile.convert_f32(input, output, manual_scale)
771 }
772 }
773}
774
775#[derive(Debug, Clone)]
776pub(crate) struct Indexed {
777 values: Vec<Vec<f32>>,
778 hival: u8,
779 base: Box<ColorSpace>,
780}
781
782impl Indexed {
783 fn new(array: &Array<'_>, cache: &Cache, warning_sink: &WarningSinkFn) -> Option<Self> {
784 let mut iter = array.flex_iter();
785 let _ = iter.next::<Name>()?;
787 let base_color_space = ColorSpace::new(iter.next::<Object<'_>>()?, cache, warning_sink)?;
788 let hival = iter.next::<u8>()?;
789
790 let values = {
791 let data = iter
792 .next::<Stream<'_>>()
793 .and_then(|s| decode_or_warn(&s, warning_sink))
794 .or_else(|| iter.next::<object::String>().map(|s| s.to_vec()))?;
795
796 let num_components = base_color_space.num_components();
797
798 let mut byte_iter = data.iter().copied();
799
800 let mut vals = vec![];
801 for _ in 0..=hival {
802 let mut temp = vec![];
803
804 for _ in 0..num_components {
805 temp.push(byte_iter.next()? as f32 / 255.0);
806 }
807
808 vals.push(temp);
809 }
810
811 vals
812 };
813
814 Some(Self {
815 values,
816 hival,
817 base: Box::new(base_color_space),
818 })
819 }
820}
821
822impl ToRgb for Indexed {
823 fn convert_f32(&self, input: &[f32], output: &mut [u8], _: bool) -> Option<()> {
824 let mut indexed = vec![0.0; input.len() * self.base.num_components() as usize];
825
826 for (input, output) in input
827 .iter()
828 .copied()
829 .zip(indexed.chunks_exact_mut(self.base.num_components() as usize))
830 {
831 let idx = (input.clamp(0.0, self.hival as f32) + 0.5) as usize;
832 output.copy_from_slice(&self.values[idx]);
833 }
834
835 self.base.convert_f32(&indexed, output, true)
836 }
837}
838
839#[derive(Debug, Clone)]
840pub(crate) struct Separation {
841 alternate_space: ColorSpace,
842 tint_transform: Function,
843 is_none_separation: bool,
844}
845
846impl Separation {
847 fn new(array: &Array<'_>, cache: &Cache, warning_sink: &WarningSinkFn) -> Option<Self> {
848 let mut iter = array.flex_iter();
849 let _ = iter.next::<Name>()?;
851 let name = iter.next::<Name>()?;
852 let alternate_space = ColorSpace::new(iter.next::<Object<'_>>()?, cache, warning_sink)?;
853 let tint_transform = Function::new(&iter.next::<Object<'_>>()?)?;
854 let is_none_separation = name.as_str() == "None";
861
862 Some(Self {
863 alternate_space,
864 tint_transform,
865 is_none_separation,
866 })
867 }
868}
869
870impl ToRgb for Separation {
871 fn convert_f32(&self, input: &[f32], output: &mut [u8], _: bool) -> Option<()> {
872 let evaluated = input
873 .iter()
874 .flat_map(|n| {
875 self.tint_transform
876 .eval(smallvec![*n])
877 .unwrap_or(self.alternate_space.initial_color())
878 })
879 .collect::<Vec<_>>();
880 self.alternate_space.convert_f32(&evaluated, output, false)
881 }
882
883 fn is_none(&self) -> bool {
884 self.is_none_separation
885 }
886}
887
888#[derive(Debug, Clone)]
889pub(crate) struct DeviceN {
890 alternate_space: ColorSpace,
891 num_components: u8,
892 tint_transform: Function,
893 is_none: bool,
894}
895
896impl DeviceN {
897 fn new(array: &Array<'_>, cache: &Cache, warning_sink: &WarningSinkFn) -> Option<Self> {
898 let mut iter = array.flex_iter();
899 let _ = iter.next::<Name>()?;
901 let names = iter.next::<Array<'_>>()?.iter::<Name>().collect::<Vec<_>>();
902 let num_components = u8::try_from(names.len()).ok()?;
903 let all_none = names.iter().all(|n| n.as_str() == "None");
909 let alternate_space = ColorSpace::new(iter.next::<Object<'_>>()?, cache, warning_sink)?;
910 let tint_transform = Function::new(&iter.next::<Object<'_>>()?)?;
911
912 if num_components == 0 {
913 return None;
914 }
915
916 Some(Self {
917 alternate_space,
918 num_components,
919 tint_transform,
920 is_none: all_none,
921 })
922 }
923}
924
925impl ToRgb for DeviceN {
926 fn convert_f32(&self, input: &[f32], output: &mut [u8], _: bool) -> Option<()> {
927 let evaluated = input
928 .chunks_exact(self.num_components as usize)
929 .flat_map(|n| {
930 self.tint_transform
931 .eval(n.to_smallvec())
932 .unwrap_or(self.alternate_space.initial_color())
933 })
934 .collect::<Vec<_>>();
935 self.alternate_space.convert_f32(&evaluated, output, false)
936 }
937
938 fn is_none(&self) -> bool {
939 self.is_none
940 }
941}
942
943struct ICCColorRepr {
944 transform_u8: Box<Transform8BitExecutor>,
945 transform_f32: Box<TransformF32BitExecutor>,
946 number_components: usize,
947 is_srgb: bool,
948 is_lab: bool,
949}
950
951#[derive(Clone)]
952pub(crate) struct ICCProfile(Arc<ICCColorRepr>);
953
954impl Debug for ICCProfile {
955 fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
956 write!(f, "ICCColor {{..}}")
957 }
958}
959
960impl ICCProfile {
961 fn new(profile: &[u8], number_components: usize) -> Option<Self> {
962 let src_profile = ColorProfile::new_from_slice(profile).ok()?;
963
964 let profile_components = match src_profile.color_space {
972 DataColorSpace::Gray => 1,
973 DataColorSpace::Rgb
974 | DataColorSpace::Lab
975 | DataColorSpace::Luv
976 | DataColorSpace::Xyz
977 | DataColorSpace::YCbr
978 | DataColorSpace::Yxy
979 | DataColorSpace::Hsv
980 | DataColorSpace::Hls
981 | DataColorSpace::Cmy
982 | DataColorSpace::Color3 => 3,
983 DataColorSpace::Cmyk | DataColorSpace::Color4 => 4,
984 _ => {
985 warn!(
986 "unsupported ICC profile color space {:?}",
987 src_profile.color_space
988 );
989 return None;
990 }
991 };
992
993 if number_components != profile_components {
994 warn!(
995 "ICCBased /N={} does not match embedded ICC profile component count {}; using profile",
996 number_components, profile_components
997 );
998 }
999
1000 const SRGB_MARKER: &[u8] = b"sRGB";
1001
1002 let is_srgb = profile
1003 .get(52..56)
1004 .map(|device_model| device_model == SRGB_MARKER)
1005 .unwrap_or(false);
1006 let is_lab = src_profile.color_space == DataColorSpace::Lab;
1007
1008 Self::new_from_src_profile(src_profile, is_srgb, is_lab, profile_components)
1009 }
1010
1011 fn new_from_src_profile(
1012 src_profile: ColorProfile,
1013 is_srgb: bool,
1014 is_lab: bool,
1015 number_components: usize,
1016 ) -> Option<Self> {
1017 let dest_profile = ColorProfile::new_srgb();
1018
1019 let src_layout = match number_components {
1027 1 => Layout::Gray,
1028 3 => Layout::Rgb,
1029 4 => Layout::Rgba, _ => {
1031 warn!("unsupported number of components {number_components} for ICC profile");
1032
1033 return None;
1034 }
1035 };
1036
1037 let intents_to_try = [
1054 RenderingIntent::RelativeColorimetric,
1055 RenderingIntent::Perceptual,
1056 RenderingIntent::Saturation,
1057 ];
1058 let mut u8_transform = None;
1059 let mut f32_transform = None;
1060 for intent in intents_to_try {
1061 let options = TransformOptions {
1062 rendering_intent: intent,
1063 ..TransformOptions::default()
1064 };
1065 let u8_ok = src_profile
1066 .create_transform_8bit(src_layout, &dest_profile, Layout::Rgb, options)
1067 .ok();
1068 let f32_ok = src_profile
1069 .create_transform_f32(src_layout, &dest_profile, Layout::Rgb, options)
1070 .ok();
1071 if let (Some(u), Some(f)) = (u8_ok, f32_ok) {
1072 u8_transform = Some(u);
1073 f32_transform = Some(f);
1074 break;
1075 }
1076 }
1077 let u8_transform = u8_transform?;
1078 let f32_transform = f32_transform?;
1079
1080 Some(Self(Arc::new(ICCColorRepr {
1081 transform_u8: u8_transform,
1082 transform_f32: f32_transform,
1083 number_components,
1084 is_srgb,
1085 is_lab,
1086 })))
1087 }
1088
1089 fn is_srgb(&self) -> bool {
1090 self.0.is_srgb
1091 }
1092
1093 fn is_lab(&self) -> bool {
1094 self.0.is_lab
1095 }
1096}
1097
1098impl ToRgb for ICCProfile {
1099 fn convert_f32(&self, input: &[f32], output: &mut [u8], _: bool) -> Option<()> {
1100 let mut temp = vec![0.0_f32; output.len()];
1101
1102 if self.is_lab() {
1103 let scaled = input
1105 .chunks_exact(3)
1106 .flat_map(|i| {
1107 [
1108 i[0] * (1.0 / 100.0),
1109 (i[1] + 128.0) * (1.0 / 255.0),
1110 (i[2] + 128.0) * (1.0 / 255.0),
1111 ]
1112 })
1113 .collect::<Vec<_>>();
1114 self.0.transform_f32.transform(&scaled, &mut temp).ok()?;
1115 } else {
1116 self.0.transform_f32.transform(input, &mut temp).ok()?;
1117 };
1118
1119 for (input, output) in temp.iter().zip(output.iter_mut()) {
1120 *output = (input * 255.0 + 0.5) as u8;
1121 }
1122
1123 Some(())
1124 }
1125
1126 fn supports_u8(&self) -> bool {
1127 true
1128 }
1129
1130 fn convert_u8(&self, input: &[u8], output: &mut [u8]) -> Option<()> {
1131 if self.is_srgb() && input.len() == output.len() {
1138 output.copy_from_slice(input);
1139 } else {
1140 self.0.transform_u8.transform(input, output).ok()?;
1141 }
1142
1143 Some(())
1144 }
1145}
1146
1147#[inline(always)]
1148fn f32_to_u8(val: f32) -> u8 {
1149 (val * 255.0 + 0.5) as u8
1150}
1151
1152#[derive(Debug, Clone)]
1153pub struct Color {
1155 color_space: ColorSpace,
1156 components: ColorComponents,
1157 opacity: f32,
1158}
1159
1160impl Color {
1161 pub(crate) fn new(color_space: ColorSpace, components: ColorComponents, opacity: f32) -> Self {
1162 Self {
1163 color_space,
1164 components,
1165 opacity,
1166 }
1167 }
1168
1169 pub fn to_rgba(&self) -> AlphaColor {
1171 self.color_space
1172 .to_rgba(&self.components, self.opacity, false)
1173 }
1174
1175 pub fn from_rgba(rgba: AlphaColor) -> Self {
1177 let c = rgba.components();
1178 Self {
1179 color_space: ColorSpace::device_rgb(),
1180 components: smallvec![c[0], c[1], c[2]],
1181 opacity: c[3],
1182 }
1183 }
1184
1185 pub fn from_device_rgb(r: f32, g: f32, b: f32) -> Self {
1187 Self {
1188 color_space: ColorSpace::device_rgb(),
1189 components: smallvec![r, g, b],
1190 opacity: 1.0,
1191 }
1192 }
1193
1194 pub fn from_device_rgb_with_opacity(r: f32, g: f32, b: f32, opacity: f32) -> Self {
1196 Self {
1197 color_space: ColorSpace::device_rgb(),
1198 components: smallvec![r, g, b],
1199 opacity,
1200 }
1201 }
1202
1203 pub fn opacity(&self) -> f32 {
1205 self.opacity
1206 }
1207
1208 pub fn is_device_cmyk(&self) -> bool {
1210 self.color_space.is_device_cmyk()
1211 }
1212
1213 pub fn device_cmyk_components(&self) -> Option<[f32; 4]> {
1215 if !self.color_space.is_device_cmyk() || self.components.len() != 4 {
1216 return None;
1217 }
1218
1219 Some([
1220 self.components[0],
1221 self.components[1],
1222 self.components[2],
1223 self.components[3],
1224 ])
1225 }
1226}
1227
1228pub(crate) trait ToRgb {
1229 fn convert_f32(&self, input: &[f32], output: &mut [u8], manual_scale: bool) -> Option<()>;
1230 fn supports_u8(&self) -> bool {
1231 false
1232 }
1233 fn convert_u8(&self, _: &[u8], _: &mut [u8]) -> Option<()> {
1234 unimplemented!();
1235 }
1236 fn is_none(&self) -> bool {
1237 false
1238 }
1239 fn to_alpha_color(
1240 &self,
1241 input: &[f32],
1242 mut opacity: f32,
1243 manual_scale: bool,
1244 ) -> Option<AlphaColor> {
1245 let mut output = [0; 3];
1246 self.convert_f32(input, &mut output, manual_scale)?;
1247
1248 if self.is_none() {
1253 opacity = 0.0;
1254 }
1255
1256 Some(AlphaColor::from_rgba8(
1257 output[0],
1258 output[1],
1259 output[2],
1260 (opacity * 255.0 + 0.5) as u8,
1261 ))
1262 }
1263}
1264
1265#[cfg(test)]
1266mod tests {
1267 use super::*;
1268 use pdf_syntax::object::{Array, FromBytes};
1269
1270 fn separation_array(ink_name: &str) -> Vec<u8> {
1274 format!(
1275 "[/Separation /{ink_name} /DeviceGray \
1276 << /FunctionType 2 /Domain [0 1] /C0 [1] /C1 [0] /N 1 >> ]",
1277 ink_name = ink_name
1278 )
1279 .into_bytes()
1280 }
1281
1282 fn make_separation(ink_name: &str) -> Option<Separation> {
1283 let bytes = separation_array(ink_name);
1284 let array = Array::from_bytes(&bytes)?;
1285 let cache = Cache::new();
1286 {
1287 let sink: WarningSinkFn = std::sync::Arc::new(|_: crate::InterpreterWarning| {});
1288 Separation::new(&array, &cache, &sink)
1289 }
1290 }
1291
1292 #[test]
1294 fn none_ink_is_suppressed() {
1295 let sep = make_separation("None").expect("should parse");
1296 assert!(sep.is_none(), "ink name 'None' must be suppressed");
1297 }
1298
1299 #[test]
1301 fn pantone_ink_is_not_suppressed() {
1302 let sep = make_separation("PANTONE#20123#20CVC").expect("should parse");
1304 assert!(!sep.is_none(), "PANTONE spot colour must not be suppressed");
1305 }
1306
1307 #[test]
1309 fn all_ink_is_not_suppressed() {
1310 let sep = make_separation("All").expect("should parse");
1311 assert!(!sep.is_none(), "'All' separation must not be suppressed");
1312 }
1313
1314 #[test]
1316 fn pantone_produces_visible_color() {
1317 let sep = make_separation("PANTONE#20123#20CVC").expect("should parse");
1318 let cs = ColorSpace(Arc::new(ColorSpaceType::Separation(sep)));
1319 let color = cs.to_rgba(&[1.0], 1.0, false);
1321 assert!(
1322 color.to_rgba8()[3] > 0,
1323 "PANTONE ink at tint=1.0 must be opaque"
1324 );
1325 }
1326
1327 #[test]
1329 fn none_produces_transparent_color() {
1330 let sep = make_separation("None").expect("should parse");
1331 let cs = ColorSpace(Arc::new(ColorSpaceType::Separation(sep)));
1332 let color = cs.to_rgba(&[1.0], 1.0, false);
1333 assert_eq!(
1334 color.to_rgba8()[3],
1335 0,
1336 "Separation/None ink must be fully transparent"
1337 );
1338 }
1339}