core: Move get_pixels from BitmapData to bitmap_data_operations

This commit is contained in:
Nathan Adams 2023-03-24 15:42:47 +01:00
parent 89c2fc841e
commit e62e05cd09
3 changed files with 32 additions and 30 deletions

View File

@ -271,8 +271,8 @@ pub fn get_pixels<'gc>(
this: Option<Object<'gc>>,
args: &[Value<'gc>],
) -> Result<Value<'gc>, 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());
}

View File

@ -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<ByteArrayStorage, Error<'gc>> {
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;

View File

@ -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<ByteArrayStorage, Error<'gc>> {
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)
}