guacamole-spice-protocol/src/libguac/palette.c

140 lines
4.0 KiB
C
Raw Normal View History

/*
* Copyright (C) 2013 Glyptodon LLC
2012-04-02 04:49:15 +00:00
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
2012-04-02 04:49:15 +00:00
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
2012-04-02 04:49:15 +00:00
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
2014-01-01 22:44:28 +00:00
#include "config.h"
2012-04-02 04:49:15 +00:00
2014-01-01 22:44:28 +00:00
#include "palette.h"
#include <inttypes.h>
2012-04-02 04:49:15 +00:00
#include <stdint.h>
2014-01-01 22:44:28 +00:00
#include <stdio.h>
#include <stdlib.h>
2012-04-02 04:49:15 +00:00
#include <string.h>
#include <cairo/cairo.h>
#include <sys/types.h>
2012-04-02 04:54:03 +00:00
guac_palette* guac_palette_alloc(cairo_surface_t* surface) {
2012-04-02 04:49:15 +00:00
int x, y;
int width = cairo_image_surface_get_width(surface);
int height = cairo_image_surface_get_height(surface);
int stride = cairo_image_surface_get_stride(surface);
unsigned char* data = cairo_image_surface_get_data(surface);
/* Allocate palette */
guac_palette* palette = (guac_palette*) malloc(sizeof(guac_palette));
memset(palette, 0, sizeof(guac_palette));
for (y=0; y<height; y++) {
for (x=0; x<width; x++) {
/* Get pixel color */
int color = ((uint32_t*) data)[x] & 0xFFFFFF;
/* Calculate hash code */
int hash = ((color & 0xFFF000) >> 12) ^ (color & 0xFFF);
guac_palette_entry* entry;
/* Search for open palette entry */
for (;;) {
entry = &(palette->entries[hash]);
/* If we've found a free space, use it */
if (entry->index == 0) {
png_color* c;
2012-04-02 04:49:15 +00:00
/* Stop if already at capacity */
if (palette->size == 256) {
2012-04-02 04:54:03 +00:00
guac_palette_free(palette);
2012-04-02 04:49:15 +00:00
return NULL;
}
/* Store in palette */
c = &(palette->colors[palette->size]);
2012-04-02 06:01:53 +00:00
c->blue = (color ) & 0xFF;
c->green = (color >> 8 ) & 0xFF;
c->red = (color >> 16) & 0xFF;
/* Add color to map */
entry->index = ++palette->size;
2012-04-02 04:49:15 +00:00
entry->color = color;
2012-04-02 04:49:15 +00:00
break;
}
/* Otherwise, if already stored here, done */
if (entry->color == color)
break;
/* Otherwise, collision. Move on to another bucket */
hash = (hash+1) & 0xFFF;
}
}
/* Advance to next data row */
data += stride;
}
return palette;
}
2012-04-02 04:57:19 +00:00
int guac_palette_find(guac_palette* palette, int color) {
/* Calculate hash code */
int hash = ((color & 0xFFF000) >> 12) ^ (color & 0xFFF);
guac_palette_entry* entry;
/* Search for palette entry */
for (;;) {
entry = &(palette->entries[hash]);
/* If we've found a free space, color not stored. */
if (entry->index == 0)
return -1;
/* Otherwise, if color indeed stored here, done */
if (entry->color == color)
2012-04-02 06:01:53 +00:00
return entry->index - 1;
2012-04-02 04:57:19 +00:00
/* Otherwise, collision. Move on to another bucket */
hash = (hash+1) & 0xFFF;
}
}
2012-04-02 04:54:03 +00:00
void guac_palette_free(guac_palette* palette) {
2012-04-02 04:49:15 +00:00
free(palette);
}