From b8c227a8f2bb004de5819c93be5b7f6218402716 Mon Sep 17 00:00:00 2001 From: ManoloFLTK <41016272+ManoloFLTK@users.noreply.github.com> Date: Wed, 10 Aug 2022 10:53:29 +0200 Subject: [PATCH] Fix Fl_Xlib_Graphics_Driver for drawing tiled images. Conflicting demands arise in the implementation of class Fl_Xlib_Graphics_Driver for drawing images with the XRender library : 1) Issue #163 leads to use a bilinear filter to draw-and-scale images. 2) This tends to blur the edges of drawn areas which is bad for tiled images (that is because the edges get alpha values, even for an opaque source image). This commit resolves the conflict adding a means to detect whether the library is busy drawing a tiled image. If so, the bilinear filter is not applied, drawn areas don't have blurred edges, resulting in a nice tiling. With this commit, these test apps perform correctly: - tiled_image is correct at all scaling factor values also when modified to use a depth-3 or a depth-4 Fl_RGB_Image as tile; - unittests - Drawing Images is correct at all scaling factor values; - pixmap_browser scales correctly up and down JPEG and PNG images. --- FL/Fl_Tiled_Image.H | 9 ++++++--- src/Fl_Tiled_Image.cxx | 4 ++++ .../Xlib/Fl_Xlib_Graphics_Driver_image.cxx | 15 ++++++--------- 3 files changed, 16 insertions(+), 12 deletions(-) diff --git a/FL/Fl_Tiled_Image.H b/FL/Fl_Tiled_Image.H index 599de4b4e..ebccd3be4 100644 --- a/FL/Fl_Tiled_Image.H +++ b/FL/Fl_Tiled_Image.H @@ -30,13 +30,16 @@ color_average(), desaturate(), or inactive() methods. */ class FL_EXPORT Fl_Tiled_Image : public Fl_Image { - protected: +private: + static bool drawing_tiled_image_; +protected: Fl_Image *image_; // The image that is tiled int alloc_image_; // Did we allocate this image? - public: - +public: + /** Returns true when the FLTK library is currently drawing an Fl_Tiled_Image object. */ + static inline bool drawing_tiled_image() { return drawing_tiled_image_;}; Fl_Tiled_Image(Fl_Image *i, int W = 0, int H = 0); virtual ~Fl_Tiled_Image(); diff --git a/src/Fl_Tiled_Image.cxx b/src/Fl_Tiled_Image.cxx index 2ec62e79a..cfc89bfb4 100644 --- a/src/Fl_Tiled_Image.cxx +++ b/src/Fl_Tiled_Image.cxx @@ -20,6 +20,8 @@ #include #include +bool Fl_Tiled_Image::drawing_tiled_image_ = false; + /** The constructors create a new tiled image containing the specified image. Use a width and height of 0 to tile the whole window/widget. @@ -181,6 +183,7 @@ Fl_Tiled_Image::draw(int X, // I - Starting X position if (W == 0 || H == 0) return; fl_push_clip(X, Y, W, H); + drawing_tiled_image_ = true; if (cx > 0) iw -= cx; // crop image if (cy > 0) ih -= cy; @@ -194,5 +197,6 @@ Fl_Tiled_Image::draw(int X, // I - Starting X position } } } + drawing_tiled_image_ = false; fl_pop_clip(); } diff --git a/src/drivers/Xlib/Fl_Xlib_Graphics_Driver_image.cxx b/src/drivers/Xlib/Fl_Xlib_Graphics_Driver_image.cxx index 73bea246e..d57e5b031 100644 --- a/src/drivers/Xlib/Fl_Xlib_Graphics_Driver_image.cxx +++ b/src/drivers/Xlib/Fl_Xlib_Graphics_Driver_image.cxx @@ -51,6 +51,7 @@ # include # include # include +# include # include "../../Fl_Screen_Driver.H" # include "../../Fl_XColor.H" # include "../../flstring.h" @@ -780,16 +781,9 @@ void Fl_Xlib_Graphics_Driver::draw_rgb(Fl_RGB_Image *rgb, int XP, int YP, int WP if (Wfull == 0 || Hfull == 0) return; bool need_clip = (cx || cy || WP < rgb->w() || HP < rgb->h()); if (need_clip) push_clip(XP, YP, WP, HP); - int offset = 0; - if (Wfull > rgb->data_w() || Hfull > rgb->data_h()) { - // When enlarging while drawing with XRender, 1 pixel around target area seems unpainted, - // so we increase a bit the target area and move it 1 pixel to left and top. - Wfull = (rgb->w()+2)*scale(), Hfull = (rgb->h()+2)*scale(); - offset = 1; - } scale_and_render_pixmap( *Fl_Graphics_Driver::id(rgb), rgb->d(), rgb->data_w() / double(Wfull), rgb->data_h() / double(Hfull), - Xs + this->floor(offset_x_) - offset, Ys + this->floor(offset_y_) - offset, + Xs + this->floor(offset_x_), Ys + this->floor(offset_y_), Wfull, Hfull); if (need_clip) pop_clip(); } @@ -830,7 +824,10 @@ int Fl_Xlib_Graphics_Driver::scale_and_render_pixmap(Fl_Offscreen pixmap, int de { XDoubleToFixed( 0 ), XDoubleToFixed( 0 ), XDoubleToFixed( 1 ) } }}; XRenderSetPictureTransform(fl_display, src, &mat); - if (Fl_Image::scaling_algorithm() == FL_RGB_SCALING_BILINEAR) { + if (Fl_Image::scaling_algorithm() == FL_RGB_SCALING_BILINEAR && + !Fl_Tiled_Image::drawing_tiled_image()) { + // The filter is not used when drawing tiled images because drawn image edges + // become somewhat blurry. XRenderSetPictureFilter(fl_display, src, FilterBilinear, 0, 0); // A note at https://www.talisman.org/~erlkonig/misc/x11-composite-tutorial/ : // "When you use a filter you'll probably want to use PictOpOver as the render op,