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.
This commit is contained in:
ManoloFLTK 2022-08-10 10:53:29 +02:00
parent 7d7784d140
commit b8c227a8f2
3 changed files with 16 additions and 12 deletions

View File

@ -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();

View File

@ -20,6 +20,8 @@
#include <FL/Fl_Window.H>
#include <FL/fl_draw.H>
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();
}

View File

@ -51,6 +51,7 @@
# include <FL/fl_draw.H>
# include <FL/platform.H>
# include <FL/Fl_Image_Surface.H>
# include <FL/Fl_Tiled_Image.H>
# 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,