Skip to content

Commit

Permalink
temp
Browse files Browse the repository at this point in the history
  • Loading branch information
adrian17 committed Nov 26, 2024
1 parent 9a4a97d commit 64f3cbe
Show file tree
Hide file tree
Showing 9 changed files with 80 additions and 54 deletions.
10 changes: 0 additions & 10 deletions core/src/binary_data.rs

This file was deleted.

46 changes: 31 additions & 15 deletions core/src/character.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
use std::cell::RefCell;

use crate::backend::audio::SoundHandle;
use crate::binary_data::BinaryData;
use crate::display_object::{
Avm1Button, Avm2Button, BitmapClass, EditText, Graphic, MorphShape, MovieClip, Text, Video,
};
use crate::font::Font;
use crate::tag_utils::SwfSlice;
use gc_arena::{Collect, GcCell};
use ruffle_render::bitmap::{BitmapHandle, BitmapSize};
use swf::DefineBitsLossless;
Expand Down Expand Up @@ -33,7 +33,7 @@ pub enum Character<'gc> {
Text(Text<'gc>),
Sound(#[collect(require_static)] SoundHandle),
Video(Video<'gc>),
BinaryData(BinaryData),
BinaryData(SwfSlice),
}

/// Holds a bitmap from an SWF tag, plus the decoded width/height.
Expand All @@ -43,12 +43,24 @@ pub enum Character<'gc> {
#[derive(Clone, Debug)]
pub enum CompressedBitmap {
Jpeg {
data: SwfSlice,
alpha: Option<SwfSlice>,
width: u16,
height: u16,
},
// Used when stored data don't exactly correspond to source SWF data
// (for example, when we prepend JpegTables)
OwnedJpeg {
data: Vec<u8>,
alpha: Option<Vec<u8>>,
alpha: Option<SwfSlice>,
width: u16,
height: u16,
},
Lossless(DefineBitsLossless<'static>),
Lossless {
data: SwfSlice,
// note: the data inside the tag is ignored.
tag: DefineBitsLossless<'static>,
},
}

impl CompressedBitmap {
Expand All @@ -58,22 +70,26 @@ impl CompressedBitmap {
width: *width,
height: *height,
},
CompressedBitmap::Lossless(define_bits_lossless) => BitmapSize {
width: define_bits_lossless.width,
height: define_bits_lossless.height,
CompressedBitmap::OwnedJpeg { width, height, .. } => BitmapSize {
width: *width,
height: *height,
},
CompressedBitmap::Lossless { tag, .. } => BitmapSize {
width: tag.width,
height: tag.height,
},
}
}
pub fn decode(&self) -> Result<ruffle_render::bitmap::Bitmap, ruffle_render::error::Error> {
match self {
CompressedBitmap::Jpeg {
data,
alpha,
width: _,
height: _,
} => ruffle_render::utils::decode_define_bits_jpeg(data, alpha.as_deref()),
CompressedBitmap::Lossless(define_bits_lossless) => {
ruffle_render::utils::decode_define_bits_lossless(define_bits_lossless)
CompressedBitmap::Jpeg { data, alpha, .. } => {
ruffle_render::utils::decode_define_bits_jpeg(data.as_ref(), alpha.as_ref().map(|s| s.as_ref()))
}
CompressedBitmap::OwnedJpeg { data, alpha, .. } => {
ruffle_render::utils::decode_define_bits_jpeg(data, alpha.as_ref().map(|s| s.as_ref()))
}
CompressedBitmap::Lossless { data, tag } => {
ruffle_render::utils::decode_define_bits_lossless(data.as_ref(), tag)
}
}
}
Expand Down
52 changes: 32 additions & 20 deletions core/src/display_object/movie_clip.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ use bitflags::bitflags;

use crate::avm1::Avm1;
use crate::avm1::{Activation as Avm1Activation, ActivationIdentifier};
use crate::binary_data::BinaryData;
use crate::character::{Character, CompressedBitmap};
use crate::context::{ActionType, RenderContext, UpdateContext};
use crate::display_object::container::{dispatch_removed_event, ChildContainer};
Expand Down Expand Up @@ -3532,14 +3531,18 @@ impl<'gc, 'a> MovieClipData<'gc> {
.register_character(
define_bits_lossless.id,
Character::Bitmap {
compressed: CompressedBitmap::Lossless(DefineBitsLossless {
id: define_bits_lossless.id,
format: define_bits_lossless.format,
width: define_bits_lossless.width,
height: define_bits_lossless.height,
version: define_bits_lossless.version,
data: Cow::Owned(define_bits_lossless.data.into_owned()),
}),
compressed: CompressedBitmap::Lossless {
data: SwfSlice::new(self.movie(), define_bits_lossless.data),
tag: DefineBitsLossless {
id: define_bits_lossless.id,
format: define_bits_lossless.format,
width: define_bits_lossless.width,
height: define_bits_lossless.height,
version: define_bits_lossless.version,
data: &[],
//data: Cow::Owned(define_bits_lossless.data.into_owned()),
}
},
handle: RefCell::new(None),
avm2_bitmapdata_class: GcCell::new(context.gc_context, BitmapClass::NoSubclass),
},
Expand Down Expand Up @@ -3674,20 +3677,29 @@ impl<'gc, 'a> MovieClipData<'gc> {
.library_for_movie_mut(self.movie())
.jpeg_tables();
let jpeg_data =
ruffle_render::utils::glue_tables_to_jpeg(jpeg_data, jpeg_tables).into_owned();
ruffle_render::utils::glue_tables_to_jpeg(jpeg_data, jpeg_tables);
let (width, height) = ruffle_render::utils::decode_define_bits_jpeg_dimensions(&jpeg_data)?;
let compressed = match jpeg_data {
Cow::Borrowed(data) => CompressedBitmap::Jpeg {
data: SwfSlice::new(self.movie(), data),
alpha: None,
width,
height,
},
Cow::Owned(data) => CompressedBitmap::OwnedJpeg {
data,
alpha: None,
width,
height,
},
};
context
.library
.library_for_movie_mut(self.movie())
.register_character(
id,
Character::Bitmap {
compressed: CompressedBitmap::Jpeg {
data: jpeg_data,
alpha: None,
width,
height,
},
compressed,
handle: RefCell::new(None),
avm2_bitmapdata_class: GcCell::new(context.gc_context, BitmapClass::NoSubclass),
},
Expand All @@ -3711,7 +3723,7 @@ impl<'gc, 'a> MovieClipData<'gc> {
id,
Character::Bitmap {
compressed: CompressedBitmap::Jpeg {
data: jpeg_data.to_vec(),
data: SwfSlice::new(self.movie(), jpeg_data),
alpha: None,
width,
height,
Expand Down Expand Up @@ -3745,8 +3757,8 @@ impl<'gc, 'a> MovieClipData<'gc> {
id,
Character::Bitmap {
compressed: CompressedBitmap::Jpeg {
data: jpeg_data.to_owned(),
alpha: Some(alpha_data.to_owned()),
data: SwfSlice::new(self.movie(), jpeg_data),
alpha: Some(SwfSlice::new(self.movie(), alpha_data)),
width,
height,
},
Expand Down Expand Up @@ -4097,7 +4109,7 @@ impl<'gc, 'a> MovieClipData<'gc> {
reader: &mut SwfStream<'a>,
) -> Result<(), Error> {
let tag_data = reader.read_define_binary_data()?;
let binary_data = BinaryData::from_swf_tag(self.movie(), &tag_data);
let binary_data = SwfSlice::new(self.movie(), tag_data.data);
context
.library
.library_for_movie_mut(self.movie())
Expand Down
1 change: 0 additions & 1 deletion core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ extern crate num_derive;
#[macro_use]
mod avm1;
mod avm2;
mod binary_data;
pub mod bitmap;
pub mod buffer;
mod character;
Expand Down
9 changes: 9 additions & 0 deletions core/src/tag_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -357,6 +357,15 @@ impl AsRef<[u8]> for SwfSlice {
}

impl SwfSlice {
/// Construct a new SwfSlice from a movie and slice to its data.
///
/// This function panics if the slice is not fully contained by the movie.
pub fn new(movie: Arc<SwfMovie>, slice: &[u8]) -> Self {
let ret = Self::from(movie).to_subslice(slice);
assert!(ret.len() == slice.len());
ret
}

/// Creates an empty SwfSlice.
#[inline]
pub fn empty(movie: Arc<SwfMovie>) -> Self {
Expand Down
4 changes: 2 additions & 2 deletions render/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -256,9 +256,9 @@ fn decode_jpeg(jpeg_data: &[u8], alpha_data: Option<&[u8]>) -> Result<Bitmap, Er
/// Decodes the bitmap data in DefineBitsLossless tag into RGBA.
/// DefineBitsLossless is Zlib encoded pixel data (similar to PNG), possibly
/// palletized.
pub fn decode_define_bits_lossless(swf_tag: &swf::DefineBitsLossless) -> Result<Bitmap, Error> {
pub fn decode_define_bits_lossless(data: &[u8], swf_tag: &swf::DefineBitsLossless) -> Result<Bitmap, Error> {
// Decompress the image data (DEFLATE compression).
let mut decoded_data = decompress_zlib(&swf_tag.data)?;
let mut decoded_data = decompress_zlib(&data)?;

let has_alpha = swf_tag.version == 2;

Expand Down
2 changes: 1 addition & 1 deletion swf/src/read.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2509,7 +2509,7 @@ impl<'a> Reader<'a> {
format,
width,
height,
data: Cow::Borrowed(data),
data,
})
}

Expand Down
8 changes: 4 additions & 4 deletions swf/src/test_data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -165,9 +165,9 @@ pub fn tag_tests() -> Vec<TagTestData> {
format: BitmapFormat::Rgb32,
width: 8,
height: 8,
data: Cow::Borrowed(&[
data: &[
120, 218, 251, 207, 192, 240, 255, 255, 8, 198, 0, 4, 128, 127, 129,
]),
],
}),
read_tag_bytes_from_file(
"tests/swfs/DefineBitsLossless.swf",
Expand All @@ -182,9 +182,9 @@ pub fn tag_tests() -> Vec<TagTestData> {
format: BitmapFormat::Rgb32,
width: 8,
height: 8,
data: Cow::Borrowed(&[
data: &[
120, 218, 107, 96, 96, 168, 107, 24, 193, 24, 0, 227, 81, 63, 129,
]),
],
}),
read_tag_bytes_from_file(
"tests/swfs/DefineBitsLossless2.swf",
Expand Down
2 changes: 1 addition & 1 deletion swf/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1697,7 +1697,7 @@ pub struct DefineBitsLossless<'a> {
pub format: BitmapFormat,
pub width: u16,
pub height: u16,
pub data: Cow<'a, [u8]>,
pub data: &'a [u8],
}

#[derive(Clone, Copy, Debug, Eq, PartialEq)]
Expand Down

0 comments on commit 64f3cbe

Please sign in to comment.