Skip to content

Fl_Image_UserScale for Windows GDI

Raphael Kim edited this page Jul 18, 2023 · 8 revisions

What is Fl_Image_UserScale ?

  • Windows GDI implementation is not supporting RGB/RGBA scaling method by Fl_Image::scaling_algorithm( FL_RGB_SCALING_BILINEAR );.
  • fltk-custom supports additional callback based user scaling by Fl_Image:: scaling_algorithm( FL_RGB_SCALING_USER, usr_scale );

Required FLTK_EXT_VERSION

  • Minimal 10.

Fl_Image_UserScale callback

typedef void (Fl_Image_UserScale)(Fl_RGB_Image*, int, int, int, int, Fl_RGB_Image**);
typedef Fl_Image_UserScale* Fl_Image_UserScale_p; // needed for BORLAND

Fl_Image::scaling_algorithm()

static void scaling_algorithm(Fl_RGB_Scaling algorithm) {scaling_algorithm_ = algorithm; }

Usage on user application,

Implement callback

// This example is using libfl_imgtk, fastest image processing library.
// you can clone it from https://github.com/rageworx/fl_imgtk
#include <fl_imgtk.h>

// This is user registered image scaling callback.
// Fl_RGB_Image** o need to be allocated by user space.
// And it will discarded in Fl_Image.
// User only need to resize by w(idth) and h(eight) from source image of 's'.
// And `o` need to removed by user side when `s==NULL` and x,y,w,h == 0 but `*o` is not NULL.
void usr_scale(Fl_RGB_Image* s, int x, int y, int w, int h, Fl_RGB_Image** o)
{
    Fl::lock();
    // Using fl_imgtk fast rescaling method here.
    if ( ( s != NULL ) & ( w > 0 ) & ( h > 0 ) )
    {
        if ( s->array != NULL )
        {
            // To rescale in safe way, some Fl_RGB_Image like from SVG, need to
            // check with data_w() and data_h().
            Fl_RGB_Image* tmpi = (Fl_RGB_Image*)s->copy( s->data_w(), s->data_h() );
            if ( tmpi != NULL )
            {
                *o = fl_imgtk::rescale( tmpi, w, h, fl_imgtk::BICUBIC );
            }
            delete tmpi;
        }
    }
    else
    // It must be implemented for removing image.
    if ( ( *o != NULL ) & ( x == 0 ) & ( y == 0 ) & ( w == 0 ) & ( h == 0 ) )
    {
        delete (Fl_RGB_Image*)*o;
        *o = NULL;
    }
    Fl::unlock();
}

int main( ... )
{
    ... 
#ifdef FLTK_EXT_VERSION
    Fl::scheme( "flat" );
#endif /// of FLTK_EXT_VERSION

#if(FLTK_EXT_VERSION>=10)
    // User can register user scaling callback with FL_RGB_SCALING_USER.    
    Fl_Image:: scaling_algorithm( FL_RGB_SCALING_USER, usr_scale );
#endif /// of FLTK_EXT_VERSION

    ...
    return 0;
}