Initial commit - from Precise source
[freerdp-ubuntu-pcb-backport.git] / libfreerdp-gdi / palette.c
1 /**
2  * FreeRDP: A Remote Desktop Protocol Client
3  * GDI Palette Functions
4  *
5  * Copyright 2010-2011 Marc-Andre Moreau <marcandre.moreau@gmail.com>
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *     http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  */
19
20 /* GDI Palette Functions: http://msdn.microsoft.com/en-us/library/dd183454/ */
21
22 #include <stdio.h>
23 #include <string.h>
24 #include <stdlib.h>
25
26 #include <freerdp/freerdp.h>
27 #include <freerdp/gdi/gdi.h>
28
29 #include <freerdp/gdi/palette.h>
30
31 static HGDI_PALETTE hSystemPalette = NULL;
32
33 static const GDI_PALETTEENTRY default_system_palette[20] =
34 {
35         /* First 10 entries */
36         { 0x00, 0x00, 0x00 },
37         { 0x80, 0x00, 0x00 },
38         { 0x00, 0x80, 0x00 },
39         { 0x80, 0x80, 0x00 },
40         { 0x00, 0x00, 0x80 },
41         { 0x80, 0x00, 0x80 },
42         { 0x00, 0x80, 0x80 },
43         { 0xC0, 0xC0, 0xC0 },
44         { 0xC0, 0xDC, 0xC0 },
45         { 0xA6, 0xCA, 0xF0 },
46
47         /* Last 10 entries */
48         { 0xFF, 0xFB, 0xF0 },
49         { 0xA0, 0xA0, 0xA4 },
50         { 0x80, 0x80, 0x80 },
51         { 0xFF, 0x00, 0x00 },
52         { 0x00, 0xFF, 0x00 },
53         { 0xFF, 0xFF, 0x00 },
54         { 0x00, 0x00, 0xFF },
55         { 0xFF, 0x00, 0xFF },
56         { 0x00, 0xFF, 0xFF },
57         { 0xFF, 0xFF, 0xFF }
58 };
59
60 /**
61  * Create a new palette.\n
62  * @msdn{dd183507}
63  * @param original palette
64  * @return new palette
65  */
66
67 HGDI_PALETTE gdi_CreatePalette(HGDI_PALETTE palette)
68 {
69         HGDI_PALETTE hPalette = (HGDI_PALETTE) malloc(sizeof(GDI_PALETTE));
70         hPalette->count = palette->count;
71         hPalette->entries = (GDI_PALETTEENTRY*) malloc(sizeof(GDI_PALETTEENTRY) * hPalette->count);
72         memcpy(hPalette->entries, palette->entries, sizeof(GDI_PALETTEENTRY) * hPalette->count);
73         return hPalette;
74 }
75
76 /**
77  * Create system palette\n
78  * @return system palette
79  */
80
81 HGDI_PALETTE CreateSystemPalette()
82 {
83         HGDI_PALETTE palette = (HGDI_PALETTE) malloc(sizeof(GDI_PALETTE));
84
85         palette->count = 256;
86         palette->entries = (GDI_PALETTEENTRY*) malloc(sizeof(GDI_PALETTEENTRY) * 256);
87         memset(palette->entries, 0, sizeof(GDI_PALETTEENTRY) * 256);
88
89         memcpy(&palette->entries[0], &default_system_palette[0], 10 * sizeof(GDI_PALETTEENTRY));
90         memcpy(&palette->entries[256 - 10], &default_system_palette[10], 10 * sizeof(GDI_PALETTEENTRY));
91
92         return palette;
93 }
94
95 /**
96  * Get system palette\n
97  * @return system palette
98  */
99
100 HGDI_PALETTE gdi_GetSystemPalette()
101 {
102         if (hSystemPalette == NULL)
103                 hSystemPalette = CreateSystemPalette();
104
105         return hSystemPalette;
106 }