From 969f96f1b7e32ac41586e4fa47d3412aaf328a5b Mon Sep 17 00:00:00 2001 From: Michael Jumper Date: Sun, 1 Apr 2012 22:28:45 -0700 Subject: [PATCH] Use libpng if palette possible, otherwise use Cairo (stub). --- libguac/src/protocol.c | 131 ++++++++++++++++------------------------- 1 file changed, 51 insertions(+), 80 deletions(-) diff --git a/libguac/src/protocol.c b/libguac/src/protocol.c index 18647c97..160b91d6 100644 --- a/libguac/src/protocol.c +++ b/libguac/src/protocol.c @@ -152,59 +152,10 @@ void __guac_socket_flush_png(png_structp png) { /* Dummy function */ } -png_byte** __guac_create_png_rgb(cairo_surface_t* surface, int alpha) { +int __guac_socket_write_length_png_cairo(guac_socket* socket, cairo_surface_t* surface) { - 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; ysize); - - guac_palette_free(palette); - } - - png_rows = __guac_create_png_rgb(surface, 0); - png_format = PNG_COLOR_TYPE_RGB; - } - else if (format == CAIRO_FORMAT_ARGB32) { - png_rows = __guac_create_png_rgb(surface, 1); - png_format = PNG_COLOR_TYPE_RGB_ALPHA; - } - else - return -1; /* FIXME: Format not yet supported */ - - /* If not an image surface, fail */ - if (png_rows == NULL) { - return -1; /* FIXME: Set guac_error, etc? */ - } + /* If not possible, resort to Cairo PNG writer */ + if (palette == NULL) + return __guac_socket_write_length_png_cairo(socket, surface); /* Set up PNG writer */ png = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL); @@ -282,6 +220,36 @@ int __guac_socket_write_length_png(guac_socket* socket, cairo_surface_t* surface __guac_socket_write_png, __guac_socket_flush_png); + /* Copy data from surface into PNG data */ + png_rows = (png_byte**) malloc(sizeof(png_byte*) * height); + for (y=0; ysize); + + guac_palette_free(palette); + /* Write image info */ png_set_IHDR( png, @@ -289,7 +257,7 @@ int __guac_socket_write_length_png(guac_socket* socket, cairo_surface_t* surface width, height, 8, - png_format, + PNG_COLOR_TYPE_PALETTE, PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT @@ -302,7 +270,10 @@ int __guac_socket_write_length_png(guac_socket* socket, cairo_surface_t* surface /* Finish write */ png_destroy_write_struct(&png, &png_info); - __guac_free_png(png_rows, height); + /* Free PNG data */ + for (y=0; y