Blame libfreerdp/cache/palette.c

Packit 1fb8d4
/**
Packit 1fb8d4
 * FreeRDP: A Remote Desktop Protocol Implementation
Packit 1fb8d4
 * Palette (Color Table) Cache
Packit 1fb8d4
 *
Packit 1fb8d4
 * Copyright 2011 Marc-Andre Moreau <marcandre.moreau@gmail.com>
Packit 1fb8d4
 *
Packit 1fb8d4
 * Licensed under the Apache License, Version 2.0 (the "License");
Packit 1fb8d4
 * you may not use this file except in compliance with the License.
Packit 1fb8d4
 * You may obtain a copy of the License at
Packit 1fb8d4
 *
Packit 1fb8d4
 *     http://www.apache.org/licenses/LICENSE-2.0
Packit 1fb8d4
 *
Packit 1fb8d4
 * Unless required by applicable law or agreed to in writing, software
Packit 1fb8d4
 * distributed under the License is distributed on an "AS IS" BASIS,
Packit 1fb8d4
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
Packit 1fb8d4
 * See the License for the specific language governing permissions and
Packit 1fb8d4
 * limitations under the License.
Packit 1fb8d4
 */
Packit 1fb8d4
Packit 1fb8d4
#ifdef HAVE_CONFIG_H
Packit 1fb8d4
#include "config.h"
Packit 1fb8d4
#endif
Packit 1fb8d4
Packit 1fb8d4
#include <stdio.h>
Packit 1fb8d4
Packit 1fb8d4
#include <winpr/crt.h>
Packit 1fb8d4
Packit 1fb8d4
#include <freerdp/log.h>
Packit 1fb8d4
#include <freerdp/cache/palette.h>
Packit 1fb8d4
Packit 1fb8d4
#include "palette.h"
Packit 1fb8d4
Packit 1fb8d4
#define TAG FREERDP_TAG("cache.palette")
Packit 1fb8d4
Packit 1fb8d4
static void* palette_cache_get(rdpPaletteCache* palette, UINT32 index);
Packit 1fb8d4
Packit 1fb8d4
static void palette_cache_put(rdpPaletteCache* palette, UINT32 index, void* entry);
Packit 1fb8d4
Packit 1fb8d4
static BOOL update_gdi_cache_color_table(rdpContext* context,
Packit Service 5a9772
                                         const CACHE_COLOR_TABLE_ORDER* cacheColorTable)
Packit 1fb8d4
{
Packit 1fb8d4
	UINT32* colorTable;
Packit 1fb8d4
	rdpCache* cache = context->cache;
Packit Service 5a9772
	colorTable = (UINT32*)malloc(sizeof(UINT32) * 256);
Packit 1fb8d4
Packit 1fb8d4
	if (!colorTable)
Packit 1fb8d4
		return FALSE;
Packit 1fb8d4
Packit 1fb8d4
	CopyMemory(colorTable, cacheColorTable->colorTable, sizeof(UINT32) * 256);
Packit Service 5a9772
	palette_cache_put(cache->palette, cacheColorTable->cacheIndex, (void*)colorTable);
Packit 1fb8d4
	return TRUE;
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
void* palette_cache_get(rdpPaletteCache* paletteCache, UINT32 index)
Packit 1fb8d4
{
Packit 1fb8d4
	void* entry;
Packit 1fb8d4
Packit 1fb8d4
	if (index >= paletteCache->maxEntries)
Packit 1fb8d4
	{
Packit Service 5a9772
		WLog_ERR(TAG, "invalid color table index: 0x%08" PRIX32 "", index);
Packit 1fb8d4
		return NULL;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	entry = paletteCache->entries[index].entry;
Packit 1fb8d4
Packit 1fb8d4
	if (!entry)
Packit 1fb8d4
	{
Packit Service 5a9772
		WLog_ERR(TAG, "invalid color table at index: 0x%08" PRIX32 "", index);
Packit 1fb8d4
		return NULL;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	return entry;
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
void palette_cache_put(rdpPaletteCache* paletteCache, UINT32 index, void* entry)
Packit 1fb8d4
{
Packit 1fb8d4
	if (index >= paletteCache->maxEntries)
Packit 1fb8d4
	{
Packit Service 5a9772
		WLog_ERR(TAG, "invalid color table index: 0x%08" PRIX32 "", index);
Packit 1fb8d4
		free(entry);
Packit 1fb8d4
		return;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	free(paletteCache->entries[index].entry);
Packit 1fb8d4
	paletteCache->entries[index].entry = entry;
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
void palette_cache_register_callbacks(rdpUpdate* update)
Packit 1fb8d4
{
Packit 1fb8d4
	update->secondary->CacheColorTable = update_gdi_cache_color_table;
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
rdpPaletteCache* palette_cache_new(rdpSettings* settings)
Packit 1fb8d4
{
Packit 1fb8d4
	rdpPaletteCache* paletteCache;
Packit Service 5a9772
	paletteCache = (rdpPaletteCache*)calloc(1, sizeof(rdpPaletteCache));
Packit 1fb8d4
Packit 1fb8d4
	if (paletteCache)
Packit 1fb8d4
	{
Packit 1fb8d4
		paletteCache->settings = settings;
Packit 1fb8d4
		paletteCache->maxEntries = 6;
Packit Service 5a9772
		paletteCache->entries =
Packit Service 5a9772
		    (PALETTE_TABLE_ENTRY*)calloc(paletteCache->maxEntries, sizeof(PALETTE_TABLE_ENTRY));
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	return paletteCache;
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
void palette_cache_free(rdpPaletteCache* paletteCache)
Packit 1fb8d4
{
Packit 1fb8d4
	if (paletteCache)
Packit 1fb8d4
	{
Packit 1fb8d4
		UINT32 i;
Packit 1fb8d4
Packit 1fb8d4
		for (i = 0; i < paletteCache->maxEntries; i++)
Packit 1fb8d4
			free(paletteCache->entries[i].entry);
Packit 1fb8d4
Packit 1fb8d4
		free(paletteCache->entries);
Packit 1fb8d4
		free(paletteCache);
Packit 1fb8d4
	}
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
void free_palette_update(rdpContext* context, PALETTE_UPDATE* pointer)
Packit 1fb8d4
{
Packit 1fb8d4
	free(pointer);
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
PALETTE_UPDATE* copy_palette_update(rdpContext* context, const PALETTE_UPDATE* pointer)
Packit 1fb8d4
{
Packit 1fb8d4
	PALETTE_UPDATE* dst = calloc(1, sizeof(PALETTE_UPDATE));
Packit 1fb8d4
Packit 1fb8d4
	if (!dst || !pointer)
Packit 1fb8d4
		goto fail;
Packit 1fb8d4
Packit 1fb8d4
	*dst = *pointer;
Packit 1fb8d4
	return dst;
Packit 1fb8d4
fail:
Packit 1fb8d4
	free_palette_update(context, dst);
Packit 1fb8d4
	return NULL;
Packit 1fb8d4
}