zencodec 0.1.20

Shared traits and types for zen* image codecs
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
//! Mock animation codec implementing zencodec traits.
//!
//! Supports RGB8 and RGBA8. Implements:
//! - One-shot decode (Decode)
//! - Streaming decode (StreamingDecode)
//! - Full-frame animation decode (AnimationFrameDecoder)
//! - One-shot encode (Encoder) with push_rows + encode_from
//! - Full-frame animation encode (AnimationFrameEncoder)
//!
//! Used to exercise trait paths that the PNM codec doesn't cover.

use std::borrow::Cow;

use zencodec::decode::{
    AnimationFrameDecoder, DecodeCapabilities, DecodeJob, DecodeOutput, DecoderConfig, OutputInfo,
    StreamingDecode,
};
use zencodec::encode::{
    AnimationFrameEncoder, EncodeCapabilities, EncodeJob, EncodeOutput, Encoder, EncoderConfig,
};
use zencodec::{
    AnimationFrame, ImageFormat, ImageInfo, ImageSequence, Metadata, ResourceLimits,
    UnsupportedOperation,
};

use enough::{Stop, StopReason};
use zencodec::decode::{DecodeRowSink, SinkError};
use zenpixels::{PixelBuffer, PixelDescriptor, PixelSlice, PixelSliceMut};

// =========================================================================
// Error
// =========================================================================

#[derive(Debug)]
pub enum MockError {
    Unsupported(UnsupportedOperation),
    InvalidData(String),
    Cancelled(StopReason),
    LimitExceeded(zencodec::LimitExceeded),
    Sink(SinkError),
}

impl std::fmt::Display for MockError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::Unsupported(e) => write!(f, "mock: unsupported: {e}"),
            Self::InvalidData(s) => write!(f, "mock: invalid: {s}"),
            Self::Cancelled(r) => write!(f, "mock: cancelled: {r}"),
            Self::LimitExceeded(e) => write!(f, "mock: limit: {e}"),
            Self::Sink(e) => write!(f, "mock: sink: {e}"),
        }
    }
}

impl std::error::Error for MockError {
    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
        match self {
            Self::Unsupported(e) => Some(e),
            Self::LimitExceeded(e) => Some(e),
            Self::Sink(e) => Some(e.as_ref()),
            _ => None,
        }
    }
}

impl From<UnsupportedOperation> for MockError {
    fn from(e: UnsupportedOperation) -> Self {
        Self::Unsupported(e)
    }
}

impl From<StopReason> for MockError {
    fn from(r: StopReason) -> Self {
        Self::Cancelled(r)
    }
}

impl From<zencodec::LimitExceeded> for MockError {
    fn from(e: zencodec::LimitExceeded) -> Self {
        Self::LimitExceeded(e)
    }
}

// =========================================================================
// Wire format: trivial header + raw pixels
// =========================================================================
// Header: "MOCK" (4) + width:u32 LE + height:u32 LE + frame_count:u32 LE
//         + bpp:u8 (3=RGB8, 4=RGBA8) + duration_ms:u32 LE per frame
// Then raw pixel data per frame: width * height * bpp bytes

const HEADER_SIZE: usize = 4 + 4 + 4 + 4 + 1;

