mirror of
https://github.com/GraphiteEditor/Graphite.git
synced 2026-09-15 22:28:10 +08:00
Instance tables refactor part 7: Remove RasterDataType and add Raster<CPU>/Raster<GPU>
This commit is contained in:
@@ -1,4 +1,3 @@
|
||||
use crate::instances::Instances;
|
||||
use crate::text::FontCache;
|
||||
use crate::transform::Footprint;
|
||||
use crate::vector::style::ViewMode;
|
||||
@@ -53,7 +52,7 @@ impl Size for web_sys::HtmlCanvasElement {
|
||||
}
|
||||
}
|
||||
|
||||
pub type TextureDataTable = Instances<ImageTexture>;
|
||||
// pub type TextureDataTable = Instances<ImageTexture>;
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct ImageTexture {
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
use crate::application_io::{ImageTexture, TextureDataTable};
|
||||
use crate::instances::{Instance, Instances};
|
||||
use crate::raster::BlendMode;
|
||||
use crate::raster::image::{Image, RasterDataTable};
|
||||
use crate::raster::image::Image;
|
||||
use crate::raster_types::{CPU, GPU, Raster, RasterDataTable};
|
||||
use crate::transform::TransformMut;
|
||||
use crate::uuid::NodeId;
|
||||
use crate::vector::{VectorData, VectorDataTable};
|
||||
@@ -124,22 +124,17 @@ impl From<VectorDataTable> for GraphicGroupTable {
|
||||
}
|
||||
impl From<Image<Color>> for GraphicGroupTable {
|
||||
fn from(image: Image<Color>) -> Self {
|
||||
Self::new(GraphicElement::RasterDataType(RasterDataType::RasterData(RasterDataTable::new(image))))
|
||||
Self::new(GraphicElement::RasterDataCPU(RasterDataTable::<CPU>::new(Raster::new_cpu(image))))
|
||||
}
|
||||
}
|
||||
impl From<RasterDataTable<Color>> for GraphicGroupTable {
|
||||
fn from(image_frame: RasterDataTable<Color>) -> Self {
|
||||
Self::new(GraphicElement::RasterDataType(RasterDataType::RasterData(image_frame)))
|
||||
impl From<RasterDataTable<CPU>> for GraphicGroupTable {
|
||||
fn from(raster_data_table: RasterDataTable<CPU>) -> Self {
|
||||
Self::new(GraphicElement::RasterDataCPU(raster_data_table))
|
||||
}
|
||||
}
|
||||
impl From<ImageTexture> for GraphicGroupTable {
|
||||
fn from(image_texture: ImageTexture) -> Self {
|
||||
Self::new(GraphicElement::RasterDataType(RasterDataType::TextureData(TextureDataTable::new(image_texture))))
|
||||
}
|
||||
}
|
||||
impl From<TextureDataTable> for GraphicGroupTable {
|
||||
fn from(texture_frame: TextureDataTable) -> Self {
|
||||
Self::new(GraphicElement::RasterDataType(RasterDataType::TextureData(texture_frame)))
|
||||
impl From<RasterDataTable<GPU>> for GraphicGroupTable {
|
||||
fn from(raster_data_table: RasterDataTable<GPU>) -> Self {
|
||||
Self::new(GraphicElement::RasterDataGPU(raster_data_table))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -151,7 +146,8 @@ pub enum GraphicElement {
|
||||
GraphicGroup(GraphicGroupTable),
|
||||
/// A vector shape, equivalent to the SVG <path> tag: https://developer.mozilla.org/en-US/docs/Web/SVG/Element/path
|
||||
VectorData(VectorDataTable),
|
||||
RasterDataType(RasterDataType),
|
||||
RasterDataCPU(RasterDataTable<CPU>),
|
||||
RasterDataGPU(RasterDataTable<GPU>),
|
||||
}
|
||||
|
||||
impl Default for GraphicElement {
|
||||
@@ -189,50 +185,85 @@ impl GraphicElement {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn as_raster(&self) -> Option<&RasterDataType> {
|
||||
pub fn as_raster(&self) -> Option<&RasterDataTable<CPU>> {
|
||||
match self {
|
||||
GraphicElement::RasterDataType(raster) => Some(raster),
|
||||
GraphicElement::RasterDataCPU(raster) => Some(raster),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn as_raster_mut(&mut self) -> Option<&mut RasterDataType> {
|
||||
pub fn as_raster_mut(&mut self) -> Option<&mut RasterDataTable<CPU>> {
|
||||
match self {
|
||||
GraphicElement::RasterDataType(raster) => Some(raster),
|
||||
GraphicElement::RasterDataCPU(raster) => Some(raster),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// TODO: Rename to Raster
|
||||
#[derive(Clone, Debug, Hash, PartialEq, DynAny)]
|
||||
pub enum RasterDataType {
|
||||
/// 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
|
||||
// TODO: Rename to ImageTable
|
||||
RasterData(RasterDataTable<Color>),
|
||||
/// A GPU texture with a finite position and extent
|
||||
// TODO: Rename to ImageTextureTable
|
||||
TextureData(TextureDataTable),
|
||||
}
|
||||
// // TODO: Rename to Raster
|
||||
// #[derive(Clone, Debug, Hash, PartialEq, DynAny)]
|
||||
// pub enum RasterDataType {
|
||||
// /// 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
|
||||
// // TODO: Rename to ImageTable
|
||||
// RasterData(RasterDataTable<CPU>),
|
||||
// /// A GPU texture with a finite position and extent
|
||||
// // TODO: Rename to ImageTextureTable
|
||||
// TextureData(TextureDataTable),
|
||||
// }
|
||||
|
||||
impl<'de> serde::Deserialize<'de> for RasterDataType {
|
||||
// impl<'de> serde::Deserialize<'de> for RasterDataType {
|
||||
// fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
|
||||
// where
|
||||
// D: serde::Deserializer<'de>,
|
||||
// {
|
||||
// Ok(RasterDataType::RasterData(RasterDataTable::new(Image::deserialize(deserializer)?)))
|
||||
// }
|
||||
// }
|
||||
|
||||
// impl serde::Serialize for RasterDataType {
|
||||
// fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
||||
// where
|
||||
// S: serde::Serializer,
|
||||
// {
|
||||
// match self {
|
||||
// RasterDataType::RasterData(_) => self.serialize(serializer),
|
||||
// RasterDataType::TextureData(_) => todo!(),
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
|
||||
impl<'de> serde::Deserialize<'de> for Raster<CPU> {
|
||||
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
|
||||
where
|
||||
D: serde::Deserializer<'de>,
|
||||
{
|
||||
Ok(RasterDataType::RasterData(RasterDataTable::new(Image::deserialize(deserializer)?)))
|
||||
Ok(Raster::new_cpu(Image::deserialize(deserializer)?))
|
||||
}
|
||||
}
|
||||
|
||||
impl serde::Serialize for RasterDataType {
|
||||
impl serde::Serialize for Raster<CPU> {
|
||||
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
||||
where
|
||||
S: serde::Serializer,
|
||||
{
|
||||
match self {
|
||||
RasterDataType::RasterData(_) => self.serialize(serializer),
|
||||
RasterDataType::TextureData(_) => todo!(),
|
||||
}
|
||||
self.data().serialize(serializer)
|
||||
}
|
||||
}
|
||||
impl<'de> serde::Deserialize<'de> for Raster<GPU> {
|
||||
fn deserialize<D>(_deserializer: D) -> Result<Self, D::Error>
|
||||
where
|
||||
D: serde::Deserializer<'de>,
|
||||
{
|
||||
unimplemented!()
|
||||
}
|
||||
}
|
||||
|
||||
impl serde::Serialize for Raster<GPU> {
|
||||
fn serialize<S>(&self, _serializer: S) -> Result<S::Ok, S::Error>
|
||||
where
|
||||
S: serde::Serializer,
|
||||
{
|
||||
unimplemented!()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -324,8 +355,8 @@ async fn to_element<Data: Into<GraphicElement> + 'n>(
|
||||
#[implementations(
|
||||
GraphicGroupTable,
|
||||
VectorDataTable,
|
||||
RasterDataTable<Color>,
|
||||
TextureDataTable,
|
||||
RasterDataTable<CPU>,
|
||||
RasterDataTable<GPU>,
|
||||
)]
|
||||
data: Data,
|
||||
) -> GraphicElement {
|
||||
@@ -338,8 +369,8 @@ async fn to_group<Data: Into<GraphicGroupTable> + 'n>(
|
||||
#[implementations(
|
||||
GraphicGroupTable,
|
||||
VectorDataTable,
|
||||
RasterDataTable<Color>,
|
||||
TextureDataTable,
|
||||
RasterDataTable<CPU>,
|
||||
RasterDataTable<GPU>,
|
||||
)]
|
||||
element: Data,
|
||||
) -> GraphicGroupTable {
|
||||
@@ -391,8 +422,8 @@ async fn to_artboard<Data: Into<GraphicGroupTable> + 'n>(
|
||||
#[implementations(
|
||||
Context -> GraphicGroupTable,
|
||||
Context -> VectorDataTable,
|
||||
Context -> RasterDataTable<Color>,
|
||||
Context -> TextureDataTable,
|
||||
Context -> RasterDataTable<CPU>,
|
||||
Context -> RasterDataTable<GPU>,
|
||||
)]
|
||||
contents: impl Node<Context<'static>, Output = Data>,
|
||||
label: String,
|
||||
@@ -437,24 +468,28 @@ async fn append_artboard(_ctx: impl Ctx, mut artboards: ArtboardGroupTable, artb
|
||||
|
||||
// TODO: Remove this one
|
||||
impl From<Image<Color>> for GraphicElement {
|
||||
fn from(image_frame: Image<Color>) -> Self {
|
||||
GraphicElement::RasterDataType(RasterDataType::RasterData(RasterDataTable::new(image_frame)))
|
||||
fn from(raster_data: Image<Color>) -> Self {
|
||||
GraphicElement::RasterDataCPU(RasterDataTable::<CPU>::new(Raster::new_cpu(raster_data)))
|
||||
}
|
||||
}
|
||||
impl From<RasterDataTable<Color>> for GraphicElement {
|
||||
fn from(image_frame: RasterDataTable<Color>) -> Self {
|
||||
GraphicElement::RasterDataType(RasterDataType::RasterData(image_frame))
|
||||
impl From<RasterDataTable<CPU>> for GraphicElement {
|
||||
fn from(raster_data: RasterDataTable<CPU>) -> Self {
|
||||
GraphicElement::RasterDataCPU(raster_data)
|
||||
}
|
||||
}
|
||||
// TODO: Remove this one
|
||||
impl From<ImageTexture> for GraphicElement {
|
||||
fn from(image_texture: ImageTexture) -> Self {
|
||||
GraphicElement::RasterDataType(RasterDataType::TextureData(TextureDataTable::new(image_texture)))
|
||||
impl From<RasterDataTable<GPU>> for GraphicElement {
|
||||
fn from(raster_data: RasterDataTable<GPU>) -> Self {
|
||||
GraphicElement::RasterDataGPU(raster_data)
|
||||
}
|
||||
}
|
||||
impl From<TextureDataTable> for GraphicElement {
|
||||
fn from(texture_data: TextureDataTable) -> Self {
|
||||
GraphicElement::RasterDataType(RasterDataType::TextureData(texture_data))
|
||||
impl From<Raster<CPU>> for GraphicElement {
|
||||
fn from(raster_data: Raster<CPU>) -> Self {
|
||||
GraphicElement::RasterDataCPU(RasterDataTable::new(raster_data))
|
||||
}
|
||||
}
|
||||
impl From<Raster<GPU>> for GraphicElement {
|
||||
fn from(raster_data: Raster<GPU>) -> Self {
|
||||
GraphicElement::RasterDataGPU(RasterDataTable::new(raster_data))
|
||||
}
|
||||
}
|
||||
// TODO: Remove this one
|
||||
|
||||
@@ -1,13 +1,13 @@
|
||||
mod quad;
|
||||
mod rect;
|
||||
|
||||
use crate::raster::image::RasterDataTable;
|
||||
use crate::raster::{BlendMode, Image};
|
||||
use crate::raster_types::{CPU, GPU, RasterDataTable};
|
||||
use crate::transform::{Footprint, Transform};
|
||||
use crate::uuid::{NodeId, generate_uuid};
|
||||
use crate::vector::style::{Fill, Stroke, ViewMode};
|
||||
use crate::vector::{PointId, VectorDataTable};
|
||||
use crate::{Artboard, ArtboardGroupTable, Color, GraphicElement, GraphicGroupTable, RasterDataType};
|
||||
use crate::{Artboard, ArtboardGroupTable, Color, GraphicElement, GraphicGroupTable};
|
||||
use base64::Engine;
|
||||
use bezier_rs::Subpath;
|
||||
use dyn_any::DynAny;
|
||||
@@ -843,7 +843,7 @@ impl GraphicElementRendered for ArtboardGroupTable {
|
||||
}
|
||||
}
|
||||
|
||||
impl GraphicElementRendered for RasterDataTable<Color> {
|
||||
impl GraphicElementRendered for RasterDataTable<CPU> {
|
||||
fn render_svg(&self, render: &mut SvgRender, _render_params: &RenderParams) {
|
||||
for instance in self.instance_ref_iter() {
|
||||
let transform = *instance.transform * render.transform;
|
||||
@@ -923,12 +923,9 @@ impl GraphicElementRendered for RasterDataTable<Color> {
|
||||
}
|
||||
}
|
||||
|
||||
impl GraphicElementRendered for RasterDataType {
|
||||
fn render_svg(&self, render: &mut SvgRender, render_params: &RenderParams) {
|
||||
match self {
|
||||
RasterDataType::RasterData(image) => image.render_svg(render, render_params),
|
||||
RasterDataType::TextureData(_) => log::warn!("tried to render texture as an svg"),
|
||||
}
|
||||
impl GraphicElementRendered for RasterDataTable<GPU> {
|
||||
fn render_svg(&self, _render: &mut SvgRender, _render_params: &RenderParams) {
|
||||
log::warn!("tried to render texture as an svg");
|
||||
}
|
||||
|
||||
#[cfg(feature = "vello")]
|
||||
@@ -952,65 +949,34 @@ impl GraphicElementRendered for RasterDataType {
|
||||
}
|
||||
};
|
||||
|
||||
match self {
|
||||
RasterDataType::RasterData(image) => {
|
||||
for instance in image.instance_ref_iter() {
|
||||
let image = &instance.instance;
|
||||
if image.data.is_empty() {
|
||||
return;
|
||||
}
|
||||
for instance in self.instance_ref_iter() {
|
||||
let image = vello::peniko::Image::new(vec![].into(), peniko::Format::Rgba8, instance.instance.data().width(), instance.instance.data().height()).with_extend(peniko::Extend::Repeat);
|
||||
|
||||
let image = vello::peniko::Image::new(image.to_flat_u8().0.into(), peniko::Format::Rgba8, image.width, image.height).with_extend(peniko::Extend::Repeat);
|
||||
let id = image.data.id();
|
||||
context.resource_overrides.insert(id, instance.instance.data_owned());
|
||||
|
||||
render_stuff(image, *instance.transform, *instance.alpha_blending);
|
||||
}
|
||||
}
|
||||
RasterDataType::TextureData(image_texture) => {
|
||||
for instance in image_texture.instance_ref_iter() {
|
||||
let image =
|
||||
vello::peniko::Image::new(vec![].into(), peniko::Format::Rgba8, instance.instance.texture.width(), instance.instance.texture.height()).with_extend(peniko::Extend::Repeat);
|
||||
|
||||
let id = image.data.id();
|
||||
context.resource_overrides.insert(id, instance.instance.texture.clone());
|
||||
|
||||
render_stuff(image, *instance.transform, *instance.alpha_blending);
|
||||
}
|
||||
}
|
||||
render_stuff(image, *instance.transform, *instance.alpha_blending);
|
||||
}
|
||||
}
|
||||
|
||||
fn bounding_box(&self, transform: DAffine2, _include_stroke: bool) -> Option<[DVec2; 2]> {
|
||||
let calculate_transform = |instance_transform| {
|
||||
let transform: DAffine2 = transform * instance_transform;
|
||||
(transform.matrix2.determinant() != 0.).then(|| (transform * Quad::from_box([DVec2::ZERO, DVec2::ONE])).bounding_box())
|
||||
};
|
||||
|
||||
match self {
|
||||
RasterDataType::RasterData(instances) => instances.instance_ref_iter().flat_map(|instance| calculate_transform(*instance.transform)).reduce(Quad::combine_bounds),
|
||||
RasterDataType::TextureData(instances) => instances.instance_ref_iter().flat_map(|instance| calculate_transform(*instance.transform)).reduce(Quad::combine_bounds),
|
||||
}
|
||||
self.instance_ref_iter()
|
||||
.flat_map(|instance| {
|
||||
let transform = transform * *instance.transform;
|
||||
(transform.matrix2.determinant() != 0.).then(|| (transform * Quad::from_box([DVec2::ZERO, DVec2::ONE])).bounding_box())
|
||||
})
|
||||
.reduce(Quad::combine_bounds)
|
||||
}
|
||||
|
||||
fn collect_metadata(&self, metadata: &mut RenderMetadata, footprint: Footprint, element_id: Option<NodeId>) {
|
||||
let Some(element_id) = element_id else { return };
|
||||
|
||||
let subpath = Subpath::new_rect(DVec2::ZERO, DVec2::ONE);
|
||||
|
||||
metadata.click_targets.insert(element_id, vec![ClickTarget::new(subpath, 0.)]);
|
||||
metadata.upstream_footprints.insert(element_id, footprint);
|
||||
|
||||
match self {
|
||||
RasterDataType::RasterData(instances) => {
|
||||
// TODO: Find a way to handle more than one row of the graphical data table
|
||||
if let Some(image) = instances.instance_ref_iter().next() {
|
||||
metadata.local_transforms.insert(element_id, *image.transform);
|
||||
}
|
||||
}
|
||||
RasterDataType::TextureData(instances) => {
|
||||
// TODO: Find a way to handle more than one row of the graphical data table
|
||||
if let Some(image_texture) = instances.instance_ref_iter().next() {
|
||||
metadata.local_transforms.insert(element_id, *image_texture.transform);
|
||||
}
|
||||
}
|
||||
// TODO: Find a way to handle more than one row of the graphical data table
|
||||
if let Some(image) = self.instance_ref_iter().next() {
|
||||
metadata.local_transforms.insert(element_id, *image.transform);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1024,7 +990,8 @@ impl GraphicElementRendered for GraphicElement {
|
||||
fn render_svg(&self, render: &mut SvgRender, render_params: &RenderParams) {
|
||||
match self {
|
||||
GraphicElement::VectorData(vector_data) => vector_data.render_svg(render, render_params),
|
||||
GraphicElement::RasterDataType(raster) => raster.render_svg(render, render_params),
|
||||
GraphicElement::RasterDataCPU(raster) => raster.render_svg(render, render_params),
|
||||
GraphicElement::RasterDataGPU(_raster) => (),
|
||||
GraphicElement::GraphicGroup(graphic_group) => graphic_group.render_svg(render, render_params),
|
||||
}
|
||||
}
|
||||
@@ -1033,15 +1000,17 @@ impl GraphicElementRendered for GraphicElement {
|
||||
fn render_to_vello(&self, scene: &mut Scene, transform: DAffine2, context: &mut RenderContext, render_params: &RenderParams) {
|
||||
match self {
|
||||
GraphicElement::VectorData(vector_data) => vector_data.render_to_vello(scene, transform, context, render_params),
|
||||
GraphicElement::RasterDataCPU(raster) => raster.render_to_vello(scene, transform, context, render_params),
|
||||
GraphicElement::RasterDataGPU(raster) => raster.render_to_vello(scene, transform, context, render_params),
|
||||
GraphicElement::GraphicGroup(graphic_group) => graphic_group.render_to_vello(scene, transform, context, render_params),
|
||||
GraphicElement::RasterDataType(raster) => raster.render_to_vello(scene, transform, context, render_params),
|
||||
}
|
||||
}
|
||||
|
||||
fn bounding_box(&self, transform: DAffine2, include_stroke: bool) -> Option<[DVec2; 2]> {
|
||||
match self {
|
||||
GraphicElement::VectorData(vector_data) => vector_data.bounding_box(transform, include_stroke),
|
||||
GraphicElement::RasterDataType(raster) => raster.bounding_box(transform, include_stroke),
|
||||
GraphicElement::RasterDataCPU(raster) => raster.bounding_box(transform, include_stroke),
|
||||
GraphicElement::RasterDataGPU(raster) => raster.bounding_box(transform, include_stroke),
|
||||
GraphicElement::GraphicGroup(graphic_group) => graphic_group.bounding_box(transform, include_stroke),
|
||||
}
|
||||
}
|
||||
@@ -1059,21 +1028,20 @@ impl GraphicElementRendered for GraphicElement {
|
||||
metadata.local_transforms.insert(element_id, *vector_data.transform);
|
||||
}
|
||||
}
|
||||
GraphicElement::RasterDataType(raster_frame) => {
|
||||
GraphicElement::RasterDataCPU(raster_frame) => {
|
||||
metadata.upstream_footprints.insert(element_id, footprint);
|
||||
match raster_frame {
|
||||
RasterDataType::RasterData(instances) => {
|
||||
// TODO: Find a way to handle more than one row of images
|
||||
if let Some(image) = instances.instance_ref_iter().next() {
|
||||
metadata.local_transforms.insert(element_id, *image.transform);
|
||||
}
|
||||
}
|
||||
RasterDataType::TextureData(instances) => {
|
||||
// TODO: Find a way to handle more than one row of image textures
|
||||
if let Some(image_texture) = instances.instance_ref_iter().next() {
|
||||
metadata.local_transforms.insert(element_id, *image_texture.transform);
|
||||
}
|
||||
}
|
||||
|
||||
// TODO: Find a way to handle more than one row of images
|
||||
if let Some(image) = raster_frame.instance_ref_iter().next() {
|
||||
metadata.local_transforms.insert(element_id, *image.transform);
|
||||
}
|
||||
}
|
||||
GraphicElement::RasterDataGPU(raster_frame) => {
|
||||
metadata.upstream_footprints.insert(element_id, footprint);
|
||||
|
||||
// TODO: Find a way to handle more than one row of images
|
||||
if let Some(image) = raster_frame.instance_ref_iter().next() {
|
||||
metadata.local_transforms.insert(element_id, *image.transform);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1081,7 +1049,8 @@ impl GraphicElementRendered for GraphicElement {
|
||||
|
||||
match self {
|
||||
GraphicElement::VectorData(vector_data) => vector_data.collect_metadata(metadata, footprint, element_id),
|
||||
GraphicElement::RasterDataType(raster) => raster.collect_metadata(metadata, footprint, element_id),
|
||||
GraphicElement::RasterDataCPU(raster) => raster.collect_metadata(metadata, footprint, element_id),
|
||||
GraphicElement::RasterDataGPU(raster) => raster.collect_metadata(metadata, footprint, element_id),
|
||||
GraphicElement::GraphicGroup(graphic_group) => graphic_group.collect_metadata(metadata, footprint, element_id),
|
||||
}
|
||||
}
|
||||
@@ -1089,7 +1058,8 @@ impl GraphicElementRendered for GraphicElement {
|
||||
fn add_upstream_click_targets(&self, click_targets: &mut Vec<ClickTarget>) {
|
||||
match self {
|
||||
GraphicElement::VectorData(vector_data) => vector_data.add_upstream_click_targets(click_targets),
|
||||
GraphicElement::RasterDataType(raster) => raster.add_upstream_click_targets(click_targets),
|
||||
GraphicElement::RasterDataCPU(raster) => raster.add_upstream_click_targets(click_targets),
|
||||
GraphicElement::RasterDataGPU(raster) => raster.add_upstream_click_targets(click_targets),
|
||||
GraphicElement::GraphicGroup(graphic_group) => graphic_group.add_upstream_click_targets(click_targets),
|
||||
}
|
||||
}
|
||||
@@ -1098,7 +1068,8 @@ impl GraphicElementRendered for GraphicElement {
|
||||
match self {
|
||||
GraphicElement::VectorData(vector_data) => vector_data.contains_artboard(),
|
||||
GraphicElement::GraphicGroup(graphic_group) => graphic_group.contains_artboard(),
|
||||
GraphicElement::RasterDataType(raster) => raster.contains_artboard(),
|
||||
GraphicElement::RasterDataCPU(raster) => raster.contains_artboard(),
|
||||
GraphicElement::RasterDataGPU(raster) => raster.contains_artboard(),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1106,7 +1077,8 @@ impl GraphicElementRendered for GraphicElement {
|
||||
match self {
|
||||
GraphicElement::VectorData(vector_data) => vector_data.new_ids_from_hash(reference),
|
||||
GraphicElement::GraphicGroup(graphic_group) => graphic_group.new_ids_from_hash(reference),
|
||||
GraphicElement::RasterDataType(_) => (),
|
||||
GraphicElement::RasterDataCPU(_) => (),
|
||||
GraphicElement::RasterDataGPU(_) => (),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -22,6 +22,7 @@ pub mod instances;
|
||||
pub mod logic;
|
||||
pub mod misc;
|
||||
pub mod ops;
|
||||
pub mod raster_types;
|
||||
pub mod structural;
|
||||
#[cfg(feature = "std")]
|
||||
pub mod text;
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
use crate::Ctx;
|
||||
use crate::raster::BlendMode;
|
||||
use crate::raster::image::RasterDataTable;
|
||||
use crate::raster_types::{CPU, RasterDataTable};
|
||||
use crate::registry::types::{Fraction, Percentage};
|
||||
use crate::vector::style::GradientStops;
|
||||
use crate::{Color, Node};
|
||||
@@ -453,7 +453,7 @@ fn color_value(_: impl Ctx, _primary: (), #[default(Color::BLACK)] color: Option
|
||||
// _: impl Ctx,
|
||||
// #[implementations(
|
||||
// Color,
|
||||
// RasterDataTable<Color>,
|
||||
// RasterDataTable<CPU>,
|
||||
// GradientStops,
|
||||
// )]
|
||||
// mut image: T,
|
||||
@@ -515,7 +515,7 @@ fn unwrap<T: Default>(_: impl Ctx, #[implementations(Option<f64>, Option<f32>, O
|
||||
|
||||
/// Meant for debugging purposes, not general use. Clones the input value.
|
||||
#[node_macro::node(category("Debug"))]
|
||||
fn clone<'i, T: Clone + 'i>(_: impl Ctx, #[implementations(&RasterDataTable<Color>)] value: &'i T) -> T {
|
||||
fn clone<'i, T: Clone + 'i>(_: impl Ctx, #[implementations(&RasterDataTable<CPU>)] value: &'i T) -> T {
|
||||
value.clone()
|
||||
}
|
||||
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
pub use self::color::{Color, Luma, SRGBA8};
|
||||
use crate::Ctx;
|
||||
use crate::GraphicGroupTable;
|
||||
use crate::raster::image::RasterDataTable;
|
||||
use crate::raster_types::{CPU, RasterDataTable};
|
||||
use crate::registry::types::Percentage;
|
||||
use crate::vector::VectorDataTable;
|
||||
use bytemuck::{Pod, Zeroable};
|
||||
@@ -310,7 +310,7 @@ impl SetBlendMode for GraphicGroupTable {
|
||||
}
|
||||
}
|
||||
}
|
||||
impl SetBlendMode for RasterDataTable<Color> {
|
||||
impl SetBlendMode for RasterDataTable<CPU> {
|
||||
fn set_blend_mode(&mut self, blend_mode: BlendMode) {
|
||||
for instance in self.instance_mut_iter() {
|
||||
instance.alpha_blending.blend_mode = blend_mode;
|
||||
@@ -324,7 +324,7 @@ fn blend_mode<T: SetBlendMode>(
|
||||
#[implementations(
|
||||
GraphicGroupTable,
|
||||
VectorDataTable,
|
||||
RasterDataTable<Color>,
|
||||
RasterDataTable<CPU>,
|
||||
)]
|
||||
mut value: T,
|
||||
blend_mode: BlendMode,
|
||||
@@ -340,7 +340,7 @@ fn opacity<T: MultiplyAlpha>(
|
||||
#[implementations(
|
||||
GraphicGroupTable,
|
||||
VectorDataTable,
|
||||
RasterDataTable<Color>,
|
||||
RasterDataTable<CPU>,
|
||||
)]
|
||||
mut value: T,
|
||||
#[default(100.)] factor: Percentage,
|
||||
|
||||
@@ -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 {
|
||||
|
||||
102
node-graph/gcore/src/raster_types.rs
Normal file
102
node-graph/gcore/src/raster_types.rs
Normal file
@@ -0,0 +1,102 @@
|
||||
use crate::Color;
|
||||
use crate::instances::Instances;
|
||||
use crate::raster::Image;
|
||||
use core::ops::Deref;
|
||||
use dyn_any::DynAny;
|
||||
#[cfg(feature = "wgpu")]
|
||||
use std::sync::Arc;
|
||||
|
||||
#[derive(Clone, Debug, Hash, PartialEq, Eq, Copy)]
|
||||
pub struct CPU;
|
||||
#[derive(Clone, Debug, Hash, PartialEq, Eq, Copy)]
|
||||
pub struct GPU;
|
||||
|
||||
trait Storage {}
|
||||
impl Storage for CPU {}
|
||||
impl Storage for GPU {}
|
||||
|
||||
#[derive(Clone, Debug, Hash, PartialEq)]
|
||||
#[allow(private_bounds)]
|
||||
pub struct Raster<T: 'static + Storage> {
|
||||
data: RasterStorage,
|
||||
storage: T,
|
||||
}
|
||||
|
||||
unsafe impl<T: 'static + Storage> dyn_any::StaticType for Raster<T> {
|
||||
type Static = Raster<T>;
|
||||
}
|
||||
#[derive(Clone, Debug, Hash, PartialEq, DynAny)]
|
||||
pub enum RasterStorage {
|
||||
Cpu(Image<Color>),
|
||||
#[cfg(feature = "wgpu")]
|
||||
Gpu(Arc<wgpu::Texture>),
|
||||
#[cfg(not(feature = "wgpu"))]
|
||||
Gpu(()),
|
||||
}
|
||||
|
||||
impl RasterStorage {}
|
||||
impl Raster<CPU> {
|
||||
pub fn new_cpu(image: Image<Color>) -> Self {
|
||||
Self {
|
||||
data: RasterStorage::Cpu(image),
|
||||
storage: CPU,
|
||||
}
|
||||
}
|
||||
pub fn data(&self) -> &Image<Color> {
|
||||
let RasterStorage::Cpu(cpu) = &self.data else { unreachable!() };
|
||||
cpu
|
||||
}
|
||||
pub fn data_mut(&mut self) -> &mut Image<Color> {
|
||||
let RasterStorage::Cpu(cpu) = &mut self.data else { unreachable!() };
|
||||
cpu
|
||||
}
|
||||
pub fn into_data(self) -> Image<Color> {
|
||||
let RasterStorage::Cpu(cpu) = self.data else { unreachable!() };
|
||||
cpu
|
||||
}
|
||||
}
|
||||
impl Default for Raster<CPU> {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
data: RasterStorage::Cpu(Image::default()),
|
||||
storage: CPU,
|
||||
}
|
||||
}
|
||||
}
|
||||
impl Deref for Raster<CPU> {
|
||||
type Target = Image<Color>;
|
||||
|
||||
fn deref(&self) -> &Self::Target {
|
||||
self.data()
|
||||
}
|
||||
}
|
||||
#[cfg(feature = "wgpu")]
|
||||
impl Raster<GPU> {
|
||||
pub fn new_gpu(image: Arc<wgpu::Texture>) -> Self {
|
||||
Self {
|
||||
data: RasterStorage::Gpu(image),
|
||||
storage: GPU,
|
||||
}
|
||||
}
|
||||
pub fn data(&self) -> &wgpu::Texture {
|
||||
let RasterStorage::Gpu(gpu) = &self.data else { unreachable!() };
|
||||
gpu
|
||||
}
|
||||
pub fn data_mut(&mut self) -> &mut Arc<wgpu::Texture> {
|
||||
let RasterStorage::Gpu(gpu) = &mut self.data else { unreachable!() };
|
||||
gpu
|
||||
}
|
||||
pub fn data_owned(&self) -> Arc<wgpu::Texture> {
|
||||
let RasterStorage::Gpu(gpu) = &self.data else { unreachable!() };
|
||||
gpu.clone()
|
||||
}
|
||||
}
|
||||
#[cfg(feature = "wgpu")]
|
||||
impl Deref for Raster<GPU> {
|
||||
type Target = wgpu::Texture;
|
||||
|
||||
fn deref(&self) -> &Self::Target {
|
||||
self.data()
|
||||
}
|
||||
}
|
||||
pub type RasterDataTable<Storage> = Instances<Raster<Storage>>;
|
||||
@@ -1,9 +1,8 @@
|
||||
use crate::application_io::TextureDataTable;
|
||||
use crate::instances::Instances;
|
||||
use crate::raster::bbox::AxisAlignedBbox;
|
||||
use crate::raster::image::RasterDataTable;
|
||||
use crate::raster_types::{CPU, GPU, RasterDataTable};
|
||||
use crate::vector::VectorDataTable;
|
||||
use crate::{Artboard, CloneVarArgs, Color, Context, Ctx, ExtractAll, GraphicGroupTable, OwnedContextImpl};
|
||||
use crate::{Artboard, CloneVarArgs, Context, Ctx, ExtractAll, GraphicGroupTable, OwnedContextImpl};
|
||||
use core::f64;
|
||||
use glam::{DAffine2, DMat2, DVec2};
|
||||
|
||||
@@ -162,8 +161,8 @@ async fn transform<T: 'n + 'static>(
|
||||
#[implementations(
|
||||
Context -> VectorDataTable,
|
||||
Context -> GraphicGroupTable,
|
||||
Context -> RasterDataTable<Color>,
|
||||
Context -> TextureDataTable,
|
||||
Context -> RasterDataTable<CPU>,
|
||||
Context -> RasterDataTable<GPU>,
|
||||
)]
|
||||
transform_target: impl Node<Context<'static>, Output = Instances<T>>,
|
||||
translate: DVec2,
|
||||
@@ -194,7 +193,7 @@ async fn transform<T: 'n + 'static>(
|
||||
#[node_macro::node(category(""))]
|
||||
fn replace_transform<Data, TransformInput: Transform>(
|
||||
_: impl Ctx,
|
||||
#[implementations(VectorDataTable, RasterDataTable<Color>, GraphicGroupTable)] mut data: Instances<Data>,
|
||||
#[implementations(VectorDataTable, RasterDataTable<CPU>, GraphicGroupTable)] mut data: Instances<Data>,
|
||||
#[implementations(DAffine2)] transform: TransformInput,
|
||||
) -> Instances<Data> {
|
||||
for data_transform in data.instance_mut_iter() {
|
||||
@@ -209,8 +208,8 @@ async fn boundless_footprint<T: 'n + 'static>(
|
||||
#[implementations(
|
||||
Context -> VectorDataTable,
|
||||
Context -> GraphicGroupTable,
|
||||
Context -> RasterDataTable<Color>,
|
||||
Context -> TextureDataTable,
|
||||
Context -> RasterDataTable<CPU>,
|
||||
Context -> RasterDataTable<GPU>,
|
||||
Context -> String,
|
||||
Context -> f64,
|
||||
)]
|
||||
@@ -226,8 +225,8 @@ async fn freeze_real_time<T: 'n + 'static>(
|
||||
#[implementations(
|
||||
Context -> VectorDataTable,
|
||||
Context -> GraphicGroupTable,
|
||||
Context -> RasterDataTable<Color>,
|
||||
Context -> TextureDataTable,
|
||||
Context -> RasterDataTable<CPU>,
|
||||
Context -> RasterDataTable<GPU>,
|
||||
Context -> String,
|
||||
Context -> f64,
|
||||
)]
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
use crate::instances::{InstanceRef, Instances};
|
||||
use crate::raster::Color;
|
||||
use crate::raster::image::RasterDataTable;
|
||||
use crate::raster_types::{CPU, RasterDataTable};
|
||||
use crate::transform::TransformMut;
|
||||
use crate::vector::VectorDataTable;
|
||||
use crate::{CloneVarArgs, Context, Ctx, ExtractAll, ExtractIndex, ExtractVarArgs, GraphicElement, GraphicGroupTable, OwnedContextImpl};
|
||||
@@ -10,7 +9,7 @@ use glam::DVec2;
|
||||
async fn instance_on_points<T: Into<GraphicElement> + Default + Clone + 'static>(
|
||||
ctx: impl ExtractAll + CloneVarArgs + Sync + Ctx,
|
||||
points: VectorDataTable,
|
||||
#[implementations(Context -> GraphicGroupTable, Context -> VectorDataTable, Context -> RasterDataTable<Color>)] instance: impl Node<'n, Context<'static>, Output = Instances<T>>,
|
||||
#[implementations(Context -> GraphicGroupTable, Context -> VectorDataTable, Context -> RasterDataTable<CPU>)] instance: impl Node<'n, Context<'static>, Output = Instances<T>>,
|
||||
reverse: bool,
|
||||
) -> GraphicGroupTable {
|
||||
let mut result_table = GraphicGroupTable::default();
|
||||
@@ -46,7 +45,7 @@ async fn instance_on_points<T: Into<GraphicElement> + Default + Clone + 'static>
|
||||
#[node_macro::node(category("Instancing"), path(graphene_core::vector))]
|
||||
async fn instance_repeat<T: Into<GraphicElement> + Default + Clone + 'static>(
|
||||
ctx: impl ExtractAll + CloneVarArgs + Ctx,
|
||||
#[implementations(Context -> GraphicGroupTable, Context -> VectorDataTable, Context -> RasterDataTable<Color>)] instance: impl Node<'n, Context<'static>, Output = Instances<T>>,
|
||||
#[implementations(Context -> GraphicGroupTable, Context -> VectorDataTable, Context -> RasterDataTable<CPU>)] instance: impl Node<'n, Context<'static>, Output = Instances<T>>,
|
||||
#[default(1)] count: u64,
|
||||
reverse: bool,
|
||||
) -> GraphicGroupTable {
|
||||
|
||||
@@ -37,6 +37,7 @@ impl Hash for BrushStyle {
|
||||
self.hardness.to_bits().hash(state);
|
||||
self.flow.to_bits().hash(state);
|
||||
self.spacing.to_bits().hash(state);
|
||||
self.blend_mode.hash(state);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -4,7 +4,7 @@ use super::misc::{CentroidType, point_to_dvec2};
|
||||
use super::style::{Fill, Gradient, GradientStops, Stroke};
|
||||
use super::{PointId, SegmentDomain, SegmentId, StrokeId, VectorData, VectorDataTable};
|
||||
use crate::instances::{Instance, InstanceMut, Instances};
|
||||
use crate::raster::image::RasterDataTable;
|
||||
use crate::raster_types::{CPU, RasterDataTable};
|
||||
use crate::registry::types::{Angle, Fraction, IntegerCount, Length, Multiplier, Percentage, PixelLength, PixelSize, SeedValue};
|
||||
use crate::renderer::GraphicElementRendered;
|
||||
use crate::transform::{Footprint, ReferencePoint, Transform};
|
||||
@@ -204,7 +204,7 @@ where
|
||||
async fn repeat<I: 'n + Send>(
|
||||
_: impl Ctx,
|
||||
// TODO: Implement other GraphicElementRendered types.
|
||||
#[implementations(GraphicGroupTable, VectorDataTable, RasterDataTable<Color>)] instance: Instances<I>,
|
||||
#[implementations(GraphicGroupTable, VectorDataTable, RasterDataTable<CPU>)] instance: Instances<I>,
|
||||
#[default(100., 100.)]
|
||||
// TODO: When using a custom Properties panel layout in document_node_definitions.rs and this default is set, the widget weirdly doesn't show up in the Properties panel. Investigation is needed.
|
||||
direction: PixelSize,
|
||||
@@ -246,7 +246,7 @@ where
|
||||
async fn circular_repeat<I: 'n + Send>(
|
||||
_: impl Ctx,
|
||||
// TODO: Implement other GraphicElementRendered types.
|
||||
#[implementations(GraphicGroupTable, VectorDataTable, RasterDataTable<Color>)] instance: Instances<I>,
|
||||
#[implementations(GraphicGroupTable, VectorDataTable, RasterDataTable<CPU>)] instance: Instances<I>,
|
||||
angle_offset: Angle,
|
||||
#[default(5)] radius: f64,
|
||||
#[default(5)] instances: IntegerCount,
|
||||
@@ -286,7 +286,7 @@ async fn copy_to_points<I: 'n + Send>(
|
||||
points: VectorDataTable,
|
||||
#[expose]
|
||||
/// Artwork to be copied and placed at each point.
|
||||
#[implementations(GraphicGroupTable, VectorDataTable, RasterDataTable<Color>)]
|
||||
#[implementations(GraphicGroupTable, VectorDataTable, RasterDataTable<CPU>)]
|
||||
instance: Instances<I>,
|
||||
/// Minimum range of randomized sizes given to each instance.
|
||||
#[default(1)]
|
||||
@@ -370,7 +370,7 @@ where
|
||||
#[node_macro::node(category("Vector"), path(graphene_core::vector))]
|
||||
async fn mirror<I: 'n + Send>(
|
||||
_: impl Ctx,
|
||||
#[implementations(GraphicGroupTable, VectorDataTable, RasterDataTable<Color>)] instance: Instances<I>,
|
||||
#[implementations(GraphicGroupTable, VectorDataTable, RasterDataTable<CPU>)] instance: Instances<I>,
|
||||
#[default(ReferencePoint::Center)] reference_point: ReferencePoint,
|
||||
offset: f64,
|
||||
#[range((-90., 90.))] angle: Angle,
|
||||
|
||||
Reference in New Issue
Block a user