c2pa 0.80.1

Rust SDK for C2PA (Coalition for Content Provenance and Authenticity) implementors
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
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
// Copyright 2022 Adobe. All rights reserved.
// This file is licensed to you under the Apache License,
// Version 2.0 (http://www.apache.org/licenses/LICENSE-2.0)
// or the MIT license (http://opensource.org/licenses/MIT),
// at your option.

// Unless required by applicable law or agreed to in writing,
// this software is distributed on an "AS IS" BASIS, WITHOUT
// WARRANTIES OR REPRESENTATIONS OF ANY KIND, either express or
// implied. See the LICENSE-MIT and LICENSE-APACHE files for the
// specific language governing permissions and limitations under
// each license.

use std::{
    fmt,
    io::{BufRead, Cursor, Seek, Write},
};

use image::{
    codecs::{
        jpeg::JpegEncoder,
        png::{CompressionType, FilterType, PngEncoder},
    },
    DynamicImage, ImageDecoder, ImageFormat, ImageReader,
};

use crate::{
    settings::{
        builder::{ThumbnailFormat, ThumbnailQuality},
        Settings,
    },
    Error, Result,
};

impl ThumbnailFormat {
    /// Create a new [ThumbnailFormat] from the given format extension or mime type.
    ///
    /// If the format is unsupported, this function will return `None`.
    pub fn new(format: &str) -> Option<ThumbnailFormat> {
        ImageFormat::from_extension(format)
            .or_else(|| ImageFormat::from_mime_type(format))
            .and_then(|format| ThumbnailFormat::try_from(format).ok())
    }
}

impl TryFrom<ImageFormat> for ThumbnailFormat {
    type Error = Error;

    fn try_from(format: ImageFormat) -> Result<Self> {
        match format {
            ImageFormat::Png => Ok(ThumbnailFormat::Png),
            ImageFormat::Jpeg => Ok(ThumbnailFormat::Jpeg),
            ImageFormat::Gif => Ok(ThumbnailFormat::Gif),
            ImageFormat::WebP => Ok(ThumbnailFormat::WebP),
            ImageFormat::Tiff => Ok(ThumbnailFormat::Tiff),
            _ => Err(Error::UnsupportedThumbnailFormat(
                format.to_mime_type().to_owned(),
            )),
        }
    }
}

impl From<ThumbnailFormat> for ImageFormat {
    fn from(format: ThumbnailFormat) -> Self {
        match format {
            ThumbnailFormat::Png => ImageFormat::Png,
            ThumbnailFormat::Jpeg => ImageFormat::Jpeg,
            ThumbnailFormat::Gif => ImageFormat::Gif,
            ThumbnailFormat::WebP => ImageFormat::WebP,
            ThumbnailFormat::Tiff => ImageFormat::Tiff,
        }
    }
}

impl From<ThumbnailFormat> for config::ValueKind {
    fn from(value: ThumbnailFormat) -> Self {
        let variant = match value {
            ThumbnailFormat::Png => "png",
            ThumbnailFormat::Jpeg => "jpeg",
            ThumbnailFormat::Gif => "gif",
            ThumbnailFormat::WebP => "webp",
            ThumbnailFormat::Tiff => "tiff",
        };
        config::ValueKind::String(variant.to_owned())
    }
}

impl fmt::Display for ThumbnailFormat {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}", ImageFormat::from(*self).to_mime_type())
    }
}

/// Make a thumbnail from an input stream and format and return the output format and new thumbnail bytes.
///
/// This function takes into account the [Settings][crate::Settings]:
/// * `builder.thumbnail.ignore_errors`
///
/// Read [make_thumbnail_from_stream] for more information.
pub fn make_thumbnail_bytes_from_stream<R>(
    format: &str,
    input: R,
    settings: &Settings,
) -> Result<Option<(ThumbnailFormat, Vec<u8>)>>
where
    R: BufRead + Seek,
{
    let result = {
        match ThumbnailFormat::new(format) {
            Some(input_format) => {
                let mut output = Cursor::new(Vec::new());
                make_thumbnail_from_stream(input_format, None, input, &mut output, settings)
                    .map(|output_format| (output_format, output.into_inner()))
            }
            None => Err(Error::UnsupportedThumbnailFormat(format.to_owned())),
        }
    };

    let ignore_errors = settings.builder.thumbnail.ignore_errors;
    match result {
        Ok(result) => Ok(Some(result)),
        Err(_) if ignore_errors => Ok(None),
        Err(err) => Err(err),
    }
}

/// Make a thumbnail from the input stream and write to the output stream.
///
/// This function takes into account two [Settings][crate::Settings]:
/// * `builder.thumbnail.long_edge`
/// * `builder.thumbnail.quality`
/// * `builder.thumbnail.format`
/// * `builder.thumbnail.prefer_smallest_format`
pub fn make_thumbnail_from_stream<R, W>(
    input_format: ThumbnailFormat,
    output_format: Option<ThumbnailFormat>,
    input: R,
    output: &mut W,
    settings: &Settings,
) -> Result<ThumbnailFormat>
where
    R: BufRead + Seek,
    W: Write + Seek,
{
    let mut decoder = ImageReader::with_format(input, input_format.into()).into_decoder()?;
    let orientation = decoder.orientation()?;

    let mut image = DynamicImage::from_decoder(decoder)?;
    image.apply_orientation(orientation);

    let output_format = match output_format {
        Some(output_format) => output_format,
        None => {
            match settings.builder.thumbnail.format {
                Some(global_format) => global_format,
                None => {
                    let prefer_smallest_format = settings.builder.thumbnail.prefer_smallest_format;
                    match prefer_smallest_format {
                        true => match input_format {
                            // TODO: investigate more formats
                            ThumbnailFormat::Png | ThumbnailFormat::Tiff
                                if !image.color().has_alpha() =>
                            {
                                ThumbnailFormat::Jpeg
                            }
                            _ => input_format,
                        },
                        false => input_format,
                    }
                }
            }
        }
    };

    let long_edge = settings.builder.thumbnail.long_edge;
    image = image.thumbnail(long_edge, long_edge);

    let quality = settings.builder.thumbnail.quality;
    // TODO: investigate more formats
    match output_format {
        ThumbnailFormat::Jpeg => match quality {
            ThumbnailQuality::Low => {
                image.write_with_encoder(JpegEncoder::new_with_quality(output, 38))?
            }
            ThumbnailQuality::Medium => {
                image.write_with_encoder(JpegEncoder::new_with_quality(output, 75))?
            }
            ThumbnailQuality::High => {
                image.write_with_encoder(JpegEncoder::new_with_quality(output, 100))?
            }
        },
        ThumbnailFormat::Png => match quality {
            ThumbnailQuality::Low => image.write_with_encoder(PngEncoder::new_with_quality(
                output,
                CompressionType::Fast,
                FilterType::default(),
            ))?,
            ThumbnailQuality::Medium => image.write_with_encoder(PngEncoder::new_with_quality(
                output,
                CompressionType::Default,
                FilterType::default(),
            ))?,
            ThumbnailQuality::High => image.write_with_encoder(PngEncoder::new_with_quality(
                output,
                CompressionType::Best,
                FilterType::default(),
            ))?,
        },
        _ => image.write_to(output, output_format.into())?,
    }

    Ok(output_format)
}

#[cfg(test)]
pub mod tests {
    #![allow(clippy::unwrap_used)]

    use image::GenericImageView;

    use super::*;
    use crate::settings::Settings;

    const TEST_JPEG: &[u8] = include_bytes!("../../tests/fixtures/CA.jpg");
    const TEST_PNG: &[u8] = include_bytes!("../../tests/fixtures/sample1.png");

