legit-hasher 0.1.6

Every legit-hashes definition, images and text, for callers in any language: a command-line runner over JSON lines with resumable batches, and with the `serve` feature a worker that takes hashing tasks from Redis Streams
Documentation
//! Applying an EXIF orientation tag to decoded pixels: exact moves of whole pixels, no resampling.

/// Whether `tag` is an EXIF orientation that moves pixels: 2 to 8. Tag 1 is the identity, and any
/// other value isn't an orientation, so neither changes the image.
pub(crate) const fn moves_pixels(tag: u16) -> bool {
    matches!(tag, 2..=8)
}

/// Turn a `width` × `height` image of `channels` bytes a pixel the way EXIF orientation `tag`
/// says, so it displays upright. Returns the new width, height and pixels.
///
/// The tags, as EXIF defines them: 2 mirrors left to right, 3 turns half a turn, 4 mirrors top to
/// bottom, 5 transposes (mirrors along the main diagonal), 6 turns a quarter clockwise, 7
/// transverses (mirrors along the other diagonal) and 8 turns a quarter anticlockwise. Tags 5 to 8
/// swap the width and height. Any other tag returns the image unchanged.
pub(crate) fn reorient(width: u32, height: u32, channels: usize, pixels: Vec<u8>, tag: u16) -> (u32, u32, Vec<u8>) {
    if !moves_pixels(tag) {
        return (width, height, pixels);
    }
    let (w, h) = (width as usize, height as usize);
    let (out_width, out_height) = if tag >= 5 { (h, w) } else { (w, h) };
    let mut out = Vec::with_capacity(pixels.len());
    for y in 0..out_height {
        for x in 0..out_width {
            // The source pixel of output pixel `(x, y)`...
            let (sx, sy) = match tag {
                2 => (w - 1 - x, y),
                3 => (w - 1 - x, h - 1 - y),
                4 => (x, h - 1 - y),
                5 => (y, x),
                6 => (y, h - 1 - x),
                7 => (w - 1 - y, h - 1 - x),
                _ => (w - 1 - y, x),
            };
            let at = (sy * w + sx) * channels;
            out.extend_from_slice(&pixels[at..at + channels]);
        }
    }
    // The sides are those of the input, swapped or not, so they fit `u32`...
    (out_width as u32, out_height as u32, out)
}

#[cfg(test)]
mod tests {
    use super::*;

    /// A 3 × 2 image, one byte a pixel:
    ///
    /// ```text
    /// 1 2 3
    /// 4 5 6
    /// ```
    fn sample() -> Vec<u8> {
        vec![1, 2, 3, 4, 5, 6]
    }

    #[test]
    fn each_tag_moves_pixels_as_exif_defines_it() {
        let cases: [(u16, u32, u32, [u8; 6]); 8] = [
            (1, 3, 2, [1, 2, 3, 4, 5, 6]),
            (2, 3, 2, [3, 2, 1, 6, 5, 4]),
            (3, 3, 2, [6, 5, 4, 3, 2, 1]),
            (4, 3, 2, [4, 5, 6, 1, 2, 3]),
            (5, 2, 3, [1, 4, 2, 5, 3, 6]),
            (6, 2, 3, [4, 1, 5, 2, 6, 3]),
            (7, 2, 3, [6, 3, 5, 2, 4, 1]),
            (8, 2, 3, [3, 6, 2, 5, 1, 4]),
        ];
        for (tag, width, height, pixels) in cases {
            assert_eq!(
                reorient(3, 2, 1, sample(), tag),
                (width, height, pixels.to_vec()),
                "tag {tag}"
            );
        }
        assert_eq!(reorient(3, 2, 1, sample(), 9), (3, 2, sample()));
    }

    #[test]
    fn whole_pixels_move_together() {
        let rgba: Vec<u8> = (0..8).collect();
        assert_eq!(reorient(2, 1, 4, rgba, 6), (1, 2, vec![0, 1, 2, 3, 4, 5, 6, 7]));
    }
}