From c472d5d8b76e17e4dd537ca20c9bfb144b06189c Mon Sep 17 00:00:00 2001 From: Manolo Gouy Date: Fri, 9 Feb 2018 13:48:22 +0000 Subject: Fix fl_read_image() under MacOS platform when GUI is rescaled. This commit also simplifies the platform-dependent support of fl_read_image(): only Fl_XXX_Screen_Driver::read_win_rectangle() contains platform-specific code to capture pixels from the current window or from an offscreen buffer. Platform-independent function Fl_Screen_Driver::traverse_to_gl_subwindows() captures subwindows that intersect with the area fl_read_image() targets. git-svn-id: file:///fltk/svn/fltk/branches/branch-1.4@12653 ea41ed52-d2ee-0310-a9c1-e6b18d33e121 --- src/drivers/Cocoa/Fl_Cocoa_Screen_Driver.H | 5 +- src/drivers/Cocoa/Fl_Cocoa_Screen_Driver.cxx | 125 ++++++++++----------- src/drivers/Cocoa/Fl_Cocoa_Window_Driver.H | 1 - src/drivers/GDI/Fl_GDI_Image_Surface_Driver.cxx | 2 +- src/drivers/Quartz/Fl_Quartz_Graphics_Driver.H | 2 + .../Quartz/Fl_Quartz_Graphics_Driver_rect.cxx | 24 ++++ src/drivers/WinAPI/Fl_WinAPI_Screen_Driver.H | 6 +- src/drivers/WinAPI/Fl_WinAPI_Screen_Driver.cxx | 17 ++- src/drivers/X11/Fl_X11_Screen_Driver.H | 4 +- src/drivers/X11/Fl_X11_Screen_Driver.cxx | 8 +- src/drivers/X11/Fl_X11_Window_Driver.cxx | 8 +- src/drivers/Xlib/Fl_Xlib_Copy_Surface_Driver.cxx | 2 +- src/drivers/Xlib/Fl_Xlib_Image_Surface_Driver.cxx | 2 +- 13 files changed, 113 insertions(+), 93 deletions(-) (limited to 'src/drivers') diff --git a/src/drivers/Cocoa/Fl_Cocoa_Screen_Driver.H b/src/drivers/Cocoa/Fl_Cocoa_Screen_Driver.H index 12d93b9e8..077416823 100644 --- a/src/drivers/Cocoa/Fl_Cocoa_Screen_Driver.H +++ b/src/drivers/Cocoa/Fl_Cocoa_Screen_Driver.H @@ -4,7 +4,7 @@ // Definition of Apple Cocoa Screen interface // for the Fast Light Tool Kit (FLTK). // -// Copyright 2010-2017 by Bill Spitzak and others. +// Copyright 2010-2018 by Bill Spitzak and others. // // This library is free software. Distribution and use rights are outlined in // the file "COPYING" which should have been included with this file. If this @@ -40,6 +40,7 @@ class Fl_Window; class Fl_Input; +class Fl_RGB_Image; class FL_EXPORT Fl_Cocoa_Screen_Driver : public Fl_Screen_Driver { @@ -87,7 +88,6 @@ public: int insertion_point_location(int *px, int *py, int *pheight); virtual int dnd(int use_selection); virtual int compose(int &del); - virtual uchar *read_image(uchar *p, int x, int y, int w, int h, int alpha); virtual int input_widget_handle_key(int key, unsigned mods, unsigned shift, Fl_Input *input); virtual int get_mouse(int &x, int &y); virtual void enable_im(); @@ -100,6 +100,7 @@ public: virtual APP_SCALING_CAPABILITY rescalable() { return SYSTEMWIDE_APP_SCALING; } virtual float scale(int n) {return scale_;} virtual void scale(int n, float f) { scale_ = f;} + virtual Fl_RGB_Image *read_win_rectangle(int X, int Y, int w, int h); virtual int run_also_windowless(); virtual int wait_also_windowless(double delay); private: diff --git a/src/drivers/Cocoa/Fl_Cocoa_Screen_Driver.cxx b/src/drivers/Cocoa/Fl_Cocoa_Screen_Driver.cxx index 213995126..d46879932 100644 --- a/src/drivers/Cocoa/Fl_Cocoa_Screen_Driver.cxx +++ b/src/drivers/Cocoa/Fl_Cocoa_Screen_Driver.cxx @@ -256,70 +256,6 @@ int Fl_Cocoa_Screen_Driver::compose(int &del) { return 1; } -uchar * // O - Pixel buffer or NULL if failed -Fl_Cocoa_Screen_Driver::read_image(uchar *p, // I - Pixel buffer or NULL to allocate - int x, // I - Left position - int y, // I - Top position - int w, // I - Width of area to read - int h, // I - Height of area to read - int alpha)// I - Alpha value for image (0 for none) -{ - uchar *base; - int rowBytes, delta; - float s = 1; - int ori_w = w, ori_h = h; - if (fl_window == NULL) { // reading from an offscreen buffer - CGContextRef src = (CGContextRef)Fl_Surface_Device::surface()->driver()->gc(); // get bitmap context - base = (uchar *)CGBitmapContextGetData(src); // get data - if(!base) return NULL; - int sw = CGBitmapContextGetWidth(src); - int sh = CGBitmapContextGetHeight(src); - if( (sw - x < w) || (sh - y < h) ) return NULL; - rowBytes = CGBitmapContextGetBytesPerRow(src); - delta = CGBitmapContextGetBitsPerPixel(src)/8; - Fl_Image_Surface *imgs = (Fl_Image_Surface*)Fl_Surface_Device::surface(); - int fltk_w, fltk_h; - imgs->printable_rect(&fltk_w, &fltk_h); - s = sw / float(fltk_w); - x *= s; y *= s; w *= s; h *= s; - if (x + w > sw) w = sw - x; - if (y + h > sh) h = sh - y; - } - else { // reading from current window - Fl_Cocoa_Window_Driver *d = Fl_Cocoa_Window_Driver::driver(Fl_Window::current()); - base = d->bitmap_from_window_rect(x,y,w,h,&delta); - if (!base) return NULL; - rowBytes = delta*w; - x = y = 0; - } - // Allocate the image data array as needed... - int d = alpha ? 4 : 3; - if (!p) p = new uchar[w * h * d]; - // Initialize the default colors/alpha in the whole image... - memset(p, alpha, w * h * d); - // Copy the image from the off-screen buffer to the memory buffer. - int idx, idy; // Current X & Y in image - uchar *pdst, *psrc; - for (idy = y, pdst = p; idy < h + y; idy ++) { - for (idx = 0, psrc = base + idy * rowBytes + x * delta; idx < w; idx ++, psrc += delta, pdst += d) { - pdst[0] = psrc[0]; // R - pdst[1] = psrc[1]; // G - pdst[2] = psrc[2]; // B - } - } - if (fl_window != NULL) delete[] base; - if (s != 1) { - Fl_RGB_Image *rgb = new Fl_RGB_Image(p, w, h, alpha ? 4 : 3); - rgb->alloc_array = 1; - Fl_RGB_Image *rgb2 = (Fl_RGB_Image*)rgb->copy(ori_w, ori_h); - rgb2->alloc_array = 0; - delete rgb; - p = (uchar*)rgb2->array; - delete rgb2; - } - return p; -} - int Fl_Cocoa_Screen_Driver::input_widget_handle_key(int key, unsigned mods, unsigned shift, Fl_Input *input) { @@ -401,6 +337,67 @@ void Fl_Cocoa_Screen_Driver::offscreen_size(Fl_Offscreen off, int &width, int &h height = CGBitmapContextGetHeight(off); } +Fl_RGB_Image *Fl_Cocoa_Screen_Driver::read_win_rectangle(int X, int Y, int w, int h) +{ + int bpp, bpr, depth = 4; + uchar *base, *p; + if (!fl_window) { // read from offscreen buffer + float s = 1; + CGContextRef src = (CGContextRef)Fl_Surface_Device::surface()->driver()->gc(); // get bitmap context + base = (uchar *)CGBitmapContextGetData(src); // get data + if(!base) return NULL; + int sw = CGBitmapContextGetWidth(src); + int sh = CGBitmapContextGetHeight(src); + if( (sw - X < w) || (sh - Y < h) ) return NULL; + bpr = CGBitmapContextGetBytesPerRow(src); + bpp = CGBitmapContextGetBitsPerPixel(src)/8; + Fl_Image_Surface *imgs = (Fl_Image_Surface*)Fl_Surface_Device::surface(); + int fltk_w, fltk_h; + imgs->printable_rect(&fltk_w, &fltk_h); + s = sw / float(fltk_w); + X *= s; Y *= s; w *= s; h *= s; + if (X + w > sw) w = sw - X; + if (Y + h > sh) h = sh - Y; + // Copy the image from the off-screen buffer to the memory buffer. + int idx, idy; // Current X & Y in image + uchar *pdst, *psrc; + p = new uchar[w * h * depth]; + for (idy = Y, pdst = p; idy < h + Y; idy ++) { + for (idx = 0, psrc = base + idy * bpr + X * bpp; idx < w; idx ++, psrc += bpp, pdst += depth) { + pdst[0] = psrc[0]; // R + pdst[1] = psrc[1]; // G + pdst[2] = psrc[2]; // B + } + } + bpr = 0; + } else { // read from window + Fl_Cocoa_Window_Driver *d = Fl_Cocoa_Window_Driver::driver(Fl_Window::current()); + CGImageRef cgimg = d->CGImage_from_window_rect(X, Y, w, h); + if (!cgimg) { + return NULL; + } + w = CGImageGetWidth(cgimg); + h = CGImageGetHeight(cgimg); + Fl_Image_Surface *surf = new Fl_Image_Surface(w, h); + Fl_Surface_Device::push_current(surf); + ((Fl_Quartz_Graphics_Driver*)fl_graphics_driver)->draw_CGImage(cgimg, 0, 0, w, h, 0, 0, w, h); + CGContextRef gc = (CGContextRef)fl_graphics_driver->gc(); + w = CGBitmapContextGetWidth(gc); + h = CGBitmapContextGetHeight(gc); + bpr = CGBitmapContextGetBytesPerRow(gc); + bpp = CGBitmapContextGetBitsPerPixel(gc)/8; + base = (uchar*)CGBitmapContextGetData(gc); + p = new uchar[bpr * h]; + memcpy(p, base, bpr * h); + Fl_Surface_Device::pop_current(); + delete surf; + CFRelease(cgimg); + } + Fl_RGB_Image *rgb = new Fl_RGB_Image(p, w, h, depth, bpr); + rgb->alloc_array = 1; + return rgb; +} + // // End of "$Id$". // diff --git a/src/drivers/Cocoa/Fl_Cocoa_Window_Driver.H b/src/drivers/Cocoa/Fl_Cocoa_Window_Driver.H index 08a8ce42c..ef64799c9 100644 --- a/src/drivers/Cocoa/Fl_Cocoa_Window_Driver.H +++ b/src/drivers/Cocoa/Fl_Cocoa_Window_Driver.H @@ -88,7 +88,6 @@ public: CGRect* subRect() { return subRect_; } // getter void subRect(CGRect *r) { subRect_ = r; } // setter static void destroy(FLWindow*); - unsigned char *bitmap_from_window_rect(int x, int y, int w, int h, int *bytesPerPixel); CGImageRef CGImage_from_window_rect(int x, int y, int w, int h); // --- window data diff --git a/src/drivers/GDI/Fl_GDI_Image_Surface_Driver.cxx b/src/drivers/GDI/Fl_GDI_Image_Surface_Driver.cxx index 573bca696..453a5f600 100644 --- a/src/drivers/GDI/Fl_GDI_Image_Surface_Driver.cxx +++ b/src/drivers/GDI/Fl_GDI_Image_Surface_Driver.cxx @@ -92,7 +92,7 @@ void Fl_GDI_Image_Surface_Driver::untranslate() { Fl_RGB_Image* Fl_GDI_Image_Surface_Driver::image() { - Fl_RGB_Image *image = Fl::screen_driver()->read_win_rectangle(NULL, 0, 0, width, height, 0); + Fl_RGB_Image *image = Fl::screen_driver()->read_win_rectangle( 0, 0, width, height); return image; } diff --git a/src/drivers/Quartz/Fl_Quartz_Graphics_Driver.H b/src/drivers/Quartz/Fl_Quartz_Graphics_Driver.H index 60a261e94..b23817d46 100644 --- a/src/drivers/Quartz/Fl_Quartz_Graphics_Driver.H +++ b/src/drivers/Quartz/Fl_Quartz_Graphics_Driver.H @@ -184,6 +184,8 @@ protected: #else void descriptor_init(const char* name, Fl_Fontsize Size, Fl_Quartz_Font_Descriptor *d); #endif + virtual bool overlay_rect_unscaled() {return false; } + virtual void overlay_rect(int x, int y, int w , int h); }; class Fl_Quartz_Printer_Graphics_Driver : public Fl_Quartz_Graphics_Driver { diff --git a/src/drivers/Quartz/Fl_Quartz_Graphics_Driver_rect.cxx b/src/drivers/Quartz/Fl_Quartz_Graphics_Driver_rect.cxx index 3a83b94d6..8153f4e78 100644 --- a/src/drivers/Quartz/Fl_Quartz_Graphics_Driver_rect.cxx +++ b/src/drivers/Quartz/Fl_Quartz_Graphics_Driver_rect.cxx @@ -173,6 +173,30 @@ void Fl_Quartz_Graphics_Driver::loop(int x, int y, int x1, int y1, int x2, int y if (quartz_line_width_ > 1.5f) CGContextSetShouldAntialias(gc_, false); } +// returns y of horizontal line corresponding to pixels of current window +// read by Fl_Cocoa_Screen_Driver::read_win_rectangle( -, y, -, 1) +// when GUI is scaled by s +static float overlay_y(int y, float s, int H) { + int a, b, c; + a = int(H*s) - (y+1)*s; // in Cocoa units from window bottom + c = (s > 1 ? s : 1); // height of pixels read in Cocoa units + b = H*s - (a+c); // top of read image from window top in Cocoa units + return b/s; // top of read image from window top in FLTK units +} + +void Fl_Quartz_Graphics_Driver::overlay_rect(int x, int y, int w , int h) { + float s = scale(); + CGContextSetLineWidth(gc_, 0.01); + int H = Fl_Window::current()->h(); + CGContextMoveToPoint(gc_, x, overlay_y(y, s, H)); + CGContextAddLineToPoint(gc_, x+w-1, overlay_y(y, s, H)); + CGContextAddLineToPoint(gc_, x+w-1, overlay_y(y+h-1, s, H)); + CGContextAddLineToPoint(gc_, x, overlay_y(y+h-1, s, H)); + CGContextClosePath(gc_); + CGContextStrokePath(gc_); + CGContextSetLineWidth(gc_, quartz_line_width_); +} + void Fl_Quartz_Graphics_Driver::polygon(int x, int y, int x1, int y1, int x2, int y2) { CGContextSetShouldAntialias(gc_, true); CGContextMoveToPoint(gc_, x, y); diff --git a/src/drivers/WinAPI/Fl_WinAPI_Screen_Driver.H b/src/drivers/WinAPI/Fl_WinAPI_Screen_Driver.H index e28fb791a..b5d238ba9 100644 --- a/src/drivers/WinAPI/Fl_WinAPI_Screen_Driver.H +++ b/src/drivers/WinAPI/Fl_WinAPI_Screen_Driver.H @@ -4,7 +4,7 @@ // Definition of MSWindows Win32/64 Screen interface // for the Fast Light Tool Kit (FLTK). // -// Copyright 2010-2016 by Bill Spitzak and others. +// Copyright 2010-2018 by Bill Spitzak and others. // // This library is free software. Distribution and use rights are outlined in // the file "COPYING" which should have been included with this file. If this @@ -79,8 +79,8 @@ public: virtual void remove_timeout(Fl_Timeout_Handler cb, void *argp); virtual int dnd(int unused); virtual int compose(int &del); - virtual Fl_RGB_Image *read_win_rectangle(uchar *p, int X, int Y, int w, int h, int alpha); - Fl_RGB_Image *read_win_rectangle_unscaled(uchar *p, int X, int Y, int w, int h, int alpha); + virtual Fl_RGB_Image *read_win_rectangle(int X, int Y, int w, int h); + Fl_RGB_Image *read_win_rectangle_unscaled(int X, int Y, int w, int h); virtual int get_mouse(int &x, int &y); virtual void enable_im(); virtual void disable_im(); diff --git a/src/drivers/WinAPI/Fl_WinAPI_Screen_Driver.cxx b/src/drivers/WinAPI/Fl_WinAPI_Screen_Driver.cxx index 9373d345d..f44fa7a3c 100644 --- a/src/drivers/WinAPI/Fl_WinAPI_Screen_Driver.cxx +++ b/src/drivers/WinAPI/Fl_WinAPI_Screen_Driver.cxx @@ -496,24 +496,21 @@ int Fl_WinAPI_Screen_Driver::compose(int &del) { Fl_RGB_Image * // O - image or NULL if failed -Fl_WinAPI_Screen_Driver::read_win_rectangle(uchar *p, // I - Pixel buffer or NULL to allocate +Fl_WinAPI_Screen_Driver::read_win_rectangle( int X, // I - Left position int Y, // I - Top position int w, // I - Width of area to read - int h, // I - Height of area to read - int alpha) // I - Alpha value for image (0 for none) + int h) // I - Height of area to read { float s = Fl_Surface_Device::surface()->driver()->scale(); - return read_win_rectangle_unscaled(p, X*s, Y*s, w*s, h*s, alpha); + return read_win_rectangle_unscaled(X*s, Y*s, w*s, h*s); } -Fl_RGB_Image *Fl_WinAPI_Screen_Driver::read_win_rectangle_unscaled(uchar *p, int X, int Y, int w, int h, int alpha) +Fl_RGB_Image *Fl_WinAPI_Screen_Driver::read_win_rectangle_unscaled(int X, int Y, int w, int h) { - int d; // Depth of image - - // Allocate the image data array as needed... - d = alpha ? 4 : 3; - + int d = 3; // Depth of image + int alpha = 0; uchar *p = NULL; + // Allocate the image data array as needed... const uchar *oldp = p; if (!p) p = new uchar[w * h * d]; diff --git a/src/drivers/X11/Fl_X11_Screen_Driver.H b/src/drivers/X11/Fl_X11_Screen_Driver.H index 6ceca775c..78a0510a5 100644 --- a/src/drivers/X11/Fl_X11_Screen_Driver.H +++ b/src/drivers/X11/Fl_X11_Screen_Driver.H @@ -4,7 +4,7 @@ // Definition of X11 Screen interface // for the Fast Light Tool Kit (FLTK). // -// Copyright 2010-2017 by Bill Spitzak and others. +// Copyright 2010-2018 by Bill Spitzak and others. // // This library is free software. Distribution and use rights are outlined in // the file "COPYING" which should have been included with this file. If this @@ -93,7 +93,7 @@ public: virtual int compose(int &del); virtual void compose_reset(); virtual int text_display_can_leak(); - virtual Fl_RGB_Image *read_win_rectangle(uchar *p, int X, int Y, int w, int h, int alpha); + virtual Fl_RGB_Image *read_win_rectangle(int X, int Y, int w, int h); virtual int get_mouse(int &x, int &y); virtual void enable_im(); virtual void disable_im(); diff --git a/src/drivers/X11/Fl_X11_Screen_Driver.cxx b/src/drivers/X11/Fl_X11_Screen_Driver.cxx index 6b253931d..7f6dc5528 100644 --- a/src/drivers/X11/Fl_X11_Screen_Driver.cxx +++ b/src/drivers/X11/Fl_X11_Screen_Driver.cxx @@ -750,7 +750,7 @@ extern "C" { } } -Fl_RGB_Image *Fl_X11_Screen_Driver::read_win_rectangle(uchar *p, int X, int Y, int w, int h, int alpha) +Fl_RGB_Image *Fl_X11_Screen_Driver::read_win_rectangle(int X, int Y, int w, int h) { XImage *image; // Captured image int i, maxindex; // Looping vars @@ -862,14 +862,14 @@ Fl_RGB_Image *Fl_X11_Screen_Driver::read_win_rectangle(uchar *p, int X, int Y, i printf("map_entries = %d\n", fl_visual->visual->map_entries); #endif // DEBUG - d = alpha ? 4 : 3; - + d = 3; + uchar *p = NULL; // Allocate the image data array as needed... const uchar *oldp = p; if (!p) p = new uchar[w * h * d]; // Initialize the default colors/alpha in the whole image... - memset(p, alpha, w * h * d); + memset(p, 0, w * h * d); // Check that we have valid mask/shift values... if (!image->red_mask && image->bits_per_pixel > 12) { diff --git a/src/drivers/X11/Fl_X11_Window_Driver.cxx b/src/drivers/X11/Fl_X11_Window_Driver.cxx index 5612ce4df..b89688b47 100644 --- a/src/drivers/X11/Fl_X11_Window_Driver.cxx +++ b/src/drivers/X11/Fl_X11_Window_Driver.cxx @@ -413,22 +413,22 @@ void Fl_X11_Window_Driver::capture_titlebar_and_borders(Fl_Shared_Image*& top, F htop /= s; wsides /= s; fl_window = parent; if (htop) { - r_top = Fl::screen_driver()->read_win_rectangle(NULL, 0, 0, - (w() + 2 * wsides), htop, 0); + r_top = Fl::screen_driver()->read_win_rectangle(0, 0, - (w() + 2 * wsides), htop); top = Fl_Shared_Image::get(r_top); top->scale(w() + 2 * wsides, htop, 0, 1); } if (wsides) { - r_left = Fl::screen_driver()->read_win_rectangle(NULL, 0, htop, -wsides, h(), 0); + r_left = Fl::screen_driver()->read_win_rectangle(0, htop, -wsides, h()); if (r_left) { left = Fl_Shared_Image::get(r_left); left->scale(wsides, h(), 0, 1); } - r_right = Fl::screen_driver()->read_win_rectangle(NULL, w() + wsides, htop, -wsides, h(), 0); + r_right = Fl::screen_driver()->read_win_rectangle(w() + wsides, htop, -wsides, h()); if (r_right) { right = Fl_Shared_Image::get(r_right); right->scale(wsides, h(), 0, 1); } - r_bottom = Fl::screen_driver()->read_win_rectangle(NULL, 0, htop + h(), -(w() + 2*wsides), hbottom, 0); + r_bottom = Fl::screen_driver()->read_win_rectangle(0, htop + h(), -(w() + 2*wsides), hbottom); if (r_bottom) { bottom = Fl_Shared_Image::get(r_bottom); bottom->scale(w() + 2*wsides, wsides, 0, 1); diff --git a/src/drivers/Xlib/Fl_Xlib_Copy_Surface_Driver.cxx b/src/drivers/Xlib/Fl_Xlib_Copy_Surface_Driver.cxx index 73a69109b..74a5022f4 100644 --- a/src/drivers/Xlib/Fl_Xlib_Copy_Surface_Driver.cxx +++ b/src/drivers/Xlib/Fl_Xlib_Copy_Surface_Driver.cxx @@ -67,7 +67,7 @@ Fl_Xlib_Copy_Surface_Driver::~Fl_Xlib_Copy_Surface_Driver() { driver()->pop_clip(); bool need_push = (Fl_Surface_Device::surface() != this); if (need_push) Fl_Surface_Device::push_current(this); - Fl_RGB_Image *rgb = Fl::screen_driver()->read_win_rectangle(NULL, 0, 0, width, height, 0); + Fl_RGB_Image *rgb = Fl::screen_driver()->read_win_rectangle(0, 0, width, height); if (need_push) Fl_Surface_Device::pop_current(); Fl_X11_Screen_Driver::copy_image(rgb->array, rgb->w(), rgb->h(), 1); delete rgb; diff --git a/src/drivers/Xlib/Fl_Xlib_Image_Surface_Driver.cxx b/src/drivers/Xlib/Fl_Xlib_Image_Surface_Driver.cxx index 0d3383cc5..c5bcce9af 100644 --- a/src/drivers/Xlib/Fl_Xlib_Image_Surface_Driver.cxx +++ b/src/drivers/Xlib/Fl_Xlib_Image_Surface_Driver.cxx @@ -80,7 +80,7 @@ void Fl_Xlib_Image_Surface_Driver::untranslate() { Fl_RGB_Image* Fl_Xlib_Image_Surface_Driver::image() { - Fl_RGB_Image *image = Fl::screen_driver()->read_win_rectangle(NULL, 0, 0, width, height, 0); + Fl_RGB_Image *image = Fl::screen_driver()->read_win_rectangle(0, 0, width, height); return image; } -- cgit v1.2.3