Image classes: memorise the width and the height of the cached form of the image to support GUI scaling

git-svn-id: file:///fltk/svn/fltk/branches/branch-1.4@12811 ea41ed52-d2ee-0310-a9c1-e6b18d33e121
This commit is contained in:
Manolo Gouy 2018-03-28 13:00:12 +00:00
parent 29bda776bb
commit 458d063643
10 changed files with 79 additions and 38 deletions

View File

@ -44,15 +44,15 @@ private:
int &X, int &Y, int &W, int &H);
/** for internal use */
fl_uintptr_t id_;
float cache_scale_; // graphics scaling value when id_ was computed
int cache_w_, cache_h_; // size of bitmap when cached
public:
/** The constructors create a new bitmap from the specified bitmap data */
Fl_Bitmap(const uchar *bits, int W, int H) :
Fl_Image(W,H,0), array(bits), alloc_array(0), id_(0), cache_scale_(1) {data((const char **)&array, 1);}
Fl_Image(W,H,0), array(bits), alloc_array(0), id_(0), cache_w_(0),cache_h_(0) {data((const char **)&array, 1);}
/** The constructors create a new bitmap from the specified bitmap data */
Fl_Bitmap(const char *bits, int W, int H) :
Fl_Image(W,H,0), array((const uchar *)bits), alloc_array(0), id_(0), cache_scale_(1) {data((const char **)&array, 1);}
Fl_Image(W,H,0), array((const uchar *)bits), alloc_array(0), id_(0), cache_w_(0),cache_h_(0) {data((const char **)&array, 1);}
virtual ~Fl_Bitmap();
virtual Fl_Image *copy(int W, int H);
Fl_Image *copy() { return Fl_Image::copy(); }

View File

@ -199,12 +199,21 @@ protected:
static fl_uintptr_t* mask(Fl_RGB_Image *rgb) {return &(rgb->mask_);}
/** Accessor to a private member variable of Fl_Pixmap */
static fl_uintptr_t* mask(Fl_Pixmap *pm) {return &(pm->mask_);}
/** Accessor to a private member variable of Fl_Pixmap */
static float* cache_scale(Fl_Pixmap *pm) {return &(pm->cache_scale_);}
/** Accessor to a private member variable of Fl_Bitmap */
static float* cache_scale(Fl_Bitmap *bm) {return &(bm->cache_scale_);}
/** Accessor to a private member variable of Fl_RGB_Image */
static float* cache_scale(Fl_RGB_Image *rgb) {return &(rgb->cache_scale_);}
/** Accessor to private member variables of Fl_Pixmap */
static void cache_w_h(Fl_Pixmap *pm, int*& pwidth, int*& pheight) {
pwidth = &(pm->cache_w_);
pheight = &(pm->cache_h_);
}
/** Accessor to private member variables of Fl_Bitmap */
static void cache_w_h(Fl_Bitmap *bm, int*& pwidth, int*& pheight) {
pwidth = &(bm->cache_w_);
pheight = &(bm->cache_h_);
}
/** Accessor to private member variables of Fl_RGB_Image */
static void cache_w_h(Fl_RGB_Image *rgb, int*& pwidth, int*& pheight) {
pwidth = &(rgb->cache_w_);
pheight = &(rgb->cache_h_);
}
/** Accessor to a private member variable of Fl_Pixmap */
static Fl_Color* pixmap_bg_color(Fl_Pixmap *pm) {return &(pm->pixmap_bg_color);}
/** For internal library use only */

View File

@ -262,8 +262,8 @@ private:
// These two variables are used to cache the image and mask for the main display graphics driver
fl_uintptr_t id_;
fl_uintptr_t mask_;
float cache_scale_; // graphics scaling value when id_ was computed
int cache_w_, cache_h_; // size of image when cached
public:
Fl_RGB_Image(const uchar *bits, int W, int H, int D=3, int LD=0);

View File

@ -55,7 +55,7 @@ private:
fl_uintptr_t id_;
fl_uintptr_t mask_;
Fl_Color pixmap_bg_color;
float cache_scale_; // graphics scaling value when id_ was computed
int cache_w_, cache_h_; // size of pixmap when cached
public:

View File

