078af159161be2670864798770736abfc52a29aa
[mono.git] / mcs / class / System.Drawing / System.Drawing.Imaging / ColorPalette.cs
1 //
2 // System.Drawing.Imaging.ColorPalette.cs
3 //
4 // (C) 2002 Ximian, Inc.  http://www.ximian.com
5 //
6 // Author:
7 //   Miguel de Icaza (miguel@ximian.com)
8 //
9
10 //
11 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
12 //
13 // Permission is hereby granted, free of charge, to any person obtaining
14 // a copy of this software and associated documentation files (the
15 // "Software"), to deal in the Software without restriction, including
16 // without limitation the rights to use, copy, modify, merge, publish,
17 // distribute, sublicense, and/or sell copies of the Software, and to
18 // permit persons to whom the Software is furnished to do so, subject to
19 // the following conditions:
20 // 
21 // The above copyright notice and this permission notice shall be
22 // included in all copies or substantial portions of the Software.
23 // 
24 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
27 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
28 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
29 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
30 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
31 //
32
33 using System;
34 using System.Drawing;
35 using System.Runtime.InteropServices;
36
37 namespace System.Drawing.Imaging
38 {
39         public sealed class ColorPalette {
40                 // 0x1: the color values in the array contain alpha information
41                 // 0x2: the color values are grayscale values.
42                 // 0x4: the colors in the array are halftone values.
43
44                 private int flags;
45                 private Color [] entries;
46
47                 //
48                 // There is no public constructor, this will be used somewhere in the
49                 // drawing code
50                 //
51                 internal ColorPalette ()
52                 {
53                         entries = new Color [0];
54                 }
55
56                 internal ColorPalette (int flags, Color[] colors) {
57                         this.flags = flags;
58                         entries = colors;
59                 }
60
61                 public Color [] Entries {
62                         get {
63                                 return entries;
64                         }
65                 }
66
67                 public int Flags {
68                         get {
69                                 return flags;
70                         }
71                 }
72 #if !TARGET_JVM
73                 /* Caller should call FreeHGlobal*/
74                 internal IntPtr getGDIPalette() 
75                 {
76                         GdiColorPalette palette = new GdiColorPalette ();                       
77                         Color[] entries = Entries;
78                         int entry = 0;                  
79                         int size = Marshal.SizeOf (palette) + (Marshal.SizeOf (entry) * entries.Length);                        
80                         IntPtr lfBuffer = Marshal.AllocHGlobal(size);                   
81                         
82                         palette.Flags = Flags;
83                         palette.Count = entries.Length;
84                                                 
85                         int[] values = new int[palette.Count];
86                         
87                         for (int i = 0; i < values.Length; i++) {
88                                 values[i] = entries[i].ToArgb();                                
89                         }                       
90                         
91                         Marshal.StructureToPtr (palette, lfBuffer, false);      
92                         Marshal.Copy (values, 0, (IntPtr) (lfBuffer.ToInt64() + Marshal.SizeOf (palette)), values.Length);
93                         
94                         return lfBuffer;
95                 }
96                         
97                 internal void setFromGDIPalette (IntPtr palette)
98                 {
99                         IntPtr ptr = palette;
100                         int cnt, color;
101                         int offset;
102
103                         flags = Marshal.ReadInt32 (ptr); ptr = (IntPtr) (ptr.ToInt64() + 4);
104                         cnt = Marshal.ReadInt32 (ptr); ptr = (IntPtr) (ptr.ToInt64() + 4);
105                                                 
106                         entries = new Color [cnt];                      
107                                                 
108                         offset = 0;
109                         for (int i = 0; i < cnt; i++) {
110                                 color = Marshal.ReadInt32 (ptr, offset);
111                                 entries[i] = Color.FromArgb (color);
112                                 offset += 4;
113                         }
114                 }
115 #endif
116         }
117 }