diff options
| author | ManoloFLTK <41016272+ManoloFLTK@users.noreply.github.com> | 2023-11-20 17:13:09 +0100 |
|---|---|---|
| committer | ManoloFLTK <41016272+ManoloFLTK@users.noreply.github.com> | 2023-12-04 12:34:44 +0100 |
| commit | 3e61ec7044942ebaeda948c64df762a0250f8954 (patch) | |
| tree | 642c08a6f7b120c2237f5e457074670d0ccf3e86 /src/drivers/GDI | |
| parent | b9ac6bd728659a7780d7cde024f34607601e8a7f (diff) | |
New member function Fl_Image_Surface::mask(Fl_RGB_Image*)
Diffstat (limited to 'src/drivers/GDI')
| -rw-r--r-- | src/drivers/GDI/Fl_GDI_Graphics_Driver.H | 1 | ||||
| -rw-r--r-- | src/drivers/GDI/Fl_GDI_Image_Surface_Driver.H | 6 | ||||
| -rw-r--r-- | src/drivers/GDI/Fl_GDI_Image_Surface_Driver.cxx | 86 |
3 files changed, 93 insertions, 0 deletions
diff --git a/src/drivers/GDI/Fl_GDI_Graphics_Driver.H b/src/drivers/GDI/Fl_GDI_Graphics_Driver.H index 4d1f45c82..03371c8c4 100644 --- a/src/drivers/GDI/Fl_GDI_Graphics_Driver.H +++ b/src/drivers/GDI/Fl_GDI_Graphics_Driver.H @@ -70,6 +70,7 @@ public: // --- bitmap stuff static HBITMAP create_bitmask(int w, int h, const uchar *array); // NOT virtual + static HBITMAP calc_HBITMAP_mask(Fl_RGB_Image *mask); void delete_bitmask(fl_uintptr_t bm) FL_OVERRIDE; HBITMAP create_alphamask(int w, int h, int d, int ld, const uchar *array); void draw_unscaled(const char* str, int n, int x, int y) FL_OVERRIDE; diff --git a/src/drivers/GDI/Fl_GDI_Image_Surface_Driver.H b/src/drivers/GDI/Fl_GDI_Image_Surface_Driver.H index d44d57cd6..129a4ecbc 100644 --- a/src/drivers/GDI/Fl_GDI_Image_Surface_Driver.H +++ b/src/drivers/GDI/Fl_GDI_Image_Surface_Driver.H @@ -26,6 +26,12 @@ class Fl_GDI_Image_Surface_Driver : public Fl_Image_Surface_Driver { public: HWND pre_window; int _savedc; + void mask(const Fl_RGB_Image *) FL_OVERRIDE; + struct shape_data_type { + HBITMAP background; + uchar *vBits; + Fl_RGB_Image* mask; + } *shape_data_; Fl_GDI_Image_Surface_Driver(int w, int h, int high_res, Fl_Offscreen off); ~Fl_GDI_Image_Surface_Driver(); void set_current() FL_OVERRIDE; diff --git a/src/drivers/GDI/Fl_GDI_Image_Surface_Driver.cxx b/src/drivers/GDI/Fl_GDI_Image_Surface_Driver.cxx index 5a4d5ada7..7e1c2f24e 100644 --- a/src/drivers/GDI/Fl_GDI_Image_Surface_Driver.cxx +++ b/src/drivers/GDI/Fl_GDI_Image_Surface_Driver.cxx @@ -19,6 +19,7 @@ #include "../WinAPI/Fl_WinAPI_Screen_Driver.H" #include "Fl_GDI_Image_Surface_Driver.H" #include <FL/platform.H> +#include <FL/Fl_Bitmap.H> #include <windows.h> @@ -35,10 +36,16 @@ Fl_GDI_Image_Surface_Driver::Fl_GDI_Image_Surface_Driver(int w, int h, int high_ driver(Fl_Graphics_Driver::newMainGraphicsDriver()); if (d != 1 && high_res) ((Fl_GDI_Graphics_Driver*)driver())->scale(d); origin.x = origin.y = 0; + shape_data_ = NULL; } Fl_GDI_Image_Surface_Driver::~Fl_GDI_Image_Surface_Driver() { + if (shape_data_ && shape_data_->background) { + DeleteObject(shape_data_->background); + delete shape_data_->mask; + free(shape_data_); + } if (offscreen && !external_offscreen) DeleteObject((HBITMAP)offscreen); delete driver(); } @@ -67,6 +74,43 @@ void Fl_GDI_Image_Surface_Driver::untranslate() { Fl_RGB_Image* Fl_GDI_Image_Surface_Driver::image() { + if (shape_data_ && shape_data_->background) { + // get the offscreen size in pixels + HDC gc = fl_makeDC((HBITMAP)offscreen); + BITMAPINFO bmi; + bmi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER); + bmi.bmiHeader.biCompression = BI_RGB; + bmi.bmiHeader.biPlanes = 1; + bmi.bmiHeader.biBitCount = 0; + bmi.bmiHeader.biSizeImage = 0; + GetDIBits(gc, (HBITMAP)offscreen, 0, 0, NULL, &bmi, DIB_RGB_COLORS); + int W = bmi.bmiHeader.biWidth; + int H = bmi.bmiHeader.biHeight; + int line_size = ((3*W+3)/4) * 4; + + // read bits of main offscreen + uchar *dib_src = new uchar[line_size * H]; + bmi.bmiHeader.biWidth = W; + bmi.bmiHeader.biHeight = H; + bmi.bmiHeader.biCompression = BI_RGB; + bmi.bmiHeader.biBitCount = 24; + GetDIBits(gc, (HBITMAP)offscreen, 0, H, + dib_src, &bmi, DIB_RGB_COLORS); + + // draw above the secondary offscreen the main offscreen masked by shape_data_->mask + GdiFlush(); + Fl_Image_Surface_Driver::copy_with_mask(shape_data_->mask, shape_data_->vBits, dib_src, ((3*W+3)/4) * 4, true); + delete shape_data_->mask; + delete[] dib_src; + + // write bits of main offscreen + SetDIBits(gc, (HBITMAP)offscreen, 0, H, shape_data_->vBits, &bmi, DIB_RGB_COLORS); + DeleteDC(gc); + DeleteObject(shape_data_->background); + shape_data_->background = NULL; + free(shape_data_); + shape_data_ = NULL; + } Fl_RGB_Image *image = Fl::screen_driver()->read_win_rectangle( 0, 0, width, height, 0); return image; } @@ -81,3 +125,45 @@ void Fl_GDI_Image_Surface_Driver::end_current() fl_window = pre_window; Fl_Surface_Device::end_current(); } + + +void Fl_GDI_Image_Surface_Driver::mask(const Fl_RGB_Image *mask) { + shape_data_ = (struct shape_data_type*)calloc(1, sizeof(struct shape_data_type)); + // get the offscreen size in pixels + HDC gc = fl_makeDC((HBITMAP)offscreen); + BITMAPINFO bmi; + bmi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER); + bmi.bmiHeader.biCompression = BI_RGB; + bmi.bmiHeader.biPlanes = 1; + bmi.bmiHeader.biBitCount = 0; + bmi.bmiHeader.biSizeImage = 0; + + GetDIBits(gc, (HBITMAP)offscreen, 0, 0, NULL, &bmi, DIB_RGB_COLORS); + int W = bmi.bmiHeader.biWidth; + int H = bmi.bmiHeader.biHeight; + + shape_data_->mask = Fl_Image_Surface_Driver::RGB3_to_RGB1(mask, W, H); + + // duplicate current offscreen content to new offscreen + int line_size = ((3*W+3)/4) * 4; + uchar *dib = new uchar[line_size * H]; // create temporary buffer to read DIB + bmi.bmiHeader.biWidth = W; + bmi.bmiHeader.biHeight = H; + bmi.bmiHeader.biCompression = BI_RGB; + bmi.bmiHeader.biBitCount = 24; + + GetDIBits(gc, (HBITMAP)offscreen, 0, H, dib, &bmi, DIB_RGB_COLORS); + + HDC background_gc = CreateCompatibleDC(gc); + shape_data_->background = + CreateDIBSection(background_gc, &bmi, DIB_RGB_COLORS, + (void**)&shape_data_->vBits, NULL, 0); + if (!shape_data_->background) { + Fl::error("CreateDIBSection error=%lu", GetLastError()); + } + memcpy(shape_data_->vBits, dib, H * line_size); + delete[] dib; + DeleteDC(background_gc); + DeleteDC(gc); +} + |
