oxedyne/fe2o3/fe2o3_austenite/tests/colour_out.rs
17.7 KiB, 27 runs
created by r1870400018:69456, which is this file's identity for as long as the history lasts, whatever it is later renamed to
download · who wrote it · its history
| 1 | //! A page drawn through the evaluator and written in a converting mode holds every colour in the output |
| 2 | //! space and nothing in RGB. In `Cmyk` a fill, a stroke and a run of text are `k`, `K` and `g`, `G`, a raster |
| 3 | //! is `/DeviceCMYK` and each page names a CMYK transparency group; in `Grey` all of them are `g`, `G` |
| 4 | //! and `/DeviceGray`. A soft mask is `/DeviceGray` in every mode. A gradient reaches the writer as one flat ink, |
| 5 | //! so it too is converted. A raster seen twice through one mode is converted once. |
| 6 | //! |
| 7 | //! The settings that build these modes, and their refusals by key, are held in `tests/watch.rs`. |
| 8 | |
| 9 | use oxedyne_fe2o3_austenite::compile; |
| 10 | use oxedyne_fe2o3_austenite::emit::pdf::render_page; |
| 11 | use oxedyne_fe2o3_austenite::eval::fixpoint::PageSink; |
| 12 | use oxedyne_fe2o3_austenite::eval::intro::Introspector; |
| 13 | use oxedyne_fe2o3_austenite::eval::Engine; |
| 14 | use oxedyne_fe2o3_austenite::flow::text::FontStore; |
| 15 | use oxedyne_fe2o3_austenite::page::Page; |
| 16 | |
| 17 | use oxedyne_fe2o3_core::prelude::*; |
| 18 | use oxedyne_fe2o3_graphics::colour::{ |
| 19 | Ink, |
| 20 | Rgba, |
| 21 | }; |
| 22 | use oxedyne_fe2o3_graphics::icc; |
| 23 | use oxedyne_fe2o3_graphics::icc::transform::{ |
| 24 | Dev, |
| 25 | Intent, |
| 26 | Transform, |
| 27 | }; |
| 28 | use oxedyne_fe2o3_graphics::jpeg; |
| 29 | use oxedyne_fe2o3_graphics::pdf::{ |
| 30 | Black, |
| 31 | ColourOut, |
| 32 | PdfPage, |
| 33 | PdfWriter, |
| 34 | }; |
| 35 | use oxedyne_fe2o3_graphics::pixmap::{ |
| 36 | Pixmap, |
| 37 | Tone, |
| 38 | }; |
| 39 | |
| 40 | use std::path::{ |
| 41 | Path, |
| 42 | PathBuf, |
| 43 | }; |
| 44 | use std::sync::Arc; |
| 45 | |
| 46 | // Every surface has a value in a space of its own, so a colour found in the file is known by where it came from. |
| 47 | const SRC: &str = "\ |
| 48 | #set page(width: 240pt, height: 300pt, margin: 12pt) |
| 49 | Plain default text. |
| 50 | |
| 51 | #text(fill: luma(25%))[grey text] #text(fill: cmyk(100%, 0%, 0%, 0%))[cmyk text] #text(fill: rgb(\"#00ff00\"))[rgb text] |
| 52 | |
| 53 | #rect(width: 40pt, height: 12pt, fill: luma(50%)) |
| 54 | #rect(width: 40pt, height: 12pt, fill: cmyk(0%, 100%, 100%, 0%)) |
| 55 | #rect(width: 40pt, height: 12pt, fill: rgb(\"#ff0000\")) |
| 56 | #rect(width: 40pt, height: 12pt, fill: gradient.linear(rgb(\"#ff0000\"), rgb(\"#0000ff\"))) |
| 57 | |
| 58 | #grid(columns: (30pt,), stroke: 1pt + luma(75%), [grey ruled]) |
| 59 | #grid(columns: (30pt,), stroke: 1pt + cmyk(0%, 0%, 100%, 0%), [cmyk ruled]) |
| 60 | #grid(columns: (30pt,), stroke: 1pt + rgb(\"#0000ff\"), [rgb ruled]) |
| 61 | |
| 62 | #image(\"pic.png\", width: 24pt) |
| 63 | #image(\"soft.png\", width: 24pt) |
| 64 | "; |
| 65 | |
| 66 | // A page of only the two pictures, for the cache. |
| 67 | const PICS: &str = "\ |
| 68 | #set page(width: 120pt, height: 120pt, margin: 8pt) |
| 69 | #image(\"pic.png\", width: 24pt) |
| 70 | #image(\"soft.png\", width: 24pt) |
| 71 | "; |
| 72 | |
| 73 | // A page of two pictures whose sources hold one channel: an `L` PNG and a one-component JPEG. |
| 74 | const GREYS: &str = "\ |
| 75 | #set page(width: 120pt, height: 120pt, margin: 8pt) |
| 76 | #image(\"grey.png\", width: 24pt) |
| 77 | #image(\"grey.jpg\", width: 24pt) |
| 78 | "; |
| 79 | |
| 80 | struct ModeSink { |
| 81 | pages: Vec<PdfPage>, |
| 82 | mode: ColourOut, |
| 83 | out: Option<Vec<u8>>, |
| 84 | } |
| 85 | |
| 86 | impl PageSink for ModeSink { |
| 87 | fn page(&mut self, _engine: &mut Engine, page: Page) -> Outcome<()> { |
| 88 | self.pages.push(res!(render_page(&page))); |
| 89 | Ok(()) |
| 90 | } |
| 91 | |
| 92 | fn discard_pass(&mut self) -> Outcome<()> { |
| 93 | self.pages.clear(); |
| 94 | self.out = None; |
| 95 | Ok(()) |
| 96 | } |
| 97 | |
| 98 | fn finish(&mut self, _engine: &mut Engine, _intro: &Introspector) -> Outcome<()> { |
| 99 | let mut w = PdfWriter::new().with_colour_out(self.mode.clone()); |
| 100 | for p in self.pages.drain(..) { |
| 101 | w.add_page(p); |
| 102 | } |
| 103 | self.out = Some(res!(w.to_bytes())); |
| 104 | Ok(()) |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | // What a compile wrote: the text of the content streams, and the whole file read as text, for its dictionaries. |
| 109 | struct Written { |
| 110 | content: String, |
| 111 | whole: String, |
| 112 | } |
| 113 | |
| 114 | // The content streams of a file written uncompressed, joined; the binary streams are left out. |
| 115 | fn content_of(pdf: &[u8]) -> String { |
| 116 | let (open, close) = (&b"stream\n"[..], &b"\nendstream"[..]); |
| 117 | let mut text = String::new(); |
| 118 | let mut at = 0; |
| 119 | while let Some(i) = pdf[at..].windows(open.len()).position(|w| w == open) { |
| 120 | let from = at + i + open.len(); |
| 121 | let len = match pdf[from..].windows(close.len()).position(|w| w == close) { |
| 122 | Some(n) => n, |
| 123 | None => break, |
| 124 | }; |
| 125 | if let Ok(s) = std::str::from_utf8(&pdf[from..from + len]) { |
| 126 | text.push_str(s); |
| 127 | text.push('\n'); |
| 128 | } |
| 129 | at = from + len + close.len(); |
| 130 | } |
| 131 | text |
| 132 | } |
| 133 | |
| 134 | // A project directory holding the pictures: an opaque one, and one whose left half is clear. |
| 135 | fn project(name: &str, src: &str) -> Outcome<PathBuf> { |
| 136 | let dir = Path::new(env!("CARGO_TARGET_TMPDIR")).join("colour-out").join(name); |
| 137 | res!(std::fs::create_dir_all(&dir)); |
| 138 | res!(std::fs::write(dir.join("main.typ"), src)); |
| 139 | let mut pic = res!(Pixmap::filled(8, 8, Rgba::opaque(200, 40, 40))); |
| 140 | for y in 0..8 { |
| 141 | for x in 0..8 { |
| 142 | pic.set_pixel(x, y, Rgba::opaque(25 * x as u8, 30 * y as u8, 128)); |
| 143 | } |
| 144 | } |
| 145 | res!(std::fs::write(dir.join("pic.png"), res!(pic.to_png()))); |
| 146 | let mut soft = res!(Pixmap::filled(8, 8, Rgba::new(10, 160, 60, 255))); |
| 147 | for y in 0..8 { |
| 148 | for x in 0..4 { |
| 149 | soft.set_pixel(x, y, Rgba::new(10, 160, 60, 0)); |
| 150 | } |
| 151 | } |
| 152 | res!(std::fs::write(dir.join("soft.png"), res!(soft.to_png()))); |
| 153 | res!(std::fs::write(dir.join("grey.png"), grey_png(8, 8, |x, y| (16 * x + 12 * y) as u8))); |
| 154 | let mut ramp = res!(Pixmap::filled(16, 16, Rgba::opaque(0, 0, 0))); |
| 155 | for y in 0..16 { |
| 156 | for x in 0..16 { |
| 157 | let v = (15 * x + y) as u8; |
| 158 | ramp.set_pixel(x, y, Rgba::opaque(v, v, v)); |
| 159 | } |
| 160 | } |
| 161 | let opts = jpeg::Options { quality: 90, chroma: jpeg::Chroma::Full, grey: true }; |
| 162 | res!(std::fs::write(dir.join("grey.jpg"), res!(jpeg::encode_with(&ramp, &opts)))); |
| 163 | Ok(dir) |
| 164 | } |
| 165 | |
| 166 | // An eight-bit, one-channel (`L`) PNG of `w` by `h` samples, its scanlines stored rather than deflated. |
| 167 | fn grey_png(w: usize, h: usize, v: impl Fn(usize, usize) -> u8) -> Vec<u8> { |
| 168 | let mut raw = Vec::with_capacity(h * (w + 1)); |
| 169 | for y in 0..h { |
| 170 | raw.push(0); |
| 171 | for x in 0..w { |
| 172 | raw.push(v(x, y)); |
| 173 | } |
| 174 | } |
| 175 | // A zlib stream of one stored block, then the Adler-32 of the data. |
| 176 | let n = raw.len() as u16; |
| 177 | let mut z = vec![0x78, 0x01, 0x01]; |
| 178 | z.extend_from_slice(&n.to_le_bytes()); |
| 179 | z.extend_from_slice(&(!n).to_le_bytes()); |
| 180 | z.extend_from_slice(&raw); |
| 181 | let (mut a, mut b) = (1u32, 0u32); |
| 182 | for c in &raw { |
| 183 | a = (a + *c as u32) % 65521; |
| 184 | b = (b + a) % 65521; |
| 185 | } |
| 186 | z.extend_from_slice(&((b << 16) | a).to_be_bytes()); |
| 187 | let mut ihdr = Vec::with_capacity(13); |
| 188 | ihdr.extend_from_slice(&(w as u32).to_be_bytes()); |
| 189 | ihdr.extend_from_slice(&(h as u32).to_be_bytes()); |
| 190 | ihdr.extend_from_slice(&[8, 0, 0, 0, 0]); // eight bits, grey, then the only methods there are |
| 191 | let mut out = vec![0x89, b'P', b'N', b'G', 0x0D, 0x0A, 0x1A, 0x0A]; |
| 192 | for (kind, data) in [(b"IHDR", ihdr), (b"IDAT", z), (b"IEND", Vec::new())] { |
| 193 | out.extend_from_slice(&(data.len() as u32).to_be_bytes()); |
| 194 | let from = out.len(); |
| 195 | out.extend_from_slice(kind); |
| 196 | out.extend_from_slice(&data); |
| 197 | let crc = crc32(&out[from..]); |
| 198 | out.extend_from_slice(&crc.to_be_bytes()); |
| 199 | } |
| 200 | out |
| 201 | } |
| 202 | |
| 203 | fn crc32(bytes: &[u8]) -> u32 { |
| 204 | let mut c = !0u32; |
| 205 | for b in bytes { |
| 206 | c ^= *b as u32; |
| 207 | for _ in 0..8 { |
| 208 | c = if c & 1 != 0 { 0xEDB8_8320 ^ (c >> 1) } else { c >> 1 }; |
| 209 | } |
| 210 | } |
| 211 | !c |
| 212 | } |
| 213 | |
| 214 | fn written(name: &str, src: &str, mode: ColourOut) -> Outcome<Written> { |
| 215 | let dir = res!(project(name, src)); |
| 216 | let main = dir.join("main.typ"); |
| 217 | let mut sink = ModeSink { pages: Vec::new(), mode, out: None }; |
| 218 | let done = res!(compile::assemble_eval(&main, &dir, FontStore::default(), &mut sink)); |
| 219 | res!(done.laid.as_ref().map_err(|e| err!("{} did not lay out: {}", name, e.plain(); Test))); |
| 220 | let pdf = res!(sink.out.ok_or_else(|| err!("{}: no PDF", name; Test))); |
| 221 | Ok(Written { content: content_of(&pdf), whole: String::from_utf8_lossy(&pdf).to_string() }) |
| 222 | } |
| 223 | |
| 224 | fn cmyk_mode(black: Black) -> Outcome<ColourOut> { |
| 225 | let rgb = res!(Dev::srgb()); |
| 226 | let cmyk = res!(Dev::from_profile(&res!(icc::fogra39l()))); |
| 227 | ColourOut::cmyk(Arc::new(res!(Transform::new(&rgb, &cmyk, Intent::Perceptual, true))), black) |
| 228 | } |
| 229 | |
| 230 | fn grey_mode() -> Outcome<ColourOut> { |
| 231 | let rgb = res!(Dev::srgb()); |
| 232 | let grey = Dev::sgray(); |
| 233 | let cmyk = res!(Dev::from_profile(&res!(icc::fogra39l()))); |
| 234 | let xf = Arc::new(res!(Transform::new(&rgb, &grey, Intent::Perceptual, true))); |
| 235 | let cx = Arc::new(res!(Transform::new(&cmyk, &grey, Intent::Perceptual, true))); |
| 236 | ColourOut::grey(xf, cx) |
| 237 | } |
| 238 | |
| 239 | fn absent(w: &Written, bad: &[&str], mode: &str) -> Outcome<()> { |
| 240 | for b in bad { |
| 241 | if w.content.contains(b) { |
| 242 | return Err(err!("{} mode: the content holds {:?}:\n{}", mode, b, w.content; Test)); |
| 243 | } |
| 244 | } |
| 245 | Ok(()) |
| 246 | } |
| 247 | |
| 248 | fn present(w: &Written, want: &[&str], mode: &str) -> Outcome<()> { |
| 249 | for s in want { |
| 250 | if !w.content.contains(s) && !w.whole.contains(s) { |
| 251 | return Err(err!("{} mode: the file lacks {:?}:\n{}", mode, s, w.content; Test)); |
| 252 | } |
| 253 | } |
| 254 | Ok(()) |
| 255 | } |
| 256 | |
| 257 | // Does the content hold an operator `op` after exactly `n` numbers, on a line of its own? |
| 258 | fn has_op(content: &str, op: &str, n: usize) -> bool { |
| 259 | content.lines().any(|l| { |
| 260 | let parts: Vec<&str> = l.split_whitespace().collect(); |
| 261 | parts.len() == n + 1 && parts[n] == op && parts[..n].iter().all(|p| p.parse::<f64>().is_ok()) |
| 262 | }) |
| 263 | } |
| 264 | |
| 265 | // A line of the content whose operator is `op` and whose numbers are CMYK: the cyan, magenta and yellow of the |
| 266 | // line, summed. |
| 267 | fn cmy_of(content: &str, op: &str) -> Vec<f64> { |
| 268 | content.lines().filter_map(|l| { |
| 269 | let parts: Vec<&str> = l.split_whitespace().collect(); |
| 270 | if parts.len() == 5 && parts[4] == op { |
| 271 | let v: Vec<f64> = parts[..3].iter().filter_map(|p| p.parse::<f64>().ok()).collect(); |
| 272 | if v.len() == 3 { Some(v.iter().sum()) } else { None } |
| 273 | } else { None } |
| 274 | }).collect() |
| 275 | } |
| 276 | |
| 277 | #[test] |
| 278 | fn cmyk_mode_writes_every_surface_as_cmyk_or_k_only_grey() -> Outcome<()> { |
| 279 | let w = res!(written("cmyk", SRC, res!(cmyk_mode(Black::KOnly)))); |
| 280 | // Under K only, a grey and the default black stay on the black ink, and a CMYK ink is kept. |
| 281 | present(&w, &["0 g\n", "0.25 g\n", "0.5 g\n", "0.75 G\n", "1 0 0 0 k\n", "0 1 1 0 k\n", "0 0 1 0 K\n"], "cmyk")?; |
| 282 | absent(&w, &[" rg\n", " RG\n"], "cmyk")?; |
| 283 | // The green and blue are converted: they are four-number `k` and `K`, with ink on a channel besides black. |
| 284 | if cmy_of(&w.content, "k").iter().filter(|s| **s > 0.05).count() < 3 { |
| 285 | return Err(err!("cmyk mode: the converted fills are missing:\n{}", w.content; Test)); |
| 286 | } |
| 287 | if cmy_of(&w.content, "K").iter().filter(|s| **s > 0.05).count() < 2 { |
| 288 | return Err(err!("cmyk mode: the converted strokes are missing:\n{}", w.content; Test)); |
| 289 | } |
| 290 | // Images: the opaque one and the masked one are both CMYK, the mask is grey, and there is no RGB image. |
| 291 | present(&w, &["/ColorSpace /DeviceCMYK", "/ColorSpace /DeviceGray", "/SMask"], "cmyk")?; |
| 292 | if w.whole.contains("/DeviceRGB") { |
| 293 | return Err(err!("cmyk mode: the file names /DeviceRGB"; Test)); |
| 294 | } |
| 295 | // Each page says what it composites in. |
| 296 | present(&w, &["/Group << /S /Transparency /CS /DeviceCMYK /I true >>"], "cmyk") |
| 297 | } |
| 298 | |
| 299 | #[test] |
| 300 | fn cmyk_mode_with_a_rich_black_sends_a_neutral_through_the_profile() -> Outcome<()> { |
| 301 | let w = res!(written("cmyk-rich", SRC, res!(cmyk_mode(Black::Rich)))); |
| 302 | absent(&w, &[" g\n", " G\n", " rg\n", " RG\n"], "cmyk rich")?; |
| 303 | // The default black is the profile's own: four numbers, with more than black alone. |
| 304 | if !cmy_of(&w.content, "k").iter().any(|s| *s > 0.3) { |
| 305 | return Err(err!("cmyk rich mode: no neutral carries cyan, magenta and yellow:\n{}", w.content; Test)); |
| 306 | } |
| 307 | Ok(()) |
| 308 | } |
| 309 | |
| 310 | #[test] |
| 311 | fn grey_mode_writes_every_surface_as_grey() -> Outcome<()> { |
| 312 | let w = res!(written("grey", SRC, res!(grey_mode()))); |
| 313 | absent(&w, &[" rg\n", " RG\n", " k\n", " K\n"], "grey")?; |
| 314 | // Fill, stroke and text are `g` and `G`; the CMYK ink goes through its own profile to a grey. |
| 315 | if !has_op(&w.content, "g", 1) || !has_op(&w.content, "G", 1) { |
| 316 | return Err(err!("grey mode: no `g` or no `G`:\n{}", w.content; Test)); |
| 317 | } |
| 318 | present(&w, &["/ColorSpace /DeviceGray", "/SMask", "/Group << /S /Transparency /CS /DeviceGray /I true >>"], "grey")?; |
| 319 | // A grey ink is a /DeviceGray value already, so it is kept to the digit, as Ghostscript keeps it. |
| 320 | present(&w, &["0 g\n", "0.25 g\n", "0.5 g\n", "0.75 G\n"], "grey")?; |
| 321 | if w.whole.contains("/DeviceRGB") || w.whole.contains("/DeviceCMYK") { |
| 322 | return Err(err!("grey mode: the file names a colour space besides /DeviceGray"; Test)); |
| 323 | } |
| 324 | Ok(()) |
| 325 | } |
| 326 | |
| 327 | #[test] |
| 328 | fn native_and_rgb_modes_add_no_transparency_group_and_keep_rgb_images() -> Outcome<()> { |
| 329 | for (name, mode) in [("native", ColourOut::Native), ("rgb", ColourOut::Rgb)] { |
| 330 | let w = res!(written(&format!("plain-{}", name), SRC, mode)); |
| 331 | if w.whole.contains("/Group") { |
| 332 | return Err(err!("{} mode: a page carries a /Group", name; Test)); |
| 333 | } |
| 334 | present(&w, &["/ColorSpace /DeviceRGB"], name)?; |
| 335 | } |
| 336 | Ok(()) |
| 337 | } |
| 338 | |
| 339 | // Do the image XObjects number `n`, each named in `space`? |
| 340 | fn images_are(w: &Written, space: &str, n: usize, mode: &str) -> Outcome<()> { |
| 341 | let all = w.whole.matches("/Subtype /Image").count(); |
| 342 | let named = w.whole.matches(&format!("/ColorSpace {}", space)).count(); |
| 343 | if all != n || named != n { |
| 344 | return Err(err!("{} mode: {} images, {} of them {}, where {} of {} were wanted", mode, all, named, space, n, space; Test)); |
| 345 | } |
| 346 | Ok(()) |
| 347 | } |
| 348 | |
| 349 | #[test] |
| 350 | fn a_raster_from_a_grey_source_stays_grey_unless_a_rich_black_is_asked_for() -> Outcome<()> { |
| 351 | // Under K only an `L` PNG and a one-component JPEG are each a /DeviceGray image, printed with the black ink |
| 352 | // alone, as a grey ink is. |
| 353 | let w = res!(written("greys-cmyk", GREYS, res!(cmyk_mode(Black::KOnly)))); |
| 354 | images_are(&w, "/DeviceGray", 2, "cmyk")?; |
| 355 | // A rich black sends them through the profile, as it sends a grey ink. |
| 356 | let w = res!(written("greys-rich", GREYS, res!(cmyk_mode(Black::Rich)))); |
| 357 | images_are(&w, "/DeviceCMYK", 2, "cmyk rich")?; |
| 358 | let w = res!(written("greys-grey", GREYS, res!(grey_mode()))); |
| 359 | images_are(&w, "/DeviceGray", 2, "grey")?; |
| 360 | // Native and rgb write the samples as they were decoded, as before. |
| 361 | let w = res!(written("greys-native", GREYS, ColourOut::Native)); |
| 362 | images_are(&w, "/DeviceRGB", 2, "native") |
| 363 | } |
| 364 | |
| 365 | #[test] |
| 366 | fn a_flat_ink_is_taken_by_the_mode_and_a_neutral_is_exactly_neutral() -> Outcome<()> { |
| 367 | let k = res!(cmyk_mode(Black::KOnly)); |
| 368 | let rich = res!(cmyk_mode(Black::Rich)); |
| 369 | let mid = Ink::Rgb(Rgba::opaque(128, 128, 128)); |
| 370 | match res!(k.ink(mid)) { |
| 371 | Ink::Grey { v, .. } => assert!((v - 128.0 / 255.0).abs() < 1e-6, "{}", v), |
| 372 | other => return Err(err!("K only sent a neutral to {:?}", other; Test)), |
| 373 | } |
| 374 | // A near-neutral is not neutral, so it takes the transform, and so does everything under a rich black. |
| 375 | match res!(k.ink(Ink::Rgb(Rgba::opaque(128, 128, 129)))) { |
| 376 | Ink::Cmyk { .. } => (), |
| 377 | other => return Err(err!("K only sent a near-neutral to {:?}", other; Test)), |
| 378 | } |
| 379 | match res!(rich.ink(mid)) { |
| 380 | Ink::Cmyk { c, m, y, .. } => assert!(c + m + y > 0.0, "a rich neutral holds no cyan, magenta or yellow"), |
| 381 | other => return Err(err!("rich sent a neutral to {:?}", other; Test)), |
| 382 | } |
| 383 | // Under grey every ink is a grey, and a CMYK ink is read in its own profile. A grey ink is kept as it is. |
| 384 | let g = res!(grey_mode()); |
| 385 | let luma = Ink::Grey { v: 0.37, a: 255 }; |
| 386 | assert_eq!(res!(g.ink(luma)), luma, "grey output re-toned a grey ink"); |
| 387 | for ink in [mid, Ink::Cmyk { c: 1.0, m: 0.0, y: 0.0, k: 0.0, a: 255 }, Ink::Rgb(Rgba::opaque(255, 0, 0))] { |
| 388 | match res!(g.ink(ink)) { |
| 389 | Ink::Grey { .. } => (), |
| 390 | other => return Err(err!("grey sent {:?} to {:?}", ink, other; Test)), |
| 391 | } |
| 392 | } |
| 393 | // Native and rgb convert nothing. |
| 394 | assert!(res!(ColourOut::Native.image(&[0; 12], 2, 2, Tone::Colour)).is_none()); |
| 395 | assert!(res!(ColourOut::Rgb.image(&[0; 12], 2, 2, Tone::Colour)).is_none()); |
| 396 | Ok(()) |
| 397 | } |
| 398 | |
| 399 | #[test] |
| 400 | fn a_raster_comes_out_with_one_channel_in_grey_and_four_in_cmyk() -> Outcome<()> { |
| 401 | // A white and a black pixel, a red, and a mid grey. |
| 402 | let px = [255u8, 255, 255, 0, 0, 0, 255, 0, 0, 128, 128, 128]; |
| 403 | let (n, c) = res!(res!(res!(cmyk_mode(Black::KOnly)).image(&px, 2, 2, Tone::Colour)).ok_or_else(|| err!("no CMYK raster"; Test))); |
| 404 | assert_eq!((n, c.len()), (4, 16)); |
| 405 | assert!(c[..4].iter().all(|v| *v < 12), "white carries ink: {:?}", &c[..4]); |
| 406 | assert!(c[4..8].iter().any(|v| *v > 200), "black carries none: {:?}", &c[4..8]); |
| 407 | let (n, c) = res!(res!(res!(grey_mode()).image(&px, 2, 2, Tone::Colour)).ok_or_else(|| err!("no grey raster"; Test))); |
| 408 | assert_eq!((n, c.len()), (1, 4)); |
| 409 | assert!(c[0] > 245 && c[1] < 10, "white and black: {:?}", c); |
| 410 | assert!(c[3] > 100 && c[3] < 160, "the mid grey: {}", c[3]); |
| 411 | // A raster whose length is not its size is refused. |
| 412 | assert!(cmyk_mode(Black::KOnly).and_then(|m| m.image(&px[..11], 2, 2, Tone::Colour)).is_err()); |
| 413 | Ok(()) |
| 414 | } |
| 415 | |
| 416 | #[test] |
| 417 | fn a_raster_of_grey_tone_keeps_its_samples_unless_a_rich_black_is_asked_for() -> Outcome<()> { |
| 418 | // White, black, and two greys, each with its three samples equal, as a grey source decodes. |
| 419 | let px = [255u8, 255, 255, 0, 0, 0, 94, 94, 94, 200, 200, 200]; |
| 420 | for (name, mode) in [("cmyk", res!(cmyk_mode(Black::KOnly))), ("grey", res!(grey_mode()))] { |
| 421 | let (n, c) = res!(res!(mode.image(&px, 2, 2, Tone::Grey)).ok_or_else(|| err!("{}: no raster", name; Test))); |
| 422 | assert_eq!((n, c.as_slice()), (1, &[255u8, 0, 94, 200][..]), "{}", name); |
| 423 | } |
| 424 | let (n, c) = res!(res!(res!(cmyk_mode(Black::Rich)).image(&px, 2, 2, Tone::Grey)).ok_or_else(|| err!("no rich raster"; Test))); |
| 425 | assert_eq!(n, 4); |
| 426 | assert!(c[4..7].iter().any(|v| *v > 50), "a rich black holds no cyan, magenta or yellow: {:?}", &c[4..8]); |
| 427 | assert!(res!(ColourOut::Native.image(&px, 2, 2, Tone::Grey)).is_none()); |
| 428 | Ok(()) |
| 429 | } |
| 430 | |
| 431 | #[test] |
| 432 | fn a_raster_seen_again_is_read_from_the_cache() -> Outcome<()> { |
| 433 | let mode = res!(cmyk_mode(Black::KOnly)); |
| 434 | let cache = res!(mode.images().ok_or_else(|| err!("cmyk has no cache"; Test))).clone(); |
| 435 | let first = res!(written("cache-a", PICS, mode.clone())); |
| 436 | let a = res!(cache.stats()); |
| 437 | if a.misses == 0 || a.held == 0 || a.hits != 0 { |
| 438 | return Err(err!("after one compile of two pictures: {:?}", a; Test)); |
| 439 | } |
| 440 | let second = res!(written("cache-b", PICS, mode.clone())); |
| 441 | let b = res!(cache.stats()); |
| 442 | if b.misses != a.misses || b.hits < a.misses || b.held != a.held { |
| 443 | return Err(err!("a second compile converted again: {:?} then {:?}", a, b; Test)); |
| 444 | } |
| 445 | if first.whole != second.whole { |
| 446 | return Err(err!("a cached raster wrote other bytes"; Test)); |
| 447 | } |
| 448 | // A mode made afresh has a cache of its own, which holds nothing. |
| 449 | let other = res!(cmyk_mode(Black::KOnly)); |
| 450 | assert_eq!(res!(res!(other.images().ok_or_else(|| err!("no cache"; Test))).stats()).held, 0); |
| 451 | Ok(()) |
| 452 | } |