std::shared_ptr TextureBuilder::simple()

in src/gltf/TextureBuilder.cpp [173:226]


std::shared_ptr<TextureData> TextureBuilder::simple(int rawTexIndex, const std::string& tag) {
  const std::string key = texIndicesKey({rawTexIndex}, tag);
  auto iter = textureByIndicesKey.find(key);
  if (iter != textureByIndicesKey.end()) {
    return iter->second;
  }

  const RawTexture& rawTexture = raw.GetTexture(rawTexIndex);
  const std::string textureName = FileUtils::GetFileBase(rawTexture.name);
  const std::string relativeFilename = FileUtils::GetFileName(rawTexture.fileLocation);

  ImageData* image = nullptr;
  if (options.outputBinary) {
    auto bufferView = gltf.AddBufferViewForFile(*gltf.defaultBuffer, rawTexture.fileLocation);
    if (bufferView) {
      const auto& suffix = FileUtils::GetFileSuffix(rawTexture.fileLocation);
      std::string mimeType;
      if (suffix) {
        mimeType = ImageUtils::suffixToMimeType(suffix.value());
      } else {
        mimeType = "image/jpeg";
        fmt::printf(
            "Warning: Can't deduce mime type of texture '%s'; using %s.\n",
            rawTexture.fileLocation,
            mimeType);
      }
      image = new ImageData(relativeFilename, *bufferView, mimeType);
    }

  } else if (!relativeFilename.empty()) {
    image = new ImageData(relativeFilename, relativeFilename);
    std::string outputPath = outputFolder + "/" + relativeFilename;
    if (FileUtils::CopyFile(rawTexture.fileLocation, outputPath, true)) {
      if (verboseOutput) {
        fmt::printf("Copied texture '%s' to output folder: %s\n", textureName, outputPath);
      }
    } else {
      // no point commenting further on read/write error; CopyFile() does enough of that, and we
      // certainly want to to add an image struct to the glTF JSON, with the correct relative path
      // reference, even if the copy failed.
    }
  }
  if (!image) {
    // fallback is tiny transparent PNG
    image = new ImageData(
        textureName,
        "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mP8/5+hHgAHggJ/PchI7wAAAABJRU5ErkJggg==");
  }

  std::shared_ptr<TextureData> texDat = gltf.textures.hold(
      new TextureData(textureName, *gltf.defaultSampler, *gltf.images.hold(image)));
  textureByIndicesKey.insert(std::make_pair(key, texDat));
  return texDat;
}