diff options
author | Jean-Francois Mauguit <jfmauguit@mac.com> | 2024-09-24 09:03:25 -0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-09-24 09:03:25 -0400 |
commit | bab614c421ed7ae329d26bf028c4a3b1d2450f5a (patch) | |
tree | 12f17f78986871dd2cfb0a56e5e93b545c1ae0d0 /Src/Plugins/General/gen_ml/graphics.cpp | |
parent | 4bde6044fddf053f31795b9eaccdd2a5a527d21f (diff) | |
parent | 20d28e80a5c861a9d5f449ea911ab75b4f37ad0d (diff) | |
download | winamp-bab614c421ed7ae329d26bf028c4a3b1d2450f5a.tar.gz |
Merge pull request #5 from WinampDesktop/community
Merge to main
Diffstat (limited to 'Src/Plugins/General/gen_ml/graphics.cpp')
-rw-r--r-- | Src/Plugins/General/gen_ml/graphics.cpp | 130 |
1 files changed, 130 insertions, 0 deletions
diff --git a/Src/Plugins/General/gen_ml/graphics.cpp b/Src/Plugins/General/gen_ml/graphics.cpp new file mode 100644 index 00000000..6fcded96 --- /dev/null +++ b/Src/Plugins/General/gen_ml/graphics.cpp @@ -0,0 +1,130 @@ +#include ".\graphics.h" +#include "ml.h" +#include <math.h> + +HBITMAP CreateBitmapMask(HBITMAP originalBmp, int cx, int cy) +{ + return CreateBitmapMask(originalBmp, NULL, cx, cy); +} + +HBITMAP CreateBitmapMask(HBITMAP originalBmp, COLORREF transColor) +{ + return CreateBitmapMask(originalBmp, transColor, 0, 0); +} + +HBITMAP CreateBitmapMask(HBITMAP originalBmp, COLORREF transColor, int cx, int cy) +{ + HDC hdcMem, hdcMem2; + HBITMAP hbmMask; + BITMAP bm; + + GetObjectW(originalBmp, sizeof(BITMAP), &bm); + hbmMask = CreateBitmap(bm.bmWidth, bm.bmHeight, 1, 1, NULL); + + hdcMem = CreateCompatibleDC(0); + hdcMem2 = CreateCompatibleDC(0); + + HBITMAP obmp = (HBITMAP)SelectObject(hdcMem, originalBmp); + HBITMAP omsk = (HBITMAP)SelectObject(hdcMem2, hbmMask); + + if (transColor == NULL) transColor = GetPixel(hdcMem, cx, cy); + + COLORREF oldColorBK = SetBkColor(hdcMem, transColor); + + BitBlt(hdcMem2, 0, 0, bm.bmWidth, bm.bmHeight, hdcMem, 0, 0, SRCCOPY); + + BitBlt(hdcMem, 0, 0, bm.bmWidth, bm.bmHeight, hdcMem2, 0, 0, SRCINVERT); + + SetBkColor(hdcMem, oldColorBK); + + SelectObject(hdcMem, obmp); + SelectObject(hdcMem2, omsk); + + DeleteDC(hdcMem); + DeleteDC(hdcMem2); + + return hbmMask; +} + +HBITMAP ConvertTo24bpp(HBITMAP bmp, int bpp) +{ + HDC hdcMem, hdcMem2; + HBITMAP hbm24; + BITMAP bm; + + GetObjectW(bmp, sizeof(BITMAP), &bm); + + hdcMem = CreateCompatibleDC(0); + hdcMem2 = CreateCompatibleDC(0); + + void *bits; + BITMAPINFOHEADER bi; + + ZeroMemory (&bi, sizeof (bi)); + bi.biSize = sizeof (bi); + bi.biWidth= bm.bmWidth; + bi.biHeight = -bm.bmHeight; + bi.biPlanes = 1; + bi.biBitCount= (WORD)(0xFF & bpp); + + hbm24 = CreateDIBSection(hdcMem2, (BITMAPINFO *)&bi, DIB_RGB_COLORS, &bits, NULL, NULL); + + HBITMAP oBmp = (HBITMAP)SelectObject(hdcMem, bmp); + HBITMAP oBmp24 = (HBITMAP)SelectObject(hdcMem2, hbm24); + + BitBlt(hdcMem2, 0, 0, bm.bmWidth, bm.bmHeight, hdcMem, 0, 0, SRCCOPY); + + SelectObject(hdcMem, oBmp); + SelectObject(hdcMem2, oBmp24); + + DeleteDC(hdcMem); + DeleteDC(hdcMem2); + + return hbm24; +} + + +// works only with DIB +HBITMAP PatchBitmapColors24(HBITMAP bitmap, COLORREF color1, COLORREF color2, BMPFILTERPROC filterProc) +{ + BITMAP bm; + + COLOR24 clrBG, clrFG; + INT x, y; + + if (!filterProc) return NULL; + + if (!GetObjectW(bitmap, sizeof(BITMAP), &bm) || !bm.bmBits || 24 != bm.bmBitsPixel) return NULL; + + clrBG.rgbRed = (BYTE)(0xFF & color1); + clrFG.rgbRed = (BYTE)(0xFF & color2); + clrBG.rgbGreen = (BYTE)(0xFF & (color1>>8)); + clrFG.rgbGreen = (BYTE)(0xFF & (color2>>8)); + clrBG.rgbBlue = (BYTE)(0xFF & (color1>>16)); + clrFG.rgbBlue = (BYTE)(0xFF & (color2>>16)); + + for (y = 0; y < bm.bmHeight; y++) + { + LONG width = (bm.bmWidthBytes%4)?bm.bmWidth*4:bm.bmWidthBytes; + // bm.bmWidthBytes can lie so is safer to go with bm.bmWidth if the dword alignment cbeck fails + // http://blogs.msdn.com/oldnewthing/archive/2004/10/26/247918.aspx#248529 + COLOR24 *cursor = (COLOR24*)(((BYTE*)bm.bmBits) + (width*y)); + for (x = 0; x < bm.bmWidth; x++, cursor++) filterProc(&clrBG, &clrFG, cursor); + } + return bitmap; +} + +void Filter1(const COLOR24 *color1, const COLOR24 *color2, COLOR24 *pixel) +{ + pixel->rgbBlue = (BYTE)(color1->rgbBlue - (int)((1.f - (pixel->rgbBlue /255.f))* (color1->rgbBlue - color2->rgbBlue))); + pixel->rgbGreen = (BYTE)(color1->rgbGreen - (int)((1.f - (pixel->rgbGreen /255.f))* (color1->rgbGreen - color2->rgbGreen))); + pixel->rgbRed = (BYTE)(color1->rgbRed - (int)((1.f - (pixel->rgbRed /255.f))* (color1->rgbRed - color2->rgbRed))); +} + +void Filter2(const COLOR24 *color1, const COLOR24 *color2, COLOR24 *pixel) +{ + float chrom = (float)pixel->rgbBlue / 255.f; + pixel->rgbBlue = (BYTE)(color1->rgbBlue * (1.f - chrom) + color2->rgbBlue * chrom); + pixel->rgbGreen = (BYTE)(color1->rgbGreen * (1.f - chrom) + color2->rgbGreen * chrom); + pixel->rgbRed = (BYTE)(color1->rgbRed * (1.f - chrom) + color2->rgbRed * chrom); +}
\ No newline at end of file |