forge
Explore
Log in

Don't you dare go Hollow

U20: ec-image one format_of() dispatcher for decode, info and decode_animation (same signature refusal)

Browse files at this commit

18956d7d10600afcbff464371cc32dddc1bb609fAhmet Kaan GümüşParent: 58f8d8f
crates/ec-image/src/lib.rsmodified+26 -25
@@ -433,6 +433,15 @@ pub struct Info {
433 pub orientation: u8,433 pub orientation: u8,
434 }434 }
435 435
436+fn format_of(data: &[u8]) -> Result<ImageFormat> {
437+ ImageFormat::guess(data).ok_or_else(|| {
438+ Error::unsupported(
439+ "image",
440+ "no PNG, JPEG, WebP, GIF, BMP or TIFF signature at the start of the data",
441+ )
442+ })
443+}
444+
436 /// Decode `data`, guessing the format from its leading bytes.445 /// Decode `data`, guessing the format from its leading bytes.
437 pub fn decode(data: &[u8]) -> Result<Image> {446 pub fn decode(data: &[u8]) -> Result<Image> {
438 decode_with_limits(data, Limits::default())447 decode_with_limits(data, Limits::default())
@@ -440,17 +449,13 @@ pub fn decode(data: &[u8]) -> Result<Image> {
440 449
441 /// [`decode`] with caller-chosen [`Limits`].450 /// [`decode`] with caller-chosen [`Limits`].
442 pub fn decode_with_limits(data: &[u8], limits: Limits) -> Result<Image> {451 pub fn decode_with_limits(data: &[u8], limits: Limits) -> Result<Image> {
443- match ImageFormat::guess(data) {452+ match format_of(data)? {
444- Some(ImageFormat::Png) => png::decode(data, limits),453+ ImageFormat::Png => png::decode(data, limits),
445- Some(ImageFormat::Jpeg) => jpeg::decode(data, limits),454+ ImageFormat::Jpeg => jpeg::decode(data, limits),
446- Some(ImageFormat::WebP) => webp::decode(data, limits),455+ ImageFormat::WebP => webp::decode(data, limits),
447- Some(ImageFormat::Gif) => gif::decode(data, limits),456+ ImageFormat::Gif => gif::decode(data, limits),
448- Some(ImageFormat::Bmp) => bmp::decode(data, limits),457+ ImageFormat::Bmp => bmp::decode(data, limits),
449- Some(ImageFormat::Tiff) => tiff::decode(data, limits),458+ ImageFormat::Tiff => tiff::decode(data, limits),
450- None => Err(Error::unsupported(
451- "image",
452- "no PNG, JPEG, WebP, GIF, BMP or TIFF signature at the start of the data",
453- )),
454 }459 }
455 }460 }
456 461
@@ -460,17 +465,13 @@ pub fn decode_with_limits(data: &[u8], limits: Limits) -> Result<Image> {
460 /// The whole of `data` is read as header. To look at a bounded prefix or a465 /// The whole of `data` is read as header. To look at a bounded prefix or a
461 /// stream instead, use [`probe`] or [`probe_reader`].466 /// stream instead, use [`probe`] or [`probe_reader`].
462 pub fn info(data: &[u8]) -> Result<Info> {467 pub fn info(data: &[u8]) -> Result<Info> {
463- match ImageFormat::guess(data) {468+ match format_of(data)? {
464- Some(ImageFormat::Png) => png::info(data),469+ ImageFormat::Png => png::info(data),
465- Some(ImageFormat::Jpeg) => jpeg::info(data),470+ ImageFormat::Jpeg => jpeg::info(data),
466- Some(ImageFormat::WebP) => webp::info(data),471+ ImageFormat::WebP => webp::info(data),
467- Some(ImageFormat::Gif) => gif::info(data),472+ ImageFormat::Gif => gif::info(data),
468- Some(ImageFormat::Bmp) => bmp::info(data),473+ ImageFormat::Bmp => bmp::info(data),
469- Some(ImageFormat::Tiff) => tiff::info(data),474+ ImageFormat::Tiff => tiff::info(data),
470- None => Err(Error::unsupported(
471- "image",
472- "no PNG, JPEG, WebP, GIF, BMP or TIFF signature at the start of the data",
473- )),
474 }475 }
475 }476 }
476 477
@@ -502,9 +503,9 @@ pub fn decode_animation(data: &[u8]) -> Result<Vec<AnimationFrame>> {
502 /// The limits bound every frame and the whole composite, so a caller with a503 /// The limits bound every frame and the whole composite, so a caller with a
503 /// tighter memory budget than the default can lower them here.504 /// tighter memory budget than the default can lower them here.
504 pub fn decode_animation_with_limits(data: &[u8], limits: Limits) -> Result<Vec<AnimationFrame>> {505 pub fn decode_animation_with_limits(data: &[u8], limits: Limits) -> Result<Vec<AnimationFrame>> {
505- match ImageFormat::guess(data) {506+ match format_of(data)? {
506- Some(ImageFormat::Gif) => gif::decode_animation(data, limits),507+ ImageFormat::Gif => gif::decode_animation(data, limits),
507- Some(ImageFormat::WebP) if webp::is_animated(data) => webp::decode_frames(data, limits),508+ ImageFormat::WebP if webp::is_animated(data) => webp::decode_frames(data, limits),
508 _ => Ok(vec![AnimationFrame {509 _ => Ok(vec![AnimationFrame {
509 image: decode_with_limits(data, limits)?,510 image: decode_with_limits(data, limits)?,
510 delay_num: 0,511 delay_num: 0,