/*=============================================================================
Project: SharpImage
Module: siGdiLookupTable.cs
Language: C#
Author: Dan Mueller
Date: $Date: 2007-07-13 06:30:23 +1000 (Fri, 13 Jul 2007) $
Revision: $Revision: 8 $
Copyright (c) Queensland University of Technology (QUT) 2007.
All rights reserved.
This software is distributed WITHOUT ANY WARRANTY; without even
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE. See the above copyright notices for more information.
=============================================================================*/
using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.Diagnostics;
namespace SharpImage.Rendering
{
public class siGdiLookupTable : siLookupTable
{
#region Constuction and Disposal
//=====================================================================
///
/// Default constructor.
///
public siGdiLookupTable() : base()
{
}
//=====================================================================
#endregion
#region Properties
//=====================================================================
//=====================================================================
#endregion
#region Public Methods
//=====================================================================
///
/// Converts the LUT to a GDI ColorPalette. Use GetGdiPixelFormat()
/// to get the PixelFormat of the ColorPalette. This method reconstructs
/// a new ColorPalette for each call, so use with care.
///
///
public virtual ColorPalette GetGdiPalette()
{
// Get the format and empty palette
PixelFormat format = this.GetGdiPixelFormat();
ColorPalette palette = this.CreateEmptyColorPalette(format);
// Populate the palette entries
for (int i = 0; i < this.NumberOfTableValues; i++)
palette.Entries[i] = this.TableValues[i].ValueAsColor;
//Return result
return palette;
}
///
/// Gets the GDI PixelFormat of the ColorPalette. Use GetGdiPalette()
/// to get the actual palette.
///
///
public virtual PixelFormat GetGdiPixelFormat()
{
// TODO: We currently only support 8-bit LUTs
// TODO: Support RGB and RGBA colour maps
return PixelFormat.Format8bppIndexed;
}
//=====================================================================
#endregion
#region Protected Methods
//=====================================================================
///
/// Creates an empty colour palette of the given format.
///
///
///
protected ColorPalette CreateEmptyColorPalette(PixelFormat format)
{
// NOTE: This code is adapted from:
// http://support.microsoft.com/default.aspx?scid=kb;EN-US;Q319061
ColorPalette palette; // The Palette we are stealing
Bitmap bitmap; // The source of the stolen palette
// Make a new Bitmap object to steal its Palette
bitmap = new Bitmap(1, 1, format);
palette = bitmap.Palette; // Grab the palette
bitmap.Dispose(); // Cleanup the source Bitmap
return palette; // Send the palette back
}
//=====================================================================
#endregion
}
}