1 | // "$Id: SharedImage.h 4419 2005-07-15 05:48:26Z spitzak $" |
---|
2 | // |
---|
3 | // Copyright 1998-2004 by Bill Spitzak and others. |
---|
4 | // |
---|
5 | // This library is free software; you can redistribute it and/or |
---|
6 | // modify it under the terms of the GNU Library General Public |
---|
7 | // License as published by the Free Software Foundation; either |
---|
8 | // version 2 of the License, or (at your option) any later version. |
---|
9 | // |
---|
10 | // This library is distributed in the hope that it will be useful, |
---|
11 | // but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
12 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
---|
13 | // Library General Public License for more details. |
---|
14 | // |
---|
15 | // You should have received a copy of the GNU Library General Public |
---|
16 | // License along with this library; if not, write to the Free Software |
---|
17 | // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 |
---|
18 | // USA. |
---|
19 | // |
---|
20 | // Please report all bugs and problems to "fltk-bugs@fltk.org". |
---|
21 | |
---|
22 | /*! \class fltk::SharedImage |
---|
23 | |
---|
24 | Images that are all put in a tree by "name" (usually a filename) |
---|
25 | so that if the same name is used more than once the same instance |
---|
26 | is used. |
---|
27 | |
---|
28 | This is probably obsolete and will be deleted. The base Symbol class |
---|
29 | now has a lookup-by-name function, and the Image class could have |
---|
30 | methods added to it to throw away the pixmaps of lru images. |
---|
31 | |
---|
32 | */ |
---|
33 | |
---|
34 | #ifndef fltk_SharedImage_h |
---|
35 | #define fltk_SharedImage_h |
---|
36 | |
---|
37 | #include "Image.h" |
---|
38 | |
---|
39 | namespace fltk { |
---|
40 | |
---|
41 | struct FL_IMAGES_API ImageType; |
---|
42 | |
---|
43 | class FL_API SharedImage : public Image { |
---|
44 | protected: |
---|
45 | static const char* shared_image_root; |
---|
46 | |
---|
47 | static int image_used; |
---|
48 | static unsigned mem_usage_limit; |
---|
49 | |
---|
50 | static unsigned mem_used; |
---|
51 | |
---|
52 | SharedImage* l1; // Left leaf in the binary tree |
---|
53 | SharedImage* l2; // Right leaf in the binary tree |
---|
54 | const char* name; // Used to indentify the image, and as filename |
---|
55 | const uchar* datas; // If non zero, pointers on inlined compressed datas |
---|
56 | unsigned int used; // Last time used, for cache handling purpose |
---|
57 | int refcount; // Number of time this image has been get |
---|
58 | |
---|
59 | SharedImage() { }; // Constructor is private on purpose, |
---|
60 | // use the get function rather |
---|
61 | //~SharedImage(); |
---|
62 | |
---|
63 | void find_less_used(); |
---|
64 | static void check_mem_usage(); |
---|
65 | |
---|
66 | /*! Return the filename obtained from the concatenation |
---|
67 | of the image root directory and this image name |
---|
68 | WARNING : the returned pointer will be |
---|
69 | available only until next call to get_filename */ |
---|
70 | const char* get_filename() const; |
---|
71 | |
---|
72 | static const char* get_filename(const char*); |
---|
73 | |
---|
74 | virtual void read() = 0;/*!< decompress the image and create its pixmap */ |
---|
75 | |
---|
76 | static void insert(SharedImage*& p, SharedImage* image); |
---|
77 | static SharedImage* find(SharedImage* image, const char* name); |
---|
78 | void remove_from_tree(SharedImage*& p, SharedImage* image); |
---|
79 | |
---|
80 | |
---|
81 | public: |
---|
82 | static SharedImage *first_image; |
---|
83 | |
---|
84 | /*! Return an SharedImage, using the create function if an image with |
---|
85 | the given name doesn't already exist. Use datas, or read from the |
---|
86 | file with filename name if datas==0. */ |
---|
87 | static SharedImage* get(SharedImage* (*create)(), |
---|
88 | const char* name, const uchar* datas=0); |
---|
89 | |
---|
90 | /*! Reload the image, useful if it has changed on disk, or if the datas |
---|
91 | / in memory have changed (you can also give a new pointer on datas) */ |
---|
92 | void reload(const uchar* datas=0); |
---|
93 | static void reload(const char* name, const uchar* datas=0); |
---|
94 | |
---|
95 | /*! Remove an image from the database and delete it if its refcount has |
---|
96 | fallen to zero |
---|
97 | Each remove() decrements the refcount, each get() increments it |
---|
98 | Return 1 if it has been really deleted. */ |
---|
99 | int remove(); |
---|
100 | static int remove(const char* name); |
---|
101 | |
---|
102 | /*! Clear the cache for this image and all of its children in the binary tree */ |
---|
103 | void clear_cache(); |
---|
104 | |
---|
105 | /*! Set the position where images are looked for on disk */ |
---|
106 | static void set_root_directory(const char* d); |
---|
107 | |
---|
108 | /*! Set the size of the cache (0 = unlimited is the default) */ |
---|
109 | static void set_cache_size(unsigned l); |
---|
110 | |
---|
111 | void update(); |
---|
112 | void _draw(const Rectangle&) const; |
---|
113 | |
---|
114 | }; |
---|
115 | |
---|
116 | //////////////////////////////////////////////////////////////// |
---|
117 | |
---|
118 | /*! Description of an Image file format */ |
---|
119 | struct FL_IMAGES_API ImageType { |
---|
120 | // Name of the filetype as it appear in the source code LOWERCASE!!! |
---|
121 | const char* name; |
---|
122 | // Function to test the filetype |
---|
123 | bool (*test)(const uchar* datas, unsigned size); |
---|
124 | // Function to get/create an image of this type |
---|
125 | SharedImage* (*get)(const char* name, const uchar* datas); |
---|
126 | }; |
---|
127 | extern FL_IMAGES_API ImageType image_filetypes[]; |
---|
128 | |
---|
129 | /*! Try to guess the filetype |
---|
130 | Beware that calling this force you to link in all image types ! */ |
---|
131 | FL_IMAGES_API ImageType* guess_image(const char* name, const uchar* datas=0); |
---|
132 | |
---|
133 | //////////////////////////////////////////////////////////////// |
---|
134 | |
---|
135 | // |
---|
136 | // bmp and gif classes are build in libfltk so they are FL_API |
---|
137 | // |
---|
138 | |
---|
139 | class FL_API gifImage : public SharedImage { |
---|
140 | void read(); |
---|
141 | gifImage() { } |
---|
142 | static SharedImage* create() { return new gifImage; } |
---|
143 | public: |
---|
144 | static bool test(const uchar* datas, unsigned size=0); |
---|
145 | void _measure(int& W, int& H) const; |
---|
146 | static SharedImage* get(const char* name, const uchar* datas = 0) { |
---|
147 | return SharedImage::get(create, name, datas); |
---|
148 | } |
---|
149 | }; |
---|
150 | |
---|
151 | class FL_API bmpImage : public SharedImage { |
---|
152 | void read(); |
---|
153 | bmpImage() { } |
---|
154 | static SharedImage* create() { return new bmpImage; } |
---|
155 | public: |
---|
156 | static bool test(const uchar* datas, unsigned size=0); |
---|
157 | void _measure(int& W, int& H) const; |
---|
158 | static SharedImage* get(const char* name, const uchar* datas = 0) { |
---|
159 | return SharedImage::get(create, name, datas); |
---|
160 | } |
---|
161 | }; |
---|
162 | |
---|
163 | // Name collision with xpmImage: |
---|
164 | class FL_IMAGES_API xpmFileImage : public SharedImage { |
---|
165 | void read(); |
---|
166 | xpmFileImage() { } |
---|
167 | static SharedImage* create() { return new xpmFileImage; } |
---|
168 | public: |
---|
169 | static bool test(const uchar* datas, unsigned size=0); |
---|
170 | void _measure(int& W, int& H) const; |
---|
171 | static SharedImage* get(const char* name, const uchar* datas = 0) { |
---|
172 | return SharedImage::get(create, name, datas); |
---|
173 | } |
---|
174 | }; |
---|
175 | |
---|
176 | // |
---|
177 | // jpeg and png classes are in libfltk_images so they are FL_IMAGES_API |
---|
178 | // |
---|
179 | |
---|
180 | class FL_IMAGES_API jpegImage : public SharedImage { |
---|
181 | void read(); |
---|
182 | jpegImage() { } |
---|
183 | static SharedImage* create() { return new jpegImage; } |
---|
184 | public: |
---|
185 | static bool test(const uchar* datas, unsigned size=0); |
---|
186 | void _measure(int& W, int& H) const; |
---|
187 | static SharedImage* get(const char* name, const uchar* datas = 0) { |
---|
188 | return SharedImage::get(create, name, datas); |
---|
189 | } |
---|
190 | }; |
---|
191 | |
---|
192 | class FL_IMAGES_API pngImage : public SharedImage { |
---|
193 | void read(); // Uncompress PNG datas |
---|
194 | pngImage() { } |
---|
195 | static SharedImage* create() { return new pngImage; } // Instantiate |
---|
196 | public: |
---|
197 | // Check the given buffer if it is in PNG format |
---|
198 | static bool test(const uchar* datas, unsigned size=0); |
---|
199 | void _measure(int& W, int& H) const; |
---|
200 | static SharedImage* get(const char* name, const uchar* datas = 0) { |
---|
201 | return SharedImage::get(create, name, datas); |
---|
202 | } |
---|
203 | }; |
---|
204 | |
---|
205 | } |
---|
206 | |
---|
207 | #endif |
---|
208 | |
---|
209 | // End of "$Id: SharedImage.h 4419 2005-07-15 05:48:26Z spitzak $" |
---|