1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
|
//
// "$Id: Fl_Shared_Image.cxx,v 1.23 2001/09/10 01:16:17 spitzak Exp $"
//
// Image drawing code for the Fast Light Tool Kit (FLTK).
//
// Copyright 1998-1999 by Bill Spitzak and others.
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Library General Public
// License as published by the Free Software Foundation; either
// version 2 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Library General Public License for more details.
//
// You should have received a copy of the GNU Library General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
// USA.
//
// Please report all bugs and problems to "fltk-bugs@easysw.com".
//
// Draw an image that is stored compressed in a file or in memory.
// Keep uncompressed images in memory for later use.
#include <config.h>
#include <fltk/Fl.h>
#include <fltk/fl_draw.h>
#include <fltk/Fl_Shared_Image.h>
#include <fltk/Fl_Bitmap.h>
#include <fltk/x.h>
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
FL_IMAGES_API const char *Fl_Shared_Image::fl_shared_image_root=0;
FL_IMAGES_API Fl_Shared_Image *Fl_Shared_Image::first_image = 0;
FL_IMAGES_API int Fl_Shared_Image::image_used=0;
FL_IMAGES_API size_t Fl_Shared_Image::mem_usage_limit=0;
FL_IMAGES_API size_t Fl_Shared_Image::mem_used=0;
FL_IMAGES_API int Fl_Shared_Image::forbid_delete = 1;
// static size_t mem_used=0; (now moved to Fl.cxx !)
// This contains the total number of pixmap pixels in the cache
// WARNING : this is updated incrementally, so beware that it keeps balanced
// when deleting or creating pixmaps !
Fl_Shared_Image::~Fl_Shared_Image()
{
if(forbid_delete)
fprintf(stderr,
"FLTK user error : deleting an Fl_Shared_Image object is forbiden !\n");
if(id) mem_used -= w*h;
}
void Fl_Shared_Image::set_cache_size(size_t l)
{
mem_usage_limit = l;
}
static Fl_Shared_Image *limage; // used to find the less used image
void Fl_Shared_Image::find_less_used() {
if(l1) l1->find_less_used();
if(l2) l2->find_less_used();
if(id && (limage->id == 0 || used<limage->used)) limage=this;
}
void Fl_Shared_Image::check_mem_usage()
{
if(mem_usage_limit==0 || first_image==NULL || mem_used < mem_usage_limit)
return;
do {
limage=first_image;
first_image->find_less_used();
if(limage->id) {
mem_used -= limage->w*limage->h;
fl_delete_offscreen(Pixmap(limage->id));
limage->id=0;
if(limage->mask) {
fl_delete_bitmap(Pixmap(limage->mask));
limage->mask = 0;
}
} else return;
} while(mem_used >= mem_usage_limit);
}
class fl_shared_image_destructor_class {
int dummy;
public:
fl_shared_image_destructor_class() { dummy = 0; }
~fl_shared_image_destructor_class() {
if (Fl_Shared_Image::first_image) Fl_Shared_Image::first_image->clear_cache();
}
};
fl_shared_image_destructor_class fl_shared_image_destructor;
void Fl_Shared_Image::clear_cache()
{
if(id) {
mem_used -= w*h;
fl_delete_offscreen((Pixmap)id);
id=0;
if(mask) {
fl_delete_bitmap((Pixmap)mask);
mask = 0;
}
}
if (l1) l1->clear_cache();
if (l2) l2->clear_cache();
}
void Fl_Shared_Image::set_root_directory(const char *d) {
fl_shared_image_root = d;
}
void Fl_Shared_Image::insert(Fl_Shared_Image*& p, Fl_Shared_Image* image) {
if(p == 0)
p = image;
else {
int c = strcmp(image->name, p->name);
if(c<0) insert(p->l1, image);
else insert(p->l2, image);
}
}
Fl_Shared_Image* Fl_Shared_Image::find(Fl_Shared_Image* image, const char* name) {
if(image == 0) return 0;
int c = strcmp(name, image->name);
if(c == 0) return image;
else if(c<0) return find(image->l1, name);
else return find(image->l2, name);
}
const char* Fl_Shared_Image::get_filename() {
return get_filename(name);
}
const char* Fl_Shared_Image::get_filename(const char* name)
{
if (name[0] == '/' || !fl_shared_image_root || !*fl_shared_image_root)
return name;
int m = strlen(fl_shared_image_root);
int n = strlen(name) + m + 2;
static char *s;
if (s) free(s);
s = (char*) malloc(n+1);
strcpy(s, fl_shared_image_root);
if (s[m-1] != '/') s[m++] = '/';
strcpy(s+m, name);
return s;
}
Fl_Shared_Image* Fl_Shared_Image::get(Fl_Shared_Image* (*create)(),
const char* name, const uchar *datas)
{
Fl_Shared_Image *image=Fl_Shared_Image::find(first_image, name);
if(!image)
{
image=create();
image->refcount = 1;
image->name = strdup(name);
image->datas=datas;
image->w = -1; // We mark the fact the it has never been measured yet
image->l1 = image->l2 = 0;
image->id=image->mask=0;
Fl_Shared_Image::insert(first_image, image);
} else {
if(image->datas==NULL) image->datas=datas;
image->refcount++;
}
image->used = image_used++;
return image;
}
void Fl_Shared_Image::reload(const uchar* pdatas)
{
if (id) {
mem_used -= w*h;
fl_delete_offscreen((Pixmap)id);
id=0;
if (mask) {
fl_delete_bitmap((Pixmap)mask);
mask = 0;
}
}
if (pdatas) datas = pdatas;
measure(w, h);
}
void Fl_Shared_Image::reload(const char* name, const uchar* pdatas)
{
Fl_Shared_Image *image=Fl_Shared_Image::find(first_image, name);
if (image) image->reload(pdatas);
}
void Fl_Shared_Image::remove_from_tree(Fl_Shared_Image*& p, Fl_Shared_Image* image) {
if (p) {
int c = strcmp(image->name, p->name);
if (c == 0) {
if (image->l1) {
p = image->l1;
if (image->l2) insert(first_image, image->l2);
} else
p = image->l2;
} else if (c<0) remove_from_tree(p->l1, image);
else remove_from_tree(p->l2, image);
}
}
int Fl_Shared_Image::remove()
{
if (--refcount) return 0;
remove_from_tree(first_image, this);
forbid_delete = 0;
delete this;
forbid_delete = 1;
return 1;
}
int Fl_Shared_Image::remove(const char* name)
{
Fl_Shared_Image *image=Fl_Shared_Image::find(first_image, name);
if (image) return image->remove();
else return 0;
}
void Fl_Shared_Image::draw(int X, int Y, Fl_Flags flags)
{
if (w<0) measure(w, h);
if (w==0) return;
if (!id) // Need to uncompress the image ?
{
used = image_used++; // do this before check_mem_usage
mem_used += w*h;
check_mem_usage();
read();
if (!id) { // could not read the image for some reason ?
mem_used -= w*h;
w = 0; // Will never try again ...
return;
}
}
else
used = image_used++;
_draw(X, Y, flags);
}
//
// End of "$Id: Fl_Shared_Image.cxx,v 1.23 2001/09/10 01:16:17 spitzak Exp $"
//
|