Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

112 lines
3.6 KiB

  1. //----------------------------------------------------------------------------
  2. //
  3. // rgbmap.cpp
  4. //
  5. // Implements rgb colormap code.
  6. //
  7. // Copyright (C) Microsoft Corporation, 1997.
  8. //
  9. //----------------------------------------------------------------------------
  10. #include "pch.cpp"
  11. #pragma hdrstop
  12. static int CalculateShift(unsigned long mask);
  13. static unsigned long AllocateColor(void* arg, int red, int green, int blue);
  14. static void FreeColor(void* arg, unsigned long pixel);
  15. RLDDIRGBMap* RLDDICreateRGBMap(unsigned long red_mask,
  16. unsigned long green_mask,
  17. unsigned long blue_mask)
  18. {
  19. RLDDIRGBMap* rgbmap;
  20. if (D3DMalloc((void**) &rgbmap, sizeof(RLDDIRGBMap)))
  21. return NULL;
  22. rgbmap->red_mask = red_mask;
  23. rgbmap->green_mask = green_mask;
  24. rgbmap->blue_mask = blue_mask;
  25. rgbmap->red_shift = CalculateShift(rgbmap->red_mask);
  26. rgbmap->green_shift = CalculateShift(rgbmap->green_mask);
  27. rgbmap->blue_shift = CalculateShift(rgbmap->blue_mask);
  28. rgbmap->alloc.priv = rgbmap;
  29. rgbmap->alloc.allocate_color = AllocateColor;
  30. rgbmap->alloc.free_color = FreeColor;
  31. return rgbmap;
  32. }
  33. // ATTENTION this function should perhaps be packaged up and put in d3dutil
  34. static int RLDDILog2[] = {
  35. 0, 0, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3,
  36. 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
  37. 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
  38. 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
  39. 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
  40. 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
  41. 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
  42. 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
  43. 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
  44. 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
  45. 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
  46. 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
  47. 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
  48. 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
  49. 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
  50. 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
  51. 8,
  52. };
  53. static int CalculateShift(unsigned long mask)
  54. {
  55. int shift, width;
  56. shift = 0;
  57. while ((mask & 1) == 0)
  58. {
  59. shift++;
  60. mask >>= 1;
  61. }
  62. width = RLDDILog2[mask + 1];
  63. return shift - (8 - width);
  64. }
  65. /*
  66. * We can't just use left shift since some CPUs don't treat left shift by
  67. * a negative as right shift (and why should they after all).
  68. */
  69. #define SHIFT(t, v, s) \
  70. do { \
  71. if (s < 0) \
  72. t = v >> -s; \
  73. else \
  74. t = v << s; \
  75. } while (0)
  76. static unsigned long AllocateColor(void* arg, int red, int green, int blue)
  77. {
  78. RLDDIRGBMap* rgbmap = (RLDDIRGBMap*) arg;
  79. unsigned long pixel;
  80. unsigned long t;
  81. SHIFT(t, red, rgbmap->red_shift);
  82. pixel = t & rgbmap->red_mask;
  83. SHIFT(t, green, rgbmap->green_shift);
  84. pixel |= t & rgbmap->green_mask;
  85. SHIFT(t, blue, rgbmap->blue_shift);
  86. pixel |= t & rgbmap->blue_mask;
  87. return pixel;
  88. }
  89. static void FreeColor(void* arg, unsigned long pixel)
  90. {
  91. }
  92. void RLDDIDestroyRGBMap(RLDDIRGBMap* rgbmap)
  93. {
  94. D3DFree(rgbmap);
  95. }