oxidize-pdf 2.5.1

A pure Rust PDF generation and manipulation library with zero external dependencies
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
//! Calibrated color spaces for PDF graphics according to ISO 32000-1 Section 8.6.5
//!
//! This module provides CalGray and CalRGB color spaces which are device-independent
//! color spaces based on the CIE color model with calibration parameters.

use crate::objects::{Dictionary, Object};

/// CIE-based CalGray color space (ISO 32000-1 ยง8.6.5.2)
#[derive(Debug, Clone, PartialEq)]
pub struct CalGrayColorSpace {
    /// White point in CIE XYZ coordinates [Xw, Yw, Zw]
    /// Default is D50 standard illuminant
    pub white_point: [f64; 3],
    /// Black point in CIE XYZ coordinates [Xb, Yb, Zb]  
    /// Default is [0, 0, 0]
    pub black_point: [f64; 3],
    /// Gamma correction factor
    /// Default is 1.0 (no correction)
    pub gamma: f64,
}

impl Default for CalGrayColorSpace {
    fn default() -> Self {
        Self {
            white_point: [0.9505, 1.0000, 1.0890], // D50 standard illuminant
            black_point: [0.0, 0.0, 0.0],
            gamma: 1.0,
        }
    }
}

impl CalGrayColorSpace {
    /// Create a new CalGray color space with default parameters
    pub fn new() -> Self {
        Self::default()
    }

    /// Set the white point (CIE XYZ coordinates)
    pub fn with_white_point(mut self, white_point: [f64; 3]) -> Self {
        self.white_point = white_point;
        self
    }

    /// Set the black point (CIE XYZ coordinates)
    pub fn with_black_point(mut self, black_point: [f64; 3]) -> Self {
        self.black_point = black_point;
        self
    }

    /// Set the gamma correction factor
    pub fn with_gamma(mut self, gamma: f64) -> Self {
        self.gamma = gamma.max(0.0); // Gamma must be non-negative
        self
    }

    /// Common D50 illuminant
    pub fn d50() -> Self {
        Self::new() // Already uses D50 as default
    }

    /// Common D65 illuminant
    pub fn d65() -> Self {
        Self::new().with_white_point([0.9504, 1.0000, 1.0888])
    }

    /// Convert to PDF color space array
    pub fn to_pdf_array(&self) -> Vec<Object> {
        let mut array = vec![Object::Name("CalGray".to_string())];

        let mut dict = Dictionary::new();

        // White point (required)
        dict.set(
            "WhitePoint",
            Object::Array(self.white_point.iter().map(|&x| Object::Real(x)).collect()),
        );

        // Black point (optional, only include if not default)
        if self.black_point != [0.0, 0.0, 0.0] {
            dict.set(
                "BlackPoint",
                Object::Array(self.black_point.iter().map(|&x| Object::Real(x)).collect()),
            );
        }

        // Gamma (optional, only include if not 1.0)
        if self.gamma != 1.0 {
            dict.set("Gamma", Object::Real(self.gamma));
        }

        array.push(Object::Dictionary(dict));
        array
    }

    /// Apply gamma correction to a gray value
    pub fn apply_gamma(&self, gray: f64) -> f64 {
        gray.powf(self.gamma).clamp(0.0, 1.0)
    }
}

/// CIE-based CalRGB color space (ISO 32000-1 ยง8.6.5.3)
#[derive(Debug, Clone, PartialEq)]
pub struct CalRgbColorSpace {
    /// White point in CIE XYZ coordinates [Xw, Yw, Zw]
    pub white_point: [f64; 3],
    /// Black point in CIE XYZ coordinates [Xb, Yb, Zb]
    pub black_point: [f64; 3],
    /// Gamma correction factors for R, G, B channels
    pub gamma: [f64; 3],
    /// 3x3 transformation matrix from CalRGB to CIE XYZ
    /// Identity matrix is default
    pub matrix: [f64; 9],
}

impl Default for CalRgbColorSpace {
    fn default() -> Self {
        Self {
            white_point: [0.9505, 1.0000, 1.0890], // D50 standard illuminant
            black_point: [0.0, 0.0, 0.0],
            gamma: [1.0, 1.0, 1.0],
            // Identity matrix
            matrix: [1.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 1.0],
        }
    }
}

impl CalRgbColorSpace {
    /// Create a new CalRGB color space with default parameters
    pub fn new() -> Self {
        Self::default()
    }

    /// Set the white point (CIE XYZ coordinates)
    pub fn with_white_point(mut self, white_point: [f64; 3]) -> Self {
        self.white_point = white_point;
        self
    }

    /// Set the black point (CIE XYZ coordinates)  
    pub fn with_black_point(mut self, black_point: [f64; 3]) -> Self {
        self.black_point = black_point;
        self
    }

