imagine 0.5.3

A crate to help with images.
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
//! Module for BMP files.

use crate::{util::*, ImagineError};
use bitfrob::U8BitIterHigh;
use core::{mem::size_of, num::NonZeroU8};
use pack1::U32LE;
use pixel_formats::*;

pub mod iters;
pub mod nice_header;
pub mod raw_headers;
pub mod rle;

use self::{iters::*, nice_header::*, raw_headers::*, rle::*};

/// Checks if a BMP's initial 14 bytes are correct.
#[inline]
pub fn bmp_signature_is_correct(bytes: &[u8]) -> bool {
  if let Ok((file_header, _)) = try_pull_pod::<BitmapFileHeader>(bytes) {
    file_header.ty == "BM"
      && file_header.file_size.get().try_into().unwrap_or(usize::MAX) == bytes.len()
      && file_header.bitmap_offset.get().try_into().unwrap_or(usize::MAX) <= bytes.len()
  } else {
    false
  }
}

/// Computes the number of bytes per line when padding is applied.
///
/// BMP scanlines within the image data are padded to a multiple of 4 bytes
/// (except when the image is RLE encoded).
#[inline]
pub fn padded_bytes_per_line(width: u32, bits_per_pixel: u16) -> Result<usize, ImagineError> {
  let width: usize = width.try_into()?;
  let bits_per_pixel = usize::from(bits_per_pixel);
  let bits_per_line = bits_per_pixel.checked_mul(width).ok_or(ImagineError::CheckedMath)?;
  let bytes_per_line = bits_per_line / 8 + usize::from(bits_per_line % 8 != 0);
  let dwords_per_line = bytes_per_line / 4 + usize::from(bytes_per_line % 4 != 0);
  dwords_per_line.checked_mul(4).ok_or(ImagineError::CheckedMath)
}

