Skip to content

Commit 64a9dbc

Browse files
authored
images are serializable through SerializedImage (#23798)
# Objective - Some types wrap an image and can be useful to serialise ## Solution - Implement serialize on Image through SerializedImage ## Testing - CI
1 parent f05ba5e commit 64a9dbc

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

crates/bevy_image/src/image.rs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ use bevy_app::{App, Plugin};
1111
use bevy_reflect::TypePath;
1212
#[cfg(feature = "bevy_reflect")]
1313
use bevy_reflect::{std_traits::ReflectDefault, Reflect};
14+
#[cfg(feature = "serialize")]
15+
use bevy_reflect::{ReflectDeserialize, ReflectSerialize};
1416

1517
use bevy_asset::{uuid_handle, Asset, AssetApp, Assets, Handle, RenderAssetUsages};
1618
use bevy_color::{Color, ColorToComponents, Gray, LinearRgba, Srgba, Xyza};
@@ -606,6 +608,7 @@ impl ToExtents for UVec3 {
606608
derive(Reflect),
607609
reflect(opaque, Default, Debug, Clone)
608610
)]
611+
#[cfg_attr(feature = "serialize", reflect(Serialize, Deserialize))]
609612
#[cfg_attr(not(feature = "bevy_reflect"), derive(TypePath))]
610613
pub struct Image {
611614
/// Raw pixel data.
@@ -642,6 +645,25 @@ pub struct Image {
642645
pub copy_on_resize: bool,
643646
}
644647

648+
#[cfg(feature = "serialize")]
649+
mod image_serde {
650+
use super::*;
651+
652+
impl Serialize for Image {
653+
fn serialize<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
654+
super::super::serialized_image::SerializedImage::from_image(self.clone())
655+
.serialize(serializer)
656+
}
657+
}
658+
659+
impl<'de> Deserialize<'de> for Image {
660+
fn deserialize<D: serde::Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
661+
super::super::serialized_image::SerializedImage::deserialize(deserializer)
662+
.map(super::super::serialized_image::SerializedImage::into_image)
663+
}
664+
}
665+
}
666+
645667
/// Used in [`Image`], this determines what image sampler to use when rendering. The default setting,
646668
/// [`ImageSampler::Default`], will read the sampler from the `ImagePlugin` at setup.
647669
/// Setting this to [`ImageSampler::Descriptor`] will override the global default descriptor for this [`Image`].

0 commit comments

Comments
 (0)