From 2deb2fdfc969a925542a3c069e676426174cc07a Mon Sep 17 00:00:00 2001 From: Michael R Sweet Date: Fri, 29 Mar 2002 11:59:56 +0000 Subject: [PATCH] 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 --- CHANGES | 7 ++++ FL/Fl_Shared_Image.H | 13 +++++-- src/Fl_Shared_Image.cxx | 78 ++++++++++++++++++++++++++++++++++++++--- 3 files changed, 92 insertions(+), 6 deletions(-) diff --git a/CHANGES b/CHANGES index d361f5fbd..abd4b10e1 100644 --- a/CHANGES +++ b/CHANGES @@ -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 - Documentation updates. diff --git a/FL/Fl_Shared_Image.H b/FL/Fl_Shared_Image.H index d4f6971ef..1d555b3dd 100644 --- a/FL/Fl_Shared_Image.H +++ b/FL/Fl_Shared_Image.H @@ -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). // @@ -29,6 +29,10 @@ # 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. class Fl_Shared_Image : public Fl_Image { protected: @@ -36,6 +40,9 @@ class Fl_Shared_Image : public Fl_Image { static Fl_Shared_Image **images_; // Shared images static int num_images_; // Number of 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 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 **images() { return 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 // -// 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 $" // diff --git a/src/Fl_Shared_Image.cxx b/src/Fl_Shared_Image.cxx index 623e80967..ff7f9b8f5 100644 --- a/src/Fl_Shared_Image.cxx +++ b/src/Fl_Shared_Image.cxx @@ -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). // @@ -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::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... @@ -204,6 +208,7 @@ Fl_Shared_Image::release() { void Fl_Shared_Image::reload() { // Load image from disk... + int i; // Looping var FILE *fp; // File pointer uchar header[16]; // Buffer for auto-detecting files Fl_Image *img; // New image @@ -234,8 +239,14 @@ Fl_Shared_Image::reload() { img = new Fl_XBM_Image(name_); else if (memcmp(header, "/* XPM */", 9) == 0) // XPM file img = new Fl_XPM_Image(name_); - else - img = 0; + else { + // 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 (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 $". //