/// Automatically allocate and fill in a [Bitmap](crate::image::Bitmap).
///
/// * Paletted images will automatically get the color from the palette (illegal
///   palette index values will be black).
/// * RBG images with or without compression will be processed.
/// * RGBA images will fail.
///
/// The output is automatically flipped as necessary so that the output will be
/// oriented with the origin in the top left.
#[inline]
#[cfg(feature = "alloc")]
#[cfg_attr(docs_rs, doc(cfg(feature = "alloc")))]
pub fn bmp_try_bitmap_rgb<P>(
  bytes: &[u8], origin_top_left: bool,
) -> Result<crate::Bitmap<P>, ImagineError>
where
  P: Copy + From<r32g32b32_Sfloat>,
{
  #[allow(unused)]
  use alloc::vec::Vec;

  let header = bmp_get_nice_header(bytes)?;
  let target_pixel_count: usize =
    header.width.checked_mul(header.height).ok_or(ImagineError::CheckedMath)?.try_into()?;
  let mut bitmap: crate::Bitmap<P> = {
    let mut pixels = Vec::new();
    pixels.try_reserve(target_pixel_count)?;
    crate::Bitmap { width: header.width, height: header.height, pixels }
  };
  let width = header.width;
  let data_span = header.data_span;
  let image_bytes = bytes.get(data_span.0..data_span.1).ok_or(ImagineError::CheckedMath)?;
  match header.data_format {
    BmpDataFormat::Indexed1 { palette_span }
    | BmpDataFormat::Indexed4 { palette_span }
    | BmpDataFormat::Indexed4Rle { palette_span }
    | BmpDataFormat::Indexed8 { palette_span }
    | BmpDataFormat::Indexed8Rle { palette_span } => {
      // If we make a 256 element palette then indexing into the palette with a u8
      // will tend to optimize away the bounds check, and it usually goes much
      // faster than using `.get(i).unwrap_or_default()` or similar.
      let mut palette: [P; 256] = [r32g32b32_Sfloat::BLACK.into(); 256];
      let pal_bytes = bytes.get(palette_span.0..palette_span.1).ok_or(ImagineError::CheckedMath)?;
      for (chunk, p) in pal_bytes.chunks_exact(4).zip(palette.iter_mut()) {
        *p = P::from(r32g32b32_Sfloat::from(r8g8b8_Srgb { b: chunk[0], g: chunk[1], r: chunk[2] }));
      }
      let black: P = P::from(r32g32b32_Sfloat::BLACK);
      bitmap.pixels.resize(target_pixel_count, black);
      match header.data_format {
        BmpDataFormat::Indexed4Rle { .. } => {
          let mut x: u32 = 0;
          let mut y: u32 = 0;
          'rle_for: for rle_op in bmp_iter_rle4(image_bytes) {
            match rle_op {
              BmpRle4Op::EndOfBmp => break 'rle_for,
              BmpRle4Op::Newline => {
                x = 0;
                y = y.wrapping_add(1);
              }
              BmpRle4Op::Run { count, index_h, index_l } => {
                let mut it = [index_h, index_l].into_iter().cycle();
                for _ in 0..count.get() {
                  if let Some(p) = bitmap.get_mut(x, y) {
                    *p = palette[usize::from(it.next().unwrap())]
                  }
                  x = x.wrapping_add(1);
                }
              }
              BmpRle4Op::Delta { right, up } => {
                x = x.wrapping_add(right);
                y = y.wrapping_add(up);
              }
              BmpRle4Op::Raw4 { a, b, c, d } => {
                for val in [a, b, c, d] {
                  if let Some(p) = bitmap.get_mut(x, y) {
                    *p = palette[usize::from(val)]
                  }
                  x = x.wrapping_add(1);
                }
              }
              BmpRle4Op::Raw3 { a, b, c } => {
                for val in [a, b, c] {
                  if let Some(p) = bitmap.get_mut(x, y) {
                    *p = palette[usize::from(val)]
                  }
                  x = x.wrapping_add(1);
                }
              }
              BmpRle4Op::Raw2 { a, b } => {
                for val in [a, b] {
                  if let Some(p) = bitmap.get_mut(x, y) {
                    *p = palette[usize::from(val)]
                  }
                  x = x.wrapping_add(1);
                }
              }
              BmpRle4Op::Raw1 { a } => {
                if let Some(p) = bitmap.get_mut(x, y) {
                  *p = palette[usize::from(a)]
                }
                x = x.wrapping_add(1);
              }
            }
          }
        }
        BmpDataFormat::Indexed8Rle { .. } => {
          let mut x: u32 = 0;
          let mut y: u32 = 0;
          'rle_for: for rle_op in bmp_iter_rle8(image_bytes) {
            match rle_op {
              BmpRle8Op::EndOfBmp => break 'rle_for,
              BmpRle8Op::Newline => {
                x = 0;
                y = y.wrapping_add(1);
              }
              BmpRle8Op::Run { count, index } => {
                let color = palette[usize::from(index)];
                for _ in 0..count.get() {
                  if let Some(p) = bitmap.get_mut(x, y) {
                    *p = color;
                  }
                  x = x.wrapping_add(1);
                }
              }
              BmpRle8Op::Delta { right, up } => {
                x = x.wrapping_add(right);
                y = y.wrapping_add(up);
              }
              BmpRle8Op::Raw2 { q, w } => {
                if let Some(p) = bitmap.get_mut(x, y) {
                  *p = palette[usize::from(q)]
                }
                x = x.wrapping_add(1);
                if let Some(p) = bitmap.get_mut(x, y) {
                  *p = palette[usize::from(w)]
                }
                x = x.wrapping_add(1);
              }
              BmpRle8Op::Raw1 { q } => {
                if let Some(p) = bitmap.get_mut(x, y) {
                  *p = palette[usize::from(q)]
                }
                x = x.wrapping_add(1);
              }
            }
          }
        }
        other => {
          let bits_per_pixel = match other {
            BmpDataFormat::Indexed1 { .. } => 1,
            BmpDataFormat::Indexed4 { .. } => 4,
            BmpDataFormat::Indexed8 { .. } => 8,
            _ => 8,
          };
          bitmap.pixels.extend(
            bmp_iter_pal_indexes_no_compression(image_bytes, width, bits_per_pixel)
              .map(|i| palette[usize::from(i)]),
          )
        }
      }
    }
    BmpDataFormat::BGR24 => bitmap.pixels.extend(
      bmp_iter_bgr24(image_bytes, width)
        .map(|[b, g, r]| P::from(r32g32b32_Sfloat::from(r8g8b8_Srgb { r, g, b }))),
    ),
    BmpDataFormat::Bitmask16RGB { r_mask, g_mask, b_mask } => {
      bitmap
        .pixels
        .extend(bmp_iter_bitmask16_rgb(image_bytes, r_mask, g_mask, b_mask, width).map(P::from));
    }
    BmpDataFormat::Bitmask32RGB { r_mask, g_mask, b_mask } => {
      if r_mask.count_ones() == 8 && g_mask.count_ones() == 8 && b_mask.count_ones() == 8 {
        bitmap.pixels.extend(
          bmp_iter_bitmask32_srgb(image_bytes, r_mask, g_mask, b_mask, width)
            .map(|srgb| P::from(r32g32b32_Sfloat::from(srgb))),
        );
      } else {
        bitmap.pixels.extend(
          bmp_iter_bitmask32_linear_rgb(image_bytes, r_mask, g_mask, b_mask, width).map(P::from),
        );
      }
    }
    _ => return Err(ImagineError::CheckedMath),
  }
  let black: P = P::from(r32g32b32_Sfloat::BLACK);
  bitmap.pixels.resize(target_pixel_count, black);
  if header.origin_top_left != origin_top_left {
    bitmap.vertical_flip();
  }
  Ok(bitmap)
}

