/src/FreeRDP/libfreerdp/cache/palette.c
Line  | Count  | Source (jump to first uncovered line)  | 
1  |  | /**  | 
2  |  |  * FreeRDP: A Remote Desktop Protocol Implementation  | 
3  |  |  * Palette (Color Table) Cache  | 
4  |  |  *  | 
5  |  |  * Copyright 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  |  | #include <freerdp/config.h>  | 
21  |  |  | 
22  |  | #include <stdio.h>  | 
23  |  |  | 
24  |  | #include <winpr/crt.h>  | 
25  |  |  | 
26  |  | #include <freerdp/log.h>  | 
27  |  |  | 
28  |  | #include "palette.h"  | 
29  |  | #include "cache.h"  | 
30  |  |  | 
31  |  | #define TAG FREERDP_TAG("cache.palette") | 
32  |  |  | 
33  |  | static void* palette_cache_get(rdpPaletteCache* palette, UINT32 index);  | 
34  |  |  | 
35  |  | static void palette_cache_put(rdpPaletteCache* palette, UINT32 index, void* entry);  | 
36  |  |  | 
37  |  | static BOOL update_gdi_cache_color_table(rdpContext* context,  | 
38  |  |                                          const CACHE_COLOR_TABLE_ORDER* cacheColorTable)  | 
39  | 0  | { | 
40  | 0  |   UINT32* colorTable = NULL;  | 
41  | 0  |   rdpCache* cache = context->cache;  | 
42  | 0  |   colorTable = (UINT32*)malloc(sizeof(UINT32) * 256);  | 
43  |  | 
  | 
44  | 0  |   if (!colorTable)  | 
45  | 0  |     return FALSE;  | 
46  |  |  | 
47  | 0  |   CopyMemory(colorTable, cacheColorTable->colorTable, sizeof(UINT32) * 256);  | 
48  | 0  |   palette_cache_put(cache->palette, cacheColorTable->cacheIndex, (void*)colorTable);  | 
49  | 0  |   return TRUE;  | 
50  | 0  | }  | 
51  |  |  | 
52  |  | void* palette_cache_get(rdpPaletteCache* paletteCache, UINT32 index)  | 
53  | 0  | { | 
54  | 0  |   void* entry = NULL;  | 
55  | 0  | 
  | 
56  | 0  |   if (index >= paletteCache->maxEntries)  | 
57  | 0  |   { | 
58  | 0  |     WLog_ERR(TAG, "invalid color table index: 0x%08" PRIX32 "", index);  | 
59  | 0  |     return NULL;  | 
60  | 0  |   }  | 
61  | 0  | 
  | 
62  | 0  |   entry = paletteCache->entries[index].entry;  | 
63  | 0  | 
  | 
64  | 0  |   if (!entry)  | 
65  | 0  |   { | 
66  | 0  |     WLog_ERR(TAG, "invalid color table at index: 0x%08" PRIX32 "", index);  | 
67  | 0  |     return NULL;  | 
68  | 0  |   }  | 
69  | 0  | 
  | 
70  | 0  |   return entry;  | 
71  | 0  | }  | 
72  |  |  | 
73  |  | void palette_cache_put(rdpPaletteCache* paletteCache, UINT32 index, void* entry)  | 
74  | 0  | { | 
75  | 0  |   if (index >= paletteCache->maxEntries)  | 
76  | 0  |   { | 
77  | 0  |     WLog_ERR(TAG, "invalid color table index: 0x%08" PRIX32 "", index);  | 
78  | 0  |     free(entry);  | 
79  | 0  |     return;  | 
80  | 0  |   }  | 
81  |  |  | 
82  | 0  |   free(paletteCache->entries[index].entry);  | 
83  | 0  |   paletteCache->entries[index].entry = entry;  | 
84  | 0  | }  | 
85  |  |  | 
86  |  | void palette_cache_register_callbacks(rdpUpdate* update)  | 
87  | 0  | { | 
88  | 0  |   update->secondary->CacheColorTable = update_gdi_cache_color_table;  | 
89  | 0  | }  | 
90  |  |  | 
91  |  | rdpPaletteCache* palette_cache_new(rdpContext* context)  | 
92  | 0  | { | 
93  | 0  |   rdpPaletteCache* paletteCache = NULL;  | 
94  |  | 
  | 
95  | 0  |   WINPR_ASSERT(context);  | 
96  |  |  | 
97  | 0  |   paletteCache = (rdpPaletteCache*)calloc(1, sizeof(rdpPaletteCache));  | 
98  |  | 
  | 
99  | 0  |   if (paletteCache)  | 
100  | 0  |   { | 
101  | 0  |     paletteCache->context = context;  | 
102  | 0  |     paletteCache->maxEntries = 6;  | 
103  | 0  |     paletteCache->entries =  | 
104  | 0  |         (PALETTE_TABLE_ENTRY*)calloc(paletteCache->maxEntries, sizeof(PALETTE_TABLE_ENTRY));  | 
105  | 0  |   }  | 
106  |  | 
  | 
107  | 0  |   return paletteCache;  | 
108  | 0  | }  | 
109  |  |  | 
110  |  | void palette_cache_free(rdpPaletteCache* paletteCache)  | 
111  | 0  | { | 
112  | 0  |   if (paletteCache)  | 
113  | 0  |   { | 
114  | 0  |     for (UINT32 i = 0; i < paletteCache->maxEntries; i++)  | 
115  | 0  |       free(paletteCache->entries[i].entry);  | 
116  |  | 
  | 
117  | 0  |     free(paletteCache->entries);  | 
118  | 0  |     free(paletteCache);  | 
119  | 0  |   }  | 
120  | 0  | }  | 
121  |  |  | 
122  |  | void free_palette_update(rdpContext* context, PALETTE_UPDATE* pointer)  | 
123  | 0  | { | 
124  | 0  |   free(pointer);  | 
125  | 0  | }  | 
126  |  |  | 
127  |  | PALETTE_UPDATE* copy_palette_update(rdpContext* context, const PALETTE_UPDATE* pointer)  | 
128  | 0  | { | 
129  | 0  |   PALETTE_UPDATE* dst = calloc(1, sizeof(PALETTE_UPDATE));  | 
130  |  | 
  | 
131  | 0  |   if (!dst || !pointer)  | 
132  | 0  |     goto fail;  | 
133  |  |  | 
134  | 0  |   *dst = *pointer;  | 
135  | 0  |   return dst;  | 
136  | 0  | fail:  | 
137  | 0  |   WINPR_PRAGMA_DIAG_PUSH  | 
138  | 0  |   WINPR_PRAGMA_DIAG_IGNORED_MISMATCHED_DEALLOC  | 
139  | 0  |   free_palette_update(context, dst);  | 
140  | 0  |   WINPR_PRAGMA_DIAG_POP  | 
141  | 0  |   return NULL;  | 
142  | 0  | }  |