graphics.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. #include ".\graphics.h"
  2. #include "ml.h"
  3. #include <math.h>
  4. HBITMAP CreateBitmapMask(HBITMAP originalBmp, int cx, int cy)
  5. {
  6. return CreateBitmapMask(originalBmp, NULL, cx, cy);
  7. }
  8. HBITMAP CreateBitmapMask(HBITMAP originalBmp, COLORREF transColor)
  9. {
  10. return CreateBitmapMask(originalBmp, transColor, 0, 0);
  11. }
  12. HBITMAP CreateBitmapMask(HBITMAP originalBmp, COLORREF transColor, int cx, int cy)
  13. {
  14. HDC hdcMem, hdcMem2;
  15. HBITMAP hbmMask;
  16. BITMAP bm;
  17. GetObjectW(originalBmp, sizeof(BITMAP), &bm);
  18. hbmMask = CreateBitmap(bm.bmWidth, bm.bmHeight, 1, 1, NULL);
  19. hdcMem = CreateCompatibleDC(0);
  20. hdcMem2 = CreateCompatibleDC(0);
  21. HBITMAP obmp = (HBITMAP)SelectObject(hdcMem, originalBmp);
  22. HBITMAP omsk = (HBITMAP)SelectObject(hdcMem2, hbmMask);
  23. if (transColor == NULL) transColor = GetPixel(hdcMem, cx, cy);
  24. COLORREF oldColorBK = SetBkColor(hdcMem, transColor);
  25. BitBlt(hdcMem2, 0, 0, bm.bmWidth, bm.bmHeight, hdcMem, 0, 0, SRCCOPY);
  26. BitBlt(hdcMem, 0, 0, bm.bmWidth, bm.bmHeight, hdcMem2, 0, 0, SRCINVERT);
  27. SetBkColor(hdcMem, oldColorBK);
  28. SelectObject(hdcMem, obmp);
  29. SelectObject(hdcMem2, omsk);
  30. DeleteDC(hdcMem);
  31. DeleteDC(hdcMem2);
  32. return hbmMask;
  33. }
  34. HBITMAP ConvertTo24bpp(HBITMAP bmp, int bpp)
  35. {
  36. HDC hdcMem, hdcMem2;
  37. HBITMAP hbm24;
  38. BITMAP bm;
  39. GetObjectW(bmp, sizeof(BITMAP), &bm);
  40. hdcMem = CreateCompatibleDC(0);
  41. hdcMem2 = CreateCompatibleDC(0);
  42. void *bits;
  43. BITMAPINFOHEADER bi;
  44. ZeroMemory (&bi, sizeof (bi));
  45. bi.biSize = sizeof (bi);
  46. bi.biWidth= bm.bmWidth;
  47. bi.biHeight = -bm.bmHeight;
  48. bi.biPlanes = 1;
  49. bi.biBitCount= (WORD)(0xFF & bpp);
  50. hbm24 = CreateDIBSection(hdcMem2, (BITMAPINFO *)&bi, DIB_RGB_COLORS, &bits, NULL, NULL);
  51. HBITMAP oBmp = (HBITMAP)SelectObject(hdcMem, bmp);
  52. HBITMAP oBmp24 = (HBITMAP)SelectObject(hdcMem2, hbm24);
  53. BitBlt(hdcMem2, 0, 0, bm.bmWidth, bm.bmHeight, hdcMem, 0, 0, SRCCOPY);
  54. SelectObject(hdcMem, oBmp);
  55. SelectObject(hdcMem2, oBmp24);
  56. DeleteDC(hdcMem);
  57. DeleteDC(hdcMem2);
  58. return hbm24;
  59. }
  60. // works only with DIB
  61. HBITMAP PatchBitmapColors24(HBITMAP bitmap, COLORREF color1, COLORREF color2, BMPFILTERPROC filterProc)
  62. {
  63. BITMAP bm;
  64. COLOR24 clrBG, clrFG;
  65. INT x, y;
  66. if (!filterProc) return NULL;
  67. if (!GetObjectW(bitmap, sizeof(BITMAP), &bm) || !bm.bmBits || 24 != bm.bmBitsPixel) return NULL;
  68. clrBG.rgbRed = (BYTE)(0xFF & color1);
  69. clrFG.rgbRed = (BYTE)(0xFF & color2);
  70. clrBG.rgbGreen = (BYTE)(0xFF & (color1>>8));
  71. clrFG.rgbGreen = (BYTE)(0xFF & (color2>>8));
  72. clrBG.rgbBlue = (BYTE)(0xFF & (color1>>16));
  73. clrFG.rgbBlue = (BYTE)(0xFF & (color2>>16));
  74. for (y = 0; y < bm.bmHeight; y++)
  75. {
  76. LONG width = (bm.bmWidthBytes%4)?bm.bmWidth*4:bm.bmWidthBytes;
  77. // bm.bmWidthBytes can lie so is safer to go with bm.bmWidth if the dword alignment cbeck fails
  78. // http://blogs.msdn.com/oldnewthing/archive/2004/10/26/247918.aspx#248529
  79. COLOR24 *cursor = (COLOR24*)(((BYTE*)bm.bmBits) + (width*y));
  80. for (x = 0; x < bm.bmWidth; x++, cursor++) filterProc(&clrBG, &clrFG, cursor);
  81. }
  82. return bitmap;
  83. }
  84. void Filter1(const COLOR24 *color1, const COLOR24 *color2, COLOR24 *pixel)
  85. {
  86. pixel->rgbBlue = (BYTE)(color1->rgbBlue - (int)((1.f - (pixel->rgbBlue /255.f))* (color1->rgbBlue - color2->rgbBlue)));
  87. pixel->rgbGreen = (BYTE)(color1->rgbGreen - (int)((1.f - (pixel->rgbGreen /255.f))* (color1->rgbGreen - color2->rgbGreen)));
  88. pixel->rgbRed = (BYTE)(color1->rgbRed - (int)((1.f - (pixel->rgbRed /255.f))* (color1->rgbRed - color2->rgbRed)));
  89. }
  90. void Filter2(const COLOR24 *color1, const COLOR24 *color2, COLOR24 *pixel)
  91. {
  92. float chrom = (float)pixel->rgbBlue / 255.f;
  93. pixel->rgbBlue = (BYTE)(color1->rgbBlue * (1.f - chrom) + color2->rgbBlue * chrom);
  94. pixel->rgbGreen = (BYTE)(color1->rgbGreen * (1.f - chrom) + color2->rgbGreen * chrom);
  95. pixel->rgbRed = (BYTE)(color1->rgbRed * (1.f - chrom) + color2->rgbRed * chrom);
  96. }