Skip to content

Commit

Permalink
Make sure that for images with too large dimensions the error message…
Browse files Browse the repository at this point in the history
… 'Image too large to load' will display
  • Loading branch information
eeropomell committed Jun 17, 2024
1 parent e3f7d15 commit 4902e5a
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 3 deletions.
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
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 4902e5a

Please sign in to comment.