2
using System.Collections.Generic;
6
using FreeImageAPI.Metadata;
7
using System.Runtime.InteropServices;
8
using System.Diagnostics;
10
namespace FreeImageAPI
13
/// Provides methods for working with the standard bitmap palette.
15
public sealed class Palette : MemoryArray<RGBQUAD>
17
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
18
private GCHandle paletteHandle;
20
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
21
private RGBQUAD[] array;
24
/// Initializes a new instance for the given FreeImage bitmap.
26
/// <param name="dib">Handle to a FreeImage bitmap.</param>
27
/// <exception cref="ArgumentNullException"><paramref name="dib"/> is null.</exception>
28
/// <exception cref="ArgumentException"><paramref name="dib"/> is not
29
/// <see cref="FREE_IMAGE_TYPE.FIT_BITMAP"/><para/>-or-<para/>
30
/// <paramref name="dib"/> has more than 8bpp.</exception>
31
public Palette(FIBITMAP dib)
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");
49
/// Initializes a new instance for the given FITAG that contains
52
/// <param name="tag">The tag containing the palette.</param>
53
/// <exception cref="ArgumentNullException"><paramref name="tag"/> is null.</exception>
54
/// <exception cref="ArgumentException"><paramref name="tag"/> is not
55
/// <see cref="FREE_IMAGE_MDTYPE.FIDT_PALETTE"/>.</exception>
56
public Palette(FITAG tag)
57
: base(FreeImage.GetTagValue(tag), (int)FreeImage.GetTagCount(tag))
59
if (FreeImage.GetTagType(tag) != FREE_IMAGE_MDTYPE.FIDT_PALETTE)
61
throw new ArgumentException("tag");
66
/// Initializes a new instance for the given MetadataTag that contains
69
/// <param name="tag">The tag containing the palette.</param>
70
/// <exception cref="ArgumentNullException"><paramref name="dib"/> is null.</exception>
71
/// <exception cref="ArgumentException"><paramref name="tag"/> is not
72
/// <see cref="FREE_IMAGE_MDTYPE.FIDT_PALETTE"/>.</exception>
73
public Palette(MetadataTag 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");
83
/// Initializes a new instance for the given array of <see cref="RGBQUAD"/> that contains
86
/// <param name="palette">A RGBQUAD array containing the palette data to initialize this instance.</param>
87
public Palette(RGBQUAD[] palette)
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;
97
// Create an array containing a single element.
98
// Due to the fact, that it's not possible to create pointers
99
// of generic types, an array is used to obtain the memory
100
// address of an element of T.
101
base.buffer = new RGBQUAD[1];
102
// The array is pinned immediately to prevent the GC from
103
// moving it to a different position in memory.
104
base.handle = GCHandle.Alloc(buffer, GCHandleType.Pinned);
105
// The array and its content have beed pinned, so that its address
106
// can be safely requested and stored for the whole lifetime
108
base.ptr = (byte*)base.handle.AddrOfPinnedObject();
113
/// Initializes a new instance for the given array of <see cref="Color"/> that contains
116
/// <param name="palette">A Color array containing the palette data to initialize this instance.</param>
117
public Palette(Color[] palette)
118
: this(RGBQUAD.ToRGBQUAD(palette))
123
/// Initializes a new instance with the specified size.
125
/// <param name="size">The size of the palette.</param>
126
public Palette(int size)
127
: this(new RGBQUAD[size])
132
/// Gets or sets the palette through an array of <see cref="RGBQUAD"/>.
134
public RGBQUAD[] AsArray
147
/// Get an array of <see cref="System.Drawing.Color"/> that the block of memory represents.
148
/// This property is used for internal palette operations.
150
internal unsafe Color[] ColorData
155
Color[] data = new Color[length];
156
for (int i = 0; i < length; i++)
158
data[i] = Color.FromArgb((int)(((uint*)baseAddress)[i] | 0xFF000000));
165
/// Returns the palette as an array of <see cref="RGBQUAD"/>.
167
/// <returns>The palette as an array of <see cref="RGBQUAD"/>.</returns>
168
public RGBQUAD[] ToArray()
174
/// Creates a linear palette based on the provided <paramref name="color"/>.
176
/// <param name="color">The <see cref="System.Drawing.Color"/> used to colorize the palette.</param>
178
/// Only call this method on linear palettes.
180
public void Colorize(Color color)
182
Colorize(color, 0.5d);
186
/// Creates a linear palette based on the provided <paramref name="color"/>.
188
/// <param name="color">The <see cref="System.Drawing.Color"/> used to colorize the palette.</param>
189
/// <param name="splitSize">The position of the color within the new palette.
190
/// 0 < <paramref name="splitSize"/> < 1.</param>
192
/// Only call this method on linear palettes.
194
public void Colorize(Color color, double splitSize)
196
Colorize(color, (int)(length * splitSize));
200
/// Creates a linear palette based on the provided <paramref name="color"/>.
202
/// <param name="color">The <see cref="System.Drawing.Color"/> used to colorize the palette.</param>
203
/// <param name="splitSize">The position of the color within the new palette.
204
/// 0 < <paramref name="splitSize"/> < <see cref="MemoryArray<T>.Length"/>.</param>
206
/// Only call this method on linear palettes.
208
public void Colorize(Color color, int splitSize)
211
if (splitSize < 1 || splitSize >= length)
213
throw new ArgumentOutOfRangeException("splitSize");
216
RGBQUAD[] pal = new RGBQUAD[length];
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);
240
for (; i < length; i++)
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));
251
/// Creates a linear grayscale palette.
253
public void CreateGrayscalePalette()
255
Colorize(Color.White, length - 1);
259
/// Creates a linear grayscale palette.
261
/// <param name="inverse"><b>true</b> to create an inverse grayscale palette.</param>
262
public void CreateGrayscalePalette(bool inverse)
264
Colorize(Color.White, inverse ? 0 : length - 1);
268
/// Creates a linear palette with the specified <see cref="Color"/>.
271
/// A linear grayscale palette contains all shades of colors from
272
/// black to white. This method creates a similar palette with the white
273
/// color being replaced by the specified color.
275
/// <param name="color">The <see cref="Color"/> used to create the palette.</param>
276
/// <param name="inverse"><b>true</b> to create an inverse palette.</param>
277
public void CreateGrayscalePalette(Color color, bool inverse)
279
Colorize(color, inverse ? 0 : length - 1);
283
/// Reverses the palette.
285
public void Reverse()
290
Array.Reverse(array);
294
RGBQUAD[] localArray = Data;
295
Array.Reverse(localArray);
301
/// Copies the values from the specified <see cref="Palette"/> to this instance.
303
/// <param name="palette">The palette to copy from.</param>
304
/// <exception cref="ArgumentNullException">
305
/// <paramref name="palette"/> is a null reference.</exception>
306
public void CopyFrom(Palette palette)
311
throw new ArgumentNullException("palette");
313
CopyFrom(palette.Data, 0, 0, Math.Min(palette.Length, this.Length));
317
/// Copies the values from the specified <see cref="Palette"/> to this instance,
318
/// starting at the specified <paramref name="offset"/>.
320
/// <param name="palette">The palette to copy from.</param>
321
/// <param name="offset">The position in this instance where the values
322
/// will be copied to.</param>
323
/// <exception cref="ArgumentNullException">
324
/// <paramref name="palette"/> is a null reference.</exception>
325
/// <exception cref="ArgumentOutOfRangeException">
326
/// <paramref name="offset"/> is outside the range of valid indexes.</exception>
327
public void CopyFrom(Palette palette, int offset)
330
CopyFrom(palette.Data, 0, offset, Math.Min(palette.Length, this.Length - offset));
334
/// Saves this <see cref="Palette"/> to the specified file.
336
/// <param name="filename">
337
/// A string that contains the name of the file to which to save this <see cref="Palette"/>.
339
public void Save(string filename)
341
using (Stream stream = new FileStream(filename, FileMode.Create, FileAccess.Write))
348
/// Saves this <see cref="Palette"/> to the specified stream.
350
/// <param name="stream">
351
/// The <see cref="Stream"/> where the image will be saved.
353
public void Save(Stream stream)
355
Save(new BinaryWriter(stream));
359
/// Saves this <see cref="Palette"/> using the specified writer.
361
/// <param name="writer">
362
/// The <see cref="BinaryWriter"/> used to save the image.
364
public void Save(BinaryWriter writer)
367
writer.Write(ToByteArray());
371
/// Loads a palette from the specified file.
373
/// <param name="filename">The name of the palette file.</param>
374
public void Load(string filename)
376
using (Stream stream = new FileStream(filename, FileMode.Open, FileAccess.Read))
383
/// Loads a palette from the specified stream.
385
/// <param name="stream">The stream to load the palette from.</param>
386
public void Load(Stream stream)
388
Load(new BinaryReader(stream));
392
/// Loads a palette from the reader.
394
/// <param name="reader">The reader to load the palette from.</param>
395
public void Load(BinaryReader reader)
400
int size = length * sizeof(RGBQUAD);
401
byte[] data = reader.ReadBytes(size);
402
fixed (byte* src = data)
404
CopyMemory(baseAddress, src, data.Length);
410
/// Releases allocated handles associated with this instance.
412
/// <param name="disposing"><b>true</b> to release managed resources.</param>
413
protected override void Dispose(bool disposing)
415
if (paletteHandle.IsAllocated)
416
paletteHandle.Free();
419
base.Dispose(disposing);
b'\\ No newline at end of file'