Add image handler to Fl_Shared_Image class to support additional file

formats.


git-svn-id: file:///fltk/svn/fltk/branches/branch-1.1@2048 ea41ed52-d2ee-0310-a9c1-e6b18d33e121
This commit is contained in:
Michael R Sweet 2002-03-29 11:59:56 +00:00
parent ed47c6972b
commit 2deb2fdfc9
3 changed files with 92 additions and 6 deletions

View File

@ -1,3 +1,10 @@
CHANGES IN FLTK 1.1.0
- The Fl_Shared_Image class now supports additional
image handling functions - this allows you to support
additional image file formats transparently.
CHANGES IN FLTK 1.1.0b12 CHANGES IN FLTK 1.1.0b12
- Documentation updates. - Documentation updates.

View File

@ -1,5 +1,5 @@
// //
// "$Id: Fl_Shared_Image.H,v 1.22.2.2 2002/01/01 15:11:28 easysw Exp $" // "$Id: Fl_Shared_Image.H,v 1.22.2.3 2002/03/29 11:59:56 easysw Exp $"
// //
// Shared image header file for the Fast Light Tool Kit (FLTK). // Shared image header file for the Fast Light Tool Kit (FLTK).
// //
@ -29,6 +29,10 @@
# include "Fl_Image.H" # include "Fl_Image.H"
// Test function for adding new formats
typedef Fl_Image *(*Fl_Shared_Handler)(const char *name, uchar *header,
int headerlen);
// Shared images class. // Shared images class.
class Fl_Shared_Image : public Fl_Image { class Fl_Shared_Image : public Fl_Image {
protected: protected:
@ -36,6 +40,9 @@ class Fl_Shared_Image : public Fl_Image {
static Fl_Shared_Image **images_; // Shared images static Fl_Shared_Image **images_; // Shared images
static int num_images_; // Number of shared images static int num_images_; // Number of shared images
static int alloc_images_; // Allocated shared images static int alloc_images_; // Allocated shared images
static Fl_Shared_Handler **handlers_; // Additional format handlers
static int num_handlers_; // Number of format handlers
static int alloc_handlers_; // Allocated format handlers
const char *name_; // Name of image file const char *name_; // Name of image file
int original_; // Original image? int original_; // Original image?
@ -70,10 +77,12 @@ class Fl_Shared_Image : public Fl_Image {
static Fl_Shared_Image *get(const char *n, int W = 0, int H = 0); static Fl_Shared_Image *get(const char *n, int W = 0, int H = 0);
static Fl_Shared_Image **images() { return images_; } static Fl_Shared_Image **images() { return images_; }
static int num_images() { return num_images_; } static int num_images() { return num_images_; }
static void add_handler(Fl_Shared_Handler *f);
static void remove_handler(Fl_Shared_Handler *f);
}; };
#endif // !Fl_Shared_Image_H #endif // !Fl_Shared_Image_H
// //
// End of "$Id: Fl_Shared_Image.H,v 1.22.2.2 2002/01/01 15:11:28 easysw Exp $" // End of "$Id: Fl_Shared_Image.H,v 1.22.2.3 2002/03/29 11:59:56 easysw Exp $"
// //

View File

@ -1,5 +1,5 @@
// //
// "$Id: Fl_Shared_Image.cxx,v 1.23.2.7 2002/01/06 17:51:12 easysw Exp $" // "$Id: Fl_Shared_Image.cxx,v 1.23.2.8 2002/03/29 11:59:56 easysw Exp $"
// //
// Shared image code for the Fast Light Tool Kit (FLTK). // Shared image code for the Fast Light Tool Kit (FLTK).
// //
@ -46,6 +46,10 @@ Fl_Shared_Image **Fl_Shared_Image::images_ = 0; // Shared images
int Fl_Shared_Image::num_images_ = 0; // Number of shared images int Fl_Shared_Image::num_images_ = 0; // Number of shared images
int Fl_Shared_Image::alloc_images_ = 0; // Allocated shared images int Fl_Shared_Image::alloc_images_ = 0; // Allocated shared images
Fl_Shared_Handler **Fl_Shared_Image::handlers_; // Additional format handlers
int Fl_Shared_Image::num_handlers_; // Number of format handlers
int Fl_Shared_Image::alloc_handlers_; // Allocated format handlers
// //
// Typedef the C API sort function type the only way I know how... // Typedef the C API sort function type the only way I know how...
@ -204,6 +208,7 @@ Fl_Shared_Image::release() {
void void
Fl_Shared_Image::reload() { Fl_Shared_Image::reload() {
// Load image from disk... // Load image from disk...
int i; // Looping var
FILE *fp; // File pointer FILE *fp; // File pointer
uchar header[16]; // Buffer for auto-detecting files uchar header[16]; // Buffer for auto-detecting files
Fl_Image *img; // New image Fl_Image *img; // New image
@ -234,8 +239,14 @@ Fl_Shared_Image::reload() {
img = new Fl_XBM_Image(name_); img = new Fl_XBM_Image(name_);
else if (memcmp(header, "/* XPM */", 9) == 0) // XPM file else if (memcmp(header, "/* XPM */", 9) == 0) // XPM file
img = new Fl_XPM_Image(name_); img = new Fl_XPM_Image(name_);
else else {
img = 0; // Not a standard format; try an image handler...
for (i = 0, img = 0; i < num_handlers_; i ++) {
img = (*(handlers_[i]))(name_, header, sizeof(header));
if (img) break;
}
}
if (img) { if (img) {
if (alloc_image_) delete image_; if (alloc_image_) delete image_;
@ -385,5 +396,64 @@ Fl_Shared_Image::get(const char *n, int W, int H) {
// //
// End of "$Id: Fl_Shared_Image.cxx,v 1.23.2.7 2002/01/06 17:51:12 easysw Exp $". // 'Fl_Shared_Image::add_handler()' - Add a shared image handler.
//
void
Fl_Shared_Image::add_handler(Fl_Shared_Handler *f) {
int i; // Looping var...
Fl_Shared_Handler **temp; // New image handler array...
// First see if we have already added the handler...
for (i = 0; i < num_handlers_; i ++) {
if (handlers_[i] == f) return;
}
if (num_handlers_ >= alloc_handlers_) {
// Allocate more memory...
temp = new Fl_Shared_Handler *[alloc_handlers_ + 32];
if (alloc_handlers_) {
memcpy(temp, handlers_, alloc_handlers_ * sizeof(Fl_Shared_Handler *));
delete[] handlers_;
}
handlers_ = temp;
alloc_handlers_ += 32;
}
handlers_[num_handlers_] = f;
num_handlers_ ++;
}
//
// 'Fl_Shared_Image::remove_handler()' - Remove a shared image handler.
//
void
Fl_Shared_Image::remove_handler(Fl_Shared_Handler *f) {
int i; // Looping var...
// First see if the handler has been added...
for (i = 0; i < num_handlers_; i ++) {
if (handlers_[i] == f) break;
}
if (i >= num_handlers_) return;
// OK, remove the handler from the array...
num_handlers_ --;
if (i < num_handlers_) {
// Shift later handlers down 1...
memcpy(handlers_ + i, handlers_ + i + 1,
(num_handlers_ - i) * sizeof(Fl_Shared_Handler *));
}
}
//
// End of "$Id: Fl_Shared_Image.cxx,v 1.23.2.8 2002/03/29 11:59:56 easysw Exp $".
// //