00001 /* 00002 * Copyright (C) 2002 RealVNC Ltd. All Rights Reserved. 00003 * Copyright (C) 2003 Sun Microsystems, Inc. 00004 * 00005 * This is free software; you can redistribute it and/or modify 00006 * it under the terms of the GNU General Public License as published by 00007 * the Free Software Foundation; either version 2 of the License, or 00008 * (at your option) any later version. 00009 * 00010 * This software is distributed in the hope that it will be useful, 00011 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00013 * GNU General Public License for more details. 00014 * 00015 * You should have received a copy of the GNU General Public License 00016 * along with this software; if not, write to the Free Software 00017 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, 00018 * USA. 00019 */ 00020 00021 #include "zrlepalettehelper.h" 00022 #include <assert.h> 00023 #include <string.h> 00024 00025 #define ZRLE_HASH(pix) (((pix) ^ ((pix) >> 17)) & 4095) 00026 00027 void zrlePaletteHelperInit(zrlePaletteHelper *helper) 00028 { 00029 memset(helper->palette, 0, sizeof(helper->palette)); 00030 memset(helper->index, 255, sizeof(helper->index)); 00031 memset(helper->key, 0, sizeof(helper->key)); 00032 helper->size = 0; 00033 } 00034 00035 void zrlePaletteHelperInsert(zrlePaletteHelper *helper, zrle_U32 pix) 00036 { 00037 if (helper->size < ZRLE_PALETTE_MAX_SIZE) { 00038 int i = ZRLE_HASH(pix); 00039 00040 while (helper->index[i] != 255 && helper->key[i] != pix) 00041 i++; 00042 if (helper->index[i] != 255) return; 00043 00044 helper->index[i] = helper->size; 00045 helper->key[i] = pix; 00046 helper->palette[helper->size] = pix; 00047 } 00048 helper->size++; 00049 } 00050 00051 int zrlePaletteHelperLookup(zrlePaletteHelper *helper, zrle_U32 pix) 00052 { 00053 int i = ZRLE_HASH(pix); 00054 00055 assert(helper->size <= ZRLE_PALETTE_MAX_SIZE); 00056 00057 while (helper->index[i] != 255 && helper->key[i] != pix) 00058 i++; 00059 if (helper->index[i] != 255) return helper->index[i]; 00060 00061 return -1; 00062 }