UltimateZoneBuilder/Source/Data/ImageData.cs

256 lines
7.4 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;
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
internal const int LOADSTATE_NONE = 0;
internal const int LOADSTATE_LOAD = 1;
internal const int LOADSTATE_TRASH = 2;
2007-10-01 20:57:41 +00:00
#endregion
#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
// Background loading
private LinkedListNode<ImageData> loadingticket;
private int loadstate; // true when loading, false when unloading
private bool temporary;
2007-10-01 20:57:41 +00:00
// GDI bitmap
protected Bitmap bitmap;
// 2D rendering data
2007-10-27 13:59:24 +00:00
private PixelColorBlock pixeldata;
2007-10-01 20:57:41 +00:00
// Direct3D texture
private int mipmaplevels = 0; // 0 creates the full chain
private Texture texture;
2007-10-01 20:57:41 +00:00
// Disposing
protected bool isdisposed = false;
#endregion
#region ================== Properties
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; } }
2007-10-27 13:59:24 +00:00
public PixelColorBlock PixelData { get { lock(this) { return pixeldata; } } }
2008-02-24 21:52:18 +00:00
public Bitmap Bitmap { get { lock(this) { if(bitmap != null) return new Bitmap(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 IsLoaded { get { return (bitmap != null) && (loadstate != ImageData.LOADSTATE_LOAD); } }
2007-10-01 20:57:41 +00:00
public bool IsDisposed { get { return isdisposed; } }
internal bool Temporary { get { return temporary; } set { temporary = value; } }
internal int LoadState { get { return loadstate; } set { loadstate = value; } }
internal LinkedListNode<ImageData> LoadingTicket { get { return loadingticket; } set { loadingticket = value; } }
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; } }
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()
{
// We have no destructor
GC.SuppressFinalize(this);
2008-02-24 21:52:18 +00:00
// Defaults
usecolorcorrection = true;
2007-10-01 20:57:41 +00:00
}
// 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
pixeldata = null;
// Done
isdisposed = true;
2007-10-09 05:34:05 +00:00
}
2007-10-01 20:57:41 +00:00
}
}
#endregion
#region ================== Management
2007-11-10 19:24:52 +00:00
// 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;
loadstate = ImageData.LOADSTATE_NONE;
2008-02-24 21:52:18 +00:00
}
}
// This requests loading the image
2007-10-01 20:57:41 +00:00
public virtual void LoadImage()
2007-10-09 05:34:05 +00:00
{
BitmapData bmpdata = null;
2008-02-24 21:52:18 +00:00
// Determine amounts
float gamma = (float)(General.Settings.ImageBrightness + 10) * 0.1f;
float bright = (float)General.Settings.ImageBrightness * 5f;
// This applies brightness correction on the image
if((bitmap != null) && usecolorcorrection)
2008-02-24 21:52:18 +00:00
{
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)
2008-02-24 21:52:18 +00:00
{
// Apply color correction
byte* pixels = (byte*)(bmpdata.Scan0.ToPointer());
for(int p = 0; p < bmpdata.Stride * bmpdata.Height; p += 4)
{
// Apply color correction for individual colors
float r = (float)pixels[p + 0] * gamma + bright;
float g = (float)pixels[p + 1] * gamma + bright;
float b = (float)pixels[p + 2] * gamma + bright;
// Clamp to 0..255 range
if(r < 0f) pixels[p + 0] = 0; else if(r > 255f) pixels[p + 0] = 255; else pixels[p + 0] = (byte)r;
if(g < 0f) pixels[p + 1] = 0; else if(g > 255f) pixels[p + 1] = 255; else pixels[p + 1] = (byte)g;
if(b < 0f) pixels[p + 2] = 0; else if(b > 255f) pixels[p + 2] = 255; else pixels[p + 2] = (byte)b;
}
2008-02-24 21:52:18 +00:00
// Done with the lock
bitmap.UnlockBits(bmpdata);
2008-02-24 21:52:18 +00:00
}
}
// Done, reset load state
loadstate = ImageData.LOADSTATE_NONE;
2007-10-09 05:34:05 +00:00
}
// This creates the 2D pixel data
2008-01-02 21:49:43 +00:00
internal virtual void CreatePixelData()
2007-10-01 20:57:41 +00:00
{
BitmapData bmpdata;
2007-10-09 05:34:05 +00:00
2007-10-26 18:04:54 +00:00
lock(this)
2007-10-01 20:57:41 +00:00
{
2007-10-26 18:04:54 +00:00
// Only do this when data is not created yet
if((pixeldata == null) && IsLoaded)
{
// Make a data copy of the bits for the 2D renderer
bmpdata = bitmap.LockBits(new Rectangle(0, 0, bitmap.Size.Width, bitmap.Size.Height), ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb);
2007-10-27 13:59:24 +00:00
pixeldata = new PixelColorBlock(bitmap.Size.Width, bitmap.Size.Height);
General.CopyMemory((void*)pixeldata.Pointer, bmpdata.Scan0.ToPointer(), new UIntPtr(pixeldata.Length));
bitmap.UnlockBits(bmpdata);
}
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)) && IsLoaded)
{
// Write to memory stream and read from memory
memstream = new MemoryStream();
bitmap.Save(memstream, ImageFormat.Bmp);
memstream.Seek(0, SeekOrigin.Begin);
texture = Texture.FromStream(General.Map.Graphics.Device, memstream, (int)memstream.Length, bitmap.Size.Width, bitmap.Size.Height, mipmaplevels,
Usage.None, Format.Unknown, Pool.Managed, Filter.Box, Filter.Box, 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
}
#endregion
}
}