    fn create_test_jpeg_with_orientation(orientation: u16) -> Vec<u8> {
        use image::{ImageBuffer, Rgb, RgbImage};
        let width = 200;
        let height = 100;
        let mut img: RgbImage = ImageBuffer::new(width, height);

        for y in 0..height {
            for x in 0..width {
                let pixel = if x < width / 2 {
                    Rgb([255, 0, 0]) // Red on left half
                } else {
                    Rgb([0, 0, 255]) // Blue on right half
                };
                img.put_pixel(x, y, pixel);
            }
        }

        // Encode image to JPEG
        let mut jpeg_data = Vec::new();
        {
            let mut encoder =
                image::codecs::jpeg::JpegEncoder::new_with_quality(&mut jpeg_data, 90);
            encoder.encode_image(&img).unwrap();
        }

        // Insert EXIF orientation if not 1
        if orientation != 1 {
            let exif_data = vec![
                0xff,
                0xe1, // APP1 marker
                0x00,
                0x2c, // Length (44 bytes)
                0x45,
                0x78,
                0x69,
                0x66,
                0x00,
                0x00, // "Exif\0\0"
                0x49,
                0x49, // Little endian
                0x2a,
                0x00, // TIFF identifier
                0x08,
                0x00,
                0x00,
                0x00, // Offset to IFD
                0x01,
                0x00, // Number of entries
                0x12,
                0x01, // Orientation tag
                0x03,
                0x00, // SHORT type
                0x01,
                0x00,
                0x00,
                0x00, // Count
                orientation as u8,
                (orientation >> 8) as u8,
                0x00,
                0x00, // Value
                0x00,
                0x00,
                0x00,
                0x00, // Next IFD offset
            ];
            // Insert EXIF after SOI marker
            jpeg_data.splice(2..2, exif_data);
        }

        jpeg_data
    }

    #[test]
    fn test_make_thumbnail_exif_orientation_issue() {
        // Create test JPEGs with different orientations
        for orientation in 1..=8 {
            let jpeg_data = create_test_jpeg_with_orientation(orientation);

            // Generate thumbnail from stream
            let mut cursor = std::io::Cursor::new(&jpeg_data);
            let result = make_thumbnail_bytes_from_stream("jpg", &mut cursor, &Settings::default());
            assert!(
                result.is_ok(),
                "Thumbnail should be generated for orientation {orientation}"
            );

            let (format, thumbnail_data) = result.unwrap().unwrap();
            assert_eq!(format, ThumbnailFormat::Jpeg);

            let thumb = image::load_from_memory(&thumbnail_data).unwrap();

            // Select pixels from the corners
            let top_left_pixel = thumb.get_pixel(0, 0);
            let bottom_right_pixel = thumb.get_pixel(thumb.width() - 1, thumb.height() - 1);

            match orientation {
                1 | 4 | 5 | 6 => {
                    // 1 Normal, 2 flipped vertically
                    // RB
                    // RB
                    // 5 Rotated 90 CW then flipped horizontally, 6 Rotated 90 CW
                    // RR
                    // BB
                    assert!(
                        top_left_pixel.0[0] >= 250,
                        "Top-left should be red for orientation {orientation}"
                    );
                    assert!(
                        bottom_right_pixel.0[2] >= 250,
                        "Bottom-right should be blue for orientation {orientation}"
                    );
                }
                2 | 3 | 7 | 8 => {
                    // 2 Flipped horizontally, 3 rotated 180
                    // BR
                    // BR
                    // 7 Rotated 90 CCW then flipped horizontally, 8 rotated 90 CCW
                    // BB
                    // RR
                    assert!(
                        top_left_pixel.0[2] >= 250,
                        "Top-left should be blue for orientation {orientation}"
                    );
                    assert!(
                        bottom_right_pixel.0[0] >= 250,
                        "Bottom-right should be red for orientation {orientation}"
                    );
                }
                _ => unreachable!("Unexpected orientation value: {orientation}"),
            }
        }
    }

    #[test]
    fn test_make_thumbnail_from_stream() {
        let mut settings = Settings::default();
        settings.builder.thumbnail.prefer_smallest_format = false;
        settings.builder.thumbnail.ignore_errors = false;
        settings.builder.thumbnail.format = None;

        let mut output = Cursor::new(Vec::new());
        let format = make_thumbnail_from_stream(
            ThumbnailFormat::Jpeg,
            None,
            Cursor::new(TEST_JPEG),
            &mut output,
            &settings,
        )
        .unwrap();

        assert!(matches!(format, ThumbnailFormat::Jpeg));

        output.rewind().unwrap();
        ImageReader::with_format(output, format.into())
            .decode()
            .unwrap();
    }

