diff options
| author | Manolo Gouy <Manolo> | 2015-03-10 21:06:22 +0000 |
|---|---|---|
| committer | Manolo Gouy <Manolo> | 2015-03-10 21:06:22 +0000 |
| commit | dbde470f8d9803d6986ff57b7dfcb40d679508b7 (patch) | |
| tree | 4d7fbeb02dcd207e96922506e6f0a71da796db45 /src | |
| parent | cc2436954d9c281b2a0589e5fc3fe89fa9d4aede (diff) | |
Added Fl_Shared_Image::scale(width, height) to support scaled image drawing (STR #3185).
Useful for printing, PostScript or PDF output, or retina displays.
git-svn-id: file:///fltk/svn/fltk/branches/branch-1.3@10615 ea41ed52-d2ee-0310-a9c1-e6b18d33e121
Diffstat (limited to 'src')
| -rw-r--r-- | src/Fl_Image.cxx | 80 | ||||
| -rw-r--r-- | src/Fl_Pixmap.cxx | 29 | ||||
| -rw-r--r-- | src/Fl_Shared_Image.cxx | 114 | ||||
| -rw-r--r-- | src/ps_image.cxx | 26 |
4 files changed, 228 insertions, 21 deletions
diff --git a/src/Fl_Image.cxx b/src/Fl_Image.cxx index 3df1efde4..e4e88478e 100644 --- a/src/Fl_Image.cxx +++ b/src/Fl_Image.cxx @@ -3,7 +3,7 @@ // // Image drawing code for the Fast Light Tool Kit (FLTK). // -// Copyright 1998-2014 by Bill Spitzak and others. +// Copyright 1998-2015 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 @@ -548,6 +548,21 @@ static int start(Fl_RGB_Image *img, int XP, int YP, int WP, int HP, int w, int h return 0; } +/** Draws an Fl_RGB_Image scaled to width \p W & height \p H with top-left corner at \em X,Y + \return zero when the graphics driver doesn't implement scaled drawing, non-zero if it does implement it. + */ +int Fl_Graphics_Driver::draw_scaled(Fl_RGB_Image *img, int X, int Y, int W, int H) { + return 0; +} + +/** Draws an Fl_Pixmap scaled to width \p W & height \p H with top-left corner at \em X,Y + \return zero when the graphics driver doesn't implement scaled drawing, non-zero if it does implement it. + */ +int Fl_Graphics_Driver::draw_scaled(Fl_Pixmap *img, int X, int Y, int W, int H) { + return 0; +} + + #ifdef __APPLE__ static void imgProviderReleaseData (void *info, const void *data, size_t size) { @@ -612,7 +627,40 @@ void Fl_Quartz_Graphics_Driver::draw(Fl_RGB_Image *img, int XP, int YP, int WP, } } +int Fl_Quartz_Graphics_Driver::draw_scaled(Fl_RGB_Image *img, int XP, int YP, int WP, int HP) { + int X, Y, W, H; + fl_clip_box(XP,YP,WP,HP,X,Y,W,H); // X,Y,W,H will give the unclipped area of XP,YP,WP,HP + if (W == 0 || H == 0) return 1; + fl_push_no_clip(); // remove the FLTK clip that can't be rescaled + CGContextSaveGState(fl_gc); + CGContextClipToRect(fl_gc, CGRectMake(X, Y, W, H)); // this clip path will be rescaled & translated + CGContextTranslateCTM(fl_gc, XP, YP); + CGContextScaleCTM(fl_gc, float(WP)/img->w(), float(HP)/img->h()); + draw(img, 0, 0, img->w(), img->h(), 0, 0); + CGContextRestoreGState(fl_gc); + fl_pop_clip(); // restore FLTK's clip + return 1; +} + #elif defined(WIN32) +static Fl_Offscreen build_id(Fl_RGB_Image *img, void **pmask) +{ + Fl_Offscreen offs = fl_create_offscreen(img->w(), img->h()); + if ((img->d() == 2 || img->d() == 4) && fl_can_do_alpha_blending()) { + fl_begin_offscreen(offs); + fl_draw_image(img->array, 0, 0, img->w(), img->h(), img->d()|FL_IMAGE_WITH_ALPHA, img->ld()); + fl_end_offscreen(); + } else { + fl_begin_offscreen(offs); + fl_draw_image(img->array, 0, 0, img->w(), img->h(), img->d(), img->ld()); + fl_end_offscreen(); + if (img->d() == 2 || img->d() == 4) { + *pmask = fl_create_alphamask(img->w(), img->h(), img->d(), img->ld(), img->array); + } + } + return offs; +} + void Fl_GDI_Graphics_Driver::draw(Fl_RGB_Image *img, int XP, int YP, int WP, int HP, int cx, int cy) { int X, Y, W, H; // Don't draw an empty image... @@ -623,21 +671,7 @@ void Fl_GDI_Graphics_Driver::draw(Fl_RGB_Image *img, int XP, int YP, int WP, int if (start(img, XP, YP, WP, HP, img->w(), img->h(), cx, cy, X, Y, W, H)) { return; } - if (!img->id_) { - img->id_ = fl_create_offscreen(img->w(), img->h()); - if ((img->d() == 2 || img->d() == 4) && fl_can_do_alpha_blending()) { - fl_begin_offscreen((Fl_Offscreen)img->id_); - fl_draw_image(img->array, 0, 0, img->w(), img->h(), img->d()|FL_IMAGE_WITH_ALPHA, img->ld()); - fl_end_offscreen(); - } else { - fl_begin_offscreen((Fl_Offscreen)img->id_); - fl_draw_image(img->array, 0, 0, img->w(), img->h(), img->d(), img->ld()); - fl_end_offscreen(); - if (img->d() == 2 || img->d() == 4) { - img->mask_ = fl_create_alphamask(img->w(), img->h(), img->d(), img->ld(), img->array); - } - } - } + if (!img->id_) img->id_ = build_id(img, &(img->mask_)); if (img->mask_) { HDC new_gc = CreateCompatibleDC(fl_gc); int save = SaveDC(new_gc); @@ -654,6 +688,20 @@ void Fl_GDI_Graphics_Driver::draw(Fl_RGB_Image *img, int XP, int YP, int WP, int } } +int Fl_GDI_Printer_Graphics_Driver::draw_scaled(Fl_RGB_Image *img, int XP, int YP, int WP, int HP) { + XFORM old_tr, tr; + GetWorldTransform(fl_gc, &old_tr); // storing old transform + tr.eM11 = float(WP)/float(img->w()); + tr.eM22 = float(HP)/float(img->h()); + tr.eM12 = tr.eM21 = 0; + tr.eDx = XP; + tr.eDy = YP; + ModifyWorldTransform(fl_gc, &tr, MWT_LEFTMULTIPLY); + Fl_GDI_Graphics_Driver::draw(img, 0, 0, img->w(), img->h(), 0, 0); + SetWorldTransform(fl_gc, &old_tr); +return 1; +} + #else void Fl_Xlib_Graphics_Driver::draw(Fl_RGB_Image *img, int XP, int YP, int WP, int HP, int cx, int cy) { int X, Y, W, H; diff --git a/src/Fl_Pixmap.cxx b/src/Fl_Pixmap.cxx index 8064979dd..2bb776b6b 100644 --- a/src/Fl_Pixmap.cxx +++ b/src/Fl_Pixmap.cxx @@ -135,6 +135,21 @@ void Fl_Quartz_Graphics_Driver::draw(Fl_Pixmap *pxm, int XP, int YP, int WP, int copy_offscreen(X, Y, W, H, (Fl_Offscreen)pxm->id_, cx, cy); } +int Fl_Quartz_Graphics_Driver::draw_scaled(Fl_Pixmap *img, int XP, int YP, int WP, int HP) { + int X, Y, W, H; + fl_clip_box(XP,YP,WP,HP,X,Y,W,H); // X,Y,W,H will give the unclipped area of XP,YP,WP,HP + if (W == 0 || H == 0) return 1; + fl_push_no_clip(); // remove the FLTK clip that can't be rescaled + CGContextSaveGState(fl_gc); + CGContextClipToRect(fl_gc, CGRectMake(X, Y, W, H)); // this clip path will be rescaled & translated + CGContextTranslateCTM(fl_gc, XP, YP); + CGContextScaleCTM(fl_gc, float(WP)/img->w(), float(HP)/img->h()); + draw(img, 0, 0, img->w(), img->h(), 0, 0); + CGContextRestoreGState(fl_gc); + fl_pop_clip(); // restore FLTK's clip + return 1; +} + #elif defined(WIN32) void Fl_GDI_Graphics_Driver::draw(Fl_Pixmap *pxm, int XP, int YP, int WP, int HP, int cx, int cy) { int X, Y, W, H; @@ -181,6 +196,20 @@ void Fl_GDI_Printer_Graphics_Driver::draw(Fl_Pixmap *pxm, int XP, int YP, int WP } } +int Fl_GDI_Printer_Graphics_Driver::draw_scaled(Fl_Pixmap *img, int XP, int YP, int WP, int HP) { + XFORM old_tr, tr; + GetWorldTransform(fl_gc, &old_tr); // storing old transform + tr.eM11 = float(WP)/float(img->w()); + tr.eM22 = float(HP)/float(img->h()); + tr.eM12 = tr.eM21 = 0; + tr.eDx = XP; + tr.eDy = YP; + ModifyWorldTransform(fl_gc, &tr, MWT_LEFTMULTIPLY); + draw(img, 0, 0, img->w(), img->h(), 0, 0); + SetWorldTransform(fl_gc, &old_tr); +} + + #else // Xlib void Fl_Xlib_Graphics_Driver::draw(Fl_Pixmap *pxm, int XP, int YP, int WP, int HP, int cx, int cy) { int X, Y, W, H; diff --git a/src/Fl_Shared_Image.cxx b/src/Fl_Shared_Image.cxx index 2289ccdb4..4a05a46ef 100644 --- a/src/Fl_Shared_Image.cxx +++ b/src/Fl_Shared_Image.cxx @@ -3,7 +3,7 @@ // // Shared image code for the Fast Light Tool Kit (FLTK). // -// Copyright 1998-2010 by Bill Spitzak and others. +// Copyright 1998-2015 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 @@ -25,7 +25,8 @@ #include <FL/Fl_Shared_Image.H> #include <FL/Fl_XBM_Image.H> #include <FL/Fl_XPM_Image.H> - +#include <FL/Fl_Preferences.H> +#include <FL/fl_draw.H> // // Global class vars... @@ -89,6 +90,9 @@ Fl_Shared_Image::Fl_Shared_Image() : Fl_Image(0,0,0) { original_ = 0; image_ = 0; alloc_image_ = 0; +#if FLTK_ABI_VERSION >= 10304 + scaled_image_= 0; +#endif } @@ -109,6 +113,9 @@ Fl_Shared_Image::Fl_Shared_Image(const char *n, // I - Filename image_ = img; alloc_image_ = !img; original_ = 1; +#if FLTK_ABI_VERSION >= 10304 + scaled_image_= 0; +#endif if (!img) reload(); else update(); @@ -170,6 +177,9 @@ Fl_Shared_Image::update() { Fl_Shared_Image::~Fl_Shared_Image() { if (name_) delete[] (char *)name_; if (alloc_image_) delete image_; +#if FLTK_ABI_VERSION >= 10304 + delete scaled_image_; +#endif } @@ -317,13 +327,95 @@ Fl_Shared_Image::desaturate() { // // 'Fl_Shared_Image::draw()' - Draw a shared image... // - -void -Fl_Shared_Image::draw(int X, int Y, int W, int H, int cx, int cy) { +void Fl_Shared_Image::draw(int X, int Y, int W, int H, int cx, int cy) { +#if FLTK_ABI_VERSION >= 10304 + if (!image_) { + Fl_Image::draw(X, Y, W, H, cx, cy); + return; + } + if (w() == image_->w() && h() == image_->h()) { + image_->draw(X, Y, W, H, cx, cy); + return; + } + fl_push_clip(X, Y, W, H); + int use_scaled_image = 0, done; + if (image_->d() == 0) { // handles Fl_Bitmap + use_scaled_image = 1; + } + else if (image_->count() >= 2) { // handles Fl_Pixmap + done = fl_graphics_driver->draw_scaled((Fl_Pixmap*)image_, X-cx, Y-cy, w(), h()); + if (done == 0) use_scaled_image = 1; + } + else { // handles Fl_RGB_Image + done = fl_graphics_driver->draw_scaled((Fl_RGB_Image*)image_, X-cx, Y-cy, w(), h()); + if (done == 0) use_scaled_image = 1; + } + if (use_scaled_image) { + if (scaled_image_ && (scaled_image_->w() != w() || scaled_image_->h() != h())) { + delete scaled_image_; + scaled_image_ = NULL; + } + if (!scaled_image_) { + Fl_RGB_Scaling previous = RGB_scaling(); + RGB_scaling(scaling_algorithm_); // useless but no harm if image_ is not an Fl_RGB_Image + scaled_image_ = image_->copy(w(), h()); + RGB_scaling(previous); + } + scaled_image_->draw(X-cx, Y-cy, scaled_image_->w(), scaled_image_->h(), 0, 0); + } + fl_pop_clip(); +#else if (image_) image_->draw(X, Y, W, H, cx, cy); else Fl_Image::draw(X, Y, W, H, cx, cy); +#endif // FLTK_ABI_VERSION } +/** Sets the drawing size of the shared image. + This function gives the shared image its own size, independently from the size of the original image + that is typically larger than the shared image. + This can be useful to draw a shared image on a drawing surface whose resolution is higher + than the drawing unit for this surface. Examples of such drawing surfaces: laser printers, + PostScript files, PDF printers, retina displays on Apple hardware. + + \param width,height maximum width and height (in drawing units) to use when drawing the shared image + \param proportional if not null, keep the width and height of the shared image proportional to those of its original image + \param can_expand if null, the width and height of the shared image will not exceed those of the original image + + \version 1.3.4 and requires compiling with FLTK_ABI_VERSION = 10304 + + Example code: scale an image to fit in a box + \code + Fl_Box *b = ... // a box + Fl_Shared_Image *shared = Fl_Shared_Image::get("/path/to/picture.jpeg"); // read a picture file + b->image(shared); // use the shared image as the box image + b->align(FL_ALIGN_INSIDE | FL_ALIGN_CENTER | FL_ALIGN_CLIP); // the image is to be drawn centered in the box + shared->scale(b->w(), b->h(), 1); // set the drawing size of the shared image to the size of the box + \endcode + */ +void Fl_Shared_Image::scale(int width, int height, int proportional, int can_expand) +{ +#if FLTK_ABI_VERSION >= 10304 + w(width); + h(height); + if (!image_) return; + float fw = image_->w() / float(width); + float fh = image_->h() / float(height); + if (proportional) { + if (fh > fw) fw = fh; + else fh = fw; + } + if (!can_expand) { + if (fw < 1) fw = 1; + if (fh < 1) fh = 1; + } + w(image_->w() / fw); + h(image_->h() / fh); +#endif +} + + +Fl_RGB_Scaling Fl_Shared_Image::scaling_algorithm_ = FL_RGB_SCALING_BILINEAR; + // // 'Fl_Shared_Image::uncache()' - Uncache the shared image... @@ -408,6 +500,18 @@ Fl_Shared_Image* Fl_Shared_Image::get(const char *n, int W, int H) { return temp; } +/** Builds a shared image from a pre-existing Fl_RGB_Image + \param rgb an Fl_RGB_Image used to build a new shared image. + \param own_it 1 if the shared image should delete \p rgb when it is itself deleted, 0 otherwise + \version 1.3.4 + */ +Fl_Shared_Image *Fl_Shared_Image::get(Fl_RGB_Image *rgb, int own_it) +{ + Fl_Shared_Image *shared = new Fl_Shared_Image(Fl_Preferences::newUUID(), rgb); + shared->alloc_image_ = own_it; + shared->add(); + return shared; +} /** Adds a shared image handler, which is basically a test function for adding new formats */ diff --git a/src/ps_image.cxx b/src/ps_image.cxx index 3939a7511..ca71121b9 100644 --- a/src/ps_image.cxx +++ b/src/ps_image.cxx @@ -602,6 +602,32 @@ void Fl_PostScript_Graphics_Driver::draw(Fl_RGB_Image * rgb,int XP, int YP, int mask=0; } +int Fl_PostScript_Graphics_Driver::draw_scaled(Fl_RGB_Image *rgb, int XP, int YP, int WP, int HP){ + int X, Y, W, H; + clip_box(XP,YP,WP,HP,X,Y,W,H); // X,Y,W,H will give the unclipped area of XP,YP,WP,HP + if (W == 0 || H == 0) return 1; + push_no_clip(); // remove the FLTK clip that can't be rescaled + clocale_printf("%d %d %i %i CL\n", X, Y, W, H); + clocale_printf("GS %d %d TR %f %f SC GS\n", XP, YP, float(WP)/rgb->w(), float(HP)/rgb->h()); + draw(rgb, 0, 0, rgb->w(), rgb->h(), 0, 0); + clocale_printf("GR GR\n"); + pop_clip(); // restore FLTK's clip + return 1; +} + +int Fl_PostScript_Graphics_Driver::draw_scaled(Fl_Pixmap *img, int XP, int YP, int WP, int HP){ + int X, Y, W, H; + clip_box(XP,YP,WP,HP,X,Y,W,H); // X,Y,W,H will give the unclipped area of XP,YP,WP,HP + if (W == 0 || H == 0) return 1; + push_no_clip(); // remove the FLTK clip that can't be rescaled + clocale_printf("%d %d %i %i CL\n", X, Y, W, H); + clocale_printf("GS %d %d TR %f %f SC GS\n", XP, YP, float(WP)/img->w(), float(HP)/img->h()); + draw(img, 0, 0, img->w(), img->h(), 0, 0); + clocale_printf("GR GR\n"); + pop_clip(); // restore FLTK's clip + return 1; +} + void Fl_PostScript_Graphics_Driver::draw(Fl_Bitmap * bitmap,int XP, int YP, int WP, int HP, int cx, int cy){ const uchar * di = bitmap->array; int w,h; |
