Skip to main content

asset_io/
thumbnail.rs

1// Copyright 2026 Adobe. All rights reserved.
2// This file is licensed to you under the Apache License,
3// Version 2.0 (http://www.apache.org/licenses/LICENSE-2.0)
4// or the MIT license (http://opensource.org/licenses/MIT),
5// at your option.
6
7// Unless required by applicable law or agreed to in writing,
8// this software is distributed on an "AS IS" BASIS, WITHOUT
9// WARRANTIES OR REPRESENTATIONS OF ANY KIND, either express or
10// implied. See the LICENSE-MIT and LICENSE-APACHE files for the
11// specific language governing permissions and limitations under
12// each license.
13
14//! Embedded thumbnails, for any container.
15//!
16//! [`Structure::embedded_thumbnail`] works on every container because it does not
17//! look at the format. It uses the items the container's handler reported:
18//!
19//! 1. An item with a [`Thumbnail`](RelationKind::Thumbnail) relation: a
20//!    pre-rendered preview the container stores as its own item. Today that is a
21//!    HEIF item that is the source of a `thmb` reference, which the BMFF handler
22//!    finds (the `bmff` feature).
23//! 2. Otherwise, with the `exif` feature, the JPEG thumbnail in the EXIF data
24//!    (IFD1) of the first [`ItemKind::Exif`](crate::ItemKind::Exif) item. JPEG,
25//!    PNG, WebP and HEIF/AVIF all report EXIF, and the TIFF parsing lives in the
26//!    `tiff` module.
27//!
28//! So handlers only say where things are; finding the thumbnail inside them is
29//! done here, once.
30
31use std::{
32    borrow::Cow,
33    io::{Read, Seek},
34};
35
36use crate::{error::Result, item::RelationKind, structure::Structure};
37
38/// An embedded thumbnail with its data
39///
40/// This struct contains the actual thumbnail bytes extracted from an image file.
41#[derive(Debug, Clone)]
42#[non_exhaustive]
43pub struct Thumbnail {
44    /// The raw thumbnail data, in the format [`media_type`](Self::media_type) names
45    pub data: Vec<u8>,
46
47    media_type: Option<Cow<'static, str>>,
48
49    /// Width in pixels (if known)
50    pub width: Option<u32>,
51
52    /// Height in pixels (if known)
53    pub height: Option<u32>,
54}
55
56impl Thumbnail {
57    /// A thumbnail with data of unknown media type and size.
58    pub fn new(data: Vec<u8>) -> Self {
59        Self {
60            data,
61            media_type: None,
62            width: None,
63            height: None,
64        }
65    }
66
67    /// Set the media type of the data, for example `image/jpeg`.
68    pub fn with_media_type(mut self, media_type: impl Into<Cow<'static, str>>) -> Self {
69        self.media_type = Some(media_type.into());
70        self
71    }
72
73    /// Set the size in pixels.
74    pub fn with_dimensions(mut self, width: u32, height: u32) -> Self {
75        self.width = Some(width);
76        self.height = Some(height);
77        self
78    }
79
80    /// Media type of the data, or `None` when the container cannot tell (a coded
81    /// HEIF item such as `hvc1`, which is a bitstream and not a file). Reported the
82    /// same way as [`SubAsset::media_type`](crate::SubAsset::media_type).
83    pub fn media_type(&self) -> Option<&str> {
84        self.media_type.as_deref()
85    }
86}
87
88impl Structure {
89    /// The asset's embedded thumbnail, if it has one.
90    ///
91    /// An item the container records as a thumbnail (one with a
92    /// [`Thumbnail`](RelationKind::Thumbnail) relation, from a HEIF `thmb`
93    /// reference) is preferred, whatever its [kind](crate::Item::kind). Its media
94    /// type is the one the container reports for it (see
95    /// [`Item::media_type`](crate::Item::media_type)):
96    /// `image/jpeg` for a `jpeg` item, the content type of a `mime` item. For a
97    /// coded item (`hvc1`, `av01`, ...) it is `None`, and `data` is the coded item,
98    /// which needs the
99    /// codec configuration stored elsewhere in the file to decode. Failing that,
100    /// with the `exif` feature, the JPEG thumbnail in the EXIF data (IFD1) is
101    /// returned.
102    pub fn embedded_thumbnail<R: Read + Seek>(&self, src: &mut R) -> Result<Option<Thumbnail>> {
103        let thumb = self.items().find(|e| {
104            e.relations()
105                .iter()
106                .any(|r| r.kind() == RelationKind::Thumbnail)
107        });
108        if let Some(e) = thumb {
109            let mut t = Thumbnail::new(self.read_item(src, e)?);
110            if let Some(m) = e.media_type() {
111                t = t.with_media_type(m.to_owned());
112            }
113            return Ok(Some(t));
114        }
115        #[cfg(feature = "exif")]
116        {
117            self.exif_thumbnail(src)
118        }
119        #[cfg(not(feature = "exif"))]
120        {
121            Ok(None)
122        }
123    }
124}