Skip to content

Commit 85228c1

Browse files
committed
Add ThumbnailFormat
1 parent ca07cf7 commit 85228c1

File tree

1 file changed

+21
-1
lines changed

1 file changed

+21
-1
lines changed

libraries/rawkit/src/lib.rs

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,15 @@ use tiff::{Ifd, TiffError};
1919
pub(crate) const CHANNELS_IN_RGB: usize = 3;
2020
pub(crate) type Histogram = [[usize; 0x2000]; CHANNELS_IN_RGB];
2121

22+
pub enum ThumbnailFormat {
23+
Jpeg,
24+
Unsupported,
25+
}
26+
2227
/// A thumbnail image extracted from the raw file. This is usually a JPEG image.
2328
pub struct ThumbnailImage {
2429
pub data: Vec<u8>,
30+
pub format: ThumbnailFormat,
2531
}
2632

2733
/// The amount of black level to be subtracted from Raw Image.
@@ -150,18 +156,30 @@ impl RawImage {
150156
Ok(raw_image)
151157
}
152158

159+
/// Extracts the thumbnail image from the raw file.
153160
pub fn extract_thumbnail<R: Read + Seek>(reader: &mut R) -> Result<ThumbnailImage, DecoderError> {
154161
let mut file = TiffRead::new(reader)?;
155162
let ifd = Ifd::new_first_ifd(&mut file)?;
156163

164+
// TODO: ARW files Store the thumbnail offset and length in the first IFD. Add support for other file types in the future.
157165
let thumbnail_offset = ifd.get_value::<ThumbnailOffset, _>(&mut file)?;
158166
let thumbnail_length = ifd.get_value::<ThumbnailLength, _>(&mut file)?;
159167
file.seek_from_start(thumbnail_offset)?;
160168

161169
let mut thumbnail_data = vec![0; thumbnail_length as usize];
162170
file.read_exact(&mut thumbnail_data)?;
163171

164-
Ok(ThumbnailImage { data: thumbnail_data })
172+
// Check the first two bytes to determine the format of the thumbnail.
173+
// JPEG format starts with 0xFF, 0xD8.
174+
if thumbnail_data[0..2] == [0xFF, 0xD8] {
175+
return Ok(ThumbnailImage {
176+
data: thumbnail_data,
177+
format: ThumbnailFormat::Jpeg,
178+
});
179+
} else {
180+
Err(DecoderError::UnsupportedThumbnailFormat)
181+
}
182+
165183
}
166184

167185
/// Converts the [`RawImage`] to an [`Image`] with 8 bit resolution for each channel.
@@ -278,4 +296,6 @@ pub enum DecoderError {
278296
ConversionError(#[from] std::num::TryFromIntError),
279297
#[error("An IO Error ocurred")]
280298
IoError(#[from] std::io::Error),
299+
#[error("The thumbnail format is unsupported")]
300+
UnsupportedThumbnailFormat,
281301
}

0 commit comments

Comments
 (0)