UltimateZoneBuilder/Source/Data/ImageData.cs

381 lines
11 KiB
C#
Raw Normal View History

2007-10-01 20:57:41 +00:00
#region ================== Copyright (c) 2007 Pascal vd Heiden
/*
* Copyright (c) 2007 Pascal vd Heiden, www.codeimp.com
* This program is released under GNU General Public License
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
*/
#endregion
#region ================== Namespaces
using System;
using System.Collections;
using System.Collections.Generic;
using System.Globalization;
using System.Text;
using System.Drawing;
using SlimDX.Direct3D9;
using System.Drawing.Imaging;
using CodeImp.DoomBuilder.Rendering;
2007-10-07 22:21:47 +00:00
using CodeImp.DoomBuilder.IO;
2007-10-21 04:07:36 +00:00
using System.IO;
2007-11-04 22:19:30 +00:00
using System.Windows.Forms;
using System.Runtime.InteropServices;
using CodeImp.DoomBuilder.Windows;
2007-10-01 20:57:41 +00:00
#endregion
namespace CodeImp.DoomBuilder.Data
{
2008-01-02 21:49:43 +00:00
public abstract unsafe class ImageData
2007-10-01 20:57:41 +00:00
{
#region ================== Constants
2007-10-01 20:57:41 +00:00
#endregion
2007-10-01 20:57:41 +00:00
#region ================== Variables
2007-10-01 20:57:41 +00:00
// Properties
private string name;
private long longname;
2007-10-08 14:29:31 +00:00
protected int width;
protected int height;
protected float scaledwidth;
protected float scaledheight;
2008-02-24 21:52:18 +00:00
protected bool usecolorcorrection;
2007-10-07 22:21:47 +00:00
// Loading
private volatile ImageLoadState previewstate;
private volatile ImageLoadState imagestate;
private volatile int previewindex;
protected volatile bool loadfailed;
// References
private volatile bool usedinmap;
private volatile int references;
2007-10-01 20:57:41 +00:00
// GDI bitmap
protected Bitmap bitmap;
// Direct3D texture
private int mipmaplevels = 0; // 0 = all mipmaps
private Texture texture;
2007-10-01 20:57:41 +00:00
// Disposing
protected bool isdisposed = false;
2007-10-01 20:57:41 +00:00
#endregion
2007-10-01 20:57:41 +00:00
#region ================== Properties
2007-10-01 20:57:41 +00:00
public string Name { get { return name; } }
2007-10-07 22:21:47 +00:00
public long LongName { get { return longname; } }
2008-02-24 21:52:18 +00:00
public bool UseColorCorrection { get { return usecolorcorrection; } set { usecolorcorrection = value; } }
//public Bitmap Bitmap { get { lock(this) { if(bitmap != null) return new Bitmap(bitmap); else return CodeImp.DoomBuilder.Properties.Resources.Hourglass; } } }
public Bitmap Bitmap { get { lock(this) { if(bitmap != null) return bitmap; else return CodeImp.DoomBuilder.Properties.Resources.Hourglass; } } }
2007-10-26 18:04:54 +00:00
public Texture Texture { get { lock(this) { return texture; } } }
public bool IsPreviewLoaded { get { return (previewstate == ImageLoadState.Ready); } }
public bool IsImageLoaded { get { return (imagestate == ImageLoadState.Ready); } }
public bool LoadFailed { get { return loadfailed; } }
2007-10-01 20:57:41 +00:00
public bool IsDisposed { get { return isdisposed; } }
public ImageLoadState ImageState { get { return imagestate; } internal set { imagestate = value; } }
public ImageLoadState PreviewState { get { return previewstate; } internal set { previewstate = value; } }
public bool IsReferenced { get { return (references > 0) || usedinmap; } }
public bool UsedInMap { get { return usedinmap; } }
public int MipMapLevels { get { return mipmaplevels; } set { mipmaplevels = value; } }
2007-10-08 14:29:31 +00:00
public int Width { get { return width; } }
public int Height { get { return height; } }
internal int PreviewIndex { get { return previewindex; } set { previewindex = value; } }
2007-10-08 14:29:31 +00:00
public float ScaledWidth { get { return scaledwidth; } }
public float ScaledHeight { get { return scaledheight; } }
2007-10-01 20:57:41 +00:00
#endregion
#region ================== Constructor / Disposer
// Constructor
public ImageData()
{
2008-02-24 21:52:18 +00:00
// Defaults
usecolorcorrection = true;
2007-10-01 20:57:41 +00:00
}
// Destructor
~ImageData()
{
this.Dispose();
}
// Disposer
2007-10-01 20:57:41 +00:00
public virtual void Dispose()
{
// Not already disposed?
if(!isdisposed)
{
lock(this)
2007-10-09 05:34:05 +00:00
{
// Clean up
if(bitmap != null) bitmap.Dispose();
if(texture != null) texture.Dispose();
2008-02-24 21:52:18 +00:00
bitmap = null;
texture = null;
2007-10-27 13:59:24 +00:00
// Done
usedinmap = false;
imagestate = ImageLoadState.None;
previewstate = ImageLoadState.None;
isdisposed = true;
2007-10-09 05:34:05 +00:00
}
2007-10-01 20:57:41 +00:00
}
}
2007-10-01 20:57:41 +00:00
#endregion
2007-10-01 20:57:41 +00:00
#region ================== Management
2007-11-10 19:24:52 +00:00
// This sets the status of the texture usage in the map
internal void SetUsedInMap(bool used)
{
if(used != usedinmap)
{
usedinmap = used;
General.Map.Data.ProcessImage(this);
}
}
// This adds a reference
public void AddReference()
{
references++;
if(references == 1) General.Map.Data.ProcessImage(this);
}
// This removes a reference
public void RemoveReference()
{
references--;
if(references < 0) General.Fail("FAIL! (references < 0) Somewhere this image is dereferenced more than it was referenced.");
if(references == 0) General.Map.Data.ProcessImage(this);
}
// This sets the name
protected void SetName(string name)
{
this.name = name;
2007-10-07 22:21:47 +00:00
this.longname = Lump.MakeLongName(name);
}
2008-02-24 21:52:18 +00:00
// This unloads the image
public virtual void UnloadImage()
{
lock(this)
{
if(bitmap != null) bitmap.Dispose();
bitmap = null;
imagestate = ImageLoadState.None;
2008-02-24 21:52:18 +00:00
}
}
// This loads theimage
public void LoadImage()
{
// Keep original dimensions
int oldwidth = width;
int oldheight = height;
float oldscaledwidth = scaledwidth;
float oldscaledheight = scaledheight;
// Do the loading
LocalLoadImage();
// Anything changed?
2009-01-03 22:18:59 +00:00
//if((oldwidth != width) || (oldheight != height) ||
// (oldscaledwidth != scaledwidth) || (oldscaledheight != scaledheight))
{
// Notify the main thread about the change so that sectors can update their buffers
IntPtr strptr = Marshal.StringToCoTaskMemAuto(this.name);
General.SendMessage(General.MainWindow.Handle, (int)MainForm.ThreadMessages.ImageDataLoaded, strptr.ToInt32(), 0);
}
}
// This requests loading the image
protected virtual void LocalLoadImage()
2007-10-09 05:34:05 +00:00
{
BitmapData bmpdata = null;
2008-02-24 21:52:18 +00:00
lock(this)
2008-02-24 21:52:18 +00:00
{
// Bitmap loaded successfully?
if(bitmap != null)
{
// This applies brightness correction on the image
if(usecolorcorrection)
{
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.WriteLogLine("ERROR: Cannot lock image '" + name + "' for color correction. " + e.GetType().Name + ": " + e.Message);
}
// Bitmap locked?
if(bmpdata != null)
{
// Apply color correction
PixelColor* pixels = (PixelColor*)(bmpdata.Scan0.ToPointer());
General.Colors.ApplColorCorrection(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
loadfailed = true;
bitmap = new Bitmap(Properties.Resources.Failed);
width = bitmap.Size.Width;
height = bitmap.Size.Height;
scaledwidth = (float)bitmap.Size.Width * General.Map.Config.DefaultTextureScale;
scaledheight = (float)bitmap.Size.Height * General.Map.Config.DefaultTextureScale;
}
// Image is ready
imagestate = ImageLoadState.Ready;
2007-10-01 20:57:41 +00:00
}
}
// This creates the Direct3D texture
2008-01-02 21:49:43 +00:00
internal virtual void CreateTexture()
2007-10-01 20:57:41 +00:00
{
2007-10-21 04:07:36 +00:00
MemoryStream memstream;
2007-10-26 18:04:54 +00:00
lock(this)
2007-10-21 04:07:36 +00:00
{
2007-10-26 18:04:54 +00:00
// Only do this when texture is not created yet
if(((texture == null) || (texture.Disposed)) && this.IsImageLoaded)
{
Image img = bitmap;
if(loadfailed) img = Properties.Resources.Failed;
// Write to memory stream and read from memory
memstream = new MemoryStream();
img.Save(memstream, ImageFormat.Bmp);
memstream.Seek(0, SeekOrigin.Begin);
texture = Texture.FromStream(General.Map.Graphics.Device, memstream, (int)memstream.Length,
img.Size.Width, img.Size.Height, mipmaplevels, Usage.None, Format.Unknown,
Pool.Managed, General.Map.Graphics.PostFilter, General.Map.Graphics.PostFilter, 0);
memstream.Dispose();
}
2007-10-21 04:07:36 +00:00
}
2007-10-01 20:57:41 +00:00
}
// This destroys the Direct3D texture
2008-01-02 21:49:43 +00:00
internal void ReleaseTexture()
2007-10-01 20:57:41 +00:00
{
lock(this)
{
// Trash it
if(texture != null) texture.Dispose();
texture = null;
}
2007-10-01 20:57:41 +00:00
}
// This draws a preview
public virtual void DrawPreviewCentered(Graphics target, Rectangle targetview)
{
lock(this)
{
// Preview ready?
if(previewstate == ImageLoadState.Ready)
{
// Draw preview
General.Map.Data.Previews.DrawPreviewCentered(previewindex, target, targetview);
}
// Loading failed?
else if(loadfailed)
{
// Draw error bitmap
RectangleF targetpos = General.MakeZoomedRect(Properties.Resources.Failed.Size, targetview);
target.DrawImage(Properties.Resources.Hourglass, targetpos.Location);
}
else
{
// Draw loading bitmap
RectangleF targetpos = General.MakeZoomedRect(Properties.Resources.Hourglass.Size, targetview);
target.DrawImage(Properties.Resources.Hourglass, targetpos.Location);
}
}
}
// This draws a preview
public virtual void DrawPreview(Graphics target, Point targetpos)
{
lock(this)
{
// Preview ready?
if(previewstate == ImageLoadState.Ready)
{
// Draw preview
General.Map.Data.Previews.DrawPreview(previewindex, target, targetpos);
}
// Loading failed?
else if(loadfailed)
{
// Draw error bitmap
target.DrawImageUnscaled(Properties.Resources.Failed, targetpos);
}
else
{
// Draw loading bitmap
target.DrawImageUnscaled(Properties.Resources.Hourglass, targetpos);
}
}
}
// This returns a preview image
public virtual Image GetPreview()
{
lock(this)
{
// Preview ready?
if(previewstate == ImageLoadState.Ready)
{
// Make a bitmap and return it
Bitmap bmp = new Bitmap(PreviewManager.IMAGE_WIDTH, PreviewManager.IMAGE_HEIGHT);
Graphics g = Graphics.FromImage(bmp);
g.Clear(Color.Transparent);
General.Map.Data.Previews.DrawPreview(previewindex, g, new Point(0, 0));
g.Dispose();
return bmp;
}
// Loading failed?
else if(loadfailed)
{
// Return error bitmap
return Properties.Resources.Failed;
}
else
{
// Return loading bitmap
return Properties.Resources.Hourglass;
}
}
}
2007-10-01 20:57:41 +00:00
#endregion
}
}