mirror of
https://github.com/storytold/materialcraft.git
synced 2026-11-28 17:19:17 +01:00
Convert normalized RGB bitmaps to gray at the first filtered level
This commit is contained in:
1 parent
66f6ecd8db
commit
cd44602372
4 files changed
+60
-5
No files matched your search
@@ -17,15 +17,15 @@ pub(super) fn definition() -> NodeDefinition {
|
||||
|
||||
fn bitmap(context: &CookContext) -> CookResult {
|
||||
let name = context.text("resource");
|
||||
let color = context.b("color_mode");
|
||||
let image = match (context.resource)(&craft_texture::resource_key(name, context.i("colour_space") == 1)) {
|
||||
Some(image)
|
||||
if image.integer_sampling()
|
||||
&& (image.kind() == Kind::Gray || context.b("color_mode"))
|
||||
&& (image.width(), image.height()) != (context.w, context.h)
|
||||
&& ((image.width(), image.height()) != (context.w, context.h) || (!color && image.kind() == Kind::Color))
|
||||
&& [image.width(), image.height(), context.w, context.h].into_iter().all(u32::is_power_of_two) =>
|
||||
{
|
||||
// Bitmap filtering clamps at the resource edge and stores each level at the node precision.
|
||||
let kind = image.kind();
|
||||
// The first filtered level converts RGB to its channel mean before storing it.
|
||||
let kind = if color { image.kind() } else { Kind::Gray };
|
||||
crate::resize_bound_image(image, (context.w, context.h), kind, OutputFormat::U16, context.format, [false; 2])
|
||||
}
|
||||
Some(image) => image.resize(context.w, context.h, context.tile),
|
||||
@@ -36,5 +36,5 @@ fn bitmap(context: &CookContext) -> CookResult {
|
||||
None => return Err(format!("no bitmap resource `{name}`")),
|
||||
};
|
||||
|
||||
Ok(vec![if context.b("color_mode") { image.to_color() } else { image.to_gray() }])
|
||||
Ok(vec![if color { image.to_color() } else { image.to_gray() }])
|
||||
}
|
||||
@@ -144,6 +144,42 @@ fn linear_color_bitmap_filters_each_channel_at_stored_precision() {
|
||||
assert_eq!(levels, [[6554, 30969, 44718, 45746], [32382, 31354, 40221, 28913], [21074, 46774, 47417, 19661]]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn bitmap_converts_normalized_rgb_to_gray_on_the_first_filter_pass() {
|
||||
use materialcraft_core::OutputFormat;
|
||||
|
||||
let definition = find("bitmap").unwrap();
|
||||
let params = resolve_params(
|
||||
definition,
|
||||
&BTreeMap::from([("colour_space".into(), Value::Enum(1)), ("color_mode".into(), Value::Bool(false)), ("resource".into(), Value::Text("field".into()))]),
|
||||
);
|
||||
let source = Image::generate(32, 64, Kind::Color, |x, y| {
|
||||
[(583, 191, 371, 3229), (47, 313, 59, 4511), (31, 693, 41, 8943), (997, 811, 97, 9043)]
|
||||
.map(|(a, b, c, d)| ((a * x + b * y + c * x * y + d) % 65536) as f32 / 65535.0)
|
||||
})
|
||||
.with_integer_sampling(true);
|
||||
for (format, size, positions, expected) in [
|
||||
(OutputFormat::U16, [16, 16], [(0, 0), (3, 7), (15, 15)], [6388, 27024, 28209].map(|v| v as f32 / 65535.0)),
|
||||
(OutputFormat::F32, [32, 64], [(0, 0), (3, 7), (31, 63)], [0x3dadc8af, 0x3e4060c0, 0x3e80ab2c].map(f32::from_bits)),
|
||||
] {
|
||||
let context = CookContext {
|
||||
input_formats: &[],
|
||||
format,
|
||||
w: size[0],
|
||||
h: size[1],
|
||||
seed: 520885,
|
||||
tile: true,
|
||||
inputs: &[],
|
||||
params: ¶ms,
|
||||
resource: &|_| Some(source.clone()),
|
||||
vars: &BTreeMap::new(),
|
||||
};
|
||||
let result = (definition.cook)(&context).unwrap();
|
||||
let stored = crate::output_precision::apply(result[0].clone(), format);
|
||||
assert_eq!(positions.map(|(x, y)| stored.get_gray(x, y, false)), expected);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn nearest_transform_mip_keeps_the_last_column_of_an_odd_width_input() {
|
||||
let definition = find("transformation").unwrap();
|
||||
|
||||
@@ -401,6 +401,23 @@ The 64 original renders matched **108,544/108,544** pixels. The 24 saved renders
|
||||
|
||||
The default interpretation of an untagged color source, color-to-grayscale conversion and odd-sized RGBA sources have separate comparisons. For example, the 29 × 47 linear byte field still differed in the six-resolution series.
|
||||
|
||||
### Grayscale conversion on the first bitmap filter pass
|
||||
|
||||
A normalized RGBA bitmap with grayscale output filters RGB at source precision on the first resize pass. It takes the equally weighted mean of the three sampled channels, then stores the gray value at the requested precision. Later passes filter that grayscale image. A same-size render takes the same channel mean directly. Alpha varies independently in these controls.
|
||||
|
||||
Generate the modular RGBA word field at 32 × 64. For the byte field use the same four coefficient tuples modulo 256 at 64 × 32. Choose linear input and grayscale output. Render 32 × 64, 16², 16 × 64, 64 × 16, 64² and 32 × 128 in U8, U16, F16 and F32, with seed 520884 + size index. For the word field at 16², the first pixel stores 6388 in U16. A same-size F32 render starts with the word `3dadc8af`. [Field coefficients](#stored-levels-of-linear-color-bitmap-resources)
|
||||
|
||||
| Output | Renders | Exact before / total · maximum | Exact after / total · maximum |
|
||||
|---|---:|---:|---:|
|
||||
| LUM16 | 12 | 2/25,088 · 24283 | 25,088/25,088 · 0 |
|
||||
| LUM16F | 12 | 46/25,088 · 0.370605469 | 25,088/25,088 · 0 |
|
||||
| LUM32F | 12 | 0/25,088 · 0.370539784 | 25,088/25,088 · 0 |
|
||||
| LUM8 | 12 | 453/25,088 · 94 | 25,088/25,088 · 0 |
|
||||
|
||||
Across all 48 renders, **100,352/100,352** pixels match, maximum zero. Converting the source to stored gray before filtering matches 52,263/100,352. Converting at the last level matches 85,196/100,352. The 20 saved renders matched **47,616/47,616** across applications and reproduced the original buffers in both, maximum zero. The node regression reproduces the earlier error. The release node suite, formatting, strict workspace Clippy, dependency layers and WebAssembly passed.
|
||||
|
||||
These measurements cover power-of-two dimensions and an explicit linear data read. The separate comparisons for odd-sized color resources and file-default color interpretation remain open for this evaluator.
|
||||
|
||||
## 2026-11-20 root comparisons
|
||||
|
||||
### Hue, saturation, lightness and Replace Color
|
||||
|
||||
@@ -49,6 +49,8 @@ Odd-sized grayscale bitmap resources match 116,736/116,736 pixels directly and 9
|
||||
|
||||
Linear RGBA bitmap resources match 108,544/108,544 pixels on two procedural fields. [Exact counts by format and saved-package results](parity-fixes.md#stored-levels-of-linear-color-bitmap-resources)
|
||||
|
||||
Grayscale output from normalized RGB bitmaps matched 100,352/100,352 pixels across 48 renders. [Measurements and saved packages](parity-fixes.md#grayscale-conversion-on-the-first-bitmap-filter-pass)
|
||||
|
||||
Enlarged RGBA pattern backgrounds now match 71,680/71,680 pixels across 280 independent renders. The earlier source-storage tests still differ at 121 grayscale zero signs. [Background sign measurements](parity-fixes.md#signs-of-zero-in-enlarged-color-backgrounds)
|
||||
|
||||
The 32²-to-16² signed threshold controls match 2,048/2,048 pixels. [Floating texture filter measurements](parity-fixes.md#signed-zero-in-floating-point-texture-filtering)
|
||||
|
||||
Reference in new issue
Block a user