| 1 | //! OneNote 2010's page template art, recognised by the SHA-256 of OneNote's picture or the |
| 2 | //! marker in Snowbound's, and painted from vector recreations that suit the paper. |
| 3 | |
| 4 | use super::{SETTLE, Slot, density, queue}; |
| 5 | use crate::gpu::Paper; |
| 6 | use draw::RasterImage; |
| 7 | use resvg::{tiny_skia, usvg}; |
| 8 | use sha2::{Digest, Sha256}; |
| 9 | use std::{task::Waker, time::Duration}; |
| 10 | use web_time::Instant; |
| 11 | |
| 12 | /// One of OneNote 2010's page templates: its art recreated for white paper in `#rrggbb` |
| 13 | /// colours over a view box the size in pixels of the picture OneNote stores, that picture's |
| 14 | /// format, and the SHA-256 of each picture OneNote has shipped for it. |
| 15 | struct Template { |
| 16 | name: &'static str, |
| 17 | svg: &'static str, |
| 18 | jpeg: bool, |
| 19 | hashes: &'static [&'static str], |
| 20 | } |
| 21 | |
| 22 | macro_rules! template { |
| 23 | ($name:literal, $format:ident, $($hash:literal),+ $(,)?) => { |
| 24 | Template { |
| 25 | name: $name, |
| 26 | svg: include_str!(concat!("../../../assets/backgrounds/", $name, ".svg")), |
| 27 | jpeg: template!(@$format), |
| 28 | hashes: &[$($hash),+], |
| 29 | } |
| 30 | }; |
| 31 | (@jpeg) => { true }; |
| 32 | (@png) => { false }; |
| 33 | } |
| 34 | |
| 35 | const TEMPLATES: &[Template] = &[ |
| 36 | template!( |
| 37 | "bamboo", |
| 38 | jpeg, |
| 39 | "a7fa7f092eff43030a56342c39a765f8d5cc48c7db815ddfc8c1e5ec40117fae" |
| 40 | ), |
| 41 | template!( |
| 42 | "binoculars-corner", |
| 43 | jpeg, |
| 44 | "74c131777e7c437fd654427417097bc01b0813ba8e1e50e4b937bd50a1bebcdb" |
| 45 | ), |
| 46 | template!( |
| 47 | "black-and-green-title", |
| 48 | jpeg, |
| 49 | "9c552717e8d5079bbb226948641ff13532df3d7be434c6ce545f1692fa57d45a" |
| 50 | ), |
| 51 | template!( |
| 52 | "blue-bubbles-corner-margin", |
| 53 | jpeg, |
| 54 | "3c68c7ee798e62a4a99c740153f3980d7df029605c843410942c7f85e794823b" |
| 55 | ), |
| 56 | template!( |
| 57 | "blue-bubbles-corner-top", |
| 58 | jpeg, |
| 59 | "d0a3a1c3cd63c4023fe5716cbe2c211307d0e277e444d9ef76c7fc097a845fd4" |
| 60 | ), |
| 61 | template!( |
| 62 | "blue-clouds", |
| 63 | jpeg, |
| 64 | "7966f3d8a2d61ecb49a35e163781858e052c0b122a18a1238afe27b57e2850e8" |
| 65 | ), |
| 66 | template!( |
| 67 | "blue-dots", |
| 68 | jpeg, |
| 69 | "139bb0e79f89c3ddef79b1716a5fbab4c07df5785fb3cdf6b4eeddbf6c078452" |
| 70 | ), |
| 71 | template!( |
| 72 | "blue-flow", |
| 73 | png, |
| 74 | "473c90248fa33f8e49b2daf38673d87a9405481eae6a2087cc5384b695b6a4c0", |
| 75 | "51e05999a1c9f17df28cb474e57dd8e64bdab824874a532c20a23766a01f8967", |
| 76 | ), |
| 77 | template!( |
| 78 | "blue-mist-margin", |
| 79 | jpeg, |
| 80 | "83245f217deae4a4143b565e13c045dbb32a9063e8c6b2e43bb15cd76c5f9219" |
| 81 | ), |
| 82 | template!( |
| 83 | "blue-stripe-title", |
| 84 | jpeg, |
| 85 | "474f9a8c25d5e21192315397ea995b1e11e2c1608157c6e0277688091bfd136a" |
| 86 | ), |
| 87 | template!( |
| 88 | "blue-stripes", |
| 89 | jpeg, |
| 90 | "06e09de80c3f32254da4fe6b2cbad7c05ef144dd54b8c65745e195bbf7317a2e" |
| 91 | ), |
| 92 | template!( |
| 93 | "blue-swirls", |
| 94 | jpeg, |
| 95 | "10c833e47be1c8496f949a6b059c2d79212a4dd66bde62116ea337fa4fe0b654" |
| 96 | ), |
| 97 | template!( |
| 98 | "blue-wave-title", |
| 99 | jpeg, |
| 100 | "646586cb71742a2369a529876b41af6a472c35cc508d1ae5d8395d55784814f2" |
| 101 | ), |
| 102 | template!( |
| 103 | "blue-waves", |
| 104 | jpeg, |
| 105 | "fbb4573e3bee1b337077691bebae15d6fac52432405d31396d526d7694a8283d" |
| 106 | ), |
| 107 | template!( |
| 108 | "books", |
| 109 | jpeg, |
| 110 | "1591fd26e7fff5be97431d0ed3d0ade5cfc5fa74e3d7ec282fd242160ce68c1f" |
| 111 | ), |
| 112 | template!( |
| 113 | "bouquet", |
| 114 | jpeg, |
| 115 | "bdd997068701ed3a00a224eb694b003c01ac69b857fe7b4147d6c34875b1632b" |
| 116 | ), |
| 117 | template!( |
| 118 | "boxes", |
| 119 | png, |
| 120 | "e64fdcd0b108737d8b8f7b677029f924031d6bbaa50585d9c3def7c7e92ecaf2" |
| 121 | ), |
| 122 | template!( |
| 123 | "boxes-shadow", |
| 124 | png, |
| 125 | "639eadad468b6b32b9124b1f4395a8da3027ff7258d102173ba070ae2ed541ae" |
| 126 | ), |
| 127 | template!( |
| 128 | "bubbles", |
| 129 | png, |
| 130 | "5eebe803e434a845d19bc600df3c75e98bb69bd0de473ceec410d1b3a9154e28" |
| 131 | ), |
| 132 | template!( |
| 133 | "bubbles-margin-margin", |
| 134 | jpeg, |
| 135 | "509a6945facfb3ddc7be6ee8b82797ad0c72db5755486ee878125a959cc09b59" |
| 136 | ), |
| 137 | template!( |
| 138 | "bubbles-margin-top", |
| 139 | jpeg, |
| 140 | "e9746b4e9ae9ce7b3b0068779db3e113e2dfc9880f25373d745d0e700e69a906" |
| 141 | ), |
| 142 | template!( |
| 143 | "buildings", |
| 144 | jpeg, |
| 145 | "505f731cb7707efab2eb06685b392dc7e59265a40b55aae43e5dc15c0a86cba4" |
| 146 | ), |
| 147 | template!( |
| 148 | "chain-title", |
| 149 | png, |
| 150 | "edad0f03e6ff99fef9ef8e8b834ce74f26cd23c5f8c067f5cee66f304181e64d" |
| 151 | ), |
| 152 | template!( |
| 153 | "circles", |
| 154 | png, |
| 155 | "e1cb1a0ec9be62d5445c73aa84df38234002a7e164ee830c9df24997802cb5d2" |
| 156 | ), |
| 157 | template!( |
| 158 | "clock", |
| 159 | jpeg, |
| 160 | "adb1ebbe18d6cd8ff08aa9bf5c83cdb83bf9aa179698e34e93dbcdde12f04d32" |
| 161 | ), |
| 162 | template!( |
| 163 | "columns", |
| 164 | jpeg, |
| 165 | "70475431cca3c91a4efa3b8f04864371d2d3a45696674a1a0562fe9cd8db287c" |
| 166 | ), |
| 167 | template!( |
| 168 | "computer", |
| 169 | jpeg, |
| 170 | "9301db6d2d87282fcee450189aeace16d85f64273bf62713a3044992b6b7a9e9" |
| 171 | ), |
| 172 | template!( |
| 173 | "cyan-title", |
| 174 | jpeg, |
| 175 | "20016e5fa1a32dce5af4e92872597e36432185a7bb2e61c91f362bd68484529b" |
| 176 | ), |
| 177 | template!( |
| 178 | "day-planner", |
| 179 | jpeg, |
| 180 | "035b26df61855a3f36dbd30fdab0c157c04c9e8ae2197ea4d4aeb3e82e6a4c2b" |
| 181 | ), |
| 182 | template!( |
| 183 | "fireworks", |
| 184 | png, |
| 185 | "b243a24fa13bc8523450e22f408f9eff15301c938f8ca52a57018b58ce6785de" |
| 186 | ), |
| 187 | template!( |
| 188 | "flat-squares", |
| 189 | png, |
| 190 | "1262dd23ad54e935cfa10feb1be56648e43bef1116696ca71d87e6e033b1ca7d" |
| 191 | ), |
| 192 | template!( |
| 193 | "flowers-and-hearts", |
| 194 | jpeg, |
| 195 | "423b0dd1a93b391d15b1dc8d8757c3bf5725ff2e7a59e6e3140033e2876b67f6" |
| 196 | ), |
| 197 | template!( |
| 198 | "glasses-corner", |
| 199 | jpeg, |
| 200 | "131d637cdc5d0b094fb9fad17f4d2a1ace0d03613588155aacaa2d1cb4e16da9" |
| 201 | ), |
| 202 | template!( |
| 203 | "globe", |
| 204 | jpeg, |
| 205 | "20abe389c885e42b6ebe9e902976229bb6fd63c8c34cb61aa70b8b746209f90a" |
| 206 | ), |
| 207 | template!( |
| 208 | "graph", |
| 209 | png, |
| 210 | "95f59a0433050180d4c0e8858b83363d51bea6752a8b7ca516a8677854d8f5b6" |
| 211 | ), |
| 212 | template!( |
| 213 | "green-stripes", |
| 214 | jpeg, |
| 215 | "d56cefe9ee2fae72e31bdba7dd2aa4426ea22e3ceb22ef68c8f63f9f24d5a8bc" |
| 216 | ), |
| 217 | template!( |
| 218 | "hearts", |
| 219 | png, |
| 220 | "2035407a0540e1c4f7934db08ba4add750fcb9a62863ddd9553e7871c81a99e3" |
| 221 | ), |
| 222 | template!( |
| 223 | "ivy", |
| 224 | jpeg, |
| 225 | "7ce10d1ea660d2f9cf8b704f3fab2966a4ce2627d9858d32c75d857095012098" |
| 226 | ), |
| 227 | template!( |
| 228 | "large-push-pins", |
| 229 | jpeg, |
| 230 | "fb12d59b8be911247bbafdd416852e8b74b028005a141cb4dbbba109b4b6ed2c" |
| 231 | ), |
| 232 | template!( |
| 233 | "light-bulb-corner", |
| 234 | jpeg, |
| 235 | "7d2b01f17354d9237a6ab99d5b9afdf0e1cc43687125848b0c2dedfb44ce3843" |
| 236 | ), |
| 237 | template!( |
| 238 | "lilies", |
| 239 | jpeg, |
| 240 | "140dd943d0f0cfe6aaa98470b7d1a7cb62ca02cb1d8f522dd2ac77433232ef41" |
| 241 | ), |
| 242 | template!( |
| 243 | "math", |
| 244 | jpeg, |
| 245 | "1f81dde3b42f23f0666d92ebf14d62893b31b39d72c07aee070eae28c2e6980e" |
| 246 | ), |
| 247 | template!( |
| 248 | "math-science-class-notes", |
| 249 | jpeg, |
| 250 | "ec8cd7250f3d82e900e99114869777ee859ec73effabed108815f65742078c3a" |
| 251 | ), |
| 252 | template!( |
| 253 | "networking", |
| 254 | png, |
| 255 | "0401451e1d1d7dfdc29ad1b2b68a6c8ac0b706e9868bf22fab26a01cd48620ce" |
| 256 | ), |
| 257 | template!( |
| 258 | "notebook", |
| 259 | jpeg, |
| 260 | "7e6a7714a69688d9ffdf16aa942b66064a0c77fcd9b3e469f89730b4b9290c3e" |
| 261 | ), |
| 262 | template!( |
| 263 | "orange-margin", |
| 264 | jpeg, |
| 265 | "be4f0e6c89fce91b9ebd2623567f7dfc259e0e3c77c9158742b8f64b724df673" |
| 266 | ), |
| 267 | template!( |
| 268 | "paper", |
| 269 | jpeg, |
| 270 | "7354001527ab554c44e7d6981b86dd933b7dc2e0d3dc8512ad3eecd843245c52" |
| 271 | ), |
| 272 | template!( |
| 273 | "paws-cyan", |
| 274 | png, |
| 275 | "e9ead0bfc09d32cb366010cdfede1c432a2d1d550cb7332badac1bee9482bc86" |
| 276 | ), |
| 277 | template!( |
| 278 | "pencil-and-notebook", |
| 279 | jpeg, |
| 280 | "28f334b77068f71f5f92a95695433b950610204a0e5580ce567db8fad4993ecb" |
| 281 | ), |
| 282 | template!( |
| 283 | "pens", |
| 284 | jpeg, |
| 285 | "3fbe3c1c238bd7dbc67f8cff5f3bddfd513c96a9851b9616477947d21dff4b2e" |
| 286 | ), |
| 287 | template!( |
| 288 | "pixel", |
| 289 | png, |
| 290 | "5dc2367a80588a7518db5014122510bf0fd784711015ef83a8718336584f82d0" |
| 291 | ), |
| 292 | template!( |
| 293 | "pixel-blends", |
| 294 | png, |
| 295 | "edbb86f160050fbf1f9860276802bae292dbfd0bc98e3ea90d43d981e9f0c54a" |
| 296 | ), |
| 297 | template!( |
| 298 | "plain-and-simple", |
| 299 | jpeg, |
| 300 | "de35af949d4f83e97ee22f817afe2531cc4b59ff9ee6026dca7ecebc5cf2737f" |
| 301 | ), |
| 302 | template!( |
| 303 | "purple-clouds", |
| 304 | jpeg, |
| 305 | "e111f96490755c7d71e87c88acaea38afe55bb865b1a14a83c5bd239648d5e2c" |
| 306 | ), |
| 307 | template!( |
| 308 | "pushpins-corner", |
| 309 | png, |
| 310 | "62ad3c277e54f03f1adb44062407346f789e63859b7afabfd64be6af5e9f66ec" |
| 311 | ), |
| 312 | template!( |
| 313 | "rainbow", |
| 314 | jpeg, |
| 315 | "f927c7825851974a2149868146970706523a49165133cee6027a43e8c9abdf27" |
| 316 | ), |
| 317 | template!( |
| 318 | "rainbow-border", |
| 319 | png, |
| 320 | "0e31da4dfcff4a36c64c1ce940362d2309769f36369e4c43c317d5f2fa15658e" |
| 321 | ), |
| 322 | template!( |
| 323 | "rainbow-mini-border", |
| 324 | png, |
| 325 | "2e7aaf26bec32148b96442e8fff1bd2cef2d72630969f23b9a2abedb6cfec93b" |
| 326 | ), |
| 327 | template!( |
| 328 | "recipe", |
| 329 | jpeg, |
| 330 | "525a857d0eda855a64d3619df58b1c2d013a73e60fa0d49b155ecfcb2c134c7c" |
| 331 | ), |
| 332 | template!( |
| 333 | "red-and-black-margin", |
| 334 | jpeg, |
| 335 | "b4daa90d5a53fcbc85119050b5b76962443c4dd18d7f42cdc6d4e0ad8efad872" |
| 336 | ), |
| 337 | template!( |
| 338 | "side-stripes", |
| 339 | jpeg, |
| 340 | "14fa2d16310485aa1ce41f6d774a3d637e8cf8b03c4f72990155df274fdb6bd9" |
| 341 | ), |
| 342 | template!( |
| 343 | "small-push-pins", |
| 344 | jpeg, |
| 345 | "d74c3973c8d1f7c77274691afb1aa934940674341d7eee563be75e563281bdfd" |
| 346 | ), |
| 347 | template!( |
| 348 | "sparks", |
| 349 | jpeg, |
| 350 | "f091ded5e283af6848670a3172e7c43c6099875d39b3fc69c2bdba914f609602" |
| 351 | ), |
| 352 | template!( |
| 353 | "stars", |
| 354 | png, |
| 355 | "7f5b660a1a0bf46c75aaf19b4f77a0e086de003ec03afc1f58d871d55aa5ba9e" |
| 356 | ), |
| 357 | template!( |
| 358 | "swirls", |
| 359 | png, |
| 360 | "0908a4cfa23f93011176d47f45843e9ca2973030421996e8e27484781f54b0ec", |
| 361 | "8b025b80e7d398229ef19384156fe98aeefcf8f69169d69e1eeba73281b24d7e", |
| 362 | ), |
| 363 | template!( |
| 364 | "tiles", |
| 365 | jpeg, |
| 366 | "2a4a3530d652e227ddd5adc096a95f6034718f7c380b07db622022d768815059" |
| 367 | ), |
| 368 | template!( |
| 369 | "triangles-title-margin", |
| 370 | jpeg, |
| 371 | "d72900e8da72d1a7f3729971aa558e1e9b6e9cf9a0d51e83852e567256dbbfef" |
| 372 | ), |
| 373 | template!( |
| 374 | "triangles-title-top", |
| 375 | jpeg, |
| 376 | "2df453410796aec7b9efec00059b6ce64bcf67313a95ae458ba600ea5de14811" |
| 377 | ), |
| 378 | template!( |
| 379 | "tulips", |
| 380 | jpeg, |
| 381 | "5b321a4d81bd499b289b1755f6450a42047c494dfbc112dbd56da4ced2c15c1a" |
| 382 | ), |
| 383 | template!( |
| 384 | "window-reflection", |
| 385 | jpeg, |
| 386 | "494547e566fb7a63dd429eb0699fe41aa8998f8ea2f758d813fe3d56c3075719" |
| 387 | ), |
| 388 | template!( |
| 389 | "writing", |
| 390 | jpeg, |
| 391 | "5a36960df32817e8426bd40a88f88b04fb55b84baef60f1e71e0872217fdb134" |
| 392 | ), |
| 393 | ]; |
| 394 | |
| 395 | /// Names the template a picture Snowbound wrote renders: a PNG `tEXt` keyword, and the |
| 396 | /// prefix of a JPEG comment. |
| 397 | const MARKER: &str = "Snowbound template"; |
| 398 | |
| 399 | /// A raster holds at most this many bytes; page-sized art stops sharpening past about 2.9 |
| 400 | /// device pixels per point. |
| 401 | const MAX_RASTER_BYTES: f32 = 16.0 * 1024.0 * 1024.0; |
| 402 | |
| 403 | /// Art an edit brings draws in its first frame in at most this many pixels, a few |
| 404 | /// milliseconds' work, then sharpens. |
| 405 | const INLINE_PIXELS: f32 = 1024.0 * 1024.0; |
| 406 | |
| 407 | /// Template art standing in for a page's background picture. |
| 408 | pub(super) struct Background { |
| 409 | svg: &'static str, |
| 410 | /// Points; OneNote draws a picture without a stored size at its natural size. |
| 411 | pub(crate) size: [f32; 2], |
| 412 | /// For light and for dark paper. |
| 413 | variants: [Variant; 2], |
| 414 | /// Whether the first raster is made on the frame's thread rather than awaited. |
| 415 | pub(crate) inline: bool, |
| 416 | } |
| 417 | |
| 418 | /// A raster's device pixels per point, and the dark paper it was recoloured for. |
| 419 | type Made = (f32, Option<[f32; 4]>); |
| 420 | |
| 421 | #[derive(Default)] |
| 422 | struct Variant { |
| 423 | /// The raster painted and what it was made for. |
| 424 | shown: Option<(RasterImage, Made)>, |
| 425 | /// The one raster being made and what for; replacing it abandons it. |
| 426 | pending: Option<(Slot<RasterImage>, Made)>, |
| 427 | } |
| 428 | |
| 429 | impl Background { |
| 430 | /// The rasters made so far, for light and for dark paper. |
| 431 | pub(crate) fn images(&self) -> impl Iterator<Item = &RasterImage> { |
| 432 | self.variants |
| 433 | .iter() |
| 434 | .filter_map(|variant| variant.shown.as_ref().map(|(image, _)| image)) |
| 435 | } |
| 436 | |
| 437 | /// Whether both recreate the same template picture at the same size. |
| 438 | pub(crate) fn same(&self, other: &Self) -> bool { |
| 439 | self.svg == other.svg && self.size == other.size |
| 440 | } |
| 441 | |
| 442 | /// OneNote's template pictures by their hash, and those Snowbound wrote by their marker. |
| 443 | pub(crate) fn recognise(picture: &onestore::page::Image) -> Option<Self> { |
| 444 | let bytes = picture.bytes.as_deref()?; |
| 445 | let digest: String = Sha256::digest(bytes) |
| 446 | .iter() |
| 447 | .map(|byte| format!("{byte:02x}")) |
| 448 | .collect(); |
| 449 | let template = TEMPLATES |
| 450 | .iter() |
| 451 | .find(|template| template.hashes.contains(&digest.as_str())) |
| 452 | .or_else(|| { |
| 453 | let name = marker(bytes)?; |
| 454 | TEMPLATES.iter().find(|template| template.name == name) |
| 455 | })?; |
| 456 | let svg = template.svg; |
| 457 | let size = match (picture.layout.max_width, picture.layout.max_height) { |
| 458 | (Some(width), Some(height)) => [width, height], |
| 459 | _ => { |
| 460 | let size = parse(svg).size(); |
| 461 | [size.width() * 0.75, size.height() * 0.75] |
| 462 | } |
| 463 | }; |
| 464 | Some(Self { |
| 465 | svg, |
| 466 | size, |
| 467 | variants: Default::default(), |
| 468 | inline: false, |
| 469 | }) |
| 470 | } |
| 471 | |
| 472 | /// The latest raster for this paper, if one has landed. |
| 473 | pub(crate) fn image(&self, paper: Paper) -> Option<&RasterImage> { |
| 474 | let (image, _) = self.variants[usize::from(dark(paper))].shown.as_ref()?; |
| 475 | Some(image) |
| 476 | } |
| 477 | |
| 478 | /// Takes a finished raster and asks the worker for one at `scale` device pixels per |
| 479 | /// point when the shown one differs; `waker` hears when it lands. True once the raster |
| 480 | /// shown matches. |
| 481 | pub(crate) fn update(&mut self, paper: Paper, scale: f32, waker: &Waker) -> bool { |
| 482 | let dark = dark(paper); |
| 483 | let [width, height] = self.size.map(|side| side.max(1.0)); |
| 484 | let density = density(scale) |
| 485 | .min((MAX_RASTER_BYTES / (4.0 * width * height)).sqrt()) |
| 486 | .min(4096.0 / width.max(height)); |
| 487 | let made = (density, dark.then_some(paper.color)); |
| 488 | let svg = self.svg; |
| 489 | let paper = dark.then_some(paper); |
| 490 | let raster = move |density: f32| { |
| 491 | let size = [width, height].map(|side| (side * density).round().max(1.0) as u32); |
| 492 | move || rasterize(svg, paper, size) |
| 493 | }; |
| 494 | let variant = &mut self.variants[usize::from(dark)]; |
| 495 | let landed = variant |
| 496 | .pending |
| 497 | .as_ref() |
| 498 | .and_then(|(slot, made)| Some((slot.lock().unwrap().take()?, *made))); |
| 499 | if let Some((image, landed)) = landed { |
| 500 | if landed == made || variant.shown.is_none() { |
| 501 | variant.shown = Some((image, landed)); |
| 502 | } |
| 503 | variant.pending = None; |
| 504 | } |
| 505 | if variant.shown.is_none() && std::mem::take(&mut self.inline) { |
| 506 | let quick = density.min((INLINE_PIXELS / (width * height)).sqrt()); |
| 507 | variant.shown = Some((raster(quick)(), (quick, made.1))); |
| 508 | } |
| 509 | let shown = variant.shown.as_ref().map(|(_, made)| *made); |
| 510 | if shown != Some(made) && variant.pending.as_ref().map(|(_, made)| *made) != Some(made) { |
| 511 | let start = Instant::now() |
| 512 | + if shown.is_some() { |
| 513 | SETTLE |
| 514 | } else { |
| 515 | Duration::ZERO |
| 516 | }; |
| 517 | let slot = queue(start, waker, raster(density)); |
| 518 | variant.pending = Some((slot, made)); |
| 519 | } |
| 520 | shown == Some(made) |
| 521 | } |
| 522 | } |
| 523 | |
| 524 | pub(super) fn dark(paper: Paper) -> bool { |
| 525 | let [lightness, ..] = draw::oklab(paper.color); |
| 526 | let [ink, ..] = draw::oklab(paper.ink); |
| 527 | ink > lightness |
| 528 | } |
| 529 | |
| 530 | fn rasterize(svg: &str, dark: Option<Paper>, size: [u32; 2]) -> RasterImage { |
| 531 | let pixels = match dark { |
| 532 | Some(paper) => render(&onto(svg, paper), size), |
| 533 | None => render(svg, size), |
| 534 | }; |
| 535 | RasterImage::new(size, pixels).expect("Art is within the image budget") |
| 536 | } |
| 537 | |
| 538 | /// `svg` as a PNG `side` pixels square. |
| 539 | pub(super) fn png(svg: &str, side: u32) -> Vec<u8> { |
| 540 | let mut encoded = Vec::new(); |
| 541 | let mut encoder = png::Encoder::new(&mut encoded, side, side); |
| 542 | encoder.set_color(png::ColorType::Rgba); |
| 543 | encoder.set_depth(png::BitDepth::Eight); |
| 544 | encoder |
| 545 | .write_header() |
| 546 | .and_then(|mut writer| writer.write_image_data(&render(svg, [side; 2]))) |
| 547 | .expect("A PNG encodes into memory"); |
| 548 | encoded |
| 549 | } |
| 550 | |
| 551 | /// Straight-alpha RGBA rows of `svg` stretched to `width` by `height` pixels. |
| 552 | fn render(svg: &str, [width, height]: [u32; 2]) -> Vec<u8> { |
| 553 | let tree = parse(svg); |
| 554 | let mut pixmap = tiny_skia::Pixmap::new(width, height).expect("Art has a size"); |
| 555 | let view = tree.size(); |
| 556 | resvg::render( |
| 557 | &tree, |
| 558 | tiny_skia::Transform::from_scale( |
| 559 | width as f32 / view.width(), |
| 560 | height as f32 / view.height(), |
| 561 | ), |
| 562 | &mut pixmap.as_mut(), |
| 563 | ); |
| 564 | pixmap |
| 565 | .pixels() |
| 566 | .iter() |
| 567 | .flat_map(|pixel| { |
| 568 | let color = pixel.demultiply(); |
| 569 | [color.red(), color.green(), color.blue(), color.alpha()] |
| 570 | }) |
| 571 | .collect() |
| 572 | } |
| 573 | |
| 574 | /// Template art `name` for a preview: drawn `size` points big, or at its natural size, at |
| 575 | /// `density` pixels per point for `paper`, with the size in points it was drawn at. |
| 576 | pub fn template_art( |
| 577 | name: &str, |
| 578 | size: Option<[f32; 2]>, |
| 579 | density: f32, |
| 580 | paper: Paper, |
| 581 | ) -> Option<(RasterImage, [f32; 2])> { |
| 582 | let template = TEMPLATES.iter().find(|template| template.name == name)?; |
| 583 | let size = size.unwrap_or_else(|| { |
| 584 | let view = parse(template.svg).size(); |
| 585 | [view.width() * 0.75, view.height() * 0.75] |
| 586 | }); |
| 587 | let pixels = size.map(|side| (side * density).round().max(1.0) as u32); |
| 588 | Some(( |
| 589 | rasterize(template.svg, dark(paper).then_some(paper), pixels), |
| 590 | size, |
| 591 | )) |
| 592 | } |
| 593 | |
| 594 | /// Template `name`'s art as Snowbound stores it for a page: in the format and pixel size of |
| 595 | /// OneNote's picture, marked so that recognising it paints the art. |
| 596 | pub fn template_picture(name: &str) -> Option<Vec<u8>> { |
| 597 | let template = TEMPLATES.iter().find(|template| template.name == name)?; |
| 598 | let view = parse(template.svg).size(); |
| 599 | let [width, height] = [view.width(), view.height()].map(|side| side.round() as u32); |
| 600 | // Over white paper, as OneNote's pictures are drawn. |
| 601 | let rgb: Vec<u8> = render(template.svg, [width, height]) |
| 602 | .chunks_exact(4) |
| 603 | .flat_map(|pixel| { |
| 604 | let alpha = u16::from(pixel[3]); |
| 605 | [0, 1, 2].map(|channel| { |
| 606 | ((u16::from(pixel[channel]) * alpha + 255 * (255 - alpha) + 127) / 255) as u8 |
| 607 | }) |
| 608 | }) |
| 609 | .collect(); |
| 610 | let mut encoded = Vec::new(); |
| 611 | if template.jpeg { |
| 612 | image::codecs::jpeg::JpegEncoder::new_with_quality(&mut encoded, 90) |
| 613 | .encode(&rgb, width, height, image::ExtendedColorType::Rgb8) |
| 614 | .expect("Art encodes"); |
| 615 | // The comment follows the JFIF header, which must lead. |
| 616 | let [_, _, _, _, high, low, ..] = encoded[..] else { |
| 617 | unreachable!("A JPEG starts with its JFIF header") |
| 618 | }; |
| 619 | let at = 4 + usize::from(u16::from_be_bytes([high, low])); |
| 620 | let comment = format!("{MARKER} {name}"); |
| 621 | let length = u16::try_from(comment.len() + 2).expect("A short comment"); |
| 622 | let segment = [&[0xff, 0xfe][..], &length.to_be_bytes(), comment.as_bytes()].concat(); |
| 623 | encoded.splice(at..at, segment); |
| 624 | } else { |
| 625 | // OneNote's PNG templates hold 256 colours. |
| 626 | let rgba: Vec<u8> = rgb |
| 627 | .chunks_exact(3) |
| 628 | .flat_map(|pixel| [pixel[0], pixel[1], pixel[2], 255]) |
| 629 | .collect(); |
| 630 | let quantized = color_quant::NeuQuant::new(10, 256, &rgba); |
| 631 | let indices: Vec<u8> = rgba |
| 632 | .chunks_exact(4) |
| 633 | .map(|pixel| quantized.index_of(pixel) as u8) |
| 634 | .collect(); |
| 635 | let mut encoder = png::Encoder::new(&mut encoded, width, height); |
| 636 | encoder.set_color(png::ColorType::Indexed); |
| 637 | encoder.set_depth(png::BitDepth::Eight); |
| 638 | encoder.set_palette(quantized.color_map_rgb()); |
| 639 | encoder.set_compression(png::Compression::High); |
| 640 | // Palette indices compress best unfiltered. |
| 641 | encoder.set_filter(png::Filter::NoFilter); |
| 642 | encoder |
| 643 | .add_text_chunk(MARKER.into(), name.into()) |
| 644 | .expect("A Latin-1 keyword"); |
| 645 | let mut writer = encoder.write_header().expect("Art encodes"); |
| 646 | writer.write_image_data(&indices).expect("Art encodes"); |
| 647 | writer.finish().expect("Art encodes"); |
| 648 | } |
| 649 | Some(encoded) |
| 650 | } |
| 651 | |
| 652 | /// The template a picture Snowbound wrote names: a PNG's text chunk before its image data, |
| 653 | /// or a JPEG's comment before its scan. |
| 654 | fn marker(picture: &[u8]) -> Option<&str> { |
| 655 | fn named(text: &[u8], separator: u8) -> Option<&str> { |
| 656 | let name = text |
| 657 | .strip_prefix(MARKER.as_bytes())? |
| 658 | .strip_prefix(&[separator])?; |
| 659 | std::str::from_utf8(name).ok() |
| 660 | } |
| 661 | if let Some(mut rest) = picture.strip_prefix(b"\x89PNG\r\n\x1a\n") { |
| 662 | while let [a, b, c, d, kind @ ..] = rest { |
| 663 | let length = u32::from_be_bytes([*a, *b, *c, *d]) as usize; |
| 664 | match kind.get(..4)? { |
| 665 | b"IDAT" => return None, |
| 666 | b"tEXt" => { |
| 667 | if let Some(name) = named(kind.get(4..4 + length)?, 0) { |
| 668 | return Some(name); |
| 669 | } |
| 670 | } |
| 671 | _ => {} |
| 672 | } |
| 673 | rest = rest.get(12 + length..)?; |
| 674 | } |
| 675 | return None; |
| 676 | } |
| 677 | let mut rest = picture.strip_prefix(&[0xff, 0xd8])?; |
| 678 | while let [0xff, kind, high, low, ..] = *rest { |
| 679 | let length = usize::from(u16::from_be_bytes([high, low])); |
| 680 | match kind { |
| 681 | 0xda => return None, |
| 682 | 0xfe => { |
| 683 | if let Some(name) = named(rest.get(4..2 + length)?, b' ') { |
| 684 | return Some(name); |
| 685 | } |
| 686 | } |
| 687 | _ => {} |
| 688 | } |
| 689 | rest = rest.get(2 + length..)?; |
| 690 | } |
| 691 | None |
| 692 | } |
| 693 | |
| 694 | fn parse(svg: &str) -> usvg::Tree { |
| 695 | usvg::Tree::from_str(svg, &usvg::Options::default()).expect("Bundled template art is valid") |
| 696 | } |
| 697 | |
| 698 | /// Recolours `#rrggbb` art drawn for white paper onto dark `paper`: lightness turns about |
| 699 | /// the paper towards its ink, easing off so the art stays behind the text, and hue and |
| 700 | /// chroma carry over. Keywords, such as a mask's `white`, stay as they are. |
| 701 | fn onto(svg: &str, paper: Paper) -> String { |
| 702 | let [lightness, a, b] = draw::oklab(paper.color); |
| 703 | let [ink, ..] = draw::oklab(paper.ink); |
| 704 | let mut pieces = svg.split('#'); |
| 705 | let mut recoloured = pieces.next().unwrap_or_default().to_owned(); |
| 706 | for piece in pieces { |
| 707 | let color = piece |
| 708 | .get(..6) |
| 709 | .filter(|_| !piece[6..].starts_with(|c: char| c.is_ascii_alphanumeric())) |
| 710 | .and_then(|hex| u32::from_str_radix(hex, 16).ok()); |
| 711 | let Some(color) = color else { |
| 712 | recoloured.push('#'); |
| 713 | recoloured.push_str(piece); |
| 714 | continue; |
| 715 | }; |
| 716 | let [_, red, green, blue] = color.to_be_bytes(); |
| 717 | let [l, ca, cb] = draw::oklab(draw::srgb(red, green, blue)); |
| 718 | let turned = lightness + 0.7 * (1.0 - l).max(0.0).sqrt() * (ink - lightness); |
| 719 | let [red, green, blue] = draw::from_oklab([turned, a + ca, b + cb]).map(|value| { |
| 720 | let encoded = if value <= 0.003_130_8 { |
| 721 | value * 12.92 |
| 722 | } else { |
| 723 | 1.055 * value.powf(1.0 / 2.4) - 0.055 |
| 724 | }; |
| 725 | (encoded * 255.0).round() as u8 |
| 726 | }); |
| 727 | recoloured.push_str(&format!("#{red:02x}{green:02x}{blue:02x}")); |
| 728 | recoloured.push_str(&piece[6..]); |
| 729 | } |
| 730 | recoloured |
| 731 | } |
| 732 | |
| 733 | #[cfg(test)] |
| 734 | mod tests { |
| 735 | use super::*; |
| 736 | use crate::{gpu::page::PageScene, layout::TextEngine}; |
| 737 | use onestore::{ |
| 738 | ExGuid, |
| 739 | document::Layout, |
| 740 | page::{Image, Page, PageObject}, |
| 741 | }; |
| 742 | use std::{collections::BTreeMap, sync::Arc}; |
| 743 | |
| 744 | const DARK: Paper = Paper { |
| 745 | color: [0.0137, 0.0144, 0.0159, 1.0], |
| 746 | ink: [0.791, 0.791, 0.791, 1.0], |
| 747 | }; |
| 748 | |
| 749 | fn picture(bytes: &[u8]) -> Image { |
| 750 | Image { |
| 751 | id: ExGuid::default(), |
| 752 | layout: Layout { |
| 753 | x: Some(0.0), |
| 754 | y: Some(0.0), |
| 755 | max_width: Some(64.0), |
| 756 | max_height: Some(48.0), |
| 757 | ..Default::default() |
| 758 | }, |
| 759 | size: None, |
| 760 | bytes: Some(Arc::from(bytes)), |
| 761 | display: None, |
| 762 | alt: None, |
| 763 | background: true, |
| 764 | printout: None, |
| 765 | tags: Vec::new(), |
| 766 | link: None, |
| 767 | text: None, |
| 768 | } |
| 769 | } |
| 770 | |
| 771 | fn red_png() -> Vec<u8> { |
| 772 | let mut encoded = Vec::new(); |
| 773 | let mut encoder = png::Encoder::new(&mut encoded, 1, 1); |
| 774 | encoder.set_color(png::ColorType::Rgba); |
| 775 | encoder.set_depth(png::BitDepth::Eight); |
| 776 | encoder |
| 777 | .write_header() |
| 778 | .unwrap() |
| 779 | .write_image_data(&[255, 0, 0, 255]) |
| 780 | .unwrap(); |
| 781 | encoded |
| 782 | } |
| 783 | |
| 784 | fn ivy() -> Background { |
| 785 | Background { |
| 786 | svg: TEMPLATES.iter().find(|t| t.name == "ivy").unwrap().svg, |
| 787 | size: [64.0, 48.0], |
| 788 | variants: Default::default(), |
| 789 | inline: false, |
| 790 | } |
| 791 | } |
| 792 | |
| 793 | fn settle(mut update: impl FnMut() -> bool) { |
| 794 | let give_up = Instant::now() + Duration::from_secs(10); |
| 795 | while !update() { |
| 796 | assert!(Instant::now() < give_up, "the worker never delivered"); |
| 797 | std::thread::sleep(Duration::from_millis(2)); |
| 798 | } |
| 799 | } |
| 800 | |
| 801 | #[test] |
| 802 | fn rasters_land_from_the_worker_and_follow_the_zoom() { |
| 803 | let mut art = ivy(); |
| 804 | assert!(!art.update(DARK, 1.0, Waker::noop())); |
| 805 | assert!(art.image(DARK).is_none()); |
| 806 | settle(|| art.update(DARK, 1.0, Waker::noop())); |
| 807 | let size = |art: &Background| art.image(DARK).map(|image| image.pixels().len() / 4); |
| 808 | assert_eq!(size(&art), Some(64 * 48)); |
| 809 | assert!( |
| 810 | art.image(Paper::WHITE).is_none(), |
| 811 | "each paper has its own raster" |
| 812 | ); |
| 813 | // Zooming keeps the last raster up while one for the new scale is made, and a |
| 814 | // newer scale abandons the older request. |
| 815 | assert!(!art.update(DARK, 4.0, Waker::noop())); |
| 816 | let abandoned = Arc::downgrade(&art.variants[1].pending.as_ref().unwrap().0); |
| 817 | assert!(!art.update(DARK, 2.0, Waker::noop())); |
| 818 | assert!(abandoned.upgrade().is_none()); |
| 819 | assert_eq!(size(&art), Some(64 * 48)); |
| 820 | settle(|| art.update(DARK, 2.0, Waker::noop())); |
| 821 | assert_eq!(size(&art), Some(128 * 96)); |
| 822 | assert!(art.variants[1].pending.is_none()); |
| 823 | } |
| 824 | |
| 825 | #[test] |
| 826 | fn every_recreation_parses_for_light_and_dark_paper_under_distinct_names_and_hashes() { |
| 827 | let mut names = std::collections::BTreeSet::new(); |
| 828 | let mut hashes = std::collections::BTreeSet::new(); |
| 829 | for template in TEMPLATES { |
| 830 | assert!( |
| 831 | names.insert(template.name), |
| 832 | "{} is listed twice", |
| 833 | template.name |
| 834 | ); |
| 835 | for hash in template.hashes { |
| 836 | assert!( |
| 837 | hash.len() == 64 |
| 838 | && hash.bytes().all(|b| matches!(b, b'0'..=b'9' | b'a'..=b'f')) |
| 839 | ); |
| 840 | assert!(hashes.insert(hash), "{hash} is listed twice"); |
| 841 | } |
| 842 | for svg in [template.svg.to_string(), onto(template.svg, DARK)] { |
| 843 | usvg::Tree::from_str(&svg, &usvg::Options::default()).unwrap(); |
| 844 | } |
| 845 | } |
| 846 | } |
| 847 | |
| 848 | #[test] |
| 849 | fn every_template_s_art_is_recreated() { |
| 850 | for art in crate::template::TEMPLATES.iter().flat_map(|t| t.art) { |
| 851 | assert!(TEMPLATES.iter().any(|t| t.name == art.art), "{}", art.art); |
| 852 | } |
| 853 | } |
| 854 | |
| 855 | #[test] |
| 856 | fn our_template_pictures_are_recognised_as_the_template() { |
| 857 | for (name, magic, size) in [ |
| 858 | ("ivy", &b"\xff\xd8\xff\xe0"[..], [216, 792]), |
| 859 | ("hearts", &b"\x89PNG"[..], [171, 552]), |
| 860 | ] { |
| 861 | let bytes = template_picture(name).unwrap(); |
| 862 | assert!(bytes.starts_with(magic), "{name} keeps OneNote's format"); |
| 863 | assert_eq!(draw::RasterImage::measure(&bytes).unwrap(), size); |
| 864 | assert_eq!(marker(&bytes), Some(name)); |
| 865 | let art = Background::recognise(&picture(&bytes)).unwrap(); |
| 866 | let template = TEMPLATES.iter().find(|t| t.name == name).unwrap(); |
| 867 | assert_eq!(art.svg, template.svg); |
| 868 | assert!(!template.hashes.iter().any(|hash| { |
| 869 | let digest: String = Sha256::digest(&bytes) |
| 870 | .iter() |
| 871 | .map(|b| format!("{b:02x}")) |
| 872 | .collect(); |
| 873 | digest == *hash |
| 874 | })); |
| 875 | } |
| 876 | assert!(template_picture("not a template").is_none()); |
| 877 | assert_eq!(marker(&red_png()), None); |
| 878 | } |
| 879 | |
| 880 | #[test] |
| 881 | fn the_sample_notebook_carries_our_ivy() { |
| 882 | let bytes = std::fs::read("../../docs/sample-notebook/Personal/Garden.one").unwrap(); |
| 883 | let store = onestore::Store::parse(&bytes).unwrap(); |
| 884 | let index = onestore::RevisionIndex::parse(&store).unwrap(); |
| 885 | let document = onestore::document::Document::parse(&index).unwrap(); |
| 886 | let mut backgrounds = 0; |
| 887 | for (space, id) in document.pages().unwrap() { |
| 888 | let page = Page::from_revision(document.active(space).unwrap(), id).unwrap(); |
| 889 | for object in &page.objects { |
| 890 | if let PageObject::Image(image) = object |
| 891 | && image.background |
| 892 | { |
| 893 | assert_eq!(marker(image.bytes.as_deref().unwrap()), Some("ivy")); |
| 894 | assert!(Background::recognise(image).is_some()); |
| 895 | backgrounds += 1; |
| 896 | } |
| 897 | } |
| 898 | } |
| 899 | assert_eq!(backgrounds, 1); |
| 900 | } |
| 901 | |
| 902 | #[test] |
| 903 | fn other_pictures_keep_their_own_bytes() { |
| 904 | assert!(Background::recognise(&picture(&red_png())).is_none()); |
| 905 | assert!( |
| 906 | Background::recognise(&Image { |
| 907 | bytes: None, |
| 908 | ..picture(&[]) |
| 909 | }) |
| 910 | .is_none() |
| 911 | ); |
| 912 | } |
| 913 | |
| 914 | #[test] |
| 915 | fn dark_paper_takes_white_to_the_paper_and_keeps_hue() { |
| 916 | assert_eq!(onto(r##"fill="#ffffff""##, DARK), r##"fill="#1f2022""##); |
| 917 | assert_eq!( |
| 918 | onto("href=\"#beads\" stop-color=\"white\"", DARK), |
| 919 | "href=\"#beads\" stop-color=\"white\"" |
| 920 | ); |
| 921 | let leaf = onto("#8fce73", DARK); |
| 922 | let [red, green, blue] = |
| 923 | [1, 3, 5].map(|at| u8::from_str_radix(&leaf[at..at + 2], 16).unwrap()); |
| 924 | assert!(green > red && green > blue && green < 0xce, "{leaf}"); |
| 925 | } |
| 926 | |
| 927 | #[test] |
| 928 | #[ignore = "requires CANVAS_TEST_SECTION pointing to a section whose pages use OneNote's page templates"] |
| 929 | fn recognises_template_backgrounds_in_a_section() { |
| 930 | let bytes = std::fs::read(std::env::var("CANVAS_TEST_SECTION").unwrap()).unwrap(); |
| 931 | let store = onestore::Store::parse(&bytes).unwrap(); |
| 932 | let index = onestore::RevisionIndex::parse(&store).unwrap(); |
| 933 | let document = onestore::document::Document::parse(&index).unwrap(); |
| 934 | let mut recognised = 0; |
| 935 | for (space, id) in document.pages().unwrap() { |
| 936 | let page = Page::from_revision(document.active(space).unwrap(), id).unwrap(); |
| 937 | for object in &page.objects { |
| 938 | if let PageObject::Image(image) = object |
| 939 | && image.background |
| 940 | { |
| 941 | assert!(Background::recognise(image).is_some(), "{:?}", image.id); |
| 942 | recognised += 1; |
| 943 | } |
| 944 | } |
| 945 | } |
| 946 | assert!(recognised > 0); |
| 947 | } |
| 948 | |
| 949 | #[test] |
| 950 | #[ignore = "requires a native GPU adapter"] |
| 951 | fn dark_paper_paints_the_dark_recreation_instead_of_the_embedded_picture() { |
| 952 | let background = picture(&red_png()); |
| 953 | let id = background.id; |
| 954 | let page = Page { |
| 955 | identity: None, |
| 956 | created: None, |
| 957 | title: String::new(), |
| 958 | margin_origin: [0.0; 2], |
| 959 | rtl: false, |
| 960 | color: None, |
| 961 | rule_lines: None, |
| 962 | definitions: BTreeMap::new(), |
| 963 | objects: vec![PageObject::Image(background)], |
| 964 | }; |
| 965 | let mut scene = PageScene::new(page, &mut TextEngine::default()).unwrap(); |
| 966 | scene.backgrounds.insert(id, ivy()); |
| 967 | let mut before = Vec::new(); |
| 968 | scene |
| 969 | .append_primitives(&mut before, [0.0; 2], DARK) |
| 970 | .unwrap(); |
| 971 | assert!(before.is_empty(), "the paper shows until the raster lands"); |
| 972 | drop(before); |
| 973 | scene.settle(None, 1.0, DARK); |
| 974 | let mut primitives = Vec::new(); |
| 975 | scene |
| 976 | .append_primitives(&mut primitives, [0.0; 2], DARK) |
| 977 | .unwrap(); |
| 978 | |
| 979 | let instance = wgpu::Instance::new(wgpu::InstanceDescriptor::new_without_display_handle()); |
| 980 | let adapter = pollster::block_on(instance.request_adapter(&Default::default())).unwrap(); |
| 981 | let (device, queue) = |
| 982 | pollster::block_on(adapter.request_device(&Default::default())).unwrap(); |
| 983 | let size = [64_u32, 48]; |
| 984 | let target = device.create_texture(&wgpu::TextureDescriptor { |
| 985 | label: Some("Background readback test"), |
| 986 | size: wgpu::Extent3d { |
| 987 | width: size[0], |
| 988 | height: size[1], |
| 989 | depth_or_array_layers: 1, |
| 990 | }, |
| 991 | mip_level_count: 1, |
| 992 | sample_count: 1, |
| 993 | dimension: wgpu::TextureDimension::D2, |
| 994 | format: wgpu::TextureFormat::Rgba8UnormSrgb, |
| 995 | usage: wgpu::TextureUsages::RENDER_ATTACHMENT | wgpu::TextureUsages::COPY_SRC, |
| 996 | view_formats: &[], |
| 997 | }); |
| 998 | let readback = device.create_buffer(&wgpu::BufferDescriptor { |
| 999 | label: Some("Background readback"), |
| 1000 | size: u64::from(size[0] * size[1] * 4), |
| 1001 | usage: wgpu::BufferUsages::MAP_READ | wgpu::BufferUsages::COPY_DST, |
| 1002 | mapped_at_creation: false, |
| 1003 | }); |
| 1004 | let mut renderer = draw::Renderer::new(device, queue, wgpu::TextureFormat::Rgba8UnormSrgb); |
| 1005 | let viewport = crate::gpu::Viewport { |
| 1006 | size, |
| 1007 | scale: 1.0, |
| 1008 | origin: [0.0; 2], |
| 1009 | }; |
| 1010 | renderer |
| 1011 | .draw( |
| 1012 | &target.create_view(&Default::default()).into(), |
| 1013 | size, |
| 1014 | DARK.color, |
| 1015 | &[viewport.layer(&primitives)], |
| 1016 | ) |
| 1017 | .unwrap(); |
| 1018 | let mut encoder = renderer |
| 1019 | .device() |
| 1020 | .create_command_encoder(&Default::default()); |
| 1021 | encoder.copy_texture_to_buffer( |
| 1022 | target.as_image_copy(), |
| 1023 | wgpu::TexelCopyBufferInfo { |
| 1024 | buffer: &readback, |
| 1025 | layout: wgpu::TexelCopyBufferLayout { |
| 1026 | offset: 0, |
| 1027 | bytes_per_row: Some(size[0] * 4), |
| 1028 | rows_per_image: Some(size[1]), |
| 1029 | }, |
| 1030 | }, |
| 1031 | wgpu::Extent3d { |
| 1032 | width: size[0], |
| 1033 | height: size[1], |
| 1034 | depth_or_array_layers: 1, |
| 1035 | }, |
| 1036 | ); |
| 1037 | renderer.queue().submit([encoder.finish()]); |
| 1038 | readback.map_async(wgpu::MapMode::Read, .., |result| result.unwrap()); |
| 1039 | renderer |
| 1040 | .device() |
| 1041 | .poll(wgpu::PollType::Wait { |
| 1042 | submission_index: None, |
| 1043 | timeout: Some(Duration::from_secs(5)), |
| 1044 | }) |
| 1045 | .unwrap(); |
| 1046 | let pixels = readback.get_mapped_range(..).unwrap().to_vec(); |
| 1047 | let pixels: Vec<[u8; 3]> = pixels.chunks_exact(4).map(|p| [p[0], p[1], p[2]]).collect(); |
| 1048 | assert!( |
| 1049 | pixels |
| 1050 | .iter() |
| 1051 | .all(|[r, g, b]| !(*r > 200 && *g < 80 && *b < 80)), |
| 1052 | "the embedded picture was painted" |
| 1053 | ); |
| 1054 | // Leaves on dark paper stay green, darker than they are on white. |
| 1055 | assert!( |
| 1056 | pixels |
| 1057 | .iter() |
| 1058 | .any(|[r, g, b]| g > r && g > b && *g > 0x40 && *g < 0xce), |
| 1059 | "the dark recreation was not painted" |
| 1060 | ); |
| 1061 | assert!( |
| 1062 | pixels.iter().all(|[_, g, _]| *g < 0xe0), |
| 1063 | "the art kept white paper" |
| 1064 | ); |
| 1065 | } |
| 1066 | } |