summaryrefslogtreecommitdiff
path: root/FL/Fl_Shared_Image.H
blob: 11c5ce794d23f02a87452de148b33a59fca403ec (plain)
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
//
// "$Id: Fl_Shared_Image.H,v 1.22 2001/07/16 19:38:17 robertk Exp $"
//
// Image file header file 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".
//

#ifndef Fl_Shared_Image_H
#define Fl_Shared_Image_H

#include <FL/Fl_Image.H>
#include <stddef.h>

struct FL_IMAGES_API Fl_Image_Type;

// Shared images class. 
class FL_IMAGES_API Fl_Shared_Image : public Fl_Image {
protected:
  static const char* fl_shared_image_root;

  static int image_used;
  static size_t mem_usage_limit;

  static size_t mem_used;
  static int forbid_delete;

  Fl_Shared_Image* l1;    // Left leaf in the binary tree
  Fl_Shared_Image* l2;    // Right leaf in the binary tree
  const char* 	   name;  // Used to indentify the image, and as filename
  const uchar* datas; // If non zero, pointers on inlined compressed datas
  unsigned int     used;  // Last time used, for cache handling purpose
  int              refcount; // Number of time this image has been get

  Fl_Shared_Image() { };  // Constructor is private on purpose,
                          // use the get function rather
  ~Fl_Shared_Image();

  void find_less_used();
  static void check_mem_usage();

  const char* get_filename();// Return the filename obtained from the concatenation
                          // of the image root directory and this image name
                          // WARNING : the returned pointer will be
                          // available only until next call to get_filename

  static const char* get_filename(const char*);

  virtual void read() = 0;// decompress the image and create its pixmap

  static void insert(Fl_Shared_Image*& p, Fl_Shared_Image* image);
  static Fl_Shared_Image* find(Fl_Shared_Image* image, const char* name);
  void remove_from_tree(Fl_Shared_Image*& p, Fl_Shared_Image* image);


public:
  static Fl_Shared_Image  *first_image;

  // Return an Fl_Shared_Image, using the create function if an image with
  // the given name doesn't already exist. Use datas, or read from the
  // file with filename name if datas==0.
  static Fl_Shared_Image* get(Fl_Shared_Image* (*create)(),
			      const char* name, const uchar* datas=0);

  // Reload the image, useful if it has changed on disk, or if the datas
  // in memory have changed (you can also give a new pointer on datas)
  void reload(const uchar* datas=0);
  static void reload(const char* name, const uchar* datas=0);

  // Remove an image from the database and delete it if its refcount has
  // fallen to zero
  // Each remove decrement the refcount, each get increment it
  // Return 1 if it has been really deleted.
  int remove();
  static int remove(const char* name);

  // Clear the cache for this image and all of its children in the binary tree
  void clear_cache();

  // Try to guess the filetype
  // Beware that calling this force you to link in all image types !
  static Fl_Image_Type* guess(const char* name, const uchar* datas=0);

  // Set the position where images are looked for on disk
  static void set_root_directory(const char* d);

  // Set the size of the cache (0 = unlimited is the default)
  static void set_cache_size(size_t l);

  virtual void draw(int X, int Y, int W, int H, int cx, int cy);
};



// Description of a file format
struct FL_IMAGES_API Fl_Image_Type {
  // Name of the filetype as it appear in the source code (uppercase)
  const char* name;
  // Function to test the filetype
  int (*test)(const uchar* datas, size_t size=0);
  // Function to get/create an image of this type
  Fl_Shared_Image* (*get)(const char* name, const uchar* datas=0);
};
extern FL_IMAGES_API Fl_Image_Type fl_image_filetypes[];

/* Specific image format functions. Add you own file format here. */

// PNG image class
class FL_IMAGES_API Fl_PNG_Image : public Fl_Shared_Image {
  void read();		// Uncompress PNG datas
  Fl_PNG_Image() { }
  static Fl_Shared_Image* create() { return new Fl_PNG_Image; } // Instantiate
public:
// Check the given buffer if it is in PNG format
  static int test(const uchar* datas, size_t size=0);
  void measure(int& W, int& H); // Return width and heigth
  static Fl_Shared_Image* get(const char* name, const uchar* datas = 0) {
    return Fl_Shared_Image::get(create, name, datas);
  }
};

class FL_IMAGES_API Fl_GIF_Image : public Fl_Shared_Image {
  void read();
  Fl_GIF_Image() { }
  static Fl_Shared_Image* create() { return new Fl_GIF_Image; }
public:
  static int test(const uchar* datas, size_t size=0);
  void measure(int& W, int& H);
  static Fl_Shared_Image* get(const char* name, const uchar* datas = 0) {
    return Fl_Shared_Image::get(create, name, datas);
  }
};

class FL_IMAGES_API Fl_XPM_Image : public Fl_Shared_Image {
  void read();
  Fl_XPM_Image() { }
  static Fl_Shared_Image* create() { return new Fl_XPM_Image; }
public:
  static int test(const uchar* datas, size_t size=0);
  void measure(int& W, int& H);
  static Fl_Shared_Image* get(const char* name, const uchar* datas = 0) {
    return Fl_Shared_Image::get(create, name, datas);
  }
};

class FL_IMAGES_API Fl_BMP_Image : public Fl_Shared_Image {
  void read();
  Fl_BMP_Image() { }
  static Fl_Shared_Image* create() { return new Fl_BMP_Image; }
public:
  static int test(const uchar* datas, size_t size=0);
  void measure(int& W, int& H);
  static Fl_Shared_Image* get(const char* name, const uchar* datas = 0) {
    return Fl_Shared_Image::get(create, name, datas);
  }
};

class FL_IMAGES_API Fl_JPEG_Image : public Fl_Shared_Image {
  void read();
  Fl_JPEG_Image() { }
  static Fl_Shared_Image* create() { return new Fl_JPEG_Image; }
public:
  static int test(const uchar* datas, size_t size=0);
  void measure(int& W, int& H);
  static Fl_Shared_Image* get(const char* name, const uchar* datas = 0) {
    return Fl_Shared_Image::get(create, name, datas);
  }
};

//class FL_API Fl_Bitmap;
class Fl_Bitmap;
extern FL_IMAGES_API Fl_Bitmap nosuch_bitmap;

class FL_IMAGES_API Fl_UNKNOWN_Image {
public:
  static int test(const uchar*, size_t =0) { return 1; };
  static Fl_Shared_Image* get(const char*, const uchar* = 0) {
    return (Fl_Shared_Image*) &nosuch_bitmap;
  };
};

#endif

//
// End of "$Id: Fl_Shared_Image.H,v 1.22 2001/07/16 19:38:17 robertk Exp $"
//