Skip to content

Commit 6f46f21

Browse files
authored
Improve the HTTP request nodes and add new related nodes (#2896)
* Improve the network request nodes and add new ones to process data * Use Content-Type: application/octet-stream * Add 'Gamma Correction' node
1 parent 561b671 commit 6f46f21

File tree

6 files changed

+118
-13
lines changed

6 files changed

+118
-13
lines changed

node-graph/gcore/src/color/color.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -835,6 +835,20 @@ impl Color {
835835
[(gamma.red * 255.) as u8, (gamma.green * 255.) as u8, (gamma.blue * 255.) as u8, (gamma.alpha * 255.) as u8]
836836
}
837837

838+
/// Return the all RGB components as a u8 slice, first component is red, followed by green, followed by blue. Use this if the [`Color`] is in linear space.
839+
///
840+
/// # Examples
841+
/// ```
842+
/// use graphene_core::color::Color;
843+
/// let color = Color::from_rgbaf32(0.114, 0.103, 0.98, 0.97).unwrap();
844+
/// // TODO: Add test
845+
/// ```
846+
#[inline(always)]
847+
pub fn to_rgb8_srgb(&self) -> [u8; 3] {
848+
let gamma = self.to_gamma_srgb();
849+
[(gamma.red * 255.) as u8, (gamma.green * 255.) as u8, (gamma.blue * 255.) as u8]
850+
}
851+
838852
// https://www.niwa.nu/2013/05/math-behind-colorspace-conversions-rgb-hsl/
839853
/// Convert a [Color] to a hue, saturation, lightness and alpha (all between 0 and 1)
840854
///

node-graph/gcore/src/logic.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,18 @@ use crate::{Context, Ctx};
1010
use glam::{DAffine2, DVec2};
1111

1212
#[node_macro::node(category("Text"))]
13-
fn to_string<T: std::fmt::Debug>(_: impl Ctx, #[implementations(String, bool, f64, u32, u64, DVec2, VectorDataTable, DAffine2)] value: T) -> String {
13+
fn to_string<T: std::fmt::Debug>(_: impl Ctx, #[implementations(String, bool, f64, u32, u64, DVec2, DAffine2, VectorDataTable)] value: T) -> String {
1414
format!("{:?}", value)
1515
}
1616

17+
#[node_macro::node(category("Text"))]
18+
fn serialize<T: serde::Serialize>(
19+
_: impl Ctx,
20+
#[implementations(String, bool, f64, u32, u64, DVec2, DAffine2, Color, Option<Color>, GraphicGroupTable, VectorDataTable, RasterDataTable<CPU>)] value: T,
21+
) -> String {
22+
serde_json::to_string(&value).unwrap_or_else(|_| "Serialization Error".to_string())
23+
}
24+
1725
#[node_macro::node(category("Text"))]
1826
fn string_concatenate(_: impl Ctx, #[implementations(String)] first: String, second: TextArea) -> String {
1927
first.clone() + &second

node-graph/graster-nodes/src/adjustments.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,26 @@ fn luminance<T: Adjust<Color>>(
6565
input
6666
}
6767

68+
#[node_macro::node(category("Raster"))]
69+
fn gamma_correction<T: Adjust<Color>>(
70+
_: impl Ctx,
71+
#[implementations(
72+
Color,
73+
RasterDataTable<CPU>,
74+
GradientStops,
75+
)]
76+
mut input: T,
77+
#[default(2.2)]
78+
#[range((0.01, 10.))]
79+
#[hard_min(0.0001)]
80+
gamma: f64,
81+
inverse: bool,
82+
) -> T {
83+
let exponent = if inverse { 1. / gamma } else { gamma };
84+
input.adjust(|color| color.gamma(exponent as f32));
85+
input
86+
}
87+
6888
#[node_macro::node(category("Raster: Channels"))]
6989
fn extract_channel<T: Adjust<Color>>(
7090
_: impl Ctx,

node-graph/gstd/src/http.rs

Lines changed: 0 additions & 11 deletions
This file was deleted.

node-graph/gstd/src/lib.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
pub mod any;
2-
pub mod http;
32
pub mod text;
43
#[cfg(feature = "wasm")]
54
pub mod wasm_application_io;

node-graph/gstd/src/wasm_application_io.rs

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,81 @@ async fn create_surface<'a: 'n>(_: impl Ctx, editor: &'a WasmEditorApi) -> Arc<W
5959
// }
6060
// }
6161

62+
#[node_macro::node(category("Web Request"))]
63+
async fn get_request(_: impl Ctx, _primary: (), #[name("URL")] url: String, discard_result: bool) -> String {
64+
#[cfg(target_arch = "wasm32")]
65+
{
66+
if discard_result {
67+
wasm_bindgen_futures::spawn_local(async move {
68+
let _ = reqwest::get(url).await;
69+
});
70+
return String::new();
71+
}
72+
}
73+
#[cfg(not(target_arch = "wasm32"))]
74+
{
75+
#[cfg(feature = "tokio")]
76+
if discard_result {
77+
tokio::spawn(async move {
78+
let _ = reqwest::get(url).await;
79+
});
80+
return String::new();
81+
}
82+
#[cfg(not(feature = "tokio"))]
83+
if discard_result {
84+
return String::new();
85+
}
86+
}
87+
88+
let Ok(response) = reqwest::get(url).await else { return String::new() };
89+
response.text().await.ok().unwrap_or_default()
90+
}
91+
92+
#[node_macro::node(category("Web Request"))]
93+
async fn post_request(_: impl Ctx, _primary: (), #[name("URL")] url: String, body: Vec<u8>, discard_result: bool) -> String {
94+
#[cfg(target_arch = "wasm32")]
95+
{
96+
if discard_result {
97+
wasm_bindgen_futures::spawn_local(async move {
98+
let _ = reqwest::Client::new().post(url).body(body).header("Content-Type", "application/octet-stream").send().await;
99+
});
100+
return String::new();
101+
}
102+
}
103+
#[cfg(not(target_arch = "wasm32"))]
104+
{
105+
#[cfg(feature = "tokio")]
106+
if discard_result {
107+
let url = url.clone();
108+
let body = body.clone();
109+
tokio::spawn(async move {
110+
let _ = reqwest::Client::new().post(url).body(body).header("Content-Type", "application/octet-stream").send().await;
111+
});
112+
return String::new();
113+
}
114+
#[cfg(not(feature = "tokio"))]
115+
if discard_result {
116+
return String::new();
117+
}
118+
}
119+
120+
let Ok(response) = reqwest::Client::new().post(url).body(body).header("Content-Type", "application/octet-stream").send().await else {
121+
return String::new();
122+
};
123+
response.text().await.ok().unwrap_or_default()
124+
}
125+
126+
#[node_macro::node(category("Web Request"), name("String to Bytes"))]
127+
fn string_to_bytes(_: impl Ctx, string: String) -> Vec<u8> {
128+
string.into_bytes()
129+
}
130+
131+
#[node_macro::node(category("Web Request"), name("Image to Bytes"))]
132+
fn image_to_bytes(_: impl Ctx, image: RasterDataTable<CPU>) -> Vec<u8> {
133+
let Some(image) = image.instance_ref_iter().next() else { return vec![] };
134+
image.instance.data.iter().flat_map(|color| color.to_rgb8_srgb().into_iter()).collect::<Vec<u8>>()
135+
}
136+
62137
#[node_macro::node(category("Web Request"))]
63138
async fn load_resource<'a: 'n>(_: impl Ctx, _primary: (), #[scope("editor-api")] editor: &'a WasmEditorApi, #[name("URL")] url: String) -> Arc<[u8]> {
64139
let Some(api) = editor.application_io.as_ref() else {

0 commit comments

Comments
 (0)