resolved conflicts

This commit is contained in:
0SlowPoke0 2025-08-20 12:45:53 +05:30
parent 3603426aee
commit 1338310d9f
256 changed files with 5951 additions and 20321 deletions

View file

@ -13,13 +13,12 @@ std = [
"dep:dyn-any",
"dep:image",
"dep:ndarray",
"dep:bezier-rs",
"dep:rand",
"dep:rand_chacha",
"dep:fastnoise-lite",
"dep:serde",
"dep:specta",
"dep:glam"
"dep:glam",
]
[dependencies]
@ -40,11 +39,11 @@ glam = { workspace = true, optional = true }
specta = { workspace = true, optional = true }
image = { workspace = true, optional = true }
ndarray = { workspace = true, optional = true }
bezier-rs = { workspace = true, optional = true }
rand = { workspace = true, optional = true }
rand_chacha = { workspace = true, optional = true }
fastnoise-lite = { workspace = true, optional = true }
serde = { workspace = true, optional = true }
kurbo = { workspace = true }
[dev-dependencies]
tokio = { workspace = true }

View file

@ -8,13 +8,6 @@ impl Adjust<Color> for Color {
*self = map_fn(self);
}
}
impl Adjust<Color> for Option<Color> {
fn adjust(&mut self, map_fn: impl Fn(&Color) -> Color) {
if let Some(color) = self {
*color = map_fn(color)
}
}
}
#[cfg(feature = "std")]
mod adjust_std {
@ -23,13 +16,6 @@ mod adjust_std {
use graphene_core::raster_types::{CPU, Raster};
use graphene_core::table::Table;
impl Adjust<Color> for GradientStops {
fn adjust(&mut self, map_fn: impl Fn(&Color) -> Color) {
for (_, color) in self.iter_mut() {
*color = map_fn(color);
}
}
}
impl Adjust<Color> for Table<Raster<CPU>> {
fn adjust(&mut self, map_fn: impl Fn(&Color) -> Color) {
for row in self.iter_mut() {
@ -39,4 +25,25 @@ mod adjust_std {
}
}
}
impl Adjust<Color> for Table<Color> {
fn adjust(&mut self, map_fn: impl Fn(&Color) -> Color) {
for row in self.iter_mut() {
*row.element = map_fn(row.element);
}
}
}
impl Adjust<Color> for Table<GradientStops> {
fn adjust(&mut self, map_fn: impl Fn(&Color) -> Color) {
for row in self.iter_mut() {
row.element.adjust(&map_fn);
}
}
}
impl Adjust<Color> for GradientStops {
fn adjust(&mut self, map_fn: impl Fn(&Color) -> Color) {
for (_, color) in self.iter_mut() {
*color = map_fn(color);
}
}
}
}

View file

@ -44,8 +44,9 @@ pub enum LuminanceCalculation {
fn luminance<T: Adjust<Color>>(
_: impl Ctx,
#[implementations(
Color,
Table<Raster<CPU>>,
Table<Color>,
Table<GradientStops>,
GradientStops,
)]
mut input: T,
@ -68,8 +69,9 @@ fn luminance<T: Adjust<Color>>(
fn gamma_correction<T: Adjust<Color>>(
_: impl Ctx,
#[implementations(
Color,
Table<Raster<CPU>>,
Table<Color>,
Table<GradientStops>,
GradientStops,
)]
mut input: T,
@ -88,8 +90,9 @@ fn gamma_correction<T: Adjust<Color>>(
fn extract_channel<T: Adjust<Color>>(
_: impl Ctx,
#[implementations(
Color,
Table<Raster<CPU>>,
Table<Color>,
Table<GradientStops>,
GradientStops,
)]
mut input: T,
@ -111,8 +114,9 @@ fn extract_channel<T: Adjust<Color>>(
fn make_opaque<T: Adjust<Color>>(
_: impl Ctx,
#[implementations(
Color,
Table<Raster<CPU>>,
Table<Color>,
Table<GradientStops>,
GradientStops,
)]
mut input: T,
@ -136,8 +140,9 @@ fn make_opaque<T: Adjust<Color>>(
fn brightness_contrast<T: Adjust<Color>>(
_: impl Ctx,
#[implementations(
Color,
Table<Raster<CPU>>,
Table<Color>,
Table<GradientStops>,
GradientStops,
)]
mut input: T,
@ -225,8 +230,9 @@ fn brightness_contrast<T: Adjust<Color>>(
fn levels<T: Adjust<Color>>(
_: impl Ctx,
#[implementations(
Color,
Table<Raster<CPU>>,
Table<Color>,
Table<GradientStops>,
GradientStops,
)]
mut image: T,
@ -292,12 +298,13 @@ fn levels<T: Adjust<Color>>(
async fn black_and_white<T: Adjust<Color>>(
_: impl Ctx,
#[implementations(
Color,
Table<Raster<CPU>>,
Table<Color>,
Table<GradientStops>,
GradientStops,
)]
mut image: T,
#[default(Color::BLACK)] tint: Color,
#[default(Color::BLACK)] tint: Table<Color>,
#[default(40.)]
#[range((-200., 300.))]
reds: Percentage,
@ -317,6 +324,9 @@ async fn black_and_white<T: Adjust<Color>>(
#[range((-200., 300.))]
magentas: Percentage,
) -> T {
let tint: Option<Color> = tint.into();
let tint = tint.unwrap_or(Color::BLACK);
image.adjust(|color| {
let color = color.to_gamma_srgb();
@ -364,8 +374,9 @@ async fn black_and_white<T: Adjust<Color>>(
async fn hue_saturation<T: Adjust<Color>>(
_: impl Ctx,
#[implementations(
Color,
Table<Raster<CPU>>,
Table<Color>,
Table<GradientStops>,
GradientStops,
)]
mut input: T,
@ -398,8 +409,9 @@ async fn hue_saturation<T: Adjust<Color>>(
async fn invert<T: Adjust<Color>>(
_: impl Ctx,
#[implementations(
Color,
Table<Raster<CPU>>,
Table<Color>,
Table<GradientStops>,
GradientStops,
)]
mut input: T,
@ -420,8 +432,9 @@ async fn invert<T: Adjust<Color>>(
async fn threshold<T: Adjust<Color>>(
_: impl Ctx,
#[implementations(
Color,
Table<Raster<CPU>>,
Table<Color>,
Table<GradientStops>,
GradientStops,
)]
mut image: T,
@ -465,8 +478,9 @@ async fn threshold<T: Adjust<Color>>(
async fn vibrance<T: Adjust<Color>>(
_: impl Ctx,
#[implementations(
Color,
Table<Raster<CPU>>,
Table<Color>,
Table<GradientStops>,
GradientStops,
)]
mut image: T,
@ -630,8 +644,9 @@ pub enum DomainWarpType {
async fn channel_mixer<T: Adjust<Color>>(
_: impl Ctx,
#[implementations(
Color,
Table<Raster<CPU>>,
Table<Color>,
Table<GradientStops>,
GradientStops,
)]
mut image: T,
@ -758,8 +773,9 @@ pub enum SelectiveColorChoice {
async fn selective_color<T: Adjust<Color>>(
_: impl Ctx,
#[implementations(
Color,
Table<Raster<CPU>>,
Table<Color>,
Table<GradientStops>,
GradientStops,
)]
mut image: T,
@ -900,8 +916,9 @@ async fn selective_color<T: Adjust<Color>>(
async fn posterize<T: Adjust<Color>>(
_: impl Ctx,
#[implementations(
Color,
Table<Raster<CPU>>,
Table<Color>,
Table<GradientStops>,
GradientStops,
)]
mut input: T,
@ -933,8 +950,9 @@ async fn posterize<T: Adjust<Color>>(
async fn exposure<T: Adjust<Color>>(
_: impl Ctx,
#[implementations(
Color,
Table<Raster<CPU>>,
Table<Color>,
Table<GradientStops>,
GradientStops,
)]
mut input: T,

View file

@ -17,15 +17,6 @@ impl Blend<Color> for Color {
blend_fn(*self, *under)
}
}
impl Blend<Color> for Option<Color> {
fn blend(&self, under: &Self, blend_fn: impl Fn(Color, Color) -> Color) -> Self {
match (self, under) {
(Some(a), Some(b)) => Some(blend_fn(*a, *b)),
(a, None) => *a,
(None, b) => *b,
}
}
}
#[cfg(feature = "std")]
mod blend_std {
@ -51,6 +42,24 @@ mod blend_std {
result_table
}
}
impl Blend<Color> for Table<Color> {
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.iter_mut().zip(under.iter()) {
*over.element = blend_fn(*over.element, *under.element);
}
result_table
}
}
impl Blend<Color> for Table<GradientStops> {
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.iter_mut().zip(under.iter()) {
*over.element = over.element.blend(under.element, &blend_fn);
}
result_table
}
}
impl Blend<Color> for GradientStops {
fn blend(&self, under: &Self, blend_fn: impl Fn(Color, Color) -> Color) -> Self {
let mut combined_stops = self.iter().map(|(position, _)| position).chain(under.iter().map(|(position, _)| position)).collect::<Vec<_>>();
@ -126,15 +135,17 @@ pub fn apply_blend_mode(foreground: Color, background: Color, blend_mode: BlendM
async fn blend<T: Blend<Color> + Send>(
_: impl Ctx,
#[implementations(
Color,
Table<Raster<CPU>>,
Table<Color>,
Table<GradientStops>,
GradientStops,
)]
over: T,
#[expose]
#[implementations(
Color,
Table<Raster<CPU>>,
Table<Color>,
Table<GradientStops>,
GradientStops,
)]
under: T,
@ -148,17 +159,21 @@ async fn blend<T: Blend<Color> + Send>(
fn color_overlay<T: Adjust<Color>>(
_: impl Ctx,
#[implementations(
Color,
Table<Raster<CPU>>,
Table<Color>,
Table<GradientStops>,
GradientStops,
)]
mut image: T,
#[default(Color::BLACK)] color: Color,
#[default(Color::BLACK)] color: Table<Color>,
blend_mode: BlendMode,
#[default(100.)] opacity: Percentage,
) -> T {
let opacity = (opacity as f32 / 100.).clamp(0., 1.);
let color: Option<Color> = color.into();
let color = color.unwrap_or(Color::BLACK);
image.adjust(|pixel| {
let image = pixel.map_rgb(|channel| channel * (1. - opacity));
@ -171,18 +186,6 @@ fn color_overlay<T: Adjust<Color>>(
image
}
#[cfg(feature = "std")]
#[node_macro::node(category(""), skip_impl)]
fn blend_color_pair<BlendModeNode, OpacityNode>(input: (Color, Color), blend_mode: &'n BlendModeNode, opacity: &'n OpacityNode) -> Color
where
BlendModeNode: graphene_core::Node<'n, (), Output = BlendMode> + 'n,
OpacityNode: graphene_core::Node<'n, (), Output = Percentage> + 'n,
{
let blend_mode = blend_mode.eval(());
let opacity = opacity.eval(());
blend_colors(input.0, input.1, blend_mode, opacity / 100.)
}
#[cfg(all(feature = "std", test))]
mod test {
use graphene_core::blending::BlendMode;
@ -202,7 +205,13 @@ mod test {
// 100% of the output should come from the multiplied value
let opacity = 100_f64;
let result = super::color_overlay((), Table::new_from_element(Raster::new_cpu(image.clone())), overlay_color, BlendMode::Multiply, opacity);
let result = super::color_overlay(
(),
Table::new_from_element(Raster::new_cpu(image.clone())),
Table::new_from_element(overlay_color),
BlendMode::Multiply,
opacity,
);
let result = result.iter().next().unwrap().element;
// The output should just be the original green and alpha channels (as we multiply them by 1 and other channels by 0)

View file

@ -1,9 +1,8 @@
//! requires bezier-rs
use crate::curve::{Curve, CurveManipulatorGroup, ValueMapperNode};
use bezier_rs::{Bezier, TValue};
use graphene_core::color::{Channel, Linear};
use graphene_core::context::Ctx;
use graphene_core::vector::algorithms::bezpath_algorithms::pathseg_find_tvalues_for_x;
use kurbo::{CubicBez, ParamCurve, PathSeg, Point};
const WINDOW_SIZE: usize = 1024;
@ -18,7 +17,7 @@ fn generate_curves<C: Channel + Linear>(_: impl Ctx, curve: Curve, #[implementat
for sample in curve.manipulator_groups.iter().chain(std::iter::once(&end)) {
let [x0, y0, x1, y1, x2, y2, x3, y3] = [pos[0], pos[1], param[0], param[1], sample.handles[0][0], sample.handles[0][1], sample.anchor[0], sample.anchor[1]].map(f64::from);
let bezier = Bezier::from_cubic_coordinates(x0, y0, x1, y1, x2, y2, x3, y3);
let segment = PathSeg::Cubic(CubicBez::new(Point::new(x0, y0), Point::new(x1, y1), Point::new(x2, y2), Point::new(x3, y3)));
let [left, right] = [pos[0], sample.anchor[0]].map(|c| c.clamp(0., 1.));
let lut_index_left: usize = (left * (lut.len() - 1) as f32).floor() as _;
@ -30,10 +29,10 @@ fn generate_curves<C: Channel + Linear>(_: impl Ctx, curve: Curve, #[implementat
} else if x >= x3 {
y3
} else {
bezier.find_tvalues_for_x(x)
pathseg_find_tvalues_for_x(segment, x)
.next()
.map(|t| bezier.evaluate(TValue::Parametric(t.clamp(0., 1.))).y)
// Fall back to a very bad approximation if Bezier-rs fails
.map(|t| segment.eval(t.clamp(0., 1.)).y)
// Fall back to a very bad approximation if the above fails
.unwrap_or_else(|| (x - x0) / (x3 - x0) * (y3 - y0) + y0)
};
lut[index] = C::from_f64(y);

View file

@ -13,8 +13,9 @@ use graphene_core::{Color, Ctx};
async fn gradient_map<T: Adjust<Color>>(
_: impl Ctx,
#[implementations(
Color,
Table<Raster<CPU>>,
Table<Color>,
Table<GradientStops>,
GradientStops,
)]
mut image: T,

View file

@ -1,7 +1,7 @@
use graphene_core::color::Color;
use graphene_core::context::Ctx;
use graphene_core::raster_types::{CPU, Raster};
use graphene_core::table::Table;
use graphene_core::table::{Table, TableRow};
#[node_macro::node(category("Color"))]
async fn image_color_palette(
@ -10,13 +10,13 @@ async fn image_color_palette(
#[hard_min(1.)]
#[soft_max(28.)]
max_size: u32,
) -> Vec<Color> {
) -> Table<Color> {
const GRID: f32 = 3.;
let bins = GRID * GRID * GRID;
let mut histogram: Vec<usize> = vec![0; (bins + 1.) as usize];
let mut colors: Vec<Vec<Color>> = vec![vec![]; (bins + 1.) as usize];
let mut histogram = vec![0; (bins + 1.) as usize];
let mut color_bins = vec![Vec::new(); (bins + 1.) as usize];
for row in image.iter() {
for pixel in row.element.data.iter() {
@ -27,40 +27,38 @@ async fn image_color_palette(
let bin = (r * GRID + g * GRID + b * GRID) as usize;
histogram[bin] += 1;
colors[bin].push(pixel.to_gamma_srgb());
color_bins[bin].push(pixel.to_gamma_srgb());
}
}
let shorted = histogram.iter().enumerate().filter(|&(_, &count)| count > 0).map(|(i, _)| i).collect::<Vec<usize>>();
let mut palette = vec![];
shorted
.iter()
.take(max_size as usize)
.flat_map(|&i| {
let list = &color_bins[i];
for i in shorted.iter().take(max_size as usize) {
let list = colors[*i].clone();
let mut r = 0.;
let mut g = 0.;
let mut b = 0.;
let mut a = 0.;
let mut r = 0.;
let mut g = 0.;
let mut b = 0.;
let mut a = 0.;
for color in list.iter() {
r += color.r();
g += color.g();
b += color.b();
a += color.a();
}
for color in list.iter() {
r += color.r();
g += color.g();
b += color.b();
a += color.a();
}
r /= list.len() as f32;
g /= list.len() as f32;
b /= list.len() as f32;
a /= list.len() as f32;
r /= list.len() as f32;
g /= list.len() as f32;
b /= list.len() as f32;
a /= list.len() as f32;
let color = Color::from_rgbaf32(r, g, b, a).unwrap();
palette.push(color);
}
palette
Color::from_rgbaf32(r, g, b, a).map(TableRow::new_from_element).into_iter()
})
.collect()
}
#[cfg(test)]
@ -81,6 +79,6 @@ mod test {
})),
1,
);
assert_eq!(futures::executor::block_on(result), [Color::from_rgbaf32(0., 0., 0., 1.).unwrap()]);
assert_eq!(futures::executor::block_on(result), Table::new_from_element(Color::from_rgbaf32(0., 0., 0., 1.).unwrap()));
}
}

View file

@ -246,7 +246,7 @@ pub fn extend_image_to_bounds(_: impl Ctx, image: Table<Raster<CPU>>, bounds: DA
let image_data = &row.element.data;
let (image_width, image_height) = (row.element.width, row.element.height);
if image_width == 0 || image_height == 0 {
return empty_image((), bounds, Color::TRANSPARENT).into_iter().next().unwrap();
return empty_image((), bounds, Table::new_from_element(Color::TRANSPARENT)).into_iter().next().unwrap();
}
let orig_image_scale = DVec2::new(image_width as f64, image_height as f64);
@ -280,11 +280,12 @@ pub fn extend_image_to_bounds(_: impl Ctx, image: Table<Raster<CPU>>, bounds: DA
}
#[node_macro::node(category("Debug: Raster"))]
pub fn empty_image(_: impl Ctx, transform: DAffine2, color: Color) -> Table<Raster<CPU>> {
pub fn empty_image(_: impl Ctx, transform: DAffine2, color: Table<Color>) -> Table<Raster<CPU>> {
let width = transform.transform_vector2(DVec2::new(1., 0.)).length() as u32;
let height = transform.transform_vector2(DVec2::new(0., 1.)).length() as u32;
let image = Image::new(width, height, color);
let color: Option<Color> = color.into();
let image = Image::new(width, height, color.unwrap_or(Color::WHITE));
let mut result_table = Table::new_from_element(Raster::new_cpu(image));
let row = result_table.get_mut(0).unwrap();