    /// Set gamma correction factors for R, G, B channels
    pub fn with_gamma(mut self, gamma: [f64; 3]) -> Self {
        self.gamma = [gamma[0].max(0.0), gamma[1].max(0.0), gamma[2].max(0.0)];
        self
    }

    /// Set the 3x3 transformation matrix from CalRGB to CIE XYZ
    pub fn with_matrix(mut self, matrix: [f64; 9]) -> Self {
        self.matrix = matrix;
        self
    }

    /// Common sRGB color space approximation
    pub fn srgb() -> Self {
        Self::new()
            .with_white_point([0.9505, 1.0000, 1.0890]) // D50
            .with_gamma([2.2, 2.2, 2.2]) // Approximate sRGB gamma
            .with_matrix([
                // sRGB to XYZ matrix (D50 adapted)
                0.4360, 0.3851, 0.1431, 0.2225, 0.7169, 0.0606, 0.0139, 0.0971, 0.7141,
            ])
    }

    /// Adobe RGB color space
    pub fn adobe_rgb() -> Self {
        Self::new()
            .with_white_point([0.9505, 1.0000, 1.0890]) // D50
            .with_gamma([2.2, 2.2, 2.2])
            .with_matrix([
                // Adobe RGB to XYZ matrix (D50 adapted)
                0.6097, 0.2053, 0.1492, 0.3111, 0.6256, 0.0633, 0.0195, 0.0609, 0.7441,
            ])
    }

    /// Common D65 illuminant
    pub fn d65() -> Self {
        Self::new().with_white_point([0.9504, 1.0000, 1.0888])
    }

    /// Convert to PDF color space array
    pub fn to_pdf_array(&self) -> Vec<Object> {
        let mut array = vec![Object::Name("CalRGB".to_string())];

        let mut dict = Dictionary::new();

        // White point (required)
        dict.set(
            "WhitePoint",
            Object::Array(self.white_point.iter().map(|&x| Object::Real(x)).collect()),
        );

        // Black point (optional, only include if not default)
        if self.black_point != [0.0, 0.0, 0.0] {
            dict.set(
                "BlackPoint",
                Object::Array(self.black_point.iter().map(|&x| Object::Real(x)).collect()),
            );
        }

        // Gamma (optional, only include if not all 1.0)
        if self.gamma != [1.0, 1.0, 1.0] {
            dict.set(
                "Gamma",
                Object::Array(self.gamma.iter().map(|&x| Object::Real(x)).collect()),
            );
        }

        // Matrix (optional, only include if not identity)
        let identity = [1.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 1.0];
        if self.matrix != identity {
            dict.set(
                "Matrix",
                Object::Array(self.matrix.iter().map(|&x| Object::Real(x)).collect()),
            );
        }

        array.push(Object::Dictionary(dict));
        array
    }

    /// Apply gamma correction to RGB values
    pub fn apply_gamma(&self, rgb: [f64; 3]) -> [f64; 3] {
        [
            rgb[0].powf(self.gamma[0]).clamp(0.0, 1.0),
            rgb[1].powf(self.gamma[1]).clamp(0.0, 1.0),
            rgb[2].powf(self.gamma[2]).clamp(0.0, 1.0),
        ]
    }

    /// Transform CalRGB values to CIE XYZ using the matrix
    pub fn to_xyz(&self, rgb: [f64; 3]) -> [f64; 3] {
        let gamma_corrected = self.apply_gamma(rgb);
        let [r, g, b] = gamma_corrected;

        [
            self.matrix[0] * r + self.matrix[1] * g + self.matrix[2] * b,
            self.matrix[3] * r + self.matrix[4] * g + self.matrix[5] * b,
            self.matrix[6] * r + self.matrix[7] * g + self.matrix[8] * b,
        ]
    }
}

/// Color value in a calibrated color space
#[derive(Debug, Clone, PartialEq)]
pub enum CalibratedColor {
    /// Gray value in CalGray color space
    Gray(f64, CalGrayColorSpace),
    /// RGB values in CalRGB color space  
    Rgb([f64; 3], CalRgbColorSpace),
}

impl CalibratedColor {
    /// Create a calibrated gray color
    pub fn cal_gray(value: f64, color_space: CalGrayColorSpace) -> Self {
        Self::Gray(value.clamp(0.0, 1.0), color_space)
    }

    /// Create a calibrated RGB color
    pub fn cal_rgb(rgb: [f64; 3], color_space: CalRgbColorSpace) -> Self {
        Self::Rgb(
            [
                rgb[0].clamp(0.0, 1.0),
                rgb[1].clamp(0.0, 1.0),
                rgb[2].clamp(0.0, 1.0),
            ],
            color_space,
        )
    }

