corrade-vassal – Blame information for rev 1

Subversion Repositories:
Rev:
Rev Author Line No. Line
1 vero 1 using System;
2 using System.Collections.Generic;
3 using System.IO;
4 using System.Threading;
5 using System.Drawing;
6 using OpenMetaverse;
7 using OpenMetaverse.Http;
8 using OpenMetaverse.Imaging;
9  
10 namespace OpenMetaverse.TestClient
11 {
12 public class UploadImageCommand : Command
13 {
14 AutoResetEvent UploadCompleteEvent = new AutoResetEvent(false);
15 UUID TextureID = UUID.Zero;
16 DateTime start;
17  
18 public UploadImageCommand(TestClient testClient)
19 {
20 Name = "uploadimage";
21 Description = "Upload an image to your inventory. Usage: uploadimage [inventoryname] [timeout] [filename]";
22 Category = CommandCategory.Inventory;
23 }
24  
25 public override string Execute(string[] args, UUID fromAgentID)
26 {
27 string inventoryName;
28 uint timeout;
29 string fileName;
30  
31 if (args.Length != 3)
32 return "Usage: uploadimage [inventoryname] [timeout] [filename]";
33  
34 TextureID = UUID.Zero;
35 inventoryName = args[0];
36 fileName = args[2];
37 if (!UInt32.TryParse(args[1], out timeout))
38 return "Usage: uploadimage [inventoryname] [timeout] [filename]";
39  
40 Console.WriteLine("Loading image " + fileName);
41 byte[] jpeg2k = LoadImage(fileName);
42 if (jpeg2k == null)
43 return "Failed to compress image to JPEG2000";
44 Console.WriteLine("Finished compressing image to JPEG2000, uploading...");
45 start = DateTime.Now;
46 DoUpload(jpeg2k, inventoryName);
47  
48 if (UploadCompleteEvent.WaitOne((int)timeout, false))
49 {
50 return String.Format("Texture upload {0}: {1}", (TextureID != UUID.Zero) ? "succeeded" : "failed",
51 TextureID);
52 }
53 else
54 {
55 return "Texture upload timed out";
56 }
57 }
58  
59 private void DoUpload(byte[] UploadData, string FileName)
60 {
61 if (UploadData != null)
62 {
63 string name = System.IO.Path.GetFileNameWithoutExtension(FileName);
64  
65 Client.Inventory.RequestCreateItemFromAsset(UploadData, name, "Uploaded with TestClient",
66 AssetType.Texture, InventoryType.Texture, Client.Inventory.FindFolderForType(AssetType.Texture),
67 delegate(bool success, string status, UUID itemID, UUID assetID)
68 {
69 Console.WriteLine(String.Format(
70 "RequestCreateItemFromAsset() returned: Success={0}, Status={1}, ItemID={2}, AssetID={3}",
71 success, status, itemID, assetID));
72  
73 TextureID = assetID;
74 Console.WriteLine(String.Format("Upload took {0}", DateTime.Now.Subtract(start)));
75 UploadCompleteEvent.Set();
76 }
77 );
78 }
79 }
80  
81 private byte[] LoadImage(string fileName)
82 {
83 byte[] UploadData;
84 string lowfilename = fileName.ToLower();
85 Bitmap bitmap = null;
86  
87 try
88 {
89 if (lowfilename.EndsWith(".jp2") || lowfilename.EndsWith(".j2c"))
90 {
91 Image image;
92 ManagedImage managedImage;
93  
94 // Upload JPEG2000 images untouched
95 UploadData = System.IO.File.ReadAllBytes(fileName);
96  
97 OpenJPEG.DecodeToImage(UploadData, out managedImage, out image);
98 bitmap = (Bitmap)image;
99 }
100 else
101 {
102 if (lowfilename.EndsWith(".tga"))
103 bitmap = LoadTGAClass.LoadTGA(fileName);
104 else
105 bitmap = (Bitmap)System.Drawing.Image.FromFile(fileName);
106  
107 int oldwidth = bitmap.Width;
108 int oldheight = bitmap.Height;
109  
110 if (!IsPowerOfTwo((uint)oldwidth) || !IsPowerOfTwo((uint)oldheight))
111 {
112 Bitmap resized = new Bitmap(256, 256, bitmap.PixelFormat);
113 Graphics graphics = Graphics.FromImage(resized);
114  
115 graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
116 graphics.InterpolationMode =
117 System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
118 graphics.DrawImage(bitmap, 0, 0, 256, 256);
119  
120 bitmap.Dispose();
121 bitmap = resized;
122  
123 oldwidth = 256;
124 oldheight = 256;
125 }
126  
127 // Handle resizing to prevent excessively large images
128 if (oldwidth > 1024 || oldheight > 1024)
129 {
130 int newwidth = (oldwidth > 1024) ? 1024 : oldwidth;
131 int newheight = (oldheight > 1024) ? 1024 : oldheight;
132  
133 Bitmap resized = new Bitmap(newwidth, newheight, bitmap.PixelFormat);
134 Graphics graphics = Graphics.FromImage(resized);
135  
136 graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
137 graphics.InterpolationMode =
138 System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
139 graphics.DrawImage(bitmap, 0, 0, newwidth, newheight);
140  
141 bitmap.Dispose();
142 bitmap = resized;
143 }
144  
145 UploadData = OpenJPEG.EncodeFromImage(bitmap, false);
146 }
147 }
148 catch (Exception ex)
149 {
150 Console.WriteLine(ex.ToString() + " SL Image Upload ");
151 return null;
152 }
153 return UploadData;
154 }
155  
156 private static bool IsPowerOfTwo(uint n)
157 {
158 return (n & (n - 1)) == 0 && n != 0;
159 }
160 }
161 }