1998-10-21 20:29:01 +04:00
|
|
|
//
|
2002-03-26 00:08:42 +03:00
|
|
|
// "$Id: Fluid_Image.cxx,v 1.7.2.9.2.8 2002/03/25 21:08:41 easysw Exp $"
|
1998-10-21 20:29:01 +04:00
|
|
|
//
|
|
|
|
// Pixmap label support for the Fast Light Tool Kit (FLTK).
|
|
|
|
//
|
2002-01-01 18:11:33 +03:00
|
|
|
// Copyright 1998-2002 by Bill Spitzak and others.
|
1998-10-21 20:29:01 +04:00
|
|
|
//
|
|
|
|
// 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.
|
|
|
|
//
|
2000-06-06 01:21:24 +04:00
|
|
|
// Please report all bugs and problems to "fltk-bugs@fltk.org".
|
1998-10-21 20:29:01 +04:00
|
|
|
//
|
1998-10-06 22:21:25 +04:00
|
|
|
|
|
|
|
#include <FL/Fl.H>
|
|
|
|
#include <FL/Fl_Widget.H>
|
1998-12-06 18:09:22 +03:00
|
|
|
#include "Fl_Type.h"
|
|
|
|
#include "Fluid_Image.h"
|
1998-10-06 22:21:25 +04:00
|
|
|
#include <string.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <ctype.h>
|
1999-03-04 21:45:31 +03:00
|
|
|
#include <stdlib.h>
|
1998-10-06 22:21:25 +04:00
|
|
|
#include <FL/filename.H>
|
|
|
|
|
|
|
|
extern void goto_source_dir(); // in fluid.C
|
|
|
|
extern void leave_source_dir(); // in fluid.C
|
|
|
|
|
2001-11-26 01:51:34 +03:00
|
|
|
void Fluid_Image::image(Fl_Widget *o) {
|
|
|
|
o->image(img);
|
1998-10-06 22:21:25 +04:00
|
|
|
}
|
|
|
|
|
2001-11-26 01:51:34 +03:00
|
|
|
void Fluid_Image::deimage(Fl_Widget *o) {
|
|
|
|
o->deimage(img);
|
1998-10-06 22:21:25 +04:00
|
|
|
}
|
|
|
|
|
2001-11-26 01:51:34 +03:00
|
|
|
static int pixmap_header_written = 0;
|
|
|
|
static int bitmap_header_written = 0;
|
|
|
|
static int image_header_written = 0;
|
2001-09-29 10:20:15 +04:00
|
|
|
|
2001-11-26 01:51:34 +03:00
|
|
|
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;
|
1998-10-06 22:21:25 +04:00
|
|
|
}
|
2001-11-26 01:51:34 +03:00
|
|
|
write_c("static const char *%s[] = {\n",
|
2002-03-26 00:08:42 +03:00
|
|
|
unique_id(this, "idata", fl_filename_name(name()), 0));
|
2001-11-26 01:51:34 +03:00
|
|
|
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]));
|
2000-06-16 11:08:16 +04:00
|
|
|
}
|
|
|
|
}
|
2001-11-26 01:51:34 +03:00
|
|
|
for (; i < img->count(); i ++) {
|
|
|
|
write_c(",\n");
|
|
|
|
write_cstring(img->data()[i], img->w() * chars_per_color);
|
1998-10-06 22:21:25 +04:00
|
|
|
}
|
2001-11-26 01:51:34 +03:00
|
|
|
write_c("\n};\n");
|
|
|
|
write_c("static Fl_Pixmap %s(%s);\n",
|
2002-03-26 00:08:42 +03:00
|
|
|
unique_id(this, "image", fl_filename_name(name()), 0),
|
|
|
|
unique_id(this, "idata", fl_filename_name(name()), 0));
|
2001-11-26 01:51:34 +03:00
|
|
|
} 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;
|
1998-10-06 22:21:25 +04:00
|
|
|
}
|
2001-11-26 01:51:34 +03:00
|
|
|
write_c("static unsigned char %s[] =\n",
|
2002-03-26 00:08:42 +03:00
|
|
|
unique_id(this, "idata", fl_filename_name(name()), 0));
|
2001-11-26 01:51:34 +03:00
|
|
|
write_cstring(img->data()[0], ((img->w() + 7) / 8) * img->h());
|
|
|
|
write_c(";\n");
|
|
|
|
write_c("static Fl_Bitmap %s(%s, %d, %d);\n",
|
2002-03-26 00:08:42 +03:00
|
|
|
unique_id(this, "image", fl_filename_name(name()), 0),
|
|
|
|
unique_id(this, "idata", fl_filename_name(name()), 0),
|
2001-11-26 01:51:34 +03:00
|
|
|
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",
|
2002-03-26 00:08:42 +03:00
|
|
|
unique_id(this, "idata", fl_filename_name(name()), 0));
|
2001-11-26 01:51:34 +03:00
|
|
|
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",
|
2002-03-26 00:08:42 +03:00
|
|
|
unique_id(this, "image", fl_filename_name(name()), 0),
|
|
|
|
unique_id(this, "idata", fl_filename_name(name()), 0),
|
2001-11-26 01:51:34 +03:00
|
|
|
img->w(), img->h(), img->d(), img->ld());
|
1998-10-06 22:21:25 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2001-11-26 01:51:34 +03:00
|
|
|
void Fluid_Image::write_code(int inactive) {
|
|
|
|
if (!img) return;
|
|
|
|
write_c("%so->%s(%s);\n", indent(), inactive ? "deimage" : "image",
|
2002-03-26 00:08:42 +03:00
|
|
|
unique_id(this, "image", fl_filename_name(name()), 0));
|
1998-10-06 22:21:25 +04:00
|
|
|
}
|
|
|
|
|
2001-11-26 01:51:34 +03:00
|
|
|
|
1998-10-06 22:21:25 +04:00
|
|
|
////////////////////////////////////////////////////////////////
|
|
|
|
|
2001-11-26 01:51:34 +03:00
|
|
|
static Fluid_Image** images = 0; // sorted list
|
|
|
|
static int numimages = 0;
|
|
|
|
static int tablesize = 0;
|
1998-10-06 22:21:25 +04:00
|
|
|
|
|
|
|
Fluid_Image* Fluid_Image::find(const char *name) {
|
|
|
|
if (!name || !*name) return 0;
|
|
|
|
|
|
|
|
// first search to see if it exists already:
|
|
|
|
int a = 0;
|
|
|
|
int b = numimages;
|
|
|
|
while (a < b) {
|
|
|
|
int c = (a+b)/2;
|
|
|
|
int i = strcmp(name,images[c]->name_);
|
|
|
|
if (i < 0) b = c;
|
|
|
|
else if (i > 0) a = c+1;
|
|
|
|
else return images[c];
|
|
|
|
}
|
|
|
|
|
|
|
|
// no, so now see if the file exists:
|
|
|
|
|
|
|
|
goto_source_dir();
|
|
|
|
FILE *f = fopen(name,"rb");
|
|
|
|
if (!f) {
|
|
|
|
read_error("%s : %s",name,strerror(errno));
|
|
|
|
leave_source_dir();
|
|
|
|
return 0;
|
|
|
|
}
|
2001-11-26 01:51:34 +03:00
|
|
|
fclose(f);
|
1998-10-06 22:21:25 +04:00
|
|
|
|
2001-11-26 01:51:34 +03:00
|
|
|
Fluid_Image *ret = new Fluid_Image(name);
|
1998-10-06 22:21:25 +04:00
|
|
|
|
2001-11-26 01:51:34 +03:00
|
|
|
if (!ret->img->w() || !ret->img->h()) {
|
|
|
|
delete ret;
|
1998-10-06 22:21:25 +04:00
|
|
|
ret = 0;
|
|
|
|
read_error("%s : unrecognized image format", name);
|
|
|
|
}
|
|
|
|
leave_source_dir();
|
|
|
|
if (!ret) return 0;
|
|
|
|
|
|
|
|
// make a new entry in the table:
|
|
|
|
numimages++;
|
|
|
|
if (numimages > tablesize) {
|
|
|
|
tablesize = tablesize ? 2*tablesize : 16;
|
2001-11-26 01:51:34 +03:00
|
|
|
if (images) images = (Fluid_Image**)realloc(images, tablesize*sizeof(Fluid_Image*));
|
|
|
|
else images = (Fluid_Image**)malloc(tablesize*sizeof(Fluid_Image*));
|
1998-10-06 22:21:25 +04:00
|
|
|
}
|
|
|
|
for (b = numimages-1; b > a; b--) images[b] = images[b-1];
|
|
|
|
images[a] = ret;
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
Fluid_Image::Fluid_Image(const char *name) {
|
|
|
|
name_ = strdup(name);
|
|
|
|
written = 0;
|
|
|
|
refcount = 0;
|
2001-11-26 01:51:34 +03:00
|
|
|
img = Fl_Shared_Image::get(name);
|
1998-10-06 22:21:25 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void Fluid_Image::increment() {
|
|
|
|
++refcount;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Fluid_Image::decrement() {
|
|
|
|
--refcount;
|
|
|
|
if (refcount > 0) return;
|
|
|
|
delete this;
|
|
|
|
}
|
|
|
|
|
|
|
|
Fluid_Image::~Fluid_Image() {
|
|
|
|
int a;
|
|
|
|
for (a = 0;; a++) if (images[a] == this) break;
|
|
|
|
numimages--;
|
|
|
|
for (; a < numimages; a++) images[a] = images[a+1];
|
2001-11-26 01:51:34 +03:00
|
|
|
img->release();
|
1998-10-06 22:21:25 +04:00
|
|
|
free((void*)name_);
|
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////
|
|
|
|
|
2001-09-30 21:37:06 +04:00
|
|
|
#include <FL/Fl_File_Chooser.H>
|
1998-10-06 22:21:25 +04:00
|
|
|
|
|
|
|
const char *ui_find_image_name;
|
|
|
|
Fluid_Image *ui_find_image(const char *oldname) {
|
|
|
|
goto_source_dir();
|
2002-01-06 20:51:12 +03:00
|
|
|
const char *name = fl_file_chooser("Image?","*.{bm|bmp|gif|jpg|pbm|pgm|png|ppm|xbm|xpm}",oldname);
|
1998-10-06 22:21:25 +04:00
|
|
|
ui_find_image_name = name;
|
|
|
|
Fluid_Image *ret = (name && *name) ? Fluid_Image::find(name) : 0;
|
|
|
|
leave_source_dir();
|
|
|
|
return ret;
|
|
|
|
}
|
1998-10-21 20:29:01 +04:00
|
|
|
|
2001-11-26 01:51:34 +03:00
|
|
|
|
1998-10-21 20:29:01 +04:00
|
|
|
//
|
2002-03-26 00:08:42 +03:00
|
|
|
// End of "$Id: Fluid_Image.cxx,v 1.7.2.9.2.8 2002/03/25 21:08:41 easysw Exp $".
|
1998-10-21 20:29:01 +04:00
|
|
|
//
|