1998-10-20 00:46:58 +04:00
|
|
|
//
|
|
|
|
// Bitmap drawing routines for the Fast Light Tool Kit (FLTK).
|
|
|
|
//
|
2022-04-12 16:06:21 +03:00
|
|
|
// Copyright 1998-2022 by Bill Spitzak and others.
|
1998-10-20 00:46:58 +04:00
|
|
|
//
|
2011-07-19 08:49:30 +04:00
|
|
|
// 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
|
|
|
|
// file is missing or damaged, see the license at:
|
|
|
|
//
|
2020-07-01 19:03:10 +03:00
|
|
|
// https://www.fltk.org/COPYING.php
|
1998-10-20 00:46:58 +04:00
|
|
|
//
|
2020-07-01 19:03:10 +03:00
|
|
|
// Please see the following page on how to report bugs and issues:
|
2005-04-16 04:13:17 +04:00
|
|
|
//
|
2020-07-01 19:03:10 +03:00
|
|
|
// https://www.fltk.org/bugs.php
|
1998-10-20 00:46:58 +04:00
|
|
|
//
|
1998-10-06 22:21:25 +04:00
|
|
|
|
2008-09-14 22:19:41 +04:00
|
|
|
/** \fn Fl_Bitmap::Fl_Bitmap(const char *array, int W, int H)
|
|
|
|
The constructors create a new bitmap from the specified bitmap data.*/
|
|
|
|
|
|
|
|
/** \fn Fl_Bitmap::Fl_Bitmap(const unsigned char *array, int W, int H)
|
|
|
|
The constructors create a new bitmap from the specified bitmap data.*/
|
|
|
|
|
1998-10-06 22:21:25 +04:00
|
|
|
#include <FL/Fl.H>
|
|
|
|
#include <FL/fl_draw.H>
|
|
|
|
#include <FL/Fl_Widget.H>
|
|
|
|
#include <FL/Fl_Menu_Item.H>
|
|
|
|
#include <FL/Fl_Bitmap.H>
|
|
|
|
|
2002-04-15 01:26:06 +04:00
|
|
|
|
1998-10-15 18:06:16 +04:00
|
|
|
void Fl_Bitmap::draw(int XP, int YP, int WP, int HP, int cx, int cy) {
|
Image drawing: simplify the code organisation to better support Fl_Image::scale().
Graphics drivers now use up to 6 virtual member functions to support Fl_Image
drawing in the context of GUI and image rescaling :
virtual void draw_pixmap(Fl_Pixmap *pxm, int XP, int YP, int WP, int HP, int cx, int cy)
virtual void draw_bitmap(Fl_Bitmap *bm, int XP, int YP, int WP, int HP, int cx, int cy)
virtual void draw_rgb(Fl_RGB_Image *rgb, int XP, int YP, int WP, int HP, int cx, int cy)
and
virtual void draw_fixed(Fl_Pixmap *pxm, int XP, int YP, int WP, int HP, int cx, int cy)
virtual void draw_fixed(Fl_Bitmap *bm, int XP, int YP, int WP, int HP, int cx, int cy)
virtual void draw_fixed(Fl_RGB_Image *rgb, int XP, int YP, int WP, int HP, int cx, int cy)
git-svn-id: file:///fltk/svn/fltk/branches/branch-1.4@12828 ea41ed52-d2ee-0310-a9c1-e6b18d33e121
2018-04-12 16:07:00 +03:00
|
|
|
fl_graphics_driver->draw_bitmap(this, XP, YP, WP, HP, cx, cy);
|
2010-04-14 17:07:34 +04:00
|
|
|
}
|
|
|
|
|
2008-09-14 22:19:41 +04:00
|
|
|
/**
|
2016-10-14 17:33:18 +03:00
|
|
|
The destructor frees all memory and server resources that are used by
|
2008-09-14 22:19:41 +04:00
|
|
|
the bitmap.
|
|
|
|
*/
|
1998-10-06 22:21:25 +04:00
|
|
|
Fl_Bitmap::~Fl_Bitmap() {
|
2002-08-05 21:50:25 +04:00
|
|
|
uncache();
|
2001-11-19 23:59:59 +03:00
|
|
|
if (alloc_array) delete[] (uchar *)array;
|
1998-10-06 22:21:25 +04:00
|
|
|
}
|
|
|
|
|
2002-08-05 21:50:25 +04:00
|
|
|
void Fl_Bitmap::uncache() {
|
2010-03-16 02:47:47 +03:00
|
|
|
if (id_) {
|
2022-03-24 12:00:01 +03:00
|
|
|
fl_graphics_driver->delete_bitmask(id_);
|
2010-03-16 02:47:47 +03:00
|
|
|
id_ = 0;
|
2002-08-05 21:50:25 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-08-09 05:09:49 +04:00
|
|
|
void Fl_Bitmap::label(Fl_Widget* widget) {
|
|
|
|
widget->image(this);
|
1998-10-06 22:21:25 +04:00
|
|
|
}
|
|
|
|
|
2001-08-06 03:58:54 +04:00
|
|
|
void Fl_Bitmap::label(Fl_Menu_Item* m) {
|
2017-09-12 16:05:48 +03:00
|
|
|
m->label(FL_IMAGE_LABEL, (const char*)this);
|
1998-10-06 22:21:25 +04:00
|
|
|
}
|
1998-10-20 00:46:58 +04:00
|
|
|
|
2022-04-14 12:07:12 +03:00
|
|
|
Fl_Image *Fl_Bitmap::copy(int W, int H) const {
|
2020-07-01 19:03:10 +03:00
|
|
|
Fl_Bitmap *new_image; // New RGB image
|
|
|
|
uchar *new_array; // New array for image data
|
2004-09-24 20:00:11 +04:00
|
|
|
|
OK, lots of changes to the Fl_Image, Fl_Bitmap, Fl_Pixmap, and Fl_RGB_Image
classes: new copy(), copy(w,h), desaturate(), color_average(), and
inactive() methods, alloc_xyz member for copied data, etc.
Updated test programs to use inactive() and copy() methods to create
inactive images for the test buttons, plus the inactive button to toggle
it...
Added start of separate image classes, a la 2.0, for various image formats.
FLUID will also be updated for it...
git-svn-id: file:///fltk/svn/fltk/branches/branch-1.1@1703 ea41ed52-d2ee-0310-a9c1-e6b18d33e121
2001-11-19 04:06:45 +03:00
|
|
|
// Optimize the simple copy where the width and height are the same...
|
2022-04-13 10:30:31 +03:00
|
|
|
if (W == data_w() && H == data_h()) {
|
|
|
|
new_array = new uchar [H * ((W + 7) / 8)];
|
|
|
|
memcpy(new_array, array, H * ((W + 7) / 8));
|
2004-09-24 20:00:11 +04:00
|
|
|
|
2022-04-13 10:30:31 +03:00
|
|
|
new_image = new Fl_Bitmap(new_array, W, H);
|
2004-09-24 20:00:11 +04:00
|
|
|
new_image->alloc_array = 1;
|
|
|
|
return new_image;
|
|
|
|
}
|
2002-10-11 17:54:10 +04:00
|
|
|
if (W <= 0 || H <= 0) return 0;
|
OK, lots of changes to the Fl_Image, Fl_Bitmap, Fl_Pixmap, and Fl_RGB_Image
classes: new copy(), copy(w,h), desaturate(), color_average(), and
inactive() methods, alloc_xyz member for copied data, etc.
Updated test programs to use inactive() and copy() methods to create
inactive images for the test buttons, plus the inactive button to toggle
it...
Added start of separate image classes, a la 2.0, for various image formats.
FLUID will also be updated for it...
git-svn-id: file:///fltk/svn/fltk/branches/branch-1.1@1703 ea41ed52-d2ee-0310-a9c1-e6b18d33e121
2001-11-19 04:06:45 +03:00
|
|
|
|
2020-07-01 19:03:10 +03:00
|
|
|
// OK, need to resize the image data; allocate memory and
|
|
|
|
uchar *new_ptr, // Pointer into new array
|
|
|
|
new_bit, // Bit for new array
|
|
|
|
old_bit; // Bit for old array
|
|
|
|
const uchar *old_ptr; // Pointer into old array
|
|
|
|
int sx, sy, // Source coordinates
|
|
|
|
dx, dy, // Destination coordinates
|
|
|
|
xerr, yerr, // X & Y errors
|
|
|
|
xmod, ymod, // X & Y moduli
|
|
|
|
xstep, ystep; // X & Y step increments
|
OK, lots of changes to the Fl_Image, Fl_Bitmap, Fl_Pixmap, and Fl_RGB_Image
classes: new copy(), copy(w,h), desaturate(), color_average(), and
inactive() methods, alloc_xyz member for copied data, etc.
Updated test programs to use inactive() and copy() methods to create
inactive images for the test buttons, plus the inactive button to toggle
it...
Added start of separate image classes, a la 2.0, for various image formats.
FLUID will also be updated for it...
git-svn-id: file:///fltk/svn/fltk/branches/branch-1.1@1703 ea41ed52-d2ee-0310-a9c1-e6b18d33e121
2001-11-19 04:06:45 +03:00
|
|
|
|
|
|
|
|
2016-08-09 18:20:54 +03:00
|
|
|
// Figure out Bresenham step/modulus values...
|
2018-03-22 19:38:38 +03:00
|
|
|
xmod = data_w() % W;
|
|
|
|
xstep = data_w() / W;
|
|
|
|
ymod = data_h() % H;
|
|
|
|
ystep = data_h() / H;
|
OK, lots of changes to the Fl_Image, Fl_Bitmap, Fl_Pixmap, and Fl_RGB_Image
classes: new copy(), copy(w,h), desaturate(), color_average(), and
inactive() methods, alloc_xyz member for copied data, etc.
Updated test programs to use inactive() and copy() methods to create
inactive images for the test buttons, plus the inactive button to toggle
it...
Added start of separate image classes, a la 2.0, for various image formats.
FLUID will also be updated for it...
git-svn-id: file:///fltk/svn/fltk/branches/branch-1.1@1703 ea41ed52-d2ee-0310-a9c1-e6b18d33e121
2001-11-19 04:06:45 +03:00
|
|
|
|
|
|
|
// Allocate memory for the new image...
|
2004-09-24 20:00:11 +04:00
|
|
|
new_array = new uchar [H * ((W + 7) / 8)];
|
OK, lots of changes to the Fl_Image, Fl_Bitmap, Fl_Pixmap, and Fl_RGB_Image
classes: new copy(), copy(w,h), desaturate(), color_average(), and
inactive() methods, alloc_xyz member for copied data, etc.
Updated test programs to use inactive() and copy() methods to create
inactive images for the test buttons, plus the inactive button to toggle
it...
Added start of separate image classes, a la 2.0, for various image formats.
FLUID will also be updated for it...
git-svn-id: file:///fltk/svn/fltk/branches/branch-1.1@1703 ea41ed52-d2ee-0310-a9c1-e6b18d33e121
2001-11-19 04:06:45 +03:00
|
|
|
new_image = new Fl_Bitmap(new_array, W, H);
|
|
|
|
new_image->alloc_array = 1;
|
|
|
|
|
2004-09-24 20:00:11 +04:00
|
|
|
memset(new_array, 0, H * ((W + 7) / 8));
|
OK, lots of changes to the Fl_Image, Fl_Bitmap, Fl_Pixmap, and Fl_RGB_Image
classes: new copy(), copy(w,h), desaturate(), color_average(), and
inactive() methods, alloc_xyz member for copied data, etc.
Updated test programs to use inactive() and copy() methods to create
inactive images for the test buttons, plus the inactive button to toggle
it...
Added start of separate image classes, a la 2.0, for various image formats.
FLUID will also be updated for it...
git-svn-id: file:///fltk/svn/fltk/branches/branch-1.1@1703 ea41ed52-d2ee-0310-a9c1-e6b18d33e121
2001-11-19 04:06:45 +03:00
|
|
|
|
|
|
|
// Scale the image using a nearest-neighbor algorithm...
|
2002-12-20 00:34:26 +03:00
|
|
|
for (dy = H, sy = 0, yerr = H, new_ptr = new_array; dy > 0; dy --) {
|
2018-03-22 19:38:38 +03:00
|
|
|
for (dx = W, xerr = W, old_ptr = array + sy * ((data_w() + 7) / 8), sx = 0, new_bit = 1;
|
2020-07-01 19:03:10 +03:00
|
|
|
dx > 0;
|
|
|
|
dx --) {
|
2005-07-14 17:16:17 +04:00
|
|
|
old_bit = (uchar)(1 << (sx & 7));
|
OK, lots of changes to the Fl_Image, Fl_Bitmap, Fl_Pixmap, and Fl_RGB_Image
classes: new copy(), copy(w,h), desaturate(), color_average(), and
inactive() methods, alloc_xyz member for copied data, etc.
Updated test programs to use inactive() and copy() methods to create
inactive images for the test buttons, plus the inactive button to toggle
it...
Added start of separate image classes, a la 2.0, for various image formats.
FLUID will also be updated for it...
git-svn-id: file:///fltk/svn/fltk/branches/branch-1.1@1703 ea41ed52-d2ee-0310-a9c1-e6b18d33e121
2001-11-19 04:06:45 +03:00
|
|
|
if (old_ptr[sx / 8] & old_bit) *new_ptr |= new_bit;
|
|
|
|
|
2005-07-14 17:16:17 +04:00
|
|
|
if (new_bit < 128) new_bit <<= 1;
|
OK, lots of changes to the Fl_Image, Fl_Bitmap, Fl_Pixmap, and Fl_RGB_Image
classes: new copy(), copy(w,h), desaturate(), color_average(), and
inactive() methods, alloc_xyz member for copied data, etc.
Updated test programs to use inactive() and copy() methods to create
inactive images for the test buttons, plus the inactive button to toggle
it...
Added start of separate image classes, a la 2.0, for various image formats.
FLUID will also be updated for it...
git-svn-id: file:///fltk/svn/fltk/branches/branch-1.1@1703 ea41ed52-d2ee-0310-a9c1-e6b18d33e121
2001-11-19 04:06:45 +03:00
|
|
|
else {
|
2005-07-14 17:16:17 +04:00
|
|
|
new_bit = 1;
|
2020-07-01 19:03:10 +03:00
|
|
|
new_ptr ++;
|
OK, lots of changes to the Fl_Image, Fl_Bitmap, Fl_Pixmap, and Fl_RGB_Image
classes: new copy(), copy(w,h), desaturate(), color_average(), and
inactive() methods, alloc_xyz member for copied data, etc.
Updated test programs to use inactive() and copy() methods to create
inactive images for the test buttons, plus the inactive button to toggle
it...
Added start of separate image classes, a la 2.0, for various image formats.
FLUID will also be updated for it...
git-svn-id: file:///fltk/svn/fltk/branches/branch-1.1@1703 ea41ed52-d2ee-0310-a9c1-e6b18d33e121
2001-11-19 04:06:45 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
sx += xstep;
|
|
|
|
xerr -= xmod;
|
|
|
|
|
|
|
|
if (xerr <= 0) {
|
2020-07-01 19:03:10 +03:00
|
|
|
xerr += W;
|
|
|
|
sx ++;
|
OK, lots of changes to the Fl_Image, Fl_Bitmap, Fl_Pixmap, and Fl_RGB_Image
classes: new copy(), copy(w,h), desaturate(), color_average(), and
inactive() methods, alloc_xyz member for copied data, etc.
Updated test programs to use inactive() and copy() methods to create
inactive images for the test buttons, plus the inactive button to toggle
it...
Added start of separate image classes, a la 2.0, for various image formats.
FLUID will also be updated for it...
git-svn-id: file:///fltk/svn/fltk/branches/branch-1.1@1703 ea41ed52-d2ee-0310-a9c1-e6b18d33e121
2001-11-19 04:06:45 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-07-14 17:16:17 +04:00
|
|
|
if (new_bit > 1) new_ptr ++;
|
OK, lots of changes to the Fl_Image, Fl_Bitmap, Fl_Pixmap, and Fl_RGB_Image
classes: new copy(), copy(w,h), desaturate(), color_average(), and
inactive() methods, alloc_xyz member for copied data, etc.
Updated test programs to use inactive() and copy() methods to create
inactive images for the test buttons, plus the inactive button to toggle
it...
Added start of separate image classes, a la 2.0, for various image formats.
FLUID will also be updated for it...
git-svn-id: file:///fltk/svn/fltk/branches/branch-1.1@1703 ea41ed52-d2ee-0310-a9c1-e6b18d33e121
2001-11-19 04:06:45 +03:00
|
|
|
|
|
|
|
sy += ystep;
|
|
|
|
yerr -= ymod;
|
|
|
|
if (yerr <= 0) {
|
|
|
|
yerr += H;
|
|
|
|
sy ++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return new_image;
|
|
|
|
}
|