fn encode_mock_data(frames: &[(PixelSlice<'_>, u32)]) -> Vec<u8> {
    assert!(!frames.is_empty());
    let (first, _) = &frames[0];
    let w = first.width();
    let h = first.rows();
    let bpp = first.descriptor().bytes_per_pixel();
    let frame_count = frames.len() as u32;

    let mut data = Vec::new();
    data.extend_from_slice(b"MOCK");
    data.extend_from_slice(&w.to_le_bytes());
    data.extend_from_slice(&h.to_le_bytes());
    data.extend_from_slice(&frame_count.to_le_bytes());
    data.push(bpp as u8);

    for (ps, duration_ms) in frames {
        data.extend_from_slice(&duration_ms.to_le_bytes());
        for y in 0..ps.rows() {
            data.extend_from_slice(ps.row(y));
        }
    }
    data
}

fn parse_mock_header(data: &[u8]) -> Result<(u32, u32, u32, u8), MockError> {
    if data.len() < HEADER_SIZE || &data[..4] != b"MOCK" {
        return Err(MockError::InvalidData("bad mock header".into()));
    }
    let w = u32::from_le_bytes(data[4..8].try_into().unwrap());
    let h = u32::from_le_bytes(data[8..12].try_into().unwrap());
    let fc = u32::from_le_bytes(data[12..16].try_into().unwrap());
    let bpp = data[16];
    if w == 0 || h == 0 || fc == 0 || (bpp != 3 && bpp != 4) {
        return Err(MockError::InvalidData("bad dimensions or bpp".into()));
    }
    Ok((w, h, fc, bpp))
}

fn descriptor_for_bpp(bpp: u8) -> PixelDescriptor {
    match bpp {
        3 => PixelDescriptor::RGB8_SRGB,
        4 => PixelDescriptor::RGBA8_SRGB,
        _ => unreachable!(),
    }
}

// =========================================================================
// Decode: Config → Job → Decoder / StreamingDecoder / AnimationFrameDecoder
// =========================================================================

#[derive(Clone, Debug)]
pub struct MockDecoderConfig;

static MOCK_DECODE_CAPS: DecodeCapabilities = DecodeCapabilities::new()
    .with_cheap_probe(true)
    .with_animation(true)
    .with_streaming(true)
    .with_native_alpha(true)
    .with_stop(true);

impl DecoderConfig for MockDecoderConfig {
    type Error = MockError;
    type Job<'a> = MockDecodeJob;

    fn formats() -> &'static [ImageFormat] {
        // Use Pnm as a placeholder since we can't create custom formats easily
        &[ImageFormat::Pnm]
    }

    fn supported_descriptors() -> &'static [PixelDescriptor] {
        &[PixelDescriptor::RGB8_SRGB, PixelDescriptor::RGBA8_SRGB]
    }

    fn capabilities() -> &'static DecodeCapabilities {
        &MOCK_DECODE_CAPS
    }

    fn job<'a>(self) -> Self::Job<'a> {
        MockDecodeJob {
            limits: ResourceLimits::none(),
            stop: None,
            policy: None,
            crop: None,
            orientation: None,
            start_frame: None,
            ext: MockDecodeExtensions::default(),
        }
    }
}

pub struct MockDecodeJob {
    limits: ResourceLimits,
    stop: Option<zencodec::StopToken>,
    policy: Option<zencodec::decode::DecodePolicy>,
    crop: Option<(u32, u32, u32, u32)>,
    orientation: Option<zencodec::OrientationHint>,
    start_frame: Option<u32>,
    pub ext: MockDecodeExtensions,
}

impl<'a> DecodeJob<'a> for MockDecodeJob {
    type Error = MockError;
    type Dec = MockDec<'a>;
    type StreamDec = MockStreamDec<'a>;
    type AnimationFrameDec = MockAnimationFrameDec;

    fn with_stop(mut self, stop: zencodec::StopToken) -> Self {
        self.stop = Some(stop);
        self
    }

    fn with_limits(mut self, limits: ResourceLimits) -> Self {
        self.limits = limits;
        self
    }

    fn with_policy(mut self, policy: zencodec::decode::DecodePolicy) -> Self {
        self.policy = Some(policy);
        self
    }

    fn with_crop_hint(mut self, x: u32, y: u32, width: u32, height: u32) -> Self {
        self.crop = Some((x, y, width, height));
        self
    }

    fn with_orientation(mut self, hint: zencodec::OrientationHint) -> Self {
        self.orientation = Some(hint);
        self
    }

    fn with_start_frame_index(mut self, index: u32) -> Self {
        self.start_frame = Some(index);
        self
    }

    fn extensions(&self) -> Option<&dyn std::any::Any> {
        Some(&self.ext)
    }

    fn extensions_mut(&mut self) -> Option<&mut dyn std::any::Any> {
        Some(&mut self.ext)
    }

    fn probe(&self, data: &[u8]) -> Result<ImageInfo, MockError> {
        let (w, h, fc, _bpp) = parse_mock_header(data)?;
        let sequence = if fc > 1 {
            ImageSequence::Animation {
                frame_count: Some(fc),
                loop_count: None,
                random_access: false,
            }
        } else {
            ImageSequence::Single
        };
        Ok(ImageInfo::new(w, h, ImageFormat::Pnm).with_sequence(sequence))
    }

    fn output_info(&self, data: &[u8]) -> Result<OutputInfo, MockError> {
        let (w, h, _fc, bpp) = parse_mock_header(data)?;
        Ok(OutputInfo::full_decode(w, h, descriptor_for_bpp(bpp)))
    }

