pixels/
lib.rs

1/* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at https://mozilla.org/MPL/2.0/. */
4
5mod snapshot;
6
7use std::borrow::Cow;
8use std::io::Cursor;
9use std::ops::Range;
10use std::time::Duration;
11use std::{cmp, fmt, vec};
12
13use euclid::default::{Point2D, Rect, Size2D};
14use image::codecs::{bmp, gif, ico, jpeg, png, webp};
15use image::error::ImageFormatHint;
16use image::imageops::{self, FilterType};
17use image::{
18    AnimationDecoder, DynamicImage, ImageBuffer, ImageDecoder, ImageError, ImageFormat,
19    ImageResult, Limits, Rgba,
20};
21use ipc_channel::ipc::IpcSharedMemory;
22use log::debug;
23use malloc_size_of_derive::MallocSizeOf;
24use serde::{Deserialize, Serialize};
25pub use snapshot::*;
26use webrender_api::ImageKey;
27
28#[derive(Clone, Copy, Debug, Deserialize, Eq, MallocSizeOf, PartialEq, Serialize)]
29pub enum FilterQuality {
30    /// No image interpolation (Nearest-neighbor)
31    None,
32    /// Low-quality image interpolation (Bilinear)
33    Low,
34    /// Medium-quality image interpolation (CatmullRom, Mitchell)
35    Medium,
36    /// High-quality image interpolation (Lanczos)
37    High,
38}
39
40#[derive(Clone, Copy, Debug, Deserialize, Eq, MallocSizeOf, PartialEq, Serialize)]
41pub enum PixelFormat {
42    /// Luminance channel only
43    K8,
44    /// Luminance + alpha
45    KA8,
46    /// RGB, 8 bits per channel
47    RGB8,
48    /// RGB + alpha, 8 bits per channel
49    RGBA8,
50    /// BGR + alpha, 8 bits per channel
51    BGRA8,
52}
53
54/// Computes image byte length, returning None if overflow occurred or the total length exceeds
55/// the maximum image allocation size.
56pub fn compute_rgba8_byte_length_if_within_limit(width: usize, height: usize) -> Option<usize> {
57    // Maximum allowed image allocation size (2^31-1 ~ 2GB).
58    const MAX_IMAGE_BYTE_LENGTH: usize = 2147483647;
59
60    // The color components of each pixel must be stored in four sequential
61    // elements in the order of red, green, blue, and then alpha.
62    4usize
63        .checked_mul(width)
64        .and_then(|v| v.checked_mul(height))
65        .filter(|v| *v <= MAX_IMAGE_BYTE_LENGTH)
66}
67
68/// Copies the rectangle of the source image to the destination image.
69pub fn copy_rgba8_image(
70    src_size: Size2D<u32>,
71    src_rect: Rect<u32>,
72    src_pixels: &[u8],
73    dest_size: Size2D<u32>,
74    dest_rect: Rect<u32>,
75    dest_pixels: &mut [u8],
76) {
77    assert!(!src_rect.is_empty());
78    assert!(!dest_rect.is_empty());
79    assert!(Rect::from_size(src_size).contains_rect(&src_rect));
80    assert!(Rect::from_size(dest_size).contains_rect(&dest_rect));
81    assert!(src_rect.size == dest_rect.size);
82    assert_eq!(src_pixels.len() % 4, 0);
83    assert_eq!(dest_pixels.len() % 4, 0);
84
85    if src_size == dest_size && src_rect == dest_rect {
86        dest_pixels.copy_from_slice(src_pixels);
87        return;
88    }
89
90    let src_first_column_start = src_rect.origin.x as usize * 4;
91    let src_row_length = src_size.width as usize * 4;
92    let src_first_row_start = src_rect.origin.y as usize * src_row_length;
93
94    let dest_first_column_start = dest_rect.origin.x as usize * 4;
95    let dest_row_length = dest_size.width as usize * 4;
96    let dest_first_row_start = dest_rect.origin.y as usize * dest_row_length;
97
98    let (chunk_length, chunk_count) = (
99        src_rect.size.width as usize * 4,
100        src_rect.size.height as usize,
101    );
102
103    for i in 0..chunk_count {
104        let src = &src_pixels[src_first_row_start + i * src_row_length..][src_first_column_start..]
105            [..chunk_length];
106        let dest = &mut dest_pixels[dest_first_row_start + i * dest_row_length..]
107            [dest_first_column_start..][..chunk_length];
108        dest.copy_from_slice(src);
109    }
110}
111
112/// Scales the source image to the required size, performing sampling filter algorithm.
113pub fn scale_rgba8_image(
114    size: Size2D<u32>,
115    pixels: &[u8],
116    required_size: Size2D<u32>,
117    quality: FilterQuality,
118) -> Option<Vec<u8>> {
119    let filter = match quality {
120        FilterQuality::None => FilterType::Nearest,
121        FilterQuality::Low => FilterType::Triangle,
122        FilterQuality::Medium => FilterType::CatmullRom,
123        FilterQuality::High => FilterType::Lanczos3,
124    };
125
126    let buffer: ImageBuffer<Rgba<u8>, &[u8]> =
127        ImageBuffer::from_raw(size.width, size.height, pixels)?;
128
129    let scaled_buffer =
130        imageops::resize(&buffer, required_size.width, required_size.height, filter);
131
132    Some(scaled_buffer.into_vec())
133}
134
135/// Flips the source image vertically in place.
136pub fn flip_y_rgba8_image_inplace(size: Size2D<u32>, pixels: &mut [u8]) {
137    assert_eq!(pixels.len() % 4, 0);
138
139    let row_length = size.width as usize * 4;
140    let half_height = (size.height / 2) as usize;
141
142    let (left, right) = pixels.split_at_mut(pixels.len() - row_length * half_height);
143
144    for i in 0..half_height {
145        let top = &mut left[i * row_length..][..row_length];
146        let bottom = &mut right[(half_height - i - 1) * row_length..][..row_length];
147        top.swap_with_slice(bottom);
148    }
149}
150
151pub fn rgba8_get_rect(pixels: &[u8], size: Size2D<u32>, rect: Rect<u32>) -> Cow<'_, [u8]> {
152    assert!(!rect.is_empty());
153    assert!(Rect::from_size(size).contains_rect(&rect));
154    assert_eq!(pixels.len() % 4, 0);
155    assert_eq!(size.area() as usize, pixels.len() / 4);
156    let area = rect.size.area() as usize;
157    let first_column_start = rect.origin.x as usize * 4;
158    let row_length = size.width as usize * 4;
159    let first_row_start = rect.origin.y as usize * row_length;
160    if rect.origin.x == 0 && rect.size.width == size.width || rect.size.height == 1 {
161        let start = first_column_start + first_row_start;
162        return Cow::Borrowed(&pixels[start..start + area * 4]);
163    }
164    let mut data = Vec::with_capacity(area * 4);
165    for row in pixels[first_row_start..]
166        .chunks(row_length)
167        .take(rect.size.height as usize)
168    {
169        data.extend_from_slice(&row[first_column_start..][..rect.size.width as usize * 4]);
170    }
171    data.into()
172}
173
174// TODO(pcwalton): Speed up with SIMD, or better yet, find some way to not do this.
175pub fn rgba8_byte_swap_colors_inplace(pixels: &mut [u8]) {
176    assert!(pixels.len() % 4 == 0);
177    for rgba in pixels.chunks_mut(4) {
178        rgba.swap(0, 2);
179    }
180}
181
182pub fn rgba8_byte_swap_and_premultiply_inplace(pixels: &mut [u8]) {
183    assert!(pixels.len() % 4 == 0);
184    for rgba in pixels.chunks_mut(4) {
185        let b = rgba[0];
186        rgba[0] = multiply_u8_color(rgba[2], rgba[3]);
187        rgba[1] = multiply_u8_color(rgba[1], rgba[3]);
188        rgba[2] = multiply_u8_color(b, rgba[3]);
189    }
190}
191
192/// Returns true if the pixels were found to be completely opaque.
193pub fn rgba8_premultiply_inplace(pixels: &mut [u8]) -> bool {
194    assert!(pixels.len() % 4 == 0);
195    let mut is_opaque = true;
196    for rgba in pixels.chunks_mut(4) {
197        rgba[0] = multiply_u8_color(rgba[0], rgba[3]);
198        rgba[1] = multiply_u8_color(rgba[1], rgba[3]);
199        rgba[2] = multiply_u8_color(rgba[2], rgba[3]);
200        is_opaque = is_opaque && rgba[3] == 255;
201    }
202    is_opaque
203}
204
205/// Returns a*b/255, rounding any fractional bits to nearest integer
206/// to reduce the loss of precision after multiple consequence alpha
207/// (un)premultiply operations.
208#[inline(always)]
209pub fn multiply_u8_color(a: u8, b: u8) -> u8 {
210    let c = a as u32 * b as u32 + 128;
211    ((c + (c >> 8)) >> 8) as u8
212}
213
214pub fn clip(
215    mut origin: Point2D<i32>,
216    mut size: Size2D<u32>,
217    surface: Size2D<u32>,
218) -> Option<Rect<u32>> {
219    if origin.x < 0 {
220        size.width = size.width.saturating_sub(-origin.x as u32);
221        origin.x = 0;
222    }
223    if origin.y < 0 {
224        size.height = size.height.saturating_sub(-origin.y as u32);
225        origin.y = 0;
226    }
227    let origin = Point2D::new(origin.x as u32, origin.y as u32);
228    Rect::new(origin, size)
229        .intersection(&Rect::from_size(surface))
230        .filter(|rect| !rect.is_empty())
231}
232
233#[derive(PartialEq)]
234pub enum EncodedImageType {
235    Png,
236    Jpeg,
237    Webp,
238}
239
240impl From<String> for EncodedImageType {
241    // From: https://html.spec.whatwg.org/multipage/#serialising-bitmaps-to-a-file
242    // User agents must support PNG ("image/png"). User agents may support other
243    // types. If the user agent does not support the requested type, then it
244    // must create the file using the PNG format.
245    // Anything different than image/jpeg or image/webp is thus treated as PNG.
246    fn from(mime_type: String) -> Self {
247        let mime = mime_type.to_lowercase();
248        if mime == "image/jpeg" {
249            Self::Jpeg
250        } else if mime == "image/webp" {
251            Self::Webp
252        } else {
253            Self::Png
254        }
255    }
256}
257
258impl EncodedImageType {
259    pub fn as_mime_type(&self) -> String {
260        match self {
261            Self::Png => "image/png",
262            Self::Jpeg => "image/jpeg",
263            Self::Webp => "image/webp",
264        }
265        .to_owned()
266    }
267}
268
269/// Whether this response passed any CORS checks, and is thus safe to read from
270/// in cross-origin environments.
271#[derive(Clone, Copy, Debug, Deserialize, MallocSizeOf, PartialEq, Serialize)]
272pub enum CorsStatus {
273    /// The response is either same-origin or cross-origin but passed CORS checks.
274    Safe,
275    /// The response is cross-origin and did not pass CORS checks. It is unsafe
276    /// to expose pixel data to the requesting environment.
277    Unsafe,
278}
279
280#[derive(Clone, Deserialize, MallocSizeOf, Serialize)]
281pub struct RasterImage {
282    pub metadata: ImageMetadata,
283    pub format: PixelFormat,
284    pub id: Option<ImageKey>,
285    pub cors_status: CorsStatus,
286    pub bytes: IpcSharedMemory,
287    pub frames: Vec<ImageFrame>,
288}
289
290#[derive(Clone, Deserialize, MallocSizeOf, Serialize)]
291pub struct ImageFrame {
292    pub delay: Option<Duration>,
293    /// References a range of the `bytes` field from the image that this
294    /// frame belongs to.
295    pub byte_range: Range<usize>,
296    pub width: u32,
297    pub height: u32,
298}
299
300/// A non-owning reference to the data of an [ImageFrame]
301pub struct ImageFrameView<'a> {
302    pub delay: Option<Duration>,
303    pub bytes: &'a [u8],
304    pub width: u32,
305    pub height: u32,
306}
307
308impl RasterImage {
309    pub fn should_animate(&self) -> bool {
310        self.frames.len() > 1
311    }
312
313    fn frame_view<'image>(&'image self, frame: &ImageFrame) -> ImageFrameView<'image> {
314        ImageFrameView {
315            delay: frame.delay,
316            bytes: self.bytes.get(frame.byte_range.clone()).unwrap(),
317            width: frame.width,
318            height: frame.height,
319        }
320    }
321
322    pub fn frame(&self, index: usize) -> Option<ImageFrameView<'_>> {
323        self.frames.get(index).map(|frame| self.frame_view(frame))
324    }
325
326    pub fn first_frame(&self) -> ImageFrameView<'_> {
327        self.frame(0)
328            .expect("All images should have at least one frame")
329    }
330}
331
332impl fmt::Debug for RasterImage {
333    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
334        write!(
335            f,
336            "Image {{ width: {}, height: {}, format: {:?}, ..., id: {:?} }}",
337            self.metadata.width, self.metadata.height, self.format, self.id
338        )
339    }
340}
341
342#[derive(Clone, Copy, Debug, Deserialize, Eq, MallocSizeOf, PartialEq, Serialize)]
343pub struct ImageMetadata {
344    pub width: u32,
345    pub height: u32,
346}
347
348// FIXME: Images must not be copied every frame. Instead we should atomically
349// reference count them.
350
351pub fn load_from_memory(buffer: &[u8], cors_status: CorsStatus) -> Option<RasterImage> {
352    if buffer.is_empty() {
353        return None;
354    }
355
356    let image_fmt_result = detect_image_format(buffer);
357    match image_fmt_result {
358        Err(msg) => {
359            debug!("{}", msg);
360            None
361        },
362        Ok(format) => {
363            let Ok(image_decoder) = make_decoder(format, buffer) else {
364                return None;
365            };
366            match image_decoder {
367                GenericImageDecoder::Png(png_decoder) => {
368                    if png_decoder.is_apng().unwrap_or_default() {
369                        let Ok(apng_decoder) = png_decoder.apng() else {
370                            return None;
371                        };
372                        decode_animated_image(cors_status, apng_decoder)
373                    } else {
374                        decode_static_image(cors_status, *png_decoder)
375                    }
376                },
377                GenericImageDecoder::Gif(animation_decoder) => {
378                    decode_animated_image(cors_status, *animation_decoder)
379                },
380                GenericImageDecoder::Webp(webp_decoder) => {
381                    if webp_decoder.has_animation() {
382                        decode_animated_image(cors_status, *webp_decoder)
383                    } else {
384                        decode_static_image(cors_status, *webp_decoder)
385                    }
386                },
387                GenericImageDecoder::Bmp(image_decoder) => {
388                    decode_static_image(cors_status, *image_decoder)
389                },
390                GenericImageDecoder::Jpeg(image_decoder) => {
391                    decode_static_image(cors_status, *image_decoder)
392                },
393                GenericImageDecoder::Ico(image_decoder) => {
394                    decode_static_image(cors_status, *image_decoder)
395                },
396            }
397        },
398    }
399}
400
401// https://developer.mozilla.org/en-US/docs/Web/HTML/Element/img
402pub fn detect_image_format(buffer: &[u8]) -> Result<ImageFormat, &str> {
403    if is_gif(buffer) {
404        Ok(ImageFormat::Gif)
405    } else if is_jpeg(buffer) {
406        Ok(ImageFormat::Jpeg)
407    } else if is_png(buffer) {
408        Ok(ImageFormat::Png)
409    } else if is_webp(buffer) {
410        Ok(ImageFormat::WebP)
411    } else if is_bmp(buffer) {
412        Ok(ImageFormat::Bmp)
413    } else if is_ico(buffer) {
414        Ok(ImageFormat::Ico)
415    } else {
416        Err("Image Format Not Supported")
417    }
418}
419
420pub fn unmultiply_inplace<const SWAP_RB: bool>(pixels: &mut [u8]) {
421    for rgba in pixels.chunks_mut(4) {
422        let a = rgba[3] as u32;
423        let mut b = rgba[2] as u32;
424        let mut g = rgba[1] as u32;
425        let mut r = rgba[0] as u32;
426
427        if a > 0 {
428            r = r * 255 / a;
429            g = g * 255 / a;
430            b = b * 255 / a;
431
432            if SWAP_RB {
433                rgba[2] = r as u8;
434                rgba[1] = g as u8;
435                rgba[0] = b as u8;
436            } else {
437                rgba[2] = b as u8;
438                rgba[1] = g as u8;
439                rgba[0] = r as u8;
440            }
441        }
442    }
443}
444
445#[repr(u8)]
446pub enum Multiply {
447    None = 0,
448    PreMultiply = 1,
449    UnMultiply = 2,
450}
451
452pub fn transform_inplace(pixels: &mut [u8], multiply: Multiply, swap_rb: bool, clear_alpha: bool) {
453    match (multiply, swap_rb, clear_alpha) {
454        (Multiply::None, true, true) => generic_transform_inplace::<0, true, true>(pixels),
455        (Multiply::None, true, false) => generic_transform_inplace::<0, true, false>(pixels),
456        (Multiply::None, false, true) => generic_transform_inplace::<0, false, true>(pixels),
457        (Multiply::None, false, false) => generic_transform_inplace::<0, false, false>(pixels),
458        (Multiply::PreMultiply, true, true) => generic_transform_inplace::<1, true, true>(pixels),
459        (Multiply::PreMultiply, true, false) => generic_transform_inplace::<1, true, false>(pixels),
460        (Multiply::PreMultiply, false, true) => generic_transform_inplace::<1, false, true>(pixels),
461        (Multiply::PreMultiply, false, false) => {
462            generic_transform_inplace::<1, false, false>(pixels)
463        },
464        (Multiply::UnMultiply, true, true) => generic_transform_inplace::<2, true, true>(pixels),
465        (Multiply::UnMultiply, true, false) => generic_transform_inplace::<2, true, false>(pixels),
466        (Multiply::UnMultiply, false, true) => generic_transform_inplace::<2, false, true>(pixels),
467        (Multiply::UnMultiply, false, false) => {
468            generic_transform_inplace::<2, false, false>(pixels)
469        },
470    }
471}
472
473pub fn generic_transform_inplace<
474    const MULTIPLY: u8, // 1 premultiply, 2 unmultiply
475    const SWAP_RB: bool,
476    const CLEAR_ALPHA: bool,
477>(
478    pixels: &mut [u8],
479) {
480    for rgba in pixels.chunks_mut(4) {
481        match MULTIPLY {
482            1 => {
483                let a = rgba[3];
484
485                rgba[0] = multiply_u8_color(rgba[0], a);
486                rgba[1] = multiply_u8_color(rgba[1], a);
487                rgba[2] = multiply_u8_color(rgba[2], a);
488            },
489            2 => {
490                let a = rgba[3] as u32;
491
492                if a > 0 {
493                    rgba[0] = (rgba[0] as u32 * 255 / a) as u8;
494                    rgba[1] = (rgba[1] as u32 * 255 / a) as u8;
495                    rgba[2] = (rgba[2] as u32 * 255 / a) as u8;
496                }
497            },
498            _ => {},
499        }
500        if SWAP_RB {
501            rgba.swap(0, 2);
502        }
503        if CLEAR_ALPHA {
504            rgba[3] = u8::MAX;
505        }
506    }
507}
508
509fn is_gif(buffer: &[u8]) -> bool {
510    buffer.starts_with(b"GIF87a") || buffer.starts_with(b"GIF89a")
511}
512
513fn is_jpeg(buffer: &[u8]) -> bool {
514    buffer.starts_with(&[0xff, 0xd8, 0xff])
515}
516
517fn is_png(buffer: &[u8]) -> bool {
518    buffer.starts_with(&[0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A])
519}
520
521fn is_bmp(buffer: &[u8]) -> bool {
522    buffer.starts_with(&[0x42, 0x4D])
523}
524
525fn is_ico(buffer: &[u8]) -> bool {
526    buffer.starts_with(&[0x00, 0x00, 0x01, 0x00])
527}
528
529fn is_webp(buffer: &[u8]) -> bool {
530    // https://developers.google.com/speed/webp/docs/riff_container
531    // First four bytes: `RIFF`, header size 12 bytes
532    if !buffer.starts_with(b"RIFF") || buffer.len() < 12 {
533        return false;
534    }
535    let size: [u8; 4] = [buffer[4], buffer[5], buffer[6], buffer[7]];
536    // Bytes 4..8 are a little endian u32 indicating
537    // > The size of the file in bytes, starting at offset 8.
538    // > The maximum value of this field is 2^32 minus 10 bytes and thus the size
539    // > of the whole file is at most 4 GiB minus 2 bytes.
540    let len: usize = u32::from_le_bytes(size) as usize;
541    buffer[8..].len() >= len && &buffer[8..12] == b"WEBP"
542}
543
544enum GenericImageDecoder<R: std::io::BufRead + std::io::Seek> {
545    Png(Box<png::PngDecoder<R>>),
546    Gif(Box<gif::GifDecoder<R>>),
547    Webp(Box<webp::WebPDecoder<R>>),
548    Jpeg(Box<jpeg::JpegDecoder<R>>),
549    Bmp(Box<bmp::BmpDecoder<R>>),
550    Ico(Box<ico::IcoDecoder<R>>),
551}
552
553fn make_decoder(
554    format: ImageFormat,
555    buffer: &[u8],
556) -> ImageResult<GenericImageDecoder<Cursor<&[u8]>>> {
557    let limits = Limits::default();
558    let reader = Cursor::new(buffer);
559    Ok(match format {
560        ImageFormat::Png => {
561            GenericImageDecoder::Png(Box::new(png::PngDecoder::with_limits(reader, limits)?))
562        },
563        ImageFormat::Gif => GenericImageDecoder::Gif(Box::new(gif::GifDecoder::new(reader)?)),
564        ImageFormat::WebP => GenericImageDecoder::Webp(Box::new(webp::WebPDecoder::new(reader)?)),
565        ImageFormat::Jpeg => GenericImageDecoder::Jpeg(Box::new(jpeg::JpegDecoder::new(reader)?)),
566        ImageFormat::Bmp => GenericImageDecoder::Bmp(Box::new(bmp::BmpDecoder::new(reader)?)),
567        ImageFormat::Ico => GenericImageDecoder::Ico(Box::new(ico::IcoDecoder::new(reader)?)),
568        _ => {
569            return Err(ImageError::Unsupported(
570                ImageFormatHint::Exact(format).into(),
571            ));
572        },
573    })
574}
575
576fn decode_static_image(
577    cors_status: CorsStatus,
578    image_decoder: impl ImageDecoder,
579) -> Option<RasterImage> {
580    let Ok(dynamic_image) = DynamicImage::from_decoder(image_decoder) else {
581        debug!("Image decoding error");
582        return None;
583    };
584    let mut rgba = dynamic_image.into_rgba8();
585    rgba8_byte_swap_colors_inplace(&mut rgba);
586    let frame = ImageFrame {
587        delay: None,
588        byte_range: 0..rgba.len(),
589        width: rgba.width(),
590        height: rgba.height(),
591    };
592    Some(RasterImage {
593        metadata: ImageMetadata {
594            width: rgba.width(),
595            height: rgba.height(),
596        },
597        format: PixelFormat::BGRA8,
598        frames: vec![frame],
599        bytes: IpcSharedMemory::from_bytes(&rgba),
600        id: None,
601        cors_status,
602    })
603}
604
605fn decode_animated_image<'a, T>(
606    cors_status: CorsStatus,
607    animated_image_decoder: T,
608) -> Option<RasterImage>
609where
610    T: AnimationDecoder<'a>,
611{
612    let mut width = 0;
613    let mut height = 0;
614
615    // This uses `map_while`, because the first non-decodable frame seems to
616    // send the frame iterator into an infinite loop. See
617    // <https://github.com/image-rs/image/issues/2442>.
618    let mut frame_data = vec![];
619    let mut total_number_of_bytes = 0;
620    let frames: Vec<ImageFrame> = animated_image_decoder
621        .into_frames()
622        .map_while(|decoded_frame| {
623            let mut animated_frame = match decoded_frame {
624                Ok(decoded_frame) => decoded_frame,
625                Err(error) => {
626                    debug!("decode Animated frame error: {error}");
627                    return None;
628                },
629            };
630            rgba8_byte_swap_colors_inplace(animated_frame.buffer_mut());
631            let frame_start = total_number_of_bytes;
632            total_number_of_bytes += animated_frame.buffer().len();
633
634            // The image size should be at least as large as the largest frame.
635            let frame_width = animated_frame.buffer().width();
636            let frame_height = animated_frame.buffer().height();
637            width = cmp::max(width, frame_width);
638            height = cmp::max(height, frame_height);
639
640            let frame = ImageFrame {
641                byte_range: frame_start..total_number_of_bytes,
642                delay: Some(Duration::from(animated_frame.delay())),
643                width: frame_width,
644                height: frame_height,
645            };
646
647            frame_data.push(animated_frame);
648
649            Some(frame)
650        })
651        .collect();
652
653    if frames.is_empty() {
654        debug!("Animated Image decoding error");
655        return None;
656    }
657
658    // Coalesce the frame data into one single shared memory region.
659    let mut bytes = Vec::with_capacity(total_number_of_bytes);
660    for frame in frame_data {
661        bytes.extend_from_slice(frame.buffer());
662    }
663
664    Some(RasterImage {
665        metadata: ImageMetadata { width, height },
666        cors_status,
667        frames,
668        id: None,
669        format: PixelFormat::BGRA8,
670        bytes: IpcSharedMemory::from_bytes(&bytes),
671    })
672}
673
674#[cfg(test)]
675mod test {
676    use super::detect_image_format;
677
678    #[test]
679    fn test_supported_images() {
680        let gif1 = [b'G', b'I', b'F', b'8', b'7', b'a'];
681        let gif2 = [b'G', b'I', b'F', b'8', b'9', b'a'];
682        let jpeg = [0xff, 0xd8, 0xff];
683        let png = [0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A];
684        let webp = [
685            b'R', b'I', b'F', b'F', 0x04, 0x00, 0x00, 0x00, b'W', b'E', b'B', b'P',
686        ];
687        let bmp = [0x42, 0x4D];
688        let ico = [0x00, 0x00, 0x01, 0x00];
689        let junk_format = [0x01, 0x02, 0x03, 0x04, 0x05];
690
691        assert!(detect_image_format(&gif1).is_ok());
692        assert!(detect_image_format(&gif2).is_ok());
693        assert!(detect_image_format(&jpeg).is_ok());
694        assert!(detect_image_format(&png).is_ok());
695        assert!(detect_image_format(&webp).is_ok());
696        assert!(detect_image_format(&bmp).is_ok());
697        assert!(detect_image_format(&ico).is_ok());
698        assert!(detect_image_format(&junk_format).is_err());
699    }
700}