desktop-shell: properly set background widget as opaque
[profile/ivi/weston-ivi-shell.git] / wcap / wcap-decode.c
1 /*
2  * Copyright © 2012 Intel Corporation
3  *
4  * Permission to use, copy, modify, distribute, and sell this software and
5  * its documentation for any purpose is hereby granted without fee, provided
6  * that the above copyright notice appear in all copies and that both that
7  * copyright notice and this permission notice appear in supporting
8  * documentation, and that the name of the copyright holders not be used in
9  * advertising or publicity pertaining to distribution of the software
10  * without specific, written prior permission.  The copyright holders make
11  * no representations about the suitability of this software for any
12  * purpose.  It is provided "as is" without express or implied warranty.
13  *
14  * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS
15  * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
16  * FITNESS, IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY
17  * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
18  * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF
19  * CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
20  * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
21  */
22
23 #include <config.h>
24
25 #include <stdlib.h>
26 #include <stdio.h>
27 #include <stdint.h>
28 #include <sys/mman.h>
29 #include <sys/mman.h>
30 #include <sys/types.h>
31 #include <sys/stat.h>
32 #include <unistd.h>
33 #include <string.h>
34 #include <fcntl.h>
35
36 #include <cairo.h>
37
38 #include "wcap-decode.h"
39
40 static void
41 wcap_decoder_decode_rectangle(struct wcap_decoder *decoder,
42                               struct wcap_rectangle *rect)
43 {
44         uint32_t v, *p = decoder->p, *d;
45         int width = rect->x2 - rect->x1, height = rect->y2 - rect->y1;
46         int x, i, j, k, l, count = width * height;
47         unsigned char r, g, b, dr, dg, db;
48
49         d = decoder->frame + (rect->y2 - 1) * decoder->width;
50         x = rect->x1;
51         i = 0;
52         while (i < count) {
53                 v = *p++;
54                 l = v >> 24;
55                 if (l < 0xe0) {
56                         j = l + 1;
57                 } else {
58                         j = 1 << (l - 0xe0 + 7);
59                 }
60
61                 dr = (v >> 16);
62                 dg = (v >>  8);
63                 db = (v >>  0);
64                 for (k = 0; k < j; k++) {
65                         r = (d[x] >> 16) + dr;
66                         g = (d[x] >>  8) + dg;
67                         b = (d[x] >>  0) + db;
68                         d[x] = 0xff000000 | (r << 16) | (g << 8) | b;
69                         x++;
70                         if (x == rect->x2) {
71                                 x = rect->x1;
72                                 d -= decoder->width;
73                         }
74                 }
75                 i += j;
76         }
77
78         if (i != count)
79                 printf("rle encoding longer than expected (%d expected %d)\n",
80                        i, count);
81
82         decoder->p = p;
83 }
84
85 int
86 wcap_decoder_get_frame(struct wcap_decoder *decoder)
87 {
88         struct wcap_rectangle *rects;
89         struct wcap_frame_header *header;
90         uint32_t i;
91
92         if (decoder->p == decoder->end)
93                 return 0;
94
95         header = decoder->p;
96         decoder->msecs = header->msecs;
97         decoder->count++;
98
99         rects = (void *) (header + 1);
100         decoder->p = (uint32_t *) (rects + header->nrects);
101         for (i = 0; i < header->nrects; i++)
102                 wcap_decoder_decode_rectangle(decoder, &rects[i]);
103
104         return 1;
105 }
106
107 struct wcap_decoder *
108 wcap_decoder_create(const char *filename)
109 {
110         struct wcap_decoder *decoder;
111         struct wcap_header *header;
112         int frame_size;
113         struct stat buf;
114
115         decoder = malloc(sizeof *decoder);
116         if (decoder == NULL)
117                 return NULL;
118
119         decoder->fd = open(filename, O_RDONLY);
120         if (decoder->fd == -1) {
121                 free(decoder);
122                 return NULL;
123         }
124
125         fstat(decoder->fd, &buf);
126         decoder->size = buf.st_size;
127         decoder->map = mmap(NULL, decoder->size,
128                             PROT_READ, MAP_PRIVATE, decoder->fd, 0);
129         if (decoder->map == MAP_FAILED) {
130                 fprintf(stderr, "mmap failed\n");
131                 free(decoder);
132                 return NULL;
133         }
134                 
135         header = decoder->map;
136         decoder->format = header->format;
137         decoder->count = 0;
138         decoder->width = header->width;
139         decoder->height = header->height;
140         decoder->p = header + 1;
141         decoder->end = decoder->map + decoder->size;
142
143         frame_size = header->width * header->height * 4;
144         decoder->frame = malloc(frame_size);
145         if (decoder->frame == NULL) {
146                 free(decoder);
147                 return NULL;
148         }
149         memset(decoder->frame, 0, frame_size);
150
151         return decoder;
152 }
153
154 void
155 wcap_decoder_destroy(struct wcap_decoder *decoder)
156 {
157         munmap(decoder->map, decoder->size);
158         close(decoder->fd);
159         free(decoder->frame);
160         free(decoder);
161 }