zenbitmaps 0.1.5

PNM/PAM/PFM, BMP, farbfeld, QOI, TGA, and Radiance HDR image codec
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
use super::*;

// ══════════════════════════════════════════════════════════════════════
// HDR capabilities and descriptors
// ══════════════════════════════════════════════════════════════════════

static HDR_ENCODE_CAPS: EncodeCapabilities = EncodeCapabilities::new()
    .with_hdr(true)
    .with_native_f32(true)
    .with_stop(true)
    .with_enforces_max_pixels(true);

static HDR_DECODE_CAPS: DecodeCapabilities = DecodeCapabilities::new()
    .with_cheap_probe(true)
    .with_hdr(true)
    .with_native_f32(true)
    .with_streaming(true)
    .with_stop(true)
    .with_enforces_max_pixels(true)
    .with_enforces_max_memory(true)
    .with_enforces_max_input_bytes(true);

static HDR_ENCODE_DESCRIPTORS: &[PixelDescriptor] =
    &[PixelDescriptor::RGBF32_LINEAR, PixelDescriptor::RGB8_SRGB];

static HDR_DECODE_DESCRIPTORS: &[PixelDescriptor] = &[PixelDescriptor::RGBF32_LINEAR];

// ══════════════════════════════════════════════════════════════════════
// HDR codec
// ══════════════════════════════════════════════════════════════════════

// ── HdrEncoderConfig ─────────────────────────────────────────────

/// Encoding configuration for Radiance HDR (RGBE) format.
///
/// Accepts `RgbF32` (native) or `Rgb8` (converted via /255.0) input layouts.
#[derive(Clone, Debug)]
pub struct HdrEncoderConfig {
    limits: ResourceLimits,
}

impl Default for HdrEncoderConfig {
    fn default() -> Self {
        Self::new()
    }
}

impl HdrEncoderConfig {
    /// Create a new HDR encoder config with default settings.
    pub fn new() -> Self {
        Self {
            limits: ResourceLimits::none(),
        }
    }
}

impl zencodec::encode::EncoderConfig for HdrEncoderConfig {
    type Error = BitmapError;
    type Job = HdrEncodeJob;

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

    fn supported_descriptors() -> &'static [PixelDescriptor] {
        HDR_ENCODE_DESCRIPTORS
    }

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

    fn is_lossless(&self) -> Option<bool> {
        Some(false) // RGBE shared exponent is technically lossy
    }

    fn job(self) -> HdrEncodeJob {
        HdrEncodeJob {
            config: self,
            limits: None,
            stop: None,
        }
    }
}

// ── HdrEncodeJob ─────────────────────────────────────────────────

/// Per-operation HDR encode job.
pub struct HdrEncodeJob {
    config: HdrEncoderConfig,
    limits: Option<ResourceLimits>,
    stop: Option<zencodec::StopToken>,
}

impl zencodec::encode::EncodeJob for HdrEncodeJob {
    type Error = BitmapError;
    type Enc = HdrEncoder;
    type AnimationFrameEnc = ();

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

    fn with_metadata(self, _meta: Metadata) -> Self {
        self
    }

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

    fn encoder(self) -> Result<HdrEncoder, BitmapError> {
        Ok(HdrEncoder {
            config: self.config,
            limits: self.limits,
            stop: self.stop,
            accumulator: None,
        })
    }

    fn animation_frame_encoder(self) -> Result<(), BitmapError> {
        Err(BitmapError::from(
            zencodec::UnsupportedOperation::AnimationEncode,
        ))
    }
}

// ── HdrEncoder ───────────────────────────────────────────────────

/// Accumulator for streaming HDR encode via `push_rows`/`finish`.
struct HdrEncodeAccumulator {
    data: Vec<u8>,
    width: u32,
    total_rows: u32,
    layout: crate::PixelLayout,
}

/// Single-image HDR encoder.
pub struct HdrEncoder {
    config: HdrEncoderConfig,
    limits: Option<ResourceLimits>,
    stop: Option<zencodec::StopToken>,
    accumulator: Option<HdrEncodeAccumulator>,
}

impl HdrEncoder {
    fn effective_limits(&self) -> Option<Limits> {
        self.limits.as_ref().map(convert_limits).or_else(|| {
            let l = &self.config.limits;
            if l.max_pixels.is_some()
                || l.max_memory_bytes.is_some()
                || l.max_width.is_some()
                || l.max_height.is_some()
            {
                Some(convert_limits(l))
            } else {
                None
            }
        })
    }
}

fn pixel_slice_to_hdr_layout(desc: PixelDescriptor) -> Result<crate::PixelLayout, BitmapError> {
    match (desc.channel_type(), desc.layout()) {
        (ChannelType::F32, ChannelLayout::Rgb) => Ok(crate::PixelLayout::RgbF32),
        (ChannelType::U8, ChannelLayout::Rgb) => Ok(crate::PixelLayout::Rgb8),
        _ => Err(BitmapError::UnsupportedVariant(alloc::format!(
            "HDR encode: unsupported pixel format: {desc:?}"
        ))),
    }
}

