1 using System;
2 using System.Diagnostics;
3 using System.Drawing;
4 
5 namespace FreeImageAPI.Metadata
6 {
7 	/// <summary>
8 	/// Provides additional information specific for GIF files. This class cannot be inherited.
9 	/// </summary>
10 	public class GifInformation : MDM_ANIMATION
11 	{
12 		/// <summary>
13 		/// Initializes a new instance of the <see cref="GifInformation"/> class
14 		/// with the specified <see cref="FreeImageBitmap"/>.
15 		/// </summary>
16 		/// <param name="bitmap">A reference to a <see cref="FreeImageBitmap"/> instance.</param>
GifInformation(FreeImageBitmap bitmap)17 		public GifInformation(FreeImageBitmap bitmap)
18 			: base(bitmap.Dib)
19 		{
20 		}
21 
22 		/// <summary>
23 		/// Gets or sets a value indicating whether this frame uses the
24 		/// GIF image's global palette. If set to <b>false</b>, this
25 		/// frame uses its local palette.
26 		/// </summary>
27 		/// <remarks>
28 		/// <b>Handling of null values</b><para/>
29 		/// A null value indicates, that the corresponding metadata tag is not
30 		/// present in the metadata model.
31 		/// Setting this property's value to a non-null reference creates the
32 		/// metadata tag if necessary.
33 		/// Setting this property's value to a null reference deletes the
34 		/// metadata tag from the metadata model.
35 		/// </remarks>
36 		public bool? UseGlobalPalette
37 		{
38 			get
39 			{
40 				byte? useGlobalPalette = GetTagValue<byte>("NoLocalPalette");
41 				return useGlobalPalette.HasValue ? (useGlobalPalette.Value != 0) : default(bool?);
42 			}
43 			set
44 			{
45 				byte? val = null;
46 				if (value.HasValue)
47 				{
48 					val = (byte)(value.Value ? 1 : 0);
49 				}
50 				SetTagValue("NoLocalPalette", val);
51 			}
52 		}
53 
54 		/// <summary>
55 		/// Creates a global palette for the GIF image, intialized with all entries of the
56 		/// current local palette.
57 		/// The property <see cref="UseGlobalPalette"/> will be set to <b>true</b> when
58 		/// invoking this method. This effectively enables the newly created global palette.
59 		/// </summary>
60 		/// <exception cref="InvalidOperationException">
61 		/// The image does not have a palette.
62 		/// </exception>
CreateGlobalPalette()63 		public void CreateGlobalPalette()
64 		{
65 			CreateGlobalPalette(new Palette(dib));
66 		}
67 
68 		/// <summary>
69 		/// Creates a global palette for the GIF image with the specified size, intialized
70 		/// with the first <paramref name="size"/> entries of the current local palette.
71 		/// The property <see cref="UseGlobalPalette"/> will be set to <b>true</b> when
72 		/// invoking this method. This effectively enables the newly created global palette.
73 		/// </summary>
74 		/// <param name="size">The size of the newly created global palette.</param>
75 		/// <exception cref="ArgumentNullException">
76 		/// <paramref name="palette"/> is a null reference.</exception>
CreateGlobalPalette(int size)77 		public void CreateGlobalPalette(int size)
78 		{
79 			CreateGlobalPalette(new Palette(dib), size);
80 		}
81 
82 		/// <summary>
83 		/// Creates a global palette for the GIF image, intialized with the entries
84 		/// of the specified palette.
85 		/// The property <see cref="UseGlobalPalette"/> will be set to <b>true</b> when
86 		/// invoking this method. This effectively enables the newly created global palette.
87 		/// </summary>
88 		/// <param name="palette">The palette that contains the initial values for
89 		/// the newly created global palette.</param>
90 		/// <exception cref="ArgumentNullException">
91 		/// <paramref name="palette"/> is a null reference.</exception>
CreateGlobalPalette(Palette palette)92 		public void CreateGlobalPalette(Palette palette)
93 		{
94 			if (palette == null)
95 			{
96 				throw new ArgumentNullException("palette");
97 			}
98 
99 			GlobalPalette = palette;
100 			UseGlobalPalette = true;
101 		}
102 
103 		/// <summary>
104 		/// Creates a global palette for the GIF image with the specified size, intialized
105 		/// with the first <paramref name="size"/> entries of the specified palette.
106 		/// The property <see cref="UseGlobalPalette"/> will be set to <b>true</b> when
107 		/// invoking this method. This effectively enables the newly created global palette.
108 		/// </summary>
109 		/// <param name="palette">The palette that contains the initial values for
110 		/// the newly created global palette.</param>
111 		/// <param name="size">The size of the newly created global palette.</param>
112 		/// <exception cref="ArgumentNullException">
113 		/// <paramref name="palette"/> is a null reference.</exception>
CreateGlobalPalette(Palette palette, int size)114 		public void CreateGlobalPalette(Palette palette, int size)
115 		{
116 			if (palette == null)
117 			{
118 				throw new ArgumentNullException("palette");
119 			}
120 			if (size <= 0)
121 			{
122 				throw new ArgumentOutOfRangeException("size");
123 			}
124 
125 			Palette pal = new Palette(size);
126 			pal.CopyFrom(palette);
127 			GlobalPalette = palette;
128 			UseGlobalPalette = true;
129 		}
130 	}
131 }