Changeset d3c47e1 in libguac


Ignore:
Timestamp:
04/02/12 01:28:45 (14 months ago)
Author:
zhangmaike
Branches:
master, debian, rpm, unstable
Children:
593d2d0
Parents:
eaf5489
git-author:
Michael Jumper <zhangmaike@…> (04/02/12 01:28:45)
git-committer:
Michael Jumper <zhangmaike@…> (04/02/12 01:28:45)
Message:

Use libpng if palette possible, otherwise use Cairo (stub).

File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/protocol.c

    reaf5489 rd3c47e1  
    153153} 
    154154 
    155 png_byte** __guac_create_png_rgb(cairo_surface_t* surface, int alpha) { 
    156  
     155int __guac_socket_write_length_png_cairo(guac_socket* socket, cairo_surface_t* surface) { 
     156 
     157    /* STUB */ 
     158    return guac_socket_write_string(socket, "0."); 
     159 
     160} 
     161 
     162int __guac_socket_write_length_png(guac_socket* socket, cairo_surface_t* surface) { 
     163 
     164    png_structp png; 
     165    png_infop png_info; 
    157166    png_byte** png_rows; 
     167 
    158168    int x, y; 
    159169 
     170    __guac_socket_write_png_data png_data; 
     171    int base64_length; 
     172 
     173    /* Get image surface properties and data */ 
     174    cairo_format_t format = cairo_image_surface_get_format(surface); 
    160175    int width = cairo_image_surface_get_width(surface); 
    161176    int height = cairo_image_surface_get_height(surface); 
     
    163178    unsigned char* data = cairo_image_surface_get_data(surface); 
    164179 
    165     /* Fail if not an image surface */ 
    166     if (data == NULL) 
    167         return NULL; 
    168  
    169     int bpp; 
    170     if (alpha) bpp = 4; 
    171     else       bpp = 3; 
    172  
    173     /* Copy data from surface into PNG data */ 
    174     png_rows = (png_byte**) malloc(sizeof(png_byte*) * height); 
    175     for (y=0; y<height; y++) { 
    176  
    177         /* Allocate new PNG row */ 
    178         png_byte* row = (png_byte*) malloc(sizeof(png_byte) * width * bpp); 
    179         png_rows[y] = row; 
    180  
    181         /* Copy data from surface into current row */ 
    182         for (x=0; x<width; x++) { 
    183             row[bpp*x]   = data[4*x+2]; 
    184             row[bpp*x+1] = data[4*x+1]; 
    185             row[bpp*x+2] = data[4*x]; 
    186  
    187             if (alpha) 
    188                 row[bpp*x+3] = data[4*x+3]; 
    189         } 
    190  
    191         /* Advance to next data row */ 
    192         data += stride; 
    193  
    194     } 
    195  
    196     return png_rows; 
    197  
    198 } 
    199  
    200 void __guac_free_png(png_byte** png_rows, int height) { 
    201  
    202     int y; 
    203  
    204     /* Free PNG data */ 
    205     for (y=0; y<height; y++) 
    206         free(png_rows[y]); 
    207     free(png_rows); 
    208  
    209 } 
    210  
    211 int __guac_socket_write_length_png(guac_socket* socket, cairo_surface_t* surface) { 
    212  
    213     png_structp png; 
    214     png_infop png_info; 
    215     png_byte** png_rows; 
    216     int png_format; 
    217  
    218     __guac_socket_write_png_data png_data; 
    219     int base64_length; 
    220  
    221     /* Get image surface properties and data */ 
    222     cairo_format_t format = cairo_image_surface_get_format(surface); 
    223     int width = cairo_image_surface_get_width(surface); 
    224     int height = cairo_image_surface_get_height(surface); 
     180    /* If not RGB24, use Cairo PNG writer */ 
     181    if (format != CAIRO_FORMAT_RGB24 || data == NULL) 
     182        return __guac_socket_write_length_png_cairo(socket, surface); 
    225183 
    226184    /* Flush pending operations to surface */ 
    227185    cairo_surface_flush(surface); 
    228186 
    229     if (format == CAIRO_FORMAT_RGB24) { 
    230  
    231         guac_palette* palette = guac_palette_alloc(surface); 
    232         if (palette != NULL) { 
    233  
    234             fprintf(stderr, 
    235                     "Created palette with %i entries.\n", 
    236                     palette->size); 
    237  
    238             guac_palette_free(palette); 
    239         } 
    240  
    241         png_rows = __guac_create_png_rgb(surface, 0); 
    242         png_format = PNG_COLOR_TYPE_RGB; 
    243     } 
    244     else if (format == CAIRO_FORMAT_ARGB32) { 
    245         png_rows = __guac_create_png_rgb(surface, 1); 
    246         png_format = PNG_COLOR_TYPE_RGB_ALPHA; 
    247     } 
    248     else 
    249         return -1; /* FIXME: Format not yet supported */ 
    250  
    251     /* If not an image surface, fail */ 
    252     if (png_rows == NULL) { 
    253         return -1; /* FIXME: Set guac_error, etc? */ 
    254     } 
     187    /* Attempt to build palette */ 
     188    guac_palette* palette = guac_palette_alloc(surface); 
     189 
     190    /* If not possible, resort to Cairo PNG writer */ 
     191    if (palette == NULL) 
     192        return __guac_socket_write_length_png_cairo(socket, surface); 
    255193 
    256194    /* Set up PNG writer */ 
     
    282220            __guac_socket_write_png, 
    283221            __guac_socket_flush_png); 
     222 
     223    /* Copy data from surface into PNG data */ 
     224    png_rows = (png_byte**) malloc(sizeof(png_byte*) * height); 
     225    for (y=0; y<height; y++) { 
     226 
     227        /* Allocate new PNG row */ 
     228        png_byte* row = (png_byte*) malloc(sizeof(png_byte) * width); 
     229        png_rows[y] = row; 
     230 
     231        /* Copy data from surface into current row */ 
     232        for (x=0; x<width; x++) { 
     233 
     234            /* Get pixel color */ 
     235            int color = ((uint32_t*) data)[x] & 0xFFFFFF; 
     236 
     237            /* Set index in row */ 
     238            row[x] = guac_palette_find(palette, color); 
     239 
     240        } 
     241 
     242        /* Advance to next data row */ 
     243        data += stride; 
     244 
     245    } 
     246 
     247    fprintf(stderr, 
     248            "Created palette with %i entries.\n", 
     249            palette->size); 
     250 
     251    guac_palette_free(palette); 
    284252 
    285253    /* Write image info */ 
     
    290258        height, 
    291259        8, 
    292         png_format, 
     260        PNG_COLOR_TYPE_PALETTE, 
    293261        PNG_INTERLACE_NONE, 
    294262        PNG_COMPRESSION_TYPE_DEFAULT, 
     
    303271    png_destroy_write_struct(&png, &png_info); 
    304272 
    305     __guac_free_png(png_rows, height); 
     273    /* Free PNG data */ 
     274    for (y=0; y<height; y++) 
     275        free(png_rows[y]); 
     276    free(png_rows); 
    306277 
    307278    base64_length = (png_data.data_size + 2) / 3 * 4; 
Note: See TracChangeset for help on using the changeset viewer.