2 * Copyright (C) 2002 RealVNC Ltd. All Rights Reserved.
3 * Copyright (C) 2003 Sun Microsystems, Inc.
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.
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.
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,
21 #include "zrlepalettehelper.h"
25 #define ZRLE_HASH(pix) (((pix) ^ ((pix) >> 17)) & 4095)
27 void zrlePaletteHelperInit(zrlePaletteHelper *helper)
29 memset(helper->palette, 0, sizeof(helper->palette));
30 memset(helper->index, 255, sizeof(helper->index));
31 memset(helper->key, 0, sizeof(helper->key));
35 void zrlePaletteHelperInsert(zrlePaletteHelper *helper, zrle_U32 pix)
37 if (helper->size < ZRLE_PALETTE_MAX_SIZE) {
38 int i = ZRLE_HASH(pix);
40 while (helper->index[i] != 255 && helper->key[i] != pix)
42 if (helper->index[i] != 255) return;
44 helper->index[i] = helper->size;
46 helper->palette[helper->size] = pix;
51 int zrlePaletteHelperLookup(zrlePaletteHelper *helper, zrle_U32 pix)
53 int i = ZRLE_HASH(pix);
55 assert(helper->size <= ZRLE_PALETTE_MAX_SIZE);
57 while (helper->index[i] != 255 && helper->key[i] != pix)
59 if (helper->index[i] != 255) return helper->index[i];