    fn decoder(
        self,
        data: Cow<'a, [u8]>,
        _preferred: &[PixelDescriptor],
    ) -> Result<MockDec<'a>, MockError> {
        let (w, h, _, _) = parse_mock_header(&data)?;
        self.limits.check_dimensions(w, h)?;
        if let Some(ref stop) = self.stop {
            stop.check()?;
        }
        Ok(MockDec { data })
    }

    fn push_decoder(
        self,
        data: Cow<'a, [u8]>,
        sink: &mut dyn DecodeRowSink,
        preferred: &[PixelDescriptor],
    ) -> Result<OutputInfo, MockError> {
        zencodec::helpers::copy_decode_to_sink(self, data, sink, preferred, MockError::Sink)
    }

    fn streaming_decoder(
        self,
        data: Cow<'a, [u8]>,
        _preferred: &[PixelDescriptor],
    ) -> Result<MockStreamDec<'a>, MockError> {
        let (w, h, _, bpp) = parse_mock_header(&data)?;
        self.limits.check_dimensions(w, h)?;
        Ok(MockStreamDec {
            data,
            current_row: 0,
            width: w,
            height: h,
            bpp,
        })
    }

    fn animation_frame_decoder(
        self,
        data: Cow<'a, [u8]>,
        _preferred: &[PixelDescriptor],
    ) -> Result<MockAnimationFrameDec, MockError> {
        let (w, h, fc, bpp) = parse_mock_header(&data)?;
        self.limits.check_dimensions(w, h)?;
        let owned = data.into_owned();
        Ok(MockAnimationFrameDec {
            data: owned,
            width: w,
            height: h,
            frame_count: fc,
            bpp,
            current_frame: self.start_frame.unwrap_or(0),
        })
    }
}

// --- One-shot decoder ---

#[derive(Debug)]
pub struct MockDec<'a> {
    data: Cow<'a, [u8]>,
}

impl<'a> zencodec::decode::Decode for MockDec<'a> {
    type Error = MockError;

    fn decode(self) -> Result<DecodeOutput, MockError> {
        let (w, h, _fc, bpp) = parse_mock_header(&self.data)?;
        let desc = descriptor_for_bpp(bpp);
        let frame_size = w as usize * h as usize * bpp as usize;
        let data_start = HEADER_SIZE + 4; // skip first frame's duration_ms
        if self.data.len() < data_start + frame_size {
            return Err(MockError::InvalidData("truncated frame data".into()));
        }
        let pixels = &self.data[data_start..data_start + frame_size];
        let buf = PixelBuffer::from_vec(pixels.to_vec(), w, h, desc)
            .map_err(|e| MockError::InvalidData(format!("buffer: {e}")))?;
        let info = ImageInfo::new(w, h, ImageFormat::Pnm);
        Ok(DecodeOutput::new(buf, info))
    }
}

// --- Streaming decoder (yields 1 row at a time) ---

pub struct MockStreamDec<'a> {
    data: Cow<'a, [u8]>,
    current_row: u32,
    width: u32,
    height: u32,
    bpp: u8,
}

impl<'a> StreamingDecode for MockStreamDec<'a> {
    type Error = MockError;

    fn next_batch(&mut self) -> Result<Option<(u32, PixelSlice<'_>)>, MockError> {
        if self.current_row >= self.height {
            return Ok(None);
        }
        let row_bytes = self.width as usize * self.bpp as usize;
        let data_start = HEADER_SIZE + 4; // skip first frame duration
        let offset = data_start + self.current_row as usize * row_bytes;
        if offset + row_bytes > self.data.len() {
            return Err(MockError::InvalidData("truncated streaming data".into()));
        }
        let desc = descriptor_for_bpp(self.bpp);
        let row_data = &self.data[offset..offset + row_bytes];
        let ps = PixelSlice::new(row_data, self.width, 1, row_bytes, desc)
            .map_err(|e| MockError::InvalidData(format!("slice: {e}")))?;
        let y = self.current_row;
        self.current_row += 1;
        Ok(Some((y, ps)))
    }

    fn info(&self) -> &ImageInfo {
        // leak for simplicity in test code
        let info = ImageInfo::new(self.width, self.height, ImageFormat::Pnm);
        Box::leak(Box::new(info))
    }
}