/// Automatically allocate and fill in a [Bitmap](crate::Bitmap).
///
/// * Paletted images will automatically get the color from the palette (illegal
///   palette index values will be transparent black).
/// * RBG images with or without compression will be processed, an alpha value
///   of 1.0 is automatically added.
/// * RGBA images with or without compression will be processed.
///
/// The output is automatically flipped as necessary so that the output will be
/// oriented with the origin in the top left.
#[inline]
#[cfg(feature = "alloc")]
#[cfg_attr(docs_rs, doc(cfg(feature = "alloc")))]
pub fn bmp_try_bitmap_rgba<P>(
  bytes: &[u8], origin_top_left: bool,
) -> Result<crate::Bitmap<P>, ImagineError>
where
  P: Copy + From<r32g32b32a32_Sfloat>,
{
  #[allow(unused)]
  use alloc::vec::Vec;

  let header = bmp_get_nice_header(bytes)?;
  if header.width > 17_000 || header.height > 17_000 {
    return Err(ImagineError::DimensionsTooLarge);
  }
  let target_pixel_count: usize =
    header.width.checked_mul(header.height).ok_or(ImagineError::CheckedMath)?.try_into()?;
  let mut bitmap: crate::Bitmap<P> = {
    let mut pixels = Vec::new();
    pixels.try_reserve(target_pixel_count)?;
    crate::Bitmap { width: header.width, height: header.height, pixels }
  };
  let width = header.width;
  let data_span = header.data_span;
  let image_bytes = bytes.get(data_span.0..data_span.1).ok_or(ImagineError::Parse)?;
  match header.data_format {
    BmpDataFormat::Indexed1 { palette_span }
    | BmpDataFormat::Indexed4 { palette_span }
    | BmpDataFormat::Indexed4Rle { palette_span }
    | BmpDataFormat::Indexed8 { palette_span }
    | BmpDataFormat::Indexed8Rle { palette_span } => {
      // If we make a 256 element palette then indexing into the palette with a u8
      // will tend to optimize away the bounds check, and it usually goes much
      // faster than using `.get(i).unwrap_or_default()` or similar.
      let mut palette: [P; 256] = [r32g32b32a32_Sfloat::TRANSPARENT_BLACK.into(); 256];
      let pal_bytes = bytes.get(palette_span.0..palette_span.1).ok_or(ImagineError::Parse)?;
      for (chunk, p) in pal_bytes.chunks_exact(4).zip(palette.iter_mut()) {
        *p = P::from(r32g32b32a32_Sfloat::from(r8g8b8a8_Srgb {
          b: chunk[0],
          g: chunk[1],
          r: chunk[2],
          a: u8::MAX,
        }));
      }
      match header.data_format {
        BmpDataFormat::Indexed4Rle { .. } => {
          let black: P = P::from(r32g32b32a32_Sfloat::TRANSPARENT_BLACK);
          bitmap.pixels.resize(target_pixel_count, black);
          let mut x: u32 = 0;
          let mut y: u32 = 0;
          'rle_for: for rle_op in bmp_iter_rle4(image_bytes) {
            match rle_op {
              BmpRle4Op::EndOfBmp => break 'rle_for,
              BmpRle4Op::Newline => {
                x = 0;
                y = y.wrapping_add(1);
              }
              BmpRle4Op::Run { count, index_h, index_l } => {
                let mut it = [index_h, index_l].into_iter().cycle();
                for _ in 0..count.get() {
                  if let Some(p) = bitmap.get_mut(x, y) {
                    *p = palette[usize::from(it.next().unwrap())]
                  }
                  x = x.wrapping_add(1);
                }
              }
              BmpRle4Op::Delta { right, up } => {
                x = x.wrapping_add(right);
                y = y.wrapping_add(up);
              }
              BmpRle4Op::Raw4 { a, b, c, d } => {
                for val in [a, b, c, d] {
                  if let Some(p) = bitmap.get_mut(x, y) {
                    *p = palette[usize::from(val)]
                  }
                  x = x.wrapping_add(1);
                }
              }
              BmpRle4Op::Raw3 { a, b, c } => {
                for val in [a, b, c] {
                  if let Some(p) = bitmap.get_mut(x, y) {
                    *p = palette[usize::from(val)]
                  }
                  x = x.wrapping_add(1);
                }
              }
              BmpRle4Op::Raw2 { a, b } => {
                for val in [a, b] {
                  if let Some(p) = bitmap.get_mut(x, y) {
                    *p = palette[usize::from(val)]
                  }
                  x = x.wrapping_add(1);
                }
              }
              BmpRle4Op::Raw1 { a } => {
                if let Some(p) = bitmap.get_mut(x, y) {
                  *p = palette[usize::from(a)]
                }
                x = x.wrapping_add(1);
              }
            }
          }
        }
        BmpDataFormat::Indexed8Rle { .. } => {
          let black: P = P::from(r32g32b32a32_Sfloat::TRANSPARENT_BLACK);
          bitmap.pixels.resize(target_pixel_count, black);
          let mut x: u32 = 0;
          let mut y: u32 = 0;
          'rle_for: for rle_op in bmp_iter_rle8(image_bytes) {
            match rle_op {
              BmpRle8Op::EndOfBmp => break 'rle_for,
              BmpRle8Op::Newline => {
                x = 0;
                y = y.wrapping_add(1);
              }
              BmpRle8Op::Run { count, index } => {
                let color = palette[usize::from(index)];
                for _ in 0..count.get() {
                  if let Some(p) = bitmap.get_mut(x, y) {
                    *p = color;
                  }
                  x = x.wrapping_add(1);
                }
              }
              BmpRle8Op::Delta { right, up } => {
                x = x.wrapping_add(right);
                y = y.wrapping_add(up);
              }
              BmpRle8Op::Raw2 { q, w } => {
                if let Some(p) = bitmap.get_mut(x, y) {
                  *p = palette[usize::from(q)]
                }
                x = x.wrapping_add(1);
                if let Some(p) = bitmap.get_mut(x, y) {
                  *p = palette[usize::from(w)]
                }
                x = x.wrapping_add(1);
              }
              BmpRle8Op::Raw1 { q } => {
                if let Some(p) = bitmap.get_mut(x, y) {
                  *p = palette[usize::from(q)]
                }
                x = x.wrapping_add(1);
              }
            }
          }
        }
        other => {
          let bits_per_pixel = match other {
            BmpDataFormat::Indexed1 { .. } => 1,
            BmpDataFormat::Indexed4 { .. } => 4,
            BmpDataFormat::Indexed8 { .. } => 8,
            _ => 8,
          };
          bitmap.pixels.extend(
            bmp_iter_pal_indexes_no_compression(image_bytes, width, bits_per_pixel)
              .map(|i| palette[usize::from(i)]),
          )
        }
      }
    }
    BmpDataFormat::BGR24 => bitmap
      .pixels
      .extend(bmp_iter_bgr24(image_bytes, width).map(|[b, g, r]| {
        P::from(r32g32b32a32_Sfloat::from(r8g8b8a8_Srgb { r, g, b, a: u8::MAX }))
      })),
    BmpDataFormat::Bitmask16RGB { r_mask, g_mask, b_mask } => {
      bitmap.pixels.extend(
        bmp_iter_bitmask16_rgb(image_bytes, r_mask, g_mask, b_mask, width)
          .map(|rgb| P::from(r32g32b32a32_Sfloat::from(rgb))),
      );
    }
    BmpDataFormat::Bitmask32RGB { r_mask, g_mask, b_mask } => {
      if r_mask.count_ones() == 8 && g_mask.count_ones() == 8 && b_mask.count_ones() == 8 {
        bitmap.pixels.extend(
          bmp_iter_bitmask32_srgb(image_bytes, r_mask, g_mask, b_mask, width)
            .map(|srgb| P::from(r32g32b32a32_Sfloat::from(r32g32b32_Sfloat::from(srgb)))),
        );
      } else {
        bitmap.pixels.extend(
          bmp_iter_bitmask32_linear_rgb(image_bytes, r_mask, g_mask, b_mask, width)
            .map(|rgb| P::from(r32g32b32a32_Sfloat::from(rgb))),
        );
      }
    }
    BmpDataFormat::Bitmask16RGBA { r_mask, g_mask, b_mask, a_mask } => {
      bitmap.pixels.extend(
        bmp_iter_bitmask16_rgba(image_bytes, r_mask, g_mask, b_mask, a_mask, width).map(P::from),
      );
    }
    BmpDataFormat::Bitmask32RGBA { r_mask, g_mask, b_mask, a_mask } => {
      if r_mask.count_ones() == 8 && g_mask.count_ones() == 8 && b_mask.count_ones() == 8 {
        bitmap.pixels.extend(
          bmp_iter_bitmask32_srgba(image_bytes, r_mask, g_mask, b_mask, a_mask, width)
            .map(|srgb| P::from(r32g32b32a32_Sfloat::from(srgb))),
        );
      } else {
        bitmap.pixels.extend(
          bmp_iter_bitmask32_linear_rgba(image_bytes, r_mask, g_mask, b_mask, a_mask, width)
            .map(P::from),
        );
      }
    }
  }
  let black: P = P::from(r32g32b32a32_Sfloat::TRANSPARENT_BLACK);
  bitmap.pixels.resize(target_pixel_count, black);
  if header.origin_top_left != origin_top_left {
    bitmap.vertical_flip();
  }
  Ok(bitmap)
}