mirror of
https://git.do.srb2.org/STJr/UltimateZoneBuilder.git
synced 2025-01-18 22:41:46 +00:00
Refactor the ImageData load code so that the UI thread requires no locks
This commit is contained in:
parent
f41c5bdd6f
commit
d46c72b04a
18 changed files with 1267 additions and 1366 deletions
|
@ -57,19 +57,12 @@ namespace CodeImp.DoomBuilder.Data
|
|||
#region ================== Methods
|
||||
|
||||
// This loads the image
|
||||
protected override void LocalLoadImage()
|
||||
protected override LocalLoadResult LocalLoadImage()
|
||||
{
|
||||
lock(this)
|
||||
{
|
||||
// No failure checking here. I anything fails here, it is not the user's fault,
|
||||
// because the resources this loads are in the assembly.
|
||||
// No failure checking here. I anything fails here, it is not the user's fault,
|
||||
// because the resources this loads are in the assembly.
|
||||
|
||||
// Get resource from memory
|
||||
bitmap = img;
|
||||
|
||||
// Pass on to base
|
||||
base.LocalLoadImage();
|
||||
}
|
||||
return new LocalLoadResult(img);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
|
|
@ -36,70 +36,63 @@ namespace CodeImp.DoomBuilder.Data
|
|||
#region ================== Methods
|
||||
|
||||
// This loads the image
|
||||
protected override void LocalLoadImage()
|
||||
protected override LocalLoadResult LocalLoadImage()
|
||||
{
|
||||
// Leave when already loaded
|
||||
if(this.IsImageLoaded) return;
|
||||
|
||||
lock(this)
|
||||
Bitmap bitmap = new Bitmap(width, height);
|
||||
|
||||
int w = Math.Max(2, Math.Min(width, height) / 24); // line width
|
||||
int o = w / 2; // line center offset
|
||||
int l = w * 3; // line length
|
||||
name = name.ToUpperInvariant();
|
||||
|
||||
using(Graphics g = Graphics.FromImage(bitmap))
|
||||
{
|
||||
bitmap = new Bitmap(width, height);
|
||||
// Fill bg
|
||||
g.FillRectangle(Brushes.Black, 0, 0, width, height);
|
||||
|
||||
int w = Math.Max(2, Math.Min(width, height) / 24); // line width
|
||||
int o = w / 2; // line center offset
|
||||
int l = w * 3; // line length
|
||||
name = name.ToUpperInvariant();
|
||||
|
||||
using(Graphics g = Graphics.FromImage(bitmap))
|
||||
// Draw corners
|
||||
Color color = General.Colors.BrightColors[General.Random(0, General.Colors.BrightColors.Length - 1)].ToColor();
|
||||
using(var pen = new Pen(color, w))
|
||||
{
|
||||
// Fill bg
|
||||
g.FillRectangle(Brushes.Black, 0, 0, width, height);
|
||||
g.DrawLines(pen, new[] { new Point(l, o), new Point(o, o), new Point(o, l) }); // TL
|
||||
g.DrawLines(pen, new[] { new Point(width - l, o), new Point(width - o, o), new Point(width - o, l) }); // TR
|
||||
g.DrawLines(pen, new[] { new Point(l, height - o), new Point(o, height - o), new Point(o, height - l) }); // BL
|
||||
g.DrawLines(pen, new[] { new Point(width - l, height - o), new Point(width - o, height - o), new Point(width - o, height - l) }); // BR
|
||||
}
|
||||
|
||||
// Draw corners
|
||||
Color color = General.Colors.BrightColors[General.Random(0, General.Colors.BrightColors.Length - 1)].ToColor();
|
||||
using(var pen = new Pen(color, w))
|
||||
// Calculate required font size
|
||||
const string rec = "\u25CFREC";
|
||||
float targetwidth = Math.Max(l * 2, 22);
|
||||
SizeF fontsize = g.MeasureString(rec, General.MainWindow.Font);
|
||||
float scaleratio = Math.Min(targetwidth / fontsize.Height, targetwidth / fontsize.Width);
|
||||
|
||||
// Draw "REC" text
|
||||
using(Font font = new Font(General.MainWindow.Font.FontFamily, General.MainWindow.Font.Size * scaleratio))
|
||||
{
|
||||
using(var brush = new SolidBrush(Color.Red))
|
||||
{
|
||||
g.DrawLines(pen, new[] { new Point(l, o), new Point(o, o), new Point(o, l) }); // TL
|
||||
g.DrawLines(pen, new[] { new Point(width - l, o), new Point(width - o, o), new Point(width - o, l) }); // TR
|
||||
g.DrawLines(pen, new[] { new Point(l, height - o), new Point(o, height - o), new Point(o, height - l) }); // BL
|
||||
g.DrawLines(pen, new[] { new Point(width - l, height - o), new Point(width - o, height - o), new Point(width - o, height - l) }); // BR
|
||||
}
|
||||
|
||||
// Calculate required font size
|
||||
const string rec = "\u25CFREC";
|
||||
float targetwidth = Math.Max(l * 2, 22);
|
||||
SizeF fontsize = g.MeasureString(rec, General.MainWindow.Font);
|
||||
float scaleratio = Math.Min(targetwidth / fontsize.Height, targetwidth / fontsize.Width);
|
||||
|
||||
// Draw "REC" text
|
||||
using(Font font = new Font(General.MainWindow.Font.FontFamily, General.MainWindow.Font.Size * scaleratio))
|
||||
{
|
||||
using(var brush = new SolidBrush(Color.Red))
|
||||
{
|
||||
g.DrawString(rec, font, brush, new RectangleF(l / 2, l / 2 - w / 2, fontsize.Width * scaleratio, fontsize.Height * scaleratio));
|
||||
}
|
||||
}
|
||||
|
||||
// Calculate required font size
|
||||
targetwidth = Math.Min(width, height);
|
||||
targetwidth -= targetwidth / 6;
|
||||
fontsize = g.MeasureString(name, General.MainWindow.Font);
|
||||
scaleratio = Math.Min(targetwidth / fontsize.Height, targetwidth / fontsize.Width);
|
||||
|
||||
// Draw texture name
|
||||
using(Font font = new Font(General.MainWindow.Font.FontFamily, General.MainWindow.Font.Size * scaleratio))
|
||||
{
|
||||
using(var brush = new SolidBrush(color))
|
||||
{
|
||||
StringFormat sf = new StringFormat { Alignment = StringAlignment.Center, LineAlignment = StringAlignment.Center };
|
||||
g.DrawString(name, font, brush, new RectangleF(0, 0, width, height), sf);
|
||||
}
|
||||
g.DrawString(rec, font, brush, new RectangleF(l / 2, l / 2 - w / 2, fontsize.Width * scaleratio, fontsize.Height * scaleratio));
|
||||
}
|
||||
}
|
||||
|
||||
// Pass on to base
|
||||
base.LocalLoadImage();
|
||||
// Calculate required font size
|
||||
targetwidth = Math.Min(width, height);
|
||||
targetwidth -= targetwidth / 6;
|
||||
fontsize = g.MeasureString(name, General.MainWindow.Font);
|
||||
scaleratio = Math.Min(targetwidth / fontsize.Height, targetwidth / fontsize.Width);
|
||||
|
||||
// Draw texture name
|
||||
using(Font font = new Font(General.MainWindow.Font.FontFamily, General.MainWindow.Font.Size * scaleratio))
|
||||
{
|
||||
using(var brush = new SolidBrush(color))
|
||||
{
|
||||
StringFormat sf = new StringFormat { Alignment = StringAlignment.Center, LineAlignment = StringAlignment.Center };
|
||||
g.DrawString(name, font, brush, new RectangleF(0, 0, width, height), sf);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return new LocalLoadResult(bitmap);
|
||||
}
|
||||
|
||||
//mxd
|
||||
|
|
|
@ -56,42 +56,31 @@ namespace CodeImp.DoomBuilder.Data
|
|||
protected override void LocalLoadImage()
|
||||
{
|
||||
// Leave when already loaded
|
||||
if(this.IsImageLoaded) return;
|
||||
if((width == 0) || (height == 0)) return;
|
||||
|
||||
lock(this)
|
||||
// Create bitmap
|
||||
Bitmap bitmap = null;
|
||||
string error = null;
|
||||
try
|
||||
{
|
||||
// Create bitmap
|
||||
try
|
||||
bitmap = new Bitmap(width, height, PixelFormat.Format32bppArgb);
|
||||
BitmapData bitmapdata = bitmap.LockBits(new Rectangle(0, 0, width, height), ImageLockMode.WriteOnly, PixelFormat.Format32bppArgb);
|
||||
PixelColor* pixels = (PixelColor*)bitmapdata.Scan0.ToPointer();
|
||||
for(int i = 0; i < (width * height); i++)
|
||||
{
|
||||
if(bitmap != null) bitmap.Dispose();
|
||||
bitmap = new Bitmap(width, height, PixelFormat.Format32bppArgb);
|
||||
BitmapData bitmapdata = bitmap.LockBits(new Rectangle(0, 0, width, height), ImageLockMode.WriteOnly, PixelFormat.Format32bppArgb);
|
||||
PixelColor* pixels = (PixelColor*)bitmapdata.Scan0.ToPointer();
|
||||
for(int i = 0; i < (width * height); i++)
|
||||
{
|
||||
*pixels = color;
|
||||
pixels++;
|
||||
}
|
||||
bitmap.UnlockBits(bitmapdata);
|
||||
*pixels = color;
|
||||
pixels++;
|
||||
}
|
||||
catch(Exception e)
|
||||
{
|
||||
// Unable to make bitmap
|
||||
General.ErrorLogger.Add(ErrorType.Error, "Unable to create color image '" + this.Name + "'. " + e.GetType().Name + ": " + e.Message);
|
||||
loadfailed = true;
|
||||
}
|
||||
|
||||
// Dispose bitmap if load failed
|
||||
if(loadfailed && (bitmap != null))
|
||||
{
|
||||
bitmap.Dispose();
|
||||
bitmap = null;
|
||||
}
|
||||
|
||||
// Pass on to base
|
||||
base.LocalLoadImage();
|
||||
bitmap.UnlockBits(bitmapdata);
|
||||
}
|
||||
catch(Exception e)
|
||||
{
|
||||
// Unable to make bitmap
|
||||
error = "Unable to create color image '" + this.Name + "'. " + e.GetType().Name + ": " + e.Message;
|
||||
bitmap?.Dispose();
|
||||
}
|
||||
|
||||
EndLoadImage(bitmap, error);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
|
|
@ -17,6 +17,7 @@
|
|||
#region ================== Namespaces
|
||||
|
||||
using System;
|
||||
using System.Drawing;
|
||||
using System.IO;
|
||||
using CodeImp.DoomBuilder.IO;
|
||||
|
||||
|
@ -44,66 +45,52 @@ namespace CodeImp.DoomBuilder.Data
|
|||
#region ================== Methods
|
||||
|
||||
// This loads the image
|
||||
protected override void LocalLoadImage()
|
||||
protected override LocalLoadResult LocalLoadImage()
|
||||
{
|
||||
// Leave when already loaded
|
||||
if(this.IsImageLoaded) return;
|
||||
Bitmap bitmap = null;
|
||||
string error = null;
|
||||
|
||||
lock(this)
|
||||
// Get the lump data stream
|
||||
Stream lumpdata = General.Map.Data.GetColormapData(Name);
|
||||
if(lumpdata != null)
|
||||
{
|
||||
// Get the lump data stream
|
||||
Stream lumpdata = General.Map.Data.GetColormapData(Name);
|
||||
if(lumpdata != null)
|
||||
// Copy lump data to memory
|
||||
lumpdata.Seek(0, SeekOrigin.Begin);
|
||||
byte[] membytes = new byte[(int)lumpdata.Length];
|
||||
lumpdata.Read(membytes, 0, (int)lumpdata.Length);
|
||||
MemoryStream mem = new MemoryStream(membytes);
|
||||
mem.Seek(0, SeekOrigin.Begin);
|
||||
|
||||
// Get a reader for the data
|
||||
IImageReader reader = ImageDataFormat.GetImageReader(mem, ImageDataFormat.DOOMCOLORMAP, General.Map.Data.Palette);
|
||||
if(reader is UnknownImageReader)
|
||||
{
|
||||
// Copy lump data to memory
|
||||
lumpdata.Seek(0, SeekOrigin.Begin);
|
||||
byte[] membytes = new byte[(int)lumpdata.Length];
|
||||
lumpdata.Read(membytes, 0, (int)lumpdata.Length);
|
||||
MemoryStream mem = new MemoryStream(membytes);
|
||||
mem.Seek(0, SeekOrigin.Begin);
|
||||
|
||||
// Get a reader for the data
|
||||
IImageReader reader = ImageDataFormat.GetImageReader(mem, ImageDataFormat.DOOMCOLORMAP, General.Map.Data.Palette);
|
||||
if(reader is UnknownImageReader)
|
||||
{
|
||||
// Data is in an unknown format!
|
||||
General.ErrorLogger.Add(ErrorType.Error, "Colormap lump \"" + Name + "\" data format could not be read. Does this lump contain valid colormap data at all?");
|
||||
bitmap = null;
|
||||
}
|
||||
else
|
||||
{
|
||||
// Read data as bitmap
|
||||
mem.Seek(0, SeekOrigin.Begin);
|
||||
if(bitmap != null) bitmap.Dispose();
|
||||
bitmap = reader.ReadAsBitmap(mem);
|
||||
}
|
||||
|
||||
// Done
|
||||
mem.Dispose();
|
||||
|
||||
if(bitmap != null)
|
||||
{
|
||||
// Get width and height from image and set the scale
|
||||
width = bitmap.Size.Width;
|
||||
height = bitmap.Size.Height;
|
||||
scale.x = General.Map.Config.DefaultFlatScale;
|
||||
scale.y = General.Map.Config.DefaultFlatScale;
|
||||
}
|
||||
else
|
||||
{
|
||||
loadfailed = true;
|
||||
}
|
||||
// Data is in an unknown format!
|
||||
error = "Colormap lump \"" + Name + "\" data format could not be read. Does this lump contain valid colormap data at all?";
|
||||
bitmap = null;
|
||||
}
|
||||
else
|
||||
{
|
||||
// Missing a patch lump!
|
||||
General.ErrorLogger.Add(ErrorType.Error, "Missing colormap lump \"" + Name + "\". Did you forget to include required resources?");
|
||||
loadfailed = true;
|
||||
// Read data as bitmap
|
||||
mem.Seek(0, SeekOrigin.Begin);
|
||||
if(bitmap != null) bitmap.Dispose();
|
||||
bitmap = reader.ReadAsBitmap(mem);
|
||||
}
|
||||
|
||||
// Pass on to base
|
||||
base.LocalLoadImage();
|
||||
// Done
|
||||
mem.Dispose();
|
||||
}
|
||||
else
|
||||
{
|
||||
// Missing a patch lump!
|
||||
error = "Missing colormap lump \"" + Name + "\". Did you forget to include required resources?";
|
||||
}
|
||||
|
||||
return new LocalLoadResult(bitmap, error, () =>
|
||||
{
|
||||
scale.x = General.Map.Config.DefaultFlatScale;
|
||||
scale.y = General.Map.Config.DefaultFlatScale;
|
||||
});
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
|
|
@ -96,11 +96,13 @@ namespace CodeImp.DoomBuilder.Data
|
|||
private Dictionary<int, AmbientSoundInfo> ambientsounds;
|
||||
|
||||
//mxd. Text resources
|
||||
private Dictionary<ScriptType, HashSet<ScriptResource>> scriptresources;
|
||||
|
||||
// Background loading
|
||||
private Dictionary<ScriptType, HashSet<ScriptResource>> scriptresources;
|
||||
|
||||
// Background loading
|
||||
private object syncobject = new object();
|
||||
private Queue<ImageData> imageque;
|
||||
private Thread[] backgroundloader;
|
||||
private int threadsfinished;
|
||||
private bool notifiedbusy;
|
||||
|
||||
// Image previews
|
||||
|
@ -734,6 +736,7 @@ namespace CodeImp.DoomBuilder.Data
|
|||
// Timing
|
||||
loadstarttime = Clock.CurrentTime;
|
||||
loadfinishtime = 0;
|
||||
threadsfinished = 0;
|
||||
|
||||
// If a loader is already running, stop it first
|
||||
if(backgroundloader != null) StopBackgroundLoader();
|
||||
|
@ -804,7 +807,7 @@ namespace CodeImp.DoomBuilder.Data
|
|||
{
|
||||
// Get next item
|
||||
ImageData image = null;
|
||||
lock(imageque)
|
||||
lock(syncobject)
|
||||
{
|
||||
// Fetch next image to process
|
||||
if(imageque.Count > 0) image = imageque.Dequeue();
|
||||
|
@ -851,37 +854,53 @@ namespace CodeImp.DoomBuilder.Data
|
|||
}
|
||||
else
|
||||
{
|
||||
// Timing
|
||||
if(loadfinishtime == 0)
|
||||
{
|
||||
//mxd. Release PK3 files
|
||||
foreach(DataReader reader in containers)
|
||||
{
|
||||
if(reader is PK3Reader) (reader as PK3Reader).BathMode = false;
|
||||
}
|
||||
|
||||
loadfinishtime = Clock.CurrentTime;
|
||||
string deltatimesec = ((loadfinishtime - loadstarttime) / 1000.0f).ToString("########0.00");
|
||||
General.WriteLogLine("Resources loading took " + deltatimesec + " seconds");
|
||||
loadstarttime = 0; //mxd
|
||||
bool lastthread = false;
|
||||
lock (syncobject)
|
||||
{
|
||||
threadsfinished++;
|
||||
if (threadsfinished == backgroundloader.Length)
|
||||
lastthread = true;
|
||||
}
|
||||
|
||||
//mxd. Show more detailed message
|
||||
if(notifiedbusy)
|
||||
{
|
||||
notifiedbusy = false;
|
||||
General.MainWindow.ResourcesLoaded(deltatimesec);
|
||||
}
|
||||
}
|
||||
else if(notifiedbusy) //mxd. Sould never happen (?)
|
||||
{
|
||||
notifiedbusy = false;
|
||||
General.MainWindow.UpdateStatus();
|
||||
}
|
||||
if (lastthread)
|
||||
{
|
||||
// Timing
|
||||
if (loadfinishtime == 0)
|
||||
{
|
||||
//mxd. Release PK3 files
|
||||
foreach (DataReader reader in containers)
|
||||
{
|
||||
if (reader is PK3Reader) (reader as PK3Reader).BathMode = false;
|
||||
}
|
||||
|
||||
loadfinishtime = Clock.CurrentTime;
|
||||
string deltatimesec = ((loadfinishtime - loadstarttime) / 1000.0f).ToString("########0.00");
|
||||
General.WriteLogLine("Resources loading took " + deltatimesec + " seconds");
|
||||
loadstarttime = 0; //mxd
|
||||
|
||||
lock (syncobject)
|
||||
{
|
||||
threadsfinished = 0;
|
||||
}
|
||||
|
||||
//mxd. Show more detailed message
|
||||
if (notifiedbusy)
|
||||
{
|
||||
notifiedbusy = false;
|
||||
General.MainWindow.ResourcesLoaded(deltatimesec);
|
||||
}
|
||||
}
|
||||
else if (notifiedbusy) //mxd. Sould never happen (?)
|
||||
{
|
||||
notifiedbusy = false;
|
||||
General.MainWindow.UpdateStatus();
|
||||
}
|
||||
}
|
||||
|
||||
// Wait until there's more to do.
|
||||
lock (imageque)
|
||||
lock (syncobject)
|
||||
{
|
||||
if (imageque.Count == 0) Monitor.Wait(imageque);
|
||||
if (imageque.Count == 0) Monitor.Wait(syncobject);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -899,7 +918,7 @@ namespace CodeImp.DoomBuilder.Data
|
|||
{
|
||||
// Add for loading
|
||||
img.ImageState = ImageLoadState.Loading;
|
||||
lock(imageque) { imageque.Enqueue(img); Monitor.Pulse(imageque); }
|
||||
lock(syncobject) { imageque.Enqueue(img); Monitor.Pulse(syncobject); }
|
||||
}
|
||||
|
||||
// Unload this image?
|
||||
|
@ -907,7 +926,7 @@ namespace CodeImp.DoomBuilder.Data
|
|||
{
|
||||
// Add for unloading
|
||||
img.ImageState = ImageLoadState.Unloading;
|
||||
lock(imageque) { imageque.Enqueue(img); Monitor.Pulse(imageque); }
|
||||
lock(syncobject) { imageque.Enqueue(img); Monitor.Pulse(syncobject); }
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -20,6 +20,7 @@ using System;
|
|||
using System.IO;
|
||||
using CodeImp.DoomBuilder.IO;
|
||||
using CodeImp.DoomBuilder.Controls;
|
||||
using System.Drawing;
|
||||
|
||||
#endregion
|
||||
|
||||
|
@ -140,62 +141,48 @@ namespace CodeImp.DoomBuilder.Data
|
|||
}
|
||||
|
||||
// This loads the image
|
||||
protected override void LocalLoadImage()
|
||||
protected override LocalLoadResult LocalLoadImage()
|
||||
{
|
||||
// Leave when already loaded
|
||||
if (this.IsImageLoaded) return;
|
||||
// Load file data
|
||||
Bitmap bitmap = null;
|
||||
string error = null;
|
||||
|
||||
lock (this)
|
||||
MemoryStream filedata = null;
|
||||
try
|
||||
{
|
||||
// Load file data
|
||||
if (bitmap != null) bitmap.Dispose(); bitmap = null;
|
||||
|
||||
MemoryStream filedata = null;
|
||||
try
|
||||
{
|
||||
filedata = new MemoryStream(File.ReadAllBytes(filepathname));
|
||||
}
|
||||
catch (IOException)
|
||||
{
|
||||
General.ErrorLogger.Add(ErrorType.Error, "Image file \"" + filepathname + "\" could not be read, while loading image \"" + this.Name + "\". Consider reloading resources.");
|
||||
loadfailed = true;
|
||||
}
|
||||
|
||||
if (filedata != null)
|
||||
{
|
||||
// Get a reader for the data
|
||||
IImageReader reader = ImageDataFormat.GetImageReader(filedata, probableformat, General.Map.Data.Palette);
|
||||
if (!(reader is UnknownImageReader))
|
||||
{
|
||||
// Load the image
|
||||
filedata.Seek(0, SeekOrigin.Begin);
|
||||
try { bitmap = reader.ReadAsBitmap(filedata); }
|
||||
catch (InvalidDataException)
|
||||
{
|
||||
// Data cannot be read!
|
||||
bitmap = null;
|
||||
}
|
||||
}
|
||||
|
||||
// Not loaded?
|
||||
if (bitmap == null)
|
||||
{
|
||||
General.ErrorLogger.Add(ErrorType.Error, "Image file \"" + filepathname + "\" data format could not be read, while loading image \"" + this.Name + "\". Is this a valid picture file at all?");
|
||||
loadfailed = true;
|
||||
}
|
||||
else
|
||||
{
|
||||
// Get width and height
|
||||
width = bitmap.Size.Width;
|
||||
height = bitmap.Size.Height;
|
||||
}
|
||||
|
||||
filedata.Dispose();
|
||||
}
|
||||
|
||||
// Pass on to base
|
||||
base.LocalLoadImage();
|
||||
filedata = new MemoryStream(File.ReadAllBytes(filepathname));
|
||||
}
|
||||
catch (IOException)
|
||||
{
|
||||
error = "Image file \"" + filepathname + "\" could not be read, while loading image \"" + this.Name + "\". Consider reloading resources.";
|
||||
}
|
||||
|
||||
if (filedata != null)
|
||||
{
|
||||
// Get a reader for the data
|
||||
IImageReader reader = ImageDataFormat.GetImageReader(filedata, probableformat, General.Map.Data.Palette);
|
||||
if (!(reader is UnknownImageReader))
|
||||
{
|
||||
// Load the image
|
||||
filedata.Seek(0, SeekOrigin.Begin);
|
||||
try { bitmap = reader.ReadAsBitmap(filedata); }
|
||||
catch (InvalidDataException)
|
||||
{
|
||||
// Data cannot be read!
|
||||
bitmap = null;
|
||||
}
|
||||
}
|
||||
|
||||
// Not loaded?
|
||||
if (bitmap == null)
|
||||
{
|
||||
error = "Image file \"" + filepathname + "\" data format could not be read, while loading image \"" + this.Name + "\". Is this a valid picture file at all?";
|
||||
}
|
||||
|
||||
filedata.Dispose();
|
||||
}
|
||||
|
||||
return new LocalLoadResult(bitmap, error);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
|
|
@ -17,6 +17,7 @@
|
|||
#region ================== Namespaces
|
||||
|
||||
using System;
|
||||
using System.Drawing;
|
||||
using System.IO;
|
||||
using CodeImp.DoomBuilder.IO;
|
||||
|
||||
|
@ -45,74 +46,59 @@ namespace CodeImp.DoomBuilder.Data
|
|||
#region ================== Methods
|
||||
|
||||
// This loads the image
|
||||
protected override void LocalLoadImage()
|
||||
protected override LocalLoadResult LocalLoadImage()
|
||||
{
|
||||
// Leave when already loaded
|
||||
if(this.IsImageLoaded) return;
|
||||
Bitmap bitmap = null;
|
||||
string error = null;
|
||||
|
||||
lock(this)
|
||||
// Get the lump data stream
|
||||
string flatlocation = string.Empty; //mxd
|
||||
Stream lumpdata = General.Map.Data.GetFlatData(Name, hasLongName, ref flatlocation);
|
||||
if(lumpdata != null)
|
||||
{
|
||||
// Get the lump data stream
|
||||
string flatlocation = string.Empty; //mxd
|
||||
Stream lumpdata = General.Map.Data.GetFlatData(Name, hasLongName, ref flatlocation);
|
||||
if(lumpdata != null)
|
||||
// Copy lump data to memory
|
||||
byte[] membytes = new byte[(int)lumpdata.Length];
|
||||
|
||||
lock(lumpdata) //mxd
|
||||
{
|
||||
// Copy lump data to memory
|
||||
byte[] membytes = new byte[(int)lumpdata.Length];
|
||||
|
||||
lock(lumpdata) //mxd
|
||||
{
|
||||
lumpdata.Seek(0, SeekOrigin.Begin);
|
||||
lumpdata.Read(membytes, 0, (int)lumpdata.Length);
|
||||
}
|
||||
lumpdata.Seek(0, SeekOrigin.Begin);
|
||||
lumpdata.Read(membytes, 0, (int)lumpdata.Length);
|
||||
}
|
||||
|
||||
MemoryStream mem = new MemoryStream(membytes);
|
||||
mem.Seek(0, SeekOrigin.Begin);
|
||||
MemoryStream mem = new MemoryStream(membytes);
|
||||
mem.Seek(0, SeekOrigin.Begin);
|
||||
|
||||
// Get a reader for the data
|
||||
IImageReader reader = ImageDataFormat.GetImageReader(mem, ImageDataFormat.DOOMFLAT, General.Map.Data.Palette);
|
||||
if(reader is UnknownImageReader)
|
||||
{
|
||||
// Data is in an unknown format!
|
||||
General.ErrorLogger.Add(ErrorType.Error, "Flat lump \"" + Path.Combine(flatlocation, Name) + "\" data format could not be read. Does this lump contain valid picture data at all?");
|
||||
bitmap = null;
|
||||
}
|
||||
else
|
||||
{
|
||||
// Read data as bitmap
|
||||
mem.Seek(0, SeekOrigin.Begin);
|
||||
if(bitmap != null) bitmap.Dispose();
|
||||
bitmap = reader.ReadAsBitmap(mem);
|
||||
}
|
||||
|
||||
// Done
|
||||
mem.Dispose();
|
||||
|
||||
if(bitmap != null)
|
||||
{
|
||||
// Get width and height from image and set the scale
|
||||
width = bitmap.Size.Width;
|
||||
height = bitmap.Size.Height;
|
||||
scale.x = General.Map.Config.DefaultFlatScale;
|
||||
scale.y = General.Map.Config.DefaultFlatScale;
|
||||
}
|
||||
else
|
||||
{
|
||||
loadfailed = true;
|
||||
}
|
||||
// Get a reader for the data
|
||||
IImageReader reader = ImageDataFormat.GetImageReader(mem, ImageDataFormat.DOOMFLAT, General.Map.Data.Palette);
|
||||
if(reader is UnknownImageReader)
|
||||
{
|
||||
// Data is in an unknown format!
|
||||
error = "Flat lump \"" + Path.Combine(flatlocation, Name) + "\" data format could not be read. Does this lump contain valid picture data at all?";
|
||||
bitmap = null;
|
||||
}
|
||||
else
|
||||
{
|
||||
// Missing a patch lump!
|
||||
General.ErrorLogger.Add(ErrorType.Error, "Missing flat lump \"" + Name + "\". Did you forget to include required resources?");
|
||||
loadfailed = true;
|
||||
// Read data as bitmap
|
||||
mem.Seek(0, SeekOrigin.Begin);
|
||||
bitmap = reader.ReadAsBitmap(mem);
|
||||
}
|
||||
|
||||
// Pass on to base
|
||||
base.LocalLoadImage();
|
||||
// Done
|
||||
mem.Dispose();
|
||||
}
|
||||
else
|
||||
{
|
||||
// Missing a patch lump!
|
||||
error = "Missing flat lump \"" + Name + "\". Did you forget to include required resources?";
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
return new LocalLoadResult(bitmap, error, () =>
|
||||
{
|
||||
scale.x = General.Map.Config.DefaultFlatScale;
|
||||
scale.y = General.Map.Config.DefaultFlatScale;
|
||||
});
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
|
|
|
@ -108,86 +108,73 @@ namespace CodeImp.DoomBuilder.Data
|
|||
}
|
||||
|
||||
// This loads the image
|
||||
protected override void LocalLoadImage()
|
||||
protected override LocalLoadResult LocalLoadImage()
|
||||
{
|
||||
// Checks
|
||||
if(this.IsImageLoaded) return;
|
||||
// Get the patch data stream
|
||||
Bitmap bitmap = null;
|
||||
string error = null;
|
||||
string sourcelocation = string.Empty;
|
||||
Stream data = General.Map.Data.GetHiResTextureData(shortname, ref sourcelocation);
|
||||
if(data != null)
|
||||
{
|
||||
// Copy patch data to memory
|
||||
byte[] membytes = new byte[(int)data.Length];
|
||||
|
||||
lock(this)
|
||||
{
|
||||
// Get the patch data stream
|
||||
if(bitmap != null) bitmap.Dispose(); bitmap = null;
|
||||
string sourcelocation = string.Empty;
|
||||
Stream data = General.Map.Data.GetHiResTextureData(shortname, ref sourcelocation);
|
||||
if(data != null)
|
||||
lock(data) //mxd
|
||||
{
|
||||
// Copy patch data to memory
|
||||
byte[] membytes = new byte[(int)data.Length];
|
||||
|
||||
lock(data) //mxd
|
||||
{
|
||||
data.Seek(0, SeekOrigin.Begin);
|
||||
data.Read(membytes, 0, (int)data.Length);
|
||||
}
|
||||
data.Seek(0, SeekOrigin.Begin);
|
||||
data.Read(membytes, 0, (int)data.Length);
|
||||
}
|
||||
|
||||
MemoryStream mem = new MemoryStream(membytes);
|
||||
mem.Seek(0, SeekOrigin.Begin);
|
||||
MemoryStream mem = new MemoryStream(membytes);
|
||||
mem.Seek(0, SeekOrigin.Begin);
|
||||
|
||||
// Get a reader for the data
|
||||
IImageReader reader = ImageDataFormat.GetImageReader(mem, (isFlat ? ImageDataFormat.DOOMFLAT : ImageDataFormat.DOOMPICTURE), General.Map.Data.Palette);
|
||||
if(!(reader is UnknownImageReader))
|
||||
{
|
||||
// Load the image
|
||||
mem.Seek(0, SeekOrigin.Begin);
|
||||
try { bitmap = reader.ReadAsBitmap(mem); }
|
||||
catch(InvalidDataException)
|
||||
{
|
||||
// Data cannot be read!
|
||||
bitmap = null;
|
||||
}
|
||||
}
|
||||
|
||||
// Not loaded?
|
||||
if(bitmap == null)
|
||||
{
|
||||
General.ErrorLogger.Add(ErrorType.Error, "Image lump \"" + Path.Combine(sourcelocation, filepathname.Replace(Path.AltDirectorySeparatorChar, Path.DirectorySeparatorChar)) + "\" data format could not be read, while loading HiRes texture \"" + this.Name + "\". Does this lump contain valid picture data at all?");
|
||||
loadfailed = true;
|
||||
}
|
||||
else
|
||||
{
|
||||
// Get width and height from image
|
||||
width = bitmap.Size.Width;
|
||||
height = bitmap.Size.Height;
|
||||
|
||||
// Apply source overrides?
|
||||
if(!sourcesize.IsEmpty)
|
||||
{
|
||||
scale = new Vector2D(ScaledWidth / width, ScaledHeight / height);
|
||||
}
|
||||
else
|
||||
{
|
||||
if(overridesettingsapplied)
|
||||
General.ErrorLogger.Add(ErrorType.Warning, "Unable to get source texture dimensions while loading HiRes texture \"" + this.Name + "\".");
|
||||
|
||||
// Use our own size...
|
||||
sourcesize = new Size(width, height);
|
||||
}
|
||||
}
|
||||
|
||||
// Done
|
||||
mem.Dispose();
|
||||
}
|
||||
else
|
||||
// Get a reader for the data
|
||||
IImageReader reader = ImageDataFormat.GetImageReader(mem, (isFlat ? ImageDataFormat.DOOMFLAT : ImageDataFormat.DOOMPICTURE), General.Map.Data.Palette);
|
||||
if(!(reader is UnknownImageReader))
|
||||
{
|
||||
General.ErrorLogger.Add(ErrorType.Error, "Image lump \"" + shortname + "\" could not be found, while loading HiRes texture \"" + this.Name + "\". Did you forget to include required resources?");
|
||||
loadfailed = true;
|
||||
// Load the image
|
||||
mem.Seek(0, SeekOrigin.Begin);
|
||||
try { bitmap = reader.ReadAsBitmap(mem); }
|
||||
catch(InvalidDataException)
|
||||
{
|
||||
// Data cannot be read!
|
||||
bitmap = null;
|
||||
}
|
||||
}
|
||||
|
||||
// Pass on to base
|
||||
base.LocalLoadImage();
|
||||
|
||||
// Not loaded?
|
||||
if(bitmap == null)
|
||||
{
|
||||
error = "Image lump \"" + Path.Combine(sourcelocation, filepathname.Replace(Path.AltDirectorySeparatorChar, Path.DirectorySeparatorChar)) + "\" data format could not be read, while loading HiRes texture \"" + this.Name + "\". Does this lump contain valid picture data at all?";
|
||||
}
|
||||
|
||||
// Done
|
||||
mem.Dispose();
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
else
|
||||
{
|
||||
error = "Image lump \"" + shortname + "\" could not be found, while loading HiRes texture \"" + this.Name + "\". Did you forget to include required resources?";
|
||||
}
|
||||
|
||||
return new LocalLoadResult(bitmap, error, () =>
|
||||
{
|
||||
// Apply source overrides?
|
||||
if (!sourcesize.IsEmpty)
|
||||
{
|
||||
scale = new Vector2D(ScaledWidth / width, ScaledHeight / height);
|
||||
}
|
||||
else
|
||||
{
|
||||
if (overridesettingsapplied)
|
||||
General.ErrorLogger.Add(ErrorType.Warning, "Unable to get source texture dimensions while loading HiRes texture \"" + this.Name + "\".");
|
||||
|
||||
// Use our own size...
|
||||
sourcesize = new Size(width, height);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
|
|
|
@ -17,10 +17,12 @@
|
|||
#region ================== Namespaces
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Drawing;
|
||||
using System.Drawing.Drawing2D;
|
||||
using System.Drawing.Imaging;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Runtime.InteropServices;
|
||||
using CodeImp.DoomBuilder.Geometry;
|
||||
using CodeImp.DoomBuilder.IO;
|
||||
|
@ -62,20 +64,21 @@ namespace CodeImp.DoomBuilder.Data
|
|||
//mxd. Hashing
|
||||
private static int hashcounter;
|
||||
private readonly int hashcode;
|
||||
|
||||
// Loading
|
||||
private volatile ImageLoadState previewstate;
|
||||
private volatile ImageLoadState imagestate;
|
||||
private volatile int previewindex;
|
||||
protected volatile bool loadfailed;
|
||||
private volatile bool allowunload;
|
||||
|
||||
// References
|
||||
private volatile bool usedinmap;
|
||||
|
||||
// Loading
|
||||
private ImageLoadState previewstate;
|
||||
private ImageLoadState imagestate;
|
||||
private int previewindex;
|
||||
private bool loadfailed;
|
||||
private bool allowunload;
|
||||
|
||||
// References
|
||||
private volatile bool usedinmap;
|
||||
private volatile int references;
|
||||
|
||||
// GDI bitmap
|
||||
protected Bitmap bitmap;
|
||||
|
||||
// GDI bitmap
|
||||
private Bitmap _bitmap;
|
||||
protected Bitmap bitmap { get { return _bitmap; } }
|
||||
|
||||
// Direct3D texture
|
||||
private int mipmaplevels; // 0 = all mipmaps
|
||||
|
@ -84,9 +87,6 @@ namespace CodeImp.DoomBuilder.Data
|
|||
|
||||
// Disposing
|
||||
protected bool isdisposed;
|
||||
|
||||
// Dummy object used when we don't have a bitmap for locking
|
||||
private object bitmapLocker = new object();
|
||||
|
||||
#endregion
|
||||
|
||||
|
@ -104,7 +104,7 @@ namespace CodeImp.DoomBuilder.Data
|
|||
public bool HasPatchWithSameName { get { return hasPatchWithSameName; } } //mxd
|
||||
internal bool HasLongName { get { return hasLongName; } } //mxd
|
||||
public bool UseColorCorrection { get { return usecolorcorrection; } set { usecolorcorrection = value; } }
|
||||
public Texture Texture { get { lock (this) lock (bitmap ?? bitmapLocker) { return texture; } } }
|
||||
public Texture Texture { get { return texture; } }
|
||||
public bool IsPreviewLoaded { get { return (previewstate == ImageLoadState.Ready); } }
|
||||
public bool IsImageLoaded { get { return (imagestate == ImageLoadState.Ready); } }
|
||||
public bool LoadFailed { get { return loadfailed; } }
|
||||
|
@ -153,20 +153,17 @@ namespace CodeImp.DoomBuilder.Data
|
|||
// Not already disposed?
|
||||
if(!isdisposed)
|
||||
{
|
||||
lock (this) lock (bitmap ?? bitmapLocker)
|
||||
{
|
||||
// Clean up
|
||||
if(bitmap != null) bitmap.Dispose();
|
||||
if(texture != null) texture.Dispose();
|
||||
bitmap = null;
|
||||
texture = null;
|
||||
// Clean up
|
||||
_bitmap?.Dispose();
|
||||
texture?.Dispose();
|
||||
_bitmap = null;
|
||||
texture = null;
|
||||
|
||||
// Done
|
||||
usedinmap = false;
|
||||
imagestate = ImageLoadState.None;
|
||||
previewstate = ImageLoadState.None;
|
||||
isdisposed = true;
|
||||
}
|
||||
// Done
|
||||
usedinmap = false;
|
||||
imagestate = ImageLoadState.None;
|
||||
previewstate = ImageLoadState.None;
|
||||
isdisposed = true;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -212,17 +209,6 @@ namespace CodeImp.DoomBuilder.Data
|
|||
ComputeNamesWidth(); // biwa
|
||||
}
|
||||
|
||||
// This unloads the image
|
||||
public virtual void UnloadImage()
|
||||
{
|
||||
lock (this) lock (bitmap ?? bitmapLocker)
|
||||
{
|
||||
if(bitmap != null) bitmap.Dispose();
|
||||
bitmap = null;
|
||||
imagestate = ImageLoadState.None;
|
||||
}
|
||||
}
|
||||
|
||||
// biwa. Computing the widths in the constructor of ImageBrowserItem accumulates to taking forever when loading many images,
|
||||
// like when showing the texture browser of huge texture sets like OTEX
|
||||
internal void ComputeNamesWidth()
|
||||
|
@ -235,16 +221,28 @@ namespace CodeImp.DoomBuilder.Data
|
|||
// This returns the bitmap image
|
||||
public Bitmap GetBitmap()
|
||||
{
|
||||
lock (this) lock (bitmap ?? bitmapLocker)
|
||||
{
|
||||
// Image loaded successfully?
|
||||
if(!loadfailed && (imagestate == ImageLoadState.Ready) && (bitmap != null))
|
||||
return bitmap;
|
||||
// Image loaded successfully?
|
||||
if(!loadfailed && (imagestate == ImageLoadState.Ready) && (bitmap != null))
|
||||
return bitmap;
|
||||
|
||||
// Image loading failed?
|
||||
return (loadfailed ? Properties.Resources.Failed : Properties.Resources.Hourglass);
|
||||
}
|
||||
// Image loading failed?
|
||||
return (loadfailed ? Properties.Resources.Failed : Properties.Resources.Hourglass);
|
||||
}
|
||||
|
||||
// Loads the image directly. This is needed by the background loader for some patches.
|
||||
public Bitmap LocalGetBitmap()
|
||||
{
|
||||
// Note: if this turns out to be too slow, do NOT try to make it use GetBitmap or _bitmap.
|
||||
// Create a cache for the local background loader thread instead.
|
||||
|
||||
LocalLoadResult result = LocalLoadImage();
|
||||
if (result.messages.Any(x => x.Type == ErrorType.Error))
|
||||
{
|
||||
return Properties.Resources.Failed;
|
||||
}
|
||||
ConvertImageFormat(result);
|
||||
return result.bitmap;
|
||||
}
|
||||
|
||||
public void LoadImage()
|
||||
{
|
||||
|
@ -254,235 +252,292 @@ namespace CodeImp.DoomBuilder.Data
|
|||
// This loads the image
|
||||
public virtual void LoadImage(bool notify)
|
||||
{
|
||||
// Do the loading
|
||||
LocalLoadImage();
|
||||
// Do the loading
|
||||
LocalLoadResult loadResult = LocalLoadImage();
|
||||
|
||||
// Notify the main thread about the change so that sectors can update their buffers
|
||||
if (notify) General.MainWindow.ImageDataLoaded(this.name);
|
||||
}
|
||||
|
||||
// This requests loading the image
|
||||
protected virtual void LocalLoadImage()
|
||||
{
|
||||
lock (this) lock (bitmap ?? bitmapLocker)
|
||||
ConvertImageFormat(loadResult);
|
||||
|
||||
General.MainWindow.RunOnUIThread(() =>
|
||||
{
|
||||
// Bitmap loaded successfully?
|
||||
if(bitmap != null)
|
||||
// Log errors and warnings
|
||||
foreach (LogMessage message in loadResult.messages)
|
||||
{
|
||||
General.ErrorLogger.Add(message.Type, message.Text);
|
||||
}
|
||||
|
||||
if (loadResult.messages.Any(x => x.Type == ErrorType.Error))
|
||||
{
|
||||
loadfailed = true;
|
||||
}
|
||||
|
||||
// Image is ready
|
||||
imagestate = ImageLoadState.Ready;
|
||||
_bitmap = loadResult.bitmap;
|
||||
if (loadResult.uiThreadWork != null)
|
||||
loadResult.uiThreadWork();
|
||||
});
|
||||
|
||||
// Notify the main thread about the change so that sectors can update their buffers
|
||||
if (notify) General.MainWindow.ImageDataLoaded(this.name);
|
||||
}
|
||||
|
||||
// This unloads the image
|
||||
public virtual void UnloadImage()
|
||||
{
|
||||
General.MainWindow.RunOnUIThread(() =>
|
||||
{
|
||||
_bitmap?.Dispose();
|
||||
_bitmap = null;
|
||||
imagestate = ImageLoadState.None;
|
||||
});
|
||||
}
|
||||
|
||||
protected class LocalLoadResult
|
||||
{
|
||||
public LocalLoadResult(Bitmap bitmap, string error = null, Action uiThreadWork = null)
|
||||
{
|
||||
this.bitmap = bitmap;
|
||||
messages = new List<LogMessage>();
|
||||
if (error != null)
|
||||
messages.Add(new LogMessage(ErrorType.Error, error));
|
||||
this.uiThreadWork = uiThreadWork;
|
||||
}
|
||||
|
||||
public LocalLoadResult(Bitmap bitmap, IEnumerable<LogMessage> messages, Action uiThreadWork = null)
|
||||
{
|
||||
this.bitmap = bitmap;
|
||||
this.messages = messages.ToList();
|
||||
this.uiThreadWork = uiThreadWork;
|
||||
}
|
||||
|
||||
public Bitmap bitmap;
|
||||
public List<LogMessage> messages;
|
||||
public Action uiThreadWork;
|
||||
}
|
||||
|
||||
protected abstract LocalLoadResult LocalLoadImage();
|
||||
|
||||
protected class LogMessage
|
||||
{
|
||||
public LogMessage(ErrorType type, string text) { Type = type; Text = text; }
|
||||
public ErrorType Type { get; set; }
|
||||
public string Text { get; set; }
|
||||
}
|
||||
|
||||
void ConvertImageFormat(LocalLoadResult loadResult)
|
||||
{
|
||||
// Bitmap loaded successfully?
|
||||
Bitmap bitmap = loadResult.bitmap;
|
||||
if(bitmap != null)
|
||||
{
|
||||
// Bitmap has incorrect format?
|
||||
if(bitmap.PixelFormat != PixelFormat.Format32bppArgb)
|
||||
{
|
||||
// Bitmap has incorrect format?
|
||||
if(bitmap.PixelFormat != PixelFormat.Format32bppArgb)
|
||||
{
|
||||
if(dynamictexture)
|
||||
throw new Exception("Dynamic images must be in 32 bits ARGB format.");
|
||||
if(dynamictexture)
|
||||
throw new Exception("Dynamic images must be in 32 bits ARGB format.");
|
||||
|
||||
//General.ErrorLogger.Add(ErrorType.Warning, "Image '" + name + "' does not have A8R8G8B8 pixel format. Conversion was needed.");
|
||||
Bitmap oldbitmap = bitmap;
|
||||
try
|
||||
{
|
||||
// Convert to desired pixel format
|
||||
bitmap = new Bitmap(oldbitmap.Size.Width, oldbitmap.Size.Height, PixelFormat.Format32bppArgb);
|
||||
Graphics g = Graphics.FromImage(bitmap);
|
||||
g.PageUnit = GraphicsUnit.Pixel;
|
||||
g.CompositingQuality = CompositingQuality.HighQuality;
|
||||
g.InterpolationMode = InterpolationMode.NearestNeighbor;
|
||||
g.SmoothingMode = SmoothingMode.None;
|
||||
g.PixelOffsetMode = PixelOffsetMode.None;
|
||||
g.Clear(Color.Transparent);
|
||||
g.DrawImage(oldbitmap, 0, 0, oldbitmap.Size.Width, oldbitmap.Size.Height);
|
||||
g.Dispose();
|
||||
oldbitmap.Dispose();
|
||||
}
|
||||
catch(Exception e)
|
||||
{
|
||||
bitmap = oldbitmap;
|
||||
General.ErrorLogger.Add(ErrorType.Warning, "Cannot lock image \"" + name + "\" for pixel format conversion. The image may not be displayed correctly.\n" + e.GetType().Name + ": " + e.Message);
|
||||
}
|
||||
//General.ErrorLogger.Add(ErrorType.Warning, "Image '" + name + "' does not have A8R8G8B8 pixel format. Conversion was needed.");
|
||||
Bitmap oldbitmap = bitmap;
|
||||
try
|
||||
{
|
||||
// Convert to desired pixel format
|
||||
bitmap = new Bitmap(oldbitmap.Size.Width, oldbitmap.Size.Height, PixelFormat.Format32bppArgb);
|
||||
Graphics g = Graphics.FromImage(bitmap);
|
||||
g.PageUnit = GraphicsUnit.Pixel;
|
||||
g.CompositingQuality = CompositingQuality.HighQuality;
|
||||
g.InterpolationMode = InterpolationMode.NearestNeighbor;
|
||||
g.SmoothingMode = SmoothingMode.None;
|
||||
g.PixelOffsetMode = PixelOffsetMode.None;
|
||||
g.Clear(Color.Transparent);
|
||||
g.DrawImage(oldbitmap, 0, 0, oldbitmap.Size.Width, oldbitmap.Size.Height);
|
||||
g.Dispose();
|
||||
oldbitmap.Dispose();
|
||||
}
|
||||
catch(Exception e)
|
||||
{
|
||||
bitmap = oldbitmap;
|
||||
loadResult.messages.Add(new LogMessage(ErrorType.Warning, "Cannot lock image \"" + name + "\" for pixel format conversion. The image may not be displayed correctly.\n" + e.GetType().Name + ": " + e.Message));
|
||||
}
|
||||
}
|
||||
|
||||
// This applies brightness correction on the image
|
||||
if(usecolorcorrection)
|
||||
// This applies brightness correction on the image
|
||||
if(usecolorcorrection)
|
||||
{
|
||||
BitmapData bmpdata = null;
|
||||
|
||||
try
|
||||
{
|
||||
// Try locking the bitmap
|
||||
bmpdata = bitmap.LockBits(new Rectangle(0, 0, bitmap.Size.Width, bitmap.Size.Height), ImageLockMode.ReadWrite, PixelFormat.Format32bppArgb);
|
||||
}
|
||||
catch(Exception e)
|
||||
{
|
||||
loadResult.messages.Add(new LogMessage(ErrorType.Warning, "Cannot lock image \"" + name + "\" for color correction. The image may not be displayed correctly.\n" + e.GetType().Name + ": " + e.Message));
|
||||
}
|
||||
|
||||
// Bitmap locked?
|
||||
if(bmpdata != null)
|
||||
{
|
||||
// Apply color correction
|
||||
PixelColor* pixels = (PixelColor*)(bmpdata.Scan0.ToPointer());
|
||||
General.Colors.ApplyColorCorrection(pixels, bmpdata.Width * bmpdata.Height);
|
||||
bitmap.UnlockBits(bmpdata);
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
// Loading failed
|
||||
// We still mark the image as ready so that it will
|
||||
// not try loading again until Reload Resources is used
|
||||
bitmap = new Bitmap(Properties.Resources.Failed);
|
||||
}
|
||||
|
||||
if(bitmap != null)
|
||||
{
|
||||
width = bitmap.Size.Width;
|
||||
height = bitmap.Size.Height;
|
||||
|
||||
if(dynamictexture)
|
||||
{
|
||||
if((width != General.NextPowerOf2(width)) || (height != General.NextPowerOf2(height)))
|
||||
throw new Exception("Dynamic images must have a size in powers of 2.");
|
||||
}
|
||||
|
||||
// Do we still have to set a scale?
|
||||
if((scale.x == 0.0f) && (scale.y == 0.0f))
|
||||
{
|
||||
if((General.Map != null) && (General.Map.Config != null))
|
||||
{
|
||||
scale.x = General.Map.Config.DefaultTextureScale;
|
||||
scale.y = General.Map.Config.DefaultTextureScale;
|
||||
}
|
||||
else
|
||||
{
|
||||
scale.x = 1.0f;
|
||||
scale.y = 1.0f;
|
||||
}
|
||||
}
|
||||
|
||||
if(!loadfailed)
|
||||
{
|
||||
//mxd. Check translucency and calculate average color?
|
||||
if(General.Map != null && General.Map.Data != null && General.Map.Data.GlowingFlats != null &&
|
||||
General.Map.Data.GlowingFlats.ContainsKey(longname) &&
|
||||
General.Map.Data.GlowingFlats[longname].CalculateTextureColor)
|
||||
{
|
||||
BitmapData bmpdata = null;
|
||||
|
||||
try
|
||||
{
|
||||
// Try locking the bitmap
|
||||
bmpdata = bitmap.LockBits(new Rectangle(0, 0, bitmap.Size.Width, bitmap.Size.Height), ImageLockMode.ReadWrite, PixelFormat.Format32bppArgb);
|
||||
}
|
||||
catch(Exception e)
|
||||
{
|
||||
General.ErrorLogger.Add(ErrorType.Warning, "Cannot lock image \"" + name + "\" for color correction. The image may not be displayed correctly.\n" + e.GetType().Name + ": " + e.Message);
|
||||
loadResult.messages.Add(new LogMessage(ErrorType.Error, "Cannot lock image \"" + this.filepathname + "\" for glow color calculation. " + e.GetType().Name + ": " + e.Message));
|
||||
}
|
||||
|
||||
// Bitmap locked?
|
||||
if(bmpdata != null)
|
||||
{
|
||||
// Apply color correction
|
||||
PixelColor* pixels = (PixelColor*)(bmpdata.Scan0.ToPointer());
|
||||
General.Colors.ApplyColorCorrection(pixels, bmpdata.Width * bmpdata.Height);
|
||||
int numpixels = bmpdata.Width * bmpdata.Height;
|
||||
uint r = 0;
|
||||
uint g = 0;
|
||||
uint b = 0;
|
||||
|
||||
for(PixelColor* cp = pixels + numpixels - 1; cp >= pixels; cp--)
|
||||
{
|
||||
r += cp->r;
|
||||
g += cp->g;
|
||||
b += cp->b;
|
||||
|
||||
// Also check alpha
|
||||
if(cp->a > 0 && cp->a < 255) istranslucent = true;
|
||||
else if(cp->a == 0) ismasked = true;
|
||||
}
|
||||
|
||||
// Update glow data
|
||||
int br = (int)(r / numpixels);
|
||||
int bg = (int)(g / numpixels);
|
||||
int bb = (int)(b / numpixels);
|
||||
|
||||
int max = Math.Max(br, Math.Max(bg, bb));
|
||||
|
||||
// Black can't glow...
|
||||
if(max == 0)
|
||||
{
|
||||
General.Map.Data.GlowingFlats.Remove(longname);
|
||||
}
|
||||
else
|
||||
{
|
||||
// That's how it's done in GZDoom (and I may be totally wrong about this)
|
||||
br = Math.Min(255, br * 153 / max);
|
||||
bg = Math.Min(255, bg * 153 / max);
|
||||
bb = Math.Min(255, bb * 153 / max);
|
||||
|
||||
General.Map.Data.GlowingFlats[longname].Color = new PixelColor(255, (byte)br, (byte)bg, (byte)bb);
|
||||
General.Map.Data.GlowingFlats[longname].CalculateTextureColor = false;
|
||||
if(!General.Map.Data.GlowingFlats[longname].Fullbright) General.Map.Data.GlowingFlats[longname].Brightness = (br + bg + bb) / 3;
|
||||
}
|
||||
|
||||
// Release the data
|
||||
bitmap.UnlockBits(bmpdata);
|
||||
}
|
||||
}
|
||||
//mxd. Check if the texture is translucent
|
||||
else
|
||||
{
|
||||
BitmapData bmpdata = null;
|
||||
try
|
||||
{
|
||||
bmpdata = bitmap.LockBits(new Rectangle(0, 0, bitmap.Size.Width, bitmap.Size.Height), ImageLockMode.ReadWrite, PixelFormat.Format32bppArgb);
|
||||
}
|
||||
catch(Exception e)
|
||||
{
|
||||
loadResult.messages.Add(new LogMessage(ErrorType.Error, "Cannot lock image \"" + this.filepathname + "\" for translucency check. " + e.GetType().Name + ": " + e.Message));
|
||||
}
|
||||
|
||||
if(bmpdata != null)
|
||||
{
|
||||
PixelColor* pixels = (PixelColor*)(bmpdata.Scan0.ToPointer());
|
||||
int numpixels = bmpdata.Width * bmpdata.Height;
|
||||
|
||||
for(PixelColor* cp = pixels + numpixels - 1; cp >= pixels; cp--)
|
||||
{
|
||||
// Check alpha
|
||||
if(cp->a > 0 && cp->a < 255) istranslucent = true;
|
||||
else if(cp->a == 0) ismasked = true;
|
||||
}
|
||||
|
||||
// Release the data
|
||||
bitmap.UnlockBits(bmpdata);
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
// Loading failed
|
||||
// We still mark the image as ready so that it will
|
||||
// not try loading again until Reload Resources is used
|
||||
loadfailed = true;
|
||||
bitmap = new Bitmap(Properties.Resources.Failed);
|
||||
}
|
||||
|
||||
if(bitmap != null)
|
||||
{
|
||||
width = bitmap.Size.Width;
|
||||
height = bitmap.Size.Height;
|
||||
|
||||
if(dynamictexture)
|
||||
{
|
||||
if((width != General.NextPowerOf2(width)) || (height != General.NextPowerOf2(height)))
|
||||
throw new Exception("Dynamic images must have a size in powers of 2.");
|
||||
}
|
||||
|
||||
// Do we still have to set a scale?
|
||||
if((scale.x == 0.0f) && (scale.y == 0.0f))
|
||||
{
|
||||
if((General.Map != null) && (General.Map.Config != null))
|
||||
{
|
||||
scale.x = General.Map.Config.DefaultTextureScale;
|
||||
scale.y = General.Map.Config.DefaultTextureScale;
|
||||
}
|
||||
else
|
||||
{
|
||||
scale.x = 1.0f;
|
||||
scale.y = 1.0f;
|
||||
}
|
||||
}
|
||||
|
||||
if(!loadfailed)
|
||||
{
|
||||
//mxd. Check translucency and calculate average color?
|
||||
if(General.Map != null && General.Map.Data != null && General.Map.Data.GlowingFlats != null &&
|
||||
General.Map.Data.GlowingFlats.ContainsKey(longname) &&
|
||||
General.Map.Data.GlowingFlats[longname].CalculateTextureColor)
|
||||
{
|
||||
BitmapData bmpdata = null;
|
||||
try
|
||||
{
|
||||
bmpdata = bitmap.LockBits(new Rectangle(0, 0, bitmap.Size.Width, bitmap.Size.Height), ImageLockMode.ReadWrite, PixelFormat.Format32bppArgb);
|
||||
}
|
||||
catch(Exception e)
|
||||
{
|
||||
General.ErrorLogger.Add(ErrorType.Error, "Cannot lock image \"" + this.filepathname + "\" for glow color calculation. " + e.GetType().Name + ": " + e.Message);
|
||||
}
|
||||
|
||||
if(bmpdata != null)
|
||||
{
|
||||
PixelColor* pixels = (PixelColor*)(bmpdata.Scan0.ToPointer());
|
||||
int numpixels = bmpdata.Width * bmpdata.Height;
|
||||
uint r = 0;
|
||||
uint g = 0;
|
||||
uint b = 0;
|
||||
|
||||
for(PixelColor* cp = pixels + numpixels - 1; cp >= pixels; cp--)
|
||||
{
|
||||
r += cp->r;
|
||||
g += cp->g;
|
||||
b += cp->b;
|
||||
|
||||
// Also check alpha
|
||||
if(cp->a > 0 && cp->a < 255) istranslucent = true;
|
||||
else if(cp->a == 0) ismasked = true;
|
||||
}
|
||||
|
||||
// Update glow data
|
||||
int br = (int)(r / numpixels);
|
||||
int bg = (int)(g / numpixels);
|
||||
int bb = (int)(b / numpixels);
|
||||
|
||||
int max = Math.Max(br, Math.Max(bg, bb));
|
||||
|
||||
// Black can't glow...
|
||||
if(max == 0)
|
||||
{
|
||||
General.Map.Data.GlowingFlats.Remove(longname);
|
||||
}
|
||||
else
|
||||
{
|
||||
// That's how it's done in GZDoom (and I may be totally wrong about this)
|
||||
br = Math.Min(255, br * 153 / max);
|
||||
bg = Math.Min(255, bg * 153 / max);
|
||||
bb = Math.Min(255, bb * 153 / max);
|
||||
|
||||
General.Map.Data.GlowingFlats[longname].Color = new PixelColor(255, (byte)br, (byte)bg, (byte)bb);
|
||||
General.Map.Data.GlowingFlats[longname].CalculateTextureColor = false;
|
||||
if(!General.Map.Data.GlowingFlats[longname].Fullbright) General.Map.Data.GlowingFlats[longname].Brightness = (br + bg + bb) / 3;
|
||||
}
|
||||
|
||||
// Release the data
|
||||
bitmap.UnlockBits(bmpdata);
|
||||
}
|
||||
}
|
||||
//mxd. Check if the texture is translucent
|
||||
else
|
||||
{
|
||||
BitmapData bmpdata = null;
|
||||
try
|
||||
{
|
||||
bmpdata = bitmap.LockBits(new Rectangle(0, 0, bitmap.Size.Width, bitmap.Size.Height), ImageLockMode.ReadWrite, PixelFormat.Format32bppArgb);
|
||||
}
|
||||
catch(Exception e)
|
||||
{
|
||||
General.ErrorLogger.Add(ErrorType.Error, "Cannot lock image \"" + this.filepathname + "\" for translucency check. " + e.GetType().Name + ": " + e.Message);
|
||||
}
|
||||
|
||||
if(bmpdata != null)
|
||||
{
|
||||
PixelColor* pixels = (PixelColor*)(bmpdata.Scan0.ToPointer());
|
||||
int numpixels = bmpdata.Width * bmpdata.Height;
|
||||
|
||||
for(PixelColor* cp = pixels + numpixels - 1; cp >= pixels; cp--)
|
||||
{
|
||||
// Check alpha
|
||||
if(cp->a > 0 && cp->a < 255) istranslucent = true;
|
||||
else if(cp->a == 0) ismasked = true;
|
||||
}
|
||||
|
||||
// Release the data
|
||||
bitmap.UnlockBits(bmpdata);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Image is ready
|
||||
imagestate = ImageLoadState.Ready;
|
||||
}
|
||||
|
||||
loadResult.bitmap = bitmap;
|
||||
}
|
||||
|
||||
// This creates the Direct3D texture
|
||||
public virtual void CreateTexture()
|
||||
{
|
||||
lock (this) lock (bitmap ?? bitmapLocker)
|
||||
{
|
||||
// Only do this when texture is not created yet
|
||||
if(((texture == null) || (texture.Disposed)) && this.IsImageLoaded && !loadfailed)
|
||||
{
|
||||
Bitmap img = bitmap;
|
||||
if(loadfailed) img = Properties.Resources.Failed;
|
||||
// Only do this when texture is not created yet
|
||||
if(((texture == null) || (texture.Disposed)) && this.IsImageLoaded && !loadfailed)
|
||||
{
|
||||
Bitmap img = bitmap;
|
||||
if(loadfailed) img = Properties.Resources.Failed;
|
||||
|
||||
texture = new Texture(General.Map.Graphics, img);
|
||||
texture = new Texture(General.Map.Graphics, img);
|
||||
|
||||
if(dynamictexture)
|
||||
{
|
||||
if((width != texture.Width) || (height != texture.Height))
|
||||
throw new Exception("Could not create a texture with the same size as the image.");
|
||||
}
|
||||
if(dynamictexture)
|
||||
{
|
||||
if((width != texture.Width) || (height != texture.Height))
|
||||
throw new Exception("Could not create a texture with the same size as the image.");
|
||||
}
|
||||
|
||||
#if DEBUG
|
||||
texture.Tag = name; //mxd. Helps with tracking undisposed resources...
|
||||
texture.Tag = name; //mxd. Helps with tracking undisposed resources...
|
||||
#endif
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -492,77 +547,65 @@ namespace CodeImp.DoomBuilder.Data
|
|||
if(!dynamictexture)
|
||||
throw new Exception("The image must be a dynamic image to support direct updating.");
|
||||
|
||||
lock (this) lock (bitmap ?? bitmapLocker)
|
||||
{
|
||||
if((texture != null) && !texture.Disposed)
|
||||
{
|
||||
General.Map.Graphics.SetPixels(texture, bitmap);
|
||||
}
|
||||
if((texture != null) && !texture.Disposed)
|
||||
{
|
||||
General.Map.Graphics.SetPixels(texture, bitmap);
|
||||
}
|
||||
}
|
||||
|
||||
// This destroys the Direct3D texture
|
||||
public void ReleaseTexture()
|
||||
{
|
||||
lock (this) lock (bitmap ?? bitmapLocker)
|
||||
{
|
||||
// Trash it
|
||||
if(texture != null) texture.Dispose();
|
||||
texture = null;
|
||||
}
|
||||
// Trash it
|
||||
if(texture != null) texture.Dispose();
|
||||
texture = null;
|
||||
}
|
||||
|
||||
// This draws a preview
|
||||
public virtual void DrawPreview(Graphics target, Point targetpos)
|
||||
{
|
||||
lock (this) lock (bitmap ?? bitmapLocker)
|
||||
{
|
||||
// Preview ready?
|
||||
if(!loadfailed && (previewstate == ImageLoadState.Ready))
|
||||
{
|
||||
// Draw preview
|
||||
General.Map.Data.Previews.DrawPreview(previewindex, target, targetpos);
|
||||
}
|
||||
// Loading failed?
|
||||
else if(loadfailed)
|
||||
{
|
||||
// Draw error bitmap
|
||||
targetpos = new Point(targetpos.X + ((PreviewManager.MAX_PREVIEW_SIZE - Properties.Resources.Hourglass.Width) >> 1),
|
||||
targetpos.Y + ((PreviewManager.MAX_PREVIEW_SIZE - Properties.Resources.Hourglass.Height) >> 1));
|
||||
target.DrawImageUnscaled(Properties.Resources.Failed, targetpos);
|
||||
}
|
||||
else
|
||||
{
|
||||
// Draw loading bitmap
|
||||
targetpos = new Point(targetpos.X + ((PreviewManager.MAX_PREVIEW_SIZE - Properties.Resources.Hourglass.Width) >> 1),
|
||||
targetpos.Y + ((PreviewManager.MAX_PREVIEW_SIZE - Properties.Resources.Hourglass.Height) >> 1));
|
||||
target.DrawImageUnscaled(Properties.Resources.Hourglass, targetpos);
|
||||
}
|
||||
// Preview ready?
|
||||
if(!loadfailed && (previewstate == ImageLoadState.Ready))
|
||||
{
|
||||
// Draw preview
|
||||
General.Map.Data.Previews.DrawPreview(previewindex, target, targetpos);
|
||||
}
|
||||
// Loading failed?
|
||||
else if(loadfailed)
|
||||
{
|
||||
// Draw error bitmap
|
||||
targetpos = new Point(targetpos.X + ((PreviewManager.MAX_PREVIEW_SIZE - Properties.Resources.Hourglass.Width) >> 1),
|
||||
targetpos.Y + ((PreviewManager.MAX_PREVIEW_SIZE - Properties.Resources.Hourglass.Height) >> 1));
|
||||
target.DrawImageUnscaled(Properties.Resources.Failed, targetpos);
|
||||
}
|
||||
else
|
||||
{
|
||||
// Draw loading bitmap
|
||||
targetpos = new Point(targetpos.X + ((PreviewManager.MAX_PREVIEW_SIZE - Properties.Resources.Hourglass.Width) >> 1),
|
||||
targetpos.Y + ((PreviewManager.MAX_PREVIEW_SIZE - Properties.Resources.Hourglass.Height) >> 1));
|
||||
target.DrawImageUnscaled(Properties.Resources.Hourglass, targetpos);
|
||||
}
|
||||
}
|
||||
|
||||
// This returns a preview image
|
||||
public virtual Image GetPreview()
|
||||
{
|
||||
lock (this) lock (bitmap ?? bitmapLocker)
|
||||
{
|
||||
// Preview ready?
|
||||
if(previewstate == ImageLoadState.Ready)
|
||||
{
|
||||
// Make a copy
|
||||
return General.Map.Data.Previews.GetPreviewCopy(previewindex);
|
||||
}
|
||||
|
||||
// Loading failed?
|
||||
if(loadfailed)
|
||||
{
|
||||
// Return error bitmap
|
||||
return Properties.Resources.Failed;
|
||||
}
|
||||
|
||||
// Return loading bitmap
|
||||
return Properties.Resources.Hourglass;
|
||||
// Preview ready?
|
||||
if(previewstate == ImageLoadState.Ready)
|
||||
{
|
||||
// Make a copy
|
||||
return General.Map.Data.Previews.GetPreviewCopy(previewindex);
|
||||
}
|
||||
|
||||
// Loading failed?
|
||||
if(loadfailed)
|
||||
{
|
||||
// Return error bitmap
|
||||
return Properties.Resources.Failed;
|
||||
}
|
||||
|
||||
// Return loading bitmap
|
||||
return Properties.Resources.Hourglass;
|
||||
}
|
||||
|
||||
//mxd. This greatly speeds up Dictionary lookups
|
||||
|
|
|
@ -17,6 +17,7 @@
|
|||
#region ================== Namespaces
|
||||
|
||||
using System;
|
||||
using System.Drawing;
|
||||
using System.IO;
|
||||
using CodeImp.DoomBuilder.Controls;
|
||||
using CodeImp.DoomBuilder.IO;
|
||||
|
@ -109,59 +110,46 @@ namespace CodeImp.DoomBuilder.Data
|
|||
}
|
||||
|
||||
// This loads the image
|
||||
protected override void LocalLoadImage()
|
||||
protected override LocalLoadResult LocalLoadImage()
|
||||
{
|
||||
// Leave when already loaded
|
||||
if(this.IsImageLoaded) return;
|
||||
// Load file data
|
||||
Bitmap bitmap = null;
|
||||
string error = null;
|
||||
MemoryStream filedata = datareader.LoadFile(filepathname); //mxd
|
||||
|
||||
lock(this)
|
||||
isBadForLongTextureNames = false;
|
||||
|
||||
if (filedata != null)
|
||||
{
|
||||
// Load file data
|
||||
if(bitmap != null) bitmap.Dispose(); bitmap = null;
|
||||
MemoryStream filedata = datareader.LoadFile(filepathname); //mxd
|
||||
|
||||
isBadForLongTextureNames = false;
|
||||
|
||||
if (filedata != null)
|
||||
// Get a reader for the data
|
||||
IImageReader reader = ImageDataFormat.GetImageReader(filedata, probableformat, General.Map.Data.Palette);
|
||||
if(!(reader is UnknownImageReader))
|
||||
{
|
||||
// Get a reader for the data
|
||||
IImageReader reader = ImageDataFormat.GetImageReader(filedata, probableformat, General.Map.Data.Palette);
|
||||
if(!(reader is UnknownImageReader))
|
||||
// Load the image
|
||||
filedata.Seek(0, SeekOrigin.Begin);
|
||||
try
|
||||
{
|
||||
// Load the image
|
||||
filedata.Seek(0, SeekOrigin.Begin);
|
||||
try
|
||||
{
|
||||
bitmap = reader.ReadAsBitmap(filedata);
|
||||
}
|
||||
catch(InvalidDataException)
|
||||
{
|
||||
// Data cannot be read!
|
||||
bitmap = null;
|
||||
}
|
||||
bitmap = reader.ReadAsBitmap(filedata);
|
||||
}
|
||||
|
||||
// Not loaded?
|
||||
if(bitmap == null)
|
||||
catch(InvalidDataException)
|
||||
{
|
||||
General.ErrorLogger.Add(ErrorType.Error, "Image file \"" + filepathname + "\" data format could not be read, while loading texture \"" + this.Name + "\"");
|
||||
loadfailed = true;
|
||||
// Data cannot be read!
|
||||
bitmap = null;
|
||||
}
|
||||
else
|
||||
{
|
||||
// Get width and height from image
|
||||
width = bitmap.Size.Width;
|
||||
height = bitmap.Size.Height;
|
||||
}
|
||||
|
||||
filedata.Dispose();
|
||||
}
|
||||
|
||||
// Pass on to base
|
||||
base.LocalLoadImage();
|
||||
// Not loaded?
|
||||
if(bitmap == null)
|
||||
{
|
||||
error = "Image file \"" + filepathname + "\" data format could not be read, while loading texture \"" + this.Name + "\"";
|
||||
}
|
||||
|
||||
filedata.Dispose();
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
|
||||
return new LocalLoadResult(bitmap, error);
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
|
|
|
@ -69,28 +69,21 @@ namespace CodeImp.DoomBuilder.Data
|
|||
#region ================== Methods
|
||||
|
||||
// This loads the image
|
||||
protected override void LocalLoadImage()
|
||||
protected override LocalLoadResult LocalLoadImage()
|
||||
{
|
||||
if(IsImageLoaded) return; //mxd. ResourceImages can't be unloaded, so no need to reload them.
|
||||
|
||||
lock(this)
|
||||
{
|
||||
// No failure checking here. If anything fails here, it is not the user's fault,
|
||||
// because the resources this loads are in the assembly.
|
||||
// No failure checking here. If anything fails here, it is not the user's fault,
|
||||
// because the resources this loads are in the assembly.
|
||||
|
||||
// Get resource from memory
|
||||
Stream bitmapdata = assembly.GetManifestResourceStream(resourcename);
|
||||
if(bitmap != null) bitmap.Dispose();
|
||||
bitmap = (Bitmap)Image.FromStream(bitmapdata);
|
||||
bitmapdata.Dispose();
|
||||
|
||||
// Pass on to base
|
||||
base.LocalLoadImage();
|
||||
}
|
||||
}
|
||||
// Get resource from memory
|
||||
Stream bitmapdata = assembly.GetManifestResourceStream(resourcename);
|
||||
var bitmap = (Bitmap)Image.FromStream(bitmapdata);
|
||||
bitmapdata.Dispose();
|
||||
|
||||
//mxd
|
||||
public override Image GetPreview()
|
||||
return new LocalLoadResult(bitmap);
|
||||
}
|
||||
|
||||
//mxd
|
||||
public override Image GetPreview()
|
||||
{
|
||||
return base.GetBitmap();
|
||||
}
|
||||
|
|
|
@ -17,6 +17,7 @@
|
|||
#region ================== Namespaces
|
||||
|
||||
using System;
|
||||
using System.Drawing;
|
||||
using System.IO;
|
||||
using CodeImp.DoomBuilder.IO;
|
||||
|
||||
|
@ -57,70 +58,56 @@ namespace CodeImp.DoomBuilder.Data
|
|||
#region ================== Methods
|
||||
|
||||
// This loads the image
|
||||
protected override void LocalLoadImage()
|
||||
protected override LocalLoadResult LocalLoadImage()
|
||||
{
|
||||
// Checks
|
||||
if(this.IsImageLoaded) return;
|
||||
|
||||
lock(this)
|
||||
// Get the patch data stream
|
||||
Bitmap bitmap = null;
|
||||
string error = null;
|
||||
string patchlocation = string.Empty; //mxd
|
||||
Stream patchdata = General.Map.Data.GetTextureData(lumpname, hasLongName, ref patchlocation);
|
||||
if(patchdata != null)
|
||||
{
|
||||
// Get the patch data stream
|
||||
if(bitmap != null) bitmap.Dispose(); bitmap = null;
|
||||
string patchlocation = string.Empty; //mxd
|
||||
Stream patchdata = General.Map.Data.GetTextureData(lumpname, hasLongName, ref patchlocation);
|
||||
if(patchdata != null)
|
||||
{
|
||||
// Copy patch data to memory
|
||||
byte[] membytes = new byte[(int)patchdata.Length];
|
||||
// Copy patch data to memory
|
||||
byte[] membytes = new byte[(int)patchdata.Length];
|
||||
|
||||
lock(patchdata) //mxd
|
||||
{
|
||||
patchdata.Seek(0, SeekOrigin.Begin);
|
||||
patchdata.Read(membytes, 0, (int)patchdata.Length);
|
||||
}
|
||||
lock(patchdata) //mxd
|
||||
{
|
||||
patchdata.Seek(0, SeekOrigin.Begin);
|
||||
patchdata.Read(membytes, 0, (int)patchdata.Length);
|
||||
}
|
||||
|
||||
MemoryStream mem = new MemoryStream(membytes);
|
||||
mem.Seek(0, SeekOrigin.Begin);
|
||||
MemoryStream mem = new MemoryStream(membytes);
|
||||
mem.Seek(0, SeekOrigin.Begin);
|
||||
|
||||
// Get a reader for the data
|
||||
IImageReader reader = ImageDataFormat.GetImageReader(mem, ImageDataFormat.DOOMPICTURE, General.Map.Data.Palette);
|
||||
if(!(reader is UnknownImageReader))
|
||||
{
|
||||
// Load the image
|
||||
mem.Seek(0, SeekOrigin.Begin);
|
||||
try { bitmap = reader.ReadAsBitmap(mem); }
|
||||
catch(InvalidDataException)
|
||||
{
|
||||
// Data cannot be read!
|
||||
bitmap = null;
|
||||
}
|
||||
}
|
||||
|
||||
// Not loaded?
|
||||
if(bitmap == null)
|
||||
{
|
||||
General.ErrorLogger.Add(ErrorType.Error, "Image lump \"" + Path.Combine(patchlocation, lumpname) + "\" data format could not be read, while loading texture \"" + this.Name + "\". Does this lump contain valid picture data at all?");
|
||||
loadfailed = true;
|
||||
}
|
||||
else
|
||||
{
|
||||
// Get width and height from image
|
||||
width = bitmap.Size.Width;
|
||||
height = bitmap.Size.Height;
|
||||
}
|
||||
|
||||
// Done
|
||||
mem.Dispose();
|
||||
}
|
||||
else
|
||||
// Get a reader for the data
|
||||
IImageReader reader = ImageDataFormat.GetImageReader(mem, ImageDataFormat.DOOMPICTURE, General.Map.Data.Palette);
|
||||
if(!(reader is UnknownImageReader))
|
||||
{
|
||||
General.ErrorLogger.Add(ErrorType.Error, "Image lump \"" + lumpname + "\" could not be found, while loading texture \"" + this.Name + "\". Did you forget to include required resources?");
|
||||
loadfailed = true;
|
||||
// Load the image
|
||||
mem.Seek(0, SeekOrigin.Begin);
|
||||
try { bitmap = reader.ReadAsBitmap(mem); }
|
||||
catch(InvalidDataException)
|
||||
{
|
||||
// Data cannot be read!
|
||||
bitmap = null;
|
||||
}
|
||||
}
|
||||
|
||||
// Pass on to base
|
||||
base.LocalLoadImage();
|
||||
|
||||
// Not loaded?
|
||||
if(bitmap == null)
|
||||
{
|
||||
error = "Image lump \"" + Path.Combine(patchlocation, lumpname) + "\" data format could not be read, while loading texture \"" + this.Name + "\". Does this lump contain valid picture data at all?";
|
||||
}
|
||||
|
||||
// Done
|
||||
mem.Dispose();
|
||||
}
|
||||
else
|
||||
{
|
||||
error = "Image lump \"" + lumpname + "\" could not be found, while loading texture \"" + this.Name + "\". Did you forget to include required resources?";
|
||||
}
|
||||
|
||||
return new LocalLoadResult(bitmap, error);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
|
|
@ -17,6 +17,7 @@
|
|||
#region ================== Namespaces
|
||||
|
||||
using System;
|
||||
using System.Drawing;
|
||||
using System.IO;
|
||||
using System.Runtime.InteropServices;
|
||||
using CodeImp.DoomBuilder.IO;
|
||||
|
@ -68,87 +69,73 @@ namespace CodeImp.DoomBuilder.Data
|
|||
override public void LoadImage(bool notify)
|
||||
{
|
||||
// Do the loading
|
||||
LocalLoadImage();
|
||||
base.LoadImage(false);
|
||||
|
||||
// Notify the main thread about the change to redraw display
|
||||
if (notify) General.MainWindow.SpriteDataLoaded(this.Name);
|
||||
}
|
||||
|
||||
// This loads the image
|
||||
protected override void LocalLoadImage()
|
||||
protected override LocalLoadResult LocalLoadImage()
|
||||
{
|
||||
// Leave when already loaded
|
||||
if(this.IsImageLoaded) return;
|
||||
|
||||
lock(this)
|
||||
// Get the lump data stream
|
||||
Bitmap bitmap = null;
|
||||
string error = null;
|
||||
string spritelocation = string.Empty; //mxd
|
||||
Stream lumpdata = General.Map.Data.GetSpriteData(Name, ref spritelocation);
|
||||
if(lumpdata != null)
|
||||
{
|
||||
// Get the lump data stream
|
||||
string spritelocation = string.Empty; //mxd
|
||||
Stream lumpdata = General.Map.Data.GetSpriteData(Name, ref spritelocation);
|
||||
if(lumpdata != null)
|
||||
// Copy lump data to memory
|
||||
byte[] membytes = new byte[(int)lumpdata.Length];
|
||||
|
||||
lock(lumpdata) //mxd
|
||||
{
|
||||
// Copy lump data to memory
|
||||
byte[] membytes = new byte[(int)lumpdata.Length];
|
||||
|
||||
lock(lumpdata) //mxd
|
||||
{
|
||||
lumpdata.Seek(0, SeekOrigin.Begin);
|
||||
lumpdata.Read(membytes, 0, (int)lumpdata.Length);
|
||||
}
|
||||
lumpdata.Seek(0, SeekOrigin.Begin);
|
||||
lumpdata.Read(membytes, 0, (int)lumpdata.Length);
|
||||
}
|
||||
|
||||
MemoryStream mem = new MemoryStream(membytes);
|
||||
mem.Seek(0, SeekOrigin.Begin);
|
||||
MemoryStream mem = new MemoryStream(membytes);
|
||||
mem.Seek(0, SeekOrigin.Begin);
|
||||
|
||||
// Get a reader for the data
|
||||
IImageReader reader = ImageDataFormat.GetImageReader(mem, ImageDataFormat.DOOMPICTURE, General.Map.Data.Palette);
|
||||
if(reader is UnknownImageReader)
|
||||
{
|
||||
// Data is in an unknown format!
|
||||
General.ErrorLogger.Add(ErrorType.Error, "Sprite lump \"" + Path.Combine(spritelocation, Name) + "\" data format could not be read. Does this lump contain valid picture data at all?");
|
||||
bitmap = null;
|
||||
}
|
||||
else
|
||||
{
|
||||
// Read data as bitmap
|
||||
mem.Seek(0, SeekOrigin.Begin);
|
||||
if(bitmap != null) bitmap.Dispose();
|
||||
bitmap = reader.ReadAsBitmap(mem, out offsetx, out offsety);
|
||||
}
|
||||
|
||||
// Done
|
||||
mem.Dispose();
|
||||
|
||||
if(bitmap != null)
|
||||
{
|
||||
// Get width and height from image
|
||||
width = bitmap.Size.Width;
|
||||
height = bitmap.Size.Height;
|
||||
scale.x = 1.0f;
|
||||
scale.y = 1.0f;
|
||||
|
||||
// Make offset corrections if the offset was not given
|
||||
if((offsetx == int.MinValue) || (offsety == int.MinValue))
|
||||
{
|
||||
offsetx = (int)((width * scale.x) * 0.5f);
|
||||
offsety = (int)(height * scale.y);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
loadfailed = true;
|
||||
}
|
||||
// Get a reader for the data
|
||||
IImageReader reader = ImageDataFormat.GetImageReader(mem, ImageDataFormat.DOOMPICTURE, General.Map.Data.Palette);
|
||||
if(reader is UnknownImageReader)
|
||||
{
|
||||
// Data is in an unknown format!
|
||||
error = "Sprite lump \"" + Path.Combine(spritelocation, Name) + "\" data format could not be read. Does this lump contain valid picture data at all?";
|
||||
bitmap = null;
|
||||
}
|
||||
else
|
||||
{
|
||||
// Missing a patch lump!
|
||||
General.ErrorLogger.Add(ErrorType.Error, "Missing sprite lump \"" + Name + "\". Forgot to include required resources?");
|
||||
// Read data as bitmap
|
||||
mem.Seek(0, SeekOrigin.Begin);
|
||||
if(bitmap != null) bitmap.Dispose();
|
||||
bitmap = reader.ReadAsBitmap(mem, out offsetx, out offsety);
|
||||
}
|
||||
|
||||
// Pass on to base
|
||||
base.LocalLoadImage();
|
||||
|
||||
// Done
|
||||
mem.Dispose();
|
||||
}
|
||||
else
|
||||
{
|
||||
// Missing a patch lump!
|
||||
error = "Missing sprite lump \"" + Name + "\". Forgot to include required resources?";
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
return new LocalLoadResult(bitmap, error, () =>
|
||||
{
|
||||
scale.x = 1.0f;
|
||||
scale.y = 1.0f;
|
||||
|
||||
// Make offset corrections if the offset was not given
|
||||
if ((offsetx == int.MinValue) || (offsety == int.MinValue))
|
||||
{
|
||||
offsetx = (int)((width * scale.x) * 0.5f);
|
||||
offsety = (int)(height * scale.y);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
|
|
|
@ -21,6 +21,7 @@ using System.Collections.Generic;
|
|||
using System.Drawing;
|
||||
using System.Drawing.Imaging;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using CodeImp.DoomBuilder.Controls;
|
||||
using CodeImp.DoomBuilder.IO;
|
||||
using CodeImp.DoomBuilder.Rendering;
|
||||
|
@ -98,167 +99,158 @@ namespace CodeImp.DoomBuilder.Data
|
|||
}
|
||||
|
||||
// This loads the image
|
||||
protected override void LocalLoadImage()
|
||||
protected override LocalLoadResult LocalLoadImage()
|
||||
{
|
||||
// Checks
|
||||
if(this.IsImageLoaded || width == 0 || height == 0) return;
|
||||
if(width == 0 || height == 0) return new LocalLoadResult(null);
|
||||
|
||||
Graphics g = null;
|
||||
|
||||
lock(this)
|
||||
|
||||
Bitmap bitmap = null;
|
||||
List<LogMessage> messages = new List<LogMessage>();
|
||||
|
||||
// Create texture bitmap
|
||||
try
|
||||
{
|
||||
// Create texture bitmap
|
||||
try
|
||||
{
|
||||
if(bitmap != null) bitmap.Dispose();
|
||||
bitmap = new Bitmap(width, height, PixelFormat.Format32bppArgb);
|
||||
BitmapData bitmapdata = bitmap.LockBits(new Rectangle(0, 0, width, height), ImageLockMode.WriteOnly, PixelFormat.Format32bppArgb);
|
||||
PixelColor* pixels = (PixelColor*)bitmapdata.Scan0.ToPointer();
|
||||
General.ZeroMemory(new IntPtr(pixels), width * height * sizeof(PixelColor));
|
||||
bitmap.UnlockBits(bitmapdata);
|
||||
g = Graphics.FromImage(bitmap);
|
||||
}
|
||||
catch(Exception e)
|
||||
{
|
||||
// Unable to make bitmap
|
||||
General.ErrorLogger.Add(ErrorType.Error, "Unable to load texture image \"" + this.Name + "\". " + e.GetType().Name + ": " + e.Message);
|
||||
loadfailed = true;
|
||||
}
|
||||
|
||||
int missingpatches = 0; //mxd
|
||||
if(patches.Count == 0) //mxd
|
||||
{
|
||||
//mxd. Empty image will suffice here, I suppose...
|
||||
if(nulltexture)
|
||||
{
|
||||
base.LocalLoadImage();
|
||||
return;
|
||||
}
|
||||
|
||||
// No patches!
|
||||
General.ErrorLogger.Add(ErrorType.Warning, "No patches are defined for texture \"" + this.Name + "\"");
|
||||
loadfailed = true;
|
||||
}
|
||||
else if(!loadfailed)
|
||||
{
|
||||
// Go for all patches
|
||||
foreach(TexturePatch p in patches)
|
||||
{
|
||||
//mxd. Some patches (like "TNT1A0") should be skipped
|
||||
if(p.Skip) continue;
|
||||
|
||||
// Get the patch data stream
|
||||
string patchlocation = string.Empty; //mxd
|
||||
Stream patchdata = General.Map.Data.GetPatchData(p.LumpName, p.HasLongName, ref patchlocation);
|
||||
if(patchdata != null)
|
||||
{
|
||||
// Copy patch data to memory
|
||||
byte[] membytes = new byte[(int)patchdata.Length];
|
||||
|
||||
lock(patchdata) //mxd
|
||||
{
|
||||
patchdata.Seek(0, SeekOrigin.Begin);
|
||||
patchdata.Read(membytes, 0, (int)patchdata.Length);
|
||||
}
|
||||
|
||||
MemoryStream mem = new MemoryStream(membytes);
|
||||
mem.Seek(0, SeekOrigin.Begin);
|
||||
|
||||
// Get a reader for the data
|
||||
IImageReader reader = ImageDataFormat.GetImageReader(mem, ImageDataFormat.DOOMPICTURE, General.Map.Data.Palette);
|
||||
if(reader is UnknownImageReader)
|
||||
{
|
||||
//mxd. Probably that's a flat?..
|
||||
if(General.Map.Config.MixTexturesFlats)
|
||||
{
|
||||
reader = ImageDataFormat.GetImageReader(mem, ImageDataFormat.DOOMFLAT, General.Map.Data.Palette);
|
||||
}
|
||||
|
||||
if(reader is UnknownImageReader)
|
||||
{
|
||||
// Data is in an unknown format!
|
||||
if(!nulltexture) General.ErrorLogger.Add((optional ? ErrorType.Warning : ErrorType.Error), "Patch lump \"" + Path.Combine(patchlocation, p.LumpName) + "\" data format could not be read, while loading texture \"" + this.Name + "\"");
|
||||
missingpatches++; //mxd
|
||||
}
|
||||
}
|
||||
|
||||
if(!(reader is UnknownImageReader))
|
||||
{
|
||||
// Get the patch
|
||||
mem.Seek(0, SeekOrigin.Begin);
|
||||
Bitmap patchbmp = null;
|
||||
try { patchbmp = reader.ReadAsBitmap(mem); }
|
||||
catch(InvalidDataException)
|
||||
{
|
||||
// Data cannot be read!
|
||||
if(!nulltexture) General.ErrorLogger.Add((optional ? ErrorType.Warning : ErrorType.Error), "Patch lump \"" + p.LumpName + "\" data format could not be read, while loading texture \"" + this.Name + "\"");
|
||||
missingpatches++; //mxd
|
||||
}
|
||||
|
||||
if(patchbmp != null)
|
||||
{
|
||||
//mxd. Apply transformations from TexturePatch
|
||||
patchbmp = TransformPatch(p, patchbmp);
|
||||
|
||||
// Draw the patch on the texture image
|
||||
Rectangle tgtrect = new Rectangle(p.X, p.Y, patchbmp.Size.Width, patchbmp.Size.Height);
|
||||
g.DrawImageUnscaledAndClipped(patchbmp, tgtrect);
|
||||
patchbmp.Dispose();
|
||||
}
|
||||
}
|
||||
|
||||
// Done
|
||||
mem.Dispose();
|
||||
}
|
||||
else
|
||||
{
|
||||
//mxd. ZDoom can use any known graphic as patch
|
||||
if(General.Map.Config.MixTexturesFlats)
|
||||
{
|
||||
ImageData img = General.Map.Data.GetTextureImage(p.LumpName);
|
||||
if(!(img is UnknownImage) && img != this)
|
||||
{
|
||||
if(!img.IsImageLoaded) img.LoadImage();
|
||||
|
||||
//mxd. Apply transformations from TexturePatch. We don't want to modify the original bitmap here, so make a copy
|
||||
Bitmap bmp = img.GetBitmap();
|
||||
Bitmap patchbmp;
|
||||
lock (bmp)
|
||||
{
|
||||
patchbmp = TransformPatch(p, new Bitmap(bmp));
|
||||
}
|
||||
|
||||
// Draw the patch on the texture image
|
||||
Rectangle tgtrect = new Rectangle(p.X, p.Y, patchbmp.Size.Width, patchbmp.Size.Height);
|
||||
g.DrawImageUnscaledAndClipped(patchbmp, tgtrect);
|
||||
patchbmp.Dispose();
|
||||
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
// Missing a patch lump!
|
||||
if(!nulltexture) General.ErrorLogger.Add((optional ? ErrorType.Warning : ErrorType.Error), "Missing patch lump \"" + p.LumpName + "\" while loading texture \"" + this.Name + "\"");
|
||||
missingpatches++; //mxd
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Dispose bitmap if load failed
|
||||
if(!nulltexture && (bitmap != null) && (loadfailed || missingpatches >= patches.Count)) //mxd. We can still display texture if at least one of the patches was loaded
|
||||
{
|
||||
bitmap.Dispose();
|
||||
bitmap = null;
|
||||
loadfailed = true;
|
||||
}
|
||||
|
||||
// Pass on to base
|
||||
base.LocalLoadImage();
|
||||
if(bitmap != null) bitmap.Dispose();
|
||||
bitmap = new Bitmap(width, height, PixelFormat.Format32bppArgb);
|
||||
BitmapData bitmapdata = bitmap.LockBits(new Rectangle(0, 0, width, height), ImageLockMode.WriteOnly, PixelFormat.Format32bppArgb);
|
||||
PixelColor* pixels = (PixelColor*)bitmapdata.Scan0.ToPointer();
|
||||
General.ZeroMemory(new IntPtr(pixels), width * height * sizeof(PixelColor));
|
||||
bitmap.UnlockBits(bitmapdata);
|
||||
g = Graphics.FromImage(bitmap);
|
||||
}
|
||||
catch(Exception e)
|
||||
{
|
||||
// Unable to make bitmap
|
||||
messages.Add(new LogMessage(ErrorType.Warning, "Unable to load texture image \"" + this.Name + "\". " + e.GetType().Name + ": " + e.Message));
|
||||
}
|
||||
}
|
||||
|
||||
//mxd
|
||||
private Bitmap TransformPatch(TexturePatch p, Bitmap patchbmp)
|
||||
int missingpatches = 0; //mxd
|
||||
if(patches.Count == 0) //mxd
|
||||
{
|
||||
//mxd. Empty image will suffice here, I suppose...
|
||||
if(nulltexture)
|
||||
{
|
||||
return new LocalLoadResult(bitmap, messages);
|
||||
}
|
||||
|
||||
// No patches!
|
||||
messages.Add(new LogMessage(ErrorType.Warning, "No patches are defined for texture \"" + this.Name + "\""));
|
||||
}
|
||||
else if(!messages.Any(x => x.Type == ErrorType.Error))
|
||||
{
|
||||
// Go for all patches
|
||||
foreach(TexturePatch p in patches)
|
||||
{
|
||||
//mxd. Some patches (like "TNT1A0") should be skipped
|
||||
if(p.Skip) continue;
|
||||
|
||||
// Get the patch data stream
|
||||
string patchlocation = string.Empty; //mxd
|
||||
Stream patchdata = General.Map.Data.GetPatchData(p.LumpName, p.HasLongName, ref patchlocation);
|
||||
if(patchdata != null)
|
||||
{
|
||||
// Copy patch data to memory
|
||||
byte[] membytes = new byte[(int)patchdata.Length];
|
||||
|
||||
lock(patchdata) //mxd
|
||||
{
|
||||
patchdata.Seek(0, SeekOrigin.Begin);
|
||||
patchdata.Read(membytes, 0, (int)patchdata.Length);
|
||||
}
|
||||
|
||||
MemoryStream mem = new MemoryStream(membytes);
|
||||
mem.Seek(0, SeekOrigin.Begin);
|
||||
|
||||
// Get a reader for the data
|
||||
IImageReader reader = ImageDataFormat.GetImageReader(mem, ImageDataFormat.DOOMPICTURE, General.Map.Data.Palette);
|
||||
if(reader is UnknownImageReader)
|
||||
{
|
||||
//mxd. Probably that's a flat?..
|
||||
if(General.Map.Config.MixTexturesFlats)
|
||||
{
|
||||
reader = ImageDataFormat.GetImageReader(mem, ImageDataFormat.DOOMFLAT, General.Map.Data.Palette);
|
||||
}
|
||||
|
||||
if(reader is UnknownImageReader)
|
||||
{
|
||||
// Data is in an unknown format!
|
||||
if(!nulltexture) messages.Add(new LogMessage(optional ? ErrorType.Warning : ErrorType.Error, "Patch lump \"" + Path.Combine(patchlocation, p.LumpName) + "\" data format could not be read, while loading texture \"" + this.Name + "\""));
|
||||
missingpatches++; //mxd
|
||||
}
|
||||
}
|
||||
|
||||
if(!(reader is UnknownImageReader))
|
||||
{
|
||||
// Get the patch
|
||||
mem.Seek(0, SeekOrigin.Begin);
|
||||
Bitmap patchbmp = null;
|
||||
try { patchbmp = reader.ReadAsBitmap(mem); }
|
||||
catch(InvalidDataException)
|
||||
{
|
||||
// Data cannot be read!
|
||||
if(!nulltexture) messages.Add(new LogMessage(optional ? ErrorType.Warning : ErrorType.Error, "Patch lump \"" + p.LumpName + "\" data format could not be read, while loading texture \"" + this.Name + "\""));
|
||||
missingpatches++; //mxd
|
||||
}
|
||||
|
||||
if(patchbmp != null)
|
||||
{
|
||||
//mxd. Apply transformations from TexturePatch
|
||||
patchbmp = TransformPatch(p, patchbmp);
|
||||
|
||||
// Draw the patch on the texture image
|
||||
Rectangle tgtrect = new Rectangle(p.X, p.Y, patchbmp.Size.Width, patchbmp.Size.Height);
|
||||
g.DrawImageUnscaledAndClipped(patchbmp, tgtrect);
|
||||
patchbmp.Dispose();
|
||||
}
|
||||
}
|
||||
|
||||
// Done
|
||||
mem.Dispose();
|
||||
}
|
||||
else
|
||||
{
|
||||
//mxd. ZDoom can use any known graphic as patch
|
||||
if(General.Map.Config.MixTexturesFlats)
|
||||
{
|
||||
ImageData img = General.Map.Data.GetTextureImage(p.LumpName);
|
||||
if(!(img is UnknownImage) && img != this)
|
||||
{
|
||||
if(!img.IsImageLoaded) img.LoadImage();
|
||||
|
||||
//mxd. Apply transformations from TexturePatch. We don't want to modify the original bitmap here, so make a copy
|
||||
Bitmap bmp = new Bitmap(img.LocalGetBitmap());
|
||||
Bitmap patchbmp = TransformPatch(p, bmp);
|
||||
|
||||
// Draw the patch on the texture image
|
||||
Rectangle tgtrect = new Rectangle(p.X, p.Y, patchbmp.Size.Width, patchbmp.Size.Height);
|
||||
g.DrawImageUnscaledAndClipped(patchbmp, tgtrect);
|
||||
patchbmp.Dispose();
|
||||
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
// Missing a patch lump!
|
||||
if(!nulltexture) messages.Add(new LogMessage(optional ? ErrorType.Warning : ErrorType.Error, "Missing patch lump \"" + p.LumpName + "\" while loading texture \"" + this.Name + "\""));
|
||||
missingpatches++; //mxd
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Dispose bitmap if load failed
|
||||
if(!nulltexture && (bitmap != null) && (messages.Any(x => x.Type == ErrorType.Error) || missingpatches >= patches.Count)) //mxd. We can still display texture if at least one of the patches was loaded
|
||||
{
|
||||
bitmap.Dispose();
|
||||
bitmap = null;
|
||||
}
|
||||
|
||||
return new LocalLoadResult(bitmap, messages);
|
||||
}
|
||||
|
||||
//mxd
|
||||
private Bitmap TransformPatch(TexturePatch p, Bitmap patchbmp)
|
||||
{
|
||||
//mxd. Flip
|
||||
if(p.FlipX || p.FlipY)
|
||||
|
|
|
@ -23,6 +23,7 @@ using System.Drawing.Imaging;
|
|||
using CodeImp.DoomBuilder.Rendering;
|
||||
using CodeImp.DoomBuilder.IO;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
|
||||
#endregion
|
||||
|
||||
|
@ -69,115 +70,109 @@ namespace CodeImp.DoomBuilder.Data
|
|||
}
|
||||
|
||||
// This loads the image
|
||||
protected override void LocalLoadImage()
|
||||
protected override LocalLoadResult LocalLoadImage()
|
||||
{
|
||||
// Checks
|
||||
if(this.IsImageLoaded || width == 0 || height == 0) return;
|
||||
if(width == 0 || height == 0) return new LocalLoadResult(null);
|
||||
|
||||
BitmapData bitmapdata = null;
|
||||
PixelColor* pixels = (PixelColor*)0;
|
||||
|
||||
lock(this)
|
||||
|
||||
Bitmap bitmap = null;
|
||||
List<LogMessage> messages = new List<LogMessage>();
|
||||
|
||||
// Create texture bitmap
|
||||
try
|
||||
{
|
||||
// Create texture bitmap
|
||||
try
|
||||
{
|
||||
if(bitmap != null) bitmap.Dispose();
|
||||
bitmap = new Bitmap(width, height, PixelFormat.Format32bppArgb);
|
||||
bitmapdata = bitmap.LockBits(new Rectangle(0, 0, width, height), ImageLockMode.WriteOnly, PixelFormat.Format32bppArgb);
|
||||
pixels = (PixelColor*)bitmapdata.Scan0.ToPointer();
|
||||
General.ZeroMemory(new IntPtr(pixels), width * height * sizeof(PixelColor));
|
||||
}
|
||||
catch(Exception e)
|
||||
{
|
||||
// Unable to make bitmap
|
||||
General.ErrorLogger.Add(ErrorType.Error, "Unable to load texture image \"" + this.Name + "\". " + e.GetType().Name + ": " + e.Message);
|
||||
loadfailed = true;
|
||||
}
|
||||
|
||||
int missingpatches = 0; //mxd
|
||||
|
||||
if(!loadfailed)
|
||||
{
|
||||
// Go for all patches
|
||||
foreach(TexturePatch p in patches)
|
||||
{
|
||||
// Get the patch data stream
|
||||
string patchlocation = string.Empty; //mxd
|
||||
Stream patchdata = General.Map.Data.GetPatchData(p.LumpName, p.HasLongName, ref patchlocation);
|
||||
if(patchdata != null)
|
||||
{
|
||||
// Copy patch data to memory
|
||||
byte[] membytes = new byte[(int)patchdata.Length];
|
||||
|
||||
lock(patchdata) //mxd
|
||||
{
|
||||
patchdata.Seek(0, SeekOrigin.Begin);
|
||||
patchdata.Read(membytes, 0, (int)patchdata.Length);
|
||||
}
|
||||
|
||||
MemoryStream mem = new MemoryStream(membytes);
|
||||
mem.Seek(0, SeekOrigin.Begin);
|
||||
|
||||
// Get a reader for the data
|
||||
IImageReader reader = ImageDataFormat.GetImageReader(mem, ImageDataFormat.DOOMPICTURE, General.Map.Data.Palette);
|
||||
if(reader is UnknownImageReader)
|
||||
{
|
||||
//mxd. Probably that's a flat?..
|
||||
if(General.Map.Config.MixTexturesFlats)
|
||||
{
|
||||
reader = ImageDataFormat.GetImageReader(mem, ImageDataFormat.DOOMFLAT, General.Map.Data.Palette);
|
||||
}
|
||||
if(reader is UnknownImageReader)
|
||||
{
|
||||
// Data is in an unknown format!
|
||||
General.ErrorLogger.Add(ErrorType.Error, "Patch lump \"" + Path.Combine(patchlocation, p.LumpName) + "\" data format could not be read, while loading texture \"" + this.Name + "\". Does this lump contain valid picture data at all?");
|
||||
loadfailed = true;
|
||||
missingpatches++; //mxd
|
||||
}
|
||||
}
|
||||
|
||||
if(!(reader is UnknownImageReader))
|
||||
{
|
||||
// Draw the patch
|
||||
mem.Seek(0, SeekOrigin.Begin);
|
||||
try { reader.DrawToPixelData(mem, pixels, width, height, p.X, p.Y); }
|
||||
catch(InvalidDataException)
|
||||
{
|
||||
// Data cannot be read!
|
||||
General.ErrorLogger.Add(ErrorType.Error, "Patch lump \"" + p.LumpName + "\" data format could not be read, while loading texture \"" + this.Name + "\". Does this lump contain valid picture data at all?");
|
||||
loadfailed = true;
|
||||
missingpatches++; //mxd
|
||||
}
|
||||
}
|
||||
|
||||
// Done
|
||||
mem.Dispose();
|
||||
}
|
||||
else
|
||||
{
|
||||
// Missing a patch lump!
|
||||
General.ErrorLogger.Add(ErrorType.Error, "Missing patch lump \"" + p.LumpName + "\" while loading texture \"" + this.Name + "\". Did you forget to include required resources?");
|
||||
loadfailed = true;
|
||||
missingpatches++; //mxd
|
||||
}
|
||||
}
|
||||
|
||||
// Done
|
||||
bitmap.UnlockBits(bitmapdata);
|
||||
}
|
||||
|
||||
// Dispose bitmap if load failed
|
||||
if((bitmap != null) && (loadfailed || missingpatches >= patches.Count)) //mxd. We can still display texture if at least one of the patches was loaded
|
||||
{
|
||||
bitmap.Dispose();
|
||||
bitmap = null;
|
||||
loadfailed = true;
|
||||
}
|
||||
|
||||
// Pass on to base
|
||||
base.LocalLoadImage();
|
||||
if(bitmap != null) bitmap.Dispose();
|
||||
bitmap = new Bitmap(width, height, PixelFormat.Format32bppArgb);
|
||||
bitmapdata = bitmap.LockBits(new Rectangle(0, 0, width, height), ImageLockMode.WriteOnly, PixelFormat.Format32bppArgb);
|
||||
pixels = (PixelColor*)bitmapdata.Scan0.ToPointer();
|
||||
General.ZeroMemory(new IntPtr(pixels), width * height * sizeof(PixelColor));
|
||||
}
|
||||
catch(Exception e)
|
||||
{
|
||||
// Unable to make bitmap
|
||||
messages.Add(new LogMessage(ErrorType.Error, "Unable to load texture image \"" + this.Name + "\". " + e.GetType().Name + ": " + e.Message));
|
||||
}
|
||||
|
||||
int missingpatches = 0; //mxd
|
||||
|
||||
if(!messages.Any(x => x.Type == ErrorType.Error))
|
||||
{
|
||||
// Go for all patches
|
||||
foreach(TexturePatch p in patches)
|
||||
{
|
||||
// Get the patch data stream
|
||||
string patchlocation = string.Empty; //mxd
|
||||
Stream patchdata = General.Map.Data.GetPatchData(p.LumpName, p.HasLongName, ref patchlocation);
|
||||
if(patchdata != null)
|
||||
{
|
||||
// Copy patch data to memory
|
||||
byte[] membytes = new byte[(int)patchdata.Length];
|
||||
|
||||
lock(patchdata) //mxd
|
||||
{
|
||||
patchdata.Seek(0, SeekOrigin.Begin);
|
||||
patchdata.Read(membytes, 0, (int)patchdata.Length);
|
||||
}
|
||||
|
||||
MemoryStream mem = new MemoryStream(membytes);
|
||||
mem.Seek(0, SeekOrigin.Begin);
|
||||
|
||||
// Get a reader for the data
|
||||
IImageReader reader = ImageDataFormat.GetImageReader(mem, ImageDataFormat.DOOMPICTURE, General.Map.Data.Palette);
|
||||
if(reader is UnknownImageReader)
|
||||
{
|
||||
//mxd. Probably that's a flat?..
|
||||
if(General.Map.Config.MixTexturesFlats)
|
||||
{
|
||||
reader = ImageDataFormat.GetImageReader(mem, ImageDataFormat.DOOMFLAT, General.Map.Data.Palette);
|
||||
}
|
||||
if(reader is UnknownImageReader)
|
||||
{
|
||||
// Data is in an unknown format!
|
||||
messages.Add(new LogMessage(ErrorType.Error, "Patch lump \"" + Path.Combine(patchlocation, p.LumpName) + "\" data format could not be read, while loading texture \"" + this.Name + "\". Does this lump contain valid picture data at all?"));
|
||||
missingpatches++; //mxd
|
||||
}
|
||||
}
|
||||
|
||||
if(!(reader is UnknownImageReader))
|
||||
{
|
||||
// Draw the patch
|
||||
mem.Seek(0, SeekOrigin.Begin);
|
||||
try { reader.DrawToPixelData(mem, pixels, width, height, p.X, p.Y); }
|
||||
catch(InvalidDataException)
|
||||
{
|
||||
// Data cannot be read!
|
||||
messages.Add(new LogMessage(ErrorType.Error, "Patch lump \"" + p.LumpName + "\" data format could not be read, while loading texture \"" + this.Name + "\". Does this lump contain valid picture data at all?"));
|
||||
missingpatches++; //mxd
|
||||
}
|
||||
}
|
||||
|
||||
// Done
|
||||
mem.Dispose();
|
||||
}
|
||||
else
|
||||
{
|
||||
// Missing a patch lump!
|
||||
messages.Add(new LogMessage(ErrorType.Error, "Missing patch lump \"" + p.LumpName + "\" while loading texture \"" + this.Name + "\". Did you forget to include required resources?"));
|
||||
missingpatches++; //mxd
|
||||
}
|
||||
}
|
||||
|
||||
// Done
|
||||
bitmap.UnlockBits(bitmapdata);
|
||||
}
|
||||
|
||||
// Dispose bitmap if load failed
|
||||
if((bitmap != null) && (messages.Any(x => x.Type == ErrorType.Error) || missingpatches >= patches.Count)) //mxd. We can still display texture if at least one of the patches was loaded
|
||||
{
|
||||
bitmap.Dispose();
|
||||
bitmap = null;
|
||||
}
|
||||
|
||||
return new LocalLoadResult(bitmap, messages);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
|
|
@ -42,7 +42,7 @@ namespace CodeImp.DoomBuilder.Data
|
|||
this.loadbitmap = image;
|
||||
SetName("");
|
||||
|
||||
LocalLoadImage();
|
||||
LoadImage(false);
|
||||
|
||||
// We have no destructor
|
||||
GC.SuppressFinalize(this);
|
||||
|
@ -53,23 +53,16 @@ namespace CodeImp.DoomBuilder.Data
|
|||
#region ================== Methods
|
||||
|
||||
// This 'loads' the image
|
||||
protected override void LocalLoadImage()
|
||||
protected override LocalLoadResult LocalLoadImage()
|
||||
{
|
||||
//mxd. Leave when already loaded
|
||||
if(this.IsImageLoaded) return;
|
||||
|
||||
bitmap = loadbitmap;
|
||||
base.LocalLoadImage();
|
||||
}
|
||||
return new LocalLoadResult(loadbitmap);
|
||||
}
|
||||
|
||||
// This returns a preview image
|
||||
public override Image GetPreview()
|
||||
// This returns a preview image
|
||||
public override Image GetPreview()
|
||||
{
|
||||
lock(this)
|
||||
{
|
||||
// Make a copy
|
||||
return new Bitmap(loadbitmap);
|
||||
}
|
||||
// To do: do we actually need a copy here?
|
||||
return new Bitmap(loadbitmap);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
|
|
@ -56,269 +56,258 @@ namespace CodeImp.DoomBuilder.Data
|
|||
override public void LoadImage(bool notify)
|
||||
{
|
||||
// Do the loading
|
||||
LocalLoadImage();
|
||||
base.LoadImage(false);
|
||||
|
||||
// Notify the main thread about the change to redraw display
|
||||
if (notify) General.MainWindow.SpriteDataLoaded(this.Name);
|
||||
}
|
||||
|
||||
// This loads the image
|
||||
protected unsafe override void LocalLoadImage()
|
||||
protected unsafe override LocalLoadResult LocalLoadImage()
|
||||
{
|
||||
// Leave when already loaded
|
||||
if(this.IsImageLoaded) return;
|
||||
Bitmap bitmap = null;
|
||||
string error = null;
|
||||
|
||||
lock(this)
|
||||
int imgoffsetx = 0;
|
||||
int pivotz = 0;
|
||||
|
||||
// Get the lump data stream
|
||||
string voxellocation = string.Empty; //mxd
|
||||
Stream lumpdata = General.Map.Data.GetVoxelData(voxelname, ref voxellocation);
|
||||
if(lumpdata != null)
|
||||
{
|
||||
// Get the lump data stream
|
||||
string voxellocation = string.Empty; //mxd
|
||||
Stream lumpdata = General.Map.Data.GetVoxelData(voxelname, ref voxellocation);
|
||||
if(lumpdata != null)
|
||||
{
|
||||
// Copy lump data to memory
|
||||
lumpdata.Seek(0, SeekOrigin.Begin);
|
||||
byte[] membytes = new byte[(int)lumpdata.Length];
|
||||
lumpdata.Read(membytes, 0, (int)lumpdata.Length);
|
||||
// Copy lump data to memory
|
||||
lumpdata.Seek(0, SeekOrigin.Begin);
|
||||
byte[] membytes = new byte[(int)lumpdata.Length];
|
||||
lumpdata.Read(membytes, 0, (int)lumpdata.Length);
|
||||
|
||||
using(MemoryStream mem = new MemoryStream(membytes))
|
||||
using(MemoryStream mem = new MemoryStream(membytes))
|
||||
{
|
||||
mem.Seek(0, SeekOrigin.Begin);
|
||||
PixelColor[] palette = new PixelColor[256];
|
||||
|
||||
// Create front projection image from the KVX
|
||||
using(BinaryReader reader = new BinaryReader(mem, Encoding.ASCII))
|
||||
{
|
||||
mem.Seek(0, SeekOrigin.Begin);
|
||||
PixelColor[] palette = new PixelColor[256];
|
||||
reader.ReadInt32(); //numbytes, we don't use that
|
||||
int xsize = reader.ReadInt32();
|
||||
int ysize = reader.ReadInt32();
|
||||
int zsize = reader.ReadInt32();
|
||||
|
||||
// Create front projection image from the KVX
|
||||
using(BinaryReader reader = new BinaryReader(mem, Encoding.ASCII))
|
||||
// Sanity check
|
||||
if(xsize == 0 || ysize == 0 || zsize == 0)
|
||||
{
|
||||
reader.ReadInt32(); //numbytes, we don't use that
|
||||
int xsize = reader.ReadInt32();
|
||||
int ysize = reader.ReadInt32();
|
||||
int zsize = reader.ReadInt32();
|
||||
error = "Cannot create sprite image for voxel \"" + Path.Combine(voxellocation, voxelname)
|
||||
+ "\" for voxel drawing: voxel has invalid size (width: " + xsize + ", height: " + zsize + ", depth: " + ysize;
|
||||
|
||||
// Sanity check
|
||||
if(xsize == 0 || ysize == 0 || zsize == 0)
|
||||
return new LocalLoadResult(null, error);
|
||||
}
|
||||
|
||||
int pivotx = (int)Math.Round(reader.ReadInt32() / 256f);
|
||||
int pivoty = (int)Math.Round(reader.ReadInt32() / 256f);
|
||||
pivotz = (int)Math.Round(reader.ReadInt32() / 256f);
|
||||
|
||||
// Read offsets
|
||||
int[] xoffset = new int[xsize + 1]; // why is it xsize + 1, not xsize?..
|
||||
short[,] xyoffset = new short[xsize, ysize + 1]; // why is it ysize + 1, not ysize?..
|
||||
|
||||
for(int i = 0; i < xoffset.Length; i++)
|
||||
{
|
||||
xoffset[i] = reader.ReadInt32();
|
||||
}
|
||||
|
||||
for(int x = 0; x < xsize; x++)
|
||||
{
|
||||
for(int y = 0; y < ysize + 1; y++)
|
||||
{
|
||||
General.ErrorLogger.Add(ErrorType.Error, "Cannot create sprite image for voxel \"" + Path.Combine(voxellocation, voxelname)
|
||||
+ "\" for voxel drawing: voxel has invalid size (width: " + xsize + ", height: " + zsize + ", depth: " + ysize);
|
||||
loadfailed = true;
|
||||
return;
|
||||
}
|
||||
|
||||
int pivotx = (int)Math.Round(reader.ReadInt32() / 256f);
|
||||
int pivoty = (int)Math.Round(reader.ReadInt32() / 256f);
|
||||
int pivotz = (int)Math.Round(reader.ReadInt32() / 256f);
|
||||
|
||||
// Read offsets
|
||||
int[] xoffset = new int[xsize + 1]; // why is it xsize + 1, not xsize?..
|
||||
short[,] xyoffset = new short[xsize, ysize + 1]; // why is it ysize + 1, not ysize?..
|
||||
|
||||
for(int i = 0; i < xoffset.Length; i++)
|
||||
{
|
||||
xoffset[i] = reader.ReadInt32();
|
||||
}
|
||||
|
||||
for(int x = 0; x < xsize; x++)
|
||||
{
|
||||
for(int y = 0; y < ysize + 1; y++)
|
||||
{
|
||||
xyoffset[x, y] = reader.ReadInt16();
|
||||
}
|
||||
}
|
||||
|
||||
// Read slabs
|
||||
List<int> offsets = new List<int>(xsize * ysize);
|
||||
for(int x = 0; x < xsize; x++)
|
||||
{
|
||||
for(int y = 0; y < ysize; y++)
|
||||
{
|
||||
offsets.Add(xoffset[x] + xyoffset[x, y] + 28); // for some reason offsets are counted from start of xoffset[]...
|
||||
}
|
||||
}
|
||||
|
||||
int counter = 0;
|
||||
int slabsend = (int)(reader.BaseStream.Length - 768);
|
||||
|
||||
// Read palette
|
||||
if(!overridepalette)
|
||||
{
|
||||
reader.BaseStream.Position = slabsend;
|
||||
for(int i = 0; i < 256; i++)
|
||||
{
|
||||
byte r = (byte)(reader.ReadByte() * 4);
|
||||
byte g = (byte)(reader.ReadByte() * 4);
|
||||
byte b = (byte)(reader.ReadByte() * 4);
|
||||
palette[i] = new PixelColor(255, r, g, b);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
for(int i = 0; i < 256; i++) palette[i] = General.Map.Data.Palette[i];
|
||||
}
|
||||
|
||||
// Populate projection pixels array
|
||||
int imgwidth, imgheight, imgoffsetx;
|
||||
bool checkalpha = false;
|
||||
|
||||
// Convert angleoffsets to the nearest cardinal direction...
|
||||
angleoffset = General.ClampAngle((angleoffset + 45) / 90 * 90);
|
||||
|
||||
switch(angleoffset)
|
||||
{
|
||||
case 0:
|
||||
imgwidth = xsize;
|
||||
imgheight = zsize;
|
||||
imgoffsetx = pivotx;
|
||||
break;
|
||||
|
||||
case 90:
|
||||
imgwidth = ysize;
|
||||
imgheight = zsize;
|
||||
imgoffsetx = imgwidth - pivoty;
|
||||
checkalpha = true;
|
||||
break;
|
||||
|
||||
case 180:
|
||||
imgwidth = xsize;
|
||||
imgheight = zsize;
|
||||
imgoffsetx = imgwidth - pivotx;
|
||||
checkalpha = true;
|
||||
break;
|
||||
|
||||
case 270:
|
||||
imgwidth = ysize;
|
||||
imgheight = zsize;
|
||||
imgoffsetx = pivoty;
|
||||
break;
|
||||
|
||||
default: throw new InvalidDataException("Invalid AngleOffset");
|
||||
}
|
||||
|
||||
int numpixels = imgwidth * imgheight;
|
||||
PixelColor[] pixelsarr = new PixelColor[numpixels];
|
||||
|
||||
// Read pixel colors
|
||||
for(int x = 0; x < xsize; x++)
|
||||
{
|
||||
for(int y = 0; y < ysize; y++)
|
||||
{
|
||||
reader.BaseStream.Position = offsets[counter];
|
||||
int next = (counter < offsets.Count - 1 ? offsets[counter + 1] : slabsend);
|
||||
|
||||
// Read first color from the slab
|
||||
while(reader.BaseStream.Position < next)
|
||||
{
|
||||
int ztop = reader.ReadByte();
|
||||
int zleng = reader.ReadByte();
|
||||
if(ztop + zleng > zsize) break;
|
||||
byte flags = reader.ReadByte();
|
||||
|
||||
if(zleng > 0)
|
||||
{
|
||||
// Skip slab if no flags are given (otherwise some garbage pixels may be drawn)
|
||||
if(flags == 0)
|
||||
{
|
||||
reader.BaseStream.Position += zleng;
|
||||
continue;
|
||||
}
|
||||
|
||||
List<int> colorindices = new List<int>(zleng);
|
||||
for(int i = 0; i < zleng; i++)
|
||||
{
|
||||
colorindices.Add(reader.ReadByte());
|
||||
}
|
||||
|
||||
int z = ztop;
|
||||
int cstart = 0;
|
||||
while(z < ztop + zleng)
|
||||
{
|
||||
// Get pixel position
|
||||
int pixelpos;
|
||||
switch(angleoffset)
|
||||
{
|
||||
case 0: pixelpos = x + z * xsize; break;
|
||||
case 90: pixelpos = y + z * ysize; break;
|
||||
case 180: pixelpos = xsize - x - 1 + z * xsize; break;
|
||||
case 270: pixelpos = ysize - y - 1 + z * ysize; break;
|
||||
default: throw new InvalidDataException("Invalid AngleOffset");
|
||||
}
|
||||
|
||||
// Add to projection pixels array
|
||||
if((checkalpha && pixelsarr[pixelpos].a == 0) || !checkalpha)
|
||||
pixelsarr[pixelpos] = palette[colorindices[cstart]];
|
||||
|
||||
// Increment counters
|
||||
cstart++;
|
||||
z++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
counter++;
|
||||
}
|
||||
}
|
||||
|
||||
// Draw to bitmap
|
||||
if(bitmap != null) bitmap.Dispose();
|
||||
bitmap = new Bitmap(imgwidth, imgheight, PixelFormat.Format32bppArgb);
|
||||
BitmapData bmpdata = null;
|
||||
|
||||
try
|
||||
{
|
||||
bmpdata = bitmap.LockBits(new Rectangle(0, 0, imgwidth, imgheight), ImageLockMode.ReadWrite, PixelFormat.Format32bppArgb);
|
||||
}
|
||||
catch(Exception e)
|
||||
{
|
||||
General.ErrorLogger.Add(ErrorType.Error, "Cannot lock image for drawing voxel \""
|
||||
+ Path.Combine(voxellocation, voxelname) + "\". " + e.GetType().Name + ": " + e.Message);
|
||||
bitmap = null;
|
||||
}
|
||||
|
||||
if(bmpdata != null)
|
||||
{
|
||||
// Apply pixels to image
|
||||
PixelColor* pixels = (PixelColor*)bmpdata.Scan0.ToPointer();
|
||||
int i = 0;
|
||||
|
||||
for(PixelColor* cp = pixels; cp < pixels + numpixels; cp++, i++)
|
||||
{
|
||||
if(pixelsarr[i].a == 255)
|
||||
{
|
||||
cp->r = pixelsarr[i].r;
|
||||
cp->g = pixelsarr[i].g;
|
||||
cp->b = pixelsarr[i].b;
|
||||
cp->a = 255;
|
||||
}
|
||||
}
|
||||
|
||||
bitmap.UnlockBits(bmpdata);
|
||||
}
|
||||
|
||||
if(bitmap != null)
|
||||
{
|
||||
// Get width and height from image
|
||||
width = bitmap.Size.Width;
|
||||
height = bitmap.Size.Height;
|
||||
scale.x = 1.0f;
|
||||
scale.y = 1.0f;
|
||||
offsetx = imgoffsetx;
|
||||
offsety = pivotz;
|
||||
}
|
||||
else
|
||||
{
|
||||
loadfailed = true;
|
||||
xyoffset[x, y] = reader.ReadInt16();
|
||||
}
|
||||
}
|
||||
|
||||
// Read slabs
|
||||
List<int> offsets = new List<int>(xsize * ysize);
|
||||
for(int x = 0; x < xsize; x++)
|
||||
{
|
||||
for(int y = 0; y < ysize; y++)
|
||||
{
|
||||
offsets.Add(xoffset[x] + xyoffset[x, y] + 28); // for some reason offsets are counted from start of xoffset[]...
|
||||
}
|
||||
}
|
||||
|
||||
int counter = 0;
|
||||
int slabsend = (int)(reader.BaseStream.Length - 768);
|
||||
|
||||
// Read palette
|
||||
if(!overridepalette)
|
||||
{
|
||||
reader.BaseStream.Position = slabsend;
|
||||
for(int i = 0; i < 256; i++)
|
||||
{
|
||||
byte r = (byte)(reader.ReadByte() * 4);
|
||||
byte g = (byte)(reader.ReadByte() * 4);
|
||||
byte b = (byte)(reader.ReadByte() * 4);
|
||||
palette[i] = new PixelColor(255, r, g, b);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
for(int i = 0; i < 256; i++) palette[i] = General.Map.Data.Palette[i];
|
||||
}
|
||||
|
||||
// Populate projection pixels array
|
||||
int imgwidth, imgheight;
|
||||
bool checkalpha = false;
|
||||
|
||||
// Convert angleoffsets to the nearest cardinal direction...
|
||||
angleoffset = General.ClampAngle((angleoffset + 45) / 90 * 90);
|
||||
|
||||
switch(angleoffset)
|
||||
{
|
||||
case 0:
|
||||
imgwidth = xsize;
|
||||
imgheight = zsize;
|
||||
imgoffsetx = pivotx;
|
||||
break;
|
||||
|
||||
case 90:
|
||||
imgwidth = ysize;
|
||||
imgheight = zsize;
|
||||
imgoffsetx = imgwidth - pivoty;
|
||||
checkalpha = true;
|
||||
break;
|
||||
|
||||
case 180:
|
||||
imgwidth = xsize;
|
||||
imgheight = zsize;
|
||||
imgoffsetx = imgwidth - pivotx;
|
||||
checkalpha = true;
|
||||
break;
|
||||
|
||||
case 270:
|
||||
imgwidth = ysize;
|
||||
imgheight = zsize;
|
||||
imgoffsetx = pivoty;
|
||||
break;
|
||||
|
||||
default: throw new InvalidDataException("Invalid AngleOffset");
|
||||
}
|
||||
|
||||
int numpixels = imgwidth * imgheight;
|
||||
PixelColor[] pixelsarr = new PixelColor[numpixels];
|
||||
|
||||
// Read pixel colors
|
||||
for(int x = 0; x < xsize; x++)
|
||||
{
|
||||
for(int y = 0; y < ysize; y++)
|
||||
{
|
||||
reader.BaseStream.Position = offsets[counter];
|
||||
int next = (counter < offsets.Count - 1 ? offsets[counter + 1] : slabsend);
|
||||
|
||||
// Read first color from the slab
|
||||
while(reader.BaseStream.Position < next)
|
||||
{
|
||||
int ztop = reader.ReadByte();
|
||||
int zleng = reader.ReadByte();
|
||||
if(ztop + zleng > zsize) break;
|
||||
byte flags = reader.ReadByte();
|
||||
|
||||
if(zleng > 0)
|
||||
{
|
||||
// Skip slab if no flags are given (otherwise some garbage pixels may be drawn)
|
||||
if(flags == 0)
|
||||
{
|
||||
reader.BaseStream.Position += zleng;
|
||||
continue;
|
||||
}
|
||||
|
||||
List<int> colorindices = new List<int>(zleng);
|
||||
for(int i = 0; i < zleng; i++)
|
||||
{
|
||||
colorindices.Add(reader.ReadByte());
|
||||
}
|
||||
|
||||
int z = ztop;
|
||||
int cstart = 0;
|
||||
while(z < ztop + zleng)
|
||||
{
|
||||
// Get pixel position
|
||||
int pixelpos;
|
||||
switch(angleoffset)
|
||||
{
|
||||
case 0: pixelpos = x + z * xsize; break;
|
||||
case 90: pixelpos = y + z * ysize; break;
|
||||
case 180: pixelpos = xsize - x - 1 + z * xsize; break;
|
||||
case 270: pixelpos = ysize - y - 1 + z * ysize; break;
|
||||
default: throw new InvalidDataException("Invalid AngleOffset");
|
||||
}
|
||||
|
||||
// Add to projection pixels array
|
||||
if((checkalpha && pixelsarr[pixelpos].a == 0) || !checkalpha)
|
||||
pixelsarr[pixelpos] = palette[colorindices[cstart]];
|
||||
|
||||
// Increment counters
|
||||
cstart++;
|
||||
z++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
counter++;
|
||||
}
|
||||
}
|
||||
|
||||
// Draw to bitmap
|
||||
bitmap = new Bitmap(imgwidth, imgheight, PixelFormat.Format32bppArgb);
|
||||
BitmapData bmpdata = null;
|
||||
|
||||
try
|
||||
{
|
||||
bmpdata = bitmap.LockBits(new Rectangle(0, 0, imgwidth, imgheight), ImageLockMode.ReadWrite, PixelFormat.Format32bppArgb);
|
||||
}
|
||||
catch(Exception e)
|
||||
{
|
||||
error = "Cannot lock image for drawing voxel \""
|
||||
+ Path.Combine(voxellocation, voxelname) + "\". " + e.GetType().Name + ": " + e.Message;
|
||||
bitmap = null;
|
||||
}
|
||||
|
||||
if(bmpdata != null)
|
||||
{
|
||||
// Apply pixels to image
|
||||
PixelColor* pixels = (PixelColor*)bmpdata.Scan0.ToPointer();
|
||||
int i = 0;
|
||||
|
||||
for(PixelColor* cp = pixels; cp < pixels + numpixels; cp++, i++)
|
||||
{
|
||||
if(pixelsarr[i].a == 255)
|
||||
{
|
||||
cp->r = pixelsarr[i].r;
|
||||
cp->g = pixelsarr[i].g;
|
||||
cp->b = pixelsarr[i].b;
|
||||
cp->a = 255;
|
||||
}
|
||||
}
|
||||
|
||||
bitmap.UnlockBits(bmpdata);
|
||||
}
|
||||
}
|
||||
|
||||
lumpdata.Dispose();
|
||||
}
|
||||
else
|
||||
{
|
||||
// Missing voxel lump!
|
||||
General.ErrorLogger.Add(ErrorType.Error, "Missing voxel lump \"" + voxelname + "\". Forgot to include required resources?");
|
||||
}
|
||||
|
||||
// Pass on to base
|
||||
base.LocalLoadImage();
|
||||
lumpdata.Dispose();
|
||||
}
|
||||
else
|
||||
{
|
||||
// Missing voxel lump!
|
||||
error = "Missing voxel lump \"" + voxelname + "\". Forgot to include required resources?";
|
||||
}
|
||||
|
||||
return new LocalLoadResult(bitmap, error, () =>
|
||||
{
|
||||
scale.x = 1.0f;
|
||||
scale.y = 1.0f;
|
||||
offsetx = imgoffsetx;
|
||||
offsety = pivotz;
|
||||
});
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
|
|
@ -4117,18 +4117,11 @@ namespace CodeImp.DoomBuilder.Windows
|
|||
if (!InvokeRequired)
|
||||
{
|
||||
action();
|
||||
return;
|
||||
}
|
||||
|
||||
IAsyncResult result = null;
|
||||
result = BeginInvoke(new System.Action(() =>
|
||||
else
|
||||
{
|
||||
action();
|
||||
// this may happen if exiting
|
||||
while (result == null)
|
||||
System.Threading.Thread.Sleep(1);
|
||||
EndInvoke(result);
|
||||
}));
|
||||
Invoke(action);
|
||||
}
|
||||
}
|
||||
|
||||
public void UpdateStatus()
|
||||
|
|
Loading…
Reference in a new issue