From eb04738b73e5b6fd485b829fd81fedc0a1200647 Mon Sep 17 00:00:00 2001 From: Nathan Adams Date: Fri, 24 Mar 2023 14:08:58 +0100 Subject: [PATCH] core: Move set_pixel from BitmapData to bitmap_data_operations --- core/src/avm1/globals/bitmap_data.rs | 11 +++++---- .../avm2/globals/flash/display/bitmap_data.rs | 6 ++--- core/src/bitmap/bitmap_data.rs | 13 ----------- core/src/bitmap/bitmap_data_operations.rs | 23 +++++++++++++++++++ 4 files changed, 32 insertions(+), 21 deletions(-) diff --git a/core/src/avm1/globals/bitmap_data.rs b/core/src/avm1/globals/bitmap_data.rs index d5967acf5..3cec28eec 100644 --- a/core/src/avm1/globals/bitmap_data.rs +++ b/core/src/avm1/globals/bitmap_data.rs @@ -206,10 +206,13 @@ pub fn set_pixel<'gc>( let y = y_val.coerce_to_u32(activation)?; let color = color_val.coerce_to_i32(activation)?; - bitmap_data - .bitmap_data() - .write(activation.context.gc_context) - .set_pixel(x, y, color.into()); + bitmap_data_operations::set_pixel( + &mut activation.context, + bitmap_data.bitmap_data_wrapper(), + x, + y, + color.into(), + ); return Ok(Value::Undefined); } diff --git a/core/src/avm2/globals/flash/display/bitmap_data.rs b/core/src/avm2/globals/flash/display/bitmap_data.rs index d1253d7a0..7c0afe1d2 100644 --- a/core/src/avm2/globals/flash/display/bitmap_data.rs +++ b/core/src/avm2/globals/flash/display/bitmap_data.rs @@ -379,13 +379,11 @@ pub fn set_pixel<'gc>( this: Option>, args: &[Value<'gc>], ) -> Result, Error<'gc>> { - if let Some(bitmap_data) = this.and_then(|t| t.as_bitmap_data()) { + if let Some(bitmap_data) = this.and_then(|t| t.as_bitmap_data_wrapper()) { let x = args.get_u32(activation, 0)?; let y = args.get_u32(activation, 1)?; let color = args.get_i32(activation, 2)?; - bitmap_data - .write(activation.context.gc_context) - .set_pixel(x, y, color.into()); + bitmap_data_operations::set_pixel(&mut activation.context, bitmap_data, x, y, color.into()); } Ok(Value::Undefined) diff --git a/core/src/bitmap/bitmap_data.rs b/core/src/bitmap/bitmap_data.rs index 53b26c23f..48a51c6c0 100644 --- a/core/src/bitmap/bitmap_data.rs +++ b/core/src/bitmap/bitmap_data.rs @@ -621,19 +621,6 @@ impl<'gc> BitmapData<'gc> { result } - pub fn set_pixel(&mut self, x: u32, y: u32, color: Color) { - if x < self.width && y < self.height { - if self.transparency { - let current_alpha = self.get_pixel32_raw(x, y).alpha(); - let color = color.with_alpha(current_alpha).to_premultiplied_alpha(true); - self.set_pixel32_raw(x, y, color); - } else { - self.set_pixel32_raw(x, y, color.with_alpha(0xFF)); - } - self.set_cpu_dirty(PixelRegion::for_whole_size(x, y)); - } - } - #[inline] pub fn set_pixel32_raw(&mut self, x: u32, y: u32, color: Color) { self.pixels[(x + y * self.width) as usize] = color; diff --git a/core/src/bitmap/bitmap_data_operations.rs b/core/src/bitmap/bitmap_data_operations.rs index 112607d44..901f4e4bc 100644 --- a/core/src/bitmap/bitmap_data_operations.rs +++ b/core/src/bitmap/bitmap_data_operations.rs @@ -82,6 +82,29 @@ pub fn get_pixel32(target: BitmapDataWrapper, x: u32, y: u32) -> i32 { read.get_pixel32_raw(x, y).to_un_multiplied_alpha().into() } +pub fn set_pixel<'gc>( + context: &mut UpdateContext<'_, 'gc>, + target: BitmapDataWrapper<'gc>, + x: u32, + y: u32, + color: Color, +) { + if x >= target.width() || y >= target.height() { + return; + } + let target = target.sync(); + let mut write = target.write(context.gc_context); + + if write.transparency() { + let current_alpha = write.get_pixel32_raw(x, y).alpha(); + let color = color.with_alpha(current_alpha).to_premultiplied_alpha(true); + write.set_pixel32_raw(x, y, color); + } else { + write.set_pixel32_raw(x, y, color.with_alpha(0xFF)); + } + write.set_cpu_dirty(PixelRegion::for_whole_size(x, y)); +} + pub fn get_pixel(target: BitmapDataWrapper, x: u32, y: u32) -> i32 { if target.disposed() || x >= target.width() || y >= target.height() { return 0;