mirror of
https://github.com/storytold/craft-libs.git
synced 2026-11-26 10:18:05 +00:00
craft-psd: the typed views use the shared descriptor lookups
One set of Descriptor accessors: the get_* lookups from photocraft#9552. numbers, raw_data and color move next to them as get_numbers, get_raw_data and get_color.
This commit is contained in:
@@ -91,9 +91,9 @@ fn content_generator(r: &mut Reader<'_>) -> Result<Adjustment> {
|
||||
let _version = r.u32()?;
|
||||
let d = Descriptor::read(r)?;
|
||||
Ok(Adjustment::BrightnessContrast {
|
||||
brightness: d.number("Brgh").unwrap_or(0.0),
|
||||
contrast: d.number("Cntr").unwrap_or(0.0),
|
||||
legacy: d.boolean("useLegacy").unwrap_or(false),
|
||||
brightness: d.get_number("Brgh").unwrap_or(0.0),
|
||||
contrast: d.get_number("Cntr").unwrap_or(0.0),
|
||||
legacy: d.get_bool("useLegacy").unwrap_or(false),
|
||||
})
|
||||
}
|
||||
|
||||
@@ -130,11 +130,11 @@ fn exposure(r: &mut Reader<'_>) -> Result<Adjustment> {
|
||||
}
|
||||
|
||||
/// Reads the data of a `SoCo` block: the fill colour as RGB in 0..1 (see
|
||||
/// [`Descriptor::color`]), `None` for a colour model that is not RGB or gray.
|
||||
/// [`Descriptor::get_color`]), `None` for a colour model that is not RGB or gray.
|
||||
pub fn solid_color(data: &[u8]) -> Result<Option<[f64; 3]>> {
|
||||
let mut r = Reader::new(data);
|
||||
let _version = r.u32()?;
|
||||
Ok(Descriptor::read(&mut r)?.color("Clr "))
|
||||
Ok(Descriptor::read(&mut r)?.get_color("Clr "))
|
||||
}
|
||||
|
||||
/// Reads the data of an `lfx2` block: the layer effects descriptor.
|
||||
@@ -277,7 +277,7 @@ mod tests {
|
||||
lfx.extend(versioned(Descriptor::new("null").with("Scl ", Value::UnitFloat { unit: *b"#Prc", value: 100.0 })));
|
||||
let l = layer(vec![(*b"SoCo", soco.clone()), (*b"lfx2", lfx)]);
|
||||
assert_eq!(l.solid_color(), Some([1.0, 0.5, 0.0]));
|
||||
assert_eq!(l.effects().unwrap().number("Scl "), Some(100.0));
|
||||
assert_eq!(l.effects().unwrap().get_number("Scl "), Some(100.0));
|
||||
let float = Descriptor::new("RGBC")
|
||||
.with("redFloat", Value::Double(0.25))
|
||||
.with("greenFloat", Value::Double(0.5))
|
||||
|
||||
@@ -290,75 +290,6 @@ impl Descriptor {
|
||||
self.items.iter().find(|(k, _)| k.is(key)).map(|(_, v)| v)
|
||||
}
|
||||
|
||||
/// The object (`Objc` or `GlbO`) at `key`.
|
||||
pub fn object(&self, key: &str) -> Option<&Descriptor> {
|
||||
match self.get(key)? {
|
||||
Value::Descriptor(d) | Value::GlobalObject(d) => Some(d),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
/// The number at `key`: a double, a unit float, an integer or a large integer.
|
||||
pub fn number(&self, key: &str) -> Option<f64> {
|
||||
self.get(key)?.as_number()
|
||||
}
|
||||
|
||||
/// The numbers at `key`: the values of a unit float array, or the numbers of a list (items
|
||||
/// that are not numbers are left out). Empty when there is neither.
|
||||
pub fn numbers(&self, key: &str) -> Vec<f64> {
|
||||
match self.get(key) {
|
||||
Some(Value::UnitFloats { values, .. }) => values.clone(),
|
||||
Some(Value::List(l)) => l.iter().filter_map(Value::as_number).collect(),
|
||||
_ => Vec::new(),
|
||||
}
|
||||
}
|
||||
|
||||
/// The boolean at `key`.
|
||||
pub fn boolean(&self, key: &str) -> Option<bool> {
|
||||
match self.get(key)? {
|
||||
Value::Boolean(b) => Some(*b),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
/// The text at `key`, without its trailing NULs.
|
||||
pub fn text(&self, key: &str) -> Option<String> {
|
||||
match self.get(key)? {
|
||||
Value::Text(s) => Some(s.to_string_lossy()),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
/// The value id of the enumerated item at `key`.
|
||||
pub fn enumerated(&self, key: &str) -> Option<&Id> {
|
||||
match self.get(key)? {
|
||||
Value::Enumerated { value, .. } => Some(value),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
/// The raw data (`tdta`) at `key`.
|
||||
pub fn raw_data(&self, key: &str) -> Option<&[u8]> {
|
||||
match self.get(key)? {
|
||||
Value::RawData(v) => Some(v),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
/// The colour object at `key` as RGB in 0..1: an `RGBC` object with `Rd ` / `Grn ` / `Bl `
|
||||
/// in 0..255 or with `redFloat` / `greenFloat` / `blueFloat` in 0..1, or a gray (`Gry `, in
|
||||
/// percent of ink). Other colour models are `None`.
|
||||
pub fn color(&self, key: &str) -> Option<[f64; 3]> {
|
||||
let c = self.object(key)?;
|
||||
if let (Some(r), Some(g), Some(b)) = (c.number("Rd "), c.number("Grn "), c.number("Bl ")) {
|
||||
return Some([r / 255.0, g / 255.0, b / 255.0]);
|
||||
}
|
||||
if let (Some(r), Some(g), Some(b)) = (c.number("redFloat"), c.number("greenFloat"), c.number("blueFloat")) {
|
||||
return Some([r, g, b]);
|
||||
}
|
||||
c.number("Gry ").map(|g| [1.0 - g / 100.0; 3])
|
||||
}
|
||||
|
||||
/// Parses a descriptor from `data`, which must be consumed entirely.
|
||||
pub fn from_bytes(data: &[u8]) -> Result<Self> {
|
||||
let mut r = Reader::new(data);
|
||||
|
||||
@@ -43,6 +43,36 @@ impl Descriptor {
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
/// The numbers at `key`: the values of a unit float array, or the numbers of a list (items
|
||||
/// that are not numbers are left out). Empty when there is neither.
|
||||
pub fn get_numbers(&self, key: &str) -> Vec<f64> {
|
||||
match self.get(key) {
|
||||
Some(Value::UnitFloats { values, .. }) => values.clone(),
|
||||
Some(Value::List(l)) => l.iter().filter_map(Value::as_number).collect(),
|
||||
_ => Vec::new(),
|
||||
}
|
||||
}
|
||||
/// The raw data (`tdta`) at `key`.
|
||||
pub fn get_raw_data(&self, key: &str) -> Option<&[u8]> {
|
||||
match self.get(key)? {
|
||||
Value::RawData(v) => Some(v),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
/// The colour object at `key` as RGB in 0..1: an `RGBC` object with `Rd ` / `Grn ` / `Bl `
|
||||
/// in 0..255 or with `redFloat` / `greenFloat` / `blueFloat` in 0..1, or a gray (`Gry `, in
|
||||
/// percent of ink). Other colour models are `None`.
|
||||
pub fn get_color(&self, key: &str) -> Option<[f64; 3]> {
|
||||
let c = self.get_object(key)?;
|
||||
if let (Some(r), Some(g), Some(b)) = (c.get_number("Rd "), c.get_number("Grn "), c.get_number("Bl ")) {
|
||||
return Some([r / 255.0, g / 255.0, b / 255.0]);
|
||||
}
|
||||
if let (Some(r), Some(g), Some(b)) = (c.get_number("redFloat"), c.get_number("greenFloat"), c.get_number("blueFloat")) {
|
||||
return Some([r, g, b]);
|
||||
}
|
||||
c.get_number("Gry ").map(|g| [1.0 - g / 100.0; 3])
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
|
||||
+16
-16
@@ -56,7 +56,7 @@ pub struct Warp {
|
||||
|
||||
/// The eight numbers of a transform entry (`Trnf`, `nonAffineTransform`), when it has exactly eight.
|
||||
fn eight(d: &Descriptor, key: &str) -> Option<[f64; 8]> {
|
||||
d.numbers(key).try_into().ok()
|
||||
d.get_numbers(key).try_into().ok()
|
||||
}
|
||||
|
||||
/// The corner quad (top-left, top-right, bottom-right, bottom-left) from x0 y0 … x3 y3.
|
||||
@@ -68,26 +68,26 @@ impl Warp {
|
||||
/// Reads a `warp` descriptor; `None` for no warp (the identity, or a custom warp without a
|
||||
/// usable mesh).
|
||||
pub fn from_descriptor(d: &Descriptor) -> Option<Warp> {
|
||||
let style = d.enumerated("warpStyle").map_or_else(|| "warpNone".to_string(), |id| String::from_utf8_lossy(id.as_bytes()).into_owned());
|
||||
let side = |b: &Descriptor, k: &str| b.number(k).unwrap_or(0.0);
|
||||
let bounds = d.object("bounds").map_or([0.0; 4], |b| [side(b, "Left"), side(b, "Top "), side(b, "Rght"), side(b, "Btom")]);
|
||||
let style = d.get_enum("warpStyle").unwrap_or_else(|| "warpNone".to_string());
|
||||
let side = |b: &Descriptor, k: &str| b.get_number(k).unwrap_or(0.0);
|
||||
let bounds = d.get_object("bounds").map_or([0.0; 4], |b| [side(b, "Left"), side(b, "Top "), side(b, "Rght"), side(b, "Btom")]);
|
||||
let mut w = Warp {
|
||||
bend: d.number("warpValue").unwrap_or(0.0),
|
||||
horizontal: d.number("warpPerspective").unwrap_or(0.0),
|
||||
vertical: d.number("warpPerspectiveOther").unwrap_or(0.0),
|
||||
rotate_vertical: d.enumerated("warpRotate").is_some_and(|id| id.is("Vrtc")),
|
||||
bend: d.get_number("warpValue").unwrap_or(0.0),
|
||||
horizontal: d.get_number("warpPerspective").unwrap_or(0.0),
|
||||
vertical: d.get_number("warpPerspectiveOther").unwrap_or(0.0),
|
||||
rotate_vertical: d.get_enum("warpRotate").as_deref() == Some("Vrtc"),
|
||||
bounds,
|
||||
cols: 1,
|
||||
rows: 1,
|
||||
mesh: vec![],
|
||||
style,
|
||||
};
|
||||
if let Some(env) = d.object("customEnvelopeWarp") {
|
||||
let cols = env.numbers("quiltSliceX").len().saturating_sub(1).max(1);
|
||||
let rows = env.numbers("quiltSliceY").len().saturating_sub(1).max(1);
|
||||
if let Some(env) = d.get_object("customEnvelopeWarp") {
|
||||
let cols = env.get_numbers("quiltSliceX").len().saturating_sub(1).max(1);
|
||||
let rows = env.get_numbers("quiltSliceY").len().saturating_sub(1).max(1);
|
||||
if let Some(Value::ObjectArray(pts)) = env.get("meshPoints") {
|
||||
let xs = pts.body.numbers("Hrzn");
|
||||
let ys = pts.body.numbers("Vrtc");
|
||||
let xs = pts.body.get_numbers("Hrzn");
|
||||
let ys = pts.body.get_numbers("Vrtc");
|
||||
let n = cols.checked_mul(3).zip(rows.checked_mul(3)).and_then(|(c, r)| (c + 1).checked_mul(r + 1));
|
||||
if n.is_some_and(|n| xs.len() == n && ys.len() == n) {
|
||||
w.cols = cols;
|
||||
@@ -252,12 +252,12 @@ impl SmartObject {
|
||||
let _version = r.u32()?;
|
||||
let _descriptor_version = r.u32()?;
|
||||
let d = Descriptor::read(&mut r)?;
|
||||
let uuid = d.text("Idnt").unwrap_or_default();
|
||||
let uuid = d.get_text("Idnt").unwrap_or_default();
|
||||
let t = eight(&d, "Trnf").unwrap_or([0.0; 8]);
|
||||
// The non-affine transform when present (perspective), else the transform.
|
||||
let t = eight(&d, "nonAffineTransform").unwrap_or(t);
|
||||
let size = d.object("Sz ").and_then(|s| Some([s.number("Wdth")?, s.number("Hght")?]));
|
||||
let warp = d.object("warp").and_then(Warp::from_descriptor);
|
||||
let size = d.get_object("Sz ").and_then(|s| Some([s.get_number("Wdth")?, s.get_number("Hght")?]));
|
||||
let warp = d.get_object("warp").and_then(Warp::from_descriptor);
|
||||
Ok(SmartObject { uuid, quad: quad_from(t), size, warp })
|
||||
}
|
||||
|
||||
|
||||
@@ -45,13 +45,13 @@ impl TypeLayer {
|
||||
let _text_version = r.u16()?;
|
||||
let _descriptor_version = r.u32()?;
|
||||
let descriptor = Descriptor::read(&mut r)?;
|
||||
let text = descriptor.text("Txt ").unwrap_or_default().replace("\r\n", "\n").replace('\r', "\n");
|
||||
let text = descriptor.get_text("Txt ").unwrap_or_default().replace("\r\n", "\n").replace('\r', "\n");
|
||||
let text = text.strip_suffix('\n').map(str::to_string).unwrap_or(text);
|
||||
let style = descriptor.raw_data("EngineData").map(|e| engine_data::first_style(&engine_data::parse_lossy(e))).unwrap_or_default();
|
||||
let style = descriptor.get_raw_data("EngineData").map(|e| engine_data::first_style(&engine_data::parse_lossy(e))).unwrap_or_default();
|
||||
let bounds = descriptor
|
||||
.object("bounds")
|
||||
.or_else(|| descriptor.object("boundingBox"))
|
||||
.and_then(|o| Some([o.number("Left")?, o.number("Top ")?, o.number("Rght")?, o.number("Btom")?]));
|
||||
.get_object("bounds")
|
||||
.or_else(|| descriptor.get_object("boundingBox"))
|
||||
.and_then(|o| Some([o.get_number("Left")?, o.get_number("Top ")?, o.get_number("Rght")?, o.get_number("Btom")?]));
|
||||
Ok(TypeLayer { text, transform, style, bounds, descriptor })
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user