@clockgrove/assetgrove 0.0.0-stage → 0.3.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (622) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +277 -2
  3. package/dist/animation/commands.d.ts +180 -0
  4. package/dist/animation/commands.js +515 -0
  5. package/dist/animation/facing.d.ts +44 -0
  6. package/dist/animation/facing.js +100 -0
  7. package/dist/animation/fix.d.ts +136 -0
  8. package/dist/animation/fix.js +529 -0
  9. package/dist/animation/flicker.d.ts +44 -0
  10. package/dist/animation/flicker.js +117 -0
  11. package/dist/animation/halo.d.ts +72 -0
  12. package/dist/animation/halo.js +253 -0
  13. package/dist/animation/holes.d.ts +122 -0
  14. package/dist/animation/holes.js +262 -0
  15. package/dist/animation/preview.d.ts +38 -0
  16. package/dist/animation/preview.js +161 -0
  17. package/dist/animation/qa.d.ts +57 -0
  18. package/dist/animation/qa.js +418 -0
  19. package/dist/animation/run.d.ts +109 -0
  20. package/dist/animation/run.js +182 -0
  21. package/dist/animation/strip.d.ts +13 -0
  22. package/dist/animation/strip.js +41 -0
  23. package/dist/animation/template-choice.d.ts +75 -0
  24. package/dist/animation/template-choice.js +153 -0
  25. package/dist/animation/templates.d.ts +39 -0
  26. package/dist/animation/templates.js +333 -0
  27. package/dist/animation/types.d.ts +257 -0
  28. package/dist/animation/types.js +1 -0
  29. package/dist/approvals.d.ts +156 -0
  30. package/dist/approvals.js +369 -0
  31. package/dist/audio/aac.d.ts +61 -0
  32. package/dist/audio/aac.js +152 -0
  33. package/dist/audio/checks.d.ts +38 -0
  34. package/dist/audio/checks.js +254 -0
  35. package/dist/audio/delivery.d.ts +52 -0
  36. package/dist/audio/delivery.js +105 -0
  37. package/dist/audio/engine.d.ts +65 -0
  38. package/dist/audio/engine.js +164 -0
  39. package/dist/audio/handoff.d.ts +75 -0
  40. package/dist/audio/handoff.js +185 -0
  41. package/dist/audio/kinds.d.ts +6 -0
  42. package/dist/audio/kinds.js +315 -0
  43. package/dist/audio/loudness.d.ts +22 -0
  44. package/dist/audio/loudness.js +72 -0
  45. package/dist/audio/ogg.d.ts +27 -0
  46. package/dist/audio/ogg.js +65 -0
  47. package/dist/audio/operations.d.ts +20 -0
  48. package/dist/audio/operations.js +335 -0
  49. package/dist/audio/opus.d.ts +40 -0
  50. package/dist/audio/opus.js +108 -0
  51. package/dist/audio/profile.d.ts +200 -0
  52. package/dist/audio/profile.js +126 -0
  53. package/dist/audio/run.d.ts +83 -0
  54. package/dist/audio/run.js +192 -0
  55. package/dist/audio/vorbis-comments.d.ts +25 -0
  56. package/dist/audio/vorbis-comments.js +243 -0
  57. package/dist/audio/waveform.d.ts +13 -0
  58. package/dist/audio/waveform.js +52 -0
  59. package/dist/audio-parameters.d.ts +17 -0
  60. package/dist/audio-parameters.js +45 -0
  61. package/dist/audit/bar.d.ts +65 -0
  62. package/dist/audit/bar.js +97 -0
  63. package/dist/audit/members.d.ts +40 -0
  64. package/dist/audit/members.js +119 -0
  65. package/dist/bible-view.d.ts +10 -0
  66. package/dist/bible-view.js +122 -0
  67. package/dist/bible.d.ts +272 -0
  68. package/dist/bible.js +641 -0
  69. package/dist/budget.d.ts +119 -0
  70. package/dist/budget.js +227 -0
  71. package/dist/call-parameters.d.ts +71 -0
  72. package/dist/call-parameters.js +90 -0
  73. package/dist/calls.d.ts +145 -0
  74. package/dist/calls.js +788 -0
  75. package/dist/cli/main.d.ts +22 -0
  76. package/dist/cli/main.js +137 -0
  77. package/dist/cli.d.ts +2 -0
  78. package/dist/cli.js +5 -0
  79. package/dist/commands/animation-qa.d.ts +1 -0
  80. package/dist/commands/animation-qa.js +240 -0
  81. package/dist/commands/apply.d.ts +12 -0
  82. package/dist/commands/apply.js +181 -0
  83. package/dist/commands/audit.d.ts +92 -0
  84. package/dist/commands/audit.js +555 -0
  85. package/dist/commands/bible.d.ts +1 -0
  86. package/dist/commands/bible.js +39 -0
  87. package/dist/commands/browse.d.ts +12 -0
  88. package/dist/commands/browse.js +113 -0
  89. package/dist/commands/card.d.ts +3 -0
  90. package/dist/commands/card.js +445 -0
  91. package/dist/commands/compare.d.ts +1 -0
  92. package/dist/commands/compare.js +90 -0
  93. package/dist/commands/confirm.d.ts +1 -0
  94. package/dist/commands/confirm.js +267 -0
  95. package/dist/commands/context.d.ts +43 -0
  96. package/dist/commands/context.js +115 -0
  97. package/dist/commands/deliver.d.ts +1 -0
  98. package/dist/commands/deliver.js +99 -0
  99. package/dist/commands/disclosure.d.ts +1 -0
  100. package/dist/commands/disclosure.js +80 -0
  101. package/dist/commands/doctor.d.ts +1 -0
  102. package/dist/commands/doctor.js +112 -0
  103. package/dist/commands/estimate.d.ts +2 -0
  104. package/dist/commands/estimate.js +80 -0
  105. package/dist/commands/extensions.d.ts +2 -0
  106. package/dist/commands/extensions.js +70 -0
  107. package/dist/commands/fix-frame.d.ts +1 -0
  108. package/dist/commands/fix-frame.js +206 -0
  109. package/dist/commands/generate-sheet.d.ts +1 -0
  110. package/dist/commands/generate-sheet.js +245 -0
  111. package/dist/commands/identity-check.d.ts +1 -0
  112. package/dist/commands/identity-check.js +110 -0
  113. package/dist/commands/identity-eval.d.ts +1 -0
  114. package/dist/commands/identity-eval.js +197 -0
  115. package/dist/commands/import.d.ts +7 -0
  116. package/dist/commands/import.js +472 -0
  117. package/dist/commands/init.d.ts +1 -0
  118. package/dist/commands/init.js +117 -0
  119. package/dist/commands/intake.d.ts +1 -0
  120. package/dist/commands/intake.js +56 -0
  121. package/dist/commands/labels.d.ts +1 -0
  122. package/dist/commands/labels.js +101 -0
  123. package/dist/commands/library.d.ts +9 -0
  124. package/dist/commands/library.js +711 -0
  125. package/dist/commands/lod.d.ts +1 -0
  126. package/dist/commands/lod.js +93 -0
  127. package/dist/commands/look-dev.d.ts +1 -0
  128. package/dist/commands/look-dev.js +148 -0
  129. package/dist/commands/materials.d.ts +1 -0
  130. package/dist/commands/materials.js +223 -0
  131. package/dist/commands/mcp.d.ts +1 -0
  132. package/dist/commands/mcp.js +53 -0
  133. package/dist/commands/migrate.d.ts +1 -0
  134. package/dist/commands/migrate.js +123 -0
  135. package/dist/commands/op.d.ts +5 -0
  136. package/dist/commands/op.js +114 -0
  137. package/dist/commands/ops.d.ts +1 -0
  138. package/dist/commands/ops.js +17 -0
  139. package/dist/commands/pick-batch.d.ts +24 -0
  140. package/dist/commands/pick-batch.js +343 -0
  141. package/dist/commands/pick.d.ts +48 -0
  142. package/dist/commands/pick.js +469 -0
  143. package/dist/commands/plan.d.ts +3 -0
  144. package/dist/commands/plan.js +156 -0
  145. package/dist/commands/recipe.d.ts +1 -0
  146. package/dist/commands/recipe.js +52 -0
  147. package/dist/commands/registry.d.ts +5 -0
  148. package/dist/commands/registry.js +121 -0
  149. package/dist/commands/render-sprite.d.ts +1 -0
  150. package/dist/commands/render-sprite.js +67 -0
  151. package/dist/commands/rendition.d.ts +1 -0
  152. package/dist/commands/rendition.js +56 -0
  153. package/dist/commands/request-change.d.ts +2 -0
  154. package/dist/commands/request-change.js +950 -0
  155. package/dist/commands/result.d.ts +34 -0
  156. package/dist/commands/result.js +237 -0
  157. package/dist/commands/review.d.ts +28 -0
  158. package/dist/commands/review.js +444 -0
  159. package/dist/commands/rig.d.ts +1 -0
  160. package/dist/commands/rig.js +74 -0
  161. package/dist/commands/runner.d.ts +45 -0
  162. package/dist/commands/runner.js +297 -0
  163. package/dist/commands/runs.d.ts +22 -0
  164. package/dist/commands/runs.js +159 -0
  165. package/dist/commands/search.d.ts +69 -0
  166. package/dist/commands/search.js +638 -0
  167. package/dist/commands/settings.d.ts +2 -0
  168. package/dist/commands/settings.js +284 -0
  169. package/dist/commands/spend.d.ts +2 -0
  170. package/dist/commands/spend.js +159 -0
  171. package/dist/commands/stage-source.d.ts +1 -0
  172. package/dist/commands/stage-source.js +58 -0
  173. package/dist/commands/stage.d.ts +9 -0
  174. package/dist/commands/stage.js +229 -0
  175. package/dist/commands/storage.d.ts +3 -0
  176. package/dist/commands/storage.js +200 -0
  177. package/dist/commands/turnaround.d.ts +1 -0
  178. package/dist/commands/turnaround.js +171 -0
  179. package/dist/commands/types.d.ts +195 -0
  180. package/dist/commands/types.js +7 -0
  181. package/dist/commands/vary.d.ts +2 -0
  182. package/dist/commands/vary.js +222 -0
  183. package/dist/commands/write-families.d.ts +14 -0
  184. package/dist/commands/write-families.js +158 -0
  185. package/dist/compare.d.ts +87 -0
  186. package/dist/compare.js +306 -0
  187. package/dist/confirm/delegation.d.ts +30 -0
  188. package/dist/confirm/delegation.js +31 -0
  189. package/dist/confirm/index.d.ts +84 -0
  190. package/dist/confirm/index.js +176 -0
  191. package/dist/confirm/mcp.d.ts +60 -0
  192. package/dist/confirm/mcp.js +253 -0
  193. package/dist/confirm/pending.d.ts +11 -0
  194. package/dist/confirm/pending.js +40 -0
  195. package/dist/confirm/prompt.d.ts +65 -0
  196. package/dist/confirm/prompt.js +68 -0
  197. package/dist/confirm/records.d.ts +17 -0
  198. package/dist/confirm/records.js +63 -0
  199. package/dist/contact.d.ts +80 -0
  200. package/dist/contact.js +224 -0
  201. package/dist/credentials.d.ts +170 -0
  202. package/dist/credentials.js +598 -0
  203. package/dist/dataflows.d.ts +32 -0
  204. package/dist/dataflows.js +42 -0
  205. package/dist/delivery.d.ts +89 -0
  206. package/dist/delivery.js +355 -0
  207. package/dist/disclosure.d.ts +84 -0
  208. package/dist/disclosure.js +397 -0
  209. package/dist/env.d.ts +90 -0
  210. package/dist/env.js +236 -0
  211. package/dist/errors.d.ts +90 -0
  212. package/dist/errors.js +105 -0
  213. package/dist/extension/api.d.ts +389 -0
  214. package/dist/extension/api.js +20 -0
  215. package/dist/extension/context.d.ts +9 -0
  216. package/dist/extension/context.js +60 -0
  217. package/dist/extension/helpers.d.ts +75 -0
  218. package/dist/extension/helpers.js +107 -0
  219. package/dist/extension/http.d.ts +32 -0
  220. package/dist/extension/http.js +104 -0
  221. package/dist/extension/setup.d.ts +53 -0
  222. package/dist/extension/setup.js +162 -0
  223. package/dist/extension/tools.d.ts +97 -0
  224. package/dist/extension/tools.js +235 -0
  225. package/dist/extensions/core.d.ts +7 -0
  226. package/dist/extensions/core.js +18 -0
  227. package/dist/extensions/errors.d.ts +22 -0
  228. package/dist/extensions/errors.js +34 -0
  229. package/dist/extensions/registry.d.ts +119 -0
  230. package/dist/extensions/registry.js +535 -0
  231. package/dist/extensions/version.d.ts +9 -0
  232. package/dist/extensions/version.js +57 -0
  233. package/dist/formats.d.ts +46 -0
  234. package/dist/formats.js +81 -0
  235. package/dist/frames.d.ts +58 -0
  236. package/dist/frames.js +80 -0
  237. package/dist/gateway/anthropic.d.ts +22 -0
  238. package/dist/gateway/anthropic.js +65 -0
  239. package/dist/gateway/elevenlabs.d.ts +36 -0
  240. package/dist/gateway/elevenlabs.js +192 -0
  241. package/dist/gateway/failure.d.ts +29 -0
  242. package/dist/gateway/failure.js +70 -0
  243. package/dist/gateway/openai-compatible.d.ts +20 -0
  244. package/dist/gateway/openai-compatible.js +76 -0
  245. package/dist/gateway/openai.d.ts +28 -0
  246. package/dist/gateway/openai.js +101 -0
  247. package/dist/gateway/provider.d.ts +380 -0
  248. package/dist/gateway/provider.js +5 -0
  249. package/dist/gateway/scripted.d.ts +252 -0
  250. package/dist/gateway/scripted.js +244 -0
  251. package/dist/gateway/stability.d.ts +38 -0
  252. package/dist/gateway/stability.js +232 -0
  253. package/dist/gateway/structured.d.ts +31 -0
  254. package/dist/gateway/structured.js +83 -0
  255. package/dist/gateway/tripo.d.ts +48 -0
  256. package/dist/gateway/tripo.js +374 -0
  257. package/dist/gateway/vercel.d.ts +52 -0
  258. package/dist/gateway/vercel.js +298 -0
  259. package/dist/generators/generator.d.ts +236 -0
  260. package/dist/generators/generator.js +12 -0
  261. package/dist/generators/image.d.ts +21 -0
  262. package/dist/generators/image.js +286 -0
  263. package/dist/generators/spec-schema.d.ts +29 -0
  264. package/dist/generators/spec-schema.js +21 -0
  265. package/dist/git-marker.d.ts +3 -0
  266. package/dist/git-marker.js +13 -0
  267. package/dist/gitlfs.d.ts +38 -0
  268. package/dist/gitlfs.js +171 -0
  269. package/dist/guard.d.ts +55 -0
  270. package/dist/guard.js +323 -0
  271. package/dist/handoff/current-profile.d.ts +48 -0
  272. package/dist/handoff/current-profile.js +127 -0
  273. package/dist/handoff/doctor.d.ts +26 -0
  274. package/dist/handoff/doctor.js +249 -0
  275. package/dist/handoff/mark-check.d.ts +17 -0
  276. package/dist/handoff/mark-check.js +66 -0
  277. package/dist/handoff/presets/aseprite-json.d.ts +5 -0
  278. package/dist/handoff/presets/aseprite-json.js +45 -0
  279. package/dist/handoff/presets/atlas-json.d.ts +44 -0
  280. package/dist/handoff/presets/atlas-json.js +195 -0
  281. package/dist/handoff/presets/atlas-pack.d.ts +14 -0
  282. package/dist/handoff/presets/atlas-pack.js +431 -0
  283. package/dist/handoff/presets/index.d.ts +273 -0
  284. package/dist/handoff/presets/index.js +58 -0
  285. package/dist/handoff/presets/manifest.d.ts +2 -0
  286. package/dist/handoff/presets/manifest.js +49 -0
  287. package/dist/handoff/usage.d.ts +27 -0
  288. package/dist/handoff/usage.js +159 -0
  289. package/dist/handoff.d.ts +345 -0
  290. package/dist/handoff.js +1221 -0
  291. package/dist/identity-eval.d.ts +156 -0
  292. package/dist/identity-eval.js +321 -0
  293. package/dist/identity.d.ts +293 -0
  294. package/dist/identity.js +675 -0
  295. package/dist/image/apply-style.d.ts +4 -0
  296. package/dist/image/apply-style.js +37 -0
  297. package/dist/image/digits.d.ts +5 -0
  298. package/dist/image/digits.js +40 -0
  299. package/dist/image/ktx2.d.ts +83 -0
  300. package/dist/image/ktx2.js +196 -0
  301. package/dist/image/raster.d.ts +23 -0
  302. package/dist/image/raster.js +77 -0
  303. package/dist/image/resample.d.ts +10 -0
  304. package/dist/image/resample.js +82 -0
  305. package/dist/image/webp.d.ts +17 -0
  306. package/dist/image/webp.js +323 -0
  307. package/dist/init.d.ts +132 -0
  308. package/dist/init.js +425 -0
  309. package/dist/inputs.d.ts +67 -0
  310. package/dist/inputs.js +151 -0
  311. package/dist/json.d.ts +7 -0
  312. package/dist/json.js +27 -0
  313. package/dist/keys.d.ts +34 -0
  314. package/dist/keys.js +198 -0
  315. package/dist/kind-schemas.d.ts +17 -0
  316. package/dist/kind-schemas.js +205 -0
  317. package/dist/library/actions.d.ts +76 -0
  318. package/dist/library/actions.js +212 -0
  319. package/dist/library/adopt.d.ts +177 -0
  320. package/dist/library/adopt.js +624 -0
  321. package/dist/library/canonical.d.ts +7 -0
  322. package/dist/library/canonical.js +29 -0
  323. package/dist/library/card.d.ts +138 -0
  324. package/dist/library/card.js +532 -0
  325. package/dist/library/credits.d.ts +70 -0
  326. package/dist/library/credits.js +222 -0
  327. package/dist/library/describe.d.ts +48 -0
  328. package/dist/library/describe.js +199 -0
  329. package/dist/library/diff.d.ts +26 -0
  330. package/dist/library/diff.js +208 -0
  331. package/dist/library/exact.d.ts +15 -0
  332. package/dist/library/exact.js +47 -0
  333. package/dist/library/extent.d.ts +56 -0
  334. package/dist/library/extent.js +205 -0
  335. package/dist/library/folder.d.ts +85 -0
  336. package/dist/library/folder.js +291 -0
  337. package/dist/library/labels.d.ts +29 -0
  338. package/dist/library/labels.js +112 -0
  339. package/dist/library/lfs.d.ts +42 -0
  340. package/dist/library/lfs.js +140 -0
  341. package/dist/library/maintain.d.ts +100 -0
  342. package/dist/library/maintain.js +750 -0
  343. package/dist/library/media-metadata.d.ts +19 -0
  344. package/dist/library/media-metadata.js +92 -0
  345. package/dist/library/paths.d.ts +7 -0
  346. package/dist/library/paths.js +65 -0
  347. package/dist/library/relations.d.ts +59 -0
  348. package/dist/library/relations.js +176 -0
  349. package/dist/library/review.d.ts +204 -0
  350. package/dist/library/review.js +545 -0
  351. package/dist/library/stale.d.ts +88 -0
  352. package/dist/library/stale.js +229 -0
  353. package/dist/library/suggestions.d.ts +37 -0
  354. package/dist/library/suggestions.js +146 -0
  355. package/dist/library/summary.d.ts +65 -0
  356. package/dist/library/summary.js +138 -0
  357. package/dist/library/undo.d.ts +18 -0
  358. package/dist/library/undo.js +141 -0
  359. package/dist/library/worklist.d.ts +42 -0
  360. package/dist/library/worklist.js +91 -0
  361. package/dist/library/write.d.ts +282 -0
  362. package/dist/library/write.js +790 -0
  363. package/dist/license.d.ts +67 -0
  364. package/dist/license.js +205 -0
  365. package/dist/limits.d.ts +51 -0
  366. package/dist/limits.js +100 -0
  367. package/dist/loop.d.ts +244 -0
  368. package/dist/loop.js +377 -0
  369. package/dist/material/core.d.ts +70 -0
  370. package/dist/material/core.js +238 -0
  371. package/dist/material/engine.d.ts +15 -0
  372. package/dist/material/engine.js +141 -0
  373. package/dist/material/flicker.d.ts +26 -0
  374. package/dist/material/flicker.js +71 -0
  375. package/dist/material/operations.d.ts +2 -0
  376. package/dist/material/operations.js +48 -0
  377. package/dist/material/pipeline.d.ts +51 -0
  378. package/dist/material/pipeline.js +431 -0
  379. package/dist/material/profile.d.ts +29 -0
  380. package/dist/material/profile.js +97 -0
  381. package/dist/material/rendition.d.ts +5 -0
  382. package/dist/material/rendition.js +76 -0
  383. package/dist/mcp.d.ts +72 -0
  384. package/dist/mcp.js +514 -0
  385. package/dist/media.d.ts +36 -0
  386. package/dist/media.js +46 -0
  387. package/dist/metadata.d.ts +61 -0
  388. package/dist/metadata.js +748 -0
  389. package/dist/model-declarations.d.ts +28 -0
  390. package/dist/model-declarations.js +14 -0
  391. package/dist/model3d/clip-map.d.ts +49 -0
  392. package/dist/model3d/clip-map.js +129 -0
  393. package/dist/model3d/core-checks.d.ts +61 -0
  394. package/dist/model3d/core-checks.js +278 -0
  395. package/dist/model3d/facing.d.ts +49 -0
  396. package/dist/model3d/facing.js +125 -0
  397. package/dist/model3d/generator.d.ts +19 -0
  398. package/dist/model3d/generator.js +482 -0
  399. package/dist/model3d/glb.d.ts +120 -0
  400. package/dist/model3d/glb.js +155 -0
  401. package/dist/model3d/handoff.d.ts +37 -0
  402. package/dist/model3d/handoff.js +131 -0
  403. package/dist/model3d/lod/chain.d.ts +50 -0
  404. package/dist/model3d/lod/chain.js +447 -0
  405. package/dist/model3d/lod/command.d.ts +38 -0
  406. package/dist/model3d/lod/command.js +377 -0
  407. package/dist/model3d/lod/qa.d.ts +36 -0
  408. package/dist/model3d/lod/qa.js +189 -0
  409. package/dist/model3d/lod/settings.d.ts +36 -0
  410. package/dist/model3d/lod/settings.js +137 -0
  411. package/dist/model3d/lod/simplify.d.ts +35 -0
  412. package/dist/model3d/lod/simplify.js +337 -0
  413. package/dist/model3d/lod/textures.d.ts +46 -0
  414. package/dist/model3d/lod/textures.js +190 -0
  415. package/dist/model3d/lookdev/command.d.ts +219 -0
  416. package/dist/model3d/lookdev/command.js +1795 -0
  417. package/dist/model3d/lookdev/edit.d.ts +44 -0
  418. package/dist/model3d/lookdev/edit.js +241 -0
  419. package/dist/model3d/lookdev/groom.d.ts +86 -0
  420. package/dist/model3d/lookdev/groom.js +462 -0
  421. package/dist/model3d/lookdev/markings.d.ts +98 -0
  422. package/dist/model3d/lookdev/markings.js +319 -0
  423. package/dist/model3d/lookdev/project.d.ts +100 -0
  424. package/dist/model3d/lookdev/project.js +721 -0
  425. package/dist/model3d/lookdev/settings.d.ts +65 -0
  426. package/dist/model3d/lookdev/settings.js +103 -0
  427. package/dist/model3d/lookdev/texels.d.ts +32 -0
  428. package/dist/model3d/lookdev/texels.js +192 -0
  429. package/dist/model3d/profile.d.ts +101 -0
  430. package/dist/model3d/profile.js +132 -0
  431. package/dist/model3d/prop.d.ts +29 -0
  432. package/dist/model3d/prop.js +317 -0
  433. package/dist/model3d/qa.d.ts +27 -0
  434. package/dist/model3d/qa.js +105 -0
  435. package/dist/model3d/render.d.ts +172 -0
  436. package/dist/model3d/render.js +791 -0
  437. package/dist/model3d/rig/build.d.ts +14 -0
  438. package/dist/model3d/rig/build.js +210 -0
  439. package/dist/model3d/rig/candidate.d.ts +50 -0
  440. package/dist/model3d/rig/candidate.js +132 -0
  441. package/dist/model3d/rig/command.d.ts +50 -0
  442. package/dist/model3d/rig/command.js +229 -0
  443. package/dist/model3d/rig/fit.d.ts +88 -0
  444. package/dist/model3d/rig/fit.js +568 -0
  445. package/dist/model3d/rig/gait.d.ts +105 -0
  446. package/dist/model3d/rig/gait.js +334 -0
  447. package/dist/model3d/rig/math.d.ts +27 -0
  448. package/dist/model3d/rig/math.js +92 -0
  449. package/dist/model3d/rig/motion.d.ts +26 -0
  450. package/dist/model3d/rig/motion.js +223 -0
  451. package/dist/model3d/rig/qa.d.ts +76 -0
  452. package/dist/model3d/rig/qa.js +357 -0
  453. package/dist/model3d/rig/rig.d.ts +55 -0
  454. package/dist/model3d/rig/rig.js +213 -0
  455. package/dist/model3d/rig/template.d.ts +195 -0
  456. package/dist/model3d/rig/template.js +456 -0
  457. package/dist/model3d/scene.d.ts +115 -0
  458. package/dist/model3d/scene.js +684 -0
  459. package/dist/model3d/views.d.ts +77 -0
  460. package/dist/model3d/views.js +190 -0
  461. package/dist/origin.d.ts +162 -0
  462. package/dist/origin.js +134 -0
  463. package/dist/paths.d.ts +53 -0
  464. package/dist/paths.js +240 -0
  465. package/dist/planning.d.ts +261 -0
  466. package/dist/planning.js +1470 -0
  467. package/dist/policy/blocklist.d.ts +28 -0
  468. package/dist/policy/blocklist.js +58 -0
  469. package/dist/policy/brief.d.ts +67 -0
  470. package/dist/policy/brief.js +110 -0
  471. package/dist/policy/cuts.d.ts +26 -0
  472. package/dist/policy/cuts.js +80 -0
  473. package/dist/policy/model.d.ts +69 -0
  474. package/dist/policy/model.js +202 -0
  475. package/dist/policy/text.d.ts +183 -0
  476. package/dist/policy/text.js +285 -0
  477. package/dist/postprocess.d.ts +68 -0
  478. package/dist/postprocess.js +302 -0
  479. package/dist/profile.d.ts +5 -0
  480. package/dist/profile.js +98 -0
  481. package/dist/project-rules.d.ts +54 -0
  482. package/dist/project-rules.js +105 -0
  483. package/dist/provenance-binding.d.ts +55 -0
  484. package/dist/provenance-binding.js +352 -0
  485. package/dist/providers/adapter.d.ts +176 -0
  486. package/dist/providers/adapter.js +418 -0
  487. package/dist/providers/index.d.ts +33 -0
  488. package/dist/providers/index.js +49 -0
  489. package/dist/providers/script.d.ts +13 -0
  490. package/dist/providers/script.js +65 -0
  491. package/dist/providers/set.d.ts +76 -0
  492. package/dist/providers/set.js +134 -0
  493. package/dist/pure-cbor.d.ts +1 -0
  494. package/dist/pure-cbor.js +8 -0
  495. package/dist/qa/stability.d.ts +215 -0
  496. package/dist/qa/stability.js +393 -0
  497. package/dist/qa/stability.json +32 -0
  498. package/dist/qa.d.ts +36 -0
  499. package/dist/qa.js +148 -0
  500. package/dist/reconcile.d.ts +111 -0
  501. package/dist/reconcile.js +416 -0
  502. package/dist/redact.d.ts +29 -0
  503. package/dist/redact.js +74 -0
  504. package/dist/regions.d.ts +49 -0
  505. package/dist/regions.js +113 -0
  506. package/dist/release-policy.d.ts +99 -0
  507. package/dist/release-policy.js +266 -0
  508. package/dist/request.d.ts +74 -0
  509. package/dist/request.js +249 -0
  510. package/dist/routes.d.ts +327 -0
  511. package/dist/routes.js +1166 -0
  512. package/dist/runs/dir.d.ts +33 -0
  513. package/dist/runs/dir.js +30 -0
  514. package/dist/runs/execute.d.ts +86 -0
  515. package/dist/runs/execute.js +422 -0
  516. package/dist/runs/journal.d.ts +150 -0
  517. package/dist/runs/journal.js +216 -0
  518. package/dist/runs/lock.d.ts +57 -0
  519. package/dist/runs/lock.js +322 -0
  520. package/dist/runs/plan.d.ts +261 -0
  521. package/dist/runs/plan.js +404 -0
  522. package/dist/search/near-match.d.ts +32 -0
  523. package/dist/search/near-match.js +86 -0
  524. package/dist/search/reader.d.ts +17 -0
  525. package/dist/search/reader.js +334 -0
  526. package/dist/search/sheet.d.ts +4 -0
  527. package/dist/search/sheet.js +28 -0
  528. package/dist/search/style-outliers.d.ts +77 -0
  529. package/dist/search/style-outliers.js +166 -0
  530. package/dist/settings-write.d.ts +78 -0
  531. package/dist/settings-write.js +374 -0
  532. package/dist/settings.d.ts +436 -0
  533. package/dist/settings.js +622 -0
  534. package/dist/shared-settings.d.ts +4 -0
  535. package/dist/shared-settings.js +24 -0
  536. package/dist/sheet/blobs.d.ts +33 -0
  537. package/dist/sheet/blobs.js +145 -0
  538. package/dist/sheet/generate.d.ts +71 -0
  539. package/dist/sheet/generate.js +278 -0
  540. package/dist/sheet/group.d.ts +20 -0
  541. package/dist/sheet/group.js +191 -0
  542. package/dist/sheet/import.d.ts +315 -0
  543. package/dist/sheet/import.js +832 -0
  544. package/dist/sheet/matte.d.ts +25 -0
  545. package/dist/sheet/matte.js +508 -0
  546. package/dist/sheet/media-import.d.ts +41 -0
  547. package/dist/sheet/media-import.js +118 -0
  548. package/dist/sheet/register.d.ts +17 -0
  549. package/dist/sheet/register.js +176 -0
  550. package/dist/sheet/slice.d.ts +73 -0
  551. package/dist/sheet/slice.js +912 -0
  552. package/dist/sheet/types.d.ts +225 -0
  553. package/dist/sheet/types.js +1 -0
  554. package/dist/source/aseprite.d.ts +14 -0
  555. package/dist/source/aseprite.js +124 -0
  556. package/dist/source/stage.d.ts +24 -0
  557. package/dist/source/stage.js +172 -0
  558. package/dist/spend.d.ts +220 -0
  559. package/dist/spend.js +436 -0
  560. package/dist/sprite/render.d.ts +22 -0
  561. package/dist/sprite/render.js +272 -0
  562. package/dist/storage.d.ts +135 -0
  563. package/dist/storage.js +405 -0
  564. package/dist/style.d.ts +19 -0
  565. package/dist/style.js +26 -0
  566. package/dist/template.d.ts +29 -0
  567. package/dist/template.js +98 -0
  568. package/dist/testing/aseprite.d.ts +54 -0
  569. package/dist/testing/aseprite.js +191 -0
  570. package/dist/testing/cli.d.ts +24 -0
  571. package/dist/testing/cli.js +91 -0
  572. package/dist/testing/conformance.d.ts +61 -0
  573. package/dist/testing/conformance.js +455 -0
  574. package/dist/testing/index.d.ts +23 -0
  575. package/dist/testing/index.js +15 -0
  576. package/dist/testing/mcp.d.ts +25 -0
  577. package/dist/testing/mcp.js +80 -0
  578. package/dist/testing/project.d.ts +81 -0
  579. package/dist/testing/project.js +215 -0
  580. package/dist/testing/samples.d.ts +42 -0
  581. package/dist/testing/samples.js +122 -0
  582. package/dist/transform/operations.d.ts +5 -0
  583. package/dist/transform/operations.js +320 -0
  584. package/dist/transform/raster-engine.d.ts +9 -0
  585. package/dist/transform/raster-engine.js +329 -0
  586. package/dist/transform/rendition.d.ts +65 -0
  587. package/dist/transform/rendition.js +317 -0
  588. package/dist/transform/run.d.ts +138 -0
  589. package/dist/transform/run.js +442 -0
  590. package/dist/transform/schema.d.ts +33 -0
  591. package/dist/transform/schema.js +73 -0
  592. package/dist/transform/types.d.ts +101 -0
  593. package/dist/transform/types.js +1 -0
  594. package/dist/tree/address.d.ts +18 -0
  595. package/dist/tree/address.js +59 -0
  596. package/dist/tree/list.d.ts +18 -0
  597. package/dist/tree/list.js +101 -0
  598. package/dist/tree/project.d.ts +61 -0
  599. package/dist/tree/project.js +145 -0
  600. package/dist/tree/read.d.ts +43 -0
  601. package/dist/tree/read.js +715 -0
  602. package/dist/tree/search.d.ts +20 -0
  603. package/dist/tree/search.js +44 -0
  604. package/dist/tree/serve.d.ts +79 -0
  605. package/dist/tree/serve.js +415 -0
  606. package/dist/tree/thumbs.d.ts +26 -0
  607. package/dist/tree/thumbs.js +134 -0
  608. package/dist/turnaround.d.ts +195 -0
  609. package/dist/turnaround.js +622 -0
  610. package/dist/types.d.ts +595 -0
  611. package/dist/types.js +1 -0
  612. package/package.json +112 -3
  613. package/policy/blocklist.json +47 -0
  614. package/policy/prompt-policy-cuts.json +15 -0
  615. package/skills/art-direction/SKILL.md +16 -0
  616. package/skills/art-direction/default.md +14 -0
  617. package/skills/art-direction/presets/cozy-storybook.md +121 -0
  618. package/skills/art-direction/presets/dark-gritty-isometric.md +121 -0
  619. package/skills/art-direction/presets/flat-cartoon-kids.md +121 -0
  620. package/skills/art-direction/references/bible-format.md +47 -0
  621. package/skills/assetgrove/SKILL.md +109 -0
  622. package/skills/assetgrove/references/commands.md +723 -0
