Skip to content

Commit

Permalink
Improve image loading (#729)
Browse files Browse the repository at this point in the history
* Improvement to the background image loading: it loads them from the cache now and it's at least 2x faster.
* If the dimensions are exceeded, then the user will also see "image too large to load", as previously it was only for images with too large of a file size
  • Loading branch information
eeropomell authored Jul 6, 2024
1 parent 0cd60ff commit 09c7015
Show file tree
Hide file tree
Showing 4 changed files with 106 additions and 4 deletions.
9 changes: 8 additions & 1 deletion Assets/Scripts/GUI/LoadBackgroundImageButton.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,14 @@ override protected void OnButtonPressed()
{
return;
}
SceneSettings.m_Instance.LoadCustomSkybox(ReferenceImage.FileName);
if (ReferenceImage.NotLoaded)
{
// Load-on-demand.
ReferenceImage.SynchronousLoad();
}


SceneSettings.m_Instance.LoadCustomSkyboxFromCache(ReferenceImage.FilePath);
}

override public void ResetState()
Expand Down
53 changes: 51 additions & 2 deletions Assets/Scripts/ReferenceImage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,14 @@ public bool RequestLoad(bool allowMainThread = false)
// TODO Move into the async code path?
var importer = new RuntimeSVGImporter();
var tex = importer.ImportAsTexture(FilePath);

if (!ValidateDimensions(tex.width, tex.height, App.PlatformConfig.ReferenceImagesMaxDimension))
{
m_State = ImageState.ErrorImageTooLarge;
Object.Destroy(tex);
return true;
}

ImageCache.SaveImageCache(tex, FilePath);
m_ImageAspect = (float)tex.width / tex.height;
int resizeLimit = App.PlatformConfig.ReferenceImagesResizeDimension;
Expand All @@ -370,6 +378,14 @@ public bool RequestLoad(bool allowMainThread = false)
RadianceHDRTexture hdr = new RadianceHDRTexture(fileData);
Texture2D tex = new Texture2D(2, 2, TextureFormat.RGB24, false);
tex = hdr.texture;

if (!ValidateDimensions(tex.width, tex.height, App.PlatformConfig.ReferenceImagesMaxDimension))
{
m_State = ImageState.ErrorImageTooLarge;
Object.Destroy(tex);
return true;
}

ImageCache.SaveImageCache(tex, FilePath);
m_ImageAspect = (float)tex.width / tex.height;
int resizeLimit = App.PlatformConfig.ReferenceImagesResizeDimension;
Expand Down Expand Up @@ -495,6 +511,14 @@ IEnumerator<Timeslice> RequestLoadCoroutineMainThread()
// from WWW.LoadImageIntoTexture
Texture2D inTex = new Texture2D(2, 2, TextureFormat.RGBA32, true);
loader.LoadImageIntoTexture(inTex);

if (!ValidateDimensions(inTex.width, inTex.height, App.PlatformConfig.ReferenceImagesMaxDimension))
{
m_State = ImageState.ErrorImageTooLarge;
Object.Destroy(inTex);
yield break;
}

DownsizeTexture(inTex, ref m_Icon, ReferenceImageCatalog.MAX_ICON_TEX_DIMENSION);
m_Icon.wrapMode = TextureWrapMode.Clamp;
m_ImageAspect = (float)inTex.width / inTex.height;
Expand Down Expand Up @@ -548,6 +572,18 @@ IEnumerable<Timeslice> RequestLoadCoroutine()
if (imageLoad != null)
{
ControllerConsoleScript.m_Instance.AddNewLine(imageLoad.Message, true);
if (imageLoad.imageLoadErrorCode == ImageLoadError.ImageLoadErrorCode.ImageTooLargeError)
{
m_State = ImageState.ErrorImageTooLarge;
}
else
{
m_State = ImageState.Error;
}

reader = null;
yield break;

}
}

Expand Down Expand Up @@ -578,8 +614,6 @@ IEnumerable<Timeslice> RequestLoadCoroutine()
else
{
// Problem reading the file?
// images with state ImageState.Error display a generic error message 'Image failed to load' when hovering over them in the reference panel
// TODO: use more specific error messages (e.g "Too large dimensions", "Unknown format") that are set in ImageUtils.cs? (that is called by ThreadedImageReader.cs)
m_State = ImageState.Error;
reader = null;
yield break;
Expand All @@ -595,6 +629,21 @@ private bool ValidateFileSize()
return info.Length <= App.PlatformConfig.ReferenceImagesMaxFileSize;
}

private bool ValidateDimensions(int imageWidth, int imageHeight, int maxDimension)
{

// Cast to long as maxDimension is big enough on desktop to overflow
if (imageWidth * imageHeight > ((long)maxDimension * (long)maxDimension))
{
return false;
}
else
{
return true;
}

}

public string GetExportName()
{
return Path.GetFileNameWithoutExtension(FileName);
Expand Down
27 changes: 27 additions & 0 deletions Assets/Scripts/SceneSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,33 @@ public void LoadCustomSkybox(string filename)
}
}

// note: this takes in the full file path, instead of just the name
public void LoadCustomSkyboxFromCache(string filepath)
{
Texture2D tex = ImageCache.LoadImageCache(filepath);

if (tex == null)
{
LoadCustomSkybox(Path.GetFileName(filepath));
}
else
{
float aspectRatio = tex.width / tex.height;
if (aspectRatio > 1.5)
{
m_CustomSkyboxMaterial = Resources.Load<Material>("Environments/CustomSkybox");
}
else
{
m_CustomSkyboxMaterial = Resources.Load<Material>("Environments/CustomStereoSkybox");
}
m_CustomSkyboxMaterial.mainTexture = tex;
m_CustomSkyboxMaterial.SetColor("_Tint", Color.gray);
RenderSettings.skybox = m_CustomSkyboxMaterial;
RenderSettings.ambientMode = AmbientMode.Skybox;
}
}

public Quaternion GradientOrientation
{
get { return m_GradientSkew; }
Expand Down
21 changes: 20 additions & 1 deletion Assets/Scripts/Util/ImageUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,22 @@

namespace TiltBrush
{



public class ImageLoadError : Exception
{

public enum ImageLoadErrorCode
{
// This is the default one; errors like unknown format, corruption, etc.
ImageGenericError,
// too large dimensions
ImageTooLargeError
}

public ImageLoadErrorCode imageLoadErrorCode = ImageLoadErrorCode.ImageGenericError;

public ImageLoadError(string message) : base(message) { }
public ImageLoadError(string fmt, params System.Object[] args)
: base(string.Format(fmt, args))
Expand All @@ -31,6 +45,11 @@ public ImageLoadError(Exception inner, string fmt, params System.Object[] args)
: base(string.Format(fmt, args), inner)
{
}

public ImageLoadError(string message, ImageLoadErrorCode imageLoadErrorCode) : base(message)
{
this.imageLoadErrorCode = imageLoadErrorCode;
}
}

static public class ImageUtils
Expand Down Expand Up @@ -92,7 +111,7 @@ static public void ValidateDimensions(byte[] data, int maxDimension)
{
throw new ImageLoadError(
String.Format("Image dimensions {0}x{1} are greater than max dimensions of {2}x{2}!",
imageWidth, imageHeight, maxDimension));
imageWidth, imageHeight, maxDimension), ImageLoadError.ImageLoadErrorCode.ImageTooLargeError);
}
}

Expand Down

0 comments on commit 09c7015

Please sign in to comment.