New node: Pixel Noise (#1267)

Add Pixel Noise Node

Currently only White Noise is implemented, but the Code is written so that other's can be added easily
This commit is contained in:
isiko
2023-08-19 20:30:03 +02:00
committed by GitHub
parent 185106132d
commit a566331f1c
8 changed files with 117 additions and 3 deletions

View File

@@ -24,6 +24,8 @@ imaginate = ["image/png", "base64", "js-sys", "web-sys", "wasm-bindgen-futures"]
wayland = []
[dependencies]
rand = { version = "0.8.5", features = ["alloc", "small_rng"], default-features = false}
rand_chacha = { version = "0.3.1", default-features = false }
autoquant = { git = "https://github.com/truedoctor/autoquant", optional = true, features = [
"fitting",
] }

View File

@@ -2,7 +2,7 @@ use dyn_any::{DynAny, StaticType};
use glam::{DAffine2, DVec2};
use graph_craft::imaginate_input::{ImaginateController, ImaginateMaskStartingFill, ImaginateSamplingMethod};
use graph_craft::proto::DynFuture;
use graphene_core::raster::{Alpha, BlendMode, BlendNode, Image, ImageFrame, Linear, LinearChannel, Luminance, Pixel, RGBMut, Raster, RasterMut, RedGreenBlue, Sample};
use graphene_core::raster::{Alpha, BlendMode, BlendNode, Image, ImageFrame, Linear, LinearChannel, Luminance, NoiseType, Pixel, RGBMut, Raster, RasterMut, RedGreenBlue, Sample};
use graphene_core::transform::Transform;
use crate::wasm_application_io::WasmEditorApi;
@@ -16,6 +16,9 @@ use std::hash::Hash;
use std::marker::PhantomData;
use std::path::Path;
use rand::prelude::*;
use rand_chacha::ChaCha8Rng;
#[derive(Debug, DynAny)]
pub enum Error {
IO(std::io::Error),
@@ -509,6 +512,33 @@ pub struct ImageFrameNode<P, Transform> {
fn image_frame<_P: Pixel>(image: Image<_P>, transform: DAffine2) -> graphene_core::raster::ImageFrame<_P> {
graphene_core::raster::ImageFrame { image, transform }
}
#[derive(Debug, Clone, Copy)]
pub struct PixelNoiseNode<Height, Seed, NoiseType> {
height: Height,
seed: Seed,
noise_type: NoiseType,
}
#[node_macro::node_fn(PixelNoiseNode)]
fn pixel_noise(width: u32, height: u32, seed: u32, noise_type: NoiseType) -> graphene_core::raster::ImageFrame<Color> {
let mut rng = ChaCha8Rng::seed_from_u64(seed as u64);
let mut image = Image::new(width, height, Color::from_luminance(0.5));
for y in 0..height {
for x in 0..width {
let pixel = image.get_pixel_mut(x, y).unwrap();
let luminance = match noise_type {
NoiseType::WhiteNoise => rng.gen_range(0.0..1.0) as f32,
};
*pixel = Color::from_luminance(luminance);
}
}
ImageFrame::<Color> {
image,
transform: DAffine2::from_scale(DVec2::new(width as f64, height as f64)),
}
}
#[cfg(test)]
mod test {