Files
Graphite/graphene/src/operation.rs
Keavon Chambers 30719bdc72 Integrate Stable Diffusion with the Imaginate layer (#784)
* Add AI Artist layer

* WIP add a button to download the rendered folder under an AI Artist layer

* Successfully download the correct image

* Break out image downloading JS into helper function

* Change file download from using data URLs to blob URLs

* WIP rasterize to blob

* Remove dimensions from AI Artist layer

* Successfully draw rasterized image on layer after calculation

* Working txt2img generation based on user prompt

* Add img2img and the main parameters

* Fix ability to rasterize multi-depth documents with blob URL images by switching them to base64

* Fix test

* Rasterize with artboard background color

* Allow aspect ratio stretch of AI Artist images

* Add automatic resolution choosing

* Add a terminate button, and make the lifecycle more robust

* Add negative prompt

* Add range bounds for parameter inputs

* Add seed

* Add tiling and restore faces

* Add server status check, server hostname customization, and resizing layer to fit AI Artist resolution

* Fix background color of infinite canvas rasterization

* Escape prompt text sent in the JSON

* Revoke blob URLs when cleared/replaced to reduce memory leak

* Fix welcome screen logo color

* Add PreferencesMessageHandler

* Add persistent storage of preferences

* Fix crash introduced in previous commit when moving mouse on page load

* Add tooltips to the AI Artist layer properties

* Integrate AI Artist tool into the raster section of the tool shelf

* Add a refresh button to the connection status

* Fix crash when generating and switching to a different document tab

* Add persistent image storage to AI Artist layers and fix duplication bugs

* Add a generate with random seed button

* Simplify and standardize message names

* Majorly improve robustness of networking code

* Fix race condition causing default server hostname to show disconnected when app loads with AI Artist layer selected (probably, not confirmed fixed)

* Clean up messages and function calls by changing arguments into structs

* Update API to more recent server commit

* Add support for picking the sampling method

* Add machinery for filtering selected layers with type

* Replace placeholder button icons

* Improve the random icon by tilting the dice

* Use selected_layers() instead of repeating that code

* Fix borrow error

* Change message flow in progress towards fixing #797

* Allow loading image on non-active document (fixes #797)

* Reduce code duplication with rasterization

* Add AI Artist tool and layer icons, and remove ugly node layer icon style

* Rename "AI Artist" codename to "Imaginate" feature name

Co-authored-by: otdavies <oliver@psyfer.io>
Co-authored-by: 0hypercube <0hypercube@gmail.com>
2022-10-18 22:33:27 -07:00

340 lines
7.2 KiB
Rust

use crate::boolean_ops::BooleanOperation as BooleanOperationType;
use crate::layers::blend_mode::BlendMode;
use crate::layers::imaginate_layer::{ImaginateSamplingMethod, ImaginateStatus};
use crate::layers::layer_info::Layer;
use crate::layers::style::{self, Stroke};
use crate::layers::vector::consts::ManipulatorType;
use crate::layers::vector::manipulator_group::ManipulatorGroup;
use crate::layers::vector::subpath::Subpath;
use crate::LayerId;
use serde::{Deserialize, Serialize};
use std::collections::hash_map::DefaultHasher;
use std::hash::{Hash, Hasher};
#[repr(C)]
#[derive(Debug, Clone, Deserialize, Serialize, PartialEq)]
// TODO: Rename all instances of `path` to `layer_path`
/// Operations that can be performed to mutate the document.
pub enum Operation {
AddEllipse {
path: Vec<LayerId>,
insert_index: isize,
transform: [f64; 6],
style: style::PathStyle,
},
AddRect {
path: Vec<LayerId>,
insert_index: isize,
transform: [f64; 6],
style: style::PathStyle,
},
AddLine {
path: Vec<LayerId>,
insert_index: isize,
transform: [f64; 6],
style: style::PathStyle,
},
AddText {
path: Vec<LayerId>,
insert_index: isize,
transform: [f64; 6],
style: style::PathStyle,
text: String,
size: f64,
font_name: String,
font_style: String,
},
AddImage {
path: Vec<LayerId>,
insert_index: isize,
transform: [f64; 6],
mime: String,
image_data: Vec<u8>,
},
AddImaginateFrame {
path: Vec<LayerId>,
insert_index: isize,
transform: [f64; 6],
},
/// Sets a blob URL as the image source for an Image or Imaginate layer type.
/// **Be sure to call `FrontendMessage::TriggerRevokeBlobUrl` together with this.**
SetLayerBlobUrl {
layer_path: Vec<LayerId>,
blob_url: String,
resolution: (f64, f64),
},
/// Clears the image to leave the Imaginate layer un-rendered.
/// **Be sure to call `FrontendMessage::TriggerRevokeBlobUrl` together with this.**
ImaginateClear {
path: Vec<LayerId>,
},
ImaginateSetGeneratingStatus {
path: Vec<LayerId>,
percent: Option<f64>,
status: ImaginateStatus,
},
ImaginateSetImageData {
layer_path: Vec<LayerId>,
image_data: Vec<u8>,
},
ImaginateSetNegativePrompt {
path: Vec<LayerId>,
negative_prompt: String,
},
ImaginateSetPrompt {
path: Vec<LayerId>,
prompt: String,
},
ImaginateSetCfgScale {
path: Vec<LayerId>,
cfg_scale: f64,
},
ImaginateSetSamples {
path: Vec<LayerId>,
samples: u32,
},
SetImaginateSamplingMethod {
path: Vec<LayerId>,
method: ImaginateSamplingMethod,
},
ImaginateSetScaleFromResolution {
path: Vec<LayerId>,
},
ImaginateSetSeed {
path: Vec<LayerId>,
seed: u64,
},
ImaginateSetDenoisingStrength {
path: Vec<LayerId>,
denoising_strength: f64,
},
ImaginateSetUseImg2Img {
path: Vec<LayerId>,
use_img2img: bool,
},
ImaginateSetRestoreFaces {
path: Vec<LayerId>,
restore_faces: bool,
},
ImaginateSetTiling {
path: Vec<LayerId>,
tiling: bool,
},
SetPivot {
layer_path: Vec<LayerId>,
pivot: (f64, f64),
},
SetTextEditability {
path: Vec<LayerId>,
editable: bool,
},
SetTextContent {
path: Vec<LayerId>,
new_text: String,
},
AddPolyline {
path: Vec<LayerId>,
insert_index: isize,
transform: [f64; 6],
style: style::PathStyle,
points: Vec<(f64, f64)>,
},
AddSpline {
path: Vec<LayerId>,
insert_index: isize,
transform: [f64; 6],
style: style::PathStyle,
points: Vec<(f64, f64)>,
},
AddNgon {
path: Vec<LayerId>,
insert_index: isize,
transform: [f64; 6],
style: style::PathStyle,
sides: u32,
},
AddShape {
path: Vec<LayerId>,
insert_index: isize,
transform: [f64; 6],
style: style::PathStyle,
// TODO This will become a compound path once we support them.
subpath: Subpath,
},
BooleanOperation {
operation: BooleanOperationType,
selected: Vec<Vec<LayerId>>,
},
DeleteLayer {
path: Vec<LayerId>,
},
DeleteSelectedManipulatorPoints {
layer_paths: Vec<Vec<LayerId>>,
},
DeselectManipulatorPoints {
layer_path: Vec<LayerId>,
point_ids: Vec<(u64, ManipulatorType)>,
},
DeselectAllManipulatorPoints {
layer_path: Vec<LayerId>,
},
DuplicateLayer {
path: Vec<LayerId>,
},
ModifyFont {
path: Vec<LayerId>,
font_family: String,
size: f64,
font_style: String,
},
MoveSelectedManipulatorPoints {
layer_path: Vec<LayerId>,
delta: (f64, f64),
},
MoveManipulatorPoint {
layer_path: Vec<LayerId>,
id: u64,
manipulator_type: ManipulatorType,
position: (f64, f64),
},
SetManipulatorPoints {
layer_path: Vec<LayerId>,
id: u64,
manipulator_type: ManipulatorType,
position: Option<(f64, f64)>,
},
RenameLayer {
layer_path: Vec<LayerId>,
new_name: String,
},
InsertLayer {
layer: Box<Layer>,
destination_path: Vec<LayerId>,
insert_index: isize,
},
CreateFolder {
path: Vec<LayerId>,
},
TransformLayer {
path: Vec<LayerId>,
transform: [f64; 6],
},
TransformLayerInViewport {
path: Vec<LayerId>,
transform: [f64; 6],
},
SetLayerTransformInViewport {
path: Vec<LayerId>,
transform: [f64; 6],
},
SelectManipulatorPoints {
layer_path: Vec<LayerId>,
point_ids: Vec<(u64, ManipulatorType)>,
add: bool,
},
SetShapePath {
path: Vec<LayerId>,
subpath: Subpath,
},
InsertManipulatorGroup {
layer_path: Vec<LayerId>,
manipulator_group: ManipulatorGroup,
after_id: u64,
},
PushManipulatorGroup {
layer_path: Vec<LayerId>,
manipulator_group: ManipulatorGroup,
},
PushFrontManipulatorGroup {
layer_path: Vec<LayerId>,
manipulator_group: ManipulatorGroup,
},
RemoveManipulatorGroup {
layer_path: Vec<LayerId>,
id: u64,
},
RemoveManipulatorPoint {
layer_path: Vec<LayerId>,
id: u64,
manipulator_type: ManipulatorType,
},
TransformLayerInScope {
path: Vec<LayerId>,
transform: [f64; 6],
scope: [f64; 6],
},
SetLayerTransformInScope {
path: Vec<LayerId>,
transform: [f64; 6],
scope: [f64; 6],
},
SetLayerTransform {
path: Vec<LayerId>,
transform: [f64; 6],
},
ToggleLayerVisibility {
path: Vec<LayerId>,
},
SetLayerVisibility {
path: Vec<LayerId>,
visible: bool,
},
SetLayerName {
path: Vec<LayerId>,
name: String,
},
SetLayerBlendMode {
path: Vec<LayerId>,
blend_mode: BlendMode,
},
SetLayerOpacity {
path: Vec<LayerId>,
opacity: f64,
},
SetLayerStyle {
path: Vec<LayerId>,
style: style::PathStyle,
},
SetLayerFill {
path: Vec<LayerId>,
fill: style::Fill,
},
SetLayerStroke {
path: Vec<LayerId>,
stroke: Stroke,
},
SetManipulatorHandleMirroring {
layer_path: Vec<LayerId>,
id: u64,
mirror_distance: bool,
mirror_angle: bool,
},
SetSelectedHandleMirroring {
layer_path: Vec<LayerId>,
toggle_distance: bool,
toggle_angle: bool,
},
}
impl Operation {
/// Returns the byte representation of the message.
///
/// # Safety
/// This function reads from uninitialized memory!!!
/// Only use if you know what you are doing
unsafe fn as_slice(&self) -> &[u8] {
core::slice::from_raw_parts(self as *const Operation as *const u8, std::mem::size_of::<Operation>())
}
/// Returns a pseudo hash that should uniquely identify the operation.
/// This is needed because `Hash` is not implemented for f64s
///
/// # Safety
/// This function reads from uninitialized memory but the generated value should be fine.
pub fn pseudo_hash(&self) -> u64 {
let mut s = DefaultHasher::new();
unsafe { self.as_slice() }.hash(&mut s);
s.finish()
}
}