/apps/desktop/libvncserver/zrlepalettehelper.c

http://ftk.googlecode.com/ · C · 62 lines · 33 code · 10 blank · 19 comment · 13 complexity · dcff2be6900bad35121c2a45f96ea08c MD5 · raw file

  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. #include "zrlepalettehelper.h"
  21. #include <assert.h>
  22. #include <string.h>
  23. #define ZRLE_HASH(pix) (((pix) ^ ((pix) >> 17)) & 4095)
  24. void zrlePaletteHelperInit(zrlePaletteHelper *helper)
  25. {
  26. memset(helper->palette, 0, sizeof(helper->palette));
  27. memset(helper->index, 255, sizeof(helper->index));
  28. memset(helper->key, 0, sizeof(helper->key));
  29. helper->size = 0;
  30. }
  31. void zrlePaletteHelperInsert(zrlePaletteHelper *helper, zrle_U32 pix)
  32. {
  33. if (helper->size < ZRLE_PALETTE_MAX_SIZE) {
  34. int i = ZRLE_HASH(pix);
  35. while (helper->index[i] != 255 && helper->key[i] != pix)
  36. i++;
  37. if (helper->index[i] != 255) return;
  38. helper->index[i] = helper->size;
  39. helper->key[i] = pix;
  40. helper->palette[helper->size] = pix;
  41. }
  42. helper->size++;
  43. }
  44. int zrlePaletteHelperLookup(zrlePaletteHelper *helper, zrle_U32 pix)
  45. {
  46. int i = ZRLE_HASH(pix);
  47. assert(helper->size <= ZRLE_PALETTE_MAX_SIZE);
  48. while (helper->index[i] != 255 && helper->key[i] != pix)
  49. i++;
  50. if (helper->index[i] != 255) return helper->index[i];
  51. return -1;
  52. }