using System.Threading.Tasks; using UnityEngine; public static partial class AsyncImageLoader { /// Load image synchronously. This variant uses the default LoaderSettings. /// True if the data can be loaded, false otherwise. public static bool LoadImage(Texture2D texture, byte[] data) { return LoadImage(texture, data, LoaderSettings.Default); } /// Load image synchronously. This variant is similar to ImageConversion.LoadImage. /// True if the data can be loaded, false otherwise. public static bool LoadImage(Texture2D texture, byte[] data, bool markNonReadable) { var loaderSettings = LoaderSettings.Default; loaderSettings.markNonReadable = markNonReadable; return LoadImage(texture, data, loaderSettings); } /// Load image synchronously. This variant accepts a LoaderSettings. Useful for debugging and profiling. /// True if the data can be loaded, false otherwise. public static bool LoadImage(Texture2D texture, byte[] data, LoaderSettings loaderSettings) { try { if (data == null || data.Length == 0) throw new System.Exception("Input data is null or empty."); using (var importer = new ImageImporter(data, loaderSettings)) { importer.LoadIntoTexture(texture); } return true; } catch (System.Exception e) { if (loaderSettings.logException) Debug.LogException(e); return false; } } // load the bitmap inside texture2d into a byte array of desired encoding public static object SaveImage(Texture2D texture, ref byte[] jp2, LoaderSettings loaderSettings) { try { if (texture == null || !texture.isReadable) throw new System.Exception("Input data is null or empty."); using (var exporter = new ImageExporter(texture, loaderSettings)) { exporter.ExportFromTexture(ref jp2); } return true; } catch (System.Exception e) { if (loaderSettings.logException) Debug.LogException(e); return false; } } // load the color32-bitmap into a byte array of desired encoding public static object SaveImage(Color32[] texture, ref byte[] jp2, LoaderSettings loaderSettings) { try { if (texture == null || texture.Length == 0) throw new System.Exception("Input data is null or empty."); todo: // using (var exporter = new ImageExporter(texture, loaderSettings)) { // exporter.ExportFromTexture(ref jp2); // } return true; } catch (System.Exception e) { if (loaderSettings.logException) Debug.LogException(e); return false; } } /// Load image asynchronously. This variant uses the default LoaderSettings. /// True if the data can be loaded, false otherwise. public static Task LoadImageAsync(Texture2D texture, byte[] data) { return LoadImageAsync(texture, data, LoaderSettings.Default); } /// Load image asynchronously. This variant is similar to ImageConversion.LoadImage. /// True if the data can be loaded, false otherwise. public static Task LoadImageAsync(Texture2D texture, byte[] data, bool markNonReadable) { var loaderSettings = LoaderSettings.Default; loaderSettings.markNonReadable = markNonReadable; return LoadImageAsync(texture, data, loaderSettings); } /// Load image asynchronously. This variant accepts a LoaderSettings. /// True if the data can be loaded, false otherwise. public static async Task LoadImageAsync(Texture2D texture, byte[] data, LoaderSettings loaderSettings) { try { if (data == null || data.Length == 0) throw new System.Exception("Input data is null or empty."); using (var importer = await Task.Run(() => new ImageImporter(data, loaderSettings))) { await importer.LoadIntoTextureAsync(texture); } return true; } catch (System.Exception e) { if (loaderSettings.logException) Debug.LogException(e); return false; } } /// Create Texture2D from image data synchronously. This variant uses a default LoaderSettings. Linear and mipmap count can be specified. /// Texture2D object if the data can be loaded, null otherwise. public static Texture2D CreateFromImage(byte[] data) { return CreateFromImage(data, LoaderSettings.Default); } /// Create Texture2D from image data synchronously. This variant accepts a LoaderSettings. Linear and mipmap count can be specified. /// Texture2D object if the data can be loaded, null otherwise. public static Texture2D CreateFromImage(byte[] data, LoaderSettings loaderSettings) { try { if (data == null || data.Length == 0) throw new System.Exception("Input data is null or empty."); using (var importer = new ImageImporter(data, loaderSettings)) { return importer.CreateNewTexture(); } } catch (System.Exception e) { if (loaderSettings.logException) Debug.LogException(e); return null; } } /// Create Texture2D from image data asynchronously. This variant uses a default LoaderSettings. Linear and mipmap count can be specified. /// Texture2D object if the data can be loaded, null otherwise. public static Task CreateFromImageAsync(byte[] data) { return CreateFromImageAsync(data, LoaderSettings.Default); } /// Create Texture2D from image data asynchronously. This variant accepts a LoaderSettings. Linear and mipmap count can be specified. /// Texture2D object if the data can be loaded, null otherwise. public static async Task CreateFromImageAsync(byte[] data, LoaderSettings loaderSettings) { try { if (data == null || data.Length == 0) throw new System.Exception("Input data is null or empty."); using (var importer = await Task.Run(() => new ImageImporter(data, loaderSettings))) { return await importer.CreateNewTextureAsync(); } } catch (System.Exception e) { if (loaderSettings.logException) Debug.LogException(e); return null; } } /// Settings used by the image loader. public struct LoaderSettings { /// Create linear texture. Only applicable to methods that create new Texture2D. Defaults to false. public bool linear; /// Texture data won't be readable on the CPU after loading. Defaults to false. public bool markNonReadable; /// Whether or not to generate mipmaps. Defaults to true. public bool generateMipmap; /// Automatically calculate the number of mipmap levels. Defaults to true. Only applicable to methods that create new Texture2D. public bool autoMipmapCount; /// Mipmap count, including the base level. Must be greater than 1. Only applicable to methods that create new Texture2D. public int mipmapCount; /// Used to explicitly specify the image format. Defaults to FIF_UNKNOWN, which the image format will be automatically determined. public FreeImage.Format format; /// Whether or not to log exception caught by this method. Defaults to true. public bool logException; public static LoaderSettings Default => new LoaderSettings { linear = false, markNonReadable = false, generateMipmap = true, autoMipmapCount = true, format = FreeImage.Format.FIF_UNKNOWN, logException = true, }; } }