2 using System.Collections.Generic;
6 using FreeImageAPI.Metadata;
7 using System.Runtime.InteropServices;
8 using System.Diagnostics;
10 namespace FreeImageAPI
15 public sealed
class Palette : MemoryArray<RGBQUAD>
17 [DebuggerBrowsable(DebuggerBrowsableState.Never)]
18 private GCHandle paletteHandle;
20 [DebuggerBrowsable(DebuggerBrowsableState.Never)]
21 private RGBQUAD[] array;
32 : base(FreeImage.GetPalette(dib), (int)FreeImage.GetColorsUsed(dib))
36 throw new ArgumentNullException(
"dib");
38 if (FreeImage.GetImageType(dib) != FREE_IMAGE_TYPE.FIT_BITMAP)
40 throw new ArgumentException(
"dib");
42 if (FreeImage.GetBPP(dib) > 8u)
44 throw new ArgumentException(
"dib");
57 : base(FreeImage.GetTagValue(tag), (int)FreeImage.GetTagCount(tag))
59 if (FreeImage.GetTagType(tag) != FREE_IMAGE_MDTYPE.FIDT_PALETTE)
61 throw new ArgumentException(
"tag");
74 : base(FreeImage.GetTagValue(tag.tag), (int)tag.
Count)
76 if (FreeImage.GetTagType(tag) != FREE_IMAGE_MDTYPE.FIDT_PALETTE)
78 throw new ArgumentException(
"tag");
91 this.array = (
RGBQUAD[])palette.Clone();
92 this.paletteHandle = GCHandle.Alloc(array, GCHandleType.Pinned);
94 base.baseAddress = (byte*)this.paletteHandle.AddrOfPinnedObject();
95 base.length = (int)this.array.Length;
104 base.handle = GCHandle.Alloc(
buffer, GCHandleType.Pinned);
108 base.ptr = (byte*)base.handle.AddrOfPinnedObject();
118 : this(
RGBQUAD.ToRGBQUAD(palette))
150 internal unsafe
Color[] ColorData
156 for (
int i = 0; i <
length; i++)
158 data[i] = Color.FromArgb((int)(((uint*)
baseAddress)[i] | 0xFF000000));
211 if (splitSize < 1 || splitSize >=
length)
213 throw new ArgumentOutOfRangeException(
"splitSize");
218 double red = color.R;
219 double green = color.G;
220 double blue = color.B;
226 g = green / splitSize;
227 b = blue / splitSize;
229 for (; i <= splitSize; i++)
231 pal[i].rgbRed = (byte)(i * r);
232 pal[i].rgbGreen = (byte)(i * g);
233 pal[i].rgbBlue = (byte)(i * b);
236 r = (255 - red) / (
length - splitSize);
237 g = (255 - green) / (
length - splitSize);
238 b = (255 - blue) / (
length - splitSize);
242 pal[i].rgbRed = (byte)(red + ((i - splitSize) * r));
243 pal[i].rgbGreen = (byte)(green + ((i - splitSize) * g));
244 pal[i].rgbBlue = (byte)(blue + ((i - splitSize) *
b));
290 Array.Reverse(array);
295 Array.Reverse(localArray);
311 throw new ArgumentNullException(
"palette");
313 CopyFrom(palette.Data, 0, 0, Math.Min(palette.Length,
this.Length));
330 CopyFrom(palette.Data, 0, offset, Math.Min(palette.Length,
this.Length - offset));
339 public void Save(
string filename)
341 using (
Stream stream =
new FileStream(filename,
FileMode.Create, FileAccess.Write))
355 Save(
new BinaryWriter(stream));
364 public void Save(BinaryWriter writer)
374 public void Load(
string filename)
376 using (
Stream stream =
new FileStream(filename,
FileMode.Open, FileAccess.Read))
388 Load(
new BinaryReader(stream));
395 public void Load(BinaryReader reader)
401 byte[] data = reader.ReadBytes(size);
402 fixed (byte* src = data)
413 protected override void Dispose(
bool disposing)
415 if (paletteHandle.IsAllocated)
416 paletteHandle.Free();
419 base.Dispose(disposing);
void Load(string filename)
Loads a palette from the specified file.
override void Dispose(bool disposing)
Releases allocated handles associated with this instance.
The FIBITMAP structure is a handle to a FreeImage bimtap.
T[] buffer
Array of T containing a single element. The array is used as a workaround, because there are no point...
Palette(Color[] palette)
Initializes a new instance for the given array of Color that contains a palette.
System.IO.FileMode FileMode
Palette(RGBQUAD[] palette)
Initializes a new instance for the given array of RGBQUAD that contains a palette.
RGBQUAD[] AsArray
Gets or sets the palette through an array of RGBQUAD.
static unsafe void CopyMemory(byte *dest, byte *src, int len)
Copies a block of memory from one location to another.
void Load(Stream stream)
Loads a palette from the specified stream.
void Load(BinaryReader reader)
Loads a palette from the reader.
void Colorize(Color color)
Creates a linear palette based on the provided color .
Palette(int size)
Initializes a new instance with the specified size.
Palette(FIBITMAP dib)
Initializes a new instance for the given FreeImage bitmap.
Provides methods for working with the standard bitmap palette.
The RGBQUAD structure describes a color consisting of relative intensities of red, green, blue and alpha value. Each single color component consumes 8 bits and so, takes values in the range from 0 to 255.
virtual void EnsureNotDisposed()
Throws an ObjectDisposedException if this instance is disposed.
RGBQUAD[] ToArray()
Returns the palette as an array of RGBQUAD.
SiliconStudio.Core.Mathematics.Color Color
void Save(Stream stream)
Saves this Palette to the specified stream.
byte * baseAddress
Baseaddress of the wrapped memory.
void CreateGrayscalePalette(bool inverse)
Creates a linear grayscale palette.
void Save(string filename)
Saves this Palette to the specified file.
void CopyFrom(Palette palette)
Copies the values from the specified Palette to this instance.
int length
Number of elements being wrapped.
void CreateGrayscalePalette()
Creates a linear grayscale palette.
Palette(MetadataTag tag)
Initializes a new instance for the given MetadataTag that contains a palette.
The FITAG structure is a handle to a FreeImage metadata tag.
void Save(BinaryWriter writer)
Saves this Palette using the specified writer.
void CopyFrom(Palette palette, int offset)
Copies the values from the specified Palette to this instance, starting at the specified offset ...
void Colorize(Color color, double splitSize)
Creates a linear palette based on the provided color .
void Colorize(Color color, int splitSize)
Creates a linear palette based on the provided color .
Palette(FITAG tag)
Initializes a new instance for the given FITAG that contains a palette.
int Count
Gets a 32-bit integer that represents the total number of elements in the MemoryArray<T>.
void CreateGrayscalePalette(Color color, bool inverse)
Creates a linear palette with the specified Color.
byte[] ToByteArray()
Returns the represented block of memory as an array of Byte.
bool IsNull
Gets whether the handle is a null or not.
T[] Data
Gets or sets the values of the unmanaged array.
void Reverse()
Reverses the palette.