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 #define ZRLE_HASH(pix) (((pix) ^ ((pix) >> 17)) & 4095)
26
zrlePaletteHelperInit(zrlePaletteHelper * helper)27 void zrlePaletteHelperInit(zrlePaletteHelper *helper)
28 {
29 memset(helper->palette, 0, sizeof(helper->palette));
30 memset(helper->index, 255, sizeof(helper->index));
31 memset(helper->key, 0, sizeof(helper->key));
32 helper->size = 0;
33 }
34
zrlePaletteHelperInsert(zrlePaletteHelper * helper,zrle_U32 pix)35 void zrlePaletteHelperInsert(zrlePaletteHelper *helper, zrle_U32 pix)
36 {
37 if (helper->size < ZRLE_PALETTE_MAX_SIZE) {
38 int i = ZRLE_HASH(pix);
39
40 while (helper->index[i] != 255 && helper->key[i] != pix)
41 i++;
42 if (helper->index[i] != 255) return;
43
44 helper->index[i] = helper->size;
45 helper->key[i] = pix;
46 helper->palette[helper->size] = pix;
47 }
48 helper->size++;
49 }
50
zrlePaletteHelperLookup(zrlePaletteHelper * helper,zrle_U32 pix)51 int zrlePaletteHelperLookup(zrlePaletteHelper *helper, zrle_U32 pix)
52 {
53 int i = ZRLE_HASH(pix);
54
55 assert(helper->size <= ZRLE_PALETTE_MAX_SIZE);
56
57 while (helper->index[i] != 255 && helper->key[i] != pix)
58 i++;
59 if (helper->index[i] != 255) return helper->index[i];
60
61 return -1;
62 }
63