summaryrefslogtreecommitdiff
path: root/fluid
diff options
context:
space:
mode:
authorMichael R Sweet <michael.r.sweet@gmail.com>2001-11-25 22:51:34 +0000
committerMichael R Sweet <michael.r.sweet@gmail.com>2001-11-25 22:51:34 +0000
commit2b826b0f318e8df5176d3a5fa2edba4b1811e78d (patch)
tree8be0ceaaf079f90e5e58c6af83ca9615b9ec9939 /fluid
parentc147aca0541e9f45b1bd9c8eed4ec71cb1c650ec (diff)
Redo Fluid_Image class to use Fl_Shared_Image instead of its own subclasses
to read files... git-svn-id: file:///fltk/svn/fltk/branches/branch-1.1@1730 ea41ed52-d2ee-0310-a9c1-e6b18d33e121
Diffstat (limited to 'fluid')
-rw-r--r--fluid/Fluid_Image.cxx404
-rw-r--r--fluid/Fluid_Image.h28
2 files changed, 102 insertions, 330 deletions
diff --git a/fluid/Fluid_Image.cxx b/fluid/Fluid_Image.cxx
index 34aeccfda..82298dd4e 100644
--- a/fluid/Fluid_Image.cxx
+++ b/fluid/Fluid_Image.cxx
@@ -1,5 +1,5 @@
//
-// "$Id: Fluid_Image.cxx,v 1.7.2.9.2.4 2001/11/22 15:35:01 easysw Exp $"
+// "$Id: Fluid_Image.cxx,v 1.7.2.9.2.5 2001/11/25 22:51:34 easysw Exp $"
//
// Pixmap label support for the Fast Light Tool Kit (FLTK).
//
@@ -37,319 +37,98 @@
extern void goto_source_dir(); // in fluid.C
extern void leave_source_dir(); // in fluid.C
-////////////////////////////////////////////////////////////////
-#include <FL/Fl_Pixmap.H>
-
-class pixmap_image : public Fluid_Image {
-protected:
- Fl_Pixmap *p;
- int numlines;
- int *linelength;
-public:
- pixmap_image(const char *name, FILE *);
- ~pixmap_image();
- virtual void image(Fl_Widget *); // set the image of this widget
- virtual void deimage(Fl_Widget *); // set the deimage of this widget
- virtual void write_static();
- virtual void write_code(int inactive = 0);
- static int test_file(char *buffer);
-};
-
-int pixmap_image::test_file(char *buffer) {
- return (strstr(buffer,"/* XPM") != 0);
-}
-
-void pixmap_image::image(Fl_Widget *o) {
- o->image(p);
-}
-
-void pixmap_image::deimage(Fl_Widget *o) {
- o->deimage(p);
-}
-
-static int pixmap_header_written;
-
-void pixmap_image::write_static() {
- if (!p) return;
- write_c("\n");
- if (pixmap_header_written != write_number) {
- write_c("#include <FL/Fl_Pixmap.H>\n");
- pixmap_header_written = write_number;
- }
- write_c("static const char *%s[] = {\n",
- unique_id(this, "image", filename_name(name()), 0));
- int l;
- for (l = 0; l < numlines; l++) {
- if (l) write_c(",\n");
- write_cstring(p->data()[l],linelength[l]);
- }
- write_c("\n};\n");
- write_c("static Fl_Pixmap %s(%s);\n",
- unique_id(this, "pixmap", filename_name(name()), 0),
- unique_id(this, "image", filename_name(name()), 0));
+void Fluid_Image::image(Fl_Widget *o) {
+ o->image(img);
}
-void pixmap_image::write_code(int inactive) {
- if (!p) return;
- write_c("%so->%s(%s);\n", indent(), inactive ? "deimage" : "image",
- unique_id(this, "pixmap", filename_name(name()), 0));
-}
-
-static int hexdigit(int x) {
- if (isdigit(x)) return x-'0';
- if (isupper(x)) return x-'A'+10;
- if (islower(x)) return x-'a'+10;
- return 20;
+void Fluid_Image::deimage(Fl_Widget *o) {
+ o->deimage(img);
}
-#define MAXSIZE 2048
-#define INITIALLINES 1024
-
-pixmap_image::pixmap_image(const char *name, FILE *f) : Fluid_Image(name) {
- p = 0;
- numlines = 0;
- linelength = 0;
+static int pixmap_header_written = 0;
+static int bitmap_header_written = 0;
+static int image_header_written = 0;
- if (!f) return; // for subclasses
- // read all the c-strings out of the file:
- char* local_data[INITIALLINES];
- char** data = local_data;
- int local_length[INITIALLINES];
- int* length = local_length;
- int malloc_size = INITIALLINES;
- char buffer[MAXSIZE+20];
- int i = 0;
- while (fgets(buffer,MAXSIZE+20,f)) {
- if (buffer[0] != '\"') continue;
- char *myp = buffer;
- char *q = buffer+1;
- while (*q != '\"' && myp < buffer+MAXSIZE) {
- if (*q == '\\') switch (*++q) {
- case '\n':
- fgets(q,(buffer+MAXSIZE+20)-q,f); break;
- case 0:
- break;
- case 'x': {
- q++;
- int n = 0;
- for (int x = 0; x < 3; x++) {
- int d = hexdigit(*q);
- if (d > 15) break;
- n = (n<<4)+d;
- q++;
- }
- *myp++ = n;
- } break;
- default: {
- int c = *q++;
- if (c>='0' && c<='7') {
- c -= '0';
- for (int x=0; x<2; x++) {
- int d = hexdigit(*q);
- if (d>7) break;
- c = (c<<3)+d;
- q++;
- }
- }
- *myp++ = c;
- } break;
- } else {
- *myp++ = *q++;
- }
+void Fluid_Image::write_static() {
+ if (!img) return;
+ if (img->count() > 1) {
+ // Write Pixmap data...
+ write_c("\n");
+ if (pixmap_header_written != write_number) {
+ write_c("#include <FL/Fl_Pixmap.H>\n");
+ pixmap_header_written = write_number;
}
- *myp++ = 0;
- if (i >= malloc_size) {
- malloc_size = 2*malloc_size;
- if (data == local_data) {
- data = (char**)malloc(malloc_size*sizeof(char*));
- memcpy(data, local_data, i*sizeof(char*));
- length = (int*)malloc(malloc_size*sizeof(int));
- memcpy(length, local_length, i*sizeof(int));
- } else {
- data = (char**)realloc(data, malloc_size*sizeof(char*));
- length = (int*)realloc(length, malloc_size*sizeof(int));
+ write_c("static const char *%s[] = {\n",
+ unique_id(this, "idata", filename_name(name()), 0));
+ write_cstring(img->data()[0], strlen(img->data()[0]));
+
+ int i;
+ int ncolors, chars_per_color;
+ sscanf(img->data()[0], "%*d%*d%d%d", &ncolors, &chars_per_color);
+
+ if (ncolors < 0) {
+ write_c(",\n");
+ write_cstring(img->data()[1], ncolors * -4);
+ i = 2;
+ } else {
+ for (i = 1; i <= ncolors; i ++) {
+ write_c(",\n");
+ write_cstring(img->data()[i], strlen(img->data()[i]));
}
}
- data[i] = new char[myp-buffer];
- memcpy(data[i], buffer,myp-buffer);
- length[i] = myp-buffer-1;
- i++;
- }
-
- if (data == local_data) {
- data = (char**)malloc(i*sizeof(char*));
- memcpy(data, local_data, i*sizeof(char*));
- length = (int*)malloc(i*sizeof(int));
- memcpy(length, local_length, i*sizeof(int));
- }
- numlines = i;
- linelength = length;
- p = new Fl_Pixmap(data);
-}
-
-pixmap_image::~pixmap_image() {
- if (p && p->data()) {
- char** real_data = (char**)(p->data());
- for (int i = 0; i < numlines; i++) delete[] real_data[i];
- free((void*)real_data);
- }
- free((void*)linelength);
- delete p;
-}
-
-////////////////////////////////////////////////////////////////
-
-class gif_image : public pixmap_image {
-public:
- gif_image(const char *name, FILE *);
- ~gif_image();
- static int test_file(char *buffer);
-};
-
-int gif_image::test_file(char *buffer) {
- return !strncmp(buffer,"GIF",3);
-}
-
-// function in gif.C:
-int gif2xpm(
- const char *infname,// filename for error messages
- FILE *GifFile, // file to read
- char*** datap, // return xpm data here
- int** lengthp, // return line lengths here
- int inumber // which image in movie (0 = first)
-);
-
-gif_image::gif_image(const char *name, FILE *f) : pixmap_image(name,0) {
- char** datap;
- numlines = gif2xpm(name,f,&datap,&linelength,0);
- if (numlines) p = new Fl_Pixmap(datap);
- else p = 0;
-}
-
-gif_image::~gif_image() {
- if (p && p->data()) {
- char** real_data = (char**)(p->data());
- for (int i = 0; i < 3; i++) delete[] real_data[i];
- delete[] real_data;
-// p->data(0,0);
- }
-}
-
-////////////////////////////////////////////////////////////////
-#include <FL/Fl_Bitmap.H>
-
-class bitmap_image : public Fluid_Image {
- Fl_Bitmap *p;
-public:
- ~bitmap_image();
- bitmap_image(const char *name, FILE *);
- virtual void image(Fl_Widget *); // set the image of this widget
- virtual void deimage(Fl_Widget *); // set the deimage of this widget
- virtual void write_static();
- virtual void write_code(int inactive = 0);
- static int test_file(char *buffer);
-};
-
-// bad test, always do this last!
-int bitmap_image::test_file(char *buffer) {
- return (strstr(buffer,"#define ") != 0);
-}
-
-void bitmap_image::image(Fl_Widget *o) {
- o->image(p);
-}
-
-void bitmap_image::deimage(Fl_Widget *o) {
- o->deimage(p);
-}
-
-static int bitmap_header_written;
-
-void bitmap_image::write_static() {
- if (!p) return;
- write_c("\n");
- if (bitmap_header_written != write_number) {
- write_c("#include <FL/Fl_Bitmap.H>\n");
- bitmap_header_written = write_number;
- }
-#if 0 // older one
- write_c("static unsigned char %s[] = { \n",
- unique_id(this, "bits", filename_name(name()), 0));
- int n = ((p->w+7)/8)*p->h;
- int linelength = 0;
- for (int i = 0; i < n; i++) {
- if (i) {write_c(","); linelength++;}
- if (linelength > 75) {write_c("\n"); linelength=0;}
- int v = p->array[i];
- write_c("%d",v);
- linelength++; if (v>9) linelength++; if (v>99) linelength++;
- }
- write_c("\n};\n");
-#else // this seems to produce slightly shorter c++ files
- write_c("static unsigned char %s[] =\n",
- unique_id(this, "bits", filename_name(name()), 0));
- int n = ((p->w()+7)/8)*p->h();
- write_cstring((const char*)(p->array), n);
- write_c(";\n");
-#endif
- write_c("static Fl_Bitmap %s(%s, %d, %d);\n",
- unique_id(this, "bitmap", filename_name(name()), 0),
- unique_id(this, "bits", filename_name(name()), 0),
- p->w(), p->h());
-}
-
-void bitmap_image::write_code(int inactive) {
- if (!p) return;
- write_c("%so->%s(%s);\n", indent(), inactive ? "deimage" : "image",
- unique_id(this, "bitmap", filename_name(name()), 0));
-}
-
-bitmap_image::bitmap_image(const char *name, FILE *f) : Fluid_Image(name) {
- p = 0; // if any problems with parse we exit with this zero
- char buffer[1024];
- char junk[1024];
- int wh[2]; // width and height
- int i;
- for (i = 0; i<2; i++) {
- for (;;) {
- if (!fgets(buffer,1024,f)) return;
- int r = sscanf(buffer,"#define %s %d",junk,&wh[i]);
- if (r >= 2) break;
+ for (; i < img->count(); i ++) {
+ write_c(",\n");
+ write_cstring(img->data()[i], img->w() * chars_per_color);
}
- }
- // skip to data array:
- for (;;) {
- if (!fgets(buffer,1024,f)) return;
- if (!strncmp(buffer,"static ",7)) break;
- }
- int n = ((wh[0]+7)/8)*wh[1];
- uchar *data = new uchar[n];
- // read the data:
- i = 0;
- for (;i<n;) {
- if (!fgets(buffer,1024,f)) return;
- const char *a = buffer;
- while (*a && i<n) {
- int t;
- if (sscanf(a," 0x%x",&t)>0) data[i++] = t;
- while (*a && *a++ != ',');
+ write_c("\n};\n");
+ write_c("static Fl_Pixmap %s(%s);\n",
+ unique_id(this, "image", filename_name(name()), 0),
+ unique_id(this, "idata", filename_name(name()), 0));
+ } else if (img->d() == 0) {
+ // Write Bitmap data...
+ write_c("\n");
+ if (bitmap_header_written != write_number) {
+ write_c("#include <FL/Fl_Bitmap.H>\n");
+ bitmap_header_written = write_number;
}
+ write_c("static unsigned char %s[] =\n",
+ unique_id(this, "idata", filename_name(name()), 0));
+ write_cstring(img->data()[0], ((img->w() + 7) / 8) * img->h());
+ write_c(";\n");
+ write_c("static Fl_Bitmap %s(%s, %d, %d);\n",
+ unique_id(this, "image", filename_name(name()), 0),
+ unique_id(this, "idata", filename_name(name()), 0),
+ img->w(), img->h());
+ } else {
+ // Write image data...
+ write_c("\n");
+ if (image_header_written != write_number) {
+ write_c("#include <FL/Fl_Image.H>\n");
+ image_header_written = write_number;
+ }
+ write_c("static unsigned char %s[] =\n",
+ unique_id(this, "idata", filename_name(name()), 0));
+ write_cstring(img->data()[0], (img->w() * img->d() + img->ld()) * img->h());
+ write_c(";\n");
+ write_c("static Fl_RGB_Image %s(%s, %d, %d, %d, %d);\n",
+ unique_id(this, "image", filename_name(name()), 0),
+ unique_id(this, "idata", filename_name(name()), 0),
+ img->w(), img->h(), img->d(), img->ld());
}
- p = new Fl_Bitmap(data,wh[0],wh[1]);
}
-bitmap_image::~bitmap_image() {
- if (p) {
- delete[] (uchar*)(p->array);
- delete p;
- }
+void Fluid_Image::write_code(int inactive) {
+ if (!img) return;
+ write_c("%so->%s(%s);\n", indent(), inactive ? "deimage" : "image",
+ unique_id(this, "image", filename_name(name()), 0));
}
+
////////////////////////////////////////////////////////////////
-static Fluid_Image** images; // sorted list
-static int numimages;
-static int tablesize;
+static Fluid_Image** images = 0; // sorted list
+static int numimages = 0;
+static int tablesize = 0;
Fluid_Image* Fluid_Image::find(const char *name) {
if (!name || !*name) return 0;
@@ -374,28 +153,15 @@ Fluid_Image* Fluid_Image::find(const char *name) {
leave_source_dir();
return 0;
}
+ fclose(f);
- Fluid_Image *ret;
-
- // now see if we can identify the type, by reading in some data
- // and asking all the types we know about:
-
- char buffer[1025];
- fread(buffer, 1, 1024, f);
- rewind(f);
- buffer[1024] = 0; // null-terminate so strstr() works
+ Fluid_Image *ret = new Fluid_Image(name);
- if (pixmap_image::test_file(buffer)) {
- ret = new pixmap_image(name,f);
- } else if (gif_image::test_file(buffer)) {
- ret = new gif_image(name,f);
- } else if (bitmap_image::test_file(buffer)) {
- ret = new bitmap_image(name,f);
- } else {
+ if (!ret->img->w() || !ret->img->h()) {
+ delete ret;
ret = 0;
read_error("%s : unrecognized image format", name);
}
- fclose(f);
leave_source_dir();
if (!ret) return 0;
@@ -403,7 +169,8 @@ Fluid_Image* Fluid_Image::find(const char *name) {
numimages++;
if (numimages > tablesize) {
tablesize = tablesize ? 2*tablesize : 16;
- images = (Fluid_Image**)realloc(images, tablesize*sizeof(Fluid_Image*));
+ if (images) images = (Fluid_Image**)realloc(images, tablesize*sizeof(Fluid_Image*));
+ else images = (Fluid_Image**)malloc(tablesize*sizeof(Fluid_Image*));
}
for (b = numimages-1; b > a; b--) images[b] = images[b-1];
images[a] = ret;
@@ -415,6 +182,7 @@ Fluid_Image::Fluid_Image(const char *name) {
name_ = strdup(name);
written = 0;
refcount = 0;
+ img = Fl_Shared_Image::get(name);
}
void Fluid_Image::increment() {
@@ -432,6 +200,7 @@ Fluid_Image::~Fluid_Image() {
for (a = 0;; a++) if (images[a] == this) break;
numimages--;
for (; a < numimages; a++) images[a] = images[a+1];
+ img->release();
free((void*)name_);
}
@@ -442,13 +211,14 @@ Fluid_Image::~Fluid_Image() {
const char *ui_find_image_name;
Fluid_Image *ui_find_image(const char *oldname) {
goto_source_dir();
- const char *name = fl_file_chooser("Image","*.{bm|xbm|xpm|gif}",oldname);
+ const char *name = fl_file_chooser("Image?","*.{bm|gif|jpg|pbm|pgm|png|ppm|xbm|xpm}",oldname);
ui_find_image_name = name;
Fluid_Image *ret = (name && *name) ? Fluid_Image::find(name) : 0;
leave_source_dir();
return ret;
}
+
//
-// End of "$Id: Fluid_Image.cxx,v 1.7.2.9.2.4 2001/11/22 15:35:01 easysw Exp $".
+// End of "$Id: Fluid_Image.cxx,v 1.7.2.9.2.5 2001/11/25 22:51:34 easysw Exp $".
//
diff --git a/fluid/Fluid_Image.h b/fluid/Fluid_Image.h
index f513b8785..1b8694863 100644
--- a/fluid/Fluid_Image.h
+++ b/fluid/Fluid_Image.h
@@ -1,13 +1,11 @@
//
-// "$Id: Fluid_Image.h,v 1.3.2.4.2.1 2001/09/29 06:20:15 easysw Exp $"
+// "$Id: Fluid_Image.h,v 1.3.2.4.2.2 2001/11/25 22:51:34 easysw Exp $"
//
-// Pixmap image header file for the Fast Light Tool Kit (FLTK).
+// Image header file for the Fast Light Tool Kit (FLTK).
//
// This class stores the image labels for widgets in fluid. This is
-// not a class in fltk itself, and this will produce different types of
-// code depending on what the image type is. There are private subclasses
-// in Fluid_Image.C for each type of image format. Right now only xpm
-// files are supported.
+// not a class in FLTK itself, and will produce different types of
+// code depending on what the image type is.
//
// Copyright 1998-2001 by Bill Spitzak and others.
//
@@ -30,23 +28,27 @@
//
#ifndef FLUID_IMAGE_H
-#define FLUID_IMAGE_H
+# define FLUID_IMAGE_H
+
+# include <FL/Fl_Shared_Image.H>
+
class Fluid_Image {
const char *name_;
int refcount;
+ Fl_Shared_Image *img;
protected:
Fluid_Image(const char *name); // no public constructor
- virtual ~Fluid_Image(); // no public destructor
+ ~Fluid_Image(); // no public destructor
public:
int written;
static Fluid_Image* find(const char *);
void decrement(); // reference counting & automatic free
void increment();
- virtual void image(Fl_Widget *) = 0; // set the image of this widget
- virtual void deimage(Fl_Widget *) = 0; // set the deimage of this widget
- virtual void write_static() = 0;
- virtual void write_code(int inactive = 0) = 0;
+ void image(Fl_Widget *); // set the image of this widget
+ void deimage(Fl_Widget *); // set the deimage of this widget
+ void write_static();
+ void write_code(int inactive = 0);
const char *name() const {return name_;}
};
@@ -58,5 +60,5 @@ extern const char *ui_find_image_name;
#endif
//
-// End of "$Id: Fluid_Image.h,v 1.3.2.4.2.1 2001/09/29 06:20:15 easysw Exp $".
+// End of "$Id: Fluid_Image.h,v 1.3.2.4.2.2 2001/11/25 22:51:34 easysw Exp $".
//