// --- Full-frame animation decoder ---

pub struct MockAnimationFrameDec {
    data: Vec<u8>,
    width: u32,
    height: u32,
    frame_count: u32,
    bpp: u8,
    current_frame: u32,
}

impl AnimationFrameDecoder for MockAnimationFrameDec {
    type Error = MockError;

    fn wrap_sink_error(err: SinkError) -> MockError {
        MockError::Sink(err)
    }

    fn info(&self) -> &ImageInfo {
        Box::leak(Box::new(
            ImageInfo::new(self.width, self.height, ImageFormat::Pnm).with_sequence(
                ImageSequence::Animation {
                    frame_count: Some(self.frame_count),
                    loop_count: None,
                    random_access: false,
                },
            ),
        ))
    }

    fn frame_count(&self) -> Option<u32> {
        Some(self.frame_count)
    }

    fn loop_count(&self) -> Option<u32> {
        Some(0) // infinite loop
    }

    fn render_next_frame(
        &mut self,
        stop: Option<&dyn Stop>,
    ) -> Result<Option<AnimationFrame<'_>>, MockError> {
        if let Some(s) = stop {
            s.check()?;
        }
        if self.current_frame >= self.frame_count {
            return Ok(None);
        }

        let frame_pixels = self.width as usize * self.height as usize * self.bpp as usize;
        let frame_data_size = 4 + frame_pixels; // duration + pixels
        let frame_offset = HEADER_SIZE + self.current_frame as usize * frame_data_size;

        if frame_offset + frame_data_size > self.data.len() {
            return Err(MockError::InvalidData("truncated animation data".into()));
        }

        let duration_ms = u32::from_le_bytes(
            self.data[frame_offset..frame_offset + 4]
                .try_into()
                .unwrap(),
        );
        let pixel_start = frame_offset + 4;
        let pixel_end = pixel_start + frame_pixels;
        let desc = descriptor_for_bpp(self.bpp);
        let row_bytes = self.width as usize * self.bpp as usize;

        let ps = PixelSlice::new(
            &self.data[pixel_start..pixel_end],
            self.width,
            self.height,
            row_bytes,
            desc,
        )
        .map_err(|e| MockError::InvalidData(format!("frame slice: {e}")))?;

        let frame = AnimationFrame::new(ps, duration_ms, self.current_frame);
        self.current_frame += 1;
        Ok(Some(frame))
    }

    fn render_next_frame_to_sink(
        &mut self,
        stop: Option<&dyn Stop>,
        sink: &mut dyn DecodeRowSink,
    ) -> Result<Option<OutputInfo>, MockError> {
        zencodec::helpers::copy_frame_to_sink(self, stop, sink)
    }
}

// =========================================================================
// Extension types (for testing extensions() / extensions_mut())
// =========================================================================

/// Mock encode extensions — codec-specific knobs accessible through dyn dispatch.
#[derive(Debug, Default)]
pub struct MockEncodeExtensions {
    pub optimize: bool,
    pub custom_tag: Option<String>,
}

/// Mock decode extensions.
#[derive(Debug, Default)]
pub struct MockDecodeExtensions {
    pub strict_parsing: bool,
}

// =========================================================================
// Encode: Config → Job → Encoder / AnimationFrameEncoder
// =========================================================================

#[derive(Clone, Debug)]
pub struct MockEncoderConfig {
    quality: Option<f32>,
    effort: Option<i32>,
    lossless: Option<bool>,
    alpha_quality: Option<f32>,
}

impl MockEncoderConfig {
    pub fn new() -> Self {
        Self {
            quality: None,
            effort: None,
            lossless: None,
            alpha_quality: None,
        }
    }
}

static MOCK_ENCODE_CAPS: EncodeCapabilities = EncodeCapabilities::new()
    .with_lossless(true)
    .with_lossy(true)
    .with_native_alpha(true)
    .with_animation(true)
    .with_push_rows(true)
    .with_encode_from(true)
    .with_stop(true)
    .with_icc(true)
    .with_exif(true)
    .with_effort_range(0, 10)
    .with_quality_range(0.0, 100.0);

impl EncoderConfig for MockEncoderConfig {
    type Error = MockError;
    type Job = MockEncodeJob;

    fn format() -> ImageFormat {
        ImageFormat::Pnm
    }

