avm1: Deduplicate threshold operations
This commit is contained in:
parent
ea6ea8074a
commit
c6bd431864
|
@ -941,16 +941,20 @@ impl<'gc> BitmapData<'gc> {
|
||||||
/// This implements the threshold operation generically over the test operation performed for each pixel
|
/// This implements the threshold operation generically over the test operation performed for each pixel
|
||||||
/// Returns the number of pixels modified
|
/// Returns the number of pixels modified
|
||||||
#[allow(clippy::too_many_arguments)]
|
#[allow(clippy::too_many_arguments)]
|
||||||
fn threshold_internal<Op: Fn(u32) -> bool>(
|
fn threshold_internal<Op: Fn(u32, u32) -> bool>(
|
||||||
&mut self,
|
&mut self,
|
||||||
source_bitmap: &Self,
|
source_bitmap: &Self,
|
||||||
src_rect: (i32, i32, i32, i32),
|
src_rect: (i32, i32, i32, i32),
|
||||||
dest_point: (i32, i32),
|
dest_point: (i32, i32),
|
||||||
operation: Op,
|
operation: Op,
|
||||||
|
threshold: u32,
|
||||||
colour: u32,
|
colour: u32,
|
||||||
mask: u32,
|
mask: u32,
|
||||||
copy_source: bool,
|
copy_source: bool,
|
||||||
) -> u32 {
|
) -> u32 {
|
||||||
|
// Pre-compute the masked threshold
|
||||||
|
let masked_threshold = threshold & mask;
|
||||||
|
|
||||||
// Extract coords
|
// Extract coords
|
||||||
let (src_min_x, src_min_y, src_width, src_height) = src_rect;
|
let (src_min_x, src_min_y, src_width, src_height) = src_rect;
|
||||||
let (dest_min_x, dest_min_y) = dest_point;
|
let (dest_min_x, dest_min_y) = dest_point;
|
||||||
|
@ -978,7 +982,7 @@ impl<'gc> BitmapData<'gc> {
|
||||||
.to_un_multiplied_alpha();
|
.to_un_multiplied_alpha();
|
||||||
|
|
||||||
// If the test, as defined by the operation pass then set to input colour
|
// If the test, as defined by the operation pass then set to input colour
|
||||||
if operation(source_color.0 as u32 & mask) {
|
if operation(source_color.0 as u32 & mask, masked_threshold) {
|
||||||
modified_count += 1;
|
modified_count += 1;
|
||||||
self.set_pixel32_raw(dest_x as u32, dest_y as u32, Color(colour as _));
|
self.set_pixel32_raw(dest_x as u32, dest_y as u32, Color(colour as _));
|
||||||
} else {
|
} else {
|
||||||
|
@ -1012,68 +1016,28 @@ impl<'gc> BitmapData<'gc> {
|
||||||
mask: u32,
|
mask: u32,
|
||||||
copy_source: bool,
|
copy_source: bool,
|
||||||
) -> u32 {
|
) -> u32 {
|
||||||
// Pre-compute the masked threshold
|
|
||||||
let masked_threshold = threshold & mask;
|
|
||||||
|
|
||||||
// Define the test that will be performed for each pixel
|
// Define the test that will be performed for each pixel
|
||||||
match operation.to_utf8_lossy().as_ref() {
|
let op = match operation.to_utf8_lossy().as_ref() {
|
||||||
"==" => self.threshold_internal(
|
"==" => |v, mt| v == mt,
|
||||||
source_bitmap,
|
"!=" => |v, mt| v != mt,
|
||||||
src_rect,
|
"<" => |v, mt| v < mt,
|
||||||
dest_point,
|
"<=" => |v, mt| v <= mt,
|
||||||
|v| v == masked_threshold,
|
">" => |v, mt| v > mt,
|
||||||
colour,
|
">=" => |v, mt| v >= mt,
|
||||||
mask,
|
|
||||||
copy_source,
|
|
||||||
),
|
|
||||||
"!=" => self.threshold_internal(
|
|
||||||
source_bitmap,
|
|
||||||
src_rect,
|
|
||||||
dest_point,
|
|
||||||
|v| v != masked_threshold,
|
|
||||||
colour,
|
|
||||||
mask,
|
|
||||||
copy_source,
|
|
||||||
),
|
|
||||||
"<" => self.threshold_internal(
|
|
||||||
source_bitmap,
|
|
||||||
src_rect,
|
|
||||||
dest_point,
|
|
||||||
|v| v < masked_threshold,
|
|
||||||
colour,
|
|
||||||
mask,
|
|
||||||
copy_source,
|
|
||||||
),
|
|
||||||
"<=" => self.threshold_internal(
|
|
||||||
source_bitmap,
|
|
||||||
src_rect,
|
|
||||||
dest_point,
|
|
||||||
|v| v <= masked_threshold,
|
|
||||||
colour,
|
|
||||||
mask,
|
|
||||||
copy_source,
|
|
||||||
),
|
|
||||||
">" => self.threshold_internal(
|
|
||||||
source_bitmap,
|
|
||||||
src_rect,
|
|
||||||
dest_point,
|
|
||||||
|v| v > masked_threshold,
|
|
||||||
colour,
|
|
||||||
mask,
|
|
||||||
copy_source,
|
|
||||||
),
|
|
||||||
">=" => self.threshold_internal(
|
|
||||||
source_bitmap,
|
|
||||||
src_rect,
|
|
||||||
dest_point,
|
|
||||||
|v| v >= masked_threshold,
|
|
||||||
colour,
|
|
||||||
mask,
|
|
||||||
copy_source,
|
|
||||||
),
|
|
||||||
// For undefined/invalid operations FP seems to just return 0 here
|
// For undefined/invalid operations FP seems to just return 0 here
|
||||||
_ => 0,
|
_ => return 0,
|
||||||
}
|
};
|
||||||
|
|
||||||
|
self.threshold_internal(
|
||||||
|
source_bitmap,
|
||||||
|
src_rect,
|
||||||
|
dest_point,
|
||||||
|
op,
|
||||||
|
threshold,
|
||||||
|
colour,
|
||||||
|
mask,
|
||||||
|
copy_source,
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Updates the data stored with our `BitmapHandle` if this `BitmapData`
|
// Updates the data stored with our `BitmapHandle` if this `BitmapData`
|
||||||
|
|
Loading…
Reference in New Issue