1 //
2 // "$Id$"
3 //
4 // Shared image header file for the Fast Light Tool Kit (FLTK).
5 //
6 // Copyright 1998-2017 by Bill Spitzak and others.
7 //
8 // This library is free software. Distribution and use rights are outlined in
9 // the file "COPYING" which should have been included with this file.  If this
10 // file is missing or damaged, see the license at:
11 //
12 //     http://www.fltk.org/COPYING.php
13 //
14 // Please report all bugs and problems on the following page:
15 //
16 //     http://www.fltk.org/str.php
17 //
18 
19 /** \file
20    Fl_Shared_Image class. */
21 
22 #ifndef Fl_Shared_Image_H
23 #  define Fl_Shared_Image_H
24 
25 #  include "Fl_Image.H"
26 
27 
28 // Test function for adding new formats
29 typedef Fl_Image *(*Fl_Shared_Handler)(const char *name, uchar *header,
30                                        int headerlen);
31 
32 // Shared images class.
33 /**
34   This class supports caching, loading, scaling, and drawing of image files.
35 
36   Most applications will also want to link against the fltk_images library
37   and call the fl_register_images() function to support standard image
38   formats such as BMP, GIF, JPEG, and PNG.
39 
40   Images can be requested (loaded) with Fl_Shared_Image::get(), find(),
41   and some other methods. All images are cached in an internal list of
42   shared images and should be released when they are no longer needed.
43   A refcount is used to determine if a released image is to be destroyed
44   with delete.
45 
46   \see Fl_Shared_Image::get()
47   \see Fl_Shared_Image::find()
48   \see Fl_Shared_Image::release()
49 */
50 class FL_EXPORT Fl_Shared_Image : public Fl_Image {
51 
52   friend class Fl_JPEG_Image;
53   friend class Fl_PNG_Image;
54 
55 private:
56   static Fl_RGB_Scaling scaling_algorithm_; // method used to rescale RGB source images
57 #if FLTK_ABI_VERSION >= 10304
58   Fl_Image *scaled_image_;
59 #endif
60 protected:
61 
62   static Fl_Shared_Image **images_;	// Shared images
63   static int	num_images_;		// Number of shared images
64   static int	alloc_images_;		// Allocated shared images
65   static Fl_Shared_Handler *handlers_;	// Additional format handlers
66   static int	num_handlers_;		// Number of format handlers
67   static int	alloc_handlers_;	// Allocated format handlers
68 
69   const char	*name_;			// Name of image file
70   int		original_;		// Original image?
71   int		refcount_;		// Number of times this image has been used
72   Fl_Image	*image_;		// The image that is shared
73   int		alloc_image_;		// Was the image allocated?
74 
75   static int	compare(Fl_Shared_Image **i0, Fl_Shared_Image **i1);
76 
77   // Use get() and release() to load/delete images in memory...
78   Fl_Shared_Image();
79   Fl_Shared_Image(const char *n, Fl_Image *img = 0);
80   virtual ~Fl_Shared_Image();
81   void add();
82   void update();
83 
84 public:
85   /** Returns the filename of the shared image */
name()86   const char	*name() { return name_; }
87 
88   /** Returns the number of references of this shared image.
89     When reference is below 1, the image is deleted.
90   */
refcount()91   int		refcount() { return refcount_; }
92 
93   /** Returns whether this is an original image.
94     Images loaded from a file or from memory are marked \p original as
95     opposed to images created as a copy of another image with different
96     size (width or height).
97     \note This is useful for debugging (rarely used in user code).
98     \since FLTK 1.4.0
99   */
original()100   int original() { return original_; }
101 
102   void		release();
103   void		reload();
104 
105   virtual Fl_Image *copy(int W, int H);
copy()106   Fl_Image *copy() { return copy(w(), h()); }
107   virtual void color_average(Fl_Color c, float i);
108   virtual void desaturate();
109   virtual void draw(int X, int Y, int W, int H, int cx, int cy);
draw(int X,int Y)110   void draw(int X, int Y) { draw(X, Y, w(), h(), 0, 0); }
111   void scale(int width, int height, int proportional = 1, int can_expand = 0);
112   virtual void uncache();
113 
114   static Fl_Shared_Image *find(const char *name, int W = 0, int H = 0);
115   static Fl_Shared_Image *get(const char *name, int W = 0, int H = 0);
116   static Fl_Shared_Image *get(Fl_RGB_Image *rgb, int own_it = 1);
117   static Fl_Shared_Image **images();
118   static int		num_images();
119   static void		add_handler(Fl_Shared_Handler f);
120   static void		remove_handler(Fl_Shared_Handler f);
121   /** Sets what algorithm is used when resizing a source image.
122    The default algorithm is FL_RGB_SCALING_BILINEAR.
123    Drawing an Fl_Shared_Image is sometimes performed by first resizing the source image
124    and then drawing the resized copy. This occurs, e.g., when drawing to screen under Linux or MSWindows
125    after having called Fl_Shared_Image::scale().
126    This function controls what method is used when the image to be resized is an Fl_RGB_Image.
127    \version 1.3.4 and requires compiling with FLTK_ABI_VERSION = 10304
128    */
scaling_algorithm(Fl_RGB_Scaling algorithm)129   static void scaling_algorithm(Fl_RGB_Scaling algorithm) {scaling_algorithm_ = algorithm; }
130 };
131 
132 //
133 // The following function is provided in the fltk_images library and
134 // registers all of the "extra" image file formats that are not part
135 // of the core FLTK library...
136 //
137 
138 FL_EXPORT extern void fl_register_images();
139 
140 #endif // !Fl_Shared_Image_H
141 
142 //
143 // End of "$Id$"
144 //
145