/** * Strict-enough .ppda loader for formats 1–5 (Paperdoll AtlasSet consumer). */ var PpdaLoader = { SUPPORTED_FORMATS: [1, 2, 3, 4, 5], parse: function (jsonText, options) { options = options || {}; var doc; try { doc = typeof jsonText === "string" ? JSON.parse(jsonText) : jsonText; } catch (e) { throw new Error("ppda: ongeldige JSON (" + e.message + ")"); } if (!doc || typeof doc !== "object") { throw new Error("ppda: root moet een object zijn"); } var ver = doc.format_version; if (this.SUPPORTED_FORMATS.indexOf(ver) < 0) { throw new Error("ppda: format_version " + ver + " niet ondersteund (wel: " + this.SUPPORTED_FORMATS.join(",") + ")"); } if (!doc.sex || !doc.settype) { throw new Error("ppda: sex/settype verplicht"); } if (!doc.fps || doc.fps < 1) { throw new Error("ppda: fps ongeldig"); } if (!doc.cell_width || !doc.cell_height) { throw new Error("ppda: cell_width/height verplicht"); } if (!Array.isArray(doc.pitches) || !Array.isArray(doc.yaws) || !doc.pitches.length || !doc.yaws.length) { throw new Error("ppda: pitches/yaws verplicht"); } if (!doc.maps || typeof doc.maps !== "object") { throw new Error("ppda: maps verplicht"); } ["diffuse", "depth"].forEach(function (k) { if (!doc.maps[k] || !doc.maps[k].file) { throw new Error("ppda: maps." + k + ".file verplicht"); } if (String(doc.maps[k].file).indexOf("..") >= 0 || String(doc.maps[k].file).indexOf("/") >= 0 || String(doc.maps[k].file).indexOf("\\") >= 0) { throw new Error("ppda: maps." + k + ".file mag geen pad bevatten"); } }); if (!Array.isArray(doc.clips) || !doc.clips.length) { throw new Error("ppda: clips verplicht"); } for (var ci = 0; ci < doc.clips.length; ci++) { var clip = doc.clips[ci]; if (!clip.frames || clip.frames.length !== clip.frame_count) { throw new Error("ppda: clip '" + (clip.name || ci) + "' frames≠frame_count"); } for (var fi = 0; fi < clip.frames.length; fi++) { var fr = clip.frames[fi]; if (!fr.views || !fr.views.length) { throw new Error("ppda: frame zonder views"); } for (var vi = 0; vi < fr.views.length; vi++) { var v = fr.views[vi]; if (v.w == null || v.h == null || v.x == null || v.y == null) { throw new Error("ppda: view mist x/y/w/h"); } } } } var encoding = doc.depth_encoding; var bits = doc.depth_bits; if (!encoding) { // v1 / legacy long text in identity.depth → gray16 encoding = "gray"; bits = bits || 16; } else if (/gray/i.test(encoding) && encoding.indexOf("rgb_") < 0) { encoding = "gray"; } else if (encoding.indexOf("rgb_bands") >= 0) { encoding = "rgb_bands"; } else if (encoding.indexOf("rgb_packed") >= 0) { encoding = "rgb_packed"; } var near = doc.depth_near != null ? doc.depth_near : (doc.identity && doc.identity.near); var far = doc.depth_far != null ? doc.depth_far : (doc.identity && doc.identity.far); if (near == null || far == null) { throw new Error("ppda: depth_near/far of identity.near/far verplicht"); } var fmt = DepthDecode.parseFormat(encoding, bits || 16); return { format_version: ver, sex: doc.sex, settype: doc.settype, fps: doc.fps, cell_width: doc.cell_width, cell_height: doc.cell_height, pitches: doc.pitches.slice(), yaws: doc.yaws.slice(), maps: doc.maps, clips: doc.clips, identity: doc.identity || null, project: doc.project || null, depth_near: Number(near), depth_far: Number(far), depth_format: fmt, camera_distance: doc.identity && doc.identity.camera_distance != null ? Number(doc.identity.camera_distance) : 10, height_milli: doc.identity && doc.identity.framing ? doc.identity.framing.height_milli : 2600, raw: doc }; }, compatKey: function (ppda) { var id = ppda.identity || {}; return { sex: ppda.sex, body_model_sha256: id.body_model_sha256 || "", fps: ppda.fps, cell_width: ppda.cell_width, cell_height: ppda.cell_height, pitches: ppda.pitches.join(","), yaws: ppda.yaws.join(","), near: ppda.depth_near, far: ppda.depth_far }; }, assertCompatible: function (a, b) { var ka = this.compatKey(a); var kb = this.compatKey(b); var keys = Object.keys(ka); for (var i = 0; i < keys.length; i++) { var k = keys[i]; if (String(ka[k]) !== String(kb[k])) { throw new Error("ppda incompatible: " + k + " (" + ka[k] + " vs " + kb[k] + ")"); } } } }; if (typeof module !== "undefined" && module.exports) { module.exports = PpdaLoader; }