From 18956d7d10600afcbff464371cc32dddc1bb609f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ahmet=20Kaan=20G=C3=BCm=C3=BC=C5=9F?= Date: Wed, 2 Dec 2026 13:39:23 +0300 Subject: [PATCH] U20: ec-image one format_of() dispatcher for decode, info and decode_animation (same signature refusal) --- crates/ec-image/src/lib.rs | 51 +++++++++++++++++++------------------- 1 file changed, 26 insertions(+), 25 deletions(-) diff --git a/crates/ec-image/src/lib.rs b/crates/ec-image/src/lib.rs index cc8f4ec9..dca332e3 100644 --- a/crates/ec-image/src/lib.rs +++ b/crates/ec-image/src/lib.rs @@ -433,6 +433,15 @@ pub struct Info { pub orientation: u8, } +fn format_of(data: &[u8]) -> Result { + ImageFormat::guess(data).ok_or_else(|| { + Error::unsupported( + "image", + "no PNG, JPEG, WebP, GIF, BMP or TIFF signature at the start of the data", + ) + }) +} + /// Decode `data`, guessing the format from its leading bytes. pub fn decode(data: &[u8]) -> Result { decode_with_limits(data, Limits::default()) @@ -440,17 +449,13 @@ pub fn decode(data: &[u8]) -> Result { /// [`decode`] with caller-chosen [`Limits`]. pub fn decode_with_limits(data: &[u8], limits: Limits) -> Result { - match ImageFormat::guess(data) { - Some(ImageFormat::Png) => png::decode(data, limits), - Some(ImageFormat::Jpeg) => jpeg::decode(data, limits), - Some(ImageFormat::WebP) => webp::decode(data, limits), - Some(ImageFormat::Gif) => gif::decode(data, limits), - Some(ImageFormat::Bmp) => bmp::decode(data, limits), - Some(ImageFormat::Tiff) => tiff::decode(data, limits), - None => Err(Error::unsupported( - "image", - "no PNG, JPEG, WebP, GIF, BMP or TIFF signature at the start of the data", - )), + match format_of(data)? { + ImageFormat::Png => png::decode(data, limits), + ImageFormat::Jpeg => jpeg::decode(data, limits), + ImageFormat::WebP => webp::decode(data, limits), + ImageFormat::Gif => gif::decode(data, limits), + ImageFormat::Bmp => bmp::decode(data, limits), + ImageFormat::Tiff => tiff::decode(data, limits), } } @@ -460,17 +465,13 @@ pub fn decode_with_limits(data: &[u8], limits: Limits) -> Result { /// The whole of `data` is read as header. To look at a bounded prefix or a /// stream instead, use [`probe`] or [`probe_reader`]. pub fn info(data: &[u8]) -> Result { - match ImageFormat::guess(data) { - Some(ImageFormat::Png) => png::info(data), - Some(ImageFormat::Jpeg) => jpeg::info(data), - Some(ImageFormat::WebP) => webp::info(data), - Some(ImageFormat::Gif) => gif::info(data), - Some(ImageFormat::Bmp) => bmp::info(data), - Some(ImageFormat::Tiff) => tiff::info(data), - None => Err(Error::unsupported( - "image", - "no PNG, JPEG, WebP, GIF, BMP or TIFF signature at the start of the data", - )), + match format_of(data)? { + ImageFormat::Png => png::info(data), + ImageFormat::Jpeg => jpeg::info(data), + ImageFormat::WebP => webp::info(data), + ImageFormat::Gif => gif::info(data), + ImageFormat::Bmp => bmp::info(data), + ImageFormat::Tiff => tiff::info(data), } } @@ -502,9 +503,9 @@ pub fn decode_animation(data: &[u8]) -> Result> { /// The limits bound every frame and the whole composite, so a caller with a /// tighter memory budget than the default can lower them here. pub fn decode_animation_with_limits(data: &[u8], limits: Limits) -> Result> { - match ImageFormat::guess(data) { - Some(ImageFormat::Gif) => gif::decode_animation(data, limits), - Some(ImageFormat::WebP) if webp::is_animated(data) => webp::decode_frames(data, limits), + match format_of(data)? { + ImageFormat::Gif => gif::decode_animation(data, limits), + ImageFormat::WebP if webp::is_animated(data) => webp::decode_frames(data, limits), _ => Ok(vec![AnimationFrame { image: decode_with_limits(data, limits)?, delay_num: 0,