    #[test]
    fn test_make_thumbnail_from_stream_with_output() {
        let mut settings = Settings::default();
        settings.builder.thumbnail.ignore_errors = false;

        let mut output = Cursor::new(Vec::new());
        let format = make_thumbnail_from_stream(
            ThumbnailFormat::Jpeg,
            Some(ThumbnailFormat::Png),
            Cursor::new(TEST_JPEG),
            &mut output,
            &settings,
        )
        .unwrap();

        assert!(matches!(format, ThumbnailFormat::Png));

        output.rewind().unwrap();
        ImageReader::with_format(output, format.into())
            .decode()
            .unwrap();
    }

    #[test]
    fn test_make_thumbnail_bytes_from_stream() {
        let settings = Settings::new()
            .with_toml(
                &toml::toml! {
                    [builder.thumbnail]
                    prefer_smallest_format = false
                    ignore_errors = false
                }
                .to_string(),
            )
            .unwrap();

        let (format, bytes) =
            make_thumbnail_bytes_from_stream("image/jpeg", Cursor::new(TEST_JPEG), &settings)
                .unwrap()
                .unwrap();

        assert!(matches!(format, ThumbnailFormat::Jpeg));

        ImageReader::with_format(Cursor::new(bytes), format.into())
            .decode()
            .unwrap();
    }

    #[test]
    fn test_make_thumbnail_with_prefer_smallest_format() {
        let settings = Settings::new()
            .with_toml(
                &toml::toml! {
                    [builder.thumbnail]
                    prefer_smallest_format = true
                    ignore_errors = false
                }
                .to_string(),
            )
            .unwrap();

        let (format, bytes) =
            make_thumbnail_bytes_from_stream("image/png", Cursor::new(TEST_PNG), &settings)
                .unwrap()
                .unwrap();

        assert!(matches!(format, ThumbnailFormat::Jpeg));

        ImageReader::with_format(Cursor::new(bytes), format.into())
            .decode()
            .unwrap();
    }

    #[test]
    fn test_make_thumbnail_with_forced_format() {
        let mut settings = Settings::default();
        settings.builder.thumbnail.format = Some(ThumbnailFormat::Png);
        settings.builder.thumbnail.ignore_errors = false;

        let (format, bytes) =
            make_thumbnail_bytes_from_stream("image/jpeg", Cursor::new(TEST_JPEG), &settings)
                .unwrap()
                .unwrap();

        assert!(matches!(format, ThumbnailFormat::Png));

        ImageReader::with_format(Cursor::new(bytes), format.into())
            .decode()
            .unwrap();
    }

    #[test]
    fn test_make_thumbnail_with_long_edge() {
        let mut settings = Settings::default();
        settings.builder.thumbnail.ignore_errors = false;
        settings.builder.thumbnail.long_edge = 100;

        let (format, bytes) =
            make_thumbnail_bytes_from_stream("image/jpeg", Cursor::new(TEST_JPEG), &settings)
                .unwrap()
                .unwrap();

        assert!(matches!(format, ThumbnailFormat::Jpeg));

        let image = ImageReader::with_format(Cursor::new(bytes), format.into())
            .decode()
            .unwrap();
        assert!(image.width() == 100 || image.height() == 100);
    }

    #[test]
    fn test_make_thumbnail_and_ignore_errors() {
        let settings = Settings::new()
            .with_toml(
                &toml::toml! {
                    [builder.thumbnail]
                    ignore_errors = true
                }
                .to_string(),
            )
            .unwrap();

        let thumbnail =
            make_thumbnail_bytes_from_stream("image/png", Cursor::new(Vec::new()), &settings)
                .unwrap();
        assert!(thumbnail.is_none());
    }
}