From 4974f5a0828871f273d43a8a2ae95a4161aca9e0 Mon Sep 17 00:00:00 2001 From: Michael Jumper Date: Sun, 1 Apr 2012 21:20:08 -0700 Subject: [PATCH] Handle formats, basic palette mapping implementation. --- libguac/src/protocol.c | 159 +++++++++++++++++++++++++++++++++++------ 1 file changed, 136 insertions(+), 23 deletions(-) diff --git a/libguac/src/protocol.c b/libguac/src/protocol.c index 3f99fb68..00c34097 100644 --- a/libguac/src/protocol.c +++ b/libguac/src/protocol.c @@ -151,45 +151,111 @@ void __guac_socket_flush_png(png_structp png) { /* Dummy function */ } -int __guac_socket_write_length_png(guac_socket* socket, cairo_surface_t* surface) { +typedef struct __guac_palette { - png_structp png; - png_infop png_info; - png_byte** png_rows; + int index; + int color; + +} __guac_palette; + +void __guac_create_palette(cairo_surface_t* surface) { int x, y; - __guac_socket_write_png_data png_data; - int base64_length; - - /* Get image surface properties and data */ - /*cairo_format_t format = cairo_image_surface_get_format(surface);*/ 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); - /* Flush pending operations to surface */ - cairo_surface_flush(surface); + /* Simple palette map */ + __guac_palette palette[0xFFF] = {{0}}; + int colors = 0; + + for (y=0; y> 12) ^ (color & 0xFFF); + + __guac_palette* entry; + + /* Search for open palette entry */ + for (;;) { + + entry = &palette[hash]; + + /* If we've found a free space, use it */ + if (entry->index == 0) { + + /* Stop if already at capacity */ + if (colors == 256) + return; + + /* Add color to map, done */ + entry->index = ++colors; + entry->color = color; + 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; - /* If not an image surface, fail */ - if (data == NULL) { - return -1; /* FIXME: Set guac_error, etc? */ } + fprintf(stderr, "%i colors!\n", colors); + +} + +png_byte** __guac_create_png_rgb(cairo_surface_t* surface, int alpha) { + + png_byte** png_rows; + 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); + + /* Fail if not an image surface */ + if (data == NULL) + return NULL; + + int bpp; + if (alpha) bpp = 4; + else bpp = 3; + /* Copy data from surface into PNG data */ png_rows = (png_byte**) malloc(sizeof(png_byte*) * height); for (y=0; y