Skip to content

Commit

Permalink
improve comment
Browse files Browse the repository at this point in the history
  • Loading branch information
francisdb committed May 7, 2024
1 parent 28aa426 commit 7619596
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 5 deletions.
36 changes: 33 additions & 3 deletions src/vpx/expanded.rs
Original file line number Diff line number Diff line change
Expand Up @@ -321,7 +321,15 @@ fn write_image_bmp(
let copy = lzw_compressed_data.to_vec();
let cursor = io::Cursor::new(copy);
let mut reader = LzwReader::new(Box::new(cursor), width, height, 4);
let decompressed_rgba = reader.decompress();
let decompressed_bgra = reader.decompress();

// assert that alpha is 255 for all pixels
for bgra in decompressed_bgra.chunks_exact(4) {
assert_eq!(bgra[3], 255);
}

// convert to RGBA
let decompressed_rgba: Vec<u8> = swap_red_and_blue(&decompressed_bgra);

let rgba_image = image::RgbaImage::from_raw(width, height, decompressed_rgba)
.expect("Decompressed image data does not match dimensions");
Expand All @@ -343,6 +351,15 @@ fn write_image_bmp(
})
}

/// Can convert between RGBA and BGRA by swapping the red and blue channels
fn swap_red_and_blue(data: &[u8]) -> Vec<u8> {
let mut swapped = Vec::with_capacity(data.len());
for chunk in data.chunks_exact(4) {
swapped.extend_from_slice(&[chunk[2], chunk[1], chunk[0], chunk[3]])
}
swapped
}

fn read_images<P: AsRef<Path>>(expanded_dir: &P) -> io::Result<Vec<ImageData>> {
// TODO do we actually need an index?
let images_index_path = expanded_dir.as_ref().join("images.json");
Expand Down Expand Up @@ -403,9 +420,12 @@ fn read_image_bmp(data: &[u8], width: u32, height: u32) -> io::Result<Vec<u8>> {
assert_eq!(image.color(), image::ColorType::Rgb8);

// get the raw image data
let raw = image.to_rgba8().into_raw();
let raw_rgba = image.to_rgba8().into_raw();

// convert to BGRA
let raw_bgra: Vec<u8> = swap_red_and_blue(&raw_rgba);

let mut encoder = LzwWriter::new(raw, width, height, 4);
let mut encoder = LzwWriter::new(raw_bgra, width, height, 4);
Ok(encoder.compress_bits(8 + 1))
}

Expand Down Expand Up @@ -1499,6 +1519,16 @@ mod test {
Ok(assert_eq!(LZW_COMPRESSED_DATA, *read_compressed_data))
}

#[test]
pub fn test_swap_red_and_blue() {
let rgba = vec![1, 2, 3, 255];
let bgra = swap_red_and_blue(&rgba);
assert_eq!(bgra, vec![3, 2, 1, 255]);
// a second time should be the same as the original
let rgba2 = swap_red_and_blue(&bgra);
assert_eq!(rgba2, rgba);
}

#[test]
pub fn test_expand_write_read() -> TestResult {
let expanded_path = testdir!();
Expand Down
5 changes: 3 additions & 2 deletions src/vpx/image.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,12 @@ impl fmt::Debug for ImageDataJpeg {
}

/**
* An bitmap blob, typically used by textures.
* A bitmap blob, typically used by textures.
*/
#[derive(PartialEq)]
pub struct ImageDataBits {
/// Lzw compressed raw BMP 32-bit rgba bitmap data
/// Lzw compressed raw BMP 32-bit sBGRA bitmap data
/// However we expect the alpha channel to always be 255
pub lzw_compressed_data: Vec<u8>,
}

Expand Down

0 comments on commit 7619596

Please sign in to comment.