@carbonenginejs/runtime-resource 0.1.0 → 0.8.0

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 (329) hide show
  1. package/FORMAT-PROVENANCE.md +155 -135
  2. package/LICENSE +21 -21
  3. package/NOTICE +29 -29
  4. package/README.md +548 -241
  5. package/dist/CjsMotherLode.js +1271 -56
  6. package/dist/CjsMotherLode.js.map +1 -1
  7. package/dist/CjsResMan.js +3005 -740
  8. package/dist/CjsResMan.js.map +1 -1
  9. package/dist/CjsResManQueue.js +214 -214
  10. package/dist/CjsResource.js +566 -366
  11. package/dist/CjsResource.js.map +1 -1
  12. package/dist/CjsResourceSource.js +59 -59
  13. package/dist/_virtual/_rollupPluginBabelHelpers.js +153 -153
  14. package/dist/format/CjsBlueReader.js +269 -0
  15. package/dist/format/CjsBlueReader.js.map +1 -0
  16. package/dist/format/CjsFormat.js +192 -178
  17. package/dist/format/CjsFormat.js.map +1 -1
  18. package/dist/format/CjsReader.js +18 -0
  19. package/dist/format/CjsReader.js.map +1 -0
  20. package/dist/format/CjsResourceProbe.js +277 -269
  21. package/dist/format/CjsResourceProbe.js.map +1 -1
  22. package/dist/format/payloadContract.js +173 -173
  23. package/dist/formats/black/CjsBlackFormat.js +278 -272
  24. package/dist/formats/black/CjsBlackFormat.js.map +1 -1
  25. package/dist/formats/black/core/CjsBlackBinaryReader.js +153 -153
  26. package/dist/formats/black/core/CjsBlackPropertyReaders.js +382 -382
  27. package/dist/formats/black/core/CjsBlackReader.js +639 -760
  28. package/dist/formats/black/core/CjsBlackReader.js.map +1 -1
  29. package/dist/formats/black/core/CjsBlackSchemaRegistry.js +433 -433
  30. package/dist/formats/black/core/black-schema-v1-2026-07-11.json.js +4 -4
  31. package/dist/formats/black/core/blackConstants.js +7 -7
  32. package/dist/formats/black/core/blackDefinitions.js +10 -10
  33. package/dist/formats/black/core/blackEnums.js +6 -6
  34. package/dist/formats/black/core/blackSchema.js +3 -3
  35. package/dist/formats/black/core/blackVersion.js +22 -22
  36. package/dist/formats/black/core/helpers.js +182 -182
  37. package/dist/formats/black/core/schema.js +4 -4
  38. package/dist/formats/black/index.js +2 -2
  39. package/dist/formats/bnk/CjsBnkFormat.js +238 -245
  40. package/dist/formats/bnk/CjsBnkFormat.js.map +1 -1
  41. package/dist/formats/bnk/core/graph.js +140 -0
  42. package/dist/formats/bnk/core/graph.js.map +1 -0
  43. package/dist/formats/bnk/core/helpers.js +409 -354
  44. package/dist/formats/bnk/core/helpers.js.map +1 -1
  45. package/dist/formats/bnk/core/musicNodes.js +521 -0
  46. package/dist/formats/bnk/core/musicNodes.js.map +1 -0
  47. package/dist/formats/bnk/core/soundbanksInfo.js +246 -246
  48. package/dist/formats/bnk/index.js +2 -3
  49. package/dist/formats/bnk/index.js.map +1 -1
  50. package/dist/formats/cmf/CjsCmfFormat.js +497 -491
  51. package/dist/formats/cmf/CjsCmfFormat.js.map +1 -1
  52. package/dist/formats/cmf/core/binary.js +118 -118
  53. package/dist/formats/cmf/core/buffers.js +233 -233
  54. package/dist/formats/cmf/core/constants.js +47 -47
  55. package/dist/formats/cmf/core/gr2Anim.js +453 -453
  56. package/dist/formats/cmf/core/helpers.js +287 -287
  57. package/dist/formats/cmf/core/pack.js +276 -276
  58. package/dist/formats/cmf/core/schema.js +364 -364
  59. package/dist/formats/cmf/core/shared.js +268 -268
  60. package/dist/formats/cmf/core/writer.js +517 -517
  61. package/dist/formats/cmf/index.js +2 -2
  62. package/dist/formats/dds/CjsDdsFormat.js +193 -187
  63. package/dist/formats/dds/CjsDdsFormat.js.map +1 -1
  64. package/dist/formats/dds/core/bc6h.js +288 -0
  65. package/dist/formats/dds/core/bc6h.js.map +1 -0
  66. package/dist/formats/dds/core/bc7.js +251 -0
  67. package/dist/formats/dds/core/bc7.js.map +1 -0
  68. package/dist/formats/dds/core/helpers.js +815 -812
  69. package/dist/formats/dds/core/helpers.js.map +1 -1
  70. package/dist/formats/dds/index.js +2 -2
  71. package/dist/formats/fbx/CjsFbxFormat.js +266 -260
  72. package/dist/formats/fbx/CjsFbxFormat.js.map +1 -1
  73. package/dist/formats/fbx/core/helpers.js +3901 -3901
  74. package/dist/formats/fbx/index.js +2 -2
  75. package/dist/formats/flac/CjsFlacFormat.js +87 -81
  76. package/dist/formats/flac/CjsFlacFormat.js.map +1 -1
  77. package/dist/formats/flac/core/helpers.js +295 -295
  78. package/dist/formats/flac/index.js +2 -2
  79. package/dist/formats/gif/CjsGifFormat.js +87 -81
  80. package/dist/formats/gif/CjsGifFormat.js.map +1 -1
  81. package/dist/formats/gif/core/helpers.js +360 -360
  82. package/dist/formats/gif/index.js +2 -2
  83. package/dist/formats/gltf/CjsGltfFormat.js +290 -284
  84. package/dist/formats/gltf/CjsGltfFormat.js.map +1 -1
  85. package/dist/formats/gltf/core/helpers.js +292 -292
  86. package/dist/formats/gltf/core/json.js +79 -79
  87. package/dist/formats/gltf/core/parser.js +666 -666
  88. package/dist/formats/gltf/core/targets.js +163 -163
  89. package/dist/formats/gltf/index.js +2 -2
  90. package/dist/formats/index.js +23 -23
  91. package/dist/formats/jpeg/CjsJpegFormat.js +206 -200
  92. package/dist/formats/jpeg/CjsJpegFormat.js.map +1 -1
  93. package/dist/formats/jpeg/core/helpers.js +350 -350
  94. package/dist/formats/jpeg/core/jpeg.js +377 -377
  95. package/dist/formats/jpeg/index.js +2 -2
  96. package/dist/formats/mp3/CjsMp3Format.js +192 -186
  97. package/dist/formats/mp3/CjsMp3Format.js.map +1 -1
  98. package/dist/formats/mp3/core/helpers.js +338 -338
  99. package/dist/formats/mp3/index.js +2 -2
  100. package/dist/formats/mp4/CjsMp4Format.js +191 -185
  101. package/dist/formats/mp4/CjsMp4Format.js.map +1 -1
  102. package/dist/formats/mp4/core/helpers.js +449 -449
  103. package/dist/formats/mp4/index.js +2 -2
  104. package/dist/formats/obj/CjsObjFormat.js +253 -247
  105. package/dist/formats/obj/CjsObjFormat.js.map +1 -1
  106. package/dist/formats/obj/core/helpers.js +573 -573
  107. package/dist/formats/obj/core/json.js +64 -64
  108. package/dist/formats/obj/core/parser.js +321 -321
  109. package/dist/formats/obj/index.js +2 -2
  110. package/dist/formats/ogg/CjsOggFormat.js +88 -82
  111. package/dist/formats/ogg/CjsOggFormat.js.map +1 -1
  112. package/dist/formats/ogg/core/helpers.js +387 -387
  113. package/dist/formats/ogg/core/imdct.js +178 -178
  114. package/dist/formats/ogg/core/vorbis.js +999 -999
  115. package/dist/formats/ogg/index.js +2 -2
  116. package/dist/formats/png/CjsPngFormat.js +195 -189
  117. package/dist/formats/png/CjsPngFormat.js.map +1 -1
  118. package/dist/formats/png/core/helpers.js +583 -583
  119. package/dist/formats/png/index.js +2 -2
  120. package/dist/formats/red/CjsRedFormat.js +261 -247
  121. package/dist/formats/red/CjsRedFormat.js.map +1 -1
  122. package/dist/formats/red/core/CjsRedReader.js +194 -201
  123. package/dist/formats/red/core/CjsRedReader.js.map +1 -1
  124. package/dist/formats/red/core/blackDefinitions.js +3 -3
  125. package/dist/formats/red/core/helpers.js +139 -136
  126. package/dist/formats/red/core/helpers.js.map +1 -1
  127. package/dist/formats/red/core/redGraph.js +68 -68
  128. package/dist/formats/red/core/schema.js +4 -4
  129. package/dist/formats/red/index.js +2 -2
  130. package/dist/formats/stl/CjsStlFormat.js +365 -304
  131. package/dist/formats/stl/CjsStlFormat.js.map +1 -1
  132. package/dist/formats/stl/core/helpers.js +261 -257
  133. package/dist/formats/stl/core/helpers.js.map +1 -1
  134. package/dist/formats/stl/core/json.js +51 -51
  135. package/dist/formats/stl/core/stl.js +629 -544
  136. package/dist/formats/stl/core/stl.js.map +1 -1
  137. package/dist/formats/stl/core/targets.js +163 -163
  138. package/dist/formats/stl/index.js +2 -2
  139. package/dist/formats/tga/CjsTgaFormat.js +192 -186
  140. package/dist/formats/tga/CjsTgaFormat.js.map +1 -1
  141. package/dist/formats/tga/core/helpers.js +446 -446
  142. package/dist/formats/tga/index.js +2 -2
  143. package/dist/formats/wav/CjsWavFormat.js +192 -186
  144. package/dist/formats/wav/CjsWavFormat.js.map +1 -1
  145. package/dist/formats/wav/core/helpers.js +328 -328
  146. package/dist/formats/wav/index.js +2 -2
  147. package/dist/formats/webm/CjsWebmFormat.js +191 -185
  148. package/dist/formats/webm/CjsWebmFormat.js.map +1 -1
  149. package/dist/formats/webm/core/helpers.js +537 -537
  150. package/dist/formats/webm/index.js +2 -2
  151. package/dist/formats/webp/CjsWebpFormat.js +86 -80
  152. package/dist/formats/webp/CjsWebpFormat.js.map +1 -1
  153. package/dist/formats/webp/core/helpers.js +214 -214
  154. package/dist/formats/webp/index.js +2 -2
  155. package/dist/formats/wem/CjsWemFormat.js +241 -197
  156. package/dist/formats/wem/CjsWemFormat.js.map +1 -1
  157. package/dist/formats/wem/core/bitStream.js +259 -259
  158. package/dist/formats/wem/core/codebookLibrary.js +164 -164
  159. package/dist/formats/wem/core/helpers.js +417 -336
  160. package/dist/formats/wem/core/helpers.js.map +1 -1
  161. package/dist/formats/wem/core/packedCodebooksAotuv603.js +30 -30
  162. package/dist/formats/wem/core/ptadpcm.js +77 -0
  163. package/dist/formats/wem/core/ptadpcm.js.map +1 -0
  164. package/dist/formats/wem/core/resolve.js +121 -0
  165. package/dist/formats/wem/core/resolve.js.map +1 -0
  166. package/dist/formats/wem/core/wemToOgg.js +485 -485
  167. package/dist/formats/wem/index.js +2 -2
  168. package/dist/formats/yaml/CjsYamlFormat.js +83 -77
  169. package/dist/formats/yaml/CjsYamlFormat.js.map +1 -1
  170. package/dist/formats/yaml/core/CjsYamlReader.js +305 -304
  171. package/dist/formats/yaml/core/CjsYamlReader.js.map +1 -1
  172. package/dist/formats/yaml/core/helpers.js +160 -160
  173. package/dist/formats/yaml/index.js +2 -2
  174. package/dist/index.js +48 -52
  175. package/dist/index.js.map +1 -1
  176. package/dist/resourcePath.js +18 -18
  177. package/dist/resourceStates.js +9 -9
  178. package/dist/{generated/resources → resources}/AudioGeometryResData.js +47 -47
  179. package/dist/resources/AudioGeometryResData.js.map +1 -0
  180. package/dist/{generated/resources → resources}/GStateBindingCallbackData.js +31 -31
  181. package/dist/resources/GStateBindingCallbackData.js.map +1 -0
  182. package/dist/{generated/resources → resources}/MeshDecalData.js +37 -37
  183. package/dist/resources/MeshDecalData.js.map +1 -0
  184. package/dist/{generated/resources → resources}/MeshDecalLodData.js +34 -34
  185. package/dist/resources/MeshDecalLodData.js.map +1 -0
  186. package/dist/resources/Tr2EffectRes.js +71 -71
  187. package/dist/resources/Tr2GrannyIntersectionResult.js +60 -0
  188. package/dist/resources/Tr2GrannyIntersectionResult.js.map +1 -0
  189. package/dist/resources/Tr2GrannyStateRes.js +44 -44
  190. package/dist/resources/Tr2ImageRes.js +114 -114
  191. package/dist/resources/Tr2LightProfileRes.js +40 -40
  192. package/dist/{generated/resources → resources}/Tr2MaterialArea.js +34 -34
  193. package/dist/resources/Tr2MaterialArea.js.map +1 -0
  194. package/dist/{generated/resources → resources}/Tr2MaterialMesh.js +31 -31
  195. package/dist/resources/Tr2MaterialMesh.js.map +1 -0
  196. package/dist/{generated/resources → resources}/Tr2MaterialRes.js +34 -34
  197. package/dist/resources/Tr2MaterialRes.js.map +1 -0
  198. package/dist/{generated/resources → resources}/Tr2ShaderPermutation.js +43 -43
  199. package/dist/resources/Tr2ShaderPermutation.js.map +1 -0
  200. package/dist/{generated/resources → resources}/Tr2TextureLodManager.js +100 -60
  201. package/dist/resources/Tr2TextureLodManager.js.map +1 -0
  202. package/dist/{generated/resources → resources}/Tr2TextureLodUpdateRequest.js +37 -37
  203. package/dist/resources/Tr2TextureLodUpdateRequest.js.map +1 -0
  204. package/dist/{generated/resources → resources}/Tr2TexturePackChannel.js +37 -37
  205. package/dist/resources/Tr2TexturePackChannel.js.map +1 -0
  206. package/dist/resources/Tr2TexturePipeline.js +68 -0
  207. package/dist/resources/Tr2TexturePipeline.js.map +1 -0
  208. package/dist/{generated/resources → resources}/Tr2TexturePipelineParams.js +34 -34
  209. package/dist/resources/Tr2TexturePipelineParams.js.map +1 -0
  210. package/dist/{generated/resources → resources}/Tr2TexturePipelineStepCompress.js +40 -40
  211. package/dist/resources/Tr2TexturePipelineStepCompress.js.map +1 -0
  212. package/dist/{generated/resources → resources}/Tr2TexturePipelineStepLimitSize.js +34 -34
  213. package/dist/resources/Tr2TexturePipelineStepLimitSize.js.map +1 -0
  214. package/dist/{generated/resources → resources}/Tr2TexturePipelineStepLoad.js +31 -31
  215. package/dist/resources/Tr2TexturePipelineStepLoad.js.map +1 -0
  216. package/dist/{generated/resources → resources}/Tr2TexturePipelineStepPack.js +43 -43
  217. package/dist/resources/Tr2TexturePipelineStepPack.js.map +1 -0
  218. package/dist/resources/TriGeometryRes.js +239 -216
  219. package/dist/resources/TriGeometryRes.js.map +1 -1
  220. package/dist/{generated/resources → resources}/TriGeometryResAreaData.js +59 -59
  221. package/dist/resources/TriGeometryResAreaData.js.map +1 -0
  222. package/dist/{generated/resources → resources}/TriGeometryResJointData.js +38 -38
  223. package/dist/resources/TriGeometryResJointData.js.map +1 -0
  224. package/dist/{generated/resources → resources}/TriGeometryResLodData.js +88 -88
  225. package/dist/resources/TriGeometryResLodData.js.map +1 -0
  226. package/dist/{generated/resources → resources}/TriGeometryResMeshData.js +63 -63
  227. package/dist/resources/TriGeometryResMeshData.js.map +1 -0
  228. package/dist/{generated/resources → resources}/TriGeometryResSkeletonData.js +34 -34
  229. package/dist/resources/TriGeometryResSkeletonData.js.map +1 -0
  230. package/dist/resources/TriGrannyRes.js +43 -43
  231. package/dist/{generated/resources → resources}/TriJointBinding.js +38 -38
  232. package/dist/resources/TriJointBinding.js.map +1 -0
  233. package/dist/{generated/resources → resources}/TriMorphTargetGeometryConstants.js +46 -46
  234. package/dist/resources/TriMorphTargetGeometryConstants.js.map +1 -0
  235. package/dist/{generated/resources → resources}/TriRtGeometryConstants.js +88 -88
  236. package/dist/resources/TriRtGeometryConstants.js.map +1 -0
  237. package/dist/resources/TriTextureRes.js +304 -304
  238. package/dist/{generated/resources → resources}/enums.js +18 -18
  239. package/dist/resources/enums.js.map +1 -0
  240. package/dist/resources/resourceBoundary.js +47 -47
  241. package/dist/resources/texturePipelineBehavior.js +320 -0
  242. package/dist/resources/texturePipelineBehavior.js.map +1 -0
  243. package/dist/texture/CjsTextureArrayRes.js +406 -406
  244. package/dist/texture/CjsTextureParameterProxy.js +133 -133
  245. package/format-notices/black/LICENSE +21 -21
  246. package/format-notices/black/NOTICE +47 -47
  247. package/format-notices/bnk/LICENSE +21 -21
  248. package/format-notices/bnk/NOTICE +20 -17
  249. package/format-notices/cmf/LICENSE +21 -21
  250. package/format-notices/cmf/NOTICE +36 -36
  251. package/format-notices/dds/LICENSE +21 -21
  252. package/format-notices/dds/NOTICE +14 -14
  253. package/format-notices/fbx/LICENSE +21 -21
  254. package/format-notices/fbx/NOTICE +14 -14
  255. package/format-notices/flac/LICENSE +21 -21
  256. package/format-notices/flac/NOTICE +14 -14
  257. package/format-notices/gif/LICENSE +21 -21
  258. package/format-notices/gif/NOTICE +14 -14
  259. package/format-notices/gltf/LICENSE +21 -21
  260. package/format-notices/gltf/NOTICE +27 -27
  261. package/format-notices/jpeg/LICENSE +21 -21
  262. package/format-notices/jpeg/NOTICE +14 -14
  263. package/format-notices/mp3/LICENSE +21 -21
  264. package/format-notices/mp3/NOTICE +14 -14
  265. package/format-notices/mp4/LICENSE +21 -21
  266. package/format-notices/mp4/NOTICE +14 -14
  267. package/format-notices/obj/LICENSE +21 -21
  268. package/format-notices/obj/NOTICE +26 -26
  269. package/format-notices/ogg/LICENSE +21 -21
  270. package/format-notices/ogg/NOTICE +28 -28
  271. package/format-notices/png/LICENSE +21 -21
  272. package/format-notices/png/NOTICE +14 -14
  273. package/format-notices/red/LICENSE +21 -21
  274. package/format-notices/red/NOTICE +31 -31
  275. package/format-notices/stl/LICENSE +21 -21
  276. package/format-notices/stl/NOTICE +21 -21
  277. package/format-notices/tga/LICENSE +21 -21
  278. package/format-notices/tga/NOTICE +14 -14
  279. package/format-notices/wav/LICENSE +21 -21
  280. package/format-notices/wav/NOTICE +14 -14
  281. package/format-notices/webm/LICENSE +21 -21
  282. package/format-notices/webm/NOTICE +14 -14
  283. package/format-notices/webp/LICENSE +21 -21
  284. package/format-notices/webp/NOTICE +14 -14
  285. package/format-notices/wem/LICENSE +57 -57
  286. package/format-notices/wem/NOTICE +33 -33
  287. package/format-notices/yaml/LICENSE +21 -21
  288. package/format-notices/yaml/NOTICE +44 -44
  289. package/package.json +51 -51
  290. package/resource-lifecycle.md +679 -487
  291. package/dist/generated/resources/AudioGeometryResData.js.map +0 -1
  292. package/dist/generated/resources/CmfVertexReader.js +0 -46
  293. package/dist/generated/resources/CmfVertexReader.js.map +0 -1
  294. package/dist/generated/resources/GStateBindingCallbackData.js.map +0 -1
  295. package/dist/generated/resources/MeshDecalData.js.map +0 -1
  296. package/dist/generated/resources/MeshDecalLodData.js.map +0 -1
  297. package/dist/generated/resources/Tr2AsyncSave.js +0 -46
  298. package/dist/generated/resources/Tr2AsyncSave.js.map +0 -1
  299. package/dist/generated/resources/Tr2CmfContents.js +0 -37
  300. package/dist/generated/resources/Tr2CmfContents.js.map +0 -1
  301. package/dist/generated/resources/Tr2GrannyIntersectionResult.js +0 -58
  302. package/dist/generated/resources/Tr2GrannyIntersectionResult.js.map +0 -1
  303. package/dist/generated/resources/Tr2LoadPrepareFence.js +0 -37
  304. package/dist/generated/resources/Tr2LoadPrepareFence.js.map +0 -1
  305. package/dist/generated/resources/Tr2MaterialArea.js.map +0 -1
  306. package/dist/generated/resources/Tr2MaterialMesh.js.map +0 -1
  307. package/dist/generated/resources/Tr2MaterialRes.js.map +0 -1
  308. package/dist/generated/resources/Tr2ShaderPermutation.js.map +0 -1
  309. package/dist/generated/resources/Tr2TextureLodManager.js.map +0 -1
  310. package/dist/generated/resources/Tr2TextureLodUpdateRequest.js.map +0 -1
  311. package/dist/generated/resources/Tr2TexturePackChannel.js.map +0 -1
  312. package/dist/generated/resources/Tr2TexturePipeline.js +0 -44
  313. package/dist/generated/resources/Tr2TexturePipeline.js.map +0 -1
  314. package/dist/generated/resources/Tr2TexturePipelineParams.js.map +0 -1
  315. package/dist/generated/resources/Tr2TexturePipelineStepCompress.js.map +0 -1
  316. package/dist/generated/resources/Tr2TexturePipelineStepLimitSize.js.map +0 -1
  317. package/dist/generated/resources/Tr2TexturePipelineStepLoad.js.map +0 -1
  318. package/dist/generated/resources/Tr2TexturePipelineStepPack.js.map +0 -1
  319. package/dist/generated/resources/TriGeometryResAreaData.js.map +0 -1
  320. package/dist/generated/resources/TriGeometryResJointData.js.map +0 -1
  321. package/dist/generated/resources/TriGeometryResLodData.js.map +0 -1
  322. package/dist/generated/resources/TriGeometryResMeshData.js.map +0 -1
  323. package/dist/generated/resources/TriGeometryResSkeletonData.js.map +0 -1
  324. package/dist/generated/resources/TriJointBinding.js.map +0 -1
  325. package/dist/generated/resources/TriMorphTargetGeometryConstants.js.map +0 -1
  326. package/dist/generated/resources/TriRtGeometryConstants.js.map +0 -1
  327. package/dist/generated/resources/enums.js.map +0 -1
  328. package/dist/generated/resources/index.js +0 -32
  329. package/dist/generated/resources/index.js.map +0 -1