@@ -0,0 +1,912 @@
1
+ /**
2
+ * Slicing: finds each figure on a contact sheet, either from a grid (given or detected) or by
3
+ * locating connected components, and keeps labels, specks, neighbours and background out of every
4
+ * figure's mask.
5
+ */
6
+ import { InputError } from "../errors.js";
7
+ /** Px around a dropped label, speck or neighbour also kept out of the frame (its soft fringe). */
8
+ const EXCLUDE_FRINGE = 2;
9
+ /** Ratio between neighbouring areas (largest first) that separates figures from labels. */
10
+ export const SIZE_DROP = 3;
11
+ /** The default share of the largest figure under which a part is a label or speck. */
12
+ export const LABEL_FRACTION = 0.04;
13
+ /**
14
+ * The share of a cell's area under which, judged alone, its largest part is a speck: 0.5%, about
15
+ * 2,600 px of a 512x1024 cell, well above matte noise and well below any real figure (owner
16
+ * decision, #645; docs/decisions.md).
17
+ */
18
+ export const SPECK_FRACTION = 0.005;
19
+ /** The smallest area, in px, a component needs to be a figure when a cell is judged alone. */
20
+ export function speckBound(cellArea) {
21
+ return SPECK_FRACTION * cellArea;
22
+ }
23
+ /** A figure this many times the median figure's area may be touching figures merged into one. */
24
+ const MERGED_RATIO = 1.75;
25
+ /** A figure at most this fraction of the median figure's area may be part of a figure. */
26
+ const SPLIT_RATIO = 0.5;
27
+ /**
28
+ * Slicing could not find the figures the sheet must hold: the count differs from the one asked
29
+ * for. `problems` names each cell or figure that explains the difference, and why.
30
+ */
31
+ export class SliceError extends InputError {
32
+ found;
33
+ expected;
34
+ problems;
35
+ /** Where the refused sheet was kept, when it was a generated one. */
36
+ kept;
37
+ /** What the run had spent when the slice was refused, when a model made the sheet. */
38
+ spentUsd;
39
+ constructor(found, expected, problems) {
40
+ super(`slicing found ${found} figure${found === 1 ? "" : "s"} on the sheet where ${expected} ` +
41
+ `${expected === 1 ? "is" : "are"} asked for: ` +
42
+ (problems.length > 0
43
+ ? problems.join("; ")
44
+ : "no single cell or figure explains the difference"));
45
+ this.name = "SliceError";
46
+ this.found = found;
47
+ this.expected = expected;
48
+ this.problems = problems;
49
+ }
50
+ }
51
+ function median(values) {
52
+ if (values.length === 0)
53
+ return 0;
54
+ const sorted = [...values].sort((a, b) => a - b);
55
+ return sorted[Math.floor(sorted.length / 2)];
56
+ }
57
+ function medianColour(sheet, hasAlpha, indices) {
58
+ const channels = [[], [], []];
59
+ const all = [];
60
+ for (const i of indices) {
61
+ all.push(i);
62
+ if (hasAlpha && sheet.data[i * 4 + 3] < 128)
63
+ continue;
64
+ for (let k = 0; k < 3; k++)
65
+ channels[k].push(sheet.data[i * 4 + k]);
66
+ }
67
+ const count = channels[0].length;
68
+ const source = count > 0 ? channels : [0, 1, 2].map((k) => all.map((i) => sheet.data[i * 4 + k]));
69
+ return {
70
+ colour: [
71
+ median(source[0]),
72
+ median(source[1]),
73
+ median(source[2]),
74
+ ],
75
+ count,
76
+ };
77
+ }
78
+ function boxBorder(sheet, box) {
79
+ const out = [];
80
+ for (let x = box.x; x < box.x + box.width; x++) {
81
+ out.push(box.y * sheet.width + x, (box.y + box.height - 1) * sheet.width + x);
82
+ }
83
+ for (let y = box.y + 1; y < box.y + box.height - 1; y++) {
84
+ out.push(y * sheet.width + box.x, y * sheet.width + box.x + box.width - 1);
85
+ }
86
+ return out;
87
+ }
88
+ function ringAround(sheet, box, thickness) {
89
+ const x0 = Math.max(0, box.x - thickness);
90
+ const y0 = Math.max(0, box.y - thickness);
91
+ const x1 = Math.min(sheet.width, box.x + box.width + thickness);
92
+ const y1 = Math.min(sheet.height, box.y + box.height + thickness);
93
+ const out = [];
94
+ for (let y = y0; y < y1; y++) {
95
+ for (let x = x0; x < x1; x++) {
96
+ const inside = x >= box.x && x < box.x + box.width && y >= box.y && y < box.y + box.height;
97
+ if (!inside)
98
+ out.push(y * sheet.width + x);
99
+ }
100
+ }
101
+ return out;
102
+ }
103
+ function estimate(sheet, hasAlpha, indices) {
104
+ const { colour, count } = medianColour(sheet, hasAlpha, indices);
105
+ return { colour, usesColour: count > 0 };
106
+ }
107
+ function foreground(sheet, hasAlpha, bg, tolerance, box) {
108
+ const mask = new Uint8Array(box.width * box.height);
109
+ const limit = tolerance * tolerance;
110
+ for (let y = 0; y < box.height; y++) {
111
+ for (let x = 0; x < box.width; x++) {
112
+ const i = ((box.y + y) * sheet.width + box.x + x) * 4;
113
+ const d = sheet.data;
114
+ if (hasAlpha && d[i + 3] < 128)
115
+ continue;
116
+ if (!bg.usesColour) {
117
+ mask[y * box.width + x] = 1;
118
+ continue;
119
+ }
120
+ const dr = d[i] - bg.colour[0];
121
+ const dg = d[i + 1] - bg.colour[1];
122
+ const db = d[i + 2] - bg.colour[2];
123
+ if (dr * dr + dg * dg + db * db > limit)
124
+ mask[y * box.width + x] = 1;
125
+ }
126
+ }
127
+ return mask;
128
+ }
129
+ /** 8-connected components of a mask over `box`; pixel indices and boxes in sheet coordinates. */
130
+ function components(mask, box, sheetWidth) {
131
+ const seen = new Uint8Array(mask.length);
132
+ const out = [];
133
+ for (let start = 0; start < mask.length; start++) {
134
+ if (!mask[start] || seen[start])
135
+ continue;
136
+ const stack = [start];
137
+ seen[start] = 1;
138
+ const pixels = [];
139
+ let minX = Infinity;
140
+ let minY = Infinity;
141
+ let maxX = -1;
142
+ let maxY = -1;
143
+ while (stack.length > 0) {
144
+ const p = stack.pop();
145
+ const x = p % box.width;
146
+ const y = (p - x) / box.width;
147
+ pixels.push((box.y + y) * sheetWidth + box.x + x);
148
+ if (x < minX)
149
+ minX = x;
150
+ if (x > maxX)
151
+ maxX = x;
152
+ if (y < minY)
153
+ minY = y;
154
+ if (y > maxY)
155
+ maxY = y;
156
+ for (let dy = -1; dy <= 1; dy++) {
157
+ for (let dx = -1; dx <= 1; dx++) {
158
+ const nx = x + dx;
159
+ const ny = y + dy;
160
+ if (nx < 0 || ny < 0 || nx >= box.width || ny >= box.height)
161
+ continue;
162
+ const q = ny * box.width + nx;
163
+ if (!mask[q] || seen[q])
164
+ continue;
165
+ seen[q] = 1;
166
+ stack.push(q);
167
+ }
168
+ }
169
+ }
170
+ out.push({
171
+ pixels,
172
+ box: {
173
+ x: box.x + minX,
174
+ y: box.y + minY,
175
+ width: maxX - minX + 1,
176
+ height: maxY - minY + 1,
177
+ },
178
+ });
179
+ }
180
+ return out;
181
+ }
182
+ function unionBox(boxes) {
183
+ const x0 = Math.min(...boxes.map((b) => b.x));
184
+ const y0 = Math.min(...boxes.map((b) => b.y));
185
+ const x1 = Math.max(...boxes.map((b) => b.x + b.width));
186
+ const y1 = Math.max(...boxes.map((b) => b.y + b.height));
187
+ return { x: x0, y: y0, width: x1 - x0, height: y1 - y0 };
188
+ }
189
+ function inside(inner, outer, slack) {
190
+ return (inner.x >= outer.x - slack &&
191
+ inner.y >= outer.y - slack &&
192
+ inner.x + inner.width <= outer.x + outer.width + slack &&
193
+ inner.y + inner.height <= outer.y + outer.height + slack);
194
+ }
195
+ function centre(b) {
196
+ return { x: b.x + b.width / 2, y: b.y + b.height / 2 };
197
+ }
198
+ /**
199
+ * The smallest area that still counts as a figure. Components under `labelFraction` of the largest
200
+ * are labels or specks; among the rest, a drop of SIZE_DROP or more between neighbouring areas
201
+ * separates figures from the labels that are big next to a small figure.
202
+ */
203
+ function figureCutoff(components, labelFraction) {
204
+ const areas = components.map((c) => c.pixels.length).sort((a, b) => b - a);
205
+ const largest = areas[0] ?? 0;
206
+ const candidates = areas.filter((a) => a >= labelFraction * largest);
207
+ let cutoff = candidates[candidates.length - 1] ?? 0;
208
+ let bestRatio = SIZE_DROP;
209
+ for (let i = 0; i + 1 < candidates.length; i++) {
210
+ const ratio = candidates[i] / candidates[i + 1];
211
+ if (ratio >= bestRatio) {
212
+ bestRatio = ratio;
213
+ cutoff = candidates[i];
214
+ }
215
+ }
216
+ return cutoff;
217
+ }
218
+ /** A blob's id: its box on the sheet, `x,y,WxH`. Stable for one sheet and one set of options. */
219
+ function blobId(c) {
220
+ return `${c.box.x},${c.box.y},${c.box.width}x${c.box.height}`;
221
+ }
222
+ /** A component under the cutoff that a `sheet-blob` verdict made a whole figure. */
223
+ function isFigure(c, cutoff, verdicts) {
224
+ return c.pixels.length >= cutoff || verdicts.get(blobId(c))?.kind === "figure";
225
+ }
226
+ function nearest(figures, c) {
227
+ const at = centre(c.box);
228
+ let best;
229
+ let bestDistance = Infinity;
230
+ for (const f of figures) {
231
+ const fc = centre(f.box);
232
+ const distance = (fc.x - at.x) ** 2 + (fc.y - at.y) ** 2;
233
+ if (distance < bestDistance) {
234
+ bestDistance = distance;
235
+ best = f;
236
+ }
237
+ }
238
+ return best;
239
+ }
240
+ function orphanMessage(c, scope) {
241
+ return (`the blob at ${blobId(c)} was judged part of a figure, but ` +
242
+ (scope === "cell"
243
+ ? "its grid cell holds no figure for it to join; the grid does not fit the sheet, so give the grid or frame count that keeps each frame's parts in its cell"
244
+ : "the sheet holds no figure for it to join"));
245
+ }
246
+ /**
247
+ * Splits components into figures, their merged detached bits, and dropped ones. A blob's verdict
248
+ * decides it: a figure is one whatever its area, a part joins the nearest figure, a label or speck
249
+ * is dropped. A blob with no verdict keeps the area rule: under the cutoff, it joins the figure
250
+ * whose box holds it, else it is dropped. A part with no figure to join (none on the sheet, or
251
+ * none in its cell under a given grid) is refused, naming the blob, never dropped.
252
+ */
253
+ function assign(all, cutoff, verdicts, scope = "sheet") {
254
+ const figures = all.filter((c) => isFigure(c, cutoff, verdicts));
255
+ const merged = new Map(figures.map((f) => [f, []]));
256
+ const dropped = [];
257
+ for (const c of all) {
258
+ if (isFigure(c, cutoff, verdicts))
259
+ continue;
260
+ const verdict = verdicts.get(blobId(c));
261
+ let host;
262
+ if (verdict?.kind === "part") {
263
+ host =
264
+ verdict.with !== undefined
265
+ ? (figures.find((f) => blobId(f) === verdict.with) ?? nearest(figures, c))
266
+ : nearest(figures, c);
267
+ // A blob judged part of a figure is never erased: with no figure to join, refuse.
268
+ if (!host)
269
+ throw new InputError(orphanMessage(c, scope));
270
+ }
271
+ else if (verdict === undefined) {
272
+ host = figures.find((f) => inside(c.box, f.box, 2));
273
+ }
274
+ if (host)
275
+ merged.get(host).push(c);
276
+ else
277
+ dropped.push(c);
278
+ }
279
+ return { figures, merged, dropped };
280
+ }
281
+ function cluster(values, gap) {
282
+ const order = values.map((v, i) => [v, i]).sort((a, b) => a[0] - b[0]);
283
+ const ids = new Array(values.length).fill(0);
284
+ let current = 0;
285
+ order.forEach(([v, i], k) => {
286
+ if (k > 0 && v - order[k - 1][0] > gap)
287
+ current++;
288
+ ids[i] = current;
289
+ });
290
+ return ids;
291
+ }
292
+ /** How many rows the figures sit in, clustered by their centres' heights as detectGrid does. */
293
+ function figureRows(figures) {
294
+ if (figures.length === 0)
295
+ return undefined;
296
+ const boxes = figures.map((f) => f.box);
297
+ return (Math.max(...cluster(boxes.map((b) => centre(b).y), 0.5 * median(boxes.map((b) => b.height)))) + 1);
298
+ }
299
+ /**
300
+ * The figure-sized components that are whole figures, not a detached prop or part beside one: at
301
+ * least half the size of a typical large component (the median of the larger half), so neither a
302
+ * prop in every cell nor a few touching figures merged into one move the reference.
303
+ */
304
+ function wholeFigures(figures) {
305
+ if (figures.length === 0)
306
+ return [];
307
+ const sizes = figures.map((f) => f.pixels.length).sort((a, b) => a - b);
308
+ const reference = median(sizes.slice(Math.floor(sizes.length / 2)));
309
+ return figures.filter((f) => f.pixels.length >= 0.45 * reference);
310
+ }
311
+ /**
312
+ * How a given grid cuts the sheet's figure-sized components: the cells whose box holds the centres
313
+ * of two or more whole figures (so the grid has too few cells), and the share of the figures' area
314
+ * in components a cell edge cuts straight through, crossing at half the component's width or more
315
+ * (so the cells are too short). Figures that touch across an edge at a narrower contact, and the
316
+ * odd figure that hangs into the cell below, leave that share low.
317
+ */
318
+ function gridFit(sheetWidth, figures, grid) {
319
+ const { columns, rows, cell, origin } = grid;
320
+ const crowding = new Map();
321
+ for (const f of wholeFigures(figures)) {
322
+ const c = centre(f.box);
323
+ const column = Math.floor((c.x - origin.x) / cell.width);
324
+ const row = Math.floor((c.y - origin.y) / cell.height);
325
+ if (column < 0 || column >= columns || row < 0 || row >= rows)
326
+ continue;
327
+ const key = `row ${row + 1}, column ${column + 1}`;
328
+ crowding.set(key, (crowding.get(key) ?? 0) + 1);
329
+ }
330
+ const crowded = [...crowding].filter(([, n]) => n > 1).map(([key]) => key);
331
+ let total = 0;
332
+ let sliced = 0;
333
+ for (const f of figures) {
334
+ total += f.pixels.length;
335
+ const at = new Set(f.pixels);
336
+ let cutThrough = false;
337
+ for (let k = 1; k < rows && !cutThrough; k++) {
338
+ const y = origin.y + k * cell.height;
339
+ if (y <= f.box.y || y >= f.box.y + f.box.height)
340
+ continue;
341
+ const crossing = new Map();
342
+ for (let x = f.box.x; x < f.box.x + f.box.width; x++) {
343
+ if (!at.has(y * sheetWidth + x) || !at.has((y - 1) * sheetWidth + x))
344
+ continue;
345
+ const column = Math.floor((x - origin.x) / cell.width);
346
+ crossing.set(column, (crossing.get(column) ?? 0) + 1);
347
+ }
348
+ for (const n of crossing.values())
349
+ if (n >= 0.5 * f.box.width)
350
+ cutThrough = true;
351
+ }
352
+ if (cutThrough)
353
+ sliced += f.pixels.length;
354
+ }
355
+ return { crowded, slicedShare: total > 0 ? sliced / total : 0 };
356
+ }
357
+ function detectGrid(sheet, figures) {
358
+ if (figures.length < 2)
359
+ return undefined;
360
+ const boxes = figures.map((f) => f.box);
361
+ const cx = boxes.map((b) => centre(b).x);
362
+ const cy = boxes.map((b) => centre(b).y);
363
+ const columns = Math.max(...cluster(cx, 0.5 * median(boxes.map((b) => b.width)))) + 1;
364
+ const rows = Math.max(...cluster(cy, 0.5 * median(boxes.map((b) => b.height)))) + 1;
365
+ if (columns * rows < 2)
366
+ return undefined;
367
+ const cell = {
368
+ width: Math.round(sheet.width / columns),
369
+ height: Math.round(sheet.height / rows),
370
+ };
371
+ if (Math.abs(cell.width * columns - sheet.width) > columns)
372
+ return undefined;
373
+ if (Math.abs(cell.height * rows - sheet.height) > rows)
374
+ return undefined;
375
+ const taken = new Set();
376
+ for (const b of boxes) {
377
+ const c = centre(b);
378
+ const column = Math.floor(c.x / cell.width);
379
+ const row = Math.floor(c.y / cell.height);
380
+ if (column >= columns || row >= rows || taken.has(row * columns + column))
381
+ return undefined;
382
+ taken.add(row * columns + column);
383
+ const slot = { x: column * cell.width, y: row * cell.height, ...cell };
384
+ if (!inside(b, slot, Math.round(0.1 * Math.min(cell.width, cell.height))))
385
+ return undefined;
386
+ }
387
+ return { columns, rows, cell, origin: { x: 0, y: 0 } };
388
+ }
389
+ /**
390
+ * The figure's crop mask: exactly its own components' pixels. Enclosed background (a gap between
391
+ * an arm and the body) stays 0, so the background colour never becomes opaque in a frame.
392
+ */
393
+ function cropMask(parts, crop, sheetWidth) {
394
+ const mask = new Uint8Array(crop.width * crop.height);
395
+ for (const part of parts) {
396
+ for (const p of part.pixels) {
397
+ const x = (p % sheetWidth) - crop.x;
398
+ const y = Math.floor(p / sheetWidth) - crop.y;
399
+ mask[y * crop.width + x] = 1;
400
+ }
401
+ }
402
+ return mask;
403
+ }
404
+ /**
405
+ * Per crop pixel: 1 on foreground that is not the figure's (labels, specks, neighbours) and on its
406
+ * fringe within EXCLUDE_FRINGE px, never on the figure's own mask. Matting zeroes these pixels.
407
+ */
408
+ function excludedMask(mask, fg, crop) {
409
+ const out = new Uint8Array(mask.length);
410
+ const r = EXCLUDE_FRINGE;
411
+ for (let p = 0; p < mask.length; p++) {
412
+ if (!fg[p] || mask[p])
413
+ continue;
414
+ const x = p % crop.width;
415
+ const y = (p - x) / crop.width;
416
+ for (let dy = -r; dy <= r; dy++) {
417
+ for (let dx = -r; dx <= r; dx++) {
418
+ const xx = x + dx;
419
+ const yy = y + dy;
420
+ if (xx < 0 || yy < 0 || xx >= crop.width || yy >= crop.height)
421
+ continue;
422
+ const q = yy * crop.width + xx;
423
+ if (!mask[q])
424
+ out[q] = 1;
425
+ }
426
+ }
427
+ }
428
+ return out;
429
+ }
430
+ function clipBox(box, within) {
431
+ const x0 = Math.max(box.x, within.x);
432
+ const y0 = Math.max(box.y, within.y);
433
+ const x1 = Math.min(box.x + box.width, within.x + within.width);
434
+ const y1 = Math.min(box.y + box.height, within.y + within.height);
435
+ return { x: x0, y: y0, width: Math.max(0, x1 - x0), height: Math.max(0, y1 - y0) };
436
+ }
437
+ function build(sheet, found, limit, cellBox, padding, bg, hasAlpha, tolerance,
438
+ /** The background that other foreground in the crop (labels, specks, neighbours) is found against. */
439
+ segment) {
440
+ const extent = unionBox(found.parts.map((p) => p.box));
441
+ const box = clipBox({
442
+ x: extent.x - padding,
443
+ y: extent.y - padding,
444
+ width: extent.width + 2 * padding,
445
+ height: extent.height + 2 * padding,
446
+ }, limit);
447
+ const crop = {
448
+ width: box.width,
449
+ height: box.height,
450
+ data: new Uint8Array(box.width * box.height * 4),
451
+ };
452
+ for (let y = 0; y < box.height; y++) {
453
+ const from = ((box.y + y) * sheet.width + box.x) * 4;
454
+ crop.data.set(sheet.data.subarray(from, from + box.width * 4), y * box.width * 4);
455
+ }
456
+ const transforms = [
457
+ { op: "crop", box, cell: cellBox },
458
+ { op: "strip-labels", removed: found.removed },
459
+ ];
460
+ const mask = cropMask(found.parts, box, sheet.width);
461
+ const fg = foreground(sheet, hasAlpha, segment, tolerance, box);
462
+ return {
463
+ box,
464
+ crop,
465
+ mask,
466
+ excluded: excludedMask(mask, fg, box),
467
+ background: bg.colour,
468
+ transforms,
469
+ };
470
+ }
471
+ const cellKey = (row, column) => `${row},${column}`;
472
+ function cellBoxOf(grid, row, column, whole) {
473
+ return clipBox({
474
+ x: grid.origin.x + column * grid.cell.width,
475
+ y: grid.origin.y + row * grid.cell.height,
476
+ ...grid.cell,
477
+ }, whole);
478
+ }
479
+ /** Why a cell holds no figure, from its largest component (if any) and the figure size. */
480
+ function emptyReason(largest, cutoff, tolerance) {
481
+ if (largest === 0) {
482
+ return `nothing in it differs from the background by more than the colour tolerance (${tolerance})`;
483
+ }
484
+ return `its largest part is ${largest} px, under the ${cutoff} px a figure needs`;
485
+ }
486
+ /**
487
+ * A grid the caller gave: each cell is segmented on its own, against its own border's colour, so
488
+ * figures that touch across a cell edge come apart. Which parts are figures is decided on these
489
+ * same per-cell parts, never on the sheet-wide ones, whose areas differ.
490
+ */
491
+ function sliceGivenGrid(ctx, grid) {
492
+ const { sheet, whole, hasAlpha, tolerance, padding } = ctx;
493
+ const cells = [];
494
+ for (let row = 0; row < grid.rows; row++) {
495
+ for (let column = 0; column < grid.columns; column++) {
496
+ const box = cellBoxOf(grid, row, column, whole);
497
+ if (box.width === 0 || box.height === 0)
498
+ continue;
499
+ const bg = estimate(sheet, hasAlpha, boxBorder(sheet, box));
500
+ const parts = components(foreground(sheet, hasAlpha, bg, tolerance, box), box, sheet.width);
501
+ cells.push({ row, column, box, bg, parts });
502
+ }
503
+ }
504
+ const cutoff = figureCutoff(cells.flatMap((c) => c.parts), ctx.labelFraction);
505
+ const figures = [];
506
+ const empty = new Map();
507
+ for (const { row, column, box, bg, parts } of cells) {
508
+ const cellCutoff = ctx.cellAlone ? speckBound(box.width * box.height) : cutoff;
509
+ const { figures: big, merged } = assign(parts, cellCutoff, ctx.verdicts, "cell");
510
+ const main = big.sort((a, b) => b.pixels.length - a.pixels.length)[0];
511
+ if (!main) {
512
+ const largest = Math.max(0, ...parts.map((p) => p.pixels.length));
513
+ empty.set(cellKey(row, column), emptyReason(largest, cellCutoff, tolerance));
514
+ continue;
515
+ }
516
+ // Anything else in the cell that is figure-sized is dropped, as another figure is not ours.
517
+ const own = [main, ...merged.get(main)];
518
+ const removed = parts.length - own.length;
519
+ figures.push({
520
+ id: 0,
521
+ cell: { row, column, box },
522
+ ...build(sheet, { parts: own, removed }, box, box, padding, bg, hasAlpha, tolerance, bg),
523
+ });
524
+ }
525
+ figures.forEach((f, i) => (f.id = i + 1));
526
+ return { mode: "grid", grid, background: ctx.sheetBg.colour, figures, empty };
527
+ }
528
+ /**
529
+ * A grid detected from the figures: the figures are the sheet-wide components the grid was
530
+ * detected from, so no figure is lost to a second segmentation. The grid gives each its cell and
531
+ * reading order; its crop is not clipped to the cell, so a tail reaching past the cell's edge into
532
+ * the gutter is kept whole, and anything else in the crop is excluded from it.
533
+ */
534
+ function sliceDetectedGrid(ctx, grid) {
535
+ const { sheet, whole, hasAlpha, tolerance, padding, sheetBg, sheetComponents, cutoff } = ctx;
536
+ const cellOf = (b) => {
537
+ const c = centre(b);
538
+ return {
539
+ row: Math.floor((c.y - grid.origin.y) / grid.cell.height),
540
+ column: Math.floor((c.x - grid.origin.x) / grid.cell.width),
541
+ };
542
+ };
543
+ const { figures: big, merged, dropped } = assign(sheetComponents, cutoff, ctx.verdicts);
544
+ const placed = big.map((f) => {
545
+ const { row, column } = cellOf(f.box);
546
+ const removed = dropped.filter((d) => {
547
+ const at = cellOf(d.box);
548
+ return at.row === row && at.column === column;
549
+ }).length;
550
+ return { row, column, parts: [f, ...merged.get(f)], removed };
551
+ });
552
+ placed.sort((a, b) => a.row - b.row || a.column - b.column);
553
+ const figures = placed.map((p) => {
554
+ const box = cellBoxOf(grid, p.row, p.column, whole);
555
+ const bg = estimate(sheet, hasAlpha, boxBorder(sheet, box));
556
+ return {
557
+ id: 0,
558
+ cell: { row: p.row, column: p.column, box },
559
+ ...build(sheet, { parts: p.parts, removed: p.removed }, whole, box, padding, bg, hasAlpha, tolerance, sheetBg),
560
+ };
561
+ });
562
+ figures.forEach((f, i) => (f.id = i + 1));
563
+ const taken = new Set(placed.map((p) => cellKey(p.row, p.column)));
564
+ const empty = new Map();
565
+ for (let row = 0; row < grid.rows; row++) {
566
+ for (let column = 0; column < grid.columns; column++) {
567
+ if (taken.has(cellKey(row, column)))
568
+ continue;
569
+ const inCell = sheetComponents.filter((c) => {
570
+ const at = cellOf(c.box);
571
+ return at.row === row && at.column === column;
572
+ });
573
+ const largest = Math.max(0, ...inCell.map((c) => c.pixels.length));
574
+ empty.set(cellKey(row, column), emptyReason(largest, cutoff, tolerance));
575
+ }
576
+ }
577
+ return { mode: "grid", grid, background: sheetBg.colour, figures, empty };
578
+ }
579
+ /** No grid: each figure is located on its own and numbered in reading order. */
580
+ function sliceComponents(ctx) {
581
+ const { sheet, whole, hasAlpha, tolerance, padding, sheetComponents, cutoff } = ctx;
582
+ const figures = [];
583
+ const { figures: big, merged, dropped } = assign(sheetComponents, cutoff, ctx.verdicts);
584
+ const ownership = new Map(big.map((f) => [f, 0]));
585
+ for (const d of dropped) {
586
+ const c = centre(d.box);
587
+ let best;
588
+ let bestDistance = Infinity;
589
+ for (const f of big) {
590
+ const fc = centre(f.box);
591
+ const distance = (fc.x - c.x) ** 2 + (fc.y - c.y) ** 2;
592
+ if (distance < bestDistance) {
593
+ bestDistance = distance;
594
+ best = f;
595
+ }
596
+ }
597
+ if (best)
598
+ ownership.set(best, ownership.get(best) + 1);
599
+ }
600
+ const placed = big.map((f) => {
601
+ const parts = [f, ...merged.get(f)];
602
+ const extent = unionBox(parts.map((p) => p.box));
603
+ const bg = estimate(sheet, hasAlpha, ringAround(sheet, extent, 3));
604
+ return { parts, extent, bg, removed: ownership.get(f) };
605
+ });
606
+ // Rows by vertical overlap of boxes; columns by x within a row.
607
+ placed.sort((a, b) => centre(a.extent).y - centre(b.extent).y);
608
+ const rowsOf = [];
609
+ for (const p of placed) {
610
+ const top = p.extent.y;
611
+ const bottom = p.extent.y + p.extent.height;
612
+ const row = rowsOf.find((r) => Math.min(r.bottom, bottom) - Math.max(r.top, top) >= 0.5 * p.extent.height);
613
+ if (row) {
614
+ row.top = Math.min(row.top, top);
615
+ row.bottom = Math.max(row.bottom, bottom);
616
+ row.members.push(p);
617
+ }
618
+ else
619
+ rowsOf.push({ top, bottom, members: [p] });
620
+ }
621
+ rowsOf.sort((a, b) => a.top - b.top);
622
+ rowsOf.forEach((r, rowIndex) => {
623
+ r.members.sort((a, b) => a.extent.x - b.extent.x);
624
+ r.members.forEach((p, column) => {
625
+ const built = build(sheet, { parts: p.parts, removed: p.removed }, whole, whole, padding, p.bg, hasAlpha, tolerance, p.bg);
626
+ figures.push({ id: 0, cell: { row: rowIndex, column, box: built.box }, ...built });
627
+ });
628
+ });
629
+ figures.forEach((f, i) => (f.id = i + 1));
630
+ return { mode: "components", background: ctx.sheetBg.colour, figures, empty: new Map() };
631
+ }
632
+ function where(f) {
633
+ return `figure ${f.id} (row ${f.cell.row + 1}, column ${f.cell.column + 1})`;
634
+ }
635
+ /**
636
+ * Why the count differs from the one asked for: on a grid, each cell among the first `count` in
637
+ * reading order that holds no figure and why; on any sheet, a figure far above the median figure's
638
+ * area (touching figures merged) when too few were found, or far below it (a figure that came
639
+ * apart, or a large label) when too many were.
640
+ */
641
+ function explain(sliced, count) {
642
+ const problems = [];
643
+ const { grid, figures } = sliced;
644
+ if (grid && figures.length < count) {
645
+ for (let k = 0; k < Math.min(count, grid.rows * grid.columns); k++) {
646
+ const row = Math.floor(k / grid.columns);
647
+ const column = k % grid.columns;
648
+ const reason = sliced.empty.get(cellKey(row, column));
649
+ if (reason !== undefined) {
650
+ problems.push(`no figure in the cell at row ${row + 1}, column ${column + 1}: ${reason}`);
651
+ }
652
+ }
653
+ if (count > grid.rows * grid.columns) {
654
+ problems.push(`the ${grid.columns} × ${grid.rows} grid has only ${grid.rows * grid.columns} cells`);
655
+ }
656
+ }
657
+ const areas = figures.map((f) => f.mask.reduce((sum, v) => sum + v, 0));
658
+ const typical = median(areas);
659
+ figures.forEach((f, i) => {
660
+ const ratio = areas[i] / typical;
661
+ if (figures.length < count && ratio >= MERGED_RATIO) {
662
+ problems.push(`${where(f)} is ${ratio.toFixed(1)}× the median figure's area: touching figures may have merged into it`);
663
+ }
664
+ if (figures.length > count && ratio <= SPLIT_RATIO) {
665
+ problems.push(`${where(f)} is ${ratio.toFixed(2)}× the median figure's area: it may be part of a figure that came apart, or a large label`);
666
+ }
667
+ });
668
+ return problems;
669
+ }
670
+ /** The sheet-wide segmentation every slicing mode starts from. */
671
+ function context(sheet, options) {
672
+ const tolerance = options.tolerance ?? 40;
673
+ const whole = { x: 0, y: 0, width: sheet.width, height: sheet.height };
674
+ const hasAlpha = sheet.data.some((_, i) => i % 4 === 3 && sheet.data[i] !== 255);
675
+ const sheetBg = estimate(sheet, hasAlpha, boxBorder(sheet, whole));
676
+ const sheetComponents = components(foreground(sheet, hasAlpha, sheetBg, tolerance, whole), whole, sheet.width);
677
+ const labelFraction = options.labelFraction ?? LABEL_FRACTION;
678
+ return {
679
+ sheet,
680
+ whole,
681
+ hasAlpha,
682
+ tolerance,
683
+ labelFraction,
684
+ cellAlone: options.cellAlone ?? false,
685
+ padding: options.padding ?? 4,
686
+ sheetBg,
687
+ sheetComponents,
688
+ cutoff: figureCutoff(sheetComponents, labelFraction),
689
+ verdicts: new Map((options.blobs ?? []).map((b) => [b.id, b])),
690
+ };
691
+ }
692
+ /** The sheet-wide figures, the judged ones included. */
693
+ function sheetFigures(ctx) {
694
+ return ctx.sheetComponents.filter((c) => isFigure(c, ctx.cutoff, ctx.verdicts));
695
+ }
696
+ /**
697
+ * The grid a caller gave, its rows and cell height filled in. How tall a cell is: given; else the
698
+ * sheet's height over its rows. The rows are given, or follow from the count alone (count figures
699
+ * in rows of `columns`), or, with neither, are the rows the figures sit in (their centres
700
+ * clustered by height, as detectGrid does) when the figures fill that many rows one per cell and
701
+ * that cut leaves them whole, else square cells when those leave them whole. Whatever the
702
+ * figures' centres say, a cut is judged on the cut itself (gridFit): a count whose cells hold two
703
+ * whole figures, or whose edges cut through most of the figures, is refused; with no count and no
704
+ * cut that holds, the sheet is refused and the count asked for, never cut into half-figures.
705
+ */
706
+ function givenGrid(ctx, options) {
707
+ const { sheet } = ctx;
708
+ const { columns } = options.grid;
709
+ const cellWidth = options.grid.cellWidth ?? Math.floor(sheet.width / columns);
710
+ const origin = options.grid.origin ?? { x: 0, y: 0 };
711
+ const height = sheet.height - origin.y;
712
+ const figures = sheetFigures(ctx);
713
+ const cut = (rows) => ({
714
+ columns,
715
+ rows,
716
+ cell: { width: cellWidth, height: Math.floor(height / rows) },
717
+ origin,
718
+ });
719
+ if (options.grid.rows !== undefined || options.grid.cellHeight !== undefined) {
720
+ const cellHeight = options.grid.cellHeight ?? Math.floor(height / (options.grid.rows ?? 1));
721
+ return {
722
+ columns,
723
+ rows: options.grid.rows ?? Math.ceil(height / cellHeight),
724
+ cell: { width: cellWidth, height: cellHeight },
725
+ origin,
726
+ };
727
+ }
728
+ if (options.count !== undefined) {
729
+ const rows = Math.max(1, Math.ceil(options.count / columns));
730
+ const grid = cut(rows);
731
+ const fit = gridFit(sheet.width, figures, grid);
732
+ const problems = [];
733
+ if (fit.crowded.length > 0)
734
+ problems.push(`${fit.crowded.length} cell${fit.crowded.length === 1 ? "" : "s"} of ${columns} × ${rows} hold two or more whole figures (${fit.crowded.slice(0, 3).join("; ")}${fit.crowded.length > 3 ? "; …" : ""}), so the sheet holds more than ${options.count}`);
735
+ if (fit.slicedShare > 0.5)
736
+ problems.push(`${options.count} figures in rows of ${columns} make cells ${grid.cell.height} px tall, whose edges cut through ${Math.round(fit.slicedShare * 100)}% of the figures' area, so the sheet holds fewer`);
737
+ if (problems.length > 0) {
738
+ const sliced = sliceGivenGrid(ctx, grid);
739
+ throw new SliceError(sliced.figures.length, options.count, problems);
740
+ }
741
+ return grid;
742
+ }
743
+ const clustered = figureRows(wholeFigures(figures));
744
+ const candidates = [];
745
+ if (clustered !== undefined) {
746
+ const whole = wholeFigures(figures).length;
747
+ if (whole > columns * (clustered - 1) && whole <= columns * clustered)
748
+ candidates.push(clustered);
749
+ }
750
+ candidates.push(Math.max(1, Math.ceil(height / Math.min(cellWidth, height))));
751
+ const chosen = candidates.map(cut).find((g) => {
752
+ const fit = gridFit(sheet.width, figures, g);
753
+ return fit.slicedShare <= 0.5 && fit.crowded.length === 0;
754
+ });
755
+ if (!chosen)
756
+ throw new InputError(`a grid of ${columns} columns alone does not say how tall a cell is on this sheet: ` +
757
+ `neither the rows the figures sit in nor square cells hold them one whole figure per cell; ` +
758
+ `give the frame count (--frames) so the rows follow from it`);
759
+ return chosen;
760
+ }
761
+ /**
762
+ * Slices a sheet into its figures. With `options.count`, a sheet that does not hold exactly that
763
+ * many figures is refused with a SliceError that names the cells or figures behind the
764
+ * difference, so a short or long strip is never returned silently.
765
+ */
766
+ export function sliceSheet(sheet, options = {}) {
767
+ const ctx = context(sheet, options);
768
+ let sliced;
769
+ if (options.grid) {
770
+ sliced = sliceGivenGrid(ctx, givenGrid(ctx, { ...options, grid: options.grid }));
771
+ }
772
+ else {
773
+ const grid = options.components ? undefined : detectGrid(sheet, sheetFigures(ctx));
774
+ sliced = grid ? sliceDetectedGrid(ctx, grid) : sliceComponents(ctx);
775
+ }
776
+ if (options.count !== undefined && sliced.figures.length !== options.count) {
777
+ throw new SliceError(sliced.figures.length, options.count, explain(sliced, options.count));
778
+ }
779
+ return {
780
+ mode: sliced.mode,
781
+ ...(sliced.grid !== undefined && { grid: sliced.grid }),
782
+ background: sliced.background,
783
+ figures: sliced.figures,
784
+ };
785
+ }
786
+ function gapBetween(a, b) {
787
+ const dx = Math.max(0, a.x - (b.x + b.width), b.x - (a.x + a.width));
788
+ const dy = Math.max(0, a.y - (b.y + b.height), b.y - (a.y + a.height));
789
+ return Math.max(dx, dy);
790
+ }
791
+ /** The parts under `cutoff` that are at least `least` px, or outside every figure's box. */
792
+ function ambiguousIn(parts, cutoff, least) {
793
+ const figures = parts.filter((c) => c.pixels.length >= cutoff);
794
+ return parts.filter((c) => c.pixels.length < cutoff &&
795
+ (c.pixels.length >= least || !figures.some((f) => inside(c.box, f.box, 2))));
796
+ }
797
+ /**
798
+ * The blobs on a sheet that its areas leave ambiguous, largest first, with the crop each is shown
799
+ * in. A clean sheet (every component a figure, or a part inside a figure's box) has none. Slicing
800
+ * an ambiguous blob with no verdict keeps the area rule.
801
+ */
802
+ export function ambiguousBlobs(sheet, options = {}) {
803
+ const ctx = context(sheet, { ...options, blobs: [] });
804
+ let grid;
805
+ let candidates;
806
+ let figures;
807
+ if (options.grid) {
808
+ try {
809
+ grid = givenGrid(ctx, { ...options, grid: options.grid });
810
+ }
811
+ catch {
812
+ return [];
813
+ }
814
+ const cells = [];
815
+ for (let row = 0; row < grid.rows; row++) {
816
+ for (let column = 0; column < grid.columns; column++) {
817
+ const box = cellBoxOf(grid, row, column, ctx.whole);
818
+ if (box.width === 0 || box.height === 0)
819
+ continue;
820
+ const bg = estimate(sheet, ctx.hasAlpha, boxBorder(sheet, box));
821
+ cells.push(components(foreground(sheet, ctx.hasAlpha, bg, ctx.tolerance, box), box, sheet.width));
822
+ }
823
+ }
824
+ // As sliceGivenGrid decides: one cutoff over every cell's parts, figure boxes per cell.
825
+ const all = cells.flat();
826
+ const cutoff = figureCutoff(all, ctx.labelFraction);
827
+ const least = ctx.labelFraction * Math.max(0, ...all.map((p) => p.pixels.length));
828
+ figures = all.filter((c) => c.pixels.length >= cutoff);
829
+ candidates = cells.flatMap((parts) => ambiguousIn(parts, cutoff, least));
830
+ }
831
+ else {
832
+ figures = sheetFigures(ctx);
833
+ const largest = Math.max(0, ...ctx.sheetComponents.map((p) => p.pixels.length));
834
+ candidates = ambiguousIn(ctx.sheetComponents, ctx.cutoff, ctx.labelFraction * largest);
835
+ if (!options.components)
836
+ grid = detectGrid(sheet, figures);
837
+ }
838
+ if (candidates.length === 0)
839
+ return [];
840
+ // Group close candidates: within 5% of the median figure's height, at least 2 px.
841
+ const gap = Math.max(2, Math.round(0.05 * median(figures.map((f) => f.box.height))));
842
+ const group = candidates.map((_, i) => i);
843
+ const root = (i) => {
844
+ while (group[i] !== i)
845
+ i = group[i] = group[group[i]];
846
+ return i;
847
+ };
848
+ for (let i = 0; i < candidates.length; i++) {
849
+ for (let j = i + 1; j < candidates.length; j++) {
850
+ if (gapBetween(candidates[i].box, candidates[j].box) <= gap)
851
+ group[root(i)] = root(j);
852
+ }
853
+ }
854
+ const groups = new Map();
855
+ candidates.forEach((c, i) => {
856
+ const r = root(i);
857
+ groups.set(r, [...(groups.get(r) ?? []), c]);
858
+ });
859
+ const figureHeight = median(figures.map((f) => f.box.height)) || sheet.height;
860
+ const blobs = [...groups.values()].map((members) => {
861
+ const box = unionBox(members.map((m) => m.box));
862
+ const host = nearest(figures, { pixels: [], box });
863
+ let crop;
864
+ if (grid) {
865
+ const c = centre(box);
866
+ const row = Math.floor((c.y - grid.origin.y) / grid.cell.height);
867
+ const column = Math.floor((c.x - grid.origin.x) / grid.cell.width);
868
+ crop = unionBox([cellBoxOf(grid, row, column, ctx.whole), box]);
869
+ }
870
+ else {
871
+ const pad = Math.round(0.25 * figureHeight);
872
+ const span = unionBox(host ? [box, host.box] : [box]);
873
+ crop = {
874
+ x: span.x - pad,
875
+ y: span.y - pad,
876
+ width: span.width + 2 * pad,
877
+ height: span.height + 2 * pad,
878
+ };
879
+ }
880
+ crop = clipBox(crop, ctx.whole);
881
+ if (crop.width === 0 || crop.height === 0)
882
+ crop = box;
883
+ return {
884
+ ids: [...members].sort((a, b) => b.pixels.length - a.pixels.length).map(blobId),
885
+ box,
886
+ area: members.reduce((sum, m) => sum + m.pixels.length, 0),
887
+ crop,
888
+ figureArea: Math.max(0, ...figures.map((f) => f.pixels.length)),
889
+ };
890
+ });
891
+ return blobs.sort((a, b) => b.area - a.area || a.box.y - b.box.y || a.box.x - b.box.x);
892
+ }
893
+ /**
894
+ * The whole image as one figure, for matting an image that was not sliced (generated or hand-made).
895
+ * Every pixel farther than `tolerance` from the border's median colour (or, on a transparent
896
+ * border, every pixel at alpha 128 or more) is the figure's: no component is dropped as a label or
897
+ * speck, so small detached parts of an author's image are kept.
898
+ */
899
+ export function wholeImageFigure(image, tolerance = 40) {
900
+ const whole = { x: 0, y: 0, width: image.width, height: image.height };
901
+ const hasAlpha = image.data.some((_, i) => i % 4 === 3 && image.data[i] !== 255);
902
+ const bg = estimate(image, hasAlpha, boxBorder(image, whole));
903
+ return {
904
+ id: 1,
905
+ cell: { row: 0, column: 0, box: whole },
906
+ box: whole,
907
+ crop: image,
908
+ mask: foreground(image, hasAlpha, bg, tolerance, whole),
909
+ background: bg.colour,
910
+ transforms: [],
911
+ };
912
+ }