From ec652af924ec9c667505c2811d57de0917f7ee7d Mon Sep 17 00:00:00 2001 From: Michael Jumper Date: Sun, 1 Apr 2012 22:36:55 -0700 Subject: [PATCH] Store palette in libpng format, add PLTE section. --- libguac/include/palette.h | 3 ++- libguac/src/palette.c | 12 ++++++++++-- libguac/src/protocol.c | 3 +++ 3 files changed, 15 insertions(+), 3 deletions(-) diff --git a/libguac/include/palette.h b/libguac/include/palette.h index d2263e95..ce6256f0 100644 --- a/libguac/include/palette.h +++ b/libguac/include/palette.h @@ -38,6 +38,7 @@ #ifndef __GUAC_PALETTE_H #define __GUAC_PALETTE_H +#include #include typedef struct guac_palette_entry { @@ -51,7 +52,7 @@ typedef struct guac_palette_entry { typedef struct guac_palette { guac_palette_entry entries[0xFFF]; - int colors[256]; + png_color colors[256]; int size; } guac_palette; diff --git a/libguac/src/palette.c b/libguac/src/palette.c index 8d38c771..be1a6518 100644 --- a/libguac/src/palette.c +++ b/libguac/src/palette.c @@ -79,16 +79,24 @@ guac_palette* guac_palette_alloc(cairo_surface_t* surface) { /* If we've found a free space, use it */ if (entry->index == 0) { + png_color* c; + /* Stop if already at capacity */ if (palette->size == 256) { guac_palette_free(palette); return NULL; } - /* Add color to map, done */ - palette->colors[palette->size] = color; + /* Store in palette */ + c = &(palette->colors[palette->size]); + c->blue = (color & 0x0000FF); + c->green = (color & 0x00FF00) >> 8; + c->red = (color & 0xFF0000) >> 16; + + /* Add color to map */ entry->index = ++palette->size; entry->color = color; + break; } diff --git a/libguac/src/protocol.c b/libguac/src/protocol.c index 160b91d6..3e93f3b5 100644 --- a/libguac/src/protocol.c +++ b/libguac/src/protocol.c @@ -263,6 +263,9 @@ int __guac_socket_write_length_png(guac_socket* socket, cairo_surface_t* surface PNG_FILTER_TYPE_DEFAULT ); + /* Write palette */ + png_set_PLTE(png, png_info, palette->colors, palette->size); + /* Write image */ png_set_rows(png, png_info, png_rows); png_write_png(png, png_info, PNG_TRANSFORM_IDENTITY, NULL);