Kristian Høgsberg | 89060c0 | 2012-05-25 11:59:53 -0400 | [diff] [blame] | 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 <stdlib.h> |
| 24 | #include <stdio.h> |
| 25 | #include <stdint.h> |
| 26 | #include <sys/mman.h> |
| 27 | #include <sys/mman.h> |
| 28 | #include <sys/types.h> |
| 29 | #include <sys/stat.h> |
| 30 | #include <unistd.h> |
| 31 | #include <string.h> |
| 32 | #include <fcntl.h> |
| 33 | |
| 34 | #include <cairo.h> |
| 35 | |
Kristian Høgsberg | 894e0b5 | 2012-05-25 17:55:20 -0400 | [diff] [blame] | 36 | #include "wcap-decode.h" |
Kristian Høgsberg | 89060c0 | 2012-05-25 11:59:53 -0400 | [diff] [blame] | 37 | |
| 38 | static void |
| 39 | wcap_decoder_decode_rectangle(struct wcap_decoder *decoder, |
| 40 | struct wcap_rectangle *rect) |
| 41 | { |
| 42 | uint32_t v, *p = decoder->p, *d; |
| 43 | int width = rect->x2 - rect->x1, height = rect->y2 - rect->y1; |
| 44 | int x, i, j, k, l, count = width * height; |
| 45 | |
| 46 | d = decoder->frame + (rect->y2 - 1) * decoder->width; |
| 47 | x = rect->x1; |
| 48 | i = 0; |
| 49 | while (i < count) { |
| 50 | v = *p++; |
| 51 | l = v >> 24; |
| 52 | if (l < 0xe0) { |
| 53 | j = l + 1; |
| 54 | } else { |
| 55 | j = 1 << (l - 0xe0 + 7); |
| 56 | } |
| 57 | |
| 58 | for (k = 0; k < j; k++) { |
| 59 | d[x] = (d[x] + v) | 0xff000000; |
| 60 | x++; |
| 61 | if (x == rect->x2) { |
| 62 | x = rect->x1; |
| 63 | d -= decoder->width; |
| 64 | } |
| 65 | } |
| 66 | i += j; |
| 67 | } |
| 68 | |
| 69 | if (i != count) |
| 70 | printf("rle encoding longer than expected (%d expected %d)\n", |
| 71 | i, count); |
| 72 | |
| 73 | decoder->p = p; |
| 74 | } |
| 75 | |
Kristian Høgsberg | 894e0b5 | 2012-05-25 17:55:20 -0400 | [diff] [blame] | 76 | int |
Kristian Høgsberg | 89060c0 | 2012-05-25 11:59:53 -0400 | [diff] [blame] | 77 | wcap_decoder_get_frame(struct wcap_decoder *decoder) |
| 78 | { |
| 79 | struct wcap_rectangle *rects; |
| 80 | struct wcap_frame_header *header; |
| 81 | uint32_t *s; |
| 82 | uint32_t i; |
| 83 | int width, height; |
| 84 | |
| 85 | if (decoder->p == decoder->end) |
| 86 | return 0; |
| 87 | |
| 88 | header = decoder->p; |
| 89 | |
| 90 | rects = (void *) (header + 1); |
| 91 | decoder->p = (uint32_t *) (rects + header->nrects); |
| 92 | for (i = 0; i < header->nrects; i++) { |
| 93 | width = rects[i].x2 - rects[i].x1; |
| 94 | height = rects[i].y2 - rects[i].y1; |
| 95 | wcap_decoder_decode_rectangle(decoder, &rects[i]); |
| 96 | } |
| 97 | |
| 98 | return 1; |
| 99 | } |
| 100 | |
| 101 | struct wcap_decoder * |
| 102 | wcap_decoder_create(const char *filename) |
| 103 | { |
| 104 | struct wcap_decoder *decoder; |
| 105 | struct wcap_header *header; |
| 106 | int frame_size; |
| 107 | struct stat buf; |
| 108 | |
| 109 | decoder = malloc(sizeof *decoder); |
| 110 | if (decoder == NULL) |
| 111 | return NULL; |
| 112 | |
| 113 | decoder->fd = open(filename, O_RDONLY); |
| 114 | if (decoder->fd == -1) |
| 115 | return NULL; |
| 116 | |
| 117 | fstat(decoder->fd, &buf); |
| 118 | decoder->size = buf.st_size; |
| 119 | decoder->map = mmap(NULL, decoder->size, |
| 120 | PROT_READ, MAP_PRIVATE, decoder->fd, 0); |
| 121 | |
| 122 | header = decoder->map; |
Kristian Høgsberg | 2255eb0 | 2012-05-25 21:51:25 -0400 | [diff] [blame^] | 123 | decoder->format = header->format; |
Kristian Høgsberg | 89060c0 | 2012-05-25 11:59:53 -0400 | [diff] [blame] | 124 | decoder->width = header->width; |
| 125 | decoder->height = header->height; |
| 126 | decoder->p = header + 1; |
| 127 | decoder->end = decoder->map + decoder->size; |
| 128 | |
| 129 | frame_size = header->width * header->height * 4; |
| 130 | decoder->frame = malloc(frame_size); |
| 131 | memset(decoder->frame, 0, frame_size); |
| 132 | |
| 133 | return decoder; |
| 134 | } |
| 135 | |
| 136 | void |
| 137 | wcap_decoder_destroy(struct wcap_decoder *decoder) |
| 138 | { |
| 139 | munmap(decoder->map, decoder->size); |
| 140 | free(decoder->frame); |
| 141 | free(decoder); |
| 142 | } |