/src/libvncserver/src/libvncserver/zrlepalettehelper.c
Line | Count | Source |
1 | | /* |
2 | | * Copyright (C) 2002 RealVNC Ltd. All Rights Reserved. |
3 | | * Copyright (C) 2003 Sun Microsystems, Inc. |
4 | | * |
5 | | * This is free software; you can redistribute it and/or modify |
6 | | * it under the terms of the GNU General Public License as published by |
7 | | * the Free Software Foundation; either version 2 of the License, or |
8 | | * (at your option) any later version. |
9 | | * |
10 | | * This software is distributed in the hope that it will be useful, |
11 | | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
12 | | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
13 | | * GNU General Public License for more details. |
14 | | * |
15 | | * You should have received a copy of the GNU General Public License |
16 | | * along with this software; if not, write to the Free Software |
17 | | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, |
18 | | * USA. |
19 | | */ |
20 | | |
21 | | #include "zrlepalettehelper.h" |
22 | | #include <assert.h> |
23 | | #include <string.h> |
24 | | |
25 | 0 | #define ZRLE_HASH(pix) (((pix) ^ ((pix) >> 17)) & 4095) |
26 | | |
27 | | void zrlePaletteHelperInit(zrlePaletteHelper *helper) |
28 | 0 | { |
29 | 0 | memset(helper->palette, 0, sizeof(helper->palette)); |
30 | 0 | memset(helper->index, 255, sizeof(helper->index)); |
31 | 0 | memset(helper->key, 0, sizeof(helper->key)); |
32 | 0 | helper->size = 0; |
33 | 0 | } |
34 | | |
35 | | void zrlePaletteHelperInsert(zrlePaletteHelper *helper, zrle_U32 pix) |
36 | 0 | { |
37 | 0 | if (helper->size < ZRLE_PALETTE_MAX_SIZE) { |
38 | 0 | int i = ZRLE_HASH(pix); |
39 | |
|
40 | 0 | while (helper->index[i] != 255 && helper->key[i] != pix) |
41 | 0 | i++; |
42 | 0 | if (helper->index[i] != 255) return; |
43 | | |
44 | 0 | helper->index[i] = helper->size; |
45 | 0 | helper->key[i] = pix; |
46 | 0 | helper->palette[helper->size] = pix; |
47 | 0 | } |
48 | 0 | helper->size++; |
49 | 0 | } |
50 | | |
51 | | int zrlePaletteHelperLookup(zrlePaletteHelper *helper, zrle_U32 pix) |
52 | 0 | { |
53 | 0 | int i = ZRLE_HASH(pix); |
54 | |
|
55 | 0 | assert(helper->size <= ZRLE_PALETTE_MAX_SIZE); |
56 | | |
57 | 0 | while (helper->index[i] != 255 && helper->key[i] != pix) |
58 | 0 | i++; |
59 | 0 | if (helper->index[i] != 255) return helper->index[i]; |
60 | | |
61 | 0 | return -1; |
62 | 0 | } |