impl zencodec::encode::Encoder for HdrEncoder {
    type Error = BitmapError;

    fn reject(op: zencodec::UnsupportedOperation) -> BitmapError {
        BitmapError::from(op)
    }

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

    fn encode(self, pixels: PixelSlice<'_>) -> Result<EncodeOutput, BitmapError> {
        let stop: &dyn Stop = match &self.stop {
            Some(s) => s,
            None => &enough::Unstoppable,
        };
        let w = pixels.width();
        let h = pixels.rows();

        if let Some(limits) = self.effective_limits() {
            limits.check(w, h)?;
        }

        let layout = pixel_slice_to_hdr_layout(pixels.descriptor())?;
        let bytes = pixels.contiguous_bytes();
        let encoded = crate::hdr::encode(&bytes, w, h, layout, stop)?;
        Ok(EncodeOutput::new(encoded, ImageFormat::Hdr))
    }

    fn push_rows(&mut self, rows: PixelSlice<'_>) -> Result<(), BitmapError> {
        let layout = pixel_slice_to_hdr_layout(rows.descriptor())?;

        let acc = self
            .accumulator
            .get_or_insert_with(|| HdrEncodeAccumulator {
                data: Vec::new(),
                width: rows.width(),
                total_rows: 0,
                layout,
            });

        if acc.width != rows.width() || acc.layout != layout {
            return Err(BitmapError::InvalidData(
                "push_rows: width or pixel format changed".into(),
            ));
        }

        let bytes = rows.contiguous_bytes();
        acc.data.extend_from_slice(&bytes);
        acc.total_rows += rows.rows();
        Ok(())
    }

    fn finish(self) -> Result<EncodeOutput, BitmapError> {
        let acc = self
            .accumulator
            .ok_or_else(|| BitmapError::InvalidData("finish() without push_rows()".into()))?;

        let stop: &dyn Stop = match &self.stop {
            Some(s) => s,
            None => &enough::Unstoppable,
        };

        let encoded = crate::hdr::encode(&acc.data, acc.width, acc.total_rows, acc.layout, stop)?;
        Ok(EncodeOutput::new(encoded, ImageFormat::Hdr))
    }
}

// ── HdrDecoderConfig ─────────────────────────────────────────────

/// Decoding configuration for Radiance HDR (RGBE) format.
#[derive(Clone, Debug)]
pub struct HdrDecoderConfig {
    limits: Option<Limits>,
}

impl Default for HdrDecoderConfig {
    fn default() -> Self {
        Self::new()
    }
}

impl HdrDecoderConfig {
    /// Create a new HDR decoder config with default settings.
    pub fn new() -> Self {
        Self { limits: None }
    }
}

impl zencodec::decode::DecoderConfig for HdrDecoderConfig {
    type Error = BitmapError;
    type Job<'a> = HdrDecodeJob;

    fn formats() -> &'static [ImageFormat] {
        &[ImageFormat::Hdr]
    }

    fn supported_descriptors() -> &'static [PixelDescriptor] {
        HDR_DECODE_DESCRIPTORS
    }

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

    fn job<'a>(self) -> Self::Job<'a> {
        HdrDecodeJob {
            config: self,
            limits: None,
            stop: None,
            max_input_bytes: None,
            policy: None,
        }
    }
}

// ── HdrDecodeJob ─────────────────────────────────────────────────

/// Per-operation HDR decode job.
pub struct HdrDecodeJob {
    config: HdrDecoderConfig,
    limits: Option<Limits>,
    stop: Option<zencodec::StopToken>,
    max_input_bytes: Option<u64>,
    policy: Option<DecodePolicy>,
}

impl<'a> zencodec::decode::DecodeJob<'a> for HdrDecodeJob {
    type Error = BitmapError;
    type Dec = HdrDecoder<'a>;
    type StreamDec = HdrStreamingDecoder;
    type AnimationFrameDec = zencodec::Unsupported<BitmapError>;

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

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

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

    fn probe(&self, data: &[u8]) -> Result<ImageInfo, BitmapError> {
        let (width, height, _offset) = crate::hdr::decode::parse_header(data)?;
        let cicp = zencodec::Cicp::new(1, 8, 0, true);
        Ok(ImageInfo::new(width, height, ImageFormat::Hdr)
            .with_alpha(false)
            .with_bit_depth(32)
            .with_channel_count(3)
            .with_cicp(cicp)
            .with_source_encoding_details(BitmapSourceEncoding))
    }

    fn output_info(&self, data: &[u8]) -> Result<OutputInfo, BitmapError> {
        let (width, height, _offset) = crate::hdr::decode::parse_header(data)?;
        Ok(
            OutputInfo::full_decode(width, height, PixelDescriptor::RGBF32_LINEAR)
                .with_alpha(false),
        )
    }

