vibedgames 0.10.0 → 0.10.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.
- package/assets/rebuild-object/.cache/spec-search/core_3d.json +1 -0
- package/assets/rebuild-object/.cache/spec-search/cs2.json +1 -0
- package/assets/rebuild-object/LICENSE +201 -0
- package/assets/rebuild-object/NOTICE +7 -0
- package/assets/rebuild-object/docs/materials/material-reference.json +2324 -0
- package/assets/rebuild-object/docs/specs/render-profile.v2.example.json +63 -0
- package/assets/rebuild-object/docs/specs/render-profile.v2.schema.json +56 -0
- package/assets/rebuild-object/docs/specs/vocabulary/README.md +86 -0
- package/assets/rebuild-object/docs/specs/vocabulary/core_3d.jsonl +12 -0
- package/assets/rebuild-object/docs/specs/vocabulary/core_3d_reconstruction.jsonl +37 -0
- package/assets/rebuild-object/docs/specs/vocabulary/cs2.jsonl +8 -0
- package/assets/rebuild-object/docs/specs/vocabulary/cs2_reconstruction.jsonl +62 -0
- package/assets/rebuild-object/forge/_shared/artifact_cache.py +54 -0
- package/assets/rebuild-object/forge/_shared/chirality.py +272 -0
- package/assets/rebuild-object/forge/_shared/color_metrics.py +107 -0
- package/assets/rebuild-object/forge/_shared/feature_acceptance_policy.py +113 -0
- package/assets/rebuild-object/forge/_shared/image_hash.py +113 -0
- package/assets/rebuild-object/forge/_shared/jpeg.py +448 -0
- package/assets/rebuild-object/forge/_shared/material_physics.py +253 -0
- package/assets/rebuild-object/forge/_shared/pipeline_routing.py +177 -0
- package/assets/rebuild-object/forge/_shared/scalp_field.py +302 -0
- package/assets/rebuild-object/forge/_shared/sdf_primitives.py +172 -0
- package/assets/rebuild-object/forge/_shared/spec_search.py +1792 -0
- package/assets/rebuild-object/forge/_shared/spec_search_profiles.json +58 -0
- package/assets/rebuild-object/forge/_shared/status_banner.py +35 -0
- package/assets/rebuild-object/forge/_shared/subdivision.py +232 -0
- package/assets/rebuild-object/forge/_shared/vertex_paint.py +236 -0
- package/assets/rebuild-object/forge/_shared/workflow_state.py +406 -0
- package/assets/rebuild-object/forge/materials/__init__.py +17 -0
- package/assets/rebuild-object/forge/materials/compatibility.py +106 -0
- package/assets/rebuild-object/forge/materials/reference.py +240 -0
- package/assets/rebuild-object/forge/next.py +116 -0
- package/assets/rebuild-object/forge/report.py +38 -0
- package/assets/rebuild-object/forge/requirements.txt +6 -0
- package/assets/rebuild-object/forge/stage1_intake/analyze_texture.py +255 -0
- package/assets/rebuild-object/forge/stage1_intake/bind_detail_properties.py +99 -0
- package/assets/rebuild-object/forge/stage1_intake/build_detail_inventory.py +349 -0
- package/assets/rebuild-object/forge/stage1_intake/camera_fitting_math.py +133 -0
- package/assets/rebuild-object/forge/stage1_intake/camera_fitting_solver.py +197 -0
- package/assets/rebuild-object/forge/stage1_intake/camera_fitting_types.py +291 -0
- package/assets/rebuild-object/forge/stage1_intake/camera_image_helpers.py +133 -0
- package/assets/rebuild-object/forge/stage1_intake/check_intake_correctness.py +116 -0
- package/assets/rebuild-object/forge/stage1_intake/check_reference_admission.py +203 -0
- package/assets/rebuild-object/forge/stage1_intake/cs2_foundation.py +139 -0
- package/assets/rebuild-object/forge/stage1_intake/cs2_manifest.py +230 -0
- package/assets/rebuild-object/forge/stage1_intake/cs2_review_contract.py +42 -0
- package/assets/rebuild-object/forge/stage1_intake/delight_albedo.py +352 -0
- package/assets/rebuild-object/forge/stage1_intake/detect_cs2.py +276 -0
- package/assets/rebuild-object/forge/stage1_intake/detect_reference_effects.py +275 -0
- package/assets/rebuild-object/forge/stage1_intake/extract_cs2_textures.py +126 -0
- package/assets/rebuild-object/forge/stage1_intake/extract_gradient_stops.py +149 -0
- package/assets/rebuild-object/forge/stage1_intake/extract_hair_evidence.py +385 -0
- package/assets/rebuild-object/forge/stage1_intake/extract_landmarks.py +456 -0
- package/assets/rebuild-object/forge/stage1_intake/extract_part_color_recipe.py +496 -0
- package/assets/rebuild-object/forge/stage1_intake/extract_pbr_evidence.py +843 -0
- package/assets/rebuild-object/forge/stage1_intake/fetch_cs2_metadata.py +147 -0
- package/assets/rebuild-object/forge/stage1_intake/label_glb_nodes.py +318 -0
- package/assets/rebuild-object/forge/stage1_intake/locate_cs2_vpk.py +73 -0
- package/assets/rebuild-object/forge/stage1_intake/material_region_analysis.py +227 -0
- package/assets/rebuild-object/forge/stage1_intake/probe_glb.py +357 -0
- package/assets/rebuild-object/forge/stage1_intake/probe_image.py +168 -0
- package/assets/rebuild-object/forge/stage1_intake/run_vision_adapter.py +45 -0
- package/assets/rebuild-object/forge/stage1_intake/search_specs.py +258 -0
- package/assets/rebuild-object/forge/stage1_intake/semantic_decomposition.py +119 -0
- package/assets/rebuild-object/forge/stage1_intake/solve_camera_pose.py +254 -0
- package/assets/rebuild-object/forge/stage2_spec/apply_material_analysis.py +192 -0
- package/assets/rebuild-object/forge/stage2_spec/cs2_adapters.py +62 -0
- package/assets/rebuild-object/forge/stage2_spec/derive_geometry.py +118 -0
- package/assets/rebuild-object/forge/stage2_spec/hair_profile.py +306 -0
- package/assets/rebuild-object/forge/stage2_spec/humanoid_proportions.py +214 -0
- package/assets/rebuild-object/forge/stage2_spec/new_pre_spec_assessment.py +343 -0
- package/assets/rebuild-object/forge/stage2_spec/new_sculpt_spec.py +2396 -0
- package/assets/rebuild-object/forge/stage2_spec/validate_sculpt_spec.py +2871 -0
- package/assets/rebuild-object/forge/stage3_build/bake_projected_texture.py +181 -0
- package/assets/rebuild-object/forge/stage3_build/decimate.py +272 -0
- package/assets/rebuild-object/forge/stage3_build/generate_threejs_factory.py +4099 -0
- package/assets/rebuild-object/forge/stage3_build/module_cache.py +155 -0
- package/assets/rebuild-object/forge/stage3_build/morph_targets.py +178 -0
- package/assets/rebuild-object/forge/stage3_build/orchestrate_passes.py +653 -0
- package/assets/rebuild-object/forge/stage3_build/uv_unwrap.py +622 -0
- package/assets/rebuild-object/forge/stage3_build/visual_hull.py +357 -0
- package/assets/rebuild-object/forge/stage4_review/_fit_divine_eye.py +64 -0
- package/assets/rebuild-object/forge/stage4_review/append_review.py +453 -0
- package/assets/rebuild-object/forge/stage4_review/attachment_anchor.py +349 -0
- package/assets/rebuild-object/forge/stage4_review/calibrate_eye.py +131 -0
- package/assets/rebuild-object/forge/stage4_review/check_part_coverage.py +298 -0
- package/assets/rebuild-object/forge/stage4_review/compare_region_passes.py +169 -0
- package/assets/rebuild-object/forge/stage4_review/correction_loop.py +258 -0
- package/assets/rebuild-object/forge/stage4_review/cs2_review.py +224 -0
- package/assets/rebuild-object/forge/stage4_review/diagnose_render.py +356 -0
- package/assets/rebuild-object/forge/stage4_review/diagnose_render_multi_angle.py +143 -0
- package/assets/rebuild-object/forge/stage4_review/divine_eye.py +461 -0
- package/assets/rebuild-object/forge/stage4_review/fit_params.py +381 -0
- package/assets/rebuild-object/forge/stage4_review/geometry_integrity.py +294 -0
- package/assets/rebuild-object/forge/stage4_review/hair_gate.py +216 -0
- package/assets/rebuild-object/forge/stage4_review/interior_difference.py +188 -0
- package/assets/rebuild-object/forge/stage4_review/joint_loops.py +209 -0
- package/assets/rebuild-object/forge/stage4_review/make_comparison_sheet.py +292 -0
- package/assets/rebuild-object/forge/stage4_review/material_comparator.py +181 -0
- package/assets/rebuild-object/forge/stage4_review/material_feedback.py +143 -0
- package/assets/rebuild-object/forge/stage4_review/material_gate.py +126 -0
- package/assets/rebuild-object/forge/stage4_review/material_views.py +184 -0
- package/assets/rebuild-object/forge/stage4_review/mesh_reference_compare.py +362 -0
- package/assets/rebuild-object/forge/stage4_review/multi_pass.py +102 -0
- package/assets/rebuild-object/forge/stage4_review/objectness.py +143 -0
- package/assets/rebuild-object/forge/stage4_review/pairwise_penetration.py +376 -0
- package/assets/rebuild-object/forge/stage4_review/per_feature.py +215 -0
- package/assets/rebuild-object/forge/stage4_review/render_bridge.py +556 -0
- package/assets/rebuild-object/forge/stage4_review/scalp_exposure.py +257 -0
- package/assets/rebuild-object/forge/stage4_review/self_intersection.py +592 -0
- package/assets/rebuild-object/forge/stage4_review/swept_arc_gate.py +336 -0
- package/assets/rebuild-object/forge/stage4_review/turntable_gate.py +415 -0
- package/assets/rebuild-object/forge/stage4_review/validate_render_profile.py +182 -0
- package/assets/rebuild-object/forge/stage4_review/vertex_region_gate.py +456 -0
- package/assets/rebuild-object/forge/stage4_review/vlm_gate.py +196 -0
- package/assets/rebuild-object/forge/stage5_rig/NOTES.md +149 -0
- package/assets/rebuild-object/forge/stage5_rig/__init__.py +6 -0
- package/assets/rebuild-object/forge/stage5_rig/emit_rig.py +420 -0
- package/assets/rebuild-object/forge/stage5_rig/geodesic_skinning.py +508 -0
- package/assets/rebuild-object/forge/stage5_rig/rig_spec.py +174 -0
- package/assets/rebuild-object/forge/stage5_rig/validate_rig_payload.py +195 -0
- package/assets/rebuild-object/forge/state.py +98 -0
- package/assets/rebuild-object/steps.json +381 -0
- package/dist/{chunk-FXXVXDTW.js → chunk-24TITAFS.js} +1 -1
- package/dist/{chunk-OGX4DKIS.js → chunk-2C5PG2QO.js} +1 -1
- package/dist/{chunk-HYOXGNWS.js → chunk-2DEVOKI7.js} +1 -1
- package/dist/{chunk-RRI23NK2.js → chunk-2GHPYT2P.js} +1 -1
- package/dist/{chunk-S7HYWITX.js → chunk-2HXGMXGC.js} +1 -1
- package/dist/{chunk-N2UHAQXC.js → chunk-36FORP7T.js} +0 -1
- package/dist/{chunk-VB4WLJR2.js → chunk-3IXRQTRF.js} +1 -1
- package/dist/{chunk-2KVQJKIZ.js → chunk-4ENHS32B.js} +23 -36
- package/dist/{chunk-SXYQL4GK.js → chunk-4YHA32OE.js} +1 -1
- package/dist/{chunk-4FJXCS3K.js → chunk-4ZGQKPK5.js} +1 -1
- package/dist/{chunk-SBXCK2I2.js → chunk-6CLUPFQA.js} +2 -2
- package/dist/{chunk-TXOE5UY5.js → chunk-7WTF2BA4.js} +1 -1
- package/dist/{chunk-R2WVORGD.js → chunk-AAXZNK4Z.js} +1 -1
- package/dist/{chunk-DOOQUFPG.js → chunk-ALLBT4FB.js} +1 -1
- package/dist/{chunk-HRKVAXNN.js → chunk-BAZDPPIG.js} +5 -5
- package/dist/{chunk-RPJR3S5S.js → chunk-CYDENLZ4.js} +5 -8
- package/dist/{chunk-I5LELJSK.js → chunk-DKSUD44G.js} +1 -1
- package/dist/{chunk-43P4U7CB.js → chunk-DWWSJKTE.js} +1 -1
- package/dist/{chunk-SUSX3QIA.js → chunk-E72TFJZP.js} +1 -1
- package/dist/{chunk-E3FJUPLT.js → chunk-EUAAQMUA.js} +2 -0
- package/dist/{chunk-U6DTE5IX.js → chunk-F7LYJSZE.js} +1 -1
- package/dist/{chunk-MEAO33XW.js → chunk-FMON3C74.js} +8 -8
- package/dist/{chunk-GLEW7VJQ.js → chunk-FPILHRLZ.js} +1 -1
- package/dist/{chunk-OPUCUUID.js → chunk-G63MXDX2.js} +4 -0
- package/dist/{chunk-4UU622RK.js → chunk-GGO6ZYYI.js} +1 -3
- package/dist/{chunk-2ZKS7V72.js → chunk-GZCRYYS6.js} +5 -5
- package/dist/{chunk-E7NDOZ27.js → chunk-LE4GVTYI.js} +8 -8
- package/dist/{chunk-RKS7SI5L.js → chunk-NO6KPV64.js} +1 -1
- package/dist/{chunk-KJK5CB6E.js → chunk-NXW3Z4Y4.js} +1 -1
- package/dist/{chunk-7RPLQWR2.js → chunk-OHOAZGO4.js} +1 -1
- package/dist/chunk-PYJNI5WD.js +83 -0
- package/dist/{chunk-NCX6LYNJ.js → chunk-Q4XAGTFX.js} +1 -1
- package/dist/{chunk-3UGGMFJJ.js → chunk-QMZBSVKF.js} +1 -1
- package/dist/{chunk-ABTUEIRC.js → chunk-QO66UBOS.js} +2 -2
- package/dist/{chunk-LONQWRTR.js → chunk-S4KVGQQO.js} +8 -8
- package/dist/{chunk-HX5Q63UA.js → chunk-T777EXAO.js} +1 -1
- package/dist/{chunk-UQ7VO6B3.js → chunk-TZZNRA3S.js} +1 -1
- package/dist/{chunk-CTY2WZQK.js → chunk-UC3QLWMQ.js} +1 -1
- package/dist/{chunk-MR5KIFI7.js → chunk-UDJH2RED.js} +3 -3
- package/dist/{chunk-ZHLPE6LI.js → chunk-UIKYK7KI.js} +1 -1
- package/dist/{chunk-KAGVI3CE.js → chunk-UJRSQO2B.js} +1 -1
- package/dist/{chunk-Q2QC6KAZ.js → chunk-V7LEYVXB.js} +29 -25
- package/dist/{chunk-DB7WVFS7.js → chunk-WRMQEXUC.js} +1 -1
- package/dist/{chunk-HSMG6U7J.js → chunk-WXSBNC67.js} +3 -9
- package/dist/{chunk-QLCVLCPM.js → chunk-X4QIOXNQ.js} +1 -1
- package/dist/{chunk-5YPLKGY3.js → chunk-Y6WHUK2O.js} +1 -1
- package/dist/{chunk-VZEJ2ZHE.js → chunk-Z3E7ORGQ.js} +5 -5
- package/dist/index.js +20 -20
- package/package.json +32 -22
- package/dist/{chunk-Q3XQPNF6.js → chunk-BN7TR6OR.js} +3 -3
- package/dist/{chunk-LORGE2NB.js → chunk-U2NCN5RO.js} +3 -3
- package/dist/{chunk-UPHB5HWO.js → chunk-VQTTSPA3.js} +3 -3
|
@@ -0,0 +1,456 @@
|
|
|
1
|
+
#!/usr/bin/env python3
|
|
2
|
+
"""Overlay a labelled proportion/landmark guide grid on a reference image and scaffold anatomy.
|
|
3
|
+
|
|
4
|
+
Draws head-unit ticks, a rule-of-thirds grid, a center symmetry axis, default face-line
|
|
5
|
+
guides (hairline/eye/nose/mouth), and default shoulder/hip lines onto a copy of the
|
|
6
|
+
reference (see docs/UPGRADE_PLAN.md 5.3-5.4 and grimoire/character/reconstruction.md),
|
|
7
|
+
then emits an anatomy skeleton JSON for the agent to fill from what the overlay reveals.
|
|
8
|
+
The drawn lines are generic starting positions, not measurements - the agent's vision
|
|
9
|
+
supplies the actual proportions, pose, and landmark coordinates.
|
|
10
|
+
"""
|
|
11
|
+
|
|
12
|
+
from __future__ import annotations
|
|
13
|
+
|
|
14
|
+
import argparse
|
|
15
|
+
import json
|
|
16
|
+
import shutil
|
|
17
|
+
import struct
|
|
18
|
+
import subprocess
|
|
19
|
+
import sys
|
|
20
|
+
import tempfile
|
|
21
|
+
import zlib
|
|
22
|
+
from pathlib import Path
|
|
23
|
+
|
|
24
|
+
sys.path.insert(0, str(Path(__file__).resolve().parent.parent / "_shared"))
|
|
25
|
+
from jpeg import UnsupportedJpeg, decode_jpeg, is_jpeg # noqa: E402
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
PNG_SIGNATURE = b"\x89PNG\r\n\x1a\n"
|
|
29
|
+
|
|
30
|
+
MARGIN = 44
|
|
31
|
+
|
|
32
|
+
COLOR_THIRDS = (150, 150, 150)
|
|
33
|
+
COLOR_HEAD_UNIT = (60, 120, 220)
|
|
34
|
+
COLOR_HAIRLINE = (210, 80, 210)
|
|
35
|
+
COLOR_EYELINE = (230, 60, 60)
|
|
36
|
+
COLOR_NOSEBASE = (240, 150, 30)
|
|
37
|
+
COLOR_MOUTHLINE = (40, 170, 90)
|
|
38
|
+
COLOR_SHOULDER = (30, 140, 200)
|
|
39
|
+
COLOR_HIP = (170, 110, 40)
|
|
40
|
+
COLOR_CENTER = (20, 20, 20)
|
|
41
|
+
|
|
42
|
+
FONT_3X5 = {
|
|
43
|
+
"0": ["###", "#.#", "#.#", "#.#", "###"],
|
|
44
|
+
"1": [".#.", "##.", ".#.", ".#.", "###"],
|
|
45
|
+
"2": ["###", "..#", "###", "#..", "###"],
|
|
46
|
+
"3": ["###", "..#", "###", "..#", "###"],
|
|
47
|
+
"4": ["#.#", "#.#", "###", "..#", "..#"],
|
|
48
|
+
"5": ["###", "#..", "###", "..#", "###"],
|
|
49
|
+
"6": ["###", "#..", "###", "#.#", "###"],
|
|
50
|
+
"7": ["###", "..#", "..#", "..#", "..#"],
|
|
51
|
+
"8": ["###", "#.#", "###", "#.#", "###"],
|
|
52
|
+
"9": ["###", "#.#", "###", "..#", "###"],
|
|
53
|
+
"H": ["#.#", "#.#", "###", "#.#", "#.#"],
|
|
54
|
+
"E": ["###", "#..", "##.", "#..", "###"],
|
|
55
|
+
"N": ["#.#", "##.", "#.#", ".##", "#.#"],
|
|
56
|
+
"M": ["#.#", "###", "###", "#.#", "#.#"],
|
|
57
|
+
"S": [".##", "#..", ".#.", "..#", "##."],
|
|
58
|
+
"P": ["##.", "#.#", "##.", "#..", "#.."],
|
|
59
|
+
"C": [".##", "#..", "#..", "#..", ".##"],
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
|
|
63
|
+
def paeth_predictor(a: int, b: int, c: int) -> int:
|
|
64
|
+
p = a + b - c
|
|
65
|
+
pa = abs(p - a)
|
|
66
|
+
pb = abs(p - b)
|
|
67
|
+
pc = abs(p - c)
|
|
68
|
+
if pa <= pb and pa <= pc:
|
|
69
|
+
return a
|
|
70
|
+
if pb <= pc:
|
|
71
|
+
return b
|
|
72
|
+
return c
|
|
73
|
+
|
|
74
|
+
|
|
75
|
+
def read_png(path: Path) -> tuple[int, int, list[tuple[int, int, int, int]]]:
|
|
76
|
+
data = path.read_bytes()
|
|
77
|
+
if not data.startswith(PNG_SIGNATURE):
|
|
78
|
+
raise ValueError("not a PNG file")
|
|
79
|
+
cursor = len(PNG_SIGNATURE)
|
|
80
|
+
width = height = bit_depth = color_type = interlace = None
|
|
81
|
+
idat = bytearray()
|
|
82
|
+
while cursor + 8 <= len(data):
|
|
83
|
+
length = struct.unpack(">I", data[cursor : cursor + 4])[0]
|
|
84
|
+
chunk_type = data[cursor + 4 : cursor + 8]
|
|
85
|
+
chunk_data = data[cursor + 8 : cursor + 8 + length]
|
|
86
|
+
cursor += 12 + length
|
|
87
|
+
if chunk_type == b"IHDR":
|
|
88
|
+
width, height, bit_depth, color_type, _, _, interlace = struct.unpack(">IIBBBBB", chunk_data)
|
|
89
|
+
elif chunk_type == b"IDAT":
|
|
90
|
+
idat.extend(chunk_data)
|
|
91
|
+
elif chunk_type == b"IEND":
|
|
92
|
+
break
|
|
93
|
+
if width is None or height is None or bit_depth != 8 or interlace != 0:
|
|
94
|
+
raise ValueError("unsupported PNG; expected 8-bit non-interlaced image")
|
|
95
|
+
channels_by_type = {0: 1, 2: 3, 4: 2, 6: 4}
|
|
96
|
+
if color_type not in channels_by_type:
|
|
97
|
+
raise ValueError("unsupported PNG color type; convert to RGB/RGBA first")
|
|
98
|
+
channels = channels_by_type[color_type]
|
|
99
|
+
row_bytes = width * channels
|
|
100
|
+
raw = zlib.decompress(bytes(idat))
|
|
101
|
+
rows: list[bytearray] = []
|
|
102
|
+
offset = 0
|
|
103
|
+
previous = bytearray(row_bytes)
|
|
104
|
+
for _ in range(height):
|
|
105
|
+
filter_type = raw[offset]
|
|
106
|
+
offset += 1
|
|
107
|
+
row = bytearray(raw[offset : offset + row_bytes])
|
|
108
|
+
offset += row_bytes
|
|
109
|
+
for index in range(row_bytes):
|
|
110
|
+
left = row[index - channels] if index >= channels else 0
|
|
111
|
+
up = previous[index]
|
|
112
|
+
up_left = previous[index - channels] if index >= channels else 0
|
|
113
|
+
if filter_type == 1:
|
|
114
|
+
row[index] = (row[index] + left) & 0xFF
|
|
115
|
+
elif filter_type == 2:
|
|
116
|
+
row[index] = (row[index] + up) & 0xFF
|
|
117
|
+
elif filter_type == 3:
|
|
118
|
+
row[index] = (row[index] + ((left + up) // 2)) & 0xFF
|
|
119
|
+
elif filter_type == 4:
|
|
120
|
+
row[index] = (row[index] + paeth_predictor(left, up, up_left)) & 0xFF
|
|
121
|
+
elif filter_type != 0:
|
|
122
|
+
raise ValueError(f"unsupported PNG filter {filter_type}")
|
|
123
|
+
rows.append(row)
|
|
124
|
+
previous = row
|
|
125
|
+
pixels: list[tuple[int, int, int, int]] = []
|
|
126
|
+
for row in rows:
|
|
127
|
+
for x in range(width):
|
|
128
|
+
base = x * channels
|
|
129
|
+
if color_type == 0:
|
|
130
|
+
gray = row[base]
|
|
131
|
+
pixels.append((gray, gray, gray, 255))
|
|
132
|
+
elif color_type == 2:
|
|
133
|
+
pixels.append((row[base], row[base + 1], row[base + 2], 255))
|
|
134
|
+
elif color_type == 4:
|
|
135
|
+
gray = row[base]
|
|
136
|
+
pixels.append((gray, gray, gray, row[base + 1]))
|
|
137
|
+
elif color_type == 6:
|
|
138
|
+
pixels.append((row[base], row[base + 1], row[base + 2], row[base + 3]))
|
|
139
|
+
return width, height, pixels
|
|
140
|
+
|
|
141
|
+
|
|
142
|
+
def write_png_rgb(path: Path, width: int, height: int, pixels: list[tuple[int, int, int]]) -> None:
|
|
143
|
+
if len(pixels) != width * height:
|
|
144
|
+
raise ValueError("pixel payload has the wrong size")
|
|
145
|
+
|
|
146
|
+
def chunk(kind: bytes, payload: bytes) -> bytes:
|
|
147
|
+
checksum = zlib.crc32(kind)
|
|
148
|
+
checksum = zlib.crc32(payload, checksum) & 0xFFFFFFFF
|
|
149
|
+
return struct.pack(">I", len(payload)) + kind + payload + struct.pack(">I", checksum)
|
|
150
|
+
|
|
151
|
+
scanlines = bytearray()
|
|
152
|
+
for y in range(height):
|
|
153
|
+
scanlines.append(0)
|
|
154
|
+
for red, green, blue in pixels[y * width : (y + 1) * width]:
|
|
155
|
+
scanlines.extend((red, green, blue))
|
|
156
|
+
path.parent.mkdir(parents=True, exist_ok=True)
|
|
157
|
+
path.write_bytes(
|
|
158
|
+
PNG_SIGNATURE
|
|
159
|
+
+ chunk(b"IHDR", struct.pack(">IIBBBBB", width, height, 8, 2, 0, 0, 0))
|
|
160
|
+
+ chunk(b"IDAT", zlib.compress(bytes(scanlines), level=6))
|
|
161
|
+
+ chunk(b"IEND", b"")
|
|
162
|
+
)
|
|
163
|
+
|
|
164
|
+
|
|
165
|
+
def load_image(path: Path) -> tuple[int, int, list[tuple[int, int, int, int]]]:
|
|
166
|
+
try:
|
|
167
|
+
return read_png(path)
|
|
168
|
+
except Exception as direct_error:
|
|
169
|
+
jpeg_bytes = path.read_bytes()
|
|
170
|
+
if is_jpeg(jpeg_bytes):
|
|
171
|
+
try:
|
|
172
|
+
return decode_jpeg(jpeg_bytes)[:3]
|
|
173
|
+
except UnsupportedJpeg:
|
|
174
|
+
pass
|
|
175
|
+
sips = shutil.which("sips")
|
|
176
|
+
if not sips:
|
|
177
|
+
raise ValueError(f"could not decode {path.name} as PNG and sips is unavailable: {direct_error}") from direct_error
|
|
178
|
+
with tempfile.TemporaryDirectory() as tmpdir:
|
|
179
|
+
converted = Path(tmpdir) / "converted.png"
|
|
180
|
+
result = subprocess.run(
|
|
181
|
+
[sips, "-s", "format", "png", str(path), "--out", str(converted)],
|
|
182
|
+
capture_output=True,
|
|
183
|
+
text=True,
|
|
184
|
+
check=False,
|
|
185
|
+
)
|
|
186
|
+
if result.returncode != 0:
|
|
187
|
+
raise ValueError(result.stderr.strip() or result.stdout.strip() or "sips conversion failed")
|
|
188
|
+
return read_png(converted)
|
|
189
|
+
|
|
190
|
+
|
|
191
|
+
def composite_over_white(pixel: tuple[int, int, int, int]) -> tuple[int, int, int]:
|
|
192
|
+
red, green, blue, alpha = pixel
|
|
193
|
+
mix = alpha / 255.0
|
|
194
|
+
return (
|
|
195
|
+
round(red * mix + 255 * (1 - mix)),
|
|
196
|
+
round(green * mix + 255 * (1 - mix)),
|
|
197
|
+
round(blue * mix + 255 * (1 - mix)),
|
|
198
|
+
)
|
|
199
|
+
|
|
200
|
+
|
|
201
|
+
def set_pixel(canvas: list[tuple[int, int, int]], width: int, height: int, x: int, y: int, color: tuple[int, int, int]) -> None:
|
|
202
|
+
if 0 <= x < width and 0 <= y < height:
|
|
203
|
+
canvas[y * width + x] = color
|
|
204
|
+
|
|
205
|
+
|
|
206
|
+
def draw_glyph(
|
|
207
|
+
canvas: list[tuple[int, int, int]],
|
|
208
|
+
width: int,
|
|
209
|
+
height: int,
|
|
210
|
+
x0: int,
|
|
211
|
+
y0: int,
|
|
212
|
+
glyph: list[str],
|
|
213
|
+
color: tuple[int, int, int],
|
|
214
|
+
scale: int,
|
|
215
|
+
) -> None:
|
|
216
|
+
for row_index, row in enumerate(glyph):
|
|
217
|
+
for col_index, mark in enumerate(row):
|
|
218
|
+
if mark != "#":
|
|
219
|
+
continue
|
|
220
|
+
for dy in range(scale):
|
|
221
|
+
for dx in range(scale):
|
|
222
|
+
set_pixel(canvas, width, height, x0 + col_index * scale + dx, y0 + row_index * scale + dy, color)
|
|
223
|
+
|
|
224
|
+
|
|
225
|
+
def draw_text(
|
|
226
|
+
canvas: list[tuple[int, int, int]],
|
|
227
|
+
width: int,
|
|
228
|
+
height: int,
|
|
229
|
+
x0: int,
|
|
230
|
+
y0: int,
|
|
231
|
+
text: str,
|
|
232
|
+
color: tuple[int, int, int],
|
|
233
|
+
scale: int = 2,
|
|
234
|
+
) -> None:
|
|
235
|
+
cursor_x = x0
|
|
236
|
+
for character in text:
|
|
237
|
+
glyph = FONT_3X5.get(character)
|
|
238
|
+
if glyph:
|
|
239
|
+
draw_glyph(canvas, width, height, cursor_x, y0, glyph, color, scale)
|
|
240
|
+
cursor_x += 3 * scale + scale
|
|
241
|
+
|
|
242
|
+
|
|
243
|
+
def draw_hline(
|
|
244
|
+
canvas: list[tuple[int, int, int]],
|
|
245
|
+
width: int,
|
|
246
|
+
height: int,
|
|
247
|
+
y: int,
|
|
248
|
+
x_start: int,
|
|
249
|
+
x_end: int,
|
|
250
|
+
color: tuple[int, int, int],
|
|
251
|
+
dash: int = 0,
|
|
252
|
+
) -> None:
|
|
253
|
+
if not (0 <= y < height):
|
|
254
|
+
return
|
|
255
|
+
row = y * width
|
|
256
|
+
for x in range(max(0, x_start), min(width, x_end)):
|
|
257
|
+
if dash and (x // dash) % 2 == 1:
|
|
258
|
+
continue
|
|
259
|
+
canvas[row + x] = color
|
|
260
|
+
|
|
261
|
+
|
|
262
|
+
def draw_vline(
|
|
263
|
+
canvas: list[tuple[int, int, int]],
|
|
264
|
+
width: int,
|
|
265
|
+
height: int,
|
|
266
|
+
x: int,
|
|
267
|
+
y_start: int,
|
|
268
|
+
y_end: int,
|
|
269
|
+
color: tuple[int, int, int],
|
|
270
|
+
dash: int = 0,
|
|
271
|
+
) -> None:
|
|
272
|
+
if not (0 <= x < width):
|
|
273
|
+
return
|
|
274
|
+
for y in range(max(0, y_start), min(height, y_end)):
|
|
275
|
+
if dash and (y // dash) % 2 == 1:
|
|
276
|
+
continue
|
|
277
|
+
canvas[y * width + x] = color
|
|
278
|
+
|
|
279
|
+
|
|
280
|
+
def build_overlay(image: Path, overlay_path: Path, heads: int) -> dict:
|
|
281
|
+
width, height, pixels = load_image(image)
|
|
282
|
+
base = [composite_over_white(pixel) for pixel in pixels]
|
|
283
|
+
canvas_w = width + MARGIN
|
|
284
|
+
canvas_h = height
|
|
285
|
+
canvas: list[tuple[int, int, int]] = [(255, 255, 255)] * (canvas_w * canvas_h)
|
|
286
|
+
for y in range(height):
|
|
287
|
+
source_row = y * width
|
|
288
|
+
dest_row = y * canvas_w + MARGIN
|
|
289
|
+
canvas[dest_row : dest_row + width] = base[source_row : source_row + width]
|
|
290
|
+
|
|
291
|
+
for fraction in (1.0 / 3, 2.0 / 3):
|
|
292
|
+
y = round(fraction * height)
|
|
293
|
+
draw_hline(canvas, canvas_w, canvas_h, y, MARGIN, canvas_w, COLOR_THIRDS, dash=6)
|
|
294
|
+
for fraction in (1.0 / 3, 2.0 / 3):
|
|
295
|
+
x = MARGIN + round(fraction * width)
|
|
296
|
+
draw_vline(canvas, canvas_w, canvas_h, x, 0, height, COLOR_THIRDS, dash=6)
|
|
297
|
+
|
|
298
|
+
center_x = MARGIN + width // 2
|
|
299
|
+
draw_vline(canvas, canvas_w, canvas_h, center_x, 0, height, COLOR_CENTER)
|
|
300
|
+
draw_text(canvas, canvas_w, canvas_h, 4, max(0, min(height - 6, height // 2 - 3)), "C", COLOR_CENTER)
|
|
301
|
+
|
|
302
|
+
step = height / heads
|
|
303
|
+
for i in range(1, heads):
|
|
304
|
+
y = round(i * step)
|
|
305
|
+
draw_hline(canvas, canvas_w, canvas_h, y, MARGIN, canvas_w, COLOR_HEAD_UNIT, dash=10)
|
|
306
|
+
draw_text(canvas, canvas_w, canvas_h, 4, max(0, y - 3), str(i), COLOR_HEAD_UNIT)
|
|
307
|
+
|
|
308
|
+
band = step
|
|
309
|
+
face_lines = [
|
|
310
|
+
("H", COLOR_HAIRLINE, 0.05),
|
|
311
|
+
("E", COLOR_EYELINE, 0.50),
|
|
312
|
+
("N", COLOR_NOSEBASE, 0.65),
|
|
313
|
+
("M", COLOR_MOUTHLINE, 0.80),
|
|
314
|
+
]
|
|
315
|
+
for label, color, fraction in face_lines:
|
|
316
|
+
y = round(fraction * band)
|
|
317
|
+
draw_hline(canvas, canvas_w, canvas_h, y, MARGIN, MARGIN + width, color, dash=4)
|
|
318
|
+
draw_text(canvas, canvas_w, canvas_h, 4, max(0, y - 3), label, color)
|
|
319
|
+
|
|
320
|
+
shoulder_y = round(0.28 * height)
|
|
321
|
+
hip_y = round(0.55 * height)
|
|
322
|
+
draw_hline(canvas, canvas_w, canvas_h, shoulder_y, MARGIN, canvas_w, COLOR_SHOULDER, dash=14)
|
|
323
|
+
draw_text(canvas, canvas_w, canvas_h, 4, max(0, shoulder_y - 3), "S", COLOR_SHOULDER)
|
|
324
|
+
draw_hline(canvas, canvas_w, canvas_h, hip_y, MARGIN, canvas_w, COLOR_HIP, dash=14)
|
|
325
|
+
draw_text(canvas, canvas_w, canvas_h, 4, max(0, hip_y - 3), "P", COLOR_HIP)
|
|
326
|
+
|
|
327
|
+
write_png_rgb(overlay_path, canvas_w, canvas_h, canvas)
|
|
328
|
+
return {
|
|
329
|
+
"overlayImage": str(overlay_path),
|
|
330
|
+
"imageWidth": width,
|
|
331
|
+
"imageHeight": height,
|
|
332
|
+
"headUnitCount": heads,
|
|
333
|
+
"legend": {
|
|
334
|
+
"C": "center symmetry axis",
|
|
335
|
+
"1..N": "head-unit horizontal ticks (blue, dashed)",
|
|
336
|
+
"H": "hairline guide (default fraction of the first head-unit band)",
|
|
337
|
+
"E": "eye line guide",
|
|
338
|
+
"N": "nose base guide",
|
|
339
|
+
"M": "mouth line guide",
|
|
340
|
+
"S": "shoulder line guide (default fraction, adjust to observed pose)",
|
|
341
|
+
"P": "hip line guide (default fraction, adjust to observed pose)",
|
|
342
|
+
"grayDashed": "rule-of-thirds compositional grid",
|
|
343
|
+
},
|
|
344
|
+
"note": "Guide lines are generic starting positions, not measurements. Read the overlay "
|
|
345
|
+
"against the actual reference and fill anatomy with observed normalized values.",
|
|
346
|
+
}
|
|
347
|
+
|
|
348
|
+
|
|
349
|
+
def make_anatomy_skeleton(style_heads: float) -> dict:
|
|
350
|
+
joint_names = [
|
|
351
|
+
"neck",
|
|
352
|
+
"leftShoulder",
|
|
353
|
+
"rightShoulder",
|
|
354
|
+
"leftElbow",
|
|
355
|
+
"rightElbow",
|
|
356
|
+
"leftWrist",
|
|
357
|
+
"rightWrist",
|
|
358
|
+
"leftHip",
|
|
359
|
+
"rightHip",
|
|
360
|
+
"leftKnee",
|
|
361
|
+
"rightKnee",
|
|
362
|
+
"leftAnkle",
|
|
363
|
+
"rightAnkle",
|
|
364
|
+
]
|
|
365
|
+
return {
|
|
366
|
+
"styleHeads": style_heads,
|
|
367
|
+
"proportions": {
|
|
368
|
+
"headUnit": None,
|
|
369
|
+
"torso": None,
|
|
370
|
+
"legs": None,
|
|
371
|
+
"shoulderWidth": None,
|
|
372
|
+
"hipWidth": None,
|
|
373
|
+
},
|
|
374
|
+
"pose": {
|
|
375
|
+
"type": "",
|
|
376
|
+
"jointAngles": {name: [0, 0, 0] for name in joint_names},
|
|
377
|
+
},
|
|
378
|
+
"faceLandmarks": {
|
|
379
|
+
"hairline": None,
|
|
380
|
+
"eyeLine": None,
|
|
381
|
+
"eyeSpacing": None,
|
|
382
|
+
"noseBase": None,
|
|
383
|
+
"mouthLine": None,
|
|
384
|
+
"earTop": None,
|
|
385
|
+
"earBottom": None,
|
|
386
|
+
},
|
|
387
|
+
"features": [],
|
|
388
|
+
"confidence": 0.0,
|
|
389
|
+
}
|
|
390
|
+
|
|
391
|
+
|
|
392
|
+
def main(argv: list[str]) -> int:
|
|
393
|
+
parser = argparse.ArgumentParser(description=__doc__)
|
|
394
|
+
parser.add_argument("image", type=Path)
|
|
395
|
+
parser.add_argument(
|
|
396
|
+
"--out",
|
|
397
|
+
type=Path,
|
|
398
|
+
help="Output anatomy skeleton JSON path (default: <image-stem>-anatomy.json next to the image)",
|
|
399
|
+
)
|
|
400
|
+
parser.add_argument(
|
|
401
|
+
"--overlay",
|
|
402
|
+
type=Path,
|
|
403
|
+
help="Output overlay PNG path (default: <image-stem>-landmarks.png next to the image)",
|
|
404
|
+
)
|
|
405
|
+
parser.add_argument(
|
|
406
|
+
"--style-heads",
|
|
407
|
+
type=float,
|
|
408
|
+
default=6.0,
|
|
409
|
+
help="Initial head-unit estimate driving the overlay grid "
|
|
410
|
+
"(realistic ~7.5, stylized ~5-6, chibi/figurine ~2-3); refine after visual inspection",
|
|
411
|
+
)
|
|
412
|
+
parser.add_argument(
|
|
413
|
+
"--heads",
|
|
414
|
+
type=int,
|
|
415
|
+
help="Override number of head-unit tick lines drawn (default: round(--style-heads))",
|
|
416
|
+
)
|
|
417
|
+
parser.add_argument("--force", action="store_true", help="Overwrite existing outputs")
|
|
418
|
+
args = parser.parse_args(argv)
|
|
419
|
+
|
|
420
|
+
image = args.image.expanduser().resolve()
|
|
421
|
+
if not image.exists():
|
|
422
|
+
parser.error(f"{image} does not exist")
|
|
423
|
+
overlay_path = (args.overlay or image.with_name(f"{image.stem}-landmarks.png")).expanduser().resolve()
|
|
424
|
+
out_path = (args.out or image.with_name(f"{image.stem}-anatomy.json")).expanduser().resolve()
|
|
425
|
+
if not args.force:
|
|
426
|
+
for existing in (overlay_path, out_path):
|
|
427
|
+
if existing.exists():
|
|
428
|
+
parser.error(f"{existing} already exists; use --force to overwrite")
|
|
429
|
+
heads = args.heads or max(1, round(args.style_heads))
|
|
430
|
+
|
|
431
|
+
try:
|
|
432
|
+
overlay_meta = build_overlay(image, overlay_path, heads)
|
|
433
|
+
except Exception as exc:
|
|
434
|
+
print(f"error: {exc}", file=sys.stderr)
|
|
435
|
+
return 1
|
|
436
|
+
|
|
437
|
+
payload = {
|
|
438
|
+
"sourceImage": str(image),
|
|
439
|
+
"overlayImage": str(overlay_path),
|
|
440
|
+
"overlayLegend": overlay_meta["legend"],
|
|
441
|
+
"anatomy": make_anatomy_skeleton(args.style_heads),
|
|
442
|
+
"authoringInstruction": (
|
|
443
|
+
"Open overlayImage and read the reference against its head-unit ticks, thirds grid, "
|
|
444
|
+
"face-line guides, shoulder/hip lines, and center axis. Replace every null/placeholder "
|
|
445
|
+
"value in anatomy with normalized coordinates or joint angles actually observed; the "
|
|
446
|
+
"drawn guide lines are generic starting positions, not measurements."
|
|
447
|
+
),
|
|
448
|
+
}
|
|
449
|
+
out_path.parent.mkdir(parents=True, exist_ok=True)
|
|
450
|
+
out_path.write_text(json.dumps(payload, indent=2, ensure_ascii=False) + "\n", encoding="utf-8")
|
|
451
|
+
print(out_path)
|
|
452
|
+
return 0
|
|
453
|
+
|
|
454
|
+
|
|
455
|
+
if __name__ == "__main__":
|
|
456
|
+
raise SystemExit(main(sys.argv[1:]))
|