aboutsummaryrefslogtreecommitdiffstats
path: root/components/pixels
diff options
context:
space:
mode:
Diffstat (limited to 'components/pixels')
-rw-r--r--components/pixels/Cargo.toml10
-rw-r--r--components/pixels/lib.rs40
2 files changed, 50 insertions, 0 deletions
diff --git a/components/pixels/Cargo.toml b/components/pixels/Cargo.toml
new file mode 100644
index 00000000000..49dcc3833cf
--- /dev/null
+++ b/components/pixels/Cargo.toml
@@ -0,0 +1,10 @@
+[package]
+name = "pixels"
+version = "0.0.1"
+authors = ["The Servo Project Developers"]
+license = "MPL-2.0"
+publish = false
+
+[lib]
+name = "pixels"
+path = "lib.rs"
diff --git a/components/pixels/lib.rs b/components/pixels/lib.rs
new file mode 100644
index 00000000000..d91b14cb650
--- /dev/null
+++ b/components/pixels/lib.rs
@@ -0,0 +1,40 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+// TODO(pcwalton): Speed up with SIMD, or better yet, find some way to not do this.
+pub fn byte_swap_colors_inplace(pixels: &mut [u8]) {
+ assert!(pixels.len() % 4 == 0);
+ for rgba in pixels.chunks_mut(4) {
+ let b = rgba[0];
+ rgba[0] = rgba[2];
+ rgba[2] = b;
+ }
+}
+
+pub fn byte_swap_and_premultiply_inplace(pixels: &mut [u8]) {
+ assert!(pixels.len() % 4 == 0);
+ for rgba in pixels.chunks_mut(4) {
+ let b = rgba[0];
+ rgba[0] = multiply_u8_color(rgba[2], rgba[3]);
+ rgba[1] = multiply_u8_color(rgba[1], rgba[3]);
+ rgba[2] = multiply_u8_color(b, rgba[3]);
+ }
+}
+
+/// Returns true if the pixels were found to be completely opaque.
+pub fn premultiply_inplace(pixels: &mut [u8]) -> bool {
+ assert!(pixels.len() % 4 == 0);
+ let mut is_opaque = true;
+ for rgba in pixels.chunks_mut(4) {
+ rgba[0] = multiply_u8_color(rgba[0], rgba[3]);
+ rgba[1] = multiply_u8_color(rgba[1], rgba[3]);
+ rgba[2] = multiply_u8_color(rgba[2], rgba[3]);
+ is_opaque = is_opaque && rgba[3] == 255;
+ }
+ is_opaque
+}
+
+pub fn multiply_u8_color(a: u8, b: u8) -> u8 {
+ return (a as u32 * b as u32 / 255) as u8;
+}