    fn supported_descriptors() -> &'static [PixelDescriptor] {
        &[PixelDescriptor::RGB8_SRGB, PixelDescriptor::RGBA8_SRGB]
    }

    fn capabilities() -> &'static EncodeCapabilities {
        &MOCK_ENCODE_CAPS
    }

    fn with_generic_quality(mut self, quality: f32) -> Self {
        self.quality = Some(quality);
        self
    }

    fn with_generic_effort(mut self, effort: i32) -> Self {
        self.effort = Some(effort);
        self
    }

    fn with_lossless(mut self, lossless: bool) -> Self {
        self.lossless = Some(lossless);
        self
    }

    fn with_alpha_quality(mut self, quality: f32) -> Self {
        self.alpha_quality = Some(quality);
        self
    }

    fn generic_quality(&self) -> Option<f32> {
        self.quality
    }

    fn generic_effort(&self) -> Option<i32> {
        self.effort
    }

    fn is_lossless(&self) -> Option<bool> {
        self.lossless
    }

    fn alpha_quality(&self) -> Option<f32> {
        self.alpha_quality
    }

    fn job(self) -> MockEncodeJob {
        MockEncodeJob {
            limits: ResourceLimits::none(),
            stop: None,
            metadata: None,
            canvas_size: None,
            loop_count: None,
            policy: None,
            ext: MockEncodeExtensions::default(),
        }
    }
}

pub struct MockEncodeJob {
    limits: ResourceLimits,
    stop: Option<zencodec::StopToken>,
    metadata: Option<Metadata>,
    canvas_size: Option<(u32, u32)>,
    loop_count: Option<Option<u32>>,
    policy: Option<zencodec::encode::EncodePolicy>,
    pub ext: MockEncodeExtensions,
}

impl EncodeJob for MockEncodeJob {
    type Error = MockError;
    type Enc = MockEnc;
    type AnimationFrameEnc = MockAnimationFrameEnc;

    fn with_stop(mut self, stop: zencodec::StopToken) -> Self {
        self.stop = Some(stop);
        self
    }

    fn with_limits(mut self, limits: ResourceLimits) -> Self {
        self.limits = limits;
        self
    }

    fn with_policy(mut self, policy: zencodec::encode::EncodePolicy) -> Self {
        self.policy = Some(policy);
        self
    }

    fn with_metadata(mut self, meta: Metadata) -> Self {
        self.metadata = Some(meta);
        self
    }

    fn with_canvas_size(mut self, width: u32, height: u32) -> Self {
        self.canvas_size = Some((width, height));
        self
    }

    fn with_loop_count(mut self, count: Option<u32>) -> Self {
        self.loop_count = Some(count);
        self
    }

    fn extensions(&self) -> Option<&dyn std::any::Any> {
        Some(&self.ext)
    }

    fn extensions_mut(&mut self) -> Option<&mut dyn std::any::Any> {
        Some(&mut self.ext)
    }

    fn encoder(self) -> Result<MockEnc, MockError> {
        Ok(MockEnc {
            accumulated: Vec::new(),
            width: None,
            height: None,
            desc: None,
        })
    }

    fn animation_frame_encoder(self) -> Result<MockAnimationFrameEnc, MockError> {
        Ok(MockAnimationFrameEnc {
            frames: Vec::new(),
            loop_count: self.loop_count.flatten(),
        })
    }
}

// --- Single-image encoder (supports all three paths) ---

pub struct MockEnc {
    accumulated: Vec<u8>,
    width: Option<u32>,
    height: Option<u32>,
    desc: Option<PixelDescriptor>,
}

impl Encoder for MockEnc {
    type Error = MockError;

    fn reject(op: UnsupportedOperation) -> MockError {
        MockError::Unsupported(op)
    }

    fn preferred_strip_height(&self) -> u32 {
        4
    }

    fn encode(self, pixels: PixelSlice<'_>) -> Result<EncodeOutput, MockError> {
        let frame = (pixels, 0u32);
        let data = encode_mock_data(&[frame]);
        Ok(EncodeOutput::new(data, ImageFormat::Pnm))
    }

    fn push_rows(&mut self, rows: PixelSlice<'_>) -> Result<(), MockError> {
        if self.width.is_none() {
            self.width = Some(rows.width());
            self.height = Some(0);
            self.desc = Some(rows.descriptor());
        }
        let h = self.height.as_mut().unwrap();
        for y in 0..rows.rows() {
            self.accumulated.extend_from_slice(rows.row(y));
            *h += 1;
        }
        Ok(())
    }

    fn finish(self) -> Result<EncodeOutput, MockError> {
        let w = self
            .width
            .ok_or(MockError::InvalidData("no rows pushed".into()))?;
        let h = self.height.unwrap();
        let desc = self.desc.unwrap();
        let buf = PixelBuffer::from_vec(self.accumulated, w, h, desc)
            .map_err(|e| MockError::InvalidData(format!("buffer: {e}")))?;
        let ps = buf.as_slice();
        let frame = (ps, 0u32);
        let data = encode_mock_data(&[frame]);
        Ok(EncodeOutput::new(data, ImageFormat::Pnm))
    }

    fn encode_from(
        self,
        source: &mut dyn FnMut(u32, PixelSliceMut<'_>) -> usize,
    ) -> Result<EncodeOutput, MockError> {
        // Pull rows one at a time using a fixed 4-row strip
        let strip_height = 4u32;
        let mut all_data = Vec::new();
        let mut y = 0u32;
        let mut total_h = 0u32;
        let w = 4u32; // fixed for simplicity
        let desc = PixelDescriptor::RGB8_SRGB;
        let bpp = desc.bytes_per_pixel();
        let row_bytes = w as usize * bpp;

        loop {
            let mut strip_buf = vec![0u8; strip_height as usize * row_bytes];
            let rows_written = {
                let ps = PixelSliceMut::new(&mut strip_buf, w, strip_height, row_bytes, desc)
                    .map_err(|e| MockError::InvalidData(format!("pull buf: {e}")))?;
                source(y, ps)
            };
            if rows_written == 0 {
                break;
            }
            all_data.extend_from_slice(&strip_buf[..rows_written * row_bytes]);
            total_h += rows_written as u32;
            y += rows_written as u32;
        }

        if total_h == 0 {
            return Err(MockError::InvalidData("no rows from source".into()));
        }
        let buf = PixelBuffer::from_vec(all_data, w, total_h, desc)
            .map_err(|e| MockError::InvalidData(format!("buffer: {e}")))?;
        let ps = buf.as_slice();
        let data = encode_mock_data(&[(ps, 0)]);
        Ok(EncodeOutput::new(data, ImageFormat::Pnm))
    }
}

// --- Full-frame animation encoder ---

pub struct MockAnimationFrameEnc {
    frames: Vec<(Vec<u8>, u32, u32, u32, PixelDescriptor)>, // data, w, h, duration, desc
    #[allow(dead_code)]
    loop_count: Option<u32>,
}

impl AnimationFrameEncoder for MockAnimationFrameEnc {
    type Error = MockError;

    fn reject(op: UnsupportedOperation) -> MockError {
        MockError::Unsupported(op)
    }

    fn push_frame(
        &mut self,
        pixels: PixelSlice<'_>,
        duration_ms: u32,
        stop: Option<&dyn Stop>,
    ) -> Result<(), MockError> {
        if let Some(s) = stop {
            s.check()?;
        }
        let w = pixels.width();
        let h = pixels.rows();
        let desc = pixels.descriptor();
        let mut data = Vec::with_capacity(w as usize * h as usize * desc.bytes_per_pixel());
        for y in 0..h {
            data.extend_from_slice(pixels.row(y));
        }
        self.frames.push((data, w, h, duration_ms, desc));
        Ok(())
    }

    fn finish(self, stop: Option<&dyn Stop>) -> Result<EncodeOutput, MockError> {
        if let Some(s) = stop {
            s.check()?;
        }
        if self.frames.is_empty() {
            return Err(MockError::InvalidData("no frames".into()));
        }

        // Build slices for encode_mock_data
        let bufs: Vec<PixelBuffer> = self
            .frames
            .iter()
            .map(|(data, w, h, _, desc)| {
                PixelBuffer::from_vec(data.clone(), *w, *h, *desc).unwrap()
            })
            .collect();
        let frame_refs: Vec<(PixelSlice<'_>, u32)> = bufs
            .iter()
            .zip(self.frames.iter())
            .map(|(buf, (_, _, _, dur, _))| (buf.as_slice(), *dur))
            .collect();

        let data = encode_mock_data(&frame_refs);
        Ok(EncodeOutput::new(data, ImageFormat::Pnm))
    }
}