Use the session color interpretation in headless 2D previews

This commit is contained in:
chasse20 committed 2026-11-23 16:33:50 -05:00
1 parent 51602b7ee0
commit cbdf641bb0
7 files changed
+28 -8

No files matched your search

+1 -1
View File
@@ -284,7 +284,7 @@ impl Headless {
let (w, h) = (options.width.unwrap_or(image.width()).clamp(16, maximum_side), options.height.unwrap_or(image.height()).clamp(16, maximum_side));
let image = image.resize(w, h, true);
Ok((w, h, image.to_rgba8_display()))
Ok((w, h, materialcraft_engine::graph_pixels::rgba8(&image, self.session.view2d.srgb)))
}
fn render_3d(&mut self, o: &RenderOptions, max: u32) -> (u32, u32, Vec<u8>) {
+16
View File
@@ -2,6 +2,22 @@ use materialcraft_automation::headless::{Filesystem, Headless, RenderOptions};
use materialcraft_engine::new_session;
use serde_json::json;
#[test]
fn headless_graph_previews_use_the_selected_display_encoding() {
let mut headless = Headless::new(new_session(), Filesystem::Denied);
headless.session.preview_size = Some(16);
let source = headless.command_run("graph.node.add", json!({"def": "uniform", "params": {"color": [0.2, 0.4, 0.8, 1.0]}})).unwrap();
headless.command_run("view.2d", json!({"node": source["id"]})).unwrap();
let options = RenderOptions { view: Some("2d".into()), width: Some(16), height: Some(16), ..Default::default() };
for (srgb, expected) in [(true, [51, 102, 204, 255]), (false, [124, 170, 231, 255])] {
headless.command_run("view.2d", json!({"srgb": srgb})).unwrap();
let png = headless.render_png(&options).unwrap();
let image = image::load_from_memory(&png.png).unwrap().to_rgba8();
assert_eq!(image.dimensions(), (16, 16));
assert!(image.pixels().all(|pixel| pixel.0 == expected), "{srgb}: {:?}", image.get_pixel(0, 0));
}
}
#[test]
fn rejected_view_command_preserves_the_rendered_scene() {
let mut h = Headless::new(new_session(), Filesystem::Denied);
@@ -1,16 +1,19 @@
//! Display levels of cooked graph images and node thumbnails.
use materialcraft_engine::image::{Image, linear_to_srgb};
use crate::image::{Image, linear_to_srgb};
pub(crate) fn stored_byte(value: f32) -> u8 {
/// Quantize a stored channel, including alpha, to its displayed byte.
pub fn stored_byte(value: f32) -> u8 {
(value.clamp(0.0, 1.0) * 255.0 + 0.5) as u8
}
pub(crate) fn display_byte(value: f32, srgb: bool) -> u8 {
/// Interpret a graph channel as sRGB or linear light before displaying it.
pub fn display_byte(value: f32, srgb: bool) -> u8 {
stored_byte(if srgb { value } else { linear_to_srgb(value) })
}
pub(crate) fn rgba8(image: &Image, srgb: bool) -> Vec<u8> {
/// Expand the stored graph pixels into display RGBA, preserving alpha.
pub fn rgba8(image: &Image, srgb: bool) -> Vec<u8> {
let mut pixels = Vec::with_capacity(image.data().len() / image.channels() * 4);
if let Some(gray) = image.gray_data() {
for &value in gray {
+1
View File
@@ -68,6 +68,7 @@ pub use history_prefs::HistoryPrefs;
pub use library_cache_prefs::LibraryCachePrefs;
pub mod graph_edit;
pub mod graph_grid;
pub mod graph_pixels;
pub mod inspect;
pub mod material_cmds;
pub mod node_cmds;
+1 -1
View File
@@ -11,7 +11,7 @@ pub mod fonts;
pub mod function;
pub mod graph;
mod graph_drag_payload;
mod graph_pixels;
use materialcraft_engine::graph_pixels;
mod history;
pub mod i18n;
pub mod levels_editor;
+1 -1
View File
@@ -77,7 +77,7 @@ Maximum U16 error is zero throughout the three-setting desktop column. The deskt
Display the Base Color output of Bricks 01 at 1:1 with the default sRGB interpretation. With seed 0 and a 64² parent, all 4,096 pixels captured from the reference viewport match its raw U8 RGB output, maximum error 0. The old MaterialCraft viewport applied a linear-light to sRGB conversion to those already encoded numbers. The same conversion brightened graph thumbnails and the color sampler.
Use a uniform color with stored RGB (.2,.4,.8) and alpha 1. The reference viewport and node thumbnail show (51,102,204). The old MaterialCraft preview showed (124,170,231). The corrected MaterialCraft offscreen viewport shows (51,102,204). For the full byte scale, make 86 original opaque swatches whose three channels cover levels 0–255. Compare the rendered reference colors and the actual MaterialCraft display function. With the default encoded interpretation, 86/86 swatches and 258/258 channels match, maximum error 0. Reading the same numbers as linear light gives 14/86 exact swatches and 132/258 exact channels, maximum error 1. In seven further storage controls across U8, U16, F16 and F32, both interpretations stay within one displayed byte. The display choice leaves stored channel readouts and alpha intact.
Use a uniform color with stored RGB (.2,.4,.8) and alpha 1. The reference viewport and node thumbnail show (51,102,204). The old MaterialCraft preview showed (124,170,231). The corrected MaterialCraft offscreen viewport shows (51,102,204). For the full byte scale, make 86 original opaque swatches whose three channels cover levels 0–255. Compare the rendered reference colors and the actual MaterialCraft display function. With the default encoded interpretation, 86/86 swatches and 258/258 channels match, maximum error 0. Reading the same numbers as linear light gives 14/86 exact swatches and 132/258 exact channels, maximum error 1. In seven further storage controls across U8, U16, F16 and F32, both interpretations stay within one displayed byte. The display choice leaves stored channel readouts and alpha intact. The headless 2D PNG uses the same session display setting. On a 16 × 16 uniform RGBA buffer, all 256 pixels are (51,102,204,255) with the encoded interpretation and all 256 are (124,170,231,255) with linear-light interpretation. Both PNG comparisons have maximum byte error 0.
| Bricks 01 Base Color | Reference mean RGB | Old mean RGB | Corrected mean RGB | Old exact pixels / max error | Corrected exact pixels / max error |
|---|---|---|---|---:|---:|
+1 -1
View File
@@ -41,7 +41,7 @@ Nested Curvature, Curvature Smooth and Normal Sobel each match 2,304/2,304 origi
Ordinary SBS saves preserve graph seed inheritance across 22 nested graphs. The original and reopened random fields each match 22,528/22,528 pixels. BNW Spots 1 and Clouds 1 and 2 each match 9,216/9,216 pixels across four output formats in the two nested settings. [Seed settings, whole graphs and renderer repeats](parity-fixes.md#graph-random-seed-inheritance-on-save-and-in-nested-graphs)
The Bricks 01 material provides a separate 2D display check. The corrected preview matches all 258 tested encoded RGB channels. At 64², its base-color mean is (155,122,98) against (152,120,98) in the reference. The cooked material still differs. [Color swatches and material measurements](parity-fixes.md#stored-graph-color-in-the-2d-view)
The Bricks 01 material provides a separate 2D display check. The corrected preview matches all 258 tested encoded RGB channels. The headless 2D PNG also matches every pixel of a 16² uniform-color control in both display modes. At 64², its base-color mean is (155,122,98) against (152,120,98) in the reference. The cooked material still differs. [Color swatches and material measurements](parity-fixes.md#stored-graph-color-in-the-2d-view)
Tile Generator has reopened. The 4 × 14 grayscale grid matches at seeds 0 and 17 until luminance variation is enabled. The isolated setting matches 0/4,096 at each seed. [Bricks 01 tile and noise stages](parity-fixes.md#tile-luminance-variation-in-bricks-01)