2021-07-09 18:45:54 -05:00
|
|
|
/**
|
|
|
|
|
* Copyright (c) 2006-2016, openmetaverse.co
|
2022-01-02 09:04:28 -06:00
|
|
|
* Copyright (c) 2021-2022, Sjofn LLC.
|
2021-07-09 18:45:54 -05:00
|
|
|
* All rights reserved.
|
|
|
|
|
*
|
|
|
|
|
* - Redistribution and use in source and binary forms, with or without
|
|
|
|
|
* modification, are permitted provided that the following conditions are met:
|
|
|
|
|
*
|
|
|
|
|
* - Redistributions of source code must retain the above copyright notice, this
|
|
|
|
|
* list of conditions and the following disclaimer.
|
|
|
|
|
* - Neither the name of the openmetaverse.co nor the names
|
|
|
|
|
* of its contributors may be used to endorse or promote products derived from
|
|
|
|
|
* this software without specific prior written permission.
|
|
|
|
|
*
|
|
|
|
|
* 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.
|
|
|
|
|
*/
|
|
|
|
|
|
2008-04-30 22:30:41 +00:00
|
|
|
using System;
|
|
|
|
|
using System.Threading;
|
2017-09-04 09:22:53 -05:00
|
|
|
using System.Drawing;
|
|
|
|
|
using System.Drawing.Drawing2D;
|
2008-07-21 21:12:59 +00:00
|
|
|
using OpenMetaverse.Imaging;
|
2008-04-30 22:30:41 +00:00
|
|
|
|
2008-07-21 21:12:59 +00:00
|
|
|
namespace OpenMetaverse.TestClient
|
2008-04-30 22:30:41 +00:00
|
|
|
{
|
|
|
|
|
public class UploadImageCommand : Command
|
|
|
|
|
{
|
|
|
|
|
AutoResetEvent UploadCompleteEvent = new AutoResetEvent(false);
|
2008-07-25 05:15:05 +00:00
|
|
|
UUID TextureID = UUID.Zero;
|
2008-05-08 16:58:09 +00:00
|
|
|
DateTime start;
|
2008-04-30 22:30:41 +00:00
|
|
|
|
|
|
|
|
public UploadImageCommand(TestClient testClient)
|
|
|
|
|
{
|
|
|
|
|
Name = "uploadimage";
|
|
|
|
|
Description = "Upload an image to your inventory. Usage: uploadimage [inventoryname] [timeout] [filename]";
|
2008-07-25 08:55:36 +00:00
|
|
|
Category = CommandCategory.Inventory;
|
2008-04-30 22:30:41 +00:00
|
|
|
}
|
|
|
|
|
|
2008-07-25 05:15:05 +00:00
|
|
|
public override string Execute(string[] args, UUID fromAgentID)
|
2008-04-30 22:30:41 +00:00
|
|
|
{
|
|
|
|
|
uint timeout;
|
|
|
|
|
|
|
|
|
|
if (args.Length != 3)
|
|
|
|
|
return "Usage: uploadimage [inventoryname] [timeout] [filename]";
|
|
|
|
|
|
2008-07-25 05:15:05 +00:00
|
|
|
TextureID = UUID.Zero;
|
2024-06-30 18:10:23 -05:00
|
|
|
var inventoryName = args[0];
|
|
|
|
|
var fileName = args[2];
|
2022-02-25 19:38:11 -06:00
|
|
|
if (!uint.TryParse(args[1], out timeout))
|
2008-04-30 22:30:41 +00:00
|
|
|
return "Usage: uploadimage [inventoryname] [timeout] [filename]";
|
|
|
|
|
|
|
|
|
|
Console.WriteLine("Loading image " + fileName);
|
|
|
|
|
byte[] jpeg2k = LoadImage(fileName);
|
|
|
|
|
if (jpeg2k == null)
|
|
|
|
|
return "Failed to compress image to JPEG2000";
|
|
|
|
|
Console.WriteLine("Finished compressing image to JPEG2000, uploading...");
|
2008-05-08 16:58:09 +00:00
|
|
|
start = DateTime.Now;
|
2008-04-30 22:30:41 +00:00
|
|
|
DoUpload(jpeg2k, inventoryName);
|
|
|
|
|
|
|
|
|
|
if (UploadCompleteEvent.WaitOne((int)timeout, false))
|
|
|
|
|
{
|
2022-02-25 19:38:11 -06:00
|
|
|
return $"Texture upload {((TextureID != UUID.Zero) ? "succeeded" : "failed")}: {TextureID}";
|
2008-04-30 22:30:41 +00:00
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
return "Texture upload timed out";
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void DoUpload(byte[] UploadData, string FileName)
|
|
|
|
|
{
|
|
|
|
|
if (UploadData != null)
|
|
|
|
|
{
|
|
|
|
|
string name = System.IO.Path.GetFileNameWithoutExtension(FileName);
|
|
|
|
|
|
2008-05-05 23:41:41 +00:00
|
|
|
Client.Inventory.RequestCreateItemFromAsset(UploadData, name, "Uploaded with TestClient",
|
2008-04-30 22:30:41 +00:00
|
|
|
AssetType.Texture, InventoryType.Texture, Client.Inventory.FindFolderForType(AssetType.Texture),
|
2008-07-25 05:15:05 +00:00
|
|
|
delegate(bool success, string status, UUID itemID, UUID assetID)
|
2008-04-30 22:30:41 +00:00
|
|
|
{
|
2019-06-08 17:58:54 -05:00
|
|
|
Console.WriteLine(
|
|
|
|
|
"RequestCreateItemFromAsset() returned: Success={0}, Status={1}, ItemID={2}, AssetID={3}",
|
|
|
|
|
success, status, itemID, assetID);
|
2008-04-30 22:30:41 +00:00
|
|
|
|
|
|
|
|
TextureID = assetID;
|
2019-06-08 17:58:54 -05:00
|
|
|
Console.WriteLine("Upload took {0}", DateTime.Now.Subtract(start));
|
2008-04-30 22:30:41 +00:00
|
|
|
UploadCompleteEvent.Set();
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private byte[] LoadImage(string fileName)
|
|
|
|
|
{
|
2021-07-04 18:01:05 -05:00
|
|
|
byte[] UploadData = { };
|
2008-04-30 22:30:41 +00:00
|
|
|
string lowfilename = fileName.ToLower();
|
|
|
|
|
Bitmap bitmap = null;
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
if (lowfilename.EndsWith(".jp2") || lowfilename.EndsWith(".j2c"))
|
|
|
|
|
{
|
|
|
|
|
// Upload JPEG2000 images untouched
|
|
|
|
|
UploadData = System.IO.File.ReadAllBytes(fileName);
|
2021-07-04 18:01:05 -05:00
|
|
|
|
2021-07-09 18:45:54 -05:00
|
|
|
using (var reader = new OpenJpegDotNet.IO.Reader(UploadData))
|
2021-07-04 18:01:05 -05:00
|
|
|
{
|
|
|
|
|
reader.ReadHeader();
|
|
|
|
|
bitmap = reader.DecodeToBitmap();
|
|
|
|
|
}
|
2008-04-30 22:30:41 +00:00
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2021-07-04 18:01:05 -05:00
|
|
|
if (lowfilename.EndsWith(".tga")) {
|
2008-07-18 10:29:16 +00:00
|
|
|
bitmap = LoadTGAClass.LoadTGA(fileName);
|
2021-07-04 18:01:05 -05:00
|
|
|
} else {
|
2017-09-03 20:54:34 -05:00
|
|
|
bitmap = (Bitmap)Image.FromFile(fileName);
|
2021-07-04 18:01:05 -05:00
|
|
|
}
|
2008-04-30 22:30:41 +00:00
|
|
|
int oldwidth = bitmap.Width;
|
|
|
|
|
int oldheight = bitmap.Height;
|
|
|
|
|
|
|
|
|
|
if (!IsPowerOfTwo((uint)oldwidth) || !IsPowerOfTwo((uint)oldheight))
|
|
|
|
|
{
|
|
|
|
|
Bitmap resized = new Bitmap(256, 256, bitmap.PixelFormat);
|
|
|
|
|
Graphics graphics = Graphics.FromImage(resized);
|
|
|
|
|
|
2017-09-03 20:54:34 -05:00
|
|
|
graphics.SmoothingMode = SmoothingMode.HighQuality;
|
|
|
|
|
graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
|
2008-04-30 22:30:41 +00:00
|
|
|
graphics.DrawImage(bitmap, 0, 0, 256, 256);
|
|
|
|
|
|
|
|
|
|
bitmap.Dispose();
|
|
|
|
|
bitmap = resized;
|
|
|
|
|
|
|
|
|
|
oldwidth = 256;
|
|
|
|
|
oldheight = 256;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Handle resizing to prevent excessively large images
|
|
|
|
|
if (oldwidth > 1024 || oldheight > 1024)
|
|
|
|
|
{
|
|
|
|
|
int newwidth = (oldwidth > 1024) ? 1024 : oldwidth;
|
|
|
|
|
int newheight = (oldheight > 1024) ? 1024 : oldheight;
|
|
|
|
|
|
|
|
|
|
Bitmap resized = new Bitmap(newwidth, newheight, bitmap.PixelFormat);
|
|
|
|
|
Graphics graphics = Graphics.FromImage(resized);
|
|
|
|
|
|
2017-09-03 20:54:34 -05:00
|
|
|
graphics.SmoothingMode = SmoothingMode.HighQuality;
|
2008-04-30 22:30:41 +00:00
|
|
|
graphics.InterpolationMode =
|
2017-09-03 20:54:34 -05:00
|
|
|
InterpolationMode.HighQualityBicubic;
|
2008-04-30 22:30:41 +00:00
|
|
|
graphics.DrawImage(bitmap, 0, 0, newwidth, newheight);
|
|
|
|
|
|
|
|
|
|
bitmap.Dispose();
|
|
|
|
|
bitmap = resized;
|
|
|
|
|
}
|
2021-07-09 18:45:54 -05:00
|
|
|
using (var writer = new OpenJpegDotNet.IO.Writer(bitmap))
|
2021-07-04 18:01:05 -05:00
|
|
|
{
|
|
|
|
|
UploadData = writer.Encode();
|
|
|
|
|
}
|
2008-04-30 22:30:41 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
2022-02-25 19:38:11 -06:00
|
|
|
Console.WriteLine(ex + " SL Image Upload ");
|
2008-04-30 22:30:41 +00:00
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
return UploadData;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static bool IsPowerOfTwo(uint n)
|
|
|
|
|
{
|
|
|
|
|
return (n & (n - 1)) == 0 && n != 0;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|