mirror of
https://github.com/servo/servo.git
synced 2025-08-05 05:30:08 +01:00
Auto merge of #21660 - servo:webgl, r=jdm
Some more drive-by fixes for rgba8_image_to_tex_image_data <!-- Reviewable:start --> This change is [<img src="https://reviewable.io/review_button.svg" height="34" align="absmiddle" alt="Reviewable"/>](https://reviewable.io/reviews/servo/servo/21660) <!-- Reviewable:end -->
This commit is contained in:
commit
b0ee750fba
1 changed files with 18 additions and 29 deletions
|
@ -4017,14 +4017,15 @@ fn rgba8_image_to_tex_image_data(
|
||||||
match (format, data_type) {
|
match (format, data_type) {
|
||||||
(TexFormat::RGBA, TexDataType::UnsignedByte) => pixels,
|
(TexFormat::RGBA, TexDataType::UnsignedByte) => pixels,
|
||||||
(TexFormat::RGB, TexDataType::UnsignedByte) => {
|
(TexFormat::RGB, TexDataType::UnsignedByte) => {
|
||||||
// Remove alpha channel
|
for i in 0..pixel_count {
|
||||||
let mut rgb8 = Vec::<u8>::with_capacity(pixel_count * 3);
|
let rgb = {
|
||||||
for rgba8 in pixels.chunks(4) {
|
let rgb = &pixels[i * 4..i * 4 + 3];
|
||||||
rgb8.push(rgba8[0]);
|
[rgb[0], rgb[1], rgb[2]]
|
||||||
rgb8.push(rgba8[1]);
|
};
|
||||||
rgb8.push(rgba8[2]);
|
pixels[i * 3..i * 3 + 3].copy_from_slice(&rgb);
|
||||||
}
|
}
|
||||||
rgb8
|
pixels.truncate(pixel_count * 3);
|
||||||
|
pixels
|
||||||
},
|
},
|
||||||
(TexFormat::Alpha, TexDataType::UnsignedByte) => {
|
(TexFormat::Alpha, TexDataType::UnsignedByte) => {
|
||||||
for i in 0..pixel_count {
|
for i in 0..pixel_count {
|
||||||
|
@ -4126,8 +4127,8 @@ fn rgba8_image_to_tex_image_data(
|
||||||
|
|
||||||
(TexFormat::Luminance, TexDataType::Float) => {
|
(TexFormat::Luminance, TexDataType::Float) => {
|
||||||
for rgba8 in pixels.chunks_mut(4) {
|
for rgba8 in pixels.chunks_mut(4) {
|
||||||
let p = luminance(rgba8[0], rgba8[1], rgba8[2]);
|
let p = rgba8[0] as f32;
|
||||||
NativeEndian::write_f32(rgba8, p as f32);
|
NativeEndian::write_f32(rgba8, p);
|
||||||
}
|
}
|
||||||
pixels
|
pixels
|
||||||
},
|
},
|
||||||
|
@ -4135,8 +4136,7 @@ fn rgba8_image_to_tex_image_data(
|
||||||
(TexFormat::LuminanceAlpha, TexDataType::Float) => {
|
(TexFormat::LuminanceAlpha, TexDataType::Float) => {
|
||||||
let mut data = Vec::<u8>::with_capacity(pixel_count * 8);
|
let mut data = Vec::<u8>::with_capacity(pixel_count * 8);
|
||||||
for rgba8 in pixels.chunks(4) {
|
for rgba8 in pixels.chunks(4) {
|
||||||
let p = luminance(rgba8[0], rgba8[1], rgba8[2]);
|
data.write_f32::<NativeEndian>(rgba8[0] as f32).unwrap();
|
||||||
data.write_f32::<NativeEndian>(p as f32).unwrap();
|
|
||||||
data.write_f32::<NativeEndian>(rgba8[3] as f32).unwrap();
|
data.write_f32::<NativeEndian>(rgba8[3] as f32).unwrap();
|
||||||
}
|
}
|
||||||
data
|
data
|
||||||
|
@ -4172,23 +4172,20 @@ fn rgba8_image_to_tex_image_data(
|
||||||
},
|
},
|
||||||
(TexFormat::Luminance, TexDataType::HalfFloat) => {
|
(TexFormat::Luminance, TexDataType::HalfFloat) => {
|
||||||
for i in 0..pixel_count {
|
for i in 0..pixel_count {
|
||||||
let p = {
|
let p = f16::from_f32(pixels[i * 4] as f32).as_bits();
|
||||||
let rgb = &pixels[i * 4..i * 4 + 3];
|
|
||||||
f16::from_f32(luminance(rgb[0], rgb[1], rgb[2]) as f32).as_bits()
|
|
||||||
};
|
|
||||||
NativeEndian::write_u16(&mut pixels[i * 2..i * 2 + 2], p);
|
NativeEndian::write_u16(&mut pixels[i * 2..i * 2 + 2], p);
|
||||||
}
|
}
|
||||||
pixels.truncate(pixel_count * 2);
|
pixels.truncate(pixel_count * 2);
|
||||||
pixels
|
pixels
|
||||||
},
|
},
|
||||||
(TexFormat::LuminanceAlpha, TexDataType::HalfFloat) => {
|
(TexFormat::LuminanceAlpha, TexDataType::HalfFloat) => {
|
||||||
let mut data = Vec::<u8>::with_capacity(pixel_count * 8);
|
for rgba8 in pixels.chunks_mut(4) {
|
||||||
for rgba8 in pixels.chunks(4) {
|
let lum = f16::from_f32(rgba8[0] as f32).as_bits();
|
||||||
let p = luminance(rgba8[0], rgba8[1], rgba8[2]);
|
let a = f16::from_f32(rgba8[3] as f32).as_bits();
|
||||||
data.write_u16::<NativeEndian>(f16::from_f32(p as f32).as_bits()).unwrap();
|
NativeEndian::write_u16(&mut rgba8[0..2], lum);
|
||||||
data.write_u16::<NativeEndian>(f16::from_f32(rgba8[3] as f32).as_bits()).unwrap();
|
NativeEndian::write_u16(&mut rgba8[2..4], a);
|
||||||
}
|
}
|
||||||
data
|
pixels
|
||||||
},
|
},
|
||||||
|
|
||||||
// Validation should have ensured that we only hit the
|
// Validation should have ensured that we only hit the
|
||||||
|
@ -4197,11 +4194,3 @@ fn rgba8_image_to_tex_image_data(
|
||||||
_ => unreachable!("Unsupported formats {:?} {:?}", format, data_type)
|
_ => unreachable!("Unsupported formats {:?} {:?}", format, data_type)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// https://en.wikipedia.org/wiki/Relative_luminance
|
|
||||||
#[inline]
|
|
||||||
fn luminance(r: u8, g: u8, b: u8) -> u8 {
|
|
||||||
(0.2126 * (r as f32) +
|
|
||||||
0.7152 * (g as f32) +
|
|
||||||
0.0722 * (b as f32)) as u8
|
|
||||||
}
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue