2019-04-07 11:09:53 -07:00
|
|
|
|
/*
|
|
|
|
|
|
Copyright (c) 2014, Lars Brubaker
|
|
|
|
|
|
All rights reserved.
|
|
|
|
|
|
|
|
|
|
|
|
Redistribution and use in source and binary forms, with or without
|
|
|
|
|
|
modification, are permitted provided that the following conditions are met:
|
|
|
|
|
|
|
|
|
|
|
|
1. Redistributions of source code must retain the above copyright notice, this
|
|
|
|
|
|
list of conditions and the following disclaimer.
|
|
|
|
|
|
2. Redistributions in binary form must reproduce the above copyright notice,
|
|
|
|
|
|
this list of conditions and the following disclaimer in the documentation
|
|
|
|
|
|
and/or other materials provided with the distribution.
|
|
|
|
|
|
|
|
|
|
|
|
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
|
|
|
|
|
|
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
|
|
|
|
|
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
|
|
|
|
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
|
|
|
|
|
|
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
|
|
|
|
|
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
|
|
|
|
|
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
|
|
|
|
|
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
|
|
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
|
|
|
|
|
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
|
|
|
|
|
|
|
|
The views and conclusions contained in the software and documentation are those
|
|
|
|
|
|
of the authors and should not be interpreted as representing official policies,
|
|
|
|
|
|
either expressed or implied, of the FreeBSD Project.
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
using System;
|
2019-04-11 18:23:09 -07:00
|
|
|
|
using System.Collections.Generic;
|
2019-04-07 11:09:53 -07:00
|
|
|
|
using System.IO;
|
|
|
|
|
|
using System.Net;
|
2019-04-07 15:11:17 -07:00
|
|
|
|
using System.Net.Http;
|
2020-08-07 07:47:10 -07:00
|
|
|
|
using System.Threading;
|
2019-04-07 11:09:53 -07:00
|
|
|
|
using System.Threading.Tasks;
|
2019-04-11 18:23:09 -07:00
|
|
|
|
using MatterHackers.Agg;
|
|
|
|
|
|
using MatterHackers.Agg.Image;
|
|
|
|
|
|
using MatterHackers.Agg.Platform;
|
|
|
|
|
|
using MatterHackers.Agg.UI;
|
2019-04-07 11:09:53 -07:00
|
|
|
|
|
|
|
|
|
|
namespace MatterHackers.MatterControl
|
|
|
|
|
|
{
|
|
|
|
|
|
public static class WebCache
|
|
|
|
|
|
{
|
2019-04-11 18:23:09 -07:00
|
|
|
|
private static HashSet<string> savedImages = new HashSet<string>();
|
|
|
|
|
|
|
2020-10-03 17:57:53 -07:00
|
|
|
|
private static object locker = new object();
|
|
|
|
|
|
|
|
|
|
|
|
// Download an image from the web into the specified ImageBuffer
|
2021-11-26 10:16:59 -08:00
|
|
|
|
public static void RetrieveImageAsync(ImageBuffer imageToLoadInto, string uriToLoad, bool scaleToImageX)
|
2019-04-07 11:09:53 -07:00
|
|
|
|
{
|
|
|
|
|
|
var longHash = uriToLoad.GetLongHashCode();
|
2021-11-24 16:25:34 -08:00
|
|
|
|
if (scaleToImageX)
|
|
|
|
|
|
{
|
|
|
|
|
|
longHash = imageToLoadInto.Width.GetLongHashCode(longHash);
|
|
|
|
|
|
}
|
2019-05-24 21:29:32 -07:00
|
|
|
|
var imageFileName = ApplicationController.CacheablePath("Images", longHash.ToString() + ".png");
|
2019-04-07 11:09:53 -07:00
|
|
|
|
|
|
|
|
|
|
if (File.Exists(imageFileName))
|
|
|
|
|
|
{
|
|
|
|
|
|
try
|
|
|
|
|
|
{
|
2021-11-26 10:16:59 -08:00
|
|
|
|
LoadImageInto(imageToLoadInto, scaleToImageX, null, new StreamReader(imageFileName).BaseStream);
|
2019-04-07 11:09:53 -07:00
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
catch
|
|
|
|
|
|
{
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2019-04-11 18:23:09 -07:00
|
|
|
|
|
2019-04-07 11:09:53 -07:00
|
|
|
|
WebClient client = new WebClient();
|
|
|
|
|
|
client.DownloadDataCompleted += (sender, e) =>
|
|
|
|
|
|
{
|
|
|
|
|
|
try // if we get a bad result we can get a target invocation exception. In that case just don't show anything
|
|
|
|
|
|
{
|
|
|
|
|
|
Stream stream = new MemoryStream(e.Result);
|
|
|
|
|
|
|
2021-11-26 10:16:59 -08:00
|
|
|
|
LoadImageInto(imageToLoadInto, scaleToImageX, new BlenderPreMultBGRA(), stream);
|
2019-05-11 14:45:41 -07:00
|
|
|
|
|
2019-04-11 18:23:09 -07:00
|
|
|
|
if (imageToLoadInto.Width > 0
|
|
|
|
|
|
&& imageToLoadInto.Height > 0
|
|
|
|
|
|
&& !savedImages.Contains(imageFileName))
|
|
|
|
|
|
{
|
|
|
|
|
|
savedImages.Add(imageFileName);
|
2020-11-24 18:08:49 -08:00
|
|
|
|
ImageIO.SaveImageData(imageFileName, imageToLoadInto);
|
2019-04-11 18:23:09 -07:00
|
|
|
|
}
|
2019-04-07 11:09:53 -07:00
|
|
|
|
}
|
|
|
|
|
|
catch
|
|
|
|
|
|
{
|
|
|
|
|
|
}
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
|
{
|
|
|
|
|
|
client.DownloadDataAsync(new Uri(uriToLoad));
|
|
|
|
|
|
}
|
|
|
|
|
|
catch
|
|
|
|
|
|
{
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2020-10-03 17:57:53 -07:00
|
|
|
|
// Download an image from the web into the specified ImageSequence
|
2020-05-27 22:42:56 -07:00
|
|
|
|
public static void RetrieveImageSquenceAsync(ImageSequence imageSequenceToLoadInto,
|
|
|
|
|
|
string uriToLoad,
|
|
|
|
|
|
Action doneLoading = null)
|
2019-04-07 11:09:53 -07:00
|
|
|
|
{
|
|
|
|
|
|
var asyncImageSequence = new ImageSequence();
|
|
|
|
|
|
|
|
|
|
|
|
var longHash = uriToLoad.GetLongHashCode();
|
2019-05-24 21:29:32 -07:00
|
|
|
|
var pngFileName = ApplicationController.CacheablePath("Images", longHash.ToString() + ".png");
|
|
|
|
|
|
var gifFileName = ApplicationController.CacheablePath("Images", longHash.ToString() + ".gif");
|
2019-04-07 11:09:53 -07:00
|
|
|
|
|
|
|
|
|
|
if (File.Exists(pngFileName))
|
|
|
|
|
|
{
|
|
|
|
|
|
try
|
|
|
|
|
|
{
|
|
|
|
|
|
Task.Run(() =>
|
|
|
|
|
|
{
|
2020-10-03 17:57:53 -07:00
|
|
|
|
lock (locker)
|
|
|
|
|
|
{
|
2020-11-25 07:39:36 -08:00
|
|
|
|
StaticData.Instance.LoadImageSequenceData(new StreamReader(pngFileName).BaseStream, asyncImageSequence);
|
2020-10-03 17:57:53 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
2019-04-07 11:09:53 -07:00
|
|
|
|
UiThread.RunOnIdle(() =>
|
|
|
|
|
|
{
|
|
|
|
|
|
imageSequenceToLoadInto.Copy(asyncImageSequence);
|
|
|
|
|
|
imageSequenceToLoadInto.Invalidate();
|
2020-05-27 22:42:56 -07:00
|
|
|
|
doneLoading?.Invoke();
|
2019-04-07 11:09:53 -07:00
|
|
|
|
});
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
catch
|
|
|
|
|
|
{
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
else if (File.Exists(gifFileName))
|
|
|
|
|
|
{
|
2020-06-03 12:37:16 -07:00
|
|
|
|
|
|
|
|
|
|
Task.Run(() =>
|
2019-04-07 11:09:53 -07:00
|
|
|
|
{
|
2020-06-03 12:37:16 -07:00
|
|
|
|
try
|
2019-04-07 11:09:53 -07:00
|
|
|
|
{
|
2020-10-03 17:57:53 -07:00
|
|
|
|
lock (locker)
|
|
|
|
|
|
{
|
2020-11-25 07:39:36 -08:00
|
|
|
|
StaticData.Instance.LoadImageSequenceData(new StreamReader(gifFileName).BaseStream, asyncImageSequence);
|
2020-10-03 17:57:53 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
2020-06-03 12:37:16 -07:00
|
|
|
|
if (asyncImageSequence.NumFrames > 0)
|
2019-04-07 11:09:53 -07:00
|
|
|
|
{
|
2020-06-03 12:37:16 -07:00
|
|
|
|
UiThread.RunOnIdle(() =>
|
|
|
|
|
|
{
|
|
|
|
|
|
imageSequenceToLoadInto.Copy(asyncImageSequence);
|
|
|
|
|
|
imageSequenceToLoadInto.Invalidate();
|
|
|
|
|
|
doneLoading?.Invoke();
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
DownloadImageAsync(imageSequenceToLoadInto, uriToLoad, doneLoading, asyncImageSequence, pngFileName, gifFileName);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
catch
|
|
|
|
|
|
{
|
|
|
|
|
|
DownloadImageAsync(imageSequenceToLoadInto, uriToLoad, doneLoading, asyncImageSequence, pngFileName, gifFileName);
|
|
|
|
|
|
}
|
|
|
|
|
|
});
|
2019-04-07 11:09:53 -07:00
|
|
|
|
|
2020-06-03 12:37:16 -07:00
|
|
|
|
return;
|
2019-04-07 11:09:53 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
2020-06-03 12:37:16 -07:00
|
|
|
|
DownloadImageAsync(imageSequenceToLoadInto, uriToLoad, doneLoading, asyncImageSequence, pngFileName, gifFileName);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private static void DownloadImageAsync(ImageSequence imageSequenceToLoadInto, string uriToLoad, Action doneLoading, ImageSequence asyncImageSequence, string pngFileName, string gifFileName)
|
|
|
|
|
|
{
|
2019-04-07 11:09:53 -07:00
|
|
|
|
WebClient client = new WebClient();
|
|
|
|
|
|
client.DownloadDataCompleted += (object sender, DownloadDataCompletedEventArgs e) =>
|
|
|
|
|
|
{
|
|
|
|
|
|
try // if we get a bad result we can get a target invocation exception. In that case just don't show anything
|
|
|
|
|
|
{
|
|
|
|
|
|
Task.Run(() =>
|
|
|
|
|
|
{
|
|
|
|
|
|
// scale the loaded image to the size of the target image
|
|
|
|
|
|
byte[] raw = e.Result;
|
|
|
|
|
|
Stream stream = new MemoryStream(raw);
|
|
|
|
|
|
|
2020-10-03 17:57:53 -07:00
|
|
|
|
lock (locker)
|
|
|
|
|
|
{
|
2020-11-25 07:39:36 -08:00
|
|
|
|
StaticData.Instance.LoadImageSequenceData(stream, asyncImageSequence);
|
2020-10-03 17:57:53 -07:00
|
|
|
|
}
|
2019-04-07 11:09:53 -07:00
|
|
|
|
|
|
|
|
|
|
if (asyncImageSequence.Frames.Count == 1)
|
|
|
|
|
|
{
|
|
|
|
|
|
// save the as png
|
2020-10-03 17:57:53 -07:00
|
|
|
|
lock (locker)
|
|
|
|
|
|
{
|
2021-06-12 07:59:52 -07:00
|
|
|
|
if (!File.Exists(pngFileName))
|
|
|
|
|
|
{
|
|
|
|
|
|
ImageIO.SaveImageData(pngFileName, asyncImageSequence.Frames[0]);
|
|
|
|
|
|
}
|
2020-10-03 17:57:53 -07:00
|
|
|
|
}
|
2019-04-07 11:09:53 -07:00
|
|
|
|
}
|
|
|
|
|
|
else // save original stream as gif
|
|
|
|
|
|
{
|
|
|
|
|
|
using (var writter = new FileStream(gifFileName, FileMode.Create))
|
|
|
|
|
|
{
|
|
|
|
|
|
stream.Position = 0;
|
|
|
|
|
|
stream.CopyTo(writter);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
UiThread.RunOnIdle(() =>
|
|
|
|
|
|
{
|
|
|
|
|
|
imageSequenceToLoadInto.Copy(asyncImageSequence);
|
|
|
|
|
|
imageSequenceToLoadInto.Invalidate();
|
2020-05-27 22:42:56 -07:00
|
|
|
|
doneLoading?.Invoke();
|
2019-04-07 11:09:53 -07:00
|
|
|
|
});
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
catch
|
|
|
|
|
|
{
|
|
|
|
|
|
}
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
|
{
|
|
|
|
|
|
client.DownloadDataAsync(new Uri(uriToLoad));
|
|
|
|
|
|
}
|
|
|
|
|
|
catch
|
|
|
|
|
|
{
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2020-08-07 07:47:10 -07:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Return the first result that can be found (usually the cache). Wait up to 5 seconds to populate the cache
|
|
|
|
|
|
/// if it does not exist.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="uriToLoad"></param>
|
|
|
|
|
|
/// <param name="addToAppCache"></param>
|
|
|
|
|
|
/// <param name="addHeaders"></param>
|
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
|
public static string GetCachedText(string uriToLoad,
|
|
|
|
|
|
bool addToAppCache = true,
|
|
|
|
|
|
Action<HttpRequestMessage> addHeaders = null)
|
|
|
|
|
|
{
|
|
|
|
|
|
string results = null;
|
|
|
|
|
|
WebCache.RetrieveText(uriToLoad,
|
|
|
|
|
|
(content) =>
|
|
|
|
|
|
{
|
|
|
|
|
|
results = content;
|
|
|
|
|
|
},
|
|
|
|
|
|
false,
|
|
|
|
|
|
addHeaders);
|
|
|
|
|
|
|
|
|
|
|
|
var startTime = UiThread.CurrentTimerMs;
|
|
|
|
|
|
// wait up to 5 seconds for a response
|
|
|
|
|
|
while (results == null
|
|
|
|
|
|
&& UiThread.CurrentTimerMs < startTime + 5000)
|
|
|
|
|
|
{
|
|
|
|
|
|
Thread.Sleep(10);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return results;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Retrieve text from a url async, but first return any existing cache of the text synchronously
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="uriToLoad">The web path to find the text, will also be used as the cache key</param>
|
|
|
|
|
|
/// <param name="updateResult">A function to call when the text is received if it is different than the cache.</param>
|
|
|
|
|
|
/// <param name="addToAppCache">Add the results to a directory that can be copied into the main distribution,
|
|
|
|
|
|
/// or add them to a directory that is only for the local machine.</param>
|
|
|
|
|
|
public static void RetrieveText(string uriToLoad,
|
|
|
|
|
|
Action<string> updateResult,
|
|
|
|
|
|
bool addToAppCache = true,
|
|
|
|
|
|
Action<HttpRequestMessage> addHeaders = null)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (addToAppCache)
|
|
|
|
|
|
{
|
|
|
|
|
|
RetrieveText(uriToLoad, "TextWebCache", updateResult, addHeaders);
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
RetrieveText(uriToLoad, "Text", updateResult, addHeaders);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private static void RetrieveText(string uriToLoad,
|
|
|
|
|
|
string cacheFolder,
|
|
|
|
|
|
Action<string> updateResult,
|
|
|
|
|
|
Action<HttpRequestMessage> addHeaders = null)
|
2019-04-07 22:04:16 -07:00
|
|
|
|
{
|
|
|
|
|
|
var longHash = uriToLoad.GetLongHashCode();
|
|
|
|
|
|
|
2020-08-07 07:47:10 -07:00
|
|
|
|
var appDataFileName = ApplicationController.CacheablePath(cacheFolder, longHash.ToString() + ".txt");
|
2020-05-21 16:29:45 -07:00
|
|
|
|
|
2019-04-07 22:04:16 -07:00
|
|
|
|
string fileText = null;
|
2020-06-03 12:37:16 -07:00
|
|
|
|
// first try the cache in the users applications folder
|
2020-07-13 21:10:12 -07:00
|
|
|
|
if (File.Exists(appDataFileName))
|
2019-04-07 22:04:16 -07:00
|
|
|
|
{
|
|
|
|
|
|
try
|
|
|
|
|
|
{
|
2020-10-03 17:57:53 -07:00
|
|
|
|
lock (locker)
|
|
|
|
|
|
{
|
|
|
|
|
|
fileText = File.ReadAllText(appDataFileName);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2023-10-20 17:48:00 -07:00
|
|
|
|
if (!fileText.Contains("Bad credentials"))
|
|
|
|
|
|
{
|
|
|
|
|
|
updateResult?.Invoke(fileText);
|
|
|
|
|
|
}
|
2019-04-07 22:04:16 -07:00
|
|
|
|
}
|
|
|
|
|
|
catch
|
|
|
|
|
|
{
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2020-06-03 12:37:16 -07:00
|
|
|
|
else // We could not find it in the application cache. Check if it is in static data.
|
2020-05-19 10:29:17 -07:00
|
|
|
|
{
|
2020-08-07 07:47:10 -07:00
|
|
|
|
var staticDataPath = Path.Combine(cacheFolder, longHash.ToString() + ".txt");
|
2020-07-13 21:10:12 -07:00
|
|
|
|
|
2020-11-25 07:39:36 -08:00
|
|
|
|
if (StaticData.Instance.FileExists(staticDataPath))
|
2020-05-21 16:29:45 -07:00
|
|
|
|
{
|
|
|
|
|
|
try
|
|
|
|
|
|
{
|
2020-10-03 17:57:53 -07:00
|
|
|
|
lock (locker)
|
|
|
|
|
|
{
|
2020-11-25 07:39:36 -08:00
|
|
|
|
fileText = StaticData.Instance.ReadAllText(staticDataPath);
|
2020-10-03 17:57:53 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
2020-05-21 16:29:45 -07:00
|
|
|
|
updateResult?.Invoke(fileText);
|
|
|
|
|
|
}
|
|
|
|
|
|
catch
|
|
|
|
|
|
{
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2020-05-19 10:29:17 -07:00
|
|
|
|
}
|
2019-04-07 22:04:16 -07:00
|
|
|
|
|
2020-06-03 12:37:16 -07:00
|
|
|
|
// whether we find it or not check the web for the latest version
|
2019-04-07 22:04:16 -07:00
|
|
|
|
Task.Run(async () =>
|
|
|
|
|
|
{
|
2020-08-07 07:47:10 -07:00
|
|
|
|
var requestMessage = new HttpRequestMessage(HttpMethod.Get, uriToLoad);
|
|
|
|
|
|
addHeaders?.Invoke(requestMessage);
|
|
|
|
|
|
using (var client = new HttpClient())
|
2019-04-07 22:04:16 -07:00
|
|
|
|
{
|
2020-08-07 07:47:10 -07:00
|
|
|
|
using (HttpResponseMessage response = await client.SendAsync(requestMessage))
|
|
|
|
|
|
{
|
|
|
|
|
|
var text = await response.Content.ReadAsStringAsync();
|
|
|
|
|
|
if (!string.IsNullOrEmpty(text)
|
|
|
|
|
|
&& text != fileText)
|
|
|
|
|
|
{
|
|
|
|
|
|
File.WriteAllText(appDataFileName, text);
|
|
|
|
|
|
updateResult?.Invoke(text);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2019-04-07 22:04:16 -07:00
|
|
|
|
}
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2019-04-07 11:09:53 -07:00
|
|
|
|
private static void LoadImageInto(ImageBuffer imageToLoadInto, bool scaleToImageX, IRecieveBlenderByte scalingBlender, Stream stream)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (scalingBlender == null)
|
|
|
|
|
|
{
|
|
|
|
|
|
scalingBlender = new BlenderBGRA();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
ImageBuffer unScaledImage = new ImageBuffer(10, 10);
|
|
|
|
|
|
if (scaleToImageX)
|
|
|
|
|
|
{
|
2020-10-03 17:57:53 -07:00
|
|
|
|
lock (locker)
|
|
|
|
|
|
{
|
|
|
|
|
|
// scale the loaded image to the size of the target image
|
2020-11-25 07:39:36 -08:00
|
|
|
|
StaticData.Instance.LoadImageData(stream, unScaledImage);
|
2020-10-03 17:57:53 -07:00
|
|
|
|
}
|
2019-04-07 11:09:53 -07:00
|
|
|
|
|
|
|
|
|
|
// If the source image (the one we downloaded) is more than twice as big as our dest image.
|
|
|
|
|
|
while (unScaledImage.Width > imageToLoadInto.Width * 2)
|
|
|
|
|
|
{
|
|
|
|
|
|
// The image sampler we use is a 2x2 filter so we need to scale by a max of 1/2 if we want to get good results.
|
|
|
|
|
|
// So we scale as many times as we need to get the Image to be the right size.
|
|
|
|
|
|
// If this were going to be a non-uniform scale we could do the x and y separately to get better results.
|
|
|
|
|
|
ImageBuffer halfImage = new ImageBuffer(unScaledImage.Width / 2, unScaledImage.Height / 2, 32, scalingBlender);
|
|
|
|
|
|
halfImage.NewGraphics2D().Render(unScaledImage, 0, 0, 0, halfImage.Width / (double)unScaledImage.Width, halfImage.Height / (double)unScaledImage.Height);
|
|
|
|
|
|
unScaledImage = halfImage;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
double finalScale = imageToLoadInto.Width / (double)unScaledImage.Width;
|
2019-04-11 18:23:09 -07:00
|
|
|
|
imageToLoadInto.Allocate(imageToLoadInto.Width, (int)(unScaledImage.Height * finalScale), imageToLoadInto.Width * (imageToLoadInto.BitDepth / 8), imageToLoadInto.BitDepth);
|
|
|
|
|
|
imageToLoadInto.NewGraphics2D().Render(unScaledImage, 0, 0, 0, finalScale, finalScale);
|
2019-04-07 11:09:53 -07:00
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
2020-11-25 07:39:36 -08:00
|
|
|
|
StaticData.Instance.LoadImageData(stream, imageToLoadInto);
|
2019-04-07 11:09:53 -07:00
|
|
|
|
}
|
2021-11-24 17:02:26 -08:00
|
|
|
|
|
|
|
|
|
|
imageToLoadInto.MarkImageChanged();
|
2019-04-07 11:09:53 -07:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|