mirror of
https://github.com/GraphiteEditor/Graphite.git
synced 2026-09-16 23:08:05 +08:00
Instance tables refactor part 7: Remove RasterDataType and add Raster<CPU>/Raster<GPU>
This commit is contained in:
@@ -4,8 +4,9 @@ use crate::raster::curve::{CubicSplines, CurveManipulatorGroup};
|
||||
#[cfg(feature = "alloc")]
|
||||
use crate::raster::curve::{Curve, ValueMapperNode};
|
||||
#[cfg(feature = "alloc")]
|
||||
use crate::raster::image::{Image, RasterDataTable};
|
||||
use crate::raster::image::Image;
|
||||
use crate::raster::{Channel, Color, Pixel};
|
||||
use crate::raster_types::{CPU, Raster, RasterDataTable};
|
||||
use crate::registry::types::{Angle, Percentage, SignedPercentage};
|
||||
use crate::vector::VectorDataTable;
|
||||
use crate::vector::style::GradientStops;
|
||||
@@ -265,7 +266,7 @@ fn luminance<T: Adjust<Color>>(
|
||||
_: impl Ctx,
|
||||
#[implementations(
|
||||
Color,
|
||||
RasterDataTable<Color>,
|
||||
RasterDataTable<CPU>,
|
||||
GradientStops,
|
||||
)]
|
||||
mut input: T,
|
||||
@@ -289,7 +290,7 @@ fn extract_channel<T: Adjust<Color>>(
|
||||
_: impl Ctx,
|
||||
#[implementations(
|
||||
Color,
|
||||
RasterDataTable<Color>,
|
||||
RasterDataTable<CPU>,
|
||||
GradientStops,
|
||||
)]
|
||||
mut input: T,
|
||||
@@ -312,7 +313,7 @@ fn make_opaque<T: Adjust<Color>>(
|
||||
_: impl Ctx,
|
||||
#[implementations(
|
||||
Color,
|
||||
RasterDataTable<Color>,
|
||||
RasterDataTable<CPU>,
|
||||
GradientStops,
|
||||
)]
|
||||
mut input: T,
|
||||
@@ -337,7 +338,7 @@ fn brightness_contrast<T: Adjust<Color>>(
|
||||
_: impl Ctx,
|
||||
#[implementations(
|
||||
Color,
|
||||
RasterDataTable<Color>,
|
||||
RasterDataTable<CPU>,
|
||||
GradientStops,
|
||||
)]
|
||||
mut input: T,
|
||||
@@ -426,7 +427,7 @@ fn levels<T: Adjust<Color>>(
|
||||
_: impl Ctx,
|
||||
#[implementations(
|
||||
Color,
|
||||
RasterDataTable<Color>,
|
||||
RasterDataTable<CPU>,
|
||||
GradientStops,
|
||||
)]
|
||||
mut image: T,
|
||||
@@ -493,7 +494,7 @@ async fn black_and_white<T: Adjust<Color>>(
|
||||
_: impl Ctx,
|
||||
#[implementations(
|
||||
Color,
|
||||
RasterDataTable<Color>,
|
||||
RasterDataTable<CPU>,
|
||||
GradientStops,
|
||||
)]
|
||||
mut image: T,
|
||||
@@ -565,7 +566,7 @@ async fn hue_saturation<T: Adjust<Color>>(
|
||||
_: impl Ctx,
|
||||
#[implementations(
|
||||
Color,
|
||||
RasterDataTable<Color>,
|
||||
RasterDataTable<CPU>,
|
||||
GradientStops,
|
||||
)]
|
||||
mut input: T,
|
||||
@@ -599,7 +600,7 @@ async fn invert<T: Adjust<Color>>(
|
||||
_: impl Ctx,
|
||||
#[implementations(
|
||||
Color,
|
||||
RasterDataTable<Color>,
|
||||
RasterDataTable<CPU>,
|
||||
GradientStops,
|
||||
)]
|
||||
mut input: T,
|
||||
@@ -621,7 +622,7 @@ async fn threshold<T: Adjust<Color>>(
|
||||
_: impl Ctx,
|
||||
#[implementations(
|
||||
Color,
|
||||
RasterDataTable<Color>,
|
||||
RasterDataTable<CPU>,
|
||||
GradientStops,
|
||||
)]
|
||||
mut image: T,
|
||||
@@ -663,19 +664,19 @@ impl Blend<Color> for Option<Color> {
|
||||
}
|
||||
}
|
||||
}
|
||||
impl Blend<Color> for RasterDataTable<Color> {
|
||||
impl Blend<Color> for RasterDataTable<CPU> {
|
||||
fn blend(&self, under: &Self, blend_fn: impl Fn(Color, Color) -> Color) -> Self {
|
||||
let mut result_table = self.clone();
|
||||
|
||||
for (over, under) in result_table.instance_mut_iter().zip(under.instance_ref_iter()) {
|
||||
let data = over.instance.data.iter().zip(under.instance.data.iter()).map(|(a, b)| blend_fn(*a, *b)).collect();
|
||||
|
||||
*over.instance = Image {
|
||||
*over.instance = Raster::new_cpu(Image {
|
||||
data,
|
||||
width: over.instance.width,
|
||||
height: over.instance.height,
|
||||
base64_string: None,
|
||||
};
|
||||
});
|
||||
}
|
||||
|
||||
result_table
|
||||
@@ -706,14 +707,14 @@ async fn blend<T: Blend<Color> + Send>(
|
||||
_: impl Ctx,
|
||||
#[implementations(
|
||||
Color,
|
||||
RasterDataTable<Color>,
|
||||
RasterDataTable<CPU>,
|
||||
GradientStops,
|
||||
)]
|
||||
over: T,
|
||||
#[expose]
|
||||
#[implementations(
|
||||
Color,
|
||||
RasterDataTable<Color>,
|
||||
RasterDataTable<CPU>,
|
||||
GradientStops,
|
||||
)]
|
||||
under: T,
|
||||
@@ -795,13 +796,13 @@ impl Adjust<Color> for GradientStops {
|
||||
}
|
||||
}
|
||||
}
|
||||
impl<P: Pixel> Adjust<P> for RasterDataTable<P>
|
||||
impl Adjust<Color> for RasterDataTable<CPU>
|
||||
where
|
||||
GraphicElement: From<Image<P>>,
|
||||
GraphicElement: From<Image<Color>>,
|
||||
{
|
||||
fn adjust(&mut self, map_fn: impl Fn(&P) -> P) {
|
||||
fn adjust(&mut self, map_fn: impl Fn(&Color) -> Color) {
|
||||
for instance in self.instance_mut_iter() {
|
||||
for c in instance.instance.data.iter_mut() {
|
||||
for c in instance.instance.data_mut().data.iter_mut() {
|
||||
*c = map_fn(c);
|
||||
}
|
||||
}
|
||||
@@ -829,7 +830,7 @@ async fn gradient_map<T: Adjust<Color>>(
|
||||
_: impl Ctx,
|
||||
#[implementations(
|
||||
Color,
|
||||
RasterDataTable<Color>,
|
||||
RasterDataTable<CPU>,
|
||||
GradientStops,
|
||||
)]
|
||||
mut image: T,
|
||||
@@ -865,7 +866,7 @@ async fn vibrance<T: Adjust<Color>>(
|
||||
_: impl Ctx,
|
||||
#[implementations(
|
||||
Color,
|
||||
RasterDataTable<Color>,
|
||||
RasterDataTable<CPU>,
|
||||
GradientStops,
|
||||
)]
|
||||
mut image: T,
|
||||
@@ -1037,7 +1038,7 @@ async fn channel_mixer<T: Adjust<Color>>(
|
||||
_: impl Ctx,
|
||||
#[implementations(
|
||||
Color,
|
||||
RasterDataTable<Color>,
|
||||
RasterDataTable<CPU>,
|
||||
GradientStops,
|
||||
)]
|
||||
mut image: T,
|
||||
@@ -1166,7 +1167,7 @@ async fn selective_color<T: Adjust<Color>>(
|
||||
_: impl Ctx,
|
||||
#[implementations(
|
||||
Color,
|
||||
RasterDataTable<Color>,
|
||||
RasterDataTable<CPU>,
|
||||
GradientStops,
|
||||
)]
|
||||
mut image: T,
|
||||
@@ -1309,9 +1310,9 @@ impl MultiplyAlpha for GraphicGroupTable {
|
||||
}
|
||||
}
|
||||
}
|
||||
impl<P: Pixel> MultiplyAlpha for RasterDataTable<P>
|
||||
impl MultiplyAlpha for RasterDataTable<CPU>
|
||||
where
|
||||
GraphicElement: From<Image<P>>,
|
||||
GraphicElement: From<Image<Color>>,
|
||||
{
|
||||
fn multiply_alpha(&mut self, factor: f64) {
|
||||
for instance in self.instance_mut_iter() {
|
||||
@@ -1331,7 +1332,7 @@ async fn posterize<T: Adjust<Color>>(
|
||||
_: impl Ctx,
|
||||
#[implementations(
|
||||
Color,
|
||||
RasterDataTable<Color>,
|
||||
RasterDataTable<CPU>,
|
||||
GradientStops,
|
||||
)]
|
||||
mut input: T,
|
||||
@@ -1364,7 +1365,7 @@ async fn exposure<T: Adjust<Color>>(
|
||||
_: impl Ctx,
|
||||
#[implementations(
|
||||
Color,
|
||||
RasterDataTable<Color>,
|
||||
RasterDataTable<CPU>,
|
||||
GradientStops,
|
||||
)]
|
||||
mut input: T,
|
||||
@@ -1438,7 +1439,7 @@ fn color_overlay<T: Adjust<Color>>(
|
||||
_: impl Ctx,
|
||||
#[implementations(
|
||||
Color,
|
||||
RasterDataTable<Color>,
|
||||
RasterDataTable<CPU>,
|
||||
GradientStops,
|
||||
)]
|
||||
mut image: T,
|
||||
@@ -1488,7 +1489,8 @@ fn color_overlay<T: Adjust<Color>>(
|
||||
#[cfg(test)]
|
||||
mod test {
|
||||
use crate::raster::adjustments::BlendMode;
|
||||
use crate::raster::image::{Image, RasterDataTable};
|
||||
use crate::raster::image::Image;
|
||||
use crate::raster_types::{Raster, RasterDataTable};
|
||||
use crate::{Color, Node};
|
||||
use std::pin::Pin;
|
||||
|
||||
@@ -1514,7 +1516,7 @@ mod test {
|
||||
// 100% of the output should come from the multiplied value
|
||||
let opacity = 100_f64;
|
||||
|
||||
let result = super::color_overlay((), RasterDataTable::new(image.clone()), overlay_color, BlendMode::Multiply, opacity);
|
||||
let result = super::color_overlay((), RasterDataTable::new(Raster::new_cpu(image.clone())), overlay_color, BlendMode::Multiply, opacity);
|
||||
let result = result.instance_ref_iter().next().unwrap().instance;
|
||||
|
||||
// The output should just be the original green and alpha channels (as we multiply them by 1 and other channels by 0)
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
use crate::Color;
|
||||
use crate::instances::Instance;
|
||||
use crate::raster::Image;
|
||||
use crate::raster_types::CPU;
|
||||
use crate::raster_types::Raster;
|
||||
use crate::vector::brush_stroke::BrushStroke;
|
||||
use crate::vector::brush_stroke::BrushStyle;
|
||||
use core::hash::Hash;
|
||||
@@ -17,19 +17,19 @@ struct BrushCacheImpl {
|
||||
|
||||
// The strokes that have been fully processed and blended into the background.
|
||||
#[cfg_attr(feature = "serde", serde(deserialize_with = "crate::graphene_core::raster::image::migrate_image_frame_instance"))]
|
||||
background: Instance<Image<Color>>,
|
||||
background: Instance<Raster<CPU>>,
|
||||
#[cfg_attr(feature = "serde", serde(deserialize_with = "crate::graphene_core::raster::image::migrate_image_frame_instance"))]
|
||||
blended_image: Instance<Image<Color>>,
|
||||
blended_image: Instance<Raster<CPU>>,
|
||||
#[cfg_attr(feature = "serde", serde(deserialize_with = "crate::graphene_core::raster::image::migrate_image_frame_instance"))]
|
||||
last_stroke_texture: Instance<Image<Color>>,
|
||||
last_stroke_texture: Instance<Raster<CPU>>,
|
||||
|
||||
// A cache for brush textures.
|
||||
#[cfg_attr(feature = "serde", serde(skip))]
|
||||
brush_texture_cache: HashMap<BrushStyle, Image<Color>>,
|
||||
brush_texture_cache: HashMap<BrushStyle, Raster<CPU>>,
|
||||
}
|
||||
|
||||
impl BrushCacheImpl {
|
||||
fn compute_brush_plan(&mut self, mut background: Instance<Image<Color>>, input: &[BrushStroke]) -> BrushPlan {
|
||||
fn compute_brush_plan(&mut self, mut background: Instance<Raster<CPU>>, input: &[BrushStroke]) -> BrushPlan {
|
||||
// Do background invalidation.
|
||||
if background != self.background {
|
||||
self.background = background.clone();
|
||||
@@ -57,7 +57,7 @@ impl BrushCacheImpl {
|
||||
|
||||
// Check if the first non-blended stroke is an extension of the last one.
|
||||
let mut first_stroke_texture = Instance {
|
||||
instance: Image::default(),
|
||||
instance: Raster::<CPU>::default(),
|
||||
transform: glam::DAffine2::ZERO,
|
||||
..Default::default()
|
||||
};
|
||||
@@ -84,7 +84,7 @@ impl BrushCacheImpl {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn cache_results(&mut self, input: Vec<BrushStroke>, blended_image: Instance<Image<Color>>, last_stroke_texture: Instance<Image<Color>>) {
|
||||
pub fn cache_results(&mut self, input: Vec<BrushStroke>, blended_image: Instance<Raster<CPU>>, last_stroke_texture: Instance<Raster<CPU>>) {
|
||||
self.prev_input = input;
|
||||
self.blended_image = blended_image;
|
||||
self.last_stroke_texture = last_stroke_texture;
|
||||
@@ -99,8 +99,8 @@ impl Hash for BrushCacheImpl {
|
||||
#[derive(Clone, Debug, Default)]
|
||||
pub struct BrushPlan {
|
||||
pub strokes: Vec<BrushStroke>,
|
||||
pub background: Instance<Image<Color>>,
|
||||
pub first_stroke_texture: Instance<Image<Color>>,
|
||||
pub background: Instance<Raster<CPU>>,
|
||||
pub first_stroke_texture: Instance<Raster<CPU>>,
|
||||
pub first_stroke_point_skip: usize,
|
||||
}
|
||||
|
||||
@@ -164,22 +164,22 @@ impl BrushCache {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn compute_brush_plan(&self, background: Instance<Image<Color>>, input: &[BrushStroke]) -> BrushPlan {
|
||||
pub fn compute_brush_plan(&self, background: Instance<Raster<CPU>>, input: &[BrushStroke]) -> BrushPlan {
|
||||
let mut inner = self.inner.lock().unwrap();
|
||||
inner.compute_brush_plan(background, input)
|
||||
}
|
||||
|
||||
pub fn cache_results(&self, input: Vec<BrushStroke>, blended_image: Instance<Image<Color>>, last_stroke_texture: Instance<Image<Color>>) {
|
||||
pub fn cache_results(&self, input: Vec<BrushStroke>, blended_image: Instance<Raster<CPU>>, last_stroke_texture: Instance<Raster<CPU>>) {
|
||||
let mut inner = self.inner.lock().unwrap();
|
||||
inner.cache_results(input, blended_image, last_stroke_texture)
|
||||
}
|
||||
|
||||
pub fn get_cached_brush(&self, style: &BrushStyle) -> Option<Image<Color>> {
|
||||
pub fn get_cached_brush(&self, style: &BrushStyle) -> Option<Raster<CPU>> {
|
||||
let inner = self.inner.lock().unwrap();
|
||||
inner.brush_texture_cache.get(style).cloned()
|
||||
}
|
||||
|
||||
pub fn store_brush(&self, style: BrushStyle, brush: Image<Color>) {
|
||||
pub fn store_brush(&self, style: BrushStyle, brush: Raster<CPU>) {
|
||||
let mut inner = self.inner.lock().unwrap();
|
||||
inner.brush_texture_cache.insert(style, brush);
|
||||
}
|
||||
|
||||
@@ -1,11 +1,14 @@
|
||||
use crate::{
|
||||
AlphaBlending,
|
||||
instances::{Instance, Instances},
|
||||
raster_types::Raster,
|
||||
};
|
||||
|
||||
use super::Color;
|
||||
use super::discrete_srgb::float_to_srgb_u8;
|
||||
use crate::AlphaBlending;
|
||||
use crate::GraphicElement;
|
||||
use crate::instances::{Instance, Instances};
|
||||
use alloc::vec::Vec;
|
||||
use core::hash::{Hash, Hasher};
|
||||
use dyn_any::StaticType;
|
||||
use dyn_any::{DynAny, StaticType};
|
||||
use glam::{DAffine2, DVec2};
|
||||
|
||||
#[cfg(feature = "serde")]
|
||||
@@ -208,9 +211,39 @@ impl<P: Pixel> IntoIterator for Image<P> {
|
||||
}
|
||||
|
||||
// TODO: Eventually remove this migration document upgrade code
|
||||
pub fn migrate_image_frame<'de, D: serde::Deserializer<'de>>(deserializer: D) -> Result<RasterDataTable<Color>, D::Error> {
|
||||
pub fn migrate_image_frame<'de, D: serde::Deserializer<'de>>(deserializer: D) -> Result<RasterDataTable<CPU>, D::Error> {
|
||||
use serde::Deserialize;
|
||||
|
||||
type ImageFrameTable<P> = Instances<Image<P>>;
|
||||
|
||||
#[derive(Clone, Debug, Hash, PartialEq, DynAny)]
|
||||
enum RasterFrame {
|
||||
/// A CPU-based bitmap image with a finite position and extent, equivalent to the SVG <image> tag: https://developer.mozilla.org/en-US/docs/Web/SVG/Element/image
|
||||
ImageFrame(ImageFrameTable<Color>),
|
||||
}
|
||||
impl<'de> serde::Deserialize<'de> for RasterFrame {
|
||||
fn deserialize<D: serde::Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
|
||||
Ok(RasterFrame::ImageFrame(ImageFrameTable::new(Image::deserialize(deserializer)?)))
|
||||
}
|
||||
}
|
||||
impl serde::Serialize for RasterFrame {
|
||||
fn serialize<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
|
||||
match self {
|
||||
RasterFrame::ImageFrame(image_instances) => image_instances.serialize(serializer),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, Hash, PartialEq, DynAny)]
|
||||
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
|
||||
pub enum GraphicElement {
|
||||
/// Equivalent to the SVG <g> tag: https://developer.mozilla.org/en-US/docs/Web/SVG/Element/g
|
||||
GraphicGroup(GraphicGroupTable),
|
||||
/// A vector shape, equivalent to the SVG <path> tag: https://developer.mozilla.org/en-US/docs/Web/SVG/Element/path
|
||||
VectorData(VectorDataTable),
|
||||
RasterFrame(RasterFrame),
|
||||
}
|
||||
|
||||
#[derive(Clone, Default, Debug, PartialEq, specta::Type)]
|
||||
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
|
||||
pub struct ImageFrame<P: Pixel> {
|
||||
@@ -218,13 +251,13 @@ pub fn migrate_image_frame<'de, D: serde::Deserializer<'de>>(deserializer: D) ->
|
||||
}
|
||||
impl From<ImageFrame<Color>> for GraphicElement {
|
||||
fn from(image_frame: ImageFrame<Color>) -> Self {
|
||||
GraphicElement::RasterDataType(crate::RasterDataType::RasterData(RasterDataTable::new(image_frame.image)))
|
||||
GraphicElement::RasterFrame(RasterFrame::ImageFrame(ImageFrameTable::new(image_frame.image)))
|
||||
}
|
||||
}
|
||||
impl From<GraphicElement> for ImageFrame<Color> {
|
||||
fn from(element: GraphicElement) -> Self {
|
||||
match element {
|
||||
GraphicElement::RasterDataType(crate::RasterDataType::RasterData(image)) => Self {
|
||||
GraphicElement::RasterFrame(RasterFrame::ImageFrame(image)) => Self {
|
||||
image: image.instance_ref_iter().next().unwrap().instance.clone(),
|
||||
},
|
||||
_ => panic!("Expected Image, found {:?}", element),
|
||||
@@ -255,27 +288,59 @@ pub fn migrate_image_frame<'de, D: serde::Deserializer<'de>>(deserializer: D) ->
|
||||
Image(Image<Color>),
|
||||
OldImageFrame(OldImageFrame<Color>),
|
||||
ImageFrame(Instances<ImageFrame<Color>>),
|
||||
RasterDataTable(RasterDataTable<Color>),
|
||||
ImageFrameTable(ImageFrameTable<Color>),
|
||||
RasterDataTable(RasterDataTable<CPU>),
|
||||
}
|
||||
|
||||
Ok(match FormatVersions::deserialize(deserializer)? {
|
||||
FormatVersions::Image(image) => RasterDataTable::new(image),
|
||||
FormatVersions::Image(image) => RasterDataTable::new(Raster::new_cpu(image)),
|
||||
FormatVersions::OldImageFrame(image_frame_with_transform_and_blending) => {
|
||||
let OldImageFrame { image, transform, alpha_blending } = image_frame_with_transform_and_blending;
|
||||
let mut image_frame_table = RasterDataTable::new(image);
|
||||
let mut image_frame_table = RasterDataTable::new(Raster::new_cpu(image));
|
||||
*image_frame_table.instance_mut_iter().next().unwrap().transform = transform;
|
||||
*image_frame_table.instance_mut_iter().next().unwrap().alpha_blending = alpha_blending;
|
||||
image_frame_table
|
||||
}
|
||||
FormatVersions::ImageFrame(image_frame) => RasterDataTable::new(image_frame.instance_ref_iter().next().unwrap().instance.image.clone()),
|
||||
FormatVersions::RasterDataTable(image_frame_table) => image_frame_table,
|
||||
FormatVersions::ImageFrame(image_frame) => RasterDataTable::new(Raster::new_cpu(image_frame.instance_ref_iter().next().unwrap().instance.image.clone())),
|
||||
FormatVersions::ImageFrameTable(image_frame_table) => RasterDataTable::new(Raster::new_cpu(image_frame_table.instance_ref_iter().next().unwrap().instance.clone())),
|
||||
FormatVersions::RasterDataTable(raster_data_table) => raster_data_table,
|
||||
})
|
||||
}
|
||||
|
||||
// TODO: Eventually remove this migration document upgrade code
|
||||
pub fn migrate_image_frame_instance<'de, D: serde::Deserializer<'de>>(deserializer: D) -> Result<Instance<Image<Color>>, D::Error> {
|
||||
pub fn migrate_image_frame_instance<'de, D: serde::Deserializer<'de>>(deserializer: D) -> Result<Instance<Raster<CPU>>, D::Error> {
|
||||
use serde::Deserialize;
|
||||
|
||||
type ImageFrameTable<P> = Instances<Image<P>>;
|
||||
|
||||
#[derive(Clone, Debug, Hash, PartialEq, DynAny)]
|
||||
enum RasterFrame {
|
||||
/// A CPU-based bitmap image with a finite position and extent, equivalent to the SVG <image> tag: https://developer.mozilla.org/en-US/docs/Web/SVG/Element/image
|
||||
ImageFrame(ImageFrameTable<Color>),
|
||||
}
|
||||
impl<'de> serde::Deserialize<'de> for RasterFrame {
|
||||
fn deserialize<D: serde::Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
|
||||
Ok(RasterFrame::ImageFrame(ImageFrameTable::new(Image::deserialize(deserializer)?)))
|
||||
}
|
||||
}
|
||||
impl serde::Serialize for RasterFrame {
|
||||
fn serialize<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
|
||||
match self {
|
||||
RasterFrame::ImageFrame(image_instances) => image_instances.serialize(serializer),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, Hash, PartialEq, DynAny)]
|
||||
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
|
||||
pub enum GraphicElement {
|
||||
/// Equivalent to the SVG <g> tag: https://developer.mozilla.org/en-US/docs/Web/SVG/Element/g
|
||||
GraphicGroup(GraphicGroupTable),
|
||||
/// A vector shape, equivalent to the SVG <path> tag: https://developer.mozilla.org/en-US/docs/Web/SVG/Element/path
|
||||
VectorData(VectorDataTable),
|
||||
RasterFrame(RasterFrame),
|
||||
}
|
||||
|
||||
#[derive(Clone, Default, Debug, PartialEq, specta::Type)]
|
||||
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
|
||||
pub struct ImageFrame<P: Pixel> {
|
||||
@@ -283,13 +348,13 @@ pub fn migrate_image_frame_instance<'de, D: serde::Deserializer<'de>>(deserializ
|
||||
}
|
||||
impl From<ImageFrame<Color>> for GraphicElement {
|
||||
fn from(image_frame: ImageFrame<Color>) -> Self {
|
||||
GraphicElement::RasterDataType(crate::RasterDataType::RasterData(RasterDataTable::new(image_frame.image)))
|
||||
GraphicElement::RasterFrame(RasterFrame::ImageFrame(ImageFrameTable::new(image_frame.image)))
|
||||
}
|
||||
}
|
||||
impl From<GraphicElement> for ImageFrame<Color> {
|
||||
fn from(element: GraphicElement) -> Self {
|
||||
match element {
|
||||
GraphicElement::RasterDataType(crate::RasterDataType::RasterData(image)) => Self {
|
||||
GraphicElement::RasterFrame(RasterFrame::ImageFrame(image)) => Self {
|
||||
image: image.instance_ref_iter().next().unwrap().instance.clone(),
|
||||
},
|
||||
_ => panic!("Expected Image, found {:?}", element),
|
||||
@@ -320,23 +385,23 @@ pub fn migrate_image_frame_instance<'de, D: serde::Deserializer<'de>>(deserializ
|
||||
Image(Image<Color>),
|
||||
OldImageFrame(OldImageFrame<Color>),
|
||||
ImageFrame(Instances<ImageFrame<Color>>),
|
||||
RasterDataTable(RasterDataTable<Color>),
|
||||
ImageInstance(Instance<Image<Color>>),
|
||||
RasterDataTable(RasterDataTable<CPU>),
|
||||
ImageInstance(Instance<Raster<CPU>>),
|
||||
}
|
||||
|
||||
Ok(match FormatVersions::deserialize(deserializer)? {
|
||||
FormatVersions::Image(image) => Instance {
|
||||
instance: image,
|
||||
instance: Raster::new_cpu(image),
|
||||
..Default::default()
|
||||
},
|
||||
FormatVersions::OldImageFrame(image_frame_with_transform_and_blending) => Instance {
|
||||
instance: image_frame_with_transform_and_blending.image,
|
||||
instance: Raster::new_cpu(image_frame_with_transform_and_blending.image),
|
||||
transform: image_frame_with_transform_and_blending.transform,
|
||||
alpha_blending: image_frame_with_transform_and_blending.alpha_blending,
|
||||
source_node_id: None,
|
||||
},
|
||||
FormatVersions::ImageFrame(image_frame) => Instance {
|
||||
instance: image_frame.instance_ref_iter().next().unwrap().instance.image.clone(),
|
||||
instance: Raster::new_cpu(image_frame.instance_ref_iter().next().unwrap().instance.image.clone()),
|
||||
..Default::default()
|
||||
},
|
||||
FormatVersions::RasterDataTable(image_frame_table) => image_frame_table.instance_iter().next().unwrap_or_default(),
|
||||
@@ -344,8 +409,7 @@ pub fn migrate_image_frame_instance<'de, D: serde::Deserializer<'de>>(deserializ
|
||||
})
|
||||
}
|
||||
|
||||
// TODO: Rename to ImageTable
|
||||
pub type RasterDataTable<P> = Instances<Image<P>>;
|
||||
// pub type RasterDataTable<P> = Instances<Image<P>>;
|
||||
|
||||
impl<P: Debug + Copy + Pixel> Sample for Image<P> {
|
||||
type Pixel = P;
|
||||
@@ -393,22 +457,22 @@ impl From<Image<Color>> for Image<SRGBA8> {
|
||||
}
|
||||
}
|
||||
|
||||
impl From<RasterDataTable<Color>> for RasterDataTable<SRGBA8> {
|
||||
fn from(image_frame_table: RasterDataTable<Color>) -> Self {
|
||||
let mut result_table = RasterDataTable::<SRGBA8>::default();
|
||||
// impl From<RasterDataTable<CPU>> for RasterDataTable<SRGBA8> {
|
||||
// fn from(image_frame_table: RasterDataTable<CPU>) -> Self {
|
||||
// let mut result_table = RasterDataTable::<SRGBA8>::default();
|
||||
|
||||
for image_frame_instance in image_frame_table.instance_iter() {
|
||||
result_table.push(Instance {
|
||||
instance: image_frame_instance.instance.into(),
|
||||
transform: image_frame_instance.transform,
|
||||
alpha_blending: image_frame_instance.alpha_blending,
|
||||
source_node_id: image_frame_instance.source_node_id,
|
||||
});
|
||||
}
|
||||
// for image_frame_instance in image_frame_table.instance_iter() {
|
||||
// result_table.push(Instance {
|
||||
// instance: image_frame_instance.instance,
|
||||
// transform: image_frame_instance.transform,
|
||||
// alpha_blending: image_frame_instance.alpha_blending,
|
||||
// source_node_id: image_frame_instance.source_node_id,
|
||||
// });
|
||||
// }
|
||||
|
||||
result_table
|
||||
}
|
||||
}
|
||||
// result_table
|
||||
// }
|
||||
// }
|
||||
|
||||
impl From<Image<SRGBA8>> for Image<Color> {
|
||||
fn from(image: Image<SRGBA8>) -> Self {
|
||||
|
||||
Reference in New Issue
Block a user