Skip to main content

rff_format_jpeg/
lib.rs

1//! JPEG single-image container, and JPEG Multi-Picture Format (MPF).
2//!
3//! A JPEG file *is* its codec bitstream, so the demuxer hands the whole file to
4//! the [`mjpeg`](rff-codec-jpeg) codec as one packet and the muxer writes a
5//! codec packet straight out. Dimensions for `ffprobe` come from a light scan
6//! of the JPEG marker segments for the frame header (`SOFn`).
7//!
8//! A JPEG that is an **MPF** file (CIPA DC-007: an `APP2` `MPF\0` index
9//! listing more JPEG images appended after the first one's `EOI` — Ultra HDR
10//! gain-map photos, `.mpo` stereo pairs, camera previews) demuxes as one
11//! stream per image, the way FFmpeg 9.1's `jpeg_mpf` demuxer splits it:
12//! streams in index order, one packet each holding that image's bytes
13//! exactly. Each stream's [`Demuxer::stream_metadata`] names its MP Type
14//! (`mpf_type`: `primary`, `gain_map`, `disparity`, `large_thumbnail_vga`,
15//! ...). Both the `jpeg` and the `jpeg_mpf` demuxers do this — `jpeg_mpf` is
16//! what content sniffing picks when the index is in the first 4 KiB, `jpeg`
17//! what it picks when a large Exif block pushes it further.
18//!
19//! The `jpeg_mpf` muxer (`-f jpeg_mpf`, or a `.mpo` name) writes N JPEG
20//! streams as one MPF file; see [`mpf::write`]. The index parsing and writing
21//! live in [`mpf`], which is `no_std`-friendly and independent of the rest of
22//! rff.
23#![forbid(unsafe_code)]
24
25extern crate alloc;
26
27pub mod mpf;
28
29use std::io::{Read, Write};
30use std::ops::Range;
31
32use rff_core::{CodecId, Dictionary, Error, Packet, Rational, Result};
33use rff_format::{Demuxer, Format, FormatRegistry, Input, MuxCaps, Muxer, Output, Stream};
34
35use mpf::{LayoutSource, MpType, TrailingPolicy};
36
37/// Register the JPEG formats (`jpeg`, `jpeg_mpf`) into a [`FormatRegistry`].
38pub fn register(registry: &mut FormatRegistry) {
39    registry.register(Format {
40        name: "jpeg",
41        long_name: "JPEG (JFIF) image",
42        extensions: &["jpg", "jpeg"],
43        demuxer: Some(|input| Box::new(JpegDemuxer::new(input))),
44        muxer: Some(|output| Box::new(JpegMuxer::new(output))),
45        muxer_path: None,
46        probe: Some(probe_jpeg),
47        mux_caps: MuxCaps::single(&[CodecId::Jpeg]).image(),
48    });
49    registry.register(Format {
50        name: "jpeg_mpf",
51        long_name: "JPEG Multi-Picture Format",
52        extensions: &["mpo"],
53        demuxer: Some(|input| Box::new(JpegDemuxer::new(input))),
54        muxer: Some(|output| Box::new(MpfMuxer::new(output))),
55        muxer_path: None,
56        probe: Some(probe_mpf),
57        mux_caps: MuxCaps {
58            codecs: &[CodecId::Jpeg],
59            max_streams: Some(mpf::MAX_IMAGES),
60            still_image: false,
61        },
62    });
63}
64
65/// Sniff JPEG by its start-of-image marker (`FF D8 FF`).
66fn probe_jpeg(data: &[u8]) -> i32 {
67    if data.len() >= 3 && data[0] == 0xFF && data[1] == 0xD8 && data[2] == 0xFF {
68        100
69    } else {
70        0
71    }
72}
73
74/// Sniff an MPF file: a JPEG whose header (within the probed bytes) has an
75/// `APP2` segment opening `MPF\0`, or an ISO 21496-1 segment carrying only
76/// the version fields (the base image of a gain-map file), as FFmpeg's
77/// probe does. Scores above plain `jpeg`.
78fn probe_mpf(data: &[u8]) -> i32 {
79    if probe_jpeg(data) == 0 {
80        return 0;
81    }
82    let mut i = 2usize;
83    while i + 4 <= data.len() {
84        if data[i] != 0xFF {
85            return 0;
86        }
87        let mut j = i + 1;
88        while data.get(j) == Some(&0xFF) {
89            j += 1;
90        }
91        let Some(&marker) = data.get(j) else {
92            return 0;
93        };
94        if marker == 0xDA || marker == 0x01 || (0xD0..=0xD9).contains(&marker) {
95            return 0;
96        }
97        let Some(len) = data.get(j + 1..j + 3) else {
98            return 0;
99        };
100        let len = u16::from_be_bytes([len[0], len[1]]) as usize;
101        if len < 2 {
102            return 0;
103        }
104        let body = data
105            .get(j + 3..(j + 1 + len).min(data.len()))
106            .unwrap_or(&[]);
107        if marker == 0xE2
108            && (body.starts_with(mpf::MPF_IDENTIFIER)
109                || (body.starts_with(mpf::ISO21496_IDENTIFIER)
110                    && len - 2 == mpf::ISO21496_IDENTIFIER.len() + 4))
111        {
112            return 101;
113        }
114        i = j + 1 + len;
115    }
116    0
117}
118
119/// Scan JPEG marker segments for a frame header (`SOF0..SOF15`, minus the
120/// non-frame `C4`/`C8`/`CC`) and return `(width, height)`.
121fn jpeg_dimensions(buf: &[u8]) -> Option<(u32, u32)> {
122    if buf.len() < 2 || buf[0] != 0xFF || buf[1] != 0xD8 {
123        return None;
124    }
125    let mut i = 2;
126    while i + 1 < buf.len() {
127        if buf[i] != 0xFF {
128            i += 1;
129            continue;
130        }
131        let marker = buf[i + 1];
132        // Standalone markers (no length): padding, SOI/EOI, RSTn, TEM.
133        if marker == 0xFF || marker == 0x01 || (0xD0..=0xD9).contains(&marker) {
134            i += 2;
135            continue;
136        }
137        // SOFn frame header: FF, marker, len(2), precision(1), height(2), width(2).
138        if (0xC0..=0xCF).contains(&marker) && marker != 0xC4 && marker != 0xC8 && marker != 0xCC {
139            if i + 9 > buf.len() {
140                return None;
141            }
142            let h = u16::from_be_bytes([buf[i + 5], buf[i + 6]]) as u32;
143            let w = u16::from_be_bytes([buf[i + 7], buf[i + 8]]) as u32;
144            return Some((w, h));
145        }
146        // Any other marker carries a 2-byte length we skip over.
147        if i + 4 > buf.len() {
148            return None;
149        }
150        let len = u16::from_be_bytes([buf[i + 2], buf[i + 3]]) as usize;
151        i += 2 + len;
152    }
153    None
154}
155
156/// The per-stream metadata keys the demuxer sets and the MPF muxer reads.
157pub mod keys {
158    /// MP Type, by [`MpType::name`](crate::mpf::MpType::name) (`primary`,
159    /// `gain_map`, `disparity`, ...) or as a hex code (`0x020002`).
160    pub const MPF_TYPE: &str = "mpf_type";
161    /// The MP Type code, `0x` and six hex digits.
162    pub const MPF_TYPE_CODE: &str = "mpf_type_code";
163    /// The image's `MPEntry` number in the MP Index.
164    pub const MPF_ENTRY: &str = "mpf_entry";
165    /// Attribute flags, comma-separated: `representative`,
166    /// `dependent_parent`, `dependent_child` (the muxer honours
167    /// `representative`; it derives the dependency flags itself).
168    pub const MPF_FLAGS: &str = "mpf_flags";
169    /// `index` (read from the MP Index) or `trailing` (no usable index: split
170    /// at `EOI`).
171    pub const MPF_SOURCE: &str = "mpf_source";
172}
173
174struct JpegDemuxer {
175    input: Option<Input>,
176    /// The whole file; a single-image file's packet takes it by move.
177    data: Vec<u8>,
178    /// `(stream, bytes)` for each packet still to hand out, in file order.
179    packets: Vec<(usize, Range<usize>)>,
180    next: usize,
181    metadata: Vec<Dictionary>,
182    discard: Vec<bool>,
183}
184
185impl JpegDemuxer {
186    fn new(input: Input) -> JpegDemuxer {
187        JpegDemuxer {
188            input: Some(input),
189            data: Vec::new(),
190            packets: Vec::new(),
191            next: 0,
192            metadata: Vec::new(),
193            discard: Vec::new(),
194        }
195    }
196}
197
198fn flags_string(attribute: u32) -> String {
199    let mut f = Vec::new();
200    if attribute & mpf::ATTR_REPRESENTATIVE != 0 {
201        f.push("representative");
202    }
203    if attribute & mpf::ATTR_DEPENDENT_PARENT != 0 {
204        f.push("dependent_parent");
205    }
206    if attribute & mpf::ATTR_DEPENDENT_CHILD != 0 {
207        f.push("dependent_child");
208    }
209    f.join(",")
210}
211
212impl Demuxer for JpegDemuxer {
213    fn read_header(&mut self) -> Result<Vec<Stream>> {
214        let mut input = self
215            .input
216            .take()
217            .ok_or_else(|| Error::invalid("jpeg demux: header already read"))?;
218        let mut buf = Vec::new();
219        input.read_to_end(&mut buf)?;
220
221        let not_jpeg = || Error::invalid("jpeg demux: not a JPEG file");
222        // What `locate` tolerated (`layout.warnings`) is not fatal: a damaged
223        // index degrades to fewer images, never to an unreadable file.
224        let layout = mpf::locate(&buf, TrailingPolicy::Split).map_err(|_| not_jpeg())?;
225
226        let mut streams = Vec::with_capacity(layout.images.len());
227        for (index, img) in layout.images.iter().enumerate() {
228            let bytes = &buf[img.range.clone()];
229            let (width, height) = match jpeg_dimensions(bytes) {
230                Some(d) => d,
231                // The first image must be a JPEG; another one we cannot size
232                // is still handed out (the decoder will judge it).
233                None if index == layout.first => return Err(not_jpeg()),
234                None => (0, 0),
235            };
236            let mut stream = Stream::new(index, CodecId::Jpeg);
237            stream.width = width;
238            stream.height = height;
239            stream.time_base = Rational::new(1, 1);
240            stream.nb_frames = Some(1);
241            let mut md = Dictionary::new();
242            if layout.source != LayoutSource::Single {
243                // The first image is what a player shows (FFmpeg marks it
244                // default); the others are alternatives or auxiliaries.
245                stream.disposition.default = Some(index == layout.first);
246                md.set(keys::MPF_TYPE, img.mp_type.to_string());
247                md.set(keys::MPF_TYPE_CODE, format!("0x{:06x}", img.mp_type.code()));
248                if let Some(e) = img.entry {
249                    md.set(keys::MPF_ENTRY, e.to_string());
250                }
251                let flags = flags_string(img.attribute);
252                if !flags.is_empty() {
253                    md.set(keys::MPF_FLAGS, flags);
254                }
255                md.set(
256                    keys::MPF_SOURCE,
257                    if layout.source == LayoutSource::Index {
258                        "index"
259                    } else {
260                        "trailing"
261                    },
262                );
263            }
264            self.metadata.push(md);
265            streams.push(stream);
266        }
267        self.packets = layout
268            .file_order()
269            .into_iter()
270            .map(|i| (i, layout.images[i].range.clone()))
271            .collect();
272        self.data = buf;
273        Ok(streams)
274    }
275
276    fn read_packet(&mut self) -> Result<Packet> {
277        while let Some((stream, range)) = self.packets.get(self.next).cloned() {
278            self.next += 1;
279            if self.discard.get(stream).copied().unwrap_or(false) {
280                continue;
281            }
282            let data = if self.packets.len() == 1 && range == (0..self.data.len()) {
283                std::mem::take(&mut self.data)
284            } else {
285                self.data[range].to_vec()
286            };
287            let mut packet = Packet::from_data(stream, data);
288            packet.flags.keyframe = true;
289            packet.pts = Some(0);
290            return Ok(packet);
291        }
292        Err(Error::Eof)
293    }
294
295    fn set_discard(&mut self, discard: &[bool]) {
296        self.discard = discard.to_vec();
297    }
298
299    fn stream_metadata(&self, index: usize) -> Option<&Dictionary> {
300        self.metadata.get(index).filter(|d| !d.is_empty())
301    }
302}
303
304struct JpegMuxer {
305    out: Output,
306}
307
308impl JpegMuxer {
309    fn new(out: Output) -> JpegMuxer {
310        JpegMuxer { out }
311    }
312}
313
314impl Muxer for JpegMuxer {
315    fn write_header(&mut self, streams: &[Stream]) -> Result<()> {
316        match streams {
317            [s] if s.codec_id == CodecId::Jpeg => Ok(()),
318            [_] => Err(Error::unsupported(
319                "jpeg mux: only the `mjpeg` codec is supported",
320            )),
321            [] => Err(Error::invalid("jpeg mux: no streams")),
322            _ => Err(Error::unsupported(
323                "jpeg mux: a JPEG file holds one image; select one stream (-map 0:N), \
324                 or write several as -f jpeg_mpf",
325            )),
326        }
327    }
328
329    fn write_packet(&mut self, packet: &Packet) -> Result<()> {
330        self.out.write_all(&packet.data)?;
331        Ok(())
332    }
333
334    fn write_trailer(&mut self) -> Result<()> {
335        self.out.flush()?;
336        Ok(())
337    }
338}
339
340/// The `jpeg_mpf` muxer: one JPEG per stream, written as one MPF file at the
341/// trailer (the index needs every image's size).
342struct MpfMuxer {
343    out: Output,
344    images: Vec<Option<Vec<u8>>>,
345    types: Vec<Option<MpType>>,
346    representative: Vec<bool>,
347    /// An `mpf_type` that names no MP Type, refused at the header.
348    bad_type: Option<(usize, String)>,
349}
350
351impl MpfMuxer {
352    fn new(out: Output) -> MpfMuxer {
353        MpfMuxer {
354            out,
355            images: Vec::new(),
356            types: Vec::new(),
357            representative: Vec::new(),
358            bad_type: None,
359        }
360    }
361
362    fn slot<T: Default + Clone>(v: &mut Vec<T>, index: usize) -> &mut T {
363        if v.len() <= index {
364            v.resize(index + 1, T::default());
365        }
366        &mut v[index]
367    }
368}
369
370impl Muxer for MpfMuxer {
371    fn set_stream_metadata(&mut self, index: usize, metadata: &Dictionary) {
372        if index >= mpf::MAX_IMAGES {
373            return;
374        }
375        if let Some(t) = metadata.get(keys::MPF_TYPE) {
376            match MpType::from_name(t) {
377                Some(t) => *Self::slot(&mut self.types, index) = Some(t),
378                None => self.bad_type = Some((index, t.to_string())),
379            }
380        }
381        if let Some(f) = metadata.get(keys::MPF_FLAGS) {
382            *Self::slot(&mut self.representative, index) =
383                f.split(',').any(|f| f.trim() == "representative");
384        }
385    }
386
387    fn write_header(&mut self, streams: &[Stream]) -> Result<()> {
388        if streams.is_empty() {
389            return Err(Error::invalid("jpeg_mpf mux: no streams"));
390        }
391        if let Some((i, t)) = &self.bad_type {
392            return Err(Error::invalid(format!(
393                "jpeg_mpf mux: stream {i}: unknown mpf_type `{t}`                  (a name such as primary, gain_map, disparity, or a code such as 0x020002)"
394            )));
395        }
396        if streams.len() > mpf::MAX_IMAGES {
397            return Err(Error::unsupported(format!(
398                "jpeg_mpf mux: at most {} images",
399                mpf::MAX_IMAGES
400            )));
401        }
402        if let Some(s) = streams.iter().find(|s| s.codec_id != CodecId::Jpeg) {
403            return Err(Error::unsupported(format!(
404                "jpeg_mpf mux: stream {} is `{}`; every image must be JPEG (pass -c:v mjpeg)",
405                s.index,
406                s.codec_id.name()
407            )));
408        }
409        self.images = vec![None; streams.len()];
410        Ok(())
411    }
412
413    fn write_packet(&mut self, packet: &Packet) -> Result<()> {
414        let slot = self
415            .images
416            .get_mut(packet.stream_index)
417            .ok_or_else(|| Error::invalid("jpeg_mpf mux: packet for an undeclared stream"))?;
418        if slot.is_some() {
419            return Err(Error::invalid(format!(
420                "jpeg_mpf mux: stream {}: expected a single JPEG image",
421                packet.stream_index
422            )));
423        }
424        *slot = Some(packet.data.clone());
425        Ok(())
426    }
427
428    fn write_trailer(&mut self) -> Result<()> {
429        let mut images = Vec::with_capacity(self.images.len());
430        for (i, img) in self.images.iter().enumerate() {
431            let data = img.as_deref().ok_or_else(|| {
432                Error::invalid(format!("jpeg_mpf mux: no image was supplied on stream {i}"))
433            })?;
434            images.push(mpf::WriteImage {
435                data,
436                mp_type: self.types.get(i).copied().flatten(),
437                representative: self.representative.get(i).copied().unwrap_or(false),
438            });
439        }
440        let file = mpf::write(&images).map_err(|e| Error::invalid(format!("jpeg_mpf mux: {e}")))?;
441        self.out.write_all(&file)?;
442        self.out.flush()?;
443        Ok(())
444    }
445}
446
447#[cfg(test)]
448mod tests {
449    use super::*;
450    use std::io::Cursor;
451    use std::sync::{Arc, Mutex};
452
453    /// A minimal baseline-JPEG header: SOI + a SOF0 declaring 5×7.
454    fn jpeg_header_5x7() -> Vec<u8> {
455        let mut v = vec![0xFF, 0xD8]; // SOI
456                                      // SOF0: marker, len=17, precision=8, height=7, width=5, 3 components...
457        v.extend_from_slice(&[0xFF, 0xC0, 0x00, 0x11, 0x08]);
458        v.extend_from_slice(&7u16.to_be_bytes());
459        v.extend_from_slice(&5u16.to_be_bytes());
460        v.extend_from_slice(&[3, 1, 0x22, 0, 2, 0x11, 0, 3, 0x11, 0]); // components
461        v
462    }
463
464    /// A structurally complete JPEG of `w`×`h` (header only; no pixels).
465    fn jpeg(w: u16, h: u16, tag: u8) -> Vec<u8> {
466        let mut v = vec![0xFF, 0xD8, 0xFF, 0xC0, 0x00, 0x0B, 0x08];
467        v.extend_from_slice(&h.to_be_bytes());
468        v.extend_from_slice(&w.to_be_bytes());
469        v.extend_from_slice(&[1, 1, 0x11, 0]);
470        v.extend_from_slice(&[0xFF, 0xDA, 0x00, 0x08, 1, 1, 0, 0, 63, 0]);
471        v.extend_from_slice(&[tag, 0xFF, 0x00, tag, 0xFF, 0xD9]);
472        v
473    }
474
475    #[test]
476    fn sniffs_and_reads_jpeg_dimensions() {
477        let jpg = jpeg_header_5x7();
478        assert_eq!(probe_jpeg(&jpg), 100);
479        assert_eq!(probe_jpeg(b"\xFF\xD8\xEEnope"), 0);
480
481        let mut dem = JpegDemuxer::new(Box::new(Cursor::new(jpg.clone())));
482        let streams = dem.read_header().unwrap();
483        assert_eq!(streams[0].codec_id, CodecId::Jpeg);
484        assert_eq!((streams[0].width, streams[0].height), (5, 7));
485        assert!(
486            dem.stream_metadata(0).is_none(),
487            "a plain JPEG has no MPF tags"
488        );
489        assert_eq!(dem.read_packet().unwrap().data, jpg);
490        assert!(matches!(dem.read_packet(), Err(Error::Eof)));
491    }
492
493    #[derive(Clone, Default)]
494    struct Sink(Arc<Mutex<Vec<u8>>>);
495    impl Write for Sink {
496        fn write(&mut self, buf: &[u8]) -> std::io::Result<usize> {
497            self.0.lock().unwrap().extend_from_slice(buf);
498            Ok(buf.len())
499        }
500        fn flush(&mut self) -> std::io::Result<()> {
501            Ok(())
502        }
503    }
504
505    fn demux(file: &[u8]) -> (Vec<Stream>, Vec<Dictionary>, Vec<Packet>) {
506        let mut dem = JpegDemuxer::new(Box::new(Cursor::new(file.to_vec())));
507        let streams = dem.read_header().unwrap();
508        let md = (0..streams.len())
509            .map(|i| dem.stream_metadata(i).cloned().unwrap_or_default())
510            .collect();
511        let mut packets = Vec::new();
512        while let Ok(p) = dem.read_packet() {
513            packets.push(p);
514        }
515        (streams, md, packets)
516    }
517
518    #[test]
519    fn mux_then_demux_round_trips_every_image() {
520        let imgs = [jpeg(64, 48, 1), jpeg(32, 24, 2), jpeg(16, 12, 3)];
521        let sink = Sink::default();
522        let mut mux = MpfMuxer::new(Box::new(sink.clone()));
523        let streams: Vec<Stream> = (0..3).map(|i| Stream::new(i, CodecId::Jpeg)).collect();
524        let mut thumb = Dictionary::new();
525        thumb.set(keys::MPF_TYPE, "large_thumbnail_vga");
526        mux.set_stream_metadata(1, &thumb);
527        mux.write_header(&streams).unwrap();
528        // Out of order on purpose: the trailer lays them out by stream.
529        for i in [2, 0, 1] {
530            mux.write_packet(&Packet::from_data(i, imgs[i].clone()))
531                .unwrap();
532        }
533        mux.write_trailer().unwrap();
534        let file = sink.0.lock().unwrap().clone();
535        assert_eq!(probe_mpf(&file), 101);
536
537        let (streams, md, packets) = demux(&file);
538        assert_eq!(streams.len(), 3);
539        assert_eq!(
540            streams
541                .iter()
542                .map(|s| (s.width, s.height))
543                .collect::<Vec<_>>(),
544            [(64, 48), (32, 24), (16, 12)]
545        );
546        assert_eq!(streams[0].disposition.default, Some(true));
547        assert_eq!(streams[1].disposition.default, Some(false));
548        assert_eq!(md[0].get(keys::MPF_TYPE), Some("primary"));
549        assert_eq!(md[0].get(keys::MPF_FLAGS), Some("dependent_parent"));
550        assert_eq!(md[1].get(keys::MPF_TYPE), Some("large_thumbnail_vga"));
551        assert_eq!(md[1].get(keys::MPF_TYPE_CODE), Some("0x010001"));
552        assert_eq!(md[1].get(keys::MPF_FLAGS), Some("dependent_child"));
553        assert_eq!(md[2].get(keys::MPF_TYPE), Some("undefined"));
554        assert_eq!(md[2].get(keys::MPF_ENTRY), Some("2"));
555        assert_eq!(md[2].get(keys::MPF_SOURCE), Some("index"));
556        assert_eq!(packets.len(), 3);
557        assert_eq!(packets[1].data, imgs[1]);
558        assert_eq!(packets[2].data, imgs[2]);
559        assert_eq!(
560            packets.iter().map(|p| p.data.len()).sum::<usize>(),
561            file.len()
562        );
563    }
564
565    #[test]
566    fn discarded_streams_are_skipped() {
567        let mut file = jpeg(8, 8, 1);
568        file.extend_from_slice(&jpeg(4, 4, 2));
569        let mut dem = JpegDemuxer::new(Box::new(Cursor::new(file)));
570        let streams = dem.read_header().unwrap();
571        assert_eq!(streams.len(), 2, "trailing-JPEG policy");
572        dem.set_discard(&[true, false]);
573        let p = dem.read_packet().unwrap();
574        assert_eq!((p.stream_index, p.data), (1, jpeg(4, 4, 2)));
575        assert!(matches!(dem.read_packet(), Err(Error::Eof)));
576    }
577
578    #[test]
579    fn muxers_refuse_what_they_cannot_write() {
580        let mut jpeg_mux = JpegMuxer::new(Box::new(Sink::default()));
581        let two = [Stream::new(0, CodecId::Jpeg), Stream::new(1, CodecId::Jpeg)];
582        assert!(jpeg_mux.write_header(&two).is_err());
583
584        let mut mux = MpfMuxer::new(Box::new(Sink::default()));
585        assert!(mux.write_header(&[Stream::new(0, CodecId::Png)]).is_err());
586        let mut mux = MpfMuxer::new(Box::new(Sink::default()));
587        mux.write_header(&two).unwrap();
588        mux.write_packet(&Packet::from_data(0, jpeg(8, 8, 1)))
589            .unwrap();
590        assert!(mux
591            .write_packet(&Packet::from_data(0, jpeg(8, 8, 1)))
592            .is_err());
593        assert!(mux.write_trailer().is_err(), "stream 1 never got an image");
594    }
595
596    #[test]
597    fn probe_mpf_needs_the_index_segment() {
598        let plain = jpeg(8, 8, 1);
599        assert_eq!(probe_mpf(&plain), 0);
600        assert_eq!(probe_mpf(b"\xFF\xD8\xFF\xE2\x00"), 0);
601        for cut in 0..plain.len() {
602            let _ = probe_mpf(&plain[..cut]);
603        }
604    }
605}