    /// Get the color space array for PDF
    pub fn color_space_array(&self) -> Vec<Object> {
        match self {
            CalibratedColor::Gray(_, cs) => cs.to_pdf_array(),
            CalibratedColor::Rgb(_, cs) => cs.to_pdf_array(),
        }
    }

    /// Get the color values as an array
    pub fn values(&self) -> Vec<f64> {
        match self {
            CalibratedColor::Gray(value, _) => vec![*value],
            CalibratedColor::Rgb(rgb, _) => rgb.to_vec(),
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_cal_gray_default() {
        let cs = CalGrayColorSpace::new();

        assert_eq!(cs.white_point, [0.9505, 1.0000, 1.0890]);
        assert_eq!(cs.black_point, [0.0, 0.0, 0.0]);
        assert_eq!(cs.gamma, 1.0);
    }

    #[test]
    fn test_cal_gray_custom() {
        let cs = CalGrayColorSpace::new()
            .with_white_point([0.95, 1.0, 1.09])
            .with_black_point([0.01, 0.01, 0.01])
            .with_gamma(2.2);

        assert_eq!(cs.white_point, [0.95, 1.0, 1.09]);
        assert_eq!(cs.black_point, [0.01, 0.01, 0.01]);
        assert_eq!(cs.gamma, 2.2);
    }

    #[test]
    fn test_cal_gray_to_pdf() {
        let cs = CalGrayColorSpace::new()
            .with_gamma(2.2)
            .with_black_point([0.01, 0.01, 0.01]);

        let pdf_array = cs.to_pdf_array();

        assert_eq!(pdf_array.len(), 2);
        assert_eq!(pdf_array[0], Object::Name("CalGray".to_string()));

        if let Object::Dictionary(dict) = &pdf_array[1] {
            assert!(dict.get("WhitePoint").is_some());
            assert!(dict.get("Gamma").is_some());
            assert!(dict.get("BlackPoint").is_some());
        } else {
            panic!("Second element should be a dictionary");
        }
    }

    #[test]
    fn test_cal_rgb_default() {
        let cs = CalRgbColorSpace::new();

        assert_eq!(cs.white_point, [0.9505, 1.0000, 1.0890]);
        assert_eq!(cs.gamma, [1.0, 1.0, 1.0]);
    }

    #[test]
    fn test_cal_rgb_srgb() {
        let cs = CalRgbColorSpace::srgb();

        assert_eq!(cs.gamma, [2.2, 2.2, 2.2]);
        assert_ne!(cs.matrix, [1.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 1.0]);
    }

    #[test]
    fn test_cal_rgb_to_pdf() {
        let cs = CalRgbColorSpace::srgb();
        let pdf_array = cs.to_pdf_array();

        assert_eq!(pdf_array.len(), 2);
        assert_eq!(pdf_array[0], Object::Name("CalRGB".to_string()));

        if let Object::Dictionary(dict) = &pdf_array[1] {
            assert!(dict.get("WhitePoint").is_some());
            assert!(dict.get("Gamma").is_some());
            assert!(dict.get("Matrix").is_some());
        } else {
            panic!("Second element should be a dictionary");
        }
    }

    #[test]
    fn test_gamma_correction() {
        let cs = CalGrayColorSpace::new().with_gamma(2.2);

        let corrected = cs.apply_gamma(0.5);
        let expected = 0.5_f64.powf(2.2);

        assert!((corrected - expected).abs() < 1e-10);
    }

    #[test]
    fn test_rgb_gamma_correction() {
        let cs = CalRgbColorSpace::new().with_gamma([2.0, 2.2, 1.8]);

        let corrected = cs.apply_gamma([0.5, 0.6, 0.7]);
        let expected = [0.5_f64.powf(2.0), 0.6_f64.powf(2.2), 0.7_f64.powf(1.8)];

        for i in 0..3 {
            assert!((corrected[i] - expected[i]).abs() < 1e-10);
        }
    }

    #[test]
    fn test_calibrated_color() {
        let cs = CalGrayColorSpace::new().with_gamma(2.2);
        let color = CalibratedColor::cal_gray(0.5, cs);

        assert_eq!(color.values(), vec![0.5]);

        let array = color.color_space_array();
        assert_eq!(array[0], Object::Name("CalGray".to_string()));
    }

    #[test]
    fn test_xyz_transform() {
        let cs = CalRgbColorSpace::new();
        let xyz = cs.to_xyz([1.0, 0.0, 0.0]); // Pure red

        // Should transform according to the matrix
        assert_eq!(xyz[0], cs.matrix[0]); // First column, first row
        assert_eq!(xyz[1], cs.matrix[3]); // First column, second row
        assert_eq!(xyz[2], cs.matrix[6]); // First column, third row
    }
}