@@ -1,812 +1,815 @@
1
- const OUTPUT_IMAGE = "image";
2
- const OUTPUT_TEXTURE = "texture";
3
- const OUTPUT_RGBA = "rgba";
4
- const OUTPUT_RAW = "raw";
5
- const OUTPUT_JSON = "json";
6
- const DEFAULT_VALUES = Object.freeze({
7
- emit: OUTPUT_RAW,
8
- inputType: "",
9
- source: ""
10
- });
11
- const DDS_HEADER_SIZE = 124;
12
- const DDS_PIXELFORMAT_OFFSET = 76;
13
- const DDS_FOURCC = 0x00000004;
14
- const DDS_RGB = 0x00000040;
15
- const DDS_ALPHAPIXELS = 0x00000001;
16
- const DDSCAPS2_CUBEMAP = 0x00000200;
17
- const DDSCAPS2_VOLUME = 0x00200000;
18
- const DDS_CUBE_FACE_FLAGS = Object.freeze([["positive-x", 0x00000400], ["negative-x", 0x00000800], ["positive-y", 0x00001000], ["negative-y", 0x00002000], ["positive-z", 0x00004000], ["negative-z", 0x00008000]]);
19
- const FOURCC_PIXEL_FORMATS = Object.freeze({
20
- DXT1: "bc1-rgba-unorm",
21
- DXT3: "bc2-rgba-unorm",
22
- DXT5: "bc3-rgba-unorm",
23
- ATI1: "bc4-r-unorm",
24
- BC4U: "bc4-r-unorm",
25
- BC4S: "bc4-r-snorm",
26
- ATI2: "bc5-rg-unorm",
27
- BC5U: "bc5-rg-unorm",
28
- BC5S: "bc5-rg-snorm"
29
- });
30
- const DXGI_PIXEL_FORMATS = Object.freeze({
31
- 2: "rgba32float",
32
- 6: "rgb32float",
33
- 10: "rgba16float",
34
- 16: "rg32float",
35
- 41: "r32float",
36
- 54: "r16float",
37
- 28: "rgba8unorm",
38
- 29: "rgba8unorm-srgb",
39
- 49: "rg8unorm",
40
- 61: "r8unorm",
41
- 71: "bc1-rgba-unorm",
42
- 72: "bc1-rgba-unorm-srgb",
43
- 74: "bc2-rgba-unorm",
44
- 75: "bc2-rgba-unorm-srgb",
45
- 77: "bc3-rgba-unorm",
46
- 78: "bc3-rgba-unorm-srgb",
47
- 80: "bc4-r-unorm",
48
- 81: "bc4-r-snorm",
49
- 83: "bc5-rg-unorm",
50
- 84: "bc5-rg-snorm",
51
- 87: "bgra8unorm",
52
- 88: "bgrx8unorm",
53
- 91: "bgra8unorm-srgb",
54
- 93: "bgrx8unorm-srgb",
55
- 95: "bc6h-rgb-ufloat",
56
- 96: "bc6h-rgb-float",
57
- 98: "bc7-rgba-unorm",
58
- 99: "bc7-rgba-unorm-srgb"
59
- });
60
- const COMPRESSED_PIXEL_FORMATS = new Set(["bc1-rgba-unorm", "bc1-rgba-unorm-srgb", "bc2-rgba-unorm", "bc2-rgba-unorm-srgb", "bc3-rgba-unorm", "bc3-rgba-unorm-srgb", "bc4-r-unorm", "bc4-r-snorm", "bc5-rg-unorm", "bc5-rg-snorm", "bc6h-rgb-ufloat", "bc6h-rgb-float", "bc7-rgba-unorm", "bc7-rgba-unorm-srgb"]);
61
- const DEBUG_OUTPUTS = Object.freeze({
62
- png: "pngJson",
63
- jpeg: "jpegJson",
64
- jpg: "jpegJson",
65
- tga: "tgaJson",
66
- dds: "ddsJson"
67
- });
68
- function normalizeValues(base = DEFAULT_VALUES, options = {}, readerName = "CjsImageFormat") {
69
- const values = {
70
- ...DEFAULT_VALUES,
71
- ...(base || {}),
72
- ...(options || {})
73
- };
74
- values.inputType = normalizeInputType(values.inputType);
75
- values.emit = normalizeEmit(values.emit, values.inputType, readerName);
76
- return values;
77
- }
78
- function normalizeInputType(inputType) {
79
- if (!inputType) return "";
80
- const value = String(inputType).replace(/^\./u, "").toLowerCase();
81
- return value === "jpg" ? "jpeg" : value;
82
- }
83
- function normalizeEmit(emit, inputType, readerName) {
84
- if (emit === undefined || emit === null) return OUTPUT_RAW;
85
- if (emit === OUTPUT_JSON && inputType) return DEBUG_OUTPUTS[inputType] || OUTPUT_JSON;
86
- if ([OUTPUT_IMAGE, OUTPUT_TEXTURE, OUTPUT_RGBA, OUTPUT_RAW, OUTPUT_JSON].includes(emit)) return emit;
87
- if (Object.values(DEBUG_OUTPUTS).includes(emit)) return emit;
88
- throw new TypeError(`${readerName}: unknown emit value ${JSON.stringify(emit)}`);
89
- }
90
- function toBytes(input) {
91
- if (input instanceof Uint8Array) return input;
92
- if (input instanceof ArrayBuffer) return new Uint8Array(input);
93
- if (ArrayBuffer.isView(input)) return new Uint8Array(input.buffer, input.byteOffset, input.byteLength);
94
- throw new TypeError("Image input must be Uint8Array, ArrayBuffer, or DataView");
95
- }
96
- function inspectWithValues(input, values = DEFAULT_VALUES, expectedType = "") {
97
- const bytes = toBytes(input);
98
- const detected = inspectBytes(bytes);
99
- const sourceFormat = expectedType || values.inputType || detected.sourceFormat;
100
- if (expectedType && detected.sourceFormat && detected.sourceFormat !== expectedType) {
101
- throw new TypeError(`CjsFormat${capitalize(expectedType)}: expected ${expectedType}, got ${detected.sourceFormat}`);
102
- }
103
- return {
104
- payloadType: sourceFormat === "dds" ? "texture" : "image",
105
- mediaTypes: sourceFormat === "dds" ? ["texture", "image"] : ["image"],
106
- sourceFormat,
107
- byteLength: bytes.byteLength,
108
- ...detected
109
- };
110
- }
111
- function isSupportedWithValues(input, values = DEFAULT_VALUES, expectedType = "") {
112
- try {
113
- const metadata = inspectWithValues(input, values, expectedType);
114
- const hasCompleteTextureData = metadata.isDataComplete !== false;
115
- const canEmitTexture = metadata.sourceFormat === "dds" && metadata.dataOffset > 0 && !!metadata.pixelFormat && hasCompleteTextureData;
116
- const canEmitRgba = canDecodeDdsToRgba(metadata) && hasCompleteTextureData;
117
- const textureUnsupportedReason = !metadata.pixelFormat ? "DDS texture format is not recognized yet." : !hasCompleteTextureData ? `DDS texture data is truncated: expected ${metadata.expectedDataBytes} bytes, got ${metadata.dataBytes}.` : "";
118
- const variants = metadata.sourceFormat === "dds" ? [{
119
- kind: "compressed",
120
- payloadType: "texture",
121
- codec: metadata.pixelFormat || metadata.textureFormat || "unknown",
122
- supported: canEmitTexture && metadata.isCompressed,
123
- nativeOnly: !!metadata.nativeTextureOnly,
124
- reason: metadata.isCompressed ? textureUnsupportedReason : "DDS is not block-compressed."
125
- }, {
126
- kind: "texture",
127
- payloadType: "texture",
128
- codec: metadata.pixelFormat || metadata.textureFormat || "unknown",
129
- supported: canEmitTexture,
130
- reason: canEmitTexture ? "" : textureUnsupportedReason
131
- }, {
132
- kind: "rgba",
133
- payloadType: "rgba",
134
- codec: canEmitRgba ? isFloatPixelFormat(metadata.pixelFormat) ? "rgba32float" : "rgba8unorm" : "rgba8unorm",
135
- supported: canEmitRgba,
136
- reason: canEmitRgba ? "" : textureUnsupportedReason || (metadata.nativeTextureOnly ? `${metadata.pixelFormat} is available as a native compressed texture only; register a software decoder or alternate representation for RGBA fallback.` : "DDS RGBA decode is not implemented for this pixel format.")
137
- }, {
138
- kind: "raw",
139
- payloadType: "raw",
140
- codec: "dds",
141
- supported: true
142
- }] : [{
143
- kind: "rgba",
144
- codec: "rgba8",
145
- supported: false,
146
- reason: `${metadata.sourceFormat.toUpperCase()} RGBA decode is not implemented yet.`
147
- }];
148
- const preferredVariant = variants.find(v => v.supported && v.kind !== "raw") || variants.find(v => v.supported);
149
- return {
150
- format: metadata.sourceFormat,
151
- source: values.source || "buffer",
152
- supported: metadata.sourceFormat ? variants.some(v => v.supported && v.kind !== "raw") ? "full" : variants.some(v => v.supported) ? "partial" : "none" : "none",
153
- confidence: metadata.sourceFormat ? 1 : 0,
154
- preferred: preferredVariant?.codec || "",
155
- reason: metadata.sourceFormat ? "Container/header recognized." : "Unrecognized image format.",
156
- metadata,
157
- variants,
158
- warnings: [],
159
- errors: []
160
- };
161
- } catch (error) {
162
- return {
163
- format: expectedType || values.inputType || "",
164
- source: values.source || "buffer",
165
- supported: "none",
166
- confidence: 0,
167
- preferred: "",
168
- reason: error.message,
169
- metadata: null,
170
- variants: [],
171
- warnings: [],
172
- errors: [error.message]
173
- };
174
- }
175
- }
176
- function readWithValues(input, values = DEFAULT_VALUES, expectedType = "") {
177
- const bytes = toBytes(input);
178
- const metadata = inspectWithValues(bytes, values, expectedType);
179
- if (values.emit === OUTPUT_RAW) {
180
- return {
181
- payloadType: "raw",
182
- sourceFormat: metadata.sourceFormat,
183
- metadata,
184
- bytes
185
- };
186
- }
187
- if (values.emit === OUTPUT_JSON || values.emit === DEBUG_OUTPUTS[metadata.sourceFormat]) {
188
- return metadata;
189
- }
190
- if (values.emit === OUTPUT_TEXTURE && metadata.sourceFormat === "dds") {
191
- return readDdsTexture(bytes, metadata);
192
- }
193
- if ((values.emit === OUTPUT_RGBA || values.emit === OUTPUT_IMAGE) && metadata.sourceFormat === "dds") {
194
- return readDdsToRgba(bytes, metadata);
195
- }
196
- const error = new Error(`${metadata.sourceFormat}: emit "${values.emit}" is not implemented yet`);
197
- error.code = "CJS_FORMAT_OUTPUT_NOT_IMPLEMENTED";
198
- error.sourceFormat = metadata.sourceFormat;
199
- error.emit = values.emit;
200
- throw error;
201
- }
202
- function toJsonValue(value) {
203
- if (value instanceof Uint8Array) {
204
- return {
205
- byteLength: value.byteLength
206
- };
207
- }
208
- if (Array.isArray(value)) return value.map(toJsonValue);
209
- if (value && typeof value === "object") {
210
- const output = {};
211
- for (const [key, entry] of Object.entries(value)) output[key] = toJsonValue(entry);
212
- return output;
213
- }
214
- return value;
215
- }
216
- function inspectBytes(bytes) {
217
- if (isPNG(bytes)) return inspectPNG(bytes);
218
- if (isJPEG(bytes)) return inspectJPEG(bytes);
219
- if (isDDS(bytes)) return inspectDDS(bytes);
220
- if (isTGA(bytes)) return inspectTGA(bytes);
221
- return {
222
- sourceFormat: "",
223
- width: 0,
224
- height: 0
225
- };
226
- }
227
- function isPNG(bytes) {
228
- return bytes.byteLength >= 24 && bytes[0] === 0x89 && bytes[1] === 0x50 && bytes[2] === 0x4e && bytes[3] === 0x47 && bytes[4] === 0x0d && bytes[5] === 0x0a && bytes[6] === 0x1a && bytes[7] === 0x0a;
229
- }
230
- function isJPEG(bytes) {
231
- return bytes.byteLength >= 4 && bytes[0] === 0xff && bytes[1] === 0xd8;
232
- }
233
- function isDDS(bytes) {
234
- return bytes.byteLength >= DDS_HEADER_SIZE + 4 && bytes[0] === 0x44 && bytes[1] === 0x44 && bytes[2] === 0x53 && bytes[3] === 0x20 && readU32LE(bytes, 4) === DDS_HEADER_SIZE;
235
- }
236
- function isTGA(bytes) {
237
- if (bytes.byteLength < 18) return false;
238
- const imageType = bytes[2],
239
- width = readU16LE(bytes, 12),
240
- height = readU16LE(bytes, 14),
241
- bpp = bytes[16];
242
- return width > 0 && height > 0 && [1, 2, 3, 9, 10, 11].includes(imageType) && [8, 16, 24, 32].includes(bpp);
243
- }
244
- function inspectPNG(bytes) {
245
- const colorType = bytes[25],
246
- bitDepth = bytes[24];
247
- return {
248
- sourceFormat: "png",
249
- width: readU32BE(bytes, 16),
250
- height: readU32BE(bytes, 20),
251
- bitDepth,
252
- colorType,
253
- channels: pngChannels(colorType),
254
- pixelFormat: `png-${bitDepth}-${colorType}`,
255
- isCompressed: false
256
- };
257
- }
258
- function inspectJPEG(bytes) {
259
- let offset = 2;
260
- while (offset + 9 < bytes.byteLength) {
261
- if (bytes[offset] !== 0xff) {
262
- offset++;
263
- continue;
264
- }
265
- const marker = bytes[offset + 1],
266
- length = readU16BE(bytes, offset + 2);
267
- if ([0xc0, 0xc1, 0xc2, 0xc3].includes(marker)) {
268
- return {
269
- sourceFormat: "jpeg",
270
- width: readU16BE(bytes, offset + 7),
271
- height: readU16BE(bytes, offset + 5),
272
- channels: bytes[offset + 9],
273
- marker,
274
- pixelFormat: "jpeg-ycbcr",
275
- isCompressed: true
276
- };
277
- }
278
- offset += Math.max(length + 2, 2);
279
- }
280
- return {
281
- sourceFormat: "jpeg",
282
- width: 0,
283
- height: 0,
284
- channels: 0,
285
- pixelFormat: "jpeg",
286
- isCompressed: true
287
- };
288
- }
289
- function inspectDDS(bytes) {
290
- const headerSize = readU32LE(bytes, 4);
291
- const pfOffset = DDS_PIXELFORMAT_OFFSET;
292
- const pfFlags = readU32LE(bytes, pfOffset + 4);
293
- const fourCcCode = readU32LE(bytes, pfOffset + 8);
294
- const fourCc = String.fromCharCode(bytes[84], bytes[85], bytes[86], bytes[87]).replace(/\0+$/u, "");
295
- const rgbBitCount = readU32LE(bytes, pfOffset + 12);
296
- const rBitMask = readU32LE(bytes, pfOffset + 16);
297
- const gBitMask = readU32LE(bytes, pfOffset + 20);
298
- const bBitMask = readU32LE(bytes, pfOffset + 24);
299
- const aBitMask = readU32LE(bytes, pfOffset + 28);
300
- const caps2 = readU32LE(bytes, 112);
301
- const isCube = !!(caps2 & DDSCAPS2_CUBEMAP);
302
- const isVolume = !!(caps2 & DDSCAPS2_VOLUME);
303
- const cubeFaces = getDdsCubeFaces(caps2);
304
- const hasDx10 = fourCc === "DX10" && bytes.byteLength >= 148;
305
- const dxgiFormat = hasDx10 ? readU32LE(bytes, 128) : 0;
306
- const resourceDimension = hasDx10 ? readU32LE(bytes, 132) : 0;
307
- const arraySize = hasDx10 ? readU32LE(bytes, 140) : 1;
308
- const dataOffset = hasDx10 ? 148 : 128;
309
- const pixelFormat = getDdsPixelFormat({
310
- pfFlags,
311
- fourCc,
312
- fourCcCode,
313
- rgbBitCount,
314
- rBitMask,
315
- gBitMask,
316
- bBitMask,
317
- aBitMask,
318
- dxgiFormat
319
- });
320
- const width = readU32LE(bytes, 16);
321
- const height = readU32LE(bytes, 12);
322
- const depth = readU32LE(bytes, 24);
323
- const mipCount = Math.max(readU32LE(bytes, 28), 1);
324
- const dataBytes = Math.max(bytes.byteLength - dataOffset, 0);
325
- const expectedDataBytes = pixelFormat ? getDdsExpectedDataBytes({
326
- pixelFormat,
327
- width,
328
- height,
329
- depth,
330
- mipCount,
331
- arraySize,
332
- isCube,
333
- isVolume
334
- }) : null;
335
- return {
336
- sourceFormat: "dds",
337
- headerSize,
338
- width,
339
- height,
340
- depth,
341
- mipCount,
342
- pitchOrLinearSize: readU32LE(bytes, 20),
343
- pfFlags,
344
- fourCc,
345
- fourCcCode,
346
- rgbBitCount,
347
- rBitMask,
348
- gBitMask,
349
- bBitMask,
350
- aBitMask,
351
- caps2,
352
- hasDx10,
353
- dxgiFormat,
354
- resourceDimension,
355
- arraySize,
356
- dataOffset,
357
- dataBytes,
358
- expectedDataBytes,
359
- isDataComplete: expectedDataBytes !== null && dataBytes >= expectedDataBytes,
360
- missingDataBytes: expectedDataBytes === null ? null : Math.max(expectedDataBytes - dataBytes, 0),
361
- extraDataBytes: expectedDataBytes === null ? null : Math.max(dataBytes - expectedDataBytes, 0),
362
- faces: isCube ? 6 : 1,
363
- cubeFaces,
364
- isCubeComplete: isCube && cubeFaces.length === 6,
365
- isCube,
366
- isVolume,
367
- dimension: isVolume ? "3d" : isCube ? "cube" : "2d",
368
- pixelFormat,
369
- textureFormat: pixelFormat || fourCc || "dds-legacy",
370
- isCompressed: COMPRESSED_PIXEL_FORMATS.has(pixelFormat),
371
- nativeTextureOnly: COMPRESSED_PIXEL_FORMATS.has(pixelFormat) && !canDecodeDdsToRgba({
372
- pixelFormat
373
- }),
374
- hasAlpha: !!(pfFlags & DDS_ALPHAPIXELS) || /rgba|bgra/u.test(pixelFormat),
375
- hasMipMaps: readU32LE(bytes, 28) > 1
376
- };
377
- }
378
- function readDdsTexture(bytes, metadata) {
379
- if (!metadata.pixelFormat) {
380
- const error = new Error("dds: texture format is not recognized yet");
381
- error.code = "CJS_FORMAT_OUTPUT_NOT_SUPPORTED";
382
- error.sourceFormat = metadata.sourceFormat;
383
- throw error;
384
- }
385
- const subresources = buildDdsSubresources(bytes, metadata);
386
- return {
387
- payloadType: OUTPUT_TEXTURE,
388
- sourceFormat: "dds",
389
- width: metadata.width,
390
- height: metadata.height,
391
- depth: metadata.depth,
392
- faces: metadata.faces,
393
- cubeFaces: metadata.cubeFaces,
394
- isCubeComplete: metadata.isCubeComplete,
395
- dimension: metadata.dimension,
396
- mipCount: metadata.mipCount,
397
- arraySize: metadata.arraySize,
398
- pixelFormat: metadata.pixelFormat,
399
- isCompressed: metadata.isCompressed,
400
- dataBytes: metadata.dataBytes,
401
- expectedDataBytes: metadata.expectedDataBytes,
402
- isDataComplete: metadata.isDataComplete,
403
- missingDataBytes: metadata.missingDataBytes,
404
- extraDataBytes: metadata.extraDataBytes,
405
- subresources,
406
- metadata,
407
- data: bytes.subarray(metadata.dataOffset, metadata.dataOffset + subresources.reduce((sum, entry) => sum + entry.byteLength, 0))
408
- };
409
- }
410
- function canDecodeDdsToRgba(metadata) {
411
- return ["rgba32float", "rgb32float", "rgba16float", "rg32float", "r32float", "r16float", "rgba8unorm", "rgba8unorm-srgb", "bgra8unorm", "bgra8unorm-srgb", "bgrx8unorm", "bgrx8unorm-srgb", "bgr8unorm", "rg8unorm", "r8unorm", "bc1-rgba-unorm", "bc1-rgba-unorm-srgb", "bc2-rgba-unorm", "bc2-rgba-unorm-srgb", "bc3-rgba-unorm", "bc3-rgba-unorm-srgb", "bc4-r-unorm", "bc4-r-snorm", "bc5-rg-unorm", "bc5-rg-snorm"].includes(metadata.pixelFormat);
412
- }
413
- function readDdsToRgba(bytes, metadata) {
414
- if (!canDecodeDdsToRgba(metadata)) {
415
- const error = new Error(`dds: RGBA decode is not implemented for ${metadata.pixelFormat || "unknown"}`);
416
- error.code = "CJS_FORMAT_OUTPUT_NOT_SUPPORTED";
417
- error.sourceFormat = metadata.sourceFormat;
418
- throw error;
419
- }
420
- const subresource = buildDdsSubresources(bytes, metadata)[0];
421
- const source = bytes.subarray(metadata.dataOffset + subresource.offset, metadata.dataOffset + subresource.offset + subresource.byteLength);
422
- const rgba = metadata.pixelFormat.startsWith("bc1-") ? decodeBc1(source, metadata.width, metadata.height, subresource.rowPitch) : metadata.pixelFormat.startsWith("bc2-") ? decodeBc2(source, metadata.width, metadata.height, subresource.rowPitch) : metadata.pixelFormat.startsWith("bc3-") ? decodeBc3(source, metadata.width, metadata.height, subresource.rowPitch) : metadata.pixelFormat.startsWith("bc4-") ? decodeBc4(source, metadata.width, metadata.height, subresource.rowPitch, metadata.pixelFormat.endsWith("-snorm")) : metadata.pixelFormat.startsWith("bc5-") ? decodeBc5(source, metadata.width, metadata.height, subresource.rowPitch, metadata.pixelFormat.endsWith("-snorm")) : isFloatPixelFormat(metadata.pixelFormat) ? decodeFloatUncompressed(source, metadata) : decodeUncompressed(source, metadata);
423
- const isFloat = isFloatPixelFormat(metadata.pixelFormat);
424
- return {
425
- payloadType: OUTPUT_RGBA,
426
- sourceFormat: "dds",
427
- width: metadata.width,
428
- height: metadata.height,
429
- pixelFormat: isFloat ? "rgba32float" : "rgba8unorm",
430
- strideBytes: metadata.width * (isFloat ? 16 : 4),
431
- origin: "top-left",
432
- colorSpace: isFloat ? "linear" : metadata.pixelFormat.endsWith("-srgb") ? "srgb" : "unknown",
433
- alphaMode: metadata.hasAlpha ? "straight" : "opaque",
434
- metadata,
435
- data: rgba
436
- };
437
- }
438
- function buildDdsSubresources(bytes, metadata) {
439
- const subresources = [];
440
- let offset = 0;
441
- const layers = Math.max(metadata.arraySize, 1) * (metadata.isCube ? 6 : 1);
442
- for (let layer = 0; layer < layers; layer++) {
443
- const arrayIndex = metadata.isCube ? Math.floor(layer / 6) : layer;
444
- const face = metadata.isCube ? layer % 6 : 0;
445
- for (let mip = 0; mip < metadata.mipCount; mip++) {
446
- const width = Math.max(metadata.width >> mip, 1);
447
- const height = Math.max(metadata.height >> mip, 1);
448
- const depth = metadata.isVolume ? Math.max(metadata.depth >> mip, 1) : 1;
449
- const layout = getDdsLevelLayout(metadata.pixelFormat, width, height, depth);
450
- if (offset + layout.byteLength > bytes.byteLength - metadata.dataOffset) {
451
- const error = new Error("dds: texture data is truncated");
452
- error.code = "CJS_FORMAT_TRUNCATED";
453
- error.sourceFormat = metadata.sourceFormat;
454
- error.expectedDataBytes = metadata.expectedDataBytes;
455
- error.dataBytes = metadata.dataBytes;
456
- error.missingDataBytes = metadata.missingDataBytes;
457
- throw error;
458
- }
459
- subresources.push({
460
- mip,
461
- layer,
462
- arrayIndex,
463
- face,
464
- offset,
465
- byteLength: layout.byteLength,
466
- rowPitch: layout.rowPitch,
467
- slicePitch: layout.slicePitch,
468
- width,
469
- height,
470
- depth
471
- });
472
- offset += layout.byteLength;
473
- }
474
- }
475
- return subresources;
476
- }
477
- function getDdsExpectedDataBytes(metadata) {
478
- let total = 0;
479
- const layers = Math.max(metadata.arraySize, 1) * (metadata.isCube ? 6 : 1);
480
- for (let layer = 0; layer < layers; layer++) {
481
- for (let mip = 0; mip < metadata.mipCount; mip++) {
482
- const width = Math.max(metadata.width >> mip, 1);
483
- const height = Math.max(metadata.height >> mip, 1);
484
- const depth = metadata.isVolume ? Math.max(metadata.depth >> mip, 1) : 1;
485
- total += getDdsLevelLayout(metadata.pixelFormat, width, height, depth).byteLength;
486
- }
487
- }
488
- return total;
489
- }
490
- function getDdsCubeFaces(caps2) {
491
- if (!(caps2 & DDSCAPS2_CUBEMAP)) {
492
- return [];
493
- }
494
- return DDS_CUBE_FACE_FLAGS.filter(entry => !!(caps2 & entry[1])).map(entry => entry[0]);
495
- }
496
- function getDdsLevelLayout(pixelFormat, width, height, depth) {
497
- const blockBytes = pixelFormat.startsWith("bc1-") || pixelFormat.startsWith("bc4-") ? 8 : pixelFormat.startsWith("bc") ? 16 : 0;
498
- if (blockBytes) {
499
- const blocksWide = Math.max(Math.ceil(width / 4), 1);
500
- const blocksHigh = Math.max(Math.ceil(height / 4), 1);
501
- const rowPitch = blocksWide * blockBytes;
502
- const slicePitch = rowPitch * blocksHigh;
503
- return {
504
- rowPitch,
505
- slicePitch,
506
- byteLength: slicePitch * depth
507
- };
508
- }
509
- const bytesPerPixel = {
510
- "rgba32float": 16,
511
- "rgb32float": 12,
512
- "rgba16float": 8,
513
- "rg32float": 8,
514
- "r32float": 4,
515
- "r16float": 2,
516
- "rgba8unorm": 4,
517
- "rgba8unorm-srgb": 4,
518
- "bgra8unorm": 4,
519
- "bgra8unorm-srgb": 4,
520
- "bgrx8unorm": 4,
521
- "bgrx8unorm-srgb": 4,
522
- "bgr8unorm": 3,
523
- "rg8unorm": 2,
524
- "r8unorm": 1
525
- }[pixelFormat];
526
- if (!bytesPerPixel) {
527
- const error = new Error(`dds: cannot calculate subresource layout for ${pixelFormat || "unknown"}`);
528
- error.code = "CJS_FORMAT_OUTPUT_NOT_SUPPORTED";
529
- throw error;
530
- }
531
- const rowPitch = width * bytesPerPixel;
532
- const slicePitch = rowPitch * height;
533
- return {
534
- rowPitch,
535
- slicePitch,
536
- byteLength: slicePitch * depth
537
- };
538
- }
539
- function decodeUncompressed(source, metadata) {
540
- const rgba = new Uint8Array(metadata.width * metadata.height * 4);
541
- const bytesPerPixel = metadata.pixelFormat === "bgr8unorm" ? 3 : metadata.pixelFormat === "rg8unorm" ? 2 : metadata.pixelFormat === "r8unorm" ? 1 : 4;
542
- for (let pixel = 0; pixel < metadata.width * metadata.height; pixel++) {
543
- const sourceOffset = pixel * bytesPerPixel;
544
- const outputOffset = pixel * 4;
545
- if (metadata.pixelFormat.startsWith("bgra") || metadata.pixelFormat.startsWith("bgrx")) {
546
- rgba[outputOffset] = source[sourceOffset + 2];
547
- rgba[outputOffset + 1] = source[sourceOffset + 1];
548
- rgba[outputOffset + 2] = source[sourceOffset];
549
- rgba[outputOffset + 3] = metadata.pixelFormat.startsWith("bgrx") ? 255 : source[sourceOffset + 3];
550
- } else if (metadata.pixelFormat === "bgr8unorm") {
551
- rgba[outputOffset] = source[sourceOffset + 2];
552
- rgba[outputOffset + 1] = source[sourceOffset + 1];
553
- rgba[outputOffset + 2] = source[sourceOffset];
554
- rgba[outputOffset + 3] = 255;
555
- } else if (metadata.pixelFormat === "rg8unorm") {
556
- rgba[outputOffset] = source[sourceOffset];
557
- rgba[outputOffset + 1] = source[sourceOffset + 1];
558
- rgba[outputOffset + 2] = 0;
559
- rgba[outputOffset + 3] = 255;
560
- } else {
561
- rgba[outputOffset] = source[sourceOffset];
562
- rgba[outputOffset + 1] = bytesPerPixel > 1 ? source[sourceOffset + 1] : source[sourceOffset];
563
- rgba[outputOffset + 2] = bytesPerPixel > 2 ? source[sourceOffset + 2] : source[sourceOffset];
564
- rgba[outputOffset + 3] = bytesPerPixel > 3 ? source[sourceOffset + 3] : 255;
565
- }
566
- }
567
- return rgba;
568
- }
569
- function decodeFloatUncompressed(source, metadata) {
570
- const rgba = new Float32Array(metadata.width * metadata.height * 4);
571
- const view = new DataView(source.buffer, source.byteOffset, source.byteLength);
572
- const bytesPerPixel = {
573
- "rgba32float": 16,
574
- "rgb32float": 12,
575
- "rgba16float": 8,
576
- "rg32float": 8,
577
- "r32float": 4,
578
- "r16float": 2
579
- }[metadata.pixelFormat];
580
- for (let pixel = 0; pixel < metadata.width * metadata.height; pixel++) {
581
- const sourceOffset = pixel * bytesPerPixel;
582
- const outputOffset = pixel * 4;
583
- let red, green, blue, alpha;
584
- if (metadata.pixelFormat === "rgba32float") {
585
- red = view.getFloat32(sourceOffset, true);
586
- green = view.getFloat32(sourceOffset + 4, true);
587
- blue = view.getFloat32(sourceOffset + 8, true);
588
- alpha = view.getFloat32(sourceOffset + 12, true);
589
- } else if (metadata.pixelFormat === "rgb32float") {
590
- red = view.getFloat32(sourceOffset, true);
591
- green = view.getFloat32(sourceOffset + 4, true);
592
- blue = view.getFloat32(sourceOffset + 8, true);
593
- alpha = 1;
594
- } else if (metadata.pixelFormat === "rgba16float") {
595
- red = halfToFloat(view.getUint16(sourceOffset, true));
596
- green = halfToFloat(view.getUint16(sourceOffset + 2, true));
597
- blue = halfToFloat(view.getUint16(sourceOffset + 4, true));
598
- alpha = halfToFloat(view.getUint16(sourceOffset + 6, true));
599
- } else if (metadata.pixelFormat === "rg32float") {
600
- red = view.getFloat32(sourceOffset, true);
601
- green = view.getFloat32(sourceOffset + 4, true);
602
- blue = 0;
603
- alpha = 1;
604
- } else {
605
- red = metadata.pixelFormat === "r16float" ? halfToFloat(view.getUint16(sourceOffset, true)) : view.getFloat32(sourceOffset, true);
606
- green = red;
607
- blue = red;
608
- alpha = 1;
609
- }
610
- rgba[outputOffset] = red;
611
- rgba[outputOffset + 1] = green;
612
- rgba[outputOffset + 2] = blue;
613
- rgba[outputOffset + 3] = alpha;
614
- }
615
- return rgba;
616
- }
617
- function isFloatPixelFormat(pixelFormat) {
618
- return ["rgba32float", "rgb32float", "rgba16float", "rg32float", "r32float", "r16float"].includes(pixelFormat);
619
- }
620
- function halfToFloat(value) {
621
- const sign = value & 0x8000 ? -1 : 1;
622
- const exponent = value >>> 10 & 0x1f;
623
- const mantissa = value & 0x03ff;
624
- if (exponent === 0) {
625
- return sign * Math.pow(2, -14) * (mantissa / 1024);
626
- }
627
- if (exponent === 0x1f) {
628
- return mantissa === 0 ? sign * Infinity : NaN;
629
- }
630
- return sign * Math.pow(2, exponent - 15) * (1 + mantissa / 1024);
631
- }
632
- function decodeBc1(source, width, height, rowPitch) {
633
- const rgba = new Uint8Array(width * height * 4);
634
- for (let blockY = 0; blockY < Math.ceil(height / 4); blockY++) {
635
- for (let blockX = 0; blockX < Math.ceil(width / 4); blockX++) {
636
- const blockOffset = blockY * rowPitch + blockX * 8;
637
- const c0 = source[blockOffset] | source[blockOffset + 1] << 8;
638
- const c1 = source[blockOffset + 2] | source[blockOffset + 3] << 8;
639
- const colors = decodeBc1Colors(c0, c1);
640
- const indices = source[blockOffset + 4] | source[blockOffset + 5] << 8 | source[blockOffset + 6] << 16 | source[blockOffset + 7] << 24;
641
- for (let y = 0; y < 4; y++) {
642
- for (let x = 0; x < 4; x++) {
643
- const outputX = blockX * 4 + x;
644
- const outputY = blockY * 4 + y;
645
- if (outputX >= width || outputY >= height) continue;
646
- const color = colors[indices >>> 2 * (y * 4 + x) & 3];
647
- rgba.set(color, (outputY * width + outputX) * 4);
648
- }
649
- }
650
- }
651
- }
652
- return rgba;
653
- }
654
- function decodeBc2(source, width, height, rowPitch) {
655
- return decodeBcAlphaColor(source, width, height, rowPitch, (blockOffset, x, y) => {
656
- const alphaNibble = source[blockOffset + y * 2 + Math.floor(x / 2)] >>> x % 2 * 4 & 0x0f;
657
- return alphaNibble * 17;
658
- }, 8);
659
- }
660
- function decodeBc3(source, width, height, rowPitch) {
661
- return decodeBcAlphaColor(source, width, height, rowPitch, (blockOffset, x, y) => {
662
- const alpha0 = source[blockOffset],
663
- alpha1 = source[blockOffset + 1];
664
- const alphaValues = alpha0 > alpha1 ? [alpha0, alpha1, Math.round((6 * alpha0 + alpha1) / 7), Math.round((5 * alpha0 + 2 * alpha1) / 7), Math.round((4 * alpha0 + 3 * alpha1) / 7), Math.round((3 * alpha0 + 4 * alpha1) / 7), Math.round((2 * alpha0 + 5 * alpha1) / 7), Math.round((alpha0 + 6 * alpha1) / 7)] : [alpha0, alpha1, Math.round((4 * alpha0 + alpha1) / 5), Math.round((3 * alpha0 + 2 * alpha1) / 5), Math.round((2 * alpha0 + 3 * alpha1) / 5), Math.round((alpha0 + 4 * alpha1) / 5), 0, 255];
665
- let alphaBits = 0n;
666
- for (let i = 0; i < 6; i++) alphaBits |= BigInt(source[blockOffset + 2 + i]) << BigInt(i * 8);
667
- return alphaValues[Number(alphaBits >> BigInt((y * 4 + x) * 3) & 7n)];
668
- }, 8);
669
- }
670
- function decodeBc4(source, width, height, rowPitch, signed) {
671
- const rgba = new Uint8Array(width * height * 4);
672
- forEachBcPixel(width, height, rowPitch, 8, (blockOffset, x, y, outputOffset) => {
673
- const value = decodeBc4Value(source, blockOffset, x, y, signed);
674
- rgba[outputOffset] = value;
675
- rgba[outputOffset + 1] = value;
676
- rgba[outputOffset + 2] = value;
677
- rgba[outputOffset + 3] = 255;
678
- });
679
- return rgba;
680
- }
681
- function decodeBc5(source, width, height, rowPitch, signed) {
682
- const rgba = new Uint8Array(width * height * 4);
683
- forEachBcPixel(width, height, rowPitch, 16, (blockOffset, x, y, outputOffset) => {
684
- rgba[outputOffset] = decodeBc4Value(source, blockOffset, x, y, signed);
685
- rgba[outputOffset + 1] = decodeBc4Value(source, blockOffset + 8, x, y, signed);
686
- rgba[outputOffset + 2] = 0;
687
- rgba[outputOffset + 3] = 255;
688
- });
689
- return rgba;
690
- }
691
- function decodeBc4Value(source, blockOffset, x, y, signed) {
692
- const endpoint0 = signed ? decodeSnormByte(source[blockOffset]) : source[blockOffset];
693
- const endpoint1 = signed ? decodeSnormByte(source[blockOffset + 1]) : source[blockOffset + 1];
694
- const values = endpoint0 > endpoint1 ? [endpoint0, endpoint1, (6 * endpoint0 + endpoint1) / 7, (5 * endpoint0 + 2 * endpoint1) / 7, (4 * endpoint0 + 3 * endpoint1) / 7, (3 * endpoint0 + 4 * endpoint1) / 7, (2 * endpoint0 + 5 * endpoint1) / 7, (endpoint0 + 6 * endpoint1) / 7] : [endpoint0, endpoint1, (4 * endpoint0 + endpoint1) / 5, (3 * endpoint0 + 2 * endpoint1) / 5, (2 * endpoint0 + 3 * endpoint1) / 5, (endpoint0 + 4 * endpoint1) / 5, signed ? -1 : 0, signed ? 1 : 255];
695
- let bits = 0n;
696
- for (let i = 0; i < 6; i++) bits |= BigInt(source[blockOffset + 2 + i]) << BigInt(i * 8);
697
- const value = values[Number(bits >> BigInt((y * 4 + x) * 3) & 7n)];
698
- return signed ? Math.max(0, Math.min(255, Math.round((value + 1) * 127.5))) : Math.round(value);
699
- }
700
- function decodeSnormByte(value) {
701
- const signed = value > 127 ? value - 256 : value;
702
- return Math.max(-1, signed / 127);
703
- }
704
- function forEachBcPixel(width, height, rowPitch, blockBytes, callback) {
705
- for (let blockY = 0; blockY < Math.ceil(height / 4); blockY++) {
706
- for (let blockX = 0; blockX < Math.ceil(width / 4); blockX++) {
707
- const blockOffset = blockY * rowPitch + blockX * blockBytes;
708
- for (let y = 0; y < 4; y++) {
709
- for (let x = 0; x < 4; x++) {
710
- const outputX = blockX * 4 + x;
711
- const outputY = blockY * 4 + y;
712
- if (outputX < width && outputY < height) {
713
- callback(blockOffset, x, y, (outputY * width + outputX) * 4);
714
- }
715
- }
716
- }
717
- }
718
- }
719
- }
720
- function decodeBcAlphaColor(source, width, height, rowPitch, alphaAt, colorOffset = 0) {
721
- const rgba = new Uint8Array(width * height * 4);
722
- for (let blockY = 0; blockY < Math.ceil(height / 4); blockY++) {
723
- for (let blockX = 0; blockX < Math.ceil(width / 4); blockX++) {
724
- const blockOffset = blockY * rowPitch + blockX * 16;
725
- const colorOffsetInBlock = blockOffset + colorOffset;
726
- const c0 = source[colorOffsetInBlock] | source[colorOffsetInBlock + 1] << 8;
727
- const c1 = source[colorOffsetInBlock + 2] | source[colorOffsetInBlock + 3] << 8;
728
- const colors = decodeBc1Colors(c0, c1, true);
729
- const indices = source[colorOffsetInBlock + 4] | source[colorOffsetInBlock + 5] << 8 | source[colorOffsetInBlock + 6] << 16 | source[colorOffsetInBlock + 7] << 24;
730
- for (let y = 0; y < 4; y++) {
731
- for (let x = 0; x < 4; x++) {
732
- const outputX = blockX * 4 + x;
733
- const outputY = blockY * 4 + y;
734
- if (outputX >= width || outputY >= height) continue;
735
- const color = colors[indices >>> 2 * (y * 4 + x) & 3];
736
- const outputOffset = (outputY * width + outputX) * 4;
737
- rgba[outputOffset] = color[0];
738
- rgba[outputOffset + 1] = color[1];
739
- rgba[outputOffset + 2] = color[2];
740
- rgba[outputOffset + 3] = alphaAt(blockOffset, x, y);
741
- }
742
- }
743
- }
744
- }
745
- return rgba;
746
- }
747
- function decodeBc1Colors(c0, c1, forceFourColor = false) {
748
- const first = decode565(c0);
749
- const second = decode565(c1);
750
- const colors = [first, second];
751
- if (c0 > c1 || forceFourColor) {
752
- colors.push([Math.round((2 * first[0] + second[0]) / 3), Math.round((2 * first[1] + second[1]) / 3), Math.round((2 * first[2] + second[2]) / 3), 255], [Math.round((first[0] + 2 * second[0]) / 3), Math.round((first[1] + 2 * second[1]) / 3), Math.round((first[2] + 2 * second[2]) / 3), 255]);
753
- } else {
754
- colors.push([Math.round((first[0] + second[0]) / 2), Math.round((first[1] + second[1]) / 2), Math.round((first[2] + second[2]) / 2), 255], [0, 0, 0, 0]);
755
- }
756
- return colors;
757
- }
758
- function decode565(value) {
759
- return [Math.round((value >>> 11 & 0x1f) * 255 / 31), Math.round((value >>> 5 & 0x3f) * 255 / 63), Math.round((value & 0x1f) * 255 / 31), 255];
760
- }
761
- function getDdsPixelFormat(format) {
762
- if (format.dxgiFormat) return DXGI_PIXEL_FORMATS[format.dxgiFormat] || "";
763
- if (format.pfFlags & DDS_FOURCC && format.fourCc) {
764
- return FOURCC_PIXEL_FORMATS[format.fourCc] || {
765
- 113: "rgba16float",
766
- 116: "rgba32float"
767
- }[format.fourCcCode] || "";
768
- }
769
- if (format.pfFlags & DDS_RGB) {
770
- if (format.rgbBitCount === 32 && format.rBitMask === 0x000000ff && format.gBitMask === 0x0000ff00 && format.bBitMask === 0x00ff0000 && format.aBitMask === 0xff000000) return "rgba8unorm";
771
- if (format.rgbBitCount === 32 && format.rBitMask === 0x00ff0000 && format.gBitMask === 0x0000ff00 && format.bBitMask === 0x000000ff && format.aBitMask === 0xff000000) return "bgra8unorm";
772
- if (format.rgbBitCount === 24 && format.rBitMask === 0x00ff0000 && format.gBitMask === 0x0000ff00 && format.bBitMask === 0x000000ff) return "bgr8unorm";
773
- }
774
- return "";
775
- }
776
- function inspectTGA(bytes) {
777
- return {
778
- sourceFormat: "tga",
779
- width: readU16LE(bytes, 12),
780
- height: readU16LE(bytes, 14),
781
- channels: Math.max(bytes[16] / 8, 1),
782
- imageType: bytes[2],
783
- pixelFormat: `tga-${bytes[16]}`,
784
- isCompressed: [9, 10, 11].includes(bytes[2])
785
- };
786
- }
787
- function pngChannels(colorType) {
788
- if (colorType === 0) return 1;
789
- if (colorType === 2) return 3;
790
- if (colorType === 3) return 1;
791
- if (colorType === 4) return 2;
792
- if (colorType === 6) return 4;
793
- return 0;
794
- }
795
- function readU16BE(bytes, offset) {
796
- return bytes[offset] << 8 | bytes[offset + 1];
797
- }
798
- function readU16LE(bytes, offset) {
799
- return bytes[offset] | bytes[offset + 1] << 8;
800
- }
801
- function readU32BE(bytes, offset) {
802
- return bytes[offset] * 0x1000000 + (bytes[offset + 1] << 16 | bytes[offset + 2] << 8 | bytes[offset + 3]) >>> 0;
803
- }
804
- function readU32LE(bytes, offset) {
805
- return (bytes[offset] | bytes[offset + 1] << 8 | bytes[offset + 2] << 16 | bytes[offset + 3] * 0x1000000) >>> 0;
806
- }
807
- function capitalize(value) {
808
- return value ? value[0].toUpperCase() + value.slice(1) : "Image";
809
- }
810
-
811
- export { DEFAULT_VALUES, OUTPUT_IMAGE, OUTPUT_JSON, OUTPUT_RAW, OUTPUT_RGBA, OUTPUT_TEXTURE, inspectBytes, inspectWithValues, isDDS, isJPEG, isPNG, isSupportedWithValues, isTGA, normalizeEmit, normalizeInputType, normalizeValues, readWithValues, toBytes, toJsonValue };
812
- //# sourceMappingURL=helpers.js.map
1
+ import { decodeBc6h } from './bc6h.js';
2
+ import { decodeBc7 } from './bc7.js';
3
+
4
+ const OUTPUT_IMAGE = "image";
5
+ const OUTPUT_TEXTURE = "texture";
6
+ const OUTPUT_RGBA = "rgba";
7
+ const OUTPUT_RAW = "raw";
8
+ const OUTPUT_JSON = "json";
9
+ const DEFAULT_VALUES = Object.freeze({
10
+ emit: OUTPUT_RAW,
11
+ inputType: "",
12
+ source: ""
13
+ });
14
+ const DDS_HEADER_SIZE = 124;
15
+ const DDS_PIXELFORMAT_OFFSET = 76;
16
+ const DDS_FOURCC = 0x00000004;
17
+ const DDS_RGB = 0x00000040;
18
+ const DDS_ALPHAPIXELS = 0x00000001;
19
+ const DDSCAPS2_CUBEMAP = 0x00000200;
20
+ const DDSCAPS2_VOLUME = 0x00200000;
21
+ const DDS_CUBE_FACE_FLAGS = Object.freeze([["positive-x", 0x00000400], ["negative-x", 0x00000800], ["positive-y", 0x00001000], ["negative-y", 0x00002000], ["positive-z", 0x00004000], ["negative-z", 0x00008000]]);
22
+ const FOURCC_PIXEL_FORMATS = Object.freeze({
23
+ DXT1: "bc1-rgba-unorm",
24
+ DXT3: "bc2-rgba-unorm",
25
+ DXT5: "bc3-rgba-unorm",
26
+ ATI1: "bc4-r-unorm",
27
+ BC4U: "bc4-r-unorm",
28
+ BC4S: "bc4-r-snorm",
29
+ ATI2: "bc5-rg-unorm",
30
+ BC5U: "bc5-rg-unorm",
31
+ BC5S: "bc5-rg-snorm"
32
+ });
33
+ const DXGI_PIXEL_FORMATS = Object.freeze({
34
+ 2: "rgba32float",
35
+ 6: "rgb32float",
36
+ 10: "rgba16float",
37
+ 16: "rg32float",
38
+ 41: "r32float",
39
+ 54: "r16float",
40
+ 28: "rgba8unorm",
41
+ 29: "rgba8unorm-srgb",
42
+ 49: "rg8unorm",
43
+ 61: "r8unorm",
44
+ 71: "bc1-rgba-unorm",
45
+ 72: "bc1-rgba-unorm-srgb",
46
+ 74: "bc2-rgba-unorm",
47
+ 75: "bc2-rgba-unorm-srgb",
48
+ 77: "bc3-rgba-unorm",
49
+ 78: "bc3-rgba-unorm-srgb",
50
+ 80: "bc4-r-unorm",
51
+ 81: "bc4-r-snorm",
52
+ 83: "bc5-rg-unorm",
53
+ 84: "bc5-rg-snorm",
54
+ 87: "bgra8unorm",
55
+ 88: "bgrx8unorm",
56
+ 91: "bgra8unorm-srgb",
57
+ 93: "bgrx8unorm-srgb",
58
+ 95: "bc6h-rgb-ufloat",
59
+ 96: "bc6h-rgb-float",
60
+ 98: "bc7-rgba-unorm",
61
+ 99: "bc7-rgba-unorm-srgb"
62
+ });
63
+ const COMPRESSED_PIXEL_FORMATS = new Set(["bc1-rgba-unorm", "bc1-rgba-unorm-srgb", "bc2-rgba-unorm", "bc2-rgba-unorm-srgb", "bc3-rgba-unorm", "bc3-rgba-unorm-srgb", "bc4-r-unorm", "bc4-r-snorm", "bc5-rg-unorm", "bc5-rg-snorm", "bc6h-rgb-ufloat", "bc6h-rgb-float", "bc7-rgba-unorm", "bc7-rgba-unorm-srgb"]);
64
+ const DEBUG_OUTPUTS = Object.freeze({
65
+ png: "pngJson",
66
+ jpeg: "jpegJson",
67
+ jpg: "jpegJson",
68
+ tga: "tgaJson",
69
+ dds: "ddsJson"
70
+ });
71
+ function normalizeValues(base = DEFAULT_VALUES, options = {}, readerName = "CjsImageFormat") {
72
+ const values = {
73
+ ...DEFAULT_VALUES,
74
+ ...(base || {}),
75
+ ...(options || {})
76
+ };
77
+ values.inputType = normalizeInputType(values.inputType);
78
+ values.emit = normalizeEmit(values.emit, values.inputType, readerName);
79
+ return values;
80
+ }
81
+ function normalizeInputType(inputType) {
82
+ if (!inputType) return "";
83
+ const value = String(inputType).replace(/^\./u, "").toLowerCase();
84
+ return value === "jpg" ? "jpeg" : value;
85
+ }
86
+ function normalizeEmit(emit, inputType, readerName) {
87
+ if (emit === undefined || emit === null) return OUTPUT_RAW;
88
+ if (emit === OUTPUT_JSON && inputType) return DEBUG_OUTPUTS[inputType] || OUTPUT_JSON;
89
+ if ([OUTPUT_IMAGE, OUTPUT_TEXTURE, OUTPUT_RGBA, OUTPUT_RAW, OUTPUT_JSON].includes(emit)) return emit;
90
+ if (Object.values(DEBUG_OUTPUTS).includes(emit)) return emit;
91
+ throw new TypeError(`${readerName}: unknown emit value ${JSON.stringify(emit)}`);
92
+ }
93
+ function toBytes(input) {
94
+ if (input instanceof Uint8Array) return input;
95
+ if (input instanceof ArrayBuffer) return new Uint8Array(input);
96
+ if (ArrayBuffer.isView(input)) return new Uint8Array(input.buffer, input.byteOffset, input.byteLength);
97
+ throw new TypeError("Image input must be Uint8Array, ArrayBuffer, or DataView");
98
+ }
99
+ function inspectWithValues(input, values = DEFAULT_VALUES, expectedType = "") {
100
+ const bytes = toBytes(input);
101
+ const detected = inspectBytes(bytes);
102
+ const sourceFormat = expectedType || values.inputType || detected.sourceFormat;
103
+ if (expectedType && detected.sourceFormat && detected.sourceFormat !== expectedType) {
104
+ throw new TypeError(`CjsFormat${capitalize(expectedType)}: expected ${expectedType}, got ${detected.sourceFormat}`);
105
+ }
106
+ return {
107
+ payloadType: sourceFormat === "dds" ? "texture" : "image",
108
+ mediaTypes: sourceFormat === "dds" ? ["texture", "image"] : ["image"],
109
+ sourceFormat,
110
+ byteLength: bytes.byteLength,
111
+ ...detected
112
+ };
113
+ }
114
+ function isSupportedWithValues(input, values = DEFAULT_VALUES, expectedType = "") {
115
+ try {
116
+ const metadata = inspectWithValues(input, values, expectedType);
117
+ const hasCompleteTextureData = metadata.isDataComplete !== false;
118
+ const canEmitTexture = metadata.sourceFormat === "dds" && metadata.dataOffset > 0 && !!metadata.pixelFormat && hasCompleteTextureData;
119
+ const canEmitRgba = canDecodeDdsToRgba(metadata) && hasCompleteTextureData;
120
+ const textureUnsupportedReason = !metadata.pixelFormat ? "DDS texture format is not recognized yet." : !hasCompleteTextureData ? `DDS texture data is truncated: expected ${metadata.expectedDataBytes} bytes, got ${metadata.dataBytes}.` : "";
121
+ const variants = metadata.sourceFormat === "dds" ? [{
122
+ kind: "compressed",
123
+ payloadType: "texture",
124
+ codec: metadata.pixelFormat || metadata.textureFormat || "unknown",
125
+ supported: canEmitTexture && metadata.isCompressed,
126
+ nativeOnly: !!metadata.nativeTextureOnly,
127
+ reason: metadata.isCompressed ? textureUnsupportedReason : "DDS is not block-compressed."
128
+ }, {
129
+ kind: "texture",
130
+ payloadType: "texture",
131
+ codec: metadata.pixelFormat || metadata.textureFormat || "unknown",
132
+ supported: canEmitTexture,
133
+ reason: canEmitTexture ? "" : textureUnsupportedReason
134
+ }, {
135
+ kind: "rgba",
136
+ payloadType: "rgba",
137
+ codec: canEmitRgba ? isFloatPixelFormat(metadata.pixelFormat) ? "rgba32float" : "rgba8unorm" : "rgba8unorm",
138
+ supported: canEmitRgba,
139
+ reason: canEmitRgba ? "" : textureUnsupportedReason || (metadata.nativeTextureOnly ? `${metadata.pixelFormat} is available as a native compressed texture only; register a software decoder or alternate representation for RGBA fallback.` : "DDS RGBA decode is not implemented for this pixel format.")
140
+ }, {
141
+ kind: "raw",
142
+ payloadType: "raw",
143
+ codec: "dds",
144
+ supported: true
145
+ }] : [{
146
+ kind: "rgba",
147
+ codec: "rgba8",
148
+ supported: false,
149
+ reason: `${metadata.sourceFormat.toUpperCase()} RGBA decode is not implemented yet.`
150
+ }];
151
+ const preferredVariant = variants.find(v => v.supported && v.kind !== "raw") || variants.find(v => v.supported);
152
+ return {
153
+ format: metadata.sourceFormat,
154
+ source: values.source || "buffer",
155
+ supported: metadata.sourceFormat ? variants.some(v => v.supported && v.kind !== "raw") ? "full" : variants.some(v => v.supported) ? "partial" : "none" : "none",
156
+ confidence: metadata.sourceFormat ? 1 : 0,
157
+ preferred: preferredVariant?.codec || "",
158
+ reason: metadata.sourceFormat ? "Container/header recognized." : "Unrecognized image format.",
159
+ metadata,
160
+ variants,
161
+ warnings: [],
162
+ errors: []
163
+ };
164
+ } catch (error) {
165
+ return {
166
+ format: expectedType || values.inputType || "",
167
+ source: values.source || "buffer",
168
+ supported: "none",
169
+ confidence: 0,
170
+ preferred: "",
171
+ reason: error.message,
172
+ metadata: null,
173
+ variants: [],
174
+ warnings: [],
175
+ errors: [error.message]
176
+ };
177
+ }
178
+ }
179
+ function readWithValues(input, values = DEFAULT_VALUES, expectedType = "") {
180
+ const bytes = toBytes(input);
181
+ const metadata = inspectWithValues(bytes, values, expectedType);
182
+ if (values.emit === OUTPUT_RAW) {
183
+ return {
184
+ payloadType: "raw",
185
+ sourceFormat: metadata.sourceFormat,
186
+ metadata,
187
+ bytes
188
+ };
189
+ }
190
+ if (values.emit === OUTPUT_JSON || values.emit === DEBUG_OUTPUTS[metadata.sourceFormat]) {
191
+ return metadata;
192
+ }
193
+ if (values.emit === OUTPUT_TEXTURE && metadata.sourceFormat === "dds") {
194
+ return readDdsTexture(bytes, metadata);
195
+ }
196
+ if ((values.emit === OUTPUT_RGBA || values.emit === OUTPUT_IMAGE) && metadata.sourceFormat === "dds") {
197
+ return readDdsToRgba(bytes, metadata);
198
+ }
199
+ const error = new Error(`${metadata.sourceFormat}: emit "${values.emit}" is not implemented yet`);
200
+ error.code = "CJS_FORMAT_OUTPUT_NOT_IMPLEMENTED";
201
+ error.sourceFormat = metadata.sourceFormat;
202
+ error.emit = values.emit;
203
+ throw error;
204
+ }
205
+ function toJsonValue(value) {
206
+ if (value instanceof Uint8Array) {
207
+ return {
208
+ byteLength: value.byteLength
209
+ };
210
+ }
211
+ if (Array.isArray(value)) return value.map(toJsonValue);
212
+ if (value && typeof value === "object") {
213
+ const output = {};
214
+ for (const [key, entry] of Object.entries(value)) output[key] = toJsonValue(entry);
215
+ return output;
216
+ }
217
+ return value;
218
+ }
219
+ function inspectBytes(bytes) {
220
+ if (isPNG(bytes)) return inspectPNG(bytes);
221
+ if (isJPEG(bytes)) return inspectJPEG(bytes);
222
+ if (isDDS(bytes)) return inspectDDS(bytes);
223
+ if (isTGA(bytes)) return inspectTGA(bytes);
224
+ return {
225
+ sourceFormat: "",
226
+ width: 0,
227
+ height: 0
228
+ };
229
+ }
230
+ function isPNG(bytes) {
231
+ return bytes.byteLength >= 24 && bytes[0] === 0x89 && bytes[1] === 0x50 && bytes[2] === 0x4e && bytes[3] === 0x47 && bytes[4] === 0x0d && bytes[5] === 0x0a && bytes[6] === 0x1a && bytes[7] === 0x0a;
232
+ }
233
+ function isJPEG(bytes) {
234
+ return bytes.byteLength >= 4 && bytes[0] === 0xff && bytes[1] === 0xd8;
235
+ }
236
+ function isDDS(bytes) {
237
+ return bytes.byteLength >= DDS_HEADER_SIZE + 4 && bytes[0] === 0x44 && bytes[1] === 0x44 && bytes[2] === 0x53 && bytes[3] === 0x20 && readU32LE(bytes, 4) === DDS_HEADER_SIZE;
238
+ }
239
+ function isTGA(bytes) {
240
+ if (bytes.byteLength < 18) return false;
241
+ const imageType = bytes[2],
242
+ width = readU16LE(bytes, 12),
243
+ height = readU16LE(bytes, 14),
244
+ bpp = bytes[16];
245
+ return width > 0 && height > 0 && [1, 2, 3, 9, 10, 11].includes(imageType) && [8, 16, 24, 32].includes(bpp);
246
+ }
247
+ function inspectPNG(bytes) {
248
+ const colorType = bytes[25],
249
+ bitDepth = bytes[24];
250
+ return {
251
+ sourceFormat: "png",
252
+ width: readU32BE(bytes, 16),
253
+ height: readU32BE(bytes, 20),
254
+ bitDepth,
255
+ colorType,
256
+ channels: pngChannels(colorType),
257
+ pixelFormat: `png-${bitDepth}-${colorType}`,
258
+ isCompressed: false
259
+ };
260
+ }
261
+ function inspectJPEG(bytes) {
262
+ let offset = 2;
263
+ while (offset + 9 < bytes.byteLength) {
264
+ if (bytes[offset] !== 0xff) {
265
+ offset++;
266
+ continue;
267
+ }
268
+ const marker = bytes[offset + 1],
269
+ length = readU16BE(bytes, offset + 2);
270
+ if ([0xc0, 0xc1, 0xc2, 0xc3].includes(marker)) {
271
+ return {
272
+ sourceFormat: "jpeg",
273
+ width: readU16BE(bytes, offset + 7),
274
+ height: readU16BE(bytes, offset + 5),
275
+ channels: bytes[offset + 9],
276
+ marker,
277
+ pixelFormat: "jpeg-ycbcr",
278
+ isCompressed: true
279
+ };
280
+ }
281
+ offset += Math.max(length + 2, 2);
282
+ }
283
+ return {
284
+ sourceFormat: "jpeg",
285
+ width: 0,
286
+ height: 0,
287
+ channels: 0,
288
+ pixelFormat: "jpeg",
289
+ isCompressed: true
290
+ };
291
+ }
292
+ function inspectDDS(bytes) {
293
+ const headerSize = readU32LE(bytes, 4);
294
+ const pfOffset = DDS_PIXELFORMAT_OFFSET;
295
+ const pfFlags = readU32LE(bytes, pfOffset + 4);
296
+ const fourCcCode = readU32LE(bytes, pfOffset + 8);
297
+ const fourCc = String.fromCharCode(bytes[84], bytes[85], bytes[86], bytes[87]).replace(/\0+$/u, "");
298
+ const rgbBitCount = readU32LE(bytes, pfOffset + 12);
299
+ const rBitMask = readU32LE(bytes, pfOffset + 16);
300
+ const gBitMask = readU32LE(bytes, pfOffset + 20);
301
+ const bBitMask = readU32LE(bytes, pfOffset + 24);
302
+ const aBitMask = readU32LE(bytes, pfOffset + 28);
303
+ const caps2 = readU32LE(bytes, 112);
304
+ const isCube = !!(caps2 & DDSCAPS2_CUBEMAP);
305
+ const isVolume = !!(caps2 & DDSCAPS2_VOLUME);
306
+ const cubeFaces = getDdsCubeFaces(caps2);
307
+ const hasDx10 = fourCc === "DX10" && bytes.byteLength >= 148;
308
+ const dxgiFormat = hasDx10 ? readU32LE(bytes, 128) : 0;
309
+ const resourceDimension = hasDx10 ? readU32LE(bytes, 132) : 0;
310
+ const arraySize = hasDx10 ? readU32LE(bytes, 140) : 1;
311
+ const dataOffset = hasDx10 ? 148 : 128;
312
+ const pixelFormat = getDdsPixelFormat({
313
+ pfFlags,
314
+ fourCc,
315
+ fourCcCode,
316
+ rgbBitCount,
317
+ rBitMask,
318
+ gBitMask,
319
+ bBitMask,
320
+ aBitMask,
321
+ dxgiFormat
322
+ });
323
+ const width = readU32LE(bytes, 16);
324
+ const height = readU32LE(bytes, 12);
325
+ const depth = readU32LE(bytes, 24);
326
+ const mipCount = Math.max(readU32LE(bytes, 28), 1);
327
+ const dataBytes = Math.max(bytes.byteLength - dataOffset, 0);
328
+ const expectedDataBytes = pixelFormat ? getDdsExpectedDataBytes({
329
+ pixelFormat,
330
+ width,
331
+ height,
332
+ depth,
333
+ mipCount,
334
+ arraySize,
335
+ isCube,
336
+ isVolume
337
+ }) : null;
338
+ return {
339
+ sourceFormat: "dds",
340
+ headerSize,
341
+ width,
342
+ height,
343
+ depth,
344
+ mipCount,
345
+ pitchOrLinearSize: readU32LE(bytes, 20),
346
+ pfFlags,
347
+ fourCc,
348
+ fourCcCode,
349
+ rgbBitCount,
350
+ rBitMask,
351
+ gBitMask,
352
+ bBitMask,
353
+ aBitMask,
354
+ caps2,
355
+ hasDx10,
356
+ dxgiFormat,
357
+ resourceDimension,
358
+ arraySize,
359
+ dataOffset,
360
+ dataBytes,
361
+ expectedDataBytes,
362
+ isDataComplete: expectedDataBytes !== null && dataBytes >= expectedDataBytes,
363
+ missingDataBytes: expectedDataBytes === null ? null : Math.max(expectedDataBytes - dataBytes, 0),
364
+ extraDataBytes: expectedDataBytes === null ? null : Math.max(dataBytes - expectedDataBytes, 0),
365
+ faces: isCube ? 6 : 1,
366
+ cubeFaces,
367
+ isCubeComplete: isCube && cubeFaces.length === 6,
368
+ isCube,
369
+ isVolume,
370
+ dimension: isVolume ? "3d" : isCube ? "cube" : "2d",
371
+ pixelFormat,
372
+ textureFormat: pixelFormat || fourCc || "dds-legacy",
373
+ isCompressed: COMPRESSED_PIXEL_FORMATS.has(pixelFormat),
374
+ nativeTextureOnly: COMPRESSED_PIXEL_FORMATS.has(pixelFormat) && !canDecodeDdsToRgba({
375
+ pixelFormat
376
+ }),
377
+ hasAlpha: !!(pfFlags & DDS_ALPHAPIXELS) || /rgba|bgra/u.test(pixelFormat),
378
+ hasMipMaps: readU32LE(bytes, 28) > 1
379
+ };
380
+ }
381
+ function readDdsTexture(bytes, metadata) {
382
+ if (!metadata.pixelFormat) {
383
+ const error = new Error("dds: texture format is not recognized yet");
384
+ error.code = "CJS_FORMAT_OUTPUT_NOT_SUPPORTED";
385
+ error.sourceFormat = metadata.sourceFormat;
386
+ throw error;
387
+ }
388
+ const subresources = buildDdsSubresources(bytes, metadata);
389
+ return {
390
+ payloadType: OUTPUT_TEXTURE,
391
+ sourceFormat: "dds",
392
+ width: metadata.width,
393
+ height: metadata.height,
394
+ depth: metadata.depth,
395
+ faces: metadata.faces,
396
+ cubeFaces: metadata.cubeFaces,
397
+ isCubeComplete: metadata.isCubeComplete,
398
+ dimension: metadata.dimension,
399
+ mipCount: metadata.mipCount,
400
+ arraySize: metadata.arraySize,
401
+ pixelFormat: metadata.pixelFormat,
402
+ isCompressed: metadata.isCompressed,
403
+ dataBytes: metadata.dataBytes,
404
+ expectedDataBytes: metadata.expectedDataBytes,
405
+ isDataComplete: metadata.isDataComplete,
406
+ missingDataBytes: metadata.missingDataBytes,
407
+ extraDataBytes: metadata.extraDataBytes,
408
+ subresources,
409
+ metadata,
410
+ data: bytes.subarray(metadata.dataOffset, metadata.dataOffset + subresources.reduce((sum, entry) => sum + entry.byteLength, 0))
411
+ };
412
+ }
413
+ function canDecodeDdsToRgba(metadata) {
414
+ return ["rgba32float", "rgb32float", "rgba16float", "rg32float", "r32float", "r16float", "rgba8unorm", "rgba8unorm-srgb", "bgra8unorm", "bgra8unorm-srgb", "bgrx8unorm", "bgrx8unorm-srgb", "bgr8unorm", "rg8unorm", "r8unorm", "bc1-rgba-unorm", "bc1-rgba-unorm-srgb", "bc2-rgba-unorm", "bc2-rgba-unorm-srgb", "bc3-rgba-unorm", "bc3-rgba-unorm-srgb", "bc4-r-unorm", "bc4-r-snorm", "bc5-rg-unorm", "bc5-rg-snorm", "bc6h-rgb-ufloat", "bc6h-rgb-float", "bc7-rgba-unorm", "bc7-rgba-unorm-srgb"].includes(metadata.pixelFormat);
415
+ }
416
+ function readDdsToRgba(bytes, metadata) {
417
+ if (!canDecodeDdsToRgba(metadata)) {
418
+ const error = new Error(`dds: RGBA decode is not implemented for ${metadata.pixelFormat || "unknown"}`);
419
+ error.code = "CJS_FORMAT_OUTPUT_NOT_SUPPORTED";
420
+ error.sourceFormat = metadata.sourceFormat;
421
+ throw error;
422
+ }
423
+ const subresource = buildDdsSubresources(bytes, metadata)[0];
424
+ const source = bytes.subarray(metadata.dataOffset + subresource.offset, metadata.dataOffset + subresource.offset + subresource.byteLength);
425
+ const rgba = metadata.pixelFormat.startsWith("bc1-") ? decodeBc1(source, metadata.width, metadata.height, subresource.rowPitch) : metadata.pixelFormat.startsWith("bc2-") ? decodeBc2(source, metadata.width, metadata.height, subresource.rowPitch) : metadata.pixelFormat.startsWith("bc3-") ? decodeBc3(source, metadata.width, metadata.height, subresource.rowPitch) : metadata.pixelFormat.startsWith("bc4-") ? decodeBc4(source, metadata.width, metadata.height, subresource.rowPitch, metadata.pixelFormat.endsWith("-snorm")) : metadata.pixelFormat.startsWith("bc5-") ? decodeBc5(source, metadata.width, metadata.height, subresource.rowPitch, metadata.pixelFormat.endsWith("-snorm")) : metadata.pixelFormat.startsWith("bc6h-") ? decodeBc6h(source, metadata.width, metadata.height, subresource.rowPitch, metadata.pixelFormat === "bc6h-rgb-float") : metadata.pixelFormat.startsWith("bc7-") ? decodeBc7(source, metadata.width, metadata.height, subresource.rowPitch) : isFloatPixelFormat(metadata.pixelFormat) ? decodeFloatUncompressed(source, metadata) : decodeUncompressed(source, metadata);
426
+ const isFloat = isFloatPixelFormat(metadata.pixelFormat);
427
+ return {
428
+ payloadType: OUTPUT_RGBA,
429
+ sourceFormat: "dds",
430
+ width: metadata.width,
431
+ height: metadata.height,
432
+ pixelFormat: isFloat ? "rgba32float" : "rgba8unorm",
433
+ strideBytes: metadata.width * (isFloat ? 16 : 4),
434
+ origin: "top-left",
435
+ colorSpace: isFloat ? "linear" : metadata.pixelFormat.endsWith("-srgb") ? "srgb" : "unknown",
436
+ alphaMode: metadata.hasAlpha ? "straight" : "opaque",
437
+ metadata,
438
+ data: rgba
439
+ };
440
+ }
441
+ function buildDdsSubresources(bytes, metadata) {
442
+ const subresources = [];
443
+ let offset = 0;
444
+ const layers = Math.max(metadata.arraySize, 1) * (metadata.isCube ? 6 : 1);
445
+ for (let layer = 0; layer < layers; layer++) {
446
+ const arrayIndex = metadata.isCube ? Math.floor(layer / 6) : layer;
447
+ const face = metadata.isCube ? layer % 6 : 0;
448
+ for (let mip = 0; mip < metadata.mipCount; mip++) {
449
+ const width = Math.max(metadata.width >> mip, 1);
450
+ const height = Math.max(metadata.height >> mip, 1);
451
+ const depth = metadata.isVolume ? Math.max(metadata.depth >> mip, 1) : 1;
452
+ const layout = getDdsLevelLayout(metadata.pixelFormat, width, height, depth);
453
+ if (offset + layout.byteLength > bytes.byteLength - metadata.dataOffset) {
454
+ const error = new Error("dds: texture data is truncated");
455
+ error.code = "CJS_FORMAT_TRUNCATED";
456
+ error.sourceFormat = metadata.sourceFormat;
457
+ error.expectedDataBytes = metadata.expectedDataBytes;
458
+ error.dataBytes = metadata.dataBytes;
459
+ error.missingDataBytes = metadata.missingDataBytes;
460
+ throw error;
461
+ }
462
+ subresources.push({
463
+ mip,
464
+ layer,
465
+ arrayIndex,
466
+ face,
467
+ offset,
468
+ byteLength: layout.byteLength,
469
+ rowPitch: layout.rowPitch,
470
+ slicePitch: layout.slicePitch,
471
+ width,
472
+ height,
473
+ depth
474
+ });
475
+ offset += layout.byteLength;
476
+ }
477
+ }
478
+ return subresources;
479
+ }
480
+ function getDdsExpectedDataBytes(metadata) {
481
+ let total = 0;
482
+ const layers = Math.max(metadata.arraySize, 1) * (metadata.isCube ? 6 : 1);
483
+ for (let layer = 0; layer < layers; layer++) {
484
+ for (let mip = 0; mip < metadata.mipCount; mip++) {
485
+ const width = Math.max(metadata.width >> mip, 1);
486
+ const height = Math.max(metadata.height >> mip, 1);
487
+ const depth = metadata.isVolume ? Math.max(metadata.depth >> mip, 1) : 1;
488
+ total += getDdsLevelLayout(metadata.pixelFormat, width, height, depth).byteLength;
489
+ }
490
+ }
491
+ return total;
492
+ }
493
+ function getDdsCubeFaces(caps2) {
494
+ if (!(caps2 & DDSCAPS2_CUBEMAP)) {
495
+ return [];
496
+ }
497
+ return DDS_CUBE_FACE_FLAGS.filter(entry => !!(caps2 & entry[1])).map(entry => entry[0]);
498
+ }
499
+ function getDdsLevelLayout(pixelFormat, width, height, depth) {
500
+ const blockBytes = pixelFormat.startsWith("bc1-") || pixelFormat.startsWith("bc4-") ? 8 : pixelFormat.startsWith("bc") ? 16 : 0;
501
+ if (blockBytes) {
502
+ const blocksWide = Math.max(Math.ceil(width / 4), 1);
503
+ const blocksHigh = Math.max(Math.ceil(height / 4), 1);
504
+ const rowPitch = blocksWide * blockBytes;
505
+ const slicePitch = rowPitch * blocksHigh;
506
+ return {
507
+ rowPitch,
508
+ slicePitch,
509
+ byteLength: slicePitch * depth
510
+ };
511
+ }
512
+ const bytesPerPixel = {
513
+ "rgba32float": 16,
514
+ "rgb32float": 12,
515
+ "rgba16float": 8,
516
+ "rg32float": 8,
517
+ "r32float": 4,
518
+ "r16float": 2,
519
+ "rgba8unorm": 4,
520
+ "rgba8unorm-srgb": 4,
521
+ "bgra8unorm": 4,
522
+ "bgra8unorm-srgb": 4,
523
+ "bgrx8unorm": 4,
524
+ "bgrx8unorm-srgb": 4,
525
+ "bgr8unorm": 3,
526
+ "rg8unorm": 2,
527
+ "r8unorm": 1
528
+ }[pixelFormat];
529
+ if (!bytesPerPixel) {
530
+ const error = new Error(`dds: cannot calculate subresource layout for ${pixelFormat || "unknown"}`);
531
+ error.code = "CJS_FORMAT_OUTPUT_NOT_SUPPORTED";
532
+ throw error;
533
+ }
534
+ const rowPitch = width * bytesPerPixel;
535
+ const slicePitch = rowPitch * height;
536
+ return {
537
+ rowPitch,
538
+ slicePitch,
539
+ byteLength: slicePitch * depth
540
+ };
541
+ }
542
+ function decodeUncompressed(source, metadata) {
543
+ const rgba = new Uint8Array(metadata.width * metadata.height * 4);
544
+ const bytesPerPixel = metadata.pixelFormat === "bgr8unorm" ? 3 : metadata.pixelFormat === "rg8unorm" ? 2 : metadata.pixelFormat === "r8unorm" ? 1 : 4;
545
+ for (let pixel = 0; pixel < metadata.width * metadata.height; pixel++) {
546
+ const sourceOffset = pixel * bytesPerPixel;
547
+ const outputOffset = pixel * 4;
548
+ if (metadata.pixelFormat.startsWith("bgra") || metadata.pixelFormat.startsWith("bgrx")) {
549
+ rgba[outputOffset] = source[sourceOffset + 2];
550
+ rgba[outputOffset + 1] = source[sourceOffset + 1];
551
+ rgba[outputOffset + 2] = source[sourceOffset];
552
+ rgba[outputOffset + 3] = metadata.pixelFormat.startsWith("bgrx") ? 255 : source[sourceOffset + 3];
553
+ } else if (metadata.pixelFormat === "bgr8unorm") {
554
+ rgba[outputOffset] = source[sourceOffset + 2];
555
+ rgba[outputOffset + 1] = source[sourceOffset + 1];
556
+ rgba[outputOffset + 2] = source[sourceOffset];
557
+ rgba[outputOffset + 3] = 255;
558
+ } else if (metadata.pixelFormat === "rg8unorm") {
559
+ rgba[outputOffset] = source[sourceOffset];
560
+ rgba[outputOffset + 1] = source[sourceOffset + 1];
561
+ rgba[outputOffset + 2] = 0;
562
+ rgba[outputOffset + 3] = 255;
563
+ } else {
564
+ rgba[outputOffset] = source[sourceOffset];
565
+ rgba[outputOffset + 1] = bytesPerPixel > 1 ? source[sourceOffset + 1] : source[sourceOffset];
566
+ rgba[outputOffset + 2] = bytesPerPixel > 2 ? source[sourceOffset + 2] : source[sourceOffset];
567
+ rgba[outputOffset + 3] = bytesPerPixel > 3 ? source[sourceOffset + 3] : 255;
568
+ }
569
+ }
570
+ return rgba;
571
+ }
572
+ function decodeFloatUncompressed(source, metadata) {
573
+ const rgba = new Float32Array(metadata.width * metadata.height * 4);
574
+ const view = new DataView(source.buffer, source.byteOffset, source.byteLength);
575
+ const bytesPerPixel = {
576
+ "rgba32float": 16,
577
+ "rgb32float": 12,
578
+ "rgba16float": 8,
579
+ "rg32float": 8,
580
+ "r32float": 4,
581
+ "r16float": 2
582
+ }[metadata.pixelFormat];
583
+ for (let pixel = 0; pixel < metadata.width * metadata.height; pixel++) {
584
+ const sourceOffset = pixel * bytesPerPixel;
585
+ const outputOffset = pixel * 4;
586
+ let red, green, blue, alpha;
587
+ if (metadata.pixelFormat === "rgba32float") {
588
+ red = view.getFloat32(sourceOffset, true);
589
+ green = view.getFloat32(sourceOffset + 4, true);
590
+ blue = view.getFloat32(sourceOffset + 8, true);
591
+ alpha = view.getFloat32(sourceOffset + 12, true);
592
+ } else if (metadata.pixelFormat === "rgb32float") {
593
+ red = view.getFloat32(sourceOffset, true);
594
+ green = view.getFloat32(sourceOffset + 4, true);
595
+ blue = view.getFloat32(sourceOffset + 8, true);
596
+ alpha = 1;
597
+ } else if (metadata.pixelFormat === "rgba16float") {
598
+ red = halfToFloat(view.getUint16(sourceOffset, true));
599
+ green = halfToFloat(view.getUint16(sourceOffset + 2, true));
600
+ blue = halfToFloat(view.getUint16(sourceOffset + 4, true));
601
+ alpha = halfToFloat(view.getUint16(sourceOffset + 6, true));
602
+ } else if (metadata.pixelFormat === "rg32float") {
603
+ red = view.getFloat32(sourceOffset, true);
604
+ green = view.getFloat32(sourceOffset + 4, true);
605
+ blue = 0;
606
+ alpha = 1;
607
+ } else {
608
+ red = metadata.pixelFormat === "r16float" ? halfToFloat(view.getUint16(sourceOffset, true)) : view.getFloat32(sourceOffset, true);
609
+ green = red;
610
+ blue = red;
611
+ alpha = 1;
612
+ }
613
+ rgba[outputOffset] = red;
614
+ rgba[outputOffset + 1] = green;
615
+ rgba[outputOffset + 2] = blue;
616
+ rgba[outputOffset + 3] = alpha;
617
+ }
618
+ return rgba;
619
+ }
620
+ function isFloatPixelFormat(pixelFormat) {
621
+ return typeof pixelFormat === "string" && (pixelFormat.startsWith("bc6h-") || ["rgba32float", "rgb32float", "rgba16float", "rg32float", "r32float", "r16float"].includes(pixelFormat));
622
+ }
623
+ function halfToFloat(value) {
624
+ const sign = value & 0x8000 ? -1 : 1;
625
+ const exponent = value >>> 10 & 0x1f;
626
+ const mantissa = value & 0x03ff;
627
+ if (exponent === 0) {
628
+ return sign * Math.pow(2, -14) * (mantissa / 1024);
629
+ }
630
+ if (exponent === 0x1f) {
631
+ return mantissa === 0 ? sign * Infinity : NaN;
632
+ }
633
+ return sign * Math.pow(2, exponent - 15) * (1 + mantissa / 1024);
634
+ }
635
+ function decodeBc1(source, width, height, rowPitch) {
636
+ const rgba = new Uint8Array(width * height * 4);
637
+ for (let blockY = 0; blockY < Math.ceil(height / 4); blockY++) {
638
+ for (let blockX = 0; blockX < Math.ceil(width / 4); blockX++) {
639
+ const blockOffset = blockY * rowPitch + blockX * 8;
640
+ const c0 = source[blockOffset] | source[blockOffset + 1] << 8;
641
+ const c1 = source[blockOffset + 2] | source[blockOffset + 3] << 8;
642
+ const colors = decodeBc1Colors(c0, c1);
643
+ const indices = source[blockOffset + 4] | source[blockOffset + 5] << 8 | source[blockOffset + 6] << 16 | source[blockOffset + 7] << 24;
644
+ for (let y = 0; y < 4; y++) {
645
+ for (let x = 0; x < 4; x++) {
646
+ const outputX = blockX * 4 + x;
647
+ const outputY = blockY * 4 + y;
648
+ if (outputX >= width || outputY >= height) continue;
649
+ const color = colors[indices >>> 2 * (y * 4 + x) & 3];
650
+ rgba.set(color, (outputY * width + outputX) * 4);
651
+ }
652
+ }
653
+ }
654
+ }
655
+ return rgba;
656
+ }
657
+ function decodeBc2(source, width, height, rowPitch) {
658
+ return decodeBcAlphaColor(source, width, height, rowPitch, (blockOffset, x, y) => {
659
+ const alphaNibble = source[blockOffset + y * 2 + Math.floor(x / 2)] >>> x % 2 * 4 & 0x0f;
660
+ return alphaNibble * 17;
661
+ }, 8);
662
+ }
663
+ function decodeBc3(source, width, height, rowPitch) {
664
+ return decodeBcAlphaColor(source, width, height, rowPitch, (blockOffset, x, y) => {
665
+ const alpha0 = source[blockOffset],
666
+ alpha1 = source[blockOffset + 1];
667
+ const alphaValues = alpha0 > alpha1 ? [alpha0, alpha1, Math.round((6 * alpha0 + alpha1) / 7), Math.round((5 * alpha0 + 2 * alpha1) / 7), Math.round((4 * alpha0 + 3 * alpha1) / 7), Math.round((3 * alpha0 + 4 * alpha1) / 7), Math.round((2 * alpha0 + 5 * alpha1) / 7), Math.round((alpha0 + 6 * alpha1) / 7)] : [alpha0, alpha1, Math.round((4 * alpha0 + alpha1) / 5), Math.round((3 * alpha0 + 2 * alpha1) / 5), Math.round((2 * alpha0 + 3 * alpha1) / 5), Math.round((alpha0 + 4 * alpha1) / 5), 0, 255];
668
+ let alphaBits = 0n;
669
+ for (let i = 0; i < 6; i++) alphaBits |= BigInt(source[blockOffset + 2 + i]) << BigInt(i * 8);
670
+ return alphaValues[Number(alphaBits >> BigInt((y * 4 + x) * 3) & 7n)];
671
+ }, 8);
672
+ }
673
+ function decodeBc4(source, width, height, rowPitch, signed) {
674
+ const rgba = new Uint8Array(width * height * 4);
675
+ forEachBcPixel(width, height, rowPitch, 8, (blockOffset, x, y, outputOffset) => {
676
+ const value = decodeBc4Value(source, blockOffset, x, y, signed);
677
+ rgba[outputOffset] = value;
678
+ rgba[outputOffset + 1] = value;
679
+ rgba[outputOffset + 2] = value;
680
+ rgba[outputOffset + 3] = 255;
681
+ });
682
+ return rgba;
683
+ }
684
+ function decodeBc5(source, width, height, rowPitch, signed) {
685
+ const rgba = new Uint8Array(width * height * 4);
686
+ forEachBcPixel(width, height, rowPitch, 16, (blockOffset, x, y, outputOffset) => {
687
+ rgba[outputOffset] = decodeBc4Value(source, blockOffset, x, y, signed);
688
+ rgba[outputOffset + 1] = decodeBc4Value(source, blockOffset + 8, x, y, signed);
689
+ rgba[outputOffset + 2] = 0;
690
+ rgba[outputOffset + 3] = 255;
691
+ });
692
+ return rgba;
693
+ }
694
+ function decodeBc4Value(source, blockOffset, x, y, signed) {
695
+ const endpoint0 = signed ? decodeSnormByte(source[blockOffset]) : source[blockOffset];
696
+ const endpoint1 = signed ? decodeSnormByte(source[blockOffset + 1]) : source[blockOffset + 1];
697
+ const values = endpoint0 > endpoint1 ? [endpoint0, endpoint1, (6 * endpoint0 + endpoint1) / 7, (5 * endpoint0 + 2 * endpoint1) / 7, (4 * endpoint0 + 3 * endpoint1) / 7, (3 * endpoint0 + 4 * endpoint1) / 7, (2 * endpoint0 + 5 * endpoint1) / 7, (endpoint0 + 6 * endpoint1) / 7] : [endpoint0, endpoint1, (4 * endpoint0 + endpoint1) / 5, (3 * endpoint0 + 2 * endpoint1) / 5, (2 * endpoint0 + 3 * endpoint1) / 5, (endpoint0 + 4 * endpoint1) / 5, signed ? -1 : 0, signed ? 1 : 255];
698
+ let bits = 0n;
699
+ for (let i = 0; i < 6; i++) bits |= BigInt(source[blockOffset + 2 + i]) << BigInt(i * 8);
700
+ const value = values[Number(bits >> BigInt((y * 4 + x) * 3) & 7n)];
701
+ return signed ? Math.max(0, Math.min(255, Math.round((value + 1) * 127.5))) : Math.round(value);
702
+ }
703
+ function decodeSnormByte(value) {
704
+ const signed = value > 127 ? value - 256 : value;
705
+ return Math.max(-1, signed / 127);
706
+ }
707
+ function forEachBcPixel(width, height, rowPitch, blockBytes, callback) {
708
+ for (let blockY = 0; blockY < Math.ceil(height / 4); blockY++) {
709
+ for (let blockX = 0; blockX < Math.ceil(width / 4); blockX++) {
710
+ const blockOffset = blockY * rowPitch + blockX * blockBytes;
711
+ for (let y = 0; y < 4; y++) {
712
+ for (let x = 0; x < 4; x++) {
713
+ const outputX = blockX * 4 + x;
714
+ const outputY = blockY * 4 + y;
715
+ if (outputX < width && outputY < height) {
716
+ callback(blockOffset, x, y, (outputY * width + outputX) * 4);
717
+ }
718
+ }
719
+ }
720
+ }
721
+ }
722
+ }
723
+ function decodeBcAlphaColor(source, width, height, rowPitch, alphaAt, colorOffset = 0) {
724
+ const rgba = new Uint8Array(width * height * 4);
725
+ for (let blockY = 0; blockY < Math.ceil(height / 4); blockY++) {
726
+ for (let blockX = 0; blockX < Math.ceil(width / 4); blockX++) {
727
+ const blockOffset = blockY * rowPitch + blockX * 16;
728
+ const colorOffsetInBlock = blockOffset + colorOffset;
729
+ const c0 = source[colorOffsetInBlock] | source[colorOffsetInBlock + 1] << 8;
730
+ const c1 = source[colorOffsetInBlock + 2] | source[colorOffsetInBlock + 3] << 8;
731
+ const colors = decodeBc1Colors(c0, c1, true);
732
+ const indices = source[colorOffsetInBlock + 4] | source[colorOffsetInBlock + 5] << 8 | source[colorOffsetInBlock + 6] << 16 | source[colorOffsetInBlock + 7] << 24;
733
+ for (let y = 0; y < 4; y++) {
734
+ for (let x = 0; x < 4; x++) {
735
+ const outputX = blockX * 4 + x;
736
+ const outputY = blockY * 4 + y;
737
+ if (outputX >= width || outputY >= height) continue;
738
+ const color = colors[indices >>> 2 * (y * 4 + x) & 3];
739
+ const outputOffset = (outputY * width + outputX) * 4;
740
+ rgba[outputOffset] = color[0];
741
+ rgba[outputOffset + 1] = color[1];
742
+ rgba[outputOffset + 2] = color[2];
743
+ rgba[outputOffset + 3] = alphaAt(blockOffset, x, y);
744
+ }
745
+ }
746
+ }
747
+ }
748
+ return rgba;
749
+ }
750
+ function decodeBc1Colors(c0, c1, forceFourColor = false) {
751
+ const first = decode565(c0);
752
+ const second = decode565(c1);
753
+ const colors = [first, second];
754
+ if (c0 > c1 || forceFourColor) {
755
+ colors.push([Math.round((2 * first[0] + second[0]) / 3), Math.round((2 * first[1] + second[1]) / 3), Math.round((2 * first[2] + second[2]) / 3), 255], [Math.round((first[0] + 2 * second[0]) / 3), Math.round((first[1] + 2 * second[1]) / 3), Math.round((first[2] + 2 * second[2]) / 3), 255]);
756
+ } else {
757
+ colors.push([Math.round((first[0] + second[0]) / 2), Math.round((first[1] + second[1]) / 2), Math.round((first[2] + second[2]) / 2), 255], [0, 0, 0, 0]);
758
+ }
759
+ return colors;
760
+ }
761
+ function decode565(value) {
762
+ return [Math.round((value >>> 11 & 0x1f) * 255 / 31), Math.round((value >>> 5 & 0x3f) * 255 / 63), Math.round((value & 0x1f) * 255 / 31), 255];
763
+ }
764
+ function getDdsPixelFormat(format) {
765
+ if (format.dxgiFormat) return DXGI_PIXEL_FORMATS[format.dxgiFormat] || "";
766
+ if (format.pfFlags & DDS_FOURCC && format.fourCc) {
767
+ return FOURCC_PIXEL_FORMATS[format.fourCc] || {
768
+ 113: "rgba16float",
769
+ 116: "rgba32float"
770
+ }[format.fourCcCode] || "";
771
+ }
772
+ if (format.pfFlags & DDS_RGB) {
773
+ if (format.rgbBitCount === 32 && format.rBitMask === 0x000000ff && format.gBitMask === 0x0000ff00 && format.bBitMask === 0x00ff0000 && format.aBitMask === 0xff000000) return "rgba8unorm";
774
+ if (format.rgbBitCount === 32 && format.rBitMask === 0x00ff0000 && format.gBitMask === 0x0000ff00 && format.bBitMask === 0x000000ff && format.aBitMask === 0xff000000) return "bgra8unorm";
775
+ if (format.rgbBitCount === 24 && format.rBitMask === 0x00ff0000 && format.gBitMask === 0x0000ff00 && format.bBitMask === 0x000000ff) return "bgr8unorm";
776
+ }
777
+ return "";
778
+ }
779
+ function inspectTGA(bytes) {
780
+ return {
781
+ sourceFormat: "tga",
782
+ width: readU16LE(bytes, 12),
783
+ height: readU16LE(bytes, 14),
784
+ channels: Math.max(bytes[16] / 8, 1),
785
+ imageType: bytes[2],
786
+ pixelFormat: `tga-${bytes[16]}`,
787
+ isCompressed: [9, 10, 11].includes(bytes[2])
788
+ };
789
+ }
790
+ function pngChannels(colorType) {
791
+ if (colorType === 0) return 1;
792
+ if (colorType === 2) return 3;
793
+ if (colorType === 3) return 1;
794
+ if (colorType === 4) return 2;
795
+ if (colorType === 6) return 4;
796
+ return 0;
797
+ }
798
+ function readU16BE(bytes, offset) {
799
+ return bytes[offset] << 8 | bytes[offset + 1];
800
+ }
801
+ function readU16LE(bytes, offset) {
802
+ return bytes[offset] | bytes[offset + 1] << 8;
803
+ }
804
+ function readU32BE(bytes, offset) {
805
+ return bytes[offset] * 0x1000000 + (bytes[offset + 1] << 16 | bytes[offset + 2] << 8 | bytes[offset + 3]) >>> 0;
806
+ }
807
+ function readU32LE(bytes, offset) {
808
+ return (bytes[offset] | bytes[offset + 1] << 8 | bytes[offset + 2] << 16 | bytes[offset + 3] * 0x1000000) >>> 0;
809
+ }
810
+ function capitalize(value) {
811
+ return value ? value[0].toUpperCase() + value.slice(1) : "Image";
812
+ }
813
+
814
+ export { DEFAULT_VALUES, OUTPUT_IMAGE, OUTPUT_JSON, OUTPUT_RAW, OUTPUT_RGBA, OUTPUT_TEXTURE, inspectBytes, inspectWithValues, isDDS, isJPEG, isPNG, isSupportedWithValues, isTGA, normalizeEmit, normalizeInputType, normalizeValues, readWithValues, toBytes, toJsonValue };
815
+ //# sourceMappingURL=helpers.js.map