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.

219 lines
7.4 KiB

  1. // File: PixInfo.cpp
  2. // Author: Michael Marr (mikemarr)
  3. #include "stdafx.h"
  4. #include "PixInfo.h"
  5. #include "DDHelper.h"
  6. static void
  7. _GetShiftMaskInfo(DWORD dwMask, BYTE *pnShiftStart, BYTE *pnLengthResidual)
  8. {
  9. MMASSERT(pnShiftStart && pnLengthResidual);
  10. // Note: - DWORD fills with zeros on right shift
  11. DWORD nShift = 0, nRes = 8;
  12. if (dwMask) {
  13. // compute shift
  14. if ((dwMask & 0xFFFF) == 0) { dwMask >>= 16, nShift += 16; }
  15. if ((dwMask & 0xFF) == 0) { dwMask >>= 8; nShift += 8; }
  16. if ((dwMask & 0xF) == 0) { dwMask >>= 4; nShift += 4; }
  17. if ((dwMask & 0x3) == 0) { dwMask >>= 2; nShift += 2; }
  18. if ((dwMask & 0x1) == 0) { dwMask >>= 1; nShift++; }
  19. // compute residual
  20. if ((dwMask & 0xFF) == 0xFF) {
  21. nRes = 0;
  22. } else {
  23. if ((dwMask & 0xF) == 0xF) { dwMask >>= 4; nRes -= 4; }
  24. if ((dwMask & 0x3) == 0x3) { dwMask >>= 2; nRes -= 2; }
  25. if ((dwMask & 0x1) == 0x1) { nRes--; }
  26. }
  27. }
  28. *pnShiftStart = (BYTE) nShift;
  29. *pnLengthResidual = (BYTE) (nRes);
  30. }
  31. HRESULT
  32. CPixelInfo::Init(BYTE tnBPP, DWORD dwRedMask, DWORD dwGreenMask,
  33. DWORD dwBlueMask, DWORD dwAlphaMask)
  34. {
  35. nBPP = tnBPP;
  36. uchFlags = 0;
  37. if (dwRedMask) {
  38. uchFlags |= flagPixiRGB;
  39. _GetShiftMaskInfo(dwRedMask, &nRedShift, &nRedResidual);
  40. _GetShiftMaskInfo(dwGreenMask, &nGreenShift, &nGreenResidual);
  41. _GetShiftMaskInfo(dwBlueMask, &nBlueShift, &nBlueResidual);
  42. _GetShiftMaskInfo(dwAlphaMask, &nAlphaShift, &nAlphaResidual);
  43. if (dwAlphaMask)
  44. uchFlags |= flagPixiAlpha;
  45. } else {
  46. nRedResidual = nGreenResidual = nBlueResidual = nAlphaResidual = 8;
  47. nRedShift = nGreenShift = nBlueShift = nAlphaShift = 0;
  48. }
  49. iRed = (nRedShift == 0 ? 0 : 2);
  50. iBlue = 2 - iRed;
  51. // MMTRACE("BPP: %2d R: %2d %2d G: %2d %2d B: %2d %2d A: %2d %2d\n", nBPP,
  52. // 8 - nRedResidual, nRedShift, 8 - nGreenResidual, nGreenShift,
  53. // 8 - nBlueResidual, nBlueShift, 8 - nAlphaResidual, nAlphaShift);
  54. return S_OK;
  55. }
  56. void
  57. CPixelInfo::GetDDPF(DDPIXELFORMAT &ddpf) const
  58. {
  59. ddpf.dwSize = sizeof(DDPIXELFORMAT);
  60. ddpf.dwFlags = DDPF_RGB;
  61. ddpf.dwRGBBitCount = nBPP;
  62. if (IsRGB()) {
  63. ddpf.dwRBitMask = ((((DWORD) 0xFF) >> nRedResidual) << nRedShift);
  64. ddpf.dwGBitMask = ((((DWORD) 0xFF) >> nGreenResidual) << nGreenShift);
  65. ddpf.dwBBitMask = ((((DWORD) 0xFF) >> nBlueResidual) << nBlueShift);
  66. ddpf.dwRGBAlphaBitMask = ((((DWORD) 0xFF) >> nAlphaResidual) << nAlphaShift);
  67. if (HasAlpha())
  68. ddpf.dwFlags |= DDPF_ALPHAPIXELS;
  69. } else {
  70. ddpf.dwFlags |= BPPToPixelFlags(nBPP);
  71. ddpf.dwRBitMask = ddpf.dwGBitMask = ddpf.dwBBitMask = ddpf.dwRGBAlphaBitMask = 0;
  72. }
  73. }
  74. BOOL
  75. CPixelInfo::operator==(const CPixelInfo &pixi) const
  76. {
  77. return ((nBPP == pixi.nBPP) && (!IsRGB() ||
  78. ((nRedShift == pixi.nRedShift) && (nGreenShift == pixi.nGreenShift) &&
  79. (nBlueShift == pixi.nBlueShift) && (nAlphaShift == pixi.nAlphaShift))));
  80. }
  81. BOOL
  82. CPixelInfo::operator==(const DDPIXELFORMAT &ddpf) const
  83. {
  84. return ((nBPP == ddpf.dwRGBBitCount) && ((!IsRGB() && (ddpf.dwRBitMask == 0)) ||
  85. ((ddpf.dwRBitMask == ((DWORD(0xFF) >> nRedResidual) << nRedShift)) &&
  86. (ddpf.dwGBitMask == ((DWORD(0xFF) >> nGreenResidual) << nGreenShift)) &&
  87. (ddpf.dwBBitMask == ((DWORD(0xFF) >> nBlueResidual) << nBlueShift)) &&
  88. (ddpf.dwRGBAlphaBitMask == ((DWORD(0xFF) >> nAlphaResidual) << nAlphaShift)))));
  89. }
  90. DWORD
  91. CPixelInfo::Pack(const BYTE *pPixel) const
  92. {
  93. MMASSERT(pPixel && (nBPP >= 8));
  94. if (nBPP == 8)
  95. return (DWORD) *pPixel;
  96. if (HasAlpha())
  97. return Pack(pPixel[0], pPixel[1], pPixel[2], pPixel[3]);
  98. else
  99. return Pack(pPixel[0], pPixel[1], pPixel[2]);
  100. }
  101. DWORD
  102. CPixelInfo::Pack(BYTE r, BYTE g, BYTE b) const
  103. {
  104. // truncate the RGB values to fit in allotted bits
  105. return (((((DWORD) r) >> nRedResidual) << nRedShift) |
  106. ((((DWORD) g) >> nGreenResidual) << nGreenShift) |
  107. ((((DWORD) b) >> nBlueResidual) << nBlueShift));
  108. }
  109. DWORD
  110. CPixelInfo::Pack(BYTE r, BYTE g, BYTE b, BYTE a) const
  111. {
  112. // truncate the alpha value to fit in allotted bits
  113. return (((((DWORD) r) >> nRedResidual) << nRedShift) |
  114. ((((DWORD) g) >> nGreenResidual) << nGreenShift) |
  115. ((((DWORD) b) >> nBlueResidual) << nBlueShift) |
  116. ((((DWORD) a) >> nAlphaResidual) << nAlphaShift));
  117. }
  118. void
  119. CPixelInfo::UnPack(DWORD dwPixel, BYTE *pR, BYTE *pG, BYTE *pB, BYTE *pA) const
  120. {
  121. MMASSERT(pR && pG && pB && pA);
  122. *pR = (BYTE) (((dwPixel >> nRedShift) & (0xFF >> nRedResidual)) << nRedResidual);
  123. *pG = (BYTE) (((dwPixel >> nGreenShift) & (0xFF >> nGreenResidual)) << nGreenResidual);
  124. *pB = (BYTE) (((dwPixel >> nBlueShift) & (0xFF >> nBlueResidual)) << nBlueResidual);
  125. *pA = (BYTE) (((dwPixel >> nAlphaShift) & (0xFF >> nAlphaResidual)) << nAlphaResidual);
  126. }
  127. void
  128. CPixelInfo::UnPack(DWORD dwPixel, BYTE *pR, BYTE *pG, BYTE *pB) const
  129. {
  130. MMASSERT(pR && pG && pB);
  131. *pR = (BYTE)(((dwPixel >> nRedShift) & (0xFF >> nRedResidual)) << nRedResidual);
  132. *pG = (BYTE)(((dwPixel >> nGreenShift) & (0xFF >> nGreenResidual)) << nGreenResidual);
  133. *pB = (BYTE)(((dwPixel >> nBlueShift) & (0xFF >> nBlueResidual)) << nBlueResidual);
  134. }
  135. DWORD
  136. CPixelInfo::TranslatePack(DWORD dwPix, const CPixelInfo &pixiSrc) const
  137. {
  138. // REVIEW: this could be optimized by splitting out the cases
  139. DWORD dwTmp;
  140. dwTmp = ((((((dwPix >> pixiSrc.nRedShift) & (0xFF >> pixiSrc.nRedResidual))
  141. << pixiSrc.nRedResidual) >> nRedResidual) << nRedShift) |
  142. (((((dwPix >> pixiSrc.nGreenShift) & (0xFF >> pixiSrc.nGreenResidual))
  143. << pixiSrc.nGreenResidual) >> nGreenResidual) << nGreenShift) |
  144. (((((dwPix >> pixiSrc.nBlueShift) & (0xFF >> pixiSrc.nBlueResidual))
  145. << pixiSrc.nBlueResidual) >> nBlueResidual) << nBlueShift));
  146. if (pixiSrc.HasAlpha())
  147. dwTmp |= (((((dwPix >> pixiSrc.nAlphaShift) & (0xFF >> pixiSrc.nAlphaResidual))
  148. << pixiSrc.nAlphaResidual) >> nAlphaResidual) << nAlphaShift);
  149. return dwTmp;
  150. }
  151. WORD
  152. CPixelInfo::Pack16(BYTE r, BYTE g, BYTE b) const
  153. {
  154. MMASSERT(nBPP == 16);
  155. return (((((WORD) r) >> nRedResidual) << nRedShift) |
  156. ((((WORD) g) >> nGreenResidual) << nGreenShift) |
  157. ((((WORD) b) >> nBlueResidual) << nBlueShift));
  158. }
  159. WORD
  160. CPixelInfo::Pack16(BYTE r, BYTE g, BYTE b, BYTE a) const
  161. {
  162. MMASSERT(nBPP == 16);
  163. return (((((WORD) r) >> nRedResidual) << nRedShift) |
  164. ((((WORD) g) >> nGreenResidual) << nGreenShift) |
  165. ((((WORD) b) >> nBlueResidual) << nBlueShift) |
  166. ((((WORD) a) >> nAlphaResidual) << nAlphaShift));
  167. }
  168. /*
  169. void
  170. TestPixi()
  171. {
  172. DDPIXELFORMAT ddpf;
  173. INIT_DXSTRUCT(ddpf);
  174. DWORD dwTmp = 0;
  175. BYTE r = 0, g = 0, b = 0, a = 0;
  176. MMASSERT(g_pixiPalette8 != g_ddpfBGR332);
  177. MMASSERT(g_pixiRGB != g_pixiBGR);
  178. MMASSERT(g_pixiRGB565 != g_ddpfBGR565);
  179. MMASSERT(g_pixiBGRA5551 == g_ddpfBGRA5551);
  180. MMASSERT(g_pixiBGRX != g_pixiBGRA);
  181. MMASSERT(g_pixiBGR != g_pixiBGRX);
  182. MMASSERT(g_pixiBGRA5551 != g_pixiBGR555);
  183. MMASSERT(g_pixiBGR332.Pack(0x00, 0xFF, 0x00) == g_ddpfBGR332.dwGBitMask);
  184. MMASSERT(g_pixiBGRA4444.Pack(0x00, 0x00, 0x00, 0xFF) == g_ddpfBGRA4444.dwRGBAlphaBitMask);
  185. MMASSERT(g_pixiBGRA5551.TranslatePack(g_pixiBGRA4444.Pack(0, 0, 0, 0xFF), g_pixiBGRA4444) == g_ddpfBGRA5551.dwRGBAlphaBitMask);
  186. g_pixiRGB.UnPack(g_pixiRGB.Pack(0xFF, 0, 0), &r, &g, &b, &a);
  187. MMASSERT((r == 0xFF) && (g == 0) && (b == 0) && (a == 0));
  188. g_pixiBGR332.GetDDPF(ddpf);
  189. MMASSERT(ddpf == g_ddpfBGR332);
  190. g_pixiPalette8.GetDDPF(ddpf);
  191. MMASSERT(ddpf == g_ddpfPalette8);
  192. g_pixiBGRA4444.GetDDPF(ddpf);
  193. MMASSERT(g_ddpfBGRA4444 == ddpf);
  194. g_pixiBGR565.GetDDPF(ddpf);
  195. MMASSERT(g_ddpfBGR565 == ddpf);
  196. }
  197. */