1998-10-19 20:46:58 +00:00
|
|
|
//
|
|
|
|
|
// Bitmap drawing routines for the Fast Light Tool Kit (FLTK).
|
|
|
|
|
//
|
2016-10-14 14:33:18 +00:00
|
|
|
// Copyright 1998-2016 by Bill Spitzak and others.
|
1998-10-19 20:46:58 +00:00
|
|
|
//
|
2011-07-19 04:49:30 +00: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 16:03:10 +00:00
|
|
|
// https://www.fltk.org/COPYING.php
|
1998-10-19 20:46:58 +00:00
|
|
|
//
|
2020-07-01 16:03:10 +00:00
|
|
|
// Please see the following page on how to report bugs and issues:
|
2005-04-16 00:13:17 +00:00
|
|
|
//
|
2020-07-01 16:03:10 +00:00
|
|
|
// https://www.fltk.org/bugs.php
|
1998-10-19 20:46:58 +00:00
|
|
|
//
|
1998-10-06 18:21:25 +00:00
|
|
|
|
2008-09-14 18:19:41 +00: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 18:21:25 +00: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>
|
|
|
|
|
|
2017-05-02 12:44:54 +00:00
|
|
|
/** Create a bit mask */
|
2004-08-25 00:20:27 +00:00
|
|
|
Fl_Bitmask fl_create_bitmask(int w, int h, const uchar *array) {
|
2016-01-31 13:51:01 +00:00
|
|
|
return fl_graphics_driver->create_bitmask(w, h, array);
|
2001-11-18 20:52:28 +00:00
|
|
|
}
|
|
|
|
|
|
2017-05-02 12:44:54 +00:00
|
|
|
/** delete a bit mask */
|
2001-11-18 20:52:28 +00:00
|
|
|
void fl_delete_bitmask(Fl_Bitmask bm) {
|
2016-11-09 09:49:48 +00:00
|
|
|
return Fl_Graphics_Driver::default_driver().delete_bitmask(bm);
|
2001-11-18 20:52:28 +00:00
|
|
|
}
|
2010-03-29 10:35:00 +00:00
|
|
|
|
2002-04-15 12:19:01 +00:00
|
|
|
// Create a 1-bit mask used for alpha blending
|
|
|
|
|
Fl_Bitmask fl_create_alphamask(int w, int h, int d, int ld, const uchar *array) {
|
2010-03-15 23:47:47 +00:00
|
|
|
Fl_Bitmask bm;
|
2002-04-14 21:26:06 +00:00
|
|
|
int bmw = (w + 7) / 8;
|
|
|
|
|
uchar *bitmap = new uchar[bmw * h];
|
|
|
|
|
uchar *bitptr, bit;
|
|
|
|
|
const uchar *dataptr;
|
|
|
|
|
int x, y;
|
|
|
|
|
static uchar dither[16][16] = { // Simple 16x16 Floyd dither
|
|
|
|
|
{ 0, 128, 32, 160, 8, 136, 40, 168,
|
|
|
|
|
2, 130, 34, 162, 10, 138, 42, 170 },
|
|
|
|
|
{ 192, 64, 224, 96, 200, 72, 232, 104,
|
|
|
|
|
194, 66, 226, 98, 202, 74, 234, 106 },
|
|
|
|
|
{ 48, 176, 16, 144, 56, 184, 24, 152,
|
|
|
|
|
50, 178, 18, 146, 58, 186, 26, 154 },
|
|
|
|
|
{ 240, 112, 208, 80, 248, 120, 216, 88,
|
|
|
|
|
242, 114, 210, 82, 250, 122, 218, 90 },
|
|
|
|
|
{ 12, 140, 44, 172, 4, 132, 36, 164,
|
|
|
|
|
14, 142, 46, 174, 6, 134, 38, 166 },
|
|
|
|
|
{ 204, 76, 236, 108, 196, 68, 228, 100,
|
|
|
|
|
206, 78, 238, 110, 198, 70, 230, 102 },
|
|
|
|
|
{ 60, 188, 28, 156, 52, 180, 20, 148,
|
|
|
|
|
62, 190, 30, 158, 54, 182, 22, 150 },
|
|
|
|
|
{ 252, 124, 220, 92, 244, 116, 212, 84,
|
|
|
|
|
254, 126, 222, 94, 246, 118, 214, 86 },
|
|
|
|
|
{ 3, 131, 35, 163, 11, 139, 43, 171,
|
|
|
|
|
1, 129, 33, 161, 9, 137, 41, 169 },
|
|
|
|
|
{ 195, 67, 227, 99, 203, 75, 235, 107,
|
|
|
|
|
193, 65, 225, 97, 201, 73, 233, 105 },
|
|
|
|
|
{ 51, 179, 19, 147, 59, 187, 27, 155,
|
|
|
|
|
49, 177, 17, 145, 57, 185, 25, 153 },
|
|
|
|
|
{ 243, 115, 211, 83, 251, 123, 219, 91,
|
|
|
|
|
241, 113, 209, 81, 249, 121, 217, 89 },
|
|
|
|
|
{ 15, 143, 47, 175, 7, 135, 39, 167,
|
|
|
|
|
13, 141, 45, 173, 5, 133, 37, 165 },
|
|
|
|
|
{ 207, 79, 239, 111, 199, 71, 231, 103,
|
|
|
|
|
205, 77, 237, 109, 197, 69, 229, 101 },
|
|
|
|
|
{ 63, 191, 31, 159, 55, 183, 23, 151,
|
|
|
|
|
61, 189, 29, 157, 53, 181, 21, 149 },
|
|
|
|
|
{ 254, 127, 223, 95, 247, 119, 215, 87,
|
|
|
|
|
253, 125, 221, 93, 245, 117, 213, 85 }
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// Generate a 1-bit "screen door" alpha mask; not always pretty, but
|
|
|
|
|
// definitely fast... In the future we may be able to support things
|
|
|
|
|
// like the RENDER extension in XFree86, when available, to provide
|
|
|
|
|
// true RGBA-blended rendering. See:
|
|
|
|
|
//
|
|
|
|
|
// http://www.xfree86.org/~keithp/render/protocol.html
|
|
|
|
|
//
|
|
|
|
|
// for more info on XRender...
|
|
|
|
|
//
|
|
|
|
|
// MacOS already provides alpha blending support and has its own
|
2002-04-15 20:52:26 +00:00
|
|
|
// fl_create_alphamask() function...
|
2002-04-14 21:26:06 +00:00
|
|
|
memset(bitmap, 0, bmw * h);
|
|
|
|
|
|
|
|
|
|
for (dataptr = array + d - 1, y = 0; y < h; y ++, dataptr += ld)
|
|
|
|
|
for (bitptr = bitmap + y * bmw, bit = 1, x = 0; x < w; x ++, dataptr += d) {
|
|
|
|
|
if (*dataptr > dither[x & 15][y & 15])
|
2020-07-01 16:03:10 +00:00
|
|
|
*bitptr |= bit;
|
2002-04-14 21:26:06 +00:00
|
|
|
if (bit < 128) bit <<= 1;
|
|
|
|
|
else {
|
2020-07-01 16:03:10 +00:00
|
|
|
bit = 1;
|
|
|
|
|
bitptr ++;
|
2002-04-14 21:26:06 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2010-03-15 23:47:47 +00:00
|
|
|
bm = fl_create_bitmask(w, h, bitmap);
|
2002-04-14 21:26:06 +00:00
|
|
|
delete[] bitmap;
|
|
|
|
|
|
2010-03-15 23:47:47 +00:00
|
|
|
return (bm);
|
2002-04-14 21:26:06 +00:00
|
|
|
}
|
|
|
|
|
|
1998-10-15 14:06:16 +00: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 13:07:00 +00:00
|
|
|
fl_graphics_driver->draw_bitmap(this, XP, YP, WP, HP, cx, cy);
|
2010-04-14 13:07:34 +00:00
|
|
|
}
|
|
|
|
|
|
2008-09-14 18:19:41 +00:00
|
|
|
/**
|
2016-10-14 14:33:18 +00:00
|
|
|
The destructor frees all memory and server resources that are used by
|
2008-09-14 18:19:41 +00:00
|
|
|
the bitmap.
|
|
|
|
|
*/
|
1998-10-06 18:21:25 +00:00
|
|
|
Fl_Bitmap::~Fl_Bitmap() {
|
2002-08-05 17:50:25 +00:00
|
|
|
uncache();
|
2001-11-19 20:59:59 +00:00
|
|
|
if (alloc_array) delete[] (uchar *)array;
|
1998-10-06 18:21:25 +00:00
|
|
|
}
|
|
|
|
|
|
2002-08-05 17:50:25 +00:00
|
|
|
void Fl_Bitmap::uncache() {
|
2010-03-15 23:47:47 +00:00
|
|
|
if (id_) {
|
2016-02-23 20:37:22 +00:00
|
|
|
fl_delete_bitmask((Fl_Bitmask)id_);
|
2010-03-15 23:47:47 +00:00
|
|
|
id_ = 0;
|
2002-08-05 17:50:25 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2002-08-09 01:09:49 +00:00
|
|
|
void Fl_Bitmap::label(Fl_Widget* widget) {
|
|
|
|
|
widget->image(this);
|
1998-10-06 18:21:25 +00:00
|
|
|
}
|
|
|
|
|
|
2001-08-05 23:58:54 +00:00
|
|
|
void Fl_Bitmap::label(Fl_Menu_Item* m) {
|
2017-09-12 13:05:48 +00:00
|
|
|
m->label(FL_IMAGE_LABEL, (const char*)this);
|
1998-10-06 18:21:25 +00:00
|
|
|
}
|
1998-10-19 20:46:58 +00: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 01:06:45 +00:00
|
|
|
Fl_Image *Fl_Bitmap::copy(int W, int H) {
|
2020-07-01 16:03:10 +00:00
|
|
|
Fl_Bitmap *new_image; // New RGB image
|
|
|
|
|
uchar *new_array; // New array for image data
|
2004-09-24 16:00:11 +00: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 01:06:45 +00:00
|
|
|
// Optimize the simple copy where the width and height are the same...
|
2018-03-22 16:38:38 +00:00
|
|
|
if (W == data_w() && H == data_h()) {
|
2004-09-24 16:00:11 +00:00
|
|
|
new_array = new uchar [H * ((W + 7) / 8)];
|
|
|
|
|
memcpy(new_array, array, H * ((W + 7) / 8));
|
|
|
|
|
|
|
|
|
|
new_image = new Fl_Bitmap(new_array, W, H);
|
|
|
|
|
new_image->alloc_array = 1;
|
|
|
|
|
|
|
|
|
|
return new_image;
|
|
|
|
|
}
|
2002-10-11 13:54:10 +00: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 01:06:45 +00:00
|
|
|
|
2020-07-01 16:03:10 +00: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 01:06:45 +00:00
|
|
|
|
|
|
|
|
|
2016-08-09 15:20:54 +00:00
|
|
|
// Figure out Bresenham step/modulus values...
|
2018-03-22 16:38:38 +00: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 01:06:45 +00:00
|
|
|
|
|
|
|
|
// Allocate memory for the new image...
|
2004-09-24 16:00:11 +00: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 01:06:45 +00:00
|
|
|
new_image = new Fl_Bitmap(new_array, W, H);
|
|
|
|
|
new_image->alloc_array = 1;
|
|
|
|
|
|
2004-09-24 16:00:11 +00: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 01:06:45 +00:00
|
|
|
|
|
|
|
|
// Scale the image using a nearest-neighbor algorithm...
|
2002-12-19 21:34:26 +00:00
|
|
|
for (dy = H, sy = 0, yerr = H, new_ptr = new_array; dy > 0; dy --) {
|
2018-03-22 16:38:38 +00:00
|
|
|
for (dx = W, xerr = W, old_ptr = array + sy * ((data_w() + 7) / 8), sx = 0, new_bit = 1;
|
2020-07-01 16:03:10 +00:00
|
|
|
dx > 0;
|
|
|
|
|
dx --) {
|
2005-07-14 13:16:17 +00: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 01:06:45 +00:00
|
|
|
if (old_ptr[sx / 8] & old_bit) *new_ptr |= new_bit;
|
|
|
|
|
|
2005-07-14 13:16:17 +00: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 01:06:45 +00:00
|
|
|
else {
|
2005-07-14 13:16:17 +00:00
|
|
|
new_bit = 1;
|
2020-07-01 16:03:10 +00: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 01:06:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
sx += xstep;
|
|
|
|
|
xerr -= xmod;
|
|
|
|
|
|
|
|
|
|
if (xerr <= 0) {
|
2020-07-01 16:03:10 +00: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 01:06:45 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2005-07-14 13:16:17 +00: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 01:06:45 +00:00
|
|
|
|
|
|
|
|
sy += ystep;
|
|
|
|
|
yerr -= ymod;
|
|
|
|
|
if (yerr <= 0) {
|
|
|
|
|
yerr += H;
|
|
|
|
|
sy ++;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return new_image;
|
|
|
|
|
}
|