mirror of
https://git.do.srb2.org/STJr/UltimateZoneBuilder.git
synced 2025-05-30 16:41:38 +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
|
@ -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
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue