graphicsObject.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. #include "./common.h"
  2. #include "./graphicsObject.h"
  3. #include "./graphics.h"
  4. GraphicsObject::GraphicsObject()
  5. : ref(1)
  6. {
  7. }
  8. GraphicsObject::~GraphicsObject()
  9. {
  10. }
  11. HRESULT GraphicsObject::CreateInstance(GraphicsObject **instance)
  12. {
  13. if (NULL == instance) return E_POINTER;
  14. *instance = new GraphicsObject();
  15. if (NULL == *instance) return E_OUTOFMEMORY;
  16. return S_OK;
  17. }
  18. size_t GraphicsObject::AddRef()
  19. {
  20. return InterlockedIncrement((LONG*)&ref);
  21. }
  22. size_t GraphicsObject::Release()
  23. {
  24. if (0 == ref)
  25. return ref;
  26. LONG r = InterlockedDecrement((LONG*)&ref);
  27. if (0 == r)
  28. delete(this);
  29. return r;
  30. }
  31. int GraphicsObject::QueryInterface(GUID interface_guid, void **object)
  32. {
  33. if (NULL == object) return E_POINTER;
  34. if (IsEqualIID(interface_guid, IFC_OmGrpahics))
  35. *object = static_cast<ifc_omgraphics*>(this);
  36. else
  37. {
  38. *object = NULL;
  39. return E_NOINTERFACE;
  40. }
  41. if (NULL == *object)
  42. return E_UNEXPECTED;
  43. AddRef();
  44. return S_OK;
  45. }
  46. HRESULT GraphicsObject::GetDistance(COLORREF rgb1, COLORREF rgb2, int *distance)
  47. {
  48. if (NULL == distance) return E_POINTER;
  49. *distance = GetColorDistance(rgb1, rgb2);
  50. return S_OK;
  51. }
  52. HRESULT GraphicsObject::GetDarker(COLORREF rgb1, COLORREF rgb2, COLORREF *result)
  53. {
  54. if (NULL == result) return E_POINTER;
  55. *result = GetDarkerColor(rgb1, rgb2);
  56. return S_OK;
  57. }
  58. HRESULT GraphicsObject::BlendColor(COLORREF rgbTop, COLORREF rgbBottom, int alpha, COLORREF *result)
  59. {
  60. if (NULL == result) return E_POINTER;
  61. *result = BlendColors(rgbTop, rgbBottom, alpha);
  62. return S_OK;
  63. }
  64. HRESULT GraphicsObject::Colorize(BYTE *pixels, long cx, long cy, WORD bpp, COLORREF rgbBk, COLORREF rgbFg, BOOL removeAlpha)
  65. {
  66. BOOL result = Image_Colorize(pixels, cx, cy, bpp, rgbBk, rgbFg, removeAlpha);
  67. return (FALSE != result) ? S_OK : S_FALSE;
  68. }
  69. HRESULT GraphicsObject::BlendOnColor(HBITMAP hbmp, RECT *prcPart, BOOL premult, COLORREF rgb)
  70. {
  71. BOOL result = Image_BlendOnColor(hbmp, prcPart, premult, rgb);
  72. return (FALSE != result) ? S_OK : S_FALSE;
  73. }
  74. HRESULT GraphicsObject::BlendOnColor2(BYTE *pixels, int bitmapCX, int bitmapCY, long x, long y, long cx, long cy, WORD bpp, BOOL premult, COLORREF rgb)
  75. {
  76. BOOL result = Image_BlendOnColorEx(pixels, bitmapCX, bitmapCY, x, y, cx, cy, bpp, premult, rgb);
  77. return (FALSE != result) ? S_OK : S_FALSE;
  78. }
  79. HRESULT GraphicsObject::Premultiply(BYTE *pixels, long cx, long cy)
  80. {
  81. BOOL result = Image_Premultiply(pixels, cx, cy);
  82. return (FALSE != result) ? S_OK : S_FALSE;
  83. }
  84. HRESULT GraphicsObject::AlphaBlend(HDC hdcDest, const RECT *rectDest, HDC hdcSrc, const RECT *rectSrc, BLENDFUNCTION blendFunction)
  85. {
  86. if (NULL == rectDest || NULL == rectSrc)
  87. return E_INVALIDARG;
  88. BOOL result = Image_AlphaBlend(hdcDest, rectDest->left, rectDest->top, rectDest->right, rectDest->bottom, hdcSrc, rectSrc->left, rectSrc->top, rectSrc->right, rectSrc->bottom, blendFunction);
  89. return (FALSE != result) ? S_OK : S_FALSE;
  90. }
  91. HRESULT GraphicsObject::AnimateRotation(HDC hdc, HBITMAP bitmapFrame, int frameCount, COLORREF rgbBk, BOOL fKeepSize, HBITMAP *result)
  92. {
  93. if (NULL == result) return E_POINTER;
  94. *result = Image_AnimateRotation(hdc, bitmapFrame, frameCount, rgbBk, fKeepSize);
  95. if (NULL == *result) return E_FAIL;
  96. return S_OK;
  97. }
  98. #define CBCLASS GraphicsObject
  99. START_DISPATCH;
  100. CB(ADDREF, AddRef)
  101. CB(RELEASE, Release)
  102. CB(QUERYINTERFACE, QueryInterface)
  103. CB(API_GETDISTANCE, GetDistance)
  104. CB(API_GETDARKER, GetDarker)
  105. CB(API_BLENDCOLOR, BlendColor)
  106. CB(API_COLORIZE, Colorize)
  107. CB(API_BLENDONCOLOR, BlendOnColor)
  108. CB(API_BLENDONCOLOR2, BlendOnColor2)
  109. CB(API_PREMULTIPLY, Premultiply)
  110. CB(API_ALPHABLEND, AlphaBlend)
  111. CB(API_ANIMATEROTATION, AnimateRotation)
  112. END_DISPATCH;
  113. #undef CBCLASS