    fn decoder(
        self,
        data: Cow<'a, [u8]>,
        _preferred: &[PixelDescriptor],
    ) -> Result<HdrDecoder<'a>, BitmapError> {
        if let Some(max) = self.max_input_bytes
            && data.len() as u64 > max
        {
            return Err(BitmapError::LimitExceeded(alloc::format!(
                "input size {} exceeds limit {max}",
                data.len()
            )));
        }
        Ok(HdrDecoder {
            config: self.config,
            limits: self.limits,
            data,
            stop: self.stop,
        })
    }

    fn push_decoder(
        self,
        data: Cow<'a, [u8]>,
        sink: &mut dyn zencodec::decode::DecodeRowSink,
        preferred: &[PixelDescriptor],
    ) -> Result<OutputInfo, Self::Error> {
        zencodec::helpers::copy_decode_to_sink(self, data, sink, preferred, |e| {
            BitmapError::InvalidData(e.to_string())
        })
    }

    fn streaming_decoder(
        self,
        data: Cow<'a, [u8]>,
        _preferred: &[PixelDescriptor],
    ) -> Result<HdrStreamingDecoder, BitmapError> {
        if let Some(max) = self.max_input_bytes
            && data.len() as u64 > max
        {
            return Err(BitmapError::LimitExceeded(alloc::format!(
                "input size {} exceeds limit {max}",
                data.len()
            )));
        }
        let (width, height, _offset) = crate::hdr::decode::parse_header(&data)?;

        let limits = self.limits.or(self.config.limits);
        if let Some(ref lim) = limits {
            lim.check(width, height)?;
        }

        let row_bytes = (width as usize)
            .checked_mul(12)
            .ok_or(BitmapError::DimensionsTooLarge { width, height })?;

        let total_bytes = row_bytes
            .checked_mul(height as usize)
            .ok_or(BitmapError::DimensionsTooLarge { width, height })?;

        crate::limits::check_output_size(total_bytes, limits.as_ref())?;

        let cicp = zencodec::Cicp::new(1, 8, 0, true);
        let info = ImageInfo::new(width, height, ImageFormat::Hdr)
            .with_alpha(false)
            .with_bit_depth(32)
            .with_channel_count(3)
            .with_cicp(cicp)
            .with_source_encoding_details(BitmapSourceEncoding);

        let stop: &dyn Stop = match &self.stop {
            Some(s) => s,
            None => &enough::Unstoppable,
        };
        let decoded = crate::hdr::decode(&data, limits.as_ref(), stop)?;
        let pixels_owned: Vec<u8> = decoded.pixels().to_vec();

        Ok(HdrStreamingDecoder {
            info,
            width,
            height,
            decoded_bytes: pixels_owned,
            row_bytes,
            current_row: 0,
        })
    }

    fn animation_frame_decoder(
        self,
        _data: Cow<'a, [u8]>,
        _preferred: &[PixelDescriptor],
    ) -> Result<zencodec::Unsupported<BitmapError>, BitmapError> {
        Err(BitmapError::from(
            zencodec::UnsupportedOperation::AnimationDecode,
        ))
    }
}

// ── HdrDecoder ───────────────────────────────────────────────────

/// Single-image HDR decoder.
pub struct HdrDecoder<'a> {
    config: HdrDecoderConfig,
    limits: Option<Limits>,
    data: Cow<'a, [u8]>,
    stop: Option<zencodec::StopToken>,
}

impl HdrDecoder<'_> {
    fn effective_limits(&self) -> Option<&Limits> {
        self.limits.as_ref().or(self.config.limits.as_ref())
    }
}

impl zencodec::decode::Decode for HdrDecoder<'_> {
    type Error = BitmapError;

    fn decode(self) -> Result<DecodeOutput, BitmapError> {
        let limits = self.effective_limits();
        let stop: &dyn Stop = match &self.stop {
            Some(s) => s,
            None => &enough::Unstoppable,
        };
        let decoded = crate::hdr::decode(&self.data, limits, stop)?;
        decode_output_from_internal(&decoded, ImageFormat::Hdr)
    }
}

// ── HdrStreamingDecoder ──────────────────────────────────────────

/// Streaming scanline-batch HDR decoder.
pub struct HdrStreamingDecoder {
    info: ImageInfo,
    width: u32,
    height: u32,
    decoded_bytes: Vec<u8>,
    row_bytes: usize,
    current_row: u32,
}

impl zencodec::decode::StreamingDecode for HdrStreamingDecoder {
    type Error = BitmapError;

    fn next_batch(&mut self) -> Result<Option<(u32, PixelSlice<'_>)>, BitmapError> {
        if self.current_row >= self.height {
            return Ok(None);
        }

        let y = self.current_row;
        let offset = (y as usize) * self.row_bytes;
        let row_data = &self.decoded_bytes[offset..offset + self.row_bytes];

        let slice = PixelSlice::new(
            row_data,
            self.width,
            1,
            self.row_bytes,
            PixelDescriptor::RGBF32_LINEAR,
        )
        .map_err(|e| BitmapError::InvalidData(e.to_string()))?;

        self.current_row += 1;
        Ok(Some((y, slice)))
    }

    fn info(&self) -> &ImageInfo {
        &self.info
    }
}