From 18b0aa03717cd8209b6c239457bcf69f0d39ecf7 Mon Sep 17 00:00:00 2001
From: Dave Parks <davep@lindenlab.com>
Date: Thu, 16 Jun 2022 16:16:53 -0500
Subject: [PATCH] SL-17619 Add support for embedded textures to GLTF importer

---
 indra/llimage/llimage.cpp          |  9 +++++++++
 indra/llimage/llimage.h            |  1 +
 indra/newview/llmaterialeditor.cpp | 27 ++++++++++++++++-----------
 indra/newview/llviewertexture.cpp  |  7 +++++++
 indra/newview/llviewertexture.h    |  2 ++
 5 files changed, 35 insertions(+), 11 deletions(-)

diff --git a/indra/llimage/llimage.cpp b/indra/llimage/llimage.cpp
index 0fa027c9c30..ad7124b5aae 100644
--- a/indra/llimage/llimage.cpp
+++ b/indra/llimage/llimage.cpp
@@ -815,6 +815,15 @@ LLImageRaw::LLImageRaw(U16 width, U16 height, S8 components)
 	++sRawImageCount;
 }
 
+LLImageRaw::LLImageRaw(const U8* data, U16 width, U16 height, S8 components)
+    : LLImageBase()
+{
+    if (allocateDataSize(width, height, components))
+    {
+        memcpy(getData(), data, width * height * components);
+    }
+}
+
 LLImageRaw::LLImageRaw(U8 *data, U16 width, U16 height, S8 components, bool no_copy)
 	: LLImageBase()
 {
diff --git a/indra/llimage/llimage.h b/indra/llimage/llimage.h
index 7a588cfb037..59c192d9f85 100644
--- a/indra/llimage/llimage.h
+++ b/indra/llimage/llimage.h
@@ -184,6 +184,7 @@ class LLImageRaw : public LLImageBase
 public:
 	LLImageRaw();
 	LLImageRaw(U16 width, U16 height, S8 components);
+    LLImageRaw(const U8* data, U16 width, U16 height, S8 components);
 	LLImageRaw(U8 *data, U16 width, U16 height, S8 components, bool no_copy = false);
 	// Construct using createFromFile (used by tools)
 	//LLImageRaw(const std::string& filename, bool j2c_lowest_mip_only = false);
diff --git a/indra/newview/llmaterialeditor.cpp b/indra/newview/llmaterialeditor.cpp
index f23564c51b9..05e7ff524a4 100644
--- a/indra/newview/llmaterialeditor.cpp
+++ b/indra/newview/llmaterialeditor.cpp
@@ -295,35 +295,40 @@ void LLMaterialFilePicker::notify(const std::vector<std::string>& filenames)
     }
 }
 
-static std::string get_texture_uri(const tinygltf::Model& model, S32 texture_index)
+const tinygltf::Image* get_image_from_texture_index(const tinygltf::Model& model, S32 texture_index)
 {
-    std::string ret;
-
     if (texture_index >= 0)
     {
         S32 source_idx = model.textures[texture_index].source;
         if (source_idx >= 0)
         {
-            ret = model.images[source_idx].uri;
+            return &(model.images[source_idx]);
         }
     }
 
-    return ret;
+    return nullptr;
 }
 
 static LLViewerFetchedTexture* get_texture(const std::string& folder, const tinygltf::Model& model, S32 texture_index)
 {
     LLViewerFetchedTexture* ret = nullptr;
-    std::string file = get_texture_uri(model, texture_index);
-    if (!file.empty())
+
+    const tinygltf::Image* image = get_image_from_texture_index(model, texture_index);
+
+    if (image != nullptr && 
+        image->bits == 8 &&
+        !image->image.empty() &&
+        image->component <= 4)
     {
-        std::string uri = folder;
-        gDirUtilp->append(uri, file);
+        LLPointer<LLImageRaw> rawImage = new LLImageRaw(&image->image[0], image->width, image->height, image->component);
+        
+        ret = LLViewerTextureManager::getFetchedTexture(rawImage, FTType::FTT_LOCAL_FILE, true);
 
-        ret = LLViewerTextureManager::getFetchedTextureFromUrl("file://" + LLURI::unescape(uri), FTT_LOCAL_FILE, TRUE, LLGLTexture::BOOST_PREVIEW);
-        //ret->setLoadedCallback(LLMaterialFilePicker::textureLoadedCallback, 0, TRUE, FALSE, opaque, NULL, FALSE);
         ret->forceToSaveRawImage(0, F32_MAX);
     }
+
+    // TODO: provide helpful error message if image fails to load
+
     return ret;
 }
 
diff --git a/indra/newview/llviewertexture.cpp b/indra/newview/llviewertexture.cpp
index e3ac56d0d39..c389d2122e9 100644
--- a/indra/newview/llviewertexture.cpp
+++ b/indra/newview/llviewertexture.cpp
@@ -287,6 +287,13 @@ LLPointer<LLViewerTexture> LLViewerTextureManager::getLocalTexture(const U32 wid
 	return tex;
 }
 
+LLViewerFetchedTexture* LLViewerTextureManager::getFetchedTexture(const LLImageRaw* raw, FTType type, bool usemipmaps)
+{
+    LLViewerFetchedTexture* ret = new LLViewerFetchedTexture(raw, type, usemipmaps);
+    gTextureList.addImage(ret, TEX_LIST_STANDARD);
+    return ret;
+}
+
 LLViewerFetchedTexture* LLViewerTextureManager::getFetchedTexture(
 	                                               const LLUUID &image_id,											       
 												   FTType f_type,
diff --git a/indra/newview/llviewertexture.h b/indra/newview/llviewertexture.h
index b953d7006b2..53cf9111022 100644
--- a/indra/newview/llviewertexture.h
+++ b/indra/newview/llviewertexture.h
@@ -668,6 +668,8 @@ class LLViewerTextureManager
 	static LLPointer<LLViewerTexture> getLocalTexture(const LLImageRaw* raw, BOOL usemipmaps) ;
 	static LLPointer<LLViewerTexture> getLocalTexture(const U32 width, const U32 height, const U8 components, BOOL usemipmaps, BOOL generate_gl_tex = TRUE) ;
 
+    static LLViewerFetchedTexture* getFetchedTexture(const LLImageRaw* raw, FTType type, bool usemipmaps);
+
 	static LLViewerFetchedTexture* getFetchedTexture(const LLUUID &image_id,									 
 									 FTType f_type = FTT_DEFAULT,
 									 BOOL usemipmap = TRUE,
-- 
GitLab