mirror of
https://github.com/storytold/photocraft.git
synced 2026-12-03 06:31:42 +01:00
Pattern Stamp: refuse an Indexed pattern from another document's colour table
Edit > Define Pattern now records the colour table of the Indexed Color document a pattern came from (Pattern::source_table), next to its source mode. In an Indexed Color document the Pattern Stamp paints only a pattern carrying that document's own table and refuses one from another Indexed document with the existing alert, painting nothing and adding no History step. The table is saved in .pcraft bundles and in the pattern library index (serde default: older files load with no table and keep painting as before). Fixes #16726, fixes #16642.
This commit is contained in:
1 parent
2e32ed7bb5
commit
8af2ca2987
19 files changed
+258
-52
No files matched your search
@@ -606,7 +606,7 @@ mod tests {
|
||||
for y in 0..5 {
|
||||
surface.fill_rect(Rect::new(0, y, 3, y + 1), &[y as f32 / 4., 0.25, 0.75, 0.5]);
|
||||
}
|
||||
let p = Pattern { id: "bands".into(), name: "bands".into(), width: 3, height: 5, source_mode: None, surface };
|
||||
let p = Pattern { id: "bands".into(), name: "bands".into(), width: 3, height: 5, source_mode: None, source_table: None, surface };
|
||||
let budget = 3 * 2 * 16;
|
||||
let mut oversized = Vec::new();
|
||||
assert!(pattern_rgba_rows_in(&p, ColorMode::Rgb, 0, p.height, &mut oversized, budget).is_none());
|
||||
@@ -638,8 +638,15 @@ mod tests {
|
||||
#[test]
|
||||
/// Reject dense paint expansion of a retained oversized pattern without materializing sparse source tiles.
|
||||
fn stored_900_megapixel_pattern_is_not_expanded_for_painting() {
|
||||
let pattern =
|
||||
Pattern { id: "large".into(), name: "large".into(), width: 30_000, height: 30_000, source_mode: None, surface: Surface::new(PixelFormat::RGBA8) };
|
||||
let pattern = Pattern {
|
||||
id: "large".into(),
|
||||
name: "large".into(),
|
||||
width: 30_000,
|
||||
height: 30_000,
|
||||
source_mode: None,
|
||||
source_table: None,
|
||||
surface: Surface::new(PixelFormat::RGBA8),
|
||||
};
|
||||
assert!(pattern_rgba(&pattern, ColorMode::Rgb).is_empty());
|
||||
let mut rows = Vec::new();
|
||||
assert!(pattern_rgba_rows(&pattern, ColorMode::Rgb, 0, pattern.height, &mut rows).is_none());
|
||||
@@ -676,7 +683,15 @@ mod tests {
|
||||
#[test]
|
||||
fn patterns_too_large_to_paint_are_skipped_without_allocating() {
|
||||
// Only the size is declared (no tiles): a hostile file can claim any size.
|
||||
let p = Pattern { id: "big".into(), name: "Big".into(), width: 9000, height: 8000, source_mode: None, surface: Surface::new(PixelFormat::RGBA8) };
|
||||
let p = Pattern {
|
||||
id: "big".into(),
|
||||
name: "Big".into(),
|
||||
width: 9000,
|
||||
height: 8000,
|
||||
source_mode: None,
|
||||
source_table: None,
|
||||
surface: Surface::new(PixelFormat::RGBA8),
|
||||
};
|
||||
assert!(!p.is_paintable());
|
||||
assert!(Tile::new(&p).is_none());
|
||||
assert!(pattern_rgba(&p, ColorMode::Rgb).is_empty());
|
||||
@@ -695,6 +710,7 @@ mod tests {
|
||||
width,
|
||||
height,
|
||||
source_mode: None,
|
||||
source_table: None,
|
||||
surface: Surface::new(format),
|
||||
};
|
||||
let float = PixelFormat::new(ColorMode::Rgb, SampleType::F32, true);
|
||||
@@ -728,6 +744,7 @@ mod tests {
|
||||
width: 5000,
|
||||
height: 5000,
|
||||
source_mode: None,
|
||||
source_table: None,
|
||||
surface: Surface::new(PixelFormat::RGBA8),
|
||||
};
|
||||
let list = [sparse("a"), sparse("b"), sparse("c")];
|
||||
@@ -789,14 +806,29 @@ mod tests {
|
||||
/// declared size past what any allocation could be) gives `None`, never an aborted allocation.
|
||||
#[test]
|
||||
fn rows_that_cannot_be_allocated_give_none() {
|
||||
let huge =
|
||||
Pattern { id: "huge".into(), name: "huge".into(), width: u32::MAX, height: u32::MAX, source_mode: None, surface: Surface::new(PixelFormat::RGBA8) };
|
||||
let huge = Pattern {
|
||||
id: "huge".into(),
|
||||
name: "huge".into(),
|
||||
width: u32::MAX,
|
||||
height: u32::MAX,
|
||||
source_mode: None,
|
||||
source_table: None,
|
||||
surface: Surface::new(PixelFormat::RGBA8),
|
||||
};
|
||||
let mut out = Vec::new();
|
||||
assert!(pattern_rgba_rows(&huge, ColorMode::Rgb, 0, u32::MAX, &mut out).is_none());
|
||||
assert!(quantized_rgba(&huge, ColorMode::Rgb, |v| v as u8).is_none());
|
||||
assert!(pattern_rgba(&huge, ColorMode::Rgb).is_empty());
|
||||
assert!(Tile::new(&huge).is_none());
|
||||
let small = Pattern { id: "small".into(), name: "small".into(), width: 3, height: 2, source_mode: None, surface: Surface::new(PixelFormat::RGBA8) };
|
||||
let small = Pattern {
|
||||
id: "small".into(),
|
||||
name: "small".into(),
|
||||
width: 3,
|
||||
height: 2,
|
||||
source_mode: None,
|
||||
source_table: None,
|
||||
surface: Surface::new(PixelFormat::RGBA8),
|
||||
};
|
||||
assert_eq!(pattern_rgba_rows(&small, ColorMode::Rgb, 0, 2, &mut out), Some(()));
|
||||
assert_eq!(out.len(), 6);
|
||||
}
|
||||
|
||||
@@ -6,6 +6,8 @@ use photocraft_color::{ColorMode, SampleType};
|
||||
use photocraft_geom::Rect;
|
||||
use photocraft_raster::{AllocationError, Interrupt, Surface};
|
||||
|
||||
use crate::ColorTable;
|
||||
|
||||
/// Largest pattern edge a file may declare (the PSD reader's limit; Define Pattern stops at 8000).
|
||||
pub const MAX_EDGE: u32 = 30_000;
|
||||
|
||||
@@ -76,6 +78,12 @@ pub struct Pattern {
|
||||
/// how the library and a document dedupe, so defining pixels that are already stored keeps the
|
||||
/// source mode of the pattern that got there first.
|
||||
pub source_mode: Option<ColorMode>,
|
||||
/// The colour table of the Indexed Color document Edit › Define Pattern took this pattern
|
||||
/// from, alongside [`Self::source_mode`] (#16726): a Pattern Stamp stroke in an Indexed
|
||||
/// document paints only a pattern carrying that document's own table, as the reference does.
|
||||
/// `None` for every other source, and for patterns saved before the table was recorded. Not
|
||||
/// part of the id, like the source mode.
|
||||
pub source_table: Option<ColorTable>,
|
||||
pub surface: Surface,
|
||||
}
|
||||
|
||||
@@ -127,7 +135,7 @@ impl Pattern {
|
||||
ctl.check().map_err(|_| PatternBuildError::Cancelled)?;
|
||||
let h2 = fnv(h1 ^ 0x9e37_79b9_7f4a_7c15, format!("{:?}", surface.format()).as_bytes());
|
||||
let id = format!("{:08x}-{:04x}-{:04x}-{:04x}-{:012x}", (h1 >> 32) as u32, (h1 >> 16) as u16, h1 as u16, (h2 >> 48) as u16, h2 & 0xffff_ffff_ffff);
|
||||
Ok(Pattern { id, name, width, height, source_mode: None, surface })
|
||||
Ok(Pattern { id, name, width, height, source_mode: None, source_table: None, surface })
|
||||
}
|
||||
|
||||
/// A pattern with an id derived from its name and pixels (same content → same id, so
|
||||
@@ -149,7 +157,7 @@ impl Pattern {
|
||||
}
|
||||
let h2 = fnv(h1 ^ 0x9e37_79b9_7f4a_7c15, format!("{:?}", surface.format()).as_bytes());
|
||||
let id = format!("{:08x}-{:04x}-{:04x}-{:04x}-{:012x}", (h1 >> 32) as u32, (h1 >> 16) as u16, h1 as u16, (h2 >> 48) as u16, h2 & 0xffff_ffff_ffff);
|
||||
Pattern { id, name, width, height, source_mode: None, surface }
|
||||
Pattern { id, name, width, height, source_mode: None, source_table: None, surface }
|
||||
}
|
||||
|
||||
/// The name shown in UIs: Photoshop's localisation keys (`$$$/Patterns/…=Water`) show
|
||||
@@ -358,6 +366,7 @@ mod tests {
|
||||
width,
|
||||
height,
|
||||
source_mode: None,
|
||||
source_table: None,
|
||||
surface: Surface::new(format),
|
||||
};
|
||||
let float = PixelFormat::new(photocraft_color::ColorMode::Rgb, SampleType::F32, true);
|
||||
@@ -383,6 +392,7 @@ mod tests {
|
||||
width,
|
||||
height,
|
||||
source_mode: None,
|
||||
source_table: None,
|
||||
surface: Surface::new(format),
|
||||
};
|
||||
let float = PixelFormat::new(photocraft_color::ColorMode::Rgb, SampleType::F32, true);
|
||||
|
||||
@@ -649,7 +649,7 @@ fn bitmap_custom_pattern_accepts_compact_pattern_past_full_rgba_limit() {
|
||||
let mut surface = photocraft_raster::Surface::new(PixelFormat::RGBA8);
|
||||
surface.write_pixel(0, 0, &[0., 0., 0., 1.]);
|
||||
surface.write_pixel(w as i32 - 1, h as i32 - 1, &[0., 0., 0., 1.]);
|
||||
let pattern = photocraft_doc::Pattern { id: "compact".into(), name: "Compact".into(), width: w, height: h, source_mode: None, surface };
|
||||
let pattern = photocraft_doc::Pattern { id: "compact".into(), name: "Compact".into(), width: w, height: h, source_mode: None, source_table: None, surface };
|
||||
assert!(pattern.is_paintable());
|
||||
assert!(pattern.pixel_count() * 16 > photocraft_doc::pattern::MAX_WORKING_BYTES);
|
||||
s.edit("pattern", |doc, _| {
|
||||
@@ -676,6 +676,7 @@ fn bitmap_custom_pattern_rejects_excessive_ranking_workspace_before_reading_pixe
|
||||
width: 6000,
|
||||
height: 5000,
|
||||
source_mode: None,
|
||||
source_table: None,
|
||||
surface: photocraft_raster::Surface::new(PixelFormat::RGBA8),
|
||||
};
|
||||
assert!(pattern.is_paintable());
|
||||
|
||||
@@ -10,7 +10,7 @@
|
||||
//!
|
||||
//! Patterns are referenced by `"pattern": id | name` (document first, then library).
|
||||
|
||||
use photocraft_color::PixelFormat;
|
||||
use photocraft_color::{ColorMode, PixelFormat};
|
||||
use photocraft_doc::{Color, Document, Fill, Layer, LayerContent, Pattern, Rect};
|
||||
use photocraft_raster::Surface;
|
||||
use serde_json::{Value, json};
|
||||
@@ -271,7 +271,10 @@ fn define_with_max_pixels(s: &mut Session, p: &Value, max_px: u64) -> Result<Val
|
||||
Some(n) if !n.trim().is_empty() => n.to_string(),
|
||||
_ => default_name(s),
|
||||
};
|
||||
let pat = Pattern { source_mode: Some(doc.mode), ..Pattern::new(name, surf, r.width(), r.height()) };
|
||||
// An Indexed document's colour table goes with it, so a Pattern Stamp stroke can tell its own
|
||||
// table from another document's (#16726).
|
||||
let source_table = if doc.mode == ColorMode::Indexed { doc.color_table.clone() } else { None };
|
||||
let pat = Pattern { source_mode: Some(doc.mode), source_table, ..Pattern::new(name, surf, r.width(), r.height()) };
|
||||
let id = pat.id.clone();
|
||||
let out = info(&pat, (pat.width, pat.height), false, true);
|
||||
if !s.patterns.items.iter().any(|q| q.id == id) {
|
||||
@@ -668,8 +671,15 @@ mod tests {
|
||||
fn a_pattern_too_large_to_paint_is_refused_not_allocated() {
|
||||
// width × height overflows u32: this panicked (debug) or wrapped (release).
|
||||
let mut s = session(8);
|
||||
let huge =
|
||||
Pattern { id: "huge".into(), name: "Huge".into(), width: 65_536, height: 65_536, source_mode: None, surface: Surface::new(PixelFormat::RGBA8) };
|
||||
let huge = Pattern {
|
||||
id: "huge".into(),
|
||||
name: "Huge".into(),
|
||||
width: 65_536,
|
||||
height: 65_536,
|
||||
source_mode: None,
|
||||
source_table: None,
|
||||
surface: Surface::new(PixelFormat::RGBA8),
|
||||
};
|
||||
s.patterns.items.push(huge);
|
||||
let e = s.execute("brush.texturePattern", json!({"pattern": "huge"})).unwrap_err();
|
||||
assert!(e.to_string().contains("would paint nothing") && e.to_string().contains("65536 × 65536"), "{e}");
|
||||
|
||||
@@ -19,7 +19,7 @@
|
||||
use std::collections::HashSet;
|
||||
|
||||
use photocraft_color::ColorMode;
|
||||
use photocraft_doc::Pattern;
|
||||
use photocraft_doc::{ColorTable, Pattern};
|
||||
use photocraft_geom::Rect;
|
||||
use photocraft_raster::Surface;
|
||||
use serde::{Deserialize, Serialize};
|
||||
@@ -51,6 +51,10 @@ pub(super) struct Entry {
|
||||
/// Photoshop `.pat`, which records no source, so the index names it (older files: none).
|
||||
#[serde(default)]
|
||||
pub source_mode: Option<ColorMode>,
|
||||
/// The colour table of the Indexed document a defined pattern came from
|
||||
/// (`Pattern::source_table`); older indexes and every other source: none.
|
||||
#[serde(default, skip_serializing_if = "Option::is_none")]
|
||||
pub source_table: Option<ColorTable>,
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize)]
|
||||
@@ -121,7 +125,7 @@ fn preview(p: &Pattern) -> Pattern {
|
||||
}
|
||||
let mut surface = Surface::new(fmt);
|
||||
surface.write_region(Rect::new(0, 0, pw as i32, ph as i32), &data);
|
||||
Pattern { id: p.id.clone(), name: p.name.clone(), width: pw, height: ph, source_mode: p.source_mode, surface }
|
||||
Pattern { id: p.id.clone(), name: p.name.clone(), width: pw, height: ph, source_mode: p.source_mode, source_table: p.source_table.clone(), surface }
|
||||
}
|
||||
|
||||
fn read_one(backend: &dyn PresetBackend, file: &str) -> Result<Pattern, String> {
|
||||
@@ -135,6 +139,7 @@ pub(super) fn load_full(backend: &dyn PresetBackend, entry: &Entry) -> Result<Pa
|
||||
p.id = entry.id.clone();
|
||||
p.name = entry.name.clone();
|
||||
p.source_mode = entry.source_mode;
|
||||
p.source_table = entry.source_table.clone();
|
||||
Ok(p)
|
||||
}
|
||||
|
||||
@@ -186,6 +191,7 @@ pub(super) fn load(backend: &dyn PresetBackend, warnings: &mut Vec<String>) -> (
|
||||
p.id = e.id.clone();
|
||||
p.name = e.name.clone();
|
||||
p.source_mode = e.source_mode;
|
||||
p.source_table = e.source_table.clone();
|
||||
Ok(Loaded { pattern: p, full: Some((e.width, e.height)) })
|
||||
}
|
||||
Err(_) => load_full(backend, &e).map(|p| Loaded { pattern: preview(&p), full: Some((p.width, p.height)) }),
|
||||
@@ -227,7 +233,15 @@ pub(super) fn sync(backend: &dyn PresetBackend, files: &mut PatternFiles, librar
|
||||
.filter(|p| !crate::pattern_cmds::is_pristine_builtin(p, &builtins))
|
||||
.map(|p| {
|
||||
let (width, height) = library.stored.get(&p.id).copied().unwrap_or((p.width, p.height));
|
||||
Entry { id: p.id.clone(), name: p.name.clone(), file: file_name(&p.id), width, height, source_mode: p.source_mode }
|
||||
Entry {
|
||||
id: p.id.clone(),
|
||||
name: p.name.clone(),
|
||||
file: file_name(&p.id),
|
||||
width,
|
||||
height,
|
||||
source_mode: p.source_mode,
|
||||
source_table: p.source_table.clone(),
|
||||
}
|
||||
})
|
||||
.collect();
|
||||
// Unreadable files stay listed (at the end) until the library takes their pattern back.
|
||||
@@ -343,7 +357,15 @@ fn migrate_legacy(backend: &dyn PresetBackend, warnings: &mut Vec<String>) -> (V
|
||||
let migration = (|| -> Result<(), String> {
|
||||
let entries: Vec<Entry> = patterns
|
||||
.iter()
|
||||
.map(|p| Entry { id: p.id.clone(), name: p.name.clone(), file: file_name(&p.id), width: p.width, height: p.height, source_mode: p.source_mode })
|
||||
.map(|p| Entry {
|
||||
id: p.id.clone(),
|
||||
name: p.name.clone(),
|
||||
file: file_name(&p.id),
|
||||
width: p.width,
|
||||
height: p.height,
|
||||
source_mode: p.source_mode,
|
||||
source_table: p.source_table.clone(),
|
||||
})
|
||||
.collect();
|
||||
let index = serde_json::to_vec_pretty(&IndexFile { version: 1, patterns: entries.clone() }).map_err(|e| e.to_string())?;
|
||||
if index.len() as u64 > MAX_PATTERN_INDEX_BYTES {
|
||||
|
||||
@@ -579,6 +579,40 @@ fn patterns_survive_a_restart() {
|
||||
assert_eq!((toner.width, toner.height), (8, 6), "its pixels and size still load");
|
||||
}
|
||||
|
||||
/// The colour table of the Indexed document a pattern was defined from (#16726) survives a
|
||||
/// restart, and an index written before that member loads the pattern with no table.
|
||||
#[test]
|
||||
fn an_indexed_patterns_colour_table_survives_a_restart() {
|
||||
let dir = TempDir::new("pattern-table");
|
||||
let (mut s, w) = session(&dir);
|
||||
assert!(w.is_empty(), "{w:?}");
|
||||
define_pattern(&mut s, "Ink", "#c02040", 8);
|
||||
s.execute("image.mode.indexedColor", json!({"palette": "exact", "forced": "none", "dither": "none"})).unwrap();
|
||||
s.execute("edit.definePattern", json!({"name": "Indexed ink", "rect": [0, 0, 4, 4]})).unwrap();
|
||||
let table = s.active().unwrap().doc.color_table.clone();
|
||||
assert!(table.is_some());
|
||||
|
||||
let (t, _) = session(&dir);
|
||||
let ink = t.patterns.items.iter().find(|p| p.name == "Indexed ink").unwrap();
|
||||
assert_eq!(ink.source_mode, Some(photocraft_color::ColorMode::Indexed));
|
||||
assert_eq!(ink.source_table, table, "the table comes back with the pattern");
|
||||
let rgb = t.patterns.items.iter().find(|p| p.name == "Ink").unwrap();
|
||||
assert_eq!(rgb.source_table, None, "an RGB source has no table");
|
||||
|
||||
let index = dir.0.join(PATTERNS_FILE);
|
||||
let mut manifest: serde_json::Value = serde_json::from_slice(&std::fs::read(&index).unwrap()).unwrap();
|
||||
let mut removed = 0;
|
||||
for entry in manifest["patterns"].as_array_mut().unwrap() {
|
||||
removed += usize::from(entry.as_object_mut().unwrap().remove("source_table").is_some());
|
||||
}
|
||||
assert_eq!(removed, 1, "the writer names the member only where there is a table");
|
||||
std::fs::write(&index, serde_json::to_vec_pretty(&manifest).unwrap()).unwrap();
|
||||
let (older, w) = session(&dir);
|
||||
assert!(w.is_empty(), "{w:?}");
|
||||
let ink = older.patterns.items.iter().find(|p| p.name == "Indexed ink").unwrap();
|
||||
assert_eq!((ink.source_mode, ink.source_table.clone()), (Some(photocraft_color::ColorMode::Indexed), None), "an older index records no table");
|
||||
}
|
||||
|
||||
fn define_pattern(s: &mut Session, name: &str, color: &str, width: u32) {
|
||||
s.execute("file.new", json!({"width": width, "height": 6})).unwrap();
|
||||
crate::tests::fill_active(s, None, color);
|
||||
|
||||
@@ -29,14 +29,21 @@ pub const INDEXED_REFUSAL: &str = "Could not fill because the pattern and image
|
||||
|
||||
/// The reference refuses a pattern whose source is not an Indexed image in an Indexed Color
|
||||
/// document (#12887): an RGB one, and Grayscale, CMYK and Lab ones. A pattern Edit › Define
|
||||
/// Pattern took from an Indexed document keeps painting there (#13071): the reference paints the
|
||||
/// one with the document's own colour table, and PhotoCraft records that mode on the pattern.
|
||||
/// A pattern whose source mode was never recorded (the library's built-ins, a `.pat` or PSD
|
||||
/// block) counts as not Indexed, so those keep being refused. Bitmap, Duotone and Multichannel
|
||||
/// documents weren't measured and paint as before.
|
||||
/// Pattern took from an Indexed document keeps painting there (#13071) only when it carries the
|
||||
/// document's own colour table: one defined in another Indexed document, with a different table,
|
||||
/// is refused too (#16726), as the reference refuses it. An Indexed-source pattern saved before
|
||||
/// the table was recorded has none to compare and paints as before. A pattern whose source mode
|
||||
/// was never recorded (the library's built-ins, a `.pat` or PSD block) counts as not Indexed, so
|
||||
/// those keep being refused. Bitmap, Duotone and Multichannel documents weren't measured and
|
||||
/// paint as before.
|
||||
fn mode_refusal(s: &Session, pat: &Pattern) -> Result<()> {
|
||||
let indexed = s.active().is_some_and(|st| st.doc.mode == ColorMode::Indexed);
|
||||
if indexed && pat.source_mode != Some(ColorMode::Indexed) { Err(EngineError::Other(INDEXED_REFUSAL.into())) } else { Ok(()) }
|
||||
let Some(doc) = s.active().map(|st| &st.doc).filter(|d| d.mode == ColorMode::Indexed) else { return Ok(()) };
|
||||
let foreign_table = match (&pat.source_table, &doc.color_table) {
|
||||
(Some(source), Some(own)) => source.colors != own.colors,
|
||||
(Some(_), None) => true,
|
||||
(None, _) => false,
|
||||
};
|
||||
if pat.source_mode != Some(ColorMode::Indexed) || foreign_table { Err(EngineError::Other(INDEXED_REFUSAL.into())) } else { Ok(()) }
|
||||
}
|
||||
|
||||
/// [`mode_refusal`] for a press with the stroke's `p`, so the canvas can alert before the stroke
|
||||
|
||||
@@ -3,6 +3,8 @@
|
||||
//! application's message, painting nothing and adding no history; the live strokes are refused the
|
||||
//! same way. #13071: a pattern Edit › Define Pattern took from an Indexed image keeps painting
|
||||
//! there, and RGB, Grayscale, CMYK and Lab documents at every depth keep painting either way.
|
||||
//! #16726: only with the document's own colour table; one from another Indexed document is
|
||||
//! refused like the rest.
|
||||
|
||||
use super::super::LiveRetouch;
|
||||
use super::*;
|
||||
@@ -146,33 +148,69 @@ fn an_indexed_source_pattern_paints_in_other_documents_as_before() {
|
||||
}
|
||||
}
|
||||
|
||||
/// The one measured row of #12887 this change knowingly does not keep: the reference refuses an
|
||||
/// Indexed pattern from ANOTHER image, whose colours are not in the document's colour table, and
|
||||
/// paints only the one that carries that table. PhotoCraft records the source mode, not the table,
|
||||
/// so this paints - the deviation tracked as #16642, pinned here exactly as it behaves, with the
|
||||
/// landed colours shown to be outside the document's own table.
|
||||
#[test]
|
||||
fn a_pattern_defined_in_another_indexed_document_paints_today() {
|
||||
let mut s = crate::session();
|
||||
let pat = indexed_doc_and_pattern(&mut s);
|
||||
// A second Indexed document, cyan over magenta: its own exact palette, which holds none of the
|
||||
// tile's red, green and blue.
|
||||
/// A second Indexed document, cyan over magenta: its own exact palette, which holds none of the
|
||||
/// quadrant tile's red, green and blue. It becomes the active document.
|
||||
fn cyan_magenta_indexed_doc(s: &mut Session) {
|
||||
s.execute("file.new", json!({"width": 64, "height": 64, "depth": 8, "mode": "rgb"})).unwrap();
|
||||
for (y, color) in [(0, "#00ffff"), (32, "#ff00ff")] {
|
||||
s.execute("select.rect", json!({"x": 0, "y": y, "width": 64, "height": 32})).unwrap();
|
||||
s.execute("edit.fill", json!({"color": color})).unwrap();
|
||||
}
|
||||
s.execute("select.deselect", json!({})).unwrap();
|
||||
to_indexed(&mut s);
|
||||
to_indexed(s);
|
||||
}
|
||||
|
||||
/// #16726 / #16642, the row of #12887 #13071 could not keep: the reference refuses an Indexed
|
||||
/// pattern from ANOTHER image, whose colour table is not the document's, with its alert, and
|
||||
/// paints only the one that carries that table. Define Pattern now records the table, so the
|
||||
/// stroke, the press check and the live strokes all refuse it, painting nothing.
|
||||
#[test]
|
||||
fn a_pattern_defined_in_another_indexed_document_is_refused() {
|
||||
let mut s = crate::session();
|
||||
let pat = indexed_doc_and_pattern(&mut s);
|
||||
cyan_magenta_indexed_doc(&mut s);
|
||||
let table = s.active().unwrap().doc.color_table.clone().expect("an Indexed document has a colour table");
|
||||
assert!(!table.colors.contains(&[255, 0, 0]), "the second table holds {table:?}");
|
||||
let before = state(&s);
|
||||
assert_eq!(press_refusal(&s, &stroke(&pat, false)), None, "no refusal for a foreign Indexed source");
|
||||
s.execute("paint.patternStamp", stroke(&pat, false)).unwrap_or_else(|e| panic!("foreign Indexed source: {e}"));
|
||||
for impressionist in [false, true] {
|
||||
let err = s.execute("paint.patternStamp", stroke(&pat, impressionist)).unwrap_err().to_string();
|
||||
assert_eq!(err, INDEXED_REFUSAL, "impressionist {impressionist}");
|
||||
assert_eq!(state(&s), before, "impressionist {impressionist}: nothing painted, no history");
|
||||
}
|
||||
assert_eq!(press_refusal(&s, &stroke(&pat, false)).as_deref(), Some(INDEXED_REFUSAL));
|
||||
let live = LiveRetouch::begin(&s, "paint.patternStamp", &stroke(&pat, false)).err().map(|e| e.to_string());
|
||||
assert_eq!(live.as_deref(), Some(INDEXED_REFUSAL), "live");
|
||||
let dabs = live_impressionist(&s, &stroke(&pat, true)).err().map(|e| e.to_string());
|
||||
assert_eq!(dabs.as_deref(), Some(INDEXED_REFUSAL), "live Impressionist");
|
||||
}
|
||||
|
||||
/// Define Pattern records the Indexed document's colour table on the pattern, and nothing for
|
||||
/// other modes.
|
||||
#[test]
|
||||
fn define_pattern_records_the_indexed_documents_colour_table() {
|
||||
let mut s = crate::session();
|
||||
let pat = indexed_doc_and_pattern(&mut s);
|
||||
let own = s.active().unwrap().doc.color_table.clone();
|
||||
assert!(own.is_some());
|
||||
assert_eq!(s.patterns.items.iter().find(|q| q.id == pat).unwrap().source_table, own);
|
||||
let rgb = quadrants(&mut s, "rgb");
|
||||
assert_eq!(s.patterns.items.iter().find(|q| q.id == rgb).unwrap().source_table, None, "an RGB source has no table");
|
||||
}
|
||||
|
||||
/// An Indexed-source pattern saved before the colour table was recorded (an older `.pcraft` or
|
||||
/// library index) has none to compare, so it keeps painting in an Indexed document, as it did.
|
||||
#[test]
|
||||
fn an_indexed_source_pattern_without_a_recorded_table_paints_as_before() {
|
||||
let mut s = crate::session();
|
||||
let pat = indexed_doc_and_pattern(&mut s);
|
||||
s.patterns.items.iter_mut().find(|q| q.id == pat).unwrap().source_table = None;
|
||||
cyan_magenta_indexed_doc(&mut s);
|
||||
let before = state(&s);
|
||||
assert_eq!(press_refusal(&s, &stroke(&pat, false)), None);
|
||||
s.execute("paint.patternStamp", stroke(&pat, false)).unwrap_or_else(|e| panic!("older Indexed source: {e}"));
|
||||
let after = state(&s);
|
||||
assert_eq!(after.1, before.1 + 1, "one history step");
|
||||
assert_eq!(after.0[25 * 64 + 5], [1.0, 0.0, 0.0, 1.0], "the tile's red lands, though this table has no red");
|
||||
assert_eq!(after.0[25 * 64 + 12], [0.0, 1.0, 0.0, 1.0], "and its green, which this table lacks too");
|
||||
assert_eq!(after.0[25 * 64 + 5], [1.0, 0.0, 0.0, 1.0], "the tile's red lands");
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -185,6 +223,7 @@ fn a_pattern_whose_source_is_not_an_indexed_image_is_refused_in_an_indexed_docum
|
||||
width: 4,
|
||||
height: 4,
|
||||
source_mode: None,
|
||||
source_table: None,
|
||||
surface: Surface::new(PixelFormat::RGBA8),
|
||||
});
|
||||
new_doc(&mut s, "rgb", 8);
|
||||
|
||||
@@ -2130,6 +2130,7 @@ fn pattern_stamp_rejects_hostile_input() {
|
||||
width: 0,
|
||||
height: 0,
|
||||
source_mode: None,
|
||||
source_table: None,
|
||||
surface: photocraft_raster::Surface::new(fmt),
|
||||
});
|
||||
assert!(s.execute("paint.patternStamp", json!({"points": [[8, 8]], "pattern": "EmptyTile", "size": 8})).is_err());
|
||||
|
||||
@@ -93,7 +93,7 @@ fn a_pattern_that_would_paint_nothing_is_reported() {
|
||||
s.execute("file.new", json!({"width": 40, "height": 30, "depth": 8})).unwrap();
|
||||
// A 32-bit 5000 x 5000 pattern (declared only: 400 MB as a working copy, past the cap).
|
||||
let surface = photocraft_raster::Surface::new(photocraft_doc::PixelFormat::RGBA32F);
|
||||
s.patterns.items.push(Pattern { id: "huge".into(), name: "Huge".into(), width: 5000, height: 5000, source_mode: None, surface });
|
||||
s.patterns.items.push(Pattern { id: "huge".into(), name: "Huge".into(), width: 5000, height: 5000, source_mode: None, source_table: None, surface });
|
||||
let list = s.execute("pattern.list", json!({})).unwrap();
|
||||
let huge = list.as_array().unwrap().iter().find(|p| p["name"] == "Huge").unwrap().clone();
|
||||
assert_eq!(huge["paintable"], json!(false));
|
||||
|
||||
@@ -282,6 +282,7 @@ pub(crate) fn doc_m(d: &Document, sink: &mut dyn Sink) -> Result<DocM> {
|
||||
width: p.width,
|
||||
height: p.height,
|
||||
source_mode: p.source_mode,
|
||||
source_table: p.source_table.clone(),
|
||||
surface: surface_m(&p.surface, sink),
|
||||
})
|
||||
.collect(),
|
||||
@@ -689,6 +690,7 @@ impl Loader<'_> {
|
||||
width: p.width,
|
||||
height: p.height,
|
||||
source_mode: p.source_mode,
|
||||
source_table: p.source_table.clone(),
|
||||
surface: self.surface(&p.surface)?,
|
||||
});
|
||||
}
|
||||
|
||||
@@ -139,6 +139,10 @@ pub struct PatternM {
|
||||
/// (`photocraft_doc::Pattern::source_mode`); absent in older files, which recorded no source.
|
||||
#[serde(default)]
|
||||
pub source_mode: Option<ColorMode>,
|
||||
/// The colour table of the Indexed document it was defined from
|
||||
/// (`photocraft_doc::Pattern::source_table`); absent for other sources and in older files.
|
||||
#[serde(default, skip_serializing_if = "Option::is_none")]
|
||||
pub source_table: Option<photocraft_doc::ColorTable>,
|
||||
pub surface: SurfaceM,
|
||||
}
|
||||
|
||||
|
||||
@@ -197,6 +197,37 @@ fn a_pattern_source_mode_round_trips_and_an_older_bundle_loads_without_it() {
|
||||
assert_eq!(load_from_bytes(&z.finish().unwrap()).unwrap().patterns[0].source_mode, None, "an older bundle records no source");
|
||||
}
|
||||
|
||||
/// The colour table of the Indexed document a pattern was defined from (#16726) comes back with
|
||||
/// it, and a bundle written before that member existed still loads, with no table recorded.
|
||||
#[test]
|
||||
fn a_pattern_source_table_round_trips_and_an_older_bundle_loads_without_it() {
|
||||
let mut doc = Document::new("Indexed pattern", photocraft_geom::Size::new(8, 8), ColorMode::Rgb, SampleType::U8);
|
||||
let mut surface = photocraft_raster::Surface::new(photocraft_color::PixelFormat::RGBA8);
|
||||
surface.fill_rect(photocraft_geom::Rect::new(0, 0, 4, 4), &[1.0, 0.0, 0.0, 1.0]);
|
||||
let mut pattern = photocraft_doc::Pattern::new("From an Indexed image", surface, 4, 4);
|
||||
pattern.source_mode = Some(ColorMode::Indexed);
|
||||
pattern.source_table = Some(photocraft_doc::ColorTable { colors: vec![[255, 0, 0], [255, 255, 255]], transparent: Some(1) });
|
||||
doc.patterns.push(pattern);
|
||||
let bytes = save_to_bytes(&doc, &SaveOptions::default()).unwrap();
|
||||
assert_eq!(load_from_bytes(&bytes).unwrap(), doc, "the table comes back with the pixels");
|
||||
|
||||
let zip = zip::ZipReader::new(&bytes).unwrap();
|
||||
let mut z = zip::ZipWriter::new();
|
||||
for e in &zip.entries {
|
||||
let data = zip.read(e, usize::MAX).unwrap();
|
||||
if e.name == "manifest.json" {
|
||||
let mut manifest: serde_json::Value = serde_json::from_slice(&data).unwrap();
|
||||
assert!(manifest["document"]["patterns"][0].as_object_mut().unwrap().remove("source_table").is_some(), "the writer names the member");
|
||||
z.add(&e.name, &serde_json::to_vec(&manifest).unwrap()).unwrap();
|
||||
} else {
|
||||
z.add(&e.name, &data).unwrap();
|
||||
}
|
||||
}
|
||||
let older = load_from_bytes(&z.finish().unwrap()).unwrap();
|
||||
assert_eq!(older.patterns[0].source_table, None, "an older bundle records no table");
|
||||
assert_eq!(older.patterns[0].source_mode, Some(ColorMode::Indexed), "and keeps its mode");
|
||||
}
|
||||
|
||||
/// Rewrite only the fixture's report manifest to exercise corrupt framing and missing-blob validation.
|
||||
fn edit_import_manifest(bytes: &[u8], edit: impl Fn(&mut serde_json::Value)) -> Vec<u8> {
|
||||
edit_import_bundle(bytes, None, edit)
|
||||
|
||||
@@ -559,7 +559,7 @@ mod pattern_upload_tests {
|
||||
for y in 0..5 {
|
||||
surface.fill_rect(Rect::new(0, y, 3, y + 1), &[y as f32 / 4., 0.25, 0.75, 0.5]);
|
||||
}
|
||||
Pattern { id: "upload".into(), name: "upload".into(), width: 3, height: 5, source_mode: None, surface }
|
||||
Pattern { id: "upload".into(), name: "upload".into(), width: 3, height: 5, source_mode: None, source_table: None, surface }
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
||||
@@ -2948,7 +2948,7 @@ fn a_pattern_too_large_for_a_gpu_texture_falls_back_to_the_cpu() {
|
||||
let mut d = Document::new("big pattern", Size::new(8, 6), ColorMode::Rgb, SampleType::U8);
|
||||
// 100 MB as RGBA8, 400 MB as an RGBA32F texture.
|
||||
let surface = photocraft_raster::Surface::with_default(PixelFormat::RGBA8, &[1.0, 0.0, 0.0, 1.0]);
|
||||
let pat = Pattern { id: "big".into(), name: "big".into(), width: 5000, height: 5000, source_mode: None, surface };
|
||||
let pat = Pattern { id: "big".into(), name: "big".into(), width: 5000, height: 5000, source_mode: None, source_table: None, surface };
|
||||
assert!(pat.is_paintable());
|
||||
d.layers.push(Layer::new(
|
||||
"pattern",
|
||||
|
||||
@@ -128,7 +128,7 @@ fn pattern_from_psd(p: &PsdPattern, oom: &mut Option<String>) -> Option<Pattern>
|
||||
}
|
||||
// A file's block says what its pixels are, not which document they came from, so the source
|
||||
// mode stays unrecorded: an Indexed document keeps refusing such a pattern (#12887, #13071).
|
||||
Some(Pattern { id: p.id.clone(), name: p.name.clone(), width: p.width, height: p.height, source_mode: None, surface })
|
||||
Some(Pattern { id: p.id.clone(), name: p.name.clone(), width: p.width, height: p.height, source_mode: None, source_table: None, surface })
|
||||
}
|
||||
|
||||
/// A document pattern as a PSD pattern (its own depth and colour model; other models as RGB).
|
||||
@@ -265,8 +265,9 @@ pub fn export_global_blocks_with_warnings(doc: &Document, warnings: &mut Vec<Str
|
||||
|
||||
fn export_global_blocks_with_warnings_limit(doc: &Document, warnings: &mut Vec<String>, limit: u64) -> Vec<photocraft_doc::PsdGlobalBlock> {
|
||||
let raw = &doc.metadata.psd_global_blocks;
|
||||
// The comparison covers every field, `Pattern::source_mode` included; a pattern this reader
|
||||
// decoded names no source on either side, so a file's blocks are still reused verbatim.
|
||||
// The comparison covers every field, `Pattern::source_mode` and `source_table` included; a
|
||||
// pattern this reader decoded names no source on either side, so a file's blocks are still
|
||||
// reused verbatim.
|
||||
if from_global_blocks(doc) == doc.patterns {
|
||||
return repair_legacy_blocks(raw);
|
||||
}
|
||||
@@ -450,6 +451,7 @@ mod unpaintable_tests {
|
||||
width: side,
|
||||
height: side,
|
||||
source_mode: None,
|
||||
source_table: None,
|
||||
surface: Surface::new(format),
|
||||
};
|
||||
let patterns =
|
||||
@@ -470,7 +472,7 @@ mod allocation_tests {
|
||||
for name in ["First", "Second"] {
|
||||
let mut surface = Surface::new(PixelFormat::RGBA8);
|
||||
surface.fill_rect(Rect::new(0, 0, 2, 2), &[1., 0., 0., 1.]);
|
||||
doc.patterns.push(Pattern { id: name.into(), name: name.into(), width: 2, height: 2, source_mode: None, surface });
|
||||
doc.patterns.push(Pattern { id: name.into(), name: name.into(), width: 2, height: 2, source_mode: None, source_table: None, surface });
|
||||
}
|
||||
let before = doc.clone();
|
||||
let error = export_global_blocks_with_limit(&doc, 16).unwrap_err();
|
||||
@@ -499,6 +501,7 @@ mod allocation_tests {
|
||||
width: 30_000,
|
||||
height: 30_000,
|
||||
source_mode: None,
|
||||
source_table: None,
|
||||
surface,
|
||||
});
|
||||
let bytes = photocraft_format::save_to_bytes(&doc, &Default::default()).unwrap();
|
||||
@@ -536,6 +539,7 @@ mod allocation_tests {
|
||||
width: 5000,
|
||||
height: 5000,
|
||||
source_mode: None,
|
||||
source_table: None,
|
||||
surface: Surface::new(PixelFormat::new(ColorMode::Rgb, SampleType::F32, true)),
|
||||
};
|
||||
assert!(p.pixel_count() <= photocraft_doc::pattern::MAX_PAINT_PIXELS);
|
||||
@@ -556,7 +560,7 @@ mod allocation_tests {
|
||||
let mut doc = Document::new("Patterns", photocraft_geom::Size::new(2, 2), ColorMode::Rgb, SampleType::U8);
|
||||
let mut surface = Surface::new(PixelFormat::RGBA8);
|
||||
surface.write_pixel(0, 0, &[1., 0., 0., 1.]);
|
||||
let small = Pattern { id: "small".into(), name: "Small".into(), width: 2, height: 2, source_mode: None, surface };
|
||||
let small = Pattern { id: "small".into(), name: "Small".into(), width: 2, height: 2, source_mode: None, source_table: None, surface };
|
||||
doc.patterns.push(small.clone());
|
||||
let raw = export_global_blocks(&doc).unwrap();
|
||||
doc.metadata.psd_global_blocks = raw.clone();
|
||||
@@ -567,6 +571,7 @@ mod allocation_tests {
|
||||
width: 30_000,
|
||||
height: 30_000,
|
||||
source_mode: None,
|
||||
source_table: None,
|
||||
surface: Surface::new(PixelFormat::RGBA8),
|
||||
});
|
||||
let mut warnings = Vec::new();
|
||||
|
||||
@@ -532,7 +532,7 @@ mod pattern_failure_tests {
|
||||
for name in ["Retained first", "Refused second"] {
|
||||
let mut surface = Surface::new(PixelFormat::RGBA8);
|
||||
surface.fill_rect(photocraft_geom::Rect::new(0, 0, 2, 2), &[0.25, 0.5, 0.75, 1.]);
|
||||
doc.patterns.push(Pattern { id: name.into(), name: name.into(), width: 2, height: 2, source_mode: None, surface });
|
||||
doc.patterns.push(Pattern { id: name.into(), name: name.into(), width: 2, height: 2, source_mode: None, source_table: None, surface });
|
||||
}
|
||||
let before = doc.clone();
|
||||
let error = document_to_psd_with_pattern_limit(&doc, 16).unwrap_err();
|
||||
|
||||
@@ -13,7 +13,15 @@ fn checker() -> Pattern {
|
||||
s.fill_rect(Rect::new(0, 0, 8, 8), &[0.8, 0.8, 0.8, 1.0]);
|
||||
s.fill_rect(Rect::new(0, 0, 4, 4), &[0.2, 0.4, 0.9, 1.0]);
|
||||
s.fill_rect(Rect::new(4, 4, 8, 8), &[0.2, 0.4, 0.9, 1.0]);
|
||||
Pattern { id: "059289fb-0000-4000-8000-000000000001".into(), name: "Checker".into(), width: 8, height: 8, source_mode: None, surface: s }
|
||||
Pattern {
|
||||
id: "059289fb-0000-4000-8000-000000000001".into(),
|
||||
name: "Checker".into(),
|
||||
width: 8,
|
||||
height: 8,
|
||||
source_mode: None,
|
||||
source_table: None,
|
||||
surface: s,
|
||||
}
|
||||
}
|
||||
|
||||
fn doc(mode: ColorMode, depth: SampleType) -> Document {
|
||||
|
||||
@@ -16,7 +16,7 @@ fn pattern(name: &str) -> Pattern {
|
||||
let mut s = Surface::new(PixelFormat::RGBA8);
|
||||
s.fill_rect(Rect::new(0, 0, 8, 8), &[0.8, 0.8, 0.8, 1.0]);
|
||||
s.fill_rect(Rect::new(0, 0, 4, 4), &[0.2, 0.4, 0.9, 1.0]);
|
||||
Pattern { id: "059289fb-0000-4000-8000-000000000001".into(), name: name.into(), width: 8, height: 8, source_mode: None, surface: s }
|
||||
Pattern { id: "059289fb-0000-4000-8000-000000000001".into(), name: name.into(), width: 8, height: 8, source_mode: None, source_table: None, surface: s }
|
||||
}
|
||||
|
||||
/// One Pattern Fill layer per name; the name's length moves the descriptor's size by two bytes
|
||||
|
||||
Reference in new issue
Block a user