@ -326,16 +326,18 @@ void Fl_Scalable_Graphics_Driver::draw(Fl_Pixmap *pxm, int XP, int YP, int WP, i
return;
}
// to allow rescale at runtime
if (*id(pxm) && *cache_scale(pxm) != scale_) {
int w2=pxm->w(), h2=pxm->h();
cache_size(pxm, w2, h2); // after this, w2 x h2 is size of desired cached image
int *pw, *ph;
cache_w_h(pxm, pw, ph); // after this, *pw x *ph is current size of cached form of bitmap
if (*id(pxm) && (*pw != w2 || *ph != h2)) {
pxm->uncache();
}
if (!*id(pxm)) {
int w2=pxm->w(), h2=pxm->h();
cache_size(pxm, w2, h2); // after this, w2 x h2 is size of desired cached image
if (pxm->data_w() != w2 || pxm->data_h() != h2) { // build a scaled id_ & pixmap_ for pxm
if (pxm->data_w() != w2 || pxm->data_h() != h2) { // build a scaled id_ & mask_ for pxm
Fl_Pixmap *pxm2 = (Fl_Pixmap*)pxm->copy(w2, h2);
*id(pxm) = cache(pxm2);
*cache_scale(pxm) = scale_;
*pw = w2; *ph = h2; // memorize size of cached form of pixmap
*mask(pxm) = *mask(pxm2);
*mask(pxm2) = 0;
delete pxm2;
@ -351,16 +353,18 @@ void Fl_Scalable_Graphics_Driver::draw(Fl_Bitmap *bm, int XP, int YP, int WP, in
if (Fl_Graphics_Driver::start_image(bm, XP, YP, WP, HP, cx, cy, X, Y, W, H)) {
return;
}
if (*id(bm) && *cache_scale(bm) != scale_) {
int w2 = bm->w(), h2 = bm->h();
cache_size(bm, w2, h2); // after this, w2 x h2 is size of desired cached image
int *pw, *ph;
cache_w_h(bm, pw, ph); // after this, *pw x *ph is current size of cached form of bitmap
if (*id(bm) && (*pw != w2 || *ph != h2)) {
bm->uncache();
}
if (!*id(bm)) {
int w2 = bm->w(), h2 = bm->h();
cache_size(bm, w2, h2); // after this, w2 x h2 is size of desired cached image
if (bm->data_w() != w2 || bm->data_h() != h2) { // build a scaled id_ for bm
Fl_Bitmap *bm2 = (Fl_Bitmap*)bm->copy(w2, h2);
*id(bm) = cache(bm2);
*cache_scale(bm) = scale_;
*pw = w2; *ph = h2; // memorize size of cached form of bitmap
delete bm2;
} else *id(bm) = cache(bm);
}
@ -387,12 +391,18 @@ void Fl_Scalable_Graphics_Driver::draw(Fl_RGB_Image *img, int XP, int YP, int WP
if (done) return;
}
// to allow rescale at runtime
if (*id(img) && *cache_scale(img) != scale_) {
int w2, h2, *pw, *ph;
if (need_scaled_drawing) {
w2 = img->w(); h2 = img->h();
cache_size(img, w2, h2);
} else {
w2 = img->data_w(); h2 = img->data_h();
} // after this, w2 x h2 is desired cached image size
cache_w_h(img, pw, ph); // after this, *pw x *ph is current size of cached image
if (*id(img) && (w2 != *pw || h2 != *ph )) {
img->uncache();
}
if (!*id(img) && need_scaled_drawing ) { // build and draw a scaled id_ for img
int w2=img->w(), h2=img->h();
cache_size(img, w2, h2);
if (!*id(img) && need_scaled_drawing) { // build and draw a scaled id_ for img
Fl_RGB_Scaling keep = Fl_Image::RGB_scaling();
Fl_Image::RGB_scaling(Fl_Image::scaling_algorithm());
Fl_RGB_Image *img2 = (Fl_RGB_Image*)img->copy(w2, h2);
@ -400,7 +410,8 @@ void Fl_Scalable_Graphics_Driver::draw(Fl_RGB_Image *img, int XP, int YP, int WP
draw_unscaled(img2, scale_, XP, YP, WP, HP, cx, cy);
*id(img) = *id(img2);
*id(img2) = 0;
*cache_scale(img) = scale_;
*pw = w2;
*ph = h2;
delete img2;
}
else { // draw img using its scaled id_

View File

@ -349,7 +349,7 @@ Fl_RGB_Image::Fl_RGB_Image(const uchar *bits, int W, int H, int D, int LD) :
alloc_array(0),
id_(0),
mask_(0),
cache_scale_(1)
cache_w_(0), cache_h_(0)
{
data((const char **)&array, 1);
ld(LD);
@ -372,7 +372,7 @@ Fl_RGB_Image::Fl_RGB_Image(const Fl_Pixmap *pxm, Fl_Color bg):
alloc_array(0),
id_(0),
mask_(0),
cache_scale_(1)
cache_w_(0), cache_h_(0)
{
if (pxm && pxm->w() > 0 && pxm->h() > 0) {
array = new uchar[w() * h() * d()];

View File

@ -42,7 +42,7 @@ void Fl_Pixmap::measure() {
if (w()<0 && data()) {
fl_measure_pixmap(data(), W, H);
w(W); h(H);
cache_scale_ = 1;
cache_w_ = cache_h_ = 0;
}
}

View File

@ -498,7 +498,10 @@ void Fl_GDI_Graphics_Driver::draw_unscaled(Fl_RGB_Image *img, float s, int X, in
if (H + cy > img->data_h()) H = img->data_h() - cy;
if (!*Fl_Graphics_Driver::id(img)) {
*Fl_Graphics_Driver::id(img) = (fl_uintptr_t)build_id(img, (void**)(Fl_Graphics_Driver::mask(img)));
*cache_scale(img) = 1;
int *pw, *ph;
cache_w_h(img, pw, ph);
*pw = img->data_w();
*ph = img->data_h();
}
Fl_Region r2 = scale_clip(s);
if (*Fl_Graphics_Driver::mask(img)) {
@ -541,7 +544,10 @@ int Fl_GDI_Graphics_Driver::draw_scaled(Fl_Image *img, int XP, int YP, int WP, i
if (!*Fl_Graphics_Driver::id(rgb)) {
*Fl_Graphics_Driver::id(rgb) = (fl_uintptr_t)build_id(rgb,
(void**)(Fl_Graphics_Driver::mask(rgb)));
*cache_scale(rgb) = 1;
int *pw, *ph;
cache_w_h(rgb, pw, ph);
*pw = rgb->data_w();
*ph = rgb->data_h();
}
cache_size(img, WP, HP);
HDC new_gc = CreateCompatibleDC(gc_);
@ -600,7 +606,10 @@ static Fl_Bitmask fl_create_bitmap(int w, int h, const uchar *data) {
}
fl_uintptr_t Fl_GDI_Graphics_Driver::cache(Fl_Bitmap *bm) {
*cache_scale(bm) = Fl_Scalable_Graphics_Driver::scale();
int *pw, *ph;
cache_w_h(bm, pw, ph);
*pw = bm->data_w();
*ph = bm->data_h();
return (fl_uintptr_t)fl_create_bitmap(bm->data_w(), bm->data_h(), bm->array);
}
@ -660,7 +669,10 @@ fl_uintptr_t Fl_GDI_Graphics_Driver::cache(Fl_Pixmap *img) {
Fl_Surface_Device::pop_current();
Fl_Offscreen id = surf->get_offscreen_before_delete();
delete surf;
*cache_scale(img) = 1;
int *pw, *ph;
cache_w_h(img, pw, ph);
*pw = img->data_w();
*ph = img->data_h();
return (fl_uintptr_t)id;
}

View File

@ -101,6 +101,7 @@ protected:
static Window draw_window;
static struct _XftDraw* draw_;
#endif
Fl_Offscreen cache_rgb(Fl_RGB_Image *img);
public:
Fl_Xlib_Graphics_Driver(void);
virtual ~Fl_Xlib_Graphics_Driver();

View File

@ -696,7 +696,7 @@ static void alpha_blend(Fl_RGB_Image *img, int X, int Y, int W, int H, int cx, i
delete[] dst;
}
static Fl_Offscreen cache_rgb(Fl_RGB_Image *img) {
Fl_Offscreen Fl_Xlib_Graphics_Driver::cache_rgb(Fl_RGB_Image *img) {
Fl_Image_Surface *surface;
int depth = img->d();
if (depth == 1 || depth == 3) {
@ -713,6 +713,10 @@ static Fl_Offscreen cache_rgb(Fl_RGB_Image *img) {
Fl_Surface_Device::pop_current();
Fl_Offscreen off = surface->get_offscreen_before_delete();
delete surface;
int *pw, *ph;
cache_w_h(img, pw, ph);
*pw = img->data_w();
*ph = img->data_h();
return off;
}
@ -728,7 +732,6 @@ void Fl_Xlib_Graphics_Driver::draw_unscaled(Fl_RGB_Image *img, float s, int X, i
if (H + cy > img->data_h()) H = img->data_h() - cy;
if (!*Fl_Graphics_Driver::id(img)) {
*Fl_Graphics_Driver::id(img) = cache_rgb(img);
*cache_scale(img) = 1;
}
Fl_Region r2 = scale_clip(s);
if (*Fl_Graphics_Driver::id(img)) {
@ -767,7 +770,10 @@ void Fl_Xlib_Graphics_Driver::uncache(Fl_RGB_Image*, fl_uintptr_t &id_, fl_uintp
}
fl_uintptr_t Fl_Xlib_Graphics_Driver::cache(Fl_Bitmap *bm) {
*cache_scale(bm) = Fl_Scalable_Graphics_Driver::scale();
int *pw, *ph;
cache_w_h(bm, pw, ph);
*pw = bm->data_w();
*ph = bm->data_h();
return (fl_uintptr_t)create_bitmask(bm->data_w(), bm->data_h(), bm->array);
}
@ -830,7 +836,10 @@ fl_uintptr_t Fl_Xlib_Graphics_Driver::cache(Fl_Pixmap *pxm) {
Fl_Surface_Device::pop_current();
Fl_Offscreen id = surf->get_offscreen_before_delete();
delete surf;
*cache_scale(pxm) = 1;
int *pw, *ph;
cache_w_h(pxm, pw, ph);
*pw = pxm->data_w();
*ph = pxm->data_h();
return (fl_uintptr_t)id;
}
@ -881,7 +890,6 @@ int Fl_Xlib_Graphics_Driver::draw_scaled(Fl_Image *img, int XP, int YP, int WP,
if (!rgb || !can_do_alpha_blending()) return 0;
if (!*Fl_Graphics_Driver::id(rgb)) {
*Fl_Graphics_Driver::id(rgb) = cache_rgb(rgb);
*cache_scale(rgb) = 1;
}
cache_size(img, WP, HP);
return scale_and_render_pixmap( *Fl_Graphics_Driver::id(rgb), rgb->d(),