Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge some byte swap/premultiply functions in their own crate
- Loading branch information
Showing
18 changed files
with
85 additions
and
96 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.