use fearless_simd::prelude::*;
use fearless_simd::{Level, dispatch, u8x32, u16x16, u32x8};
const RGB_SHUFFLE: [[[[u8; 32]; 2]; 3]; 3] = {
let mut table = [[[[0; 32]; 2]; 3]; 3];
let mut byte = 0;
while byte < 96 {
let (block, channel, lane) = (byte % 48 / 16, byte % 3, byte / 48 * 16 + byte % 16);
table[block][channel][0][lane] = (byte % 48 / 3) as u8;
table[block][channel][1][lane] = 0xff;
byte += 1;
}
table
};
#[inline(always)]
fn scale5(v: u16) -> u8 {
(v << 3 | v >> 2) as u8
}
pub(crate) fn hicolor_to_rgb888(pixels: &[u16], out: &mut [u8]) {
hicolor_to_rgb888_at(Level::new(), pixels, out);
}
fn hicolor_to_rgb888_at(level: Level, pixels: &[u16], out: &mut [u8]) {
if level.is_fallback() {
hicolor_scalar(pixels, out);
} else {
dispatch!(level, simd => hicolor_kernel(simd, pixels, out));
}
}
#[inline(always)]
fn hicolor_scalar(pixels: &[u16], out: &mut [u8]) {
for (rgb, &p) in out.as_chunks_mut::<3>().0.iter_mut().zip(pixels) {
*rgb = [scale5(p >> 10 & 31), scale5(p >> 5 & 31), scale5(p & 31)];
}
}
#[inline(always)]
fn hicolor_kernel<S: Simd>(simd: S, pixels: &[u16], out: &mut [u8]) {
let (src, src_rest) = pixels.as_chunks::<32>();
let (dst, dst_rest) = out.as_chunks_mut::<96>();
#[cfg(target_arch = "wasm32")]
let table = std::hint::black_box(&RGB_SHUFFLE);
#[cfg(not(target_arch = "wasm32"))]
let table = &RGB_SHUFFLE;
let mut masks = [[[u8x32::splat(simd, 0); 2]; 3]; 3];
for (block, table) in masks.iter_mut().zip(table) {
for (channel, table) in block.iter_mut().zip(table) {
for (mask, table) in channel.iter_mut().zip(table) {
*mask = u8x32::from_slice(simd, table);
}
}
}
for (pixels, out) in src.iter().zip(dst) {
let (p0, p1) = pixels.split_at(16);
let (p0, p1) = (u16x16::from_slice(simd, p0), u16x16::from_slice(simd, p1));
let r = simd.narrow_u16x16(red(p0), red(p1));
let g = simd.narrow_u16x16(green(p0), green(p1));
let b = simd.narrow_u16x16(blue(p0), blue(p1));
let (first, second) = out.split_at_mut(48);
let blocks = first
.as_chunks_mut::<16>()
.0
.iter_mut()
.zip(second.as_chunks_mut::<16>().0);
for ((first, second), [mr, mg, mb]) in blocks.zip(masks) {
let rgb = channel_bytes(simd, r, mr)
| channel_bytes(simd, g, mg)
| channel_bytes(simd, b, mb);
let (lo, hi) = rgb.split();
lo.store_slice(first);
hi.store_slice(second);
}
}
hicolor_scalar(src_rest, dst_rest);
}
#[inline(always)]
fn channel_bytes<S: Simd>(simd: S, plane: u8x32<S>, [pick, keep]: [u8x32<S>; 2]) -> u8x32<S> {
simd.swizzle_dyn_within_blocks_u8x32(plane, pick) & keep
}
#[inline(always)]
fn red<S: Simd>(p: u16x16<S>) -> u16x16<S> {
((p >> 7) & 0xf8) | ((p >> 12) & 0x07)
}
#[inline(always)]
fn green<S: Simd>(p: u16x16<S>) -> u16x16<S> {
((p >> 2) & 0xf8) | ((p >> 7) & 0x07)
}
#[inline(always)]
fn blue<S: Simd>(p: u16x16<S>) -> u16x16<S> {
((p << 3) & 0xf8) | ((p >> 2) & 0x07)
}
pub(crate) fn indexed_to_rgb888(pixels: &[u8], palette: &[[u8; 3]], out: &mut [u8]) {
let mut table = [0u32; 256];
for (entry, &[r, g, b]) in table.iter_mut().zip(palette) {
*entry = u32::from_le_bytes([r, g, b, 0]);
}
let (quads, rest) = pixels.as_chunks::<4>();
let (out_quads, out_rest) = out.as_chunks_mut::<12>();
for (&[a, b, c, d], out) in quads.iter().zip(out_quads) {
let [a, b, c, d] = [a, b, c, d].map(|index| table[usize::from(index)]);
let words = [a | b << 24, b >> 8 | c << 16, c >> 16 | d << 8];
for (out, word) in out.as_chunks_mut::<4>().0.iter_mut().zip(words) {
*out = word.to_le_bytes();
}
}
for (rgb, &index) in out_rest.as_chunks_mut::<3>().0.iter_mut().zip(rest) {
let [r, g, b, _] = table[usize::from(index)].to_le_bytes();
*rgb = [r, g, b];
}
}
#[inline(always)]
fn hicolor_xrgb(p: u16) -> u32 {
let p = u32::from(p);
let rgb = (p & 0x7c00) << 9 | (p & 0x03e0) << 6 | (p & 0x001f) << 3;
rgb | (rgb >> 5 & 0x07_0707)
}
#[inline(always)]
fn hicolor_xrgb_x8<S: Simd>(p: u32x8<S>) -> u32x8<S> {
let rgb = ((p & 0x7c00) << 9) | ((p & 0x03e0) << 6) | ((p & 0x001f) << 3);
rgb | ((rgb >> 5) & 0x07_0707)
}
#[inline(always)]
fn hicolor_rgba(p: u16) -> u32 {
let p = u32::from(p);
let bgr = (p & 0x7c00) >> 7 | (p & 0x03e0) << 6 | (p & 0x001f) << 19;
0xff00_0000 | bgr | (bgr >> 5 & 0x07_0707)
}
#[inline(always)]
fn hicolor_rgba_x8<S: Simd>(p: u32x8<S>) -> u32x8<S> {
let bgr = ((p & 0x7c00) >> 7) | ((p & 0x03e0) << 6) | ((p & 0x001f) << 19);
bgr | ((bgr >> 5) & 0x07_0707) | 0xff00_0000
}
pub(crate) fn hicolor_to_xrgb8888(pixels: &[u16], out: &mut [u32]) {
hicolor_to_xrgb8888_at(Level::new(), pixels, out);
}
fn hicolor_to_xrgb8888_at(level: Level, pixels: &[u16], out: &mut [u32]) {
if level.is_fallback() {
xrgb_scalar(pixels, out);
} else {
dispatch!(level, simd => xrgb_kernel(simd, pixels, out));
}
}
#[inline(always)]
fn xrgb_scalar(pixels: &[u16], out: &mut [u32]) {
for (out, &p) in out.iter_mut().zip(pixels) {
*out = hicolor_xrgb(p);
}
}
#[inline(always)]
fn xrgb_kernel<S: Simd>(simd: S, pixels: &[u16], out: &mut [u32]) {
let (src, src_rest) = pixels.as_chunks::<16>();
let (dst, dst_rest) = out.as_chunks_mut::<16>();
for (pixels, out) in src.iter().zip(dst) {
let (lo, hi) = simd.widen_u16x16(u16x16::from_slice(simd, pixels));
let (out_lo, out_hi) = out.split_at_mut(8);
hicolor_xrgb_x8(lo).store_slice(out_lo);
hicolor_xrgb_x8(hi).store_slice(out_hi);
}
xrgb_scalar(src_rest, dst_rest);
}
pub(crate) fn hicolor_to_rgba8888(pixels: &[u16], out: &mut [u8]) {
hicolor_to_rgba8888_at(Level::new(), pixels, out);
}
fn hicolor_to_rgba8888_at(level: Level, pixels: &[u16], out: &mut [u8]) {
if level.is_fallback() {
rgba_scalar(pixels, out);
} else {
dispatch!(level, simd => rgba_kernel(simd, pixels, out));
}
}
#[inline(always)]
fn rgba_scalar(pixels: &[u16], out: &mut [u8]) {
for (out, &p) in out.as_chunks_mut::<4>().0.iter_mut().zip(pixels) {
*out = hicolor_rgba(p).to_le_bytes();
}
}
#[inline(always)]
fn rgba_kernel<S: Simd>(simd: S, pixels: &[u16], out: &mut [u8]) {
let (src, src_rest) = pixels.as_chunks::<16>();
let (dst, dst_rest) = out.as_chunks_mut::<64>();
for (pixels, out) in src.iter().zip(dst) {
let (lo, hi) = simd.widen_u16x16(u16x16::from_slice(simd, pixels));
let (out_lo, out_hi) = out.split_at_mut(32);
hicolor_rgba_x8(lo).to_bytes().store_slice(out_lo);
hicolor_rgba_x8(hi).to_bytes().store_slice(out_hi);
}
rgba_scalar(src_rest, dst_rest);
}
pub(crate) fn indexed_to_xrgb8888(pixels: &[u8], palette: &[[u8; 3]], out: &mut [u32]) {
let mut table = [0; 256];
for (entry, &[r, g, b]) in table.iter_mut().zip(palette) {
*entry = u32::from_be_bytes([0, r, g, b]);
}
for (out, &index) in out.iter_mut().zip(pixels) {
*out = table[usize::from(index)];
}
}
pub(crate) fn indexed_to_rgba8888(pixels: &[u8], palette: &[[u8; 3]], out: &mut [u8]) {
let mut table = [[0, 0, 0, 0xff]; 256];
for (entry, &[r, g, b]) in table.iter_mut().zip(palette) {
*entry = [r, g, b, 0xff];
}
for (out, &index) in out.as_chunks_mut::<4>().0.iter_mut().zip(pixels) {
*out = table[usize::from(index)];
}
}
#[cfg(test)]
mod tests {
use super::*;
fn reference(pixels: &[u16]) -> Vec<u8> {
pixels
.iter()
.flat_map(|&p| {
let scale = |v: u16| (v << 3 | v >> 2) as u8;
[scale(p >> 10 & 31), scale(p >> 5 & 31), scale(p & 31)]
})
.collect()
}
#[test]
fn hicolor_kernel_matches_reference_on_every_pixel_value() {
let pixels: Vec<u16> = (0..=u16::MAX).collect();
let expected = reference(&pixels);
for level in [Level::new(), Level::baseline()] {
let mut out = vec![0; pixels.len() * 3];
hicolor_to_rgb888_at(level, &pixels, &mut out);
assert_eq!(out, expected, "{level:?}");
}
}
#[test]
fn hicolor_kernel_handles_lengths_off_the_vector_width() {
let pixels: Vec<u16> = (0..50).map(|i| i * 1311 + 7).collect();
for len in 0..=pixels.len() {
let mut out = vec![0xaa; len * 3];
hicolor_to_rgb888(&pixels[..len], &mut out);
assert_eq!(out, reference(&pixels[..len]), "{len} pixels");
}
}
fn four_byte(rgb: &[u8]) -> (Vec<u32>, Vec<u8>) {
let rgb = rgb.as_chunks::<3>().0;
let xrgb = rgb
.iter()
.map(|&[r, g, b]| u32::from_be_bytes([0, r, g, b]));
let rgba = rgb.iter().flat_map(|&[r, g, b]| [r, g, b, 0xff]);
(xrgb.collect(), rgba.collect())
}
#[test]
fn four_byte_formats_match_rgb888_on_every_pixel_value() {
let pixels: Vec<u16> = (0..=u16::MAX).collect();
let (xrgb, rgba) = four_byte(&reference(&pixels));
for level in [Level::new(), Level::baseline()] {
let mut out = vec![0; pixels.len()];
hicolor_to_xrgb8888_at(level, &pixels, &mut out);
assert!(out == xrgb, "XRGB8888 at {level:?}");
let mut out = vec![0; pixels.len() * 4];
hicolor_to_rgba8888_at(level, &pixels, &mut out);
assert!(out == rgba, "RGBA8888 at {level:?}");
}
}
#[test]
fn four_byte_kernels_handle_lengths_off_the_vector_width() {
let pixels: Vec<u16> = (0..50).map(|i| i * 1311 + 7).collect();
for len in 0..=pixels.len() {
let (xrgb, rgba) = four_byte(&reference(&pixels[..len]));
let mut out = vec![0xaaaa_aaaa; len];
hicolor_to_xrgb8888(&pixels[..len], &mut out);
assert_eq!(out, xrgb, "{len} pixels");
let mut out = vec![0xaa; len * 4];
hicolor_to_rgba8888(&pixels[..len], &mut out);
assert_eq!(out, rgba, "{len} pixels");
}
}
#[test]
fn indexed_four_byte_formats_look_up_the_palette() {
let palette = [[1, 2, 3], [4, 5, 6]];
let mut xrgb = [0xaaaa_aaaa; 3];
indexed_to_xrgb8888(&[1, 0, 200], &palette, &mut xrgb);
assert_eq!(xrgb, [0x0004_0506, 0x0001_0203, 0]);
let mut rgba = [0xaa; 12];
indexed_to_rgba8888(&[1, 0, 200], &palette, &mut rgba);
assert_eq!(rgba, [4, 5, 6, 0xff, 1, 2, 3, 0xff, 0, 0, 0, 0xff]);
}
#[test]
fn indexed_pixels_outside_the_palette_are_black() {
let palette = [[1, 2, 3], [4, 5, 6]];
let mut out = [0xaa; 9];
indexed_to_rgb888(&[1, 0, 200], &palette, &mut out);
assert_eq!(out, [4, 5, 6, 1, 2, 3, 0, 0, 0]);
}
#[test]
fn indexed_rgb888_matches_a_lookup_per_pixel() {
let palette: Vec<[u8; 3]> = (0..200u8)
.map(|i| [i, i ^ 0x5a, i.wrapping_mul(7)])
.collect();
let pixels: Vec<u8> = (0..=255).rev().chain(0..=255).collect();
for len in (0..20).chain(500..=pixels.len()) {
let pixels = &pixels[..len];
let expected: Vec<u8> = pixels
.iter()
.flat_map(|&p| palette.get(usize::from(p)).copied().unwrap_or_default())
.collect();
let mut out = vec![0xaa; len * 3];
indexed_to_rgb888(pixels, &palette, &mut out);
assert_eq!(out, expected, "{len} pixels");
}
}
}