/* Copyright (c) 2025 Alex Diener This software is provided 'as-is', without any express or implied warranty. In no event will the authors be held liable for any damages arising from the use of this software. Permission is granted to anyone to use this software for any purpose, including commercial applications, and to alter it and redistribute it freely, subject to the following restrictions: 1. The origin of this software must not be misrepresented; you must not claim that you wrote the original software. If you use this software in a product, an acknowledgment in the product documentation would be appreciated but is not required. 2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software. 3. This notice may not be removed or altered from any source distribution. Alex Diener alex@ludobloom.com */ #include "3dmodelio/glTF3DModelIO.h" #include "jsonio/JSONParser.h" #include "jsonio/JSONEmitter.h" #include "utilities/IOUtilities.h" #include "utilities/printfFormats.h" #include #include #include #include #include static void writeFileWithOverwriteConfirmation(const char * filePath, const void * data, size_t size) { if (confirmFileOverwrite(filePath, true, false)) { if (!writeFileSimple(filePath, data, size)) { fprintf(stderr, "Error: Couldn't write \"%s\" (errno = %d)\n", filePath, errno); } } else { fprintf(stderr, "Overwrite of file \"%s\" not confirmed; no data written to that location\n", filePath); } } int main(int argc, char ** argv) { const char * inFile = NULL; bool dumpContents = false; bool formatJSON = false; for (int argIndex = 1; argIndex < argc; argIndex++) { if (!strcmp(argv[argIndex], "--dump")) { dumpContents = true; } else if (!strcmp(argv[argIndex], "--format-json")) { formatJSON = true; } else if (inFile == NULL) { inFile = argv[argIndex]; } else { fprintf(stderr, "Unrecognized argument \"%s\"\n", argv[argIndex]); } } if (inFile == NULL) { fprintf(stderr, "Must specify input file\n"); return EXIT_FAILURE; } size_t size; void * data = readFileSimple(argv[1], &size); if (data == NULL) { fprintf(stderr, "Couldn't read \"%s\" (errno = %u)\n", argv[1], errno); return EXIT_FAILURE; } glbFile file; if (!glbFile_initWithData(&file, data, size)) { fprintf(stderr, "Couldn't parse \"%s\" as a glb file\n", argv[1]); return EXIT_FAILURE; } const char * baseName = getBaseName(inFile); if (dumpContents) { size_t chunkSize; glbChunkType chunkType; const void * chunkData; unsigned int unknownChunkIndex = 1; while ((chunkData = glbFile_nextChunk(&file, &chunkSize, &chunkType)) != NULL) { char filePath[2048]; switch (chunkType) { case GLB_CHUNK_JSON: snprintf_safe(filePath, sizeof(filePath), "%s.json", baseName); if (formatJSON) { struct JSONParseError jsonError; JSONNode * rootNode = JSONParser_loadString(chunkData, chunkSize, &jsonError); if (rootNode == NULL) { fprintf(stderr, "Error: Couldn't parse JSON chunk: %s (position " SIZE_T_FORMAT ", code %d); writing unformatted\n", jsonError.description, jsonError.textPosition.charIndex, jsonError.code); } else { if (confirmFileOverwrite(filePath, true, false)) { if (!JSONEmitter_writeFile(rootNode, JSONEmitterFormat_multiLine, filePath, NULL)) { fprintf(stderr, "Error: Couldn't write \"%s\" (errno = %d)\n", filePath, errno); } } else { fprintf(stderr, "Overwrite of file \"%s\" not confirmed; no data written to that location\n", filePath); } JSONNode_dispose(rootNode); continue; } } break; case GLB_CHUNK_BIN: snprintf_safe(filePath, sizeof(filePath), "%s.bin", baseName); break; default: fprintf(stderr, "Unknown chunk type 0x%X\n", chunkType); snprintf_safe(filePath, sizeof(filePath), "%s_%u.bin", baseName, unknownChunkIndex++); break; } writeFileWithOverwriteConfirmation(filePath, chunkData, chunkSize); } } glTFDocument * document = glTFDocument_readBinaryData(data, size); free(data); if (document == NULL) { fprintf(stderr, "Couldn't parse \"%s\" as a glTF document\n", argv[1]); return EXIT_FAILURE; } glTFDocumentValidationStatus status = glTFDocument_validate(document); if (status != GLTF_VALIDATION_OK) { fprintf(stderr, "Warning: glTF validation returned %d for file \"%s\"\n", status, argv[1]); } if (dumpContents) { for (unsigned int imageIndex = 0; imageIndex < document->imageCount; imageIndex++) { glTFBufferView * bufferView = &document->bufferViews[document->images[imageIndex].bufferView]; const char * extension = "unknown"; switch (document->images[imageIndex].mimeType) { case GLTF_IMAGE_MIME_TYPE_UNKNOWN: break; case GLTF_IMAGE_MIME_TYPE_JPEG: extension = "jpg"; break; case GLTF_IMAGE_MIME_TYPE_PNG: extension = "png"; break; } char filePath[2048]; snprintf_safe(filePath, sizeof(filePath), "%s_%u.%s", baseName, imageIndex, extension); writeFileWithOverwriteConfirmation(filePath, document->buffers[bufferView->buffer].data + bufferView->byteOffset, bufferView->byteLength); } } return EXIT_SUCCESS; }