2a514476b346b891aae3fb81eb7ecde199b7db8c
[libguac-client-rdp.git] / src / rdp_bitmap.c
1
2 /* ***** BEGIN LICENSE BLOCK *****
3  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
4  *
5  * The contents of this file are subject to the Mozilla Public License Version
6  * 1.1 (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  * http://www.mozilla.org/MPL/
9  *
10  * Software distributed under the License is distributed on an "AS IS" basis,
11  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12  * for the specific language governing rights and limitations under the
13  * License.
14  *
15  * The Original Code is libguac-client-rdp.
16  *
17  * The Initial Developer of the Original Code is
18  * Michael Jumper.
19  * Portions created by the Initial Developer are Copyright (C) 2011
20  * the Initial Developer. All Rights Reserved.
21  *
22  * Contributor(s):
23  * Matt Hortman
24  *
25  * Alternatively, the contents of this file may be used under the terms of
26  * either the GNU General Public License Version 2 or later (the "GPL"), or
27  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
28  * in which case the provisions of the GPL or the LGPL are applicable instead
29  * of those above. If you wish to allow use of your version of this file only
30  * under the terms of either the GPL or the LGPL, and not to allow others to
31  * use your version of this file under the terms of the MPL, indicate your
32  * decision by deleting the provisions above and replace them with the notice
33  * and other provisions required by the GPL or the LGPL. If you do not delete
34  * the provisions above, a recipient may use your version of this file under
35  * the terms of any one of the MPL, the GPL or the LGPL.
36  *
37  * ***** END LICENSE BLOCK ***** */
38
39 #include <stdio.h>
40 #include <stdlib.h>
41
42 #include <cairo/cairo.h>
43
44 #include <guacamole/socket.h>
45 #include <guacamole/client.h>
46 #include <guacamole/protocol.h>
47
48 #include <freerdp/freerdp.h>
49 #include <freerdp/utils/memory.h>
50 #include <freerdp/codec/color.h>
51 #include <freerdp/codec/bitmap.h>
52
53 #include "client.h"
54 #include "rdp_bitmap.h"
55
56 void guac_rdp_bitmap_new(rdpContext* context, rdpBitmap* bitmap) {
57
58     guac_client* client = ((rdp_freerdp_context*) context)->client;
59     guac_socket* socket = client->socket; 
60
61     /* Allocate buffer */
62     guac_layer* buffer = guac_client_alloc_buffer(client);
63
64     /* Store buffer reference in bitmap */
65     ((guac_rdp_bitmap*) bitmap)->layer = buffer;
66
67     /* Convert image data if present */
68     if (bitmap->data != NULL) {
69
70         /* Convert image data to 32-bit RGB */
71         unsigned char* image_buffer = freerdp_image_convert(bitmap->data, NULL,
72                 bitmap->width, bitmap->height,
73                 context->instance->settings->color_depth,
74                 32, ((rdp_freerdp_context*) context)->clrconv);
75
76         /* If not ephemeral, store cached image, and free image data */
77         if (!bitmap->ephemeral) {
78
79             /* Create surface from image data */
80             cairo_surface_t* surface = cairo_image_surface_create_for_data(
81                 image_buffer, CAIRO_FORMAT_RGB24,
82                 bitmap->width, bitmap->height, 4*bitmap->width);
83
84             /* Send surface to buffer */
85             guac_protocol_send_png(socket,
86                     GUAC_COMP_SRC, buffer, 0, 0, surface);
87
88             /* Free surface */
89             cairo_surface_destroy(surface);
90
91             /* Free image data if actually alloated */
92             if (image_buffer != bitmap->data)
93                 free(image_buffer);
94
95         }
96
97         /* Otherwise, store converted image in bitmap, free any existing */
98         else {
99
100             /* Free existing image, if any */
101             if (image_buffer != bitmap->data)
102                 free(bitmap->data);
103
104             /* Store converted image in bitmap */
105             bitmap->data = image_buffer;
106
107         }
108
109     }
110
111 }
112
113 void guac_rdp_bitmap_paint(rdpContext* context, rdpBitmap* bitmap) {
114
115     guac_client* client = ((rdp_freerdp_context*) context)->client;
116     guac_socket* socket = client->socket;
117
118     int width = bitmap->right - bitmap->left + 1;
119     int height = bitmap->bottom - bitmap->top + 1;
120
121     /* If not ephemeral, retrieve from cache */
122     if (!bitmap->ephemeral)
123         guac_protocol_send_copy(socket,
124                 ((guac_rdp_bitmap*) bitmap)->layer,
125                 0, 0, width, height,
126                 GUAC_COMP_OVER,
127                 GUAC_DEFAULT_LAYER, bitmap->left, bitmap->top);
128
129     /* Otherwise, draw with stored image data */
130     else if (bitmap->data != NULL) {
131
132         /* Create surface from image data */
133         cairo_surface_t* surface = cairo_image_surface_create_for_data(
134             bitmap->data, CAIRO_FORMAT_RGB24,
135             width, height, 4*bitmap->width);
136
137         /* Send surface to buffer */
138         guac_protocol_send_png(socket,
139                 GUAC_COMP_OVER, GUAC_DEFAULT_LAYER,
140                 bitmap->left, bitmap->top, surface);
141
142         /* Free surface */
143         cairo_surface_destroy(surface);
144
145     }
146
147 }
148
149 void guac_rdp_bitmap_free(rdpContext* context, rdpBitmap* bitmap) {
150     guac_client* client = ((rdp_freerdp_context*) context)->client;
151
152     /* Free layer, if any */
153     if (((guac_rdp_bitmap*) bitmap)->layer != NULL)
154         guac_client_free_buffer(client, ((guac_rdp_bitmap*) bitmap)->layer);
155 }
156
157 void guac_rdp_bitmap_setsurface(rdpContext* context, rdpBitmap* bitmap, boolean primary) {
158     guac_client* client = ((rdp_freerdp_context*) context)->client;
159
160     if (primary)
161         ((rdp_guac_client_data*) client->data)->current_surface
162             = GUAC_DEFAULT_LAYER;
163     else
164         ((rdp_guac_client_data*) client->data)->current_surface 
165             = ((guac_rdp_bitmap*) bitmap)->layer;
166
167 }
168
169 void guac_rdp_bitmap_decompress(rdpContext* context, rdpBitmap* bitmap, uint8* data, int width, int height, int bpp, int length, boolean compressed) {
170
171     int size = width * height * (bpp + 7) / 8;
172
173     if (bitmap->data == NULL)
174         bitmap->data = (uint8*) xmalloc(size);
175     else
176         bitmap->data = (uint8*) xrealloc(bitmap->data, size);
177
178     if (compressed)
179         bitmap_decompress(data, bitmap->data, width, height, length, bpp, bpp);
180     else
181         freerdp_image_flip(data, bitmap->data, width, height, bpp);
182
183     bitmap->compressed = false;
184     bitmap->length = size;
185     bitmap->bpp = bpp;
186
187 }
188