summaryrefslogtreecommitdiff
path: root/src/Fl_Image.cxx
diff options
context:
space:
mode:
authorManolo Gouy <Manolo>2015-03-10 21:06:22 +0000
committerManolo Gouy <Manolo>2015-03-10 21:06:22 +0000
commitdbde470f8d9803d6986ff57b7dfcb40d679508b7 (patch)
tree4d7fbeb02dcd207e96922506e6f0a71da796db45 /src/Fl_Image.cxx
parentcc2436954d9c281b2a0589e5fc3fe89fa9d4aede (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/Fl_Image.cxx')
-rw-r--r--src/Fl_Image.cxx80
1 files changed, 64 insertions, 16 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;