From e62e05cd0965503c6d463acd9e191d7b6c46a84e Mon Sep 17 00:00:00 2001 From: Nathan Adams Date: Fri, 24 Mar 2023 15:42:47 +0100 Subject: [PATCH] core: Move get_pixels from BitmapData to bitmap_data_operations --- .../avm2/globals/flash/display/bitmap_data.rs | 6 ++-- core/src/bitmap/bitmap_data.rs | 27 +---------------- core/src/bitmap/bitmap_data_operations.rs | 29 ++++++++++++++++++- 3 files changed, 32 insertions(+), 30 deletions(-) diff --git a/core/src/avm2/globals/flash/display/bitmap_data.rs b/core/src/avm2/globals/flash/display/bitmap_data.rs index 4fd206a94..fdb0eeaf4 100644 --- a/core/src/avm2/globals/flash/display/bitmap_data.rs +++ b/core/src/avm2/globals/flash/display/bitmap_data.rs @@ -271,8 +271,8 @@ pub fn get_pixels<'gc>( this: Option>, args: &[Value<'gc>], ) -> Result, Error<'gc>> { - if let Some(bitmap_data) = this.and_then(|t| t.as_bitmap_data()) { - bitmap_data.read().check_valid(activation)?; + if let Some(bitmap_data) = this.and_then(|t| t.as_bitmap_data_wrapper()) { + bitmap_data.check_valid(activation)?; let rectangle = args.get_object(activation, 0, "rect")?; let x = rectangle .get_public_property("x", activation)? @@ -288,7 +288,7 @@ pub fn get_pixels<'gc>( .coerce_to_i32(activation)?; let bytearray = ByteArrayObject::from_storage( activation, - bitmap_data.read().get_pixels(x, y, width, height)?, + bitmap_data_operations::get_pixels_as_byte_array(bitmap_data, x, y, width, height)?, )?; return Ok(bytearray.into()); } diff --git a/core/src/bitmap/bitmap_data.rs b/core/src/bitmap/bitmap_data.rs index 30bf42b24..e0c7190cb 100644 --- a/core/src/bitmap/bitmap_data.rs +++ b/core/src/bitmap/bitmap_data.rs @@ -1,4 +1,4 @@ -use crate::avm2::{Error, Object as Avm2Object, Value as Avm2Value}; +use crate::avm2::{Object as Avm2Object, Value as Avm2Value}; use crate::display_object::{DisplayObject, TDisplayObject}; use bitflags::bitflags; use core::fmt; @@ -401,7 +401,6 @@ mod wrapper { } } -use crate::avm2::bytearray::ByteArrayStorage; pub use wrapper::BitmapDataWrapper; impl fmt::Debug for BitmapData<'_> { @@ -573,30 +572,6 @@ impl<'gc> BitmapData<'gc> { x >= 0 && x < self.width() as i32 && y >= 0 && y < self.height() as i32 } - pub fn get_pixels( - &self, - x: i32, - y: i32, - width: i32, - height: i32, - ) -> Result> { - let mut result = ByteArrayStorage::new(); - - let x0 = x.max(0) as u32; - let y0 = y.max(0) as u32; - let x1 = (x + width).clamp(0, self.width as i32) as u32; - let y1 = (y + height).clamp(0, self.height as i32) as u32; - - for y in y0..y1 { - for x in x0..x1 { - let color = self.pixels[(x + y * self.width) as usize]; - result.write_int(color.to_un_multiplied_alpha().0)?; - } - } - - Ok(result) - } - #[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 713b050ff..5e4c37abd 100644 --- a/core/src/bitmap/bitmap_data_operations.rs +++ b/core/src/bitmap/bitmap_data_operations.rs @@ -1,4 +1,5 @@ -use crate::avm2::Value as Avm2Value; +use crate::avm2::bytearray::ByteArrayStorage; +use crate::avm2::{Error, Value as Avm2Value}; use crate::bitmap::bitmap_data::{ BitmapData, BitmapDataDrawError, BitmapDataWrapper, ChannelOptions, Color, IBitmapDrawable, LehmerRng, ThresholdOperation, @@ -1333,3 +1334,29 @@ pub fn get_vector( result } + +pub fn get_pixels_as_byte_array<'gc>( + target: BitmapDataWrapper, + x: i32, + y: i32, + width: i32, + height: i32, +) -> Result> { + let mut result = ByteArrayStorage::new(); + + let x0 = x.max(0) as u32; + let y0 = y.max(0) as u32; + let x1 = (x + width).clamp(0, target.width() as i32) as u32; + let y1 = (y + height).clamp(0, target.height() as i32) as u32; + + let target = target.sync(); + let read = target.read(); + for y in y0..y1 { + for x in x0..x1 { + let color = read.get_pixel32_raw(x, y); + result.write_int(color.to_un_multiplied_alpha().into())?; + } + } + + Ok(result) +}