1998-10-21 20:29:01 +04:00
|
|
|
//
|
2015-12-18 21:56:58 +03:00
|
|
|
// Pixmap (and other images) label support for the Fast Light Tool Kit (FLTK).
|
1998-10-21 20:29:01 +04:00
|
|
|
//
|
2022-11-15 12:09:01 +03:00
|
|
|
// Copyright 1998-2022 by Bill Spitzak and others.
|
1998-10-21 20:29:01 +04:00
|
|
|
//
|
2011-07-19 08:49:30 +04:00
|
|
|
// This library is free software. Distribution and use rights are outlined in
|
|
|
|
// the file "COPYING" which should have been included with this file. If this
|
|
|
|
// file is missing or damaged, see the license at:
|
|
|
|
//
|
2020-07-01 19:03:10 +03:00
|
|
|
// https://www.fltk.org/COPYING.php
|
1998-10-21 20:29:01 +04:00
|
|
|
//
|
2020-07-01 19:03:10 +03:00
|
|
|
// Please see the following page on how to report bugs and issues:
|
2005-04-16 04:13:17 +04:00
|
|
|
//
|
2020-07-01 19:03:10 +03:00
|
|
|
// https://www.fltk.org/bugs.php
|
1998-10-21 20:29:01 +04:00
|
|
|
//
|
1998-10-06 22:21:25 +04:00
|
|
|
|
2021-12-08 17:52:15 +03:00
|
|
|
#include "Fluid_Image.h"
|
|
|
|
|
|
|
|
#include "fluid.h"
|
|
|
|
#include "Fl_Group_Type.h"
|
|
|
|
#include "Fl_Window_Type.h"
|
|
|
|
#include "file.h"
|
|
|
|
#include "code.h"
|
|
|
|
|
1998-10-06 22:21:25 +04:00
|
|
|
#include <FL/Fl.H>
|
|
|
|
#include <FL/Fl_Widget.H>
|
2021-12-08 17:52:15 +03:00
|
|
|
#include <FL/Fl_Window.H>
|
2023-10-22 20:30:37 +03:00
|
|
|
#include "fluid_filename.h"
|
2022-01-16 21:02:36 +03:00
|
|
|
#include <FL/fl_string_functions.h>
|
2021-03-20 23:39:28 +03:00
|
|
|
#include <FL/fl_utf8.h> // fl_fopen()
|
2021-12-08 17:52:15 +03:00
|
|
|
#include <FL/Fl_File_Chooser.H>
|
2022-12-30 21:20:52 +03:00
|
|
|
#include <FL/Fl_SVG_Image.H>
|
2023-01-21 19:27:58 +03:00
|
|
|
#include <FL/Fl_Anim_GIF_Image.H>
|
2002-05-16 16:47:44 +04:00
|
|
|
#include "../src/flstring.h"
|
2021-12-08 17:52:15 +03:00
|
|
|
|
1998-10-06 22:21:25 +04:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <errno.h>
|
1999-03-04 21:45:31 +03:00
|
|
|
#include <stdlib.h>
|
2015-12-18 21:56:58 +03:00
|
|
|
#include <stdarg.h>
|
1998-10-06 22:21:25 +04:00
|
|
|
|
2001-11-26 01:51:34 +03:00
|
|
|
void Fluid_Image::image(Fl_Widget *o) {
|
2005-03-20 03:51:02 +03:00
|
|
|
if (o->window() != 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) {
|
2005-03-20 03:51:02 +03:00
|
|
|
if (o->window() != o) o->deimage(img);
|
1998-10-06 22:21:25 +04:00
|
|
|
}
|
|
|
|
|
2023-01-01 19:09:07 +03:00
|
|
|
/** Write the contents of the name() file as binary source code.
|
|
|
|
\param fmt short name of file contents for error message
|
|
|
|
\return 0 if the file could not be opened or read */
|
2023-01-26 17:23:43 +03:00
|
|
|
size_t Fluid_Image::write_static_binary(Fd_Code_Writer& f, const char* fmt) {
|
2022-12-30 21:20:52 +03:00
|
|
|
size_t nData = 0;
|
|
|
|
enter_project_dir();
|
2023-01-26 17:23:43 +03:00
|
|
|
FILE *in = fl_fopen(name(), "rb");
|
2022-12-30 21:20:52 +03:00
|
|
|
leave_project_dir();
|
2023-01-26 17:23:43 +03:00
|
|
|
if (!in) {
|
|
|
|
write_file_error(f, fmt);
|
2023-01-01 19:09:07 +03:00
|
|
|
return 0;
|
2022-12-30 21:20:52 +03:00
|
|
|
} else {
|
2023-01-26 17:23:43 +03:00
|
|
|
fseek(in, 0, SEEK_END);
|
|
|
|
nData = ftell(in);
|
|
|
|
fseek(in, 0, SEEK_SET);
|
2022-12-30 21:20:52 +03:00
|
|
|
if (nData) {
|
|
|
|
char *data = (char*)calloc(nData, 1);
|
2023-01-26 17:23:43 +03:00
|
|
|
if (fread(data, nData, 1, in)==0) { /* ignore */ }
|
|
|
|
f.write_cdata(data, (int)nData);
|
2022-12-30 21:20:52 +03:00
|
|
|
free(data);
|
|
|
|
}
|
2023-01-26 17:23:43 +03:00
|
|
|
fclose(in);
|
2022-12-30 21:20:52 +03:00
|
|
|
}
|
|
|
|
return nData;
|
|
|
|
}
|
|
|
|
|
2023-01-01 19:09:07 +03:00
|
|
|
/** Write the contents of the name() file as textual source code.
|
|
|
|
\param fmt short name of file contents for error message
|
|
|
|
\return 0 if the file could not be opened or read */
|
2023-01-26 17:23:43 +03:00
|
|
|
size_t Fluid_Image::write_static_text(Fd_Code_Writer& f, const char* fmt) {
|
2022-12-30 21:20:52 +03:00
|
|
|
size_t nData = 0;
|
|
|
|
enter_project_dir();
|
2023-01-26 17:23:43 +03:00
|
|
|
FILE *in = fl_fopen(name(), "rb");
|
2022-12-30 21:20:52 +03:00
|
|
|
leave_project_dir();
|
2023-01-26 17:23:43 +03:00
|
|
|
if (!in) {
|
|
|
|
write_file_error(f, fmt);
|
2023-01-01 19:09:07 +03:00
|
|
|
return 0;
|
2022-12-30 21:20:52 +03:00
|
|
|
} else {
|
2023-01-26 17:23:43 +03:00
|
|
|
fseek(in, 0, SEEK_END);
|
|
|
|
nData = ftell(in);
|
|
|
|
fseek(in, 0, SEEK_SET);
|
2022-12-30 21:20:52 +03:00
|
|
|
if (nData) {
|
|
|
|
char *data = (char*)calloc(nData+1, 1);
|
2023-01-26 17:23:43 +03:00
|
|
|
if (fread(data, nData, 1, in)==0) { /* ignore */ }
|
|
|
|
f.write_cstring(data, (int)nData);
|
2022-12-30 21:20:52 +03:00
|
|
|
free(data);
|
|
|
|
}
|
2023-01-26 17:23:43 +03:00
|
|
|
fclose(in);
|
2022-12-30 21:20:52 +03:00
|
|
|
}
|
|
|
|
return nData;
|
|
|
|
}
|
|
|
|
|
2023-01-26 17:23:43 +03:00
|
|
|
void Fluid_Image::write_static_rgb(Fd_Code_Writer& f, const char* idata_name) {
|
2022-12-30 21:20:52 +03:00
|
|
|
// Write image data...
|
2023-01-26 17:23:43 +03:00
|
|
|
f.write_c("\n");
|
|
|
|
f.write_c_once("#include <FL/Fl_Image.H>\n");
|
|
|
|
f.write_c("static const unsigned char %s[] =\n", idata_name);
|
2022-12-30 21:20:52 +03:00
|
|
|
const int extra_data = img->ld() ? (img->ld()-img->w()*img->d()) : 0;
|
2023-01-26 17:23:43 +03:00
|
|
|
f.write_cdata(img->data()[0], (img->w() * img->d() + extra_data) * img->h());
|
|
|
|
f.write_c(";\n");
|
|
|
|
write_initializer(f, "Fl_RGB_Image", "%s, %d, %d, %d, %d", idata_name, img->w(), img->h(), img->d(), img->ld());
|
2022-12-30 21:20:52 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
Write the static image data into the soutrce file.
|
|
|
|
|
|
|
|
If \p compressed is set, write the original image format, which requires
|
|
|
|
linking the matching image reader at runtime, or if we want to store the raw
|
|
|
|
uncompressed pixels, which makes images fast, needs no reader, but takes a
|
|
|
|
lot of memory (current default for PNG)
|
|
|
|
|
|
|
|
\param compressed write data in the original compressed file format
|
|
|
|
*/
|
2023-01-26 17:23:43 +03:00
|
|
|
void Fluid_Image::write_static(Fd_Code_Writer& f, int compressed) {
|
2001-11-26 01:51:34 +03:00
|
|
|
if (!img) return;
|
2023-01-26 17:23:43 +03:00
|
|
|
const char *idata_name = f.unique_id(this, "idata", fl_filename_name(name()), 0);
|
|
|
|
function_name_ = f.unique_id(this, "image", fl_filename_name(name()), 0);
|
2023-02-02 19:03:09 +03:00
|
|
|
|
2023-01-21 19:27:58 +03:00
|
|
|
if (is_animated_gif_) {
|
|
|
|
// Write animated gif image data...
|
2023-01-26 17:23:43 +03:00
|
|
|
f.write_c("\n");
|
|
|
|
f.write_c_once("#include <FL/Fl_Anim_GIF_Image.H>\n");
|
|
|
|
f.write_c("static const unsigned char %s[] =\n", idata_name);
|
|
|
|
size_t nData = write_static_binary(f, "AnimGIF");
|
|
|
|
f.write_c(";\n");
|
|
|
|
write_initializer(f, "Fl_Anim_GIF_Image", "\"%s\", %s, %d", fl_filename_name(name()), idata_name, nData);
|
2023-01-21 19:27:58 +03:00
|
|
|
} else if (compressed && fl_ascii_strcasecmp(fl_filename_ext(name()), ".gif")==0) {
|
2022-12-30 21:20:52 +03:00
|
|
|
// Write gif image data...
|
2023-01-26 17:23:43 +03:00
|
|
|
f.write_c("\n");
|
|
|
|
f.write_c_once("#include <FL/Fl_GIF_Image.H>\n");
|
|
|
|
f.write_c("static const unsigned char %s[] =\n", idata_name);
|
|
|
|
size_t nData = write_static_binary(f, "GIF");
|
|
|
|
f.write_c(";\n");
|
|
|
|
write_initializer(f, "Fl_GIF_Image", "\"%s\", %s, %d", fl_filename_name(name()), idata_name, nData);
|
2023-01-21 19:27:58 +03:00
|
|
|
} else if (compressed && fl_ascii_strcasecmp(fl_filename_ext(name()), ".bmp")==0) {
|
2022-12-30 21:20:52 +03:00
|
|
|
// Write bmp image data...
|
2023-01-26 17:23:43 +03:00
|
|
|
f.write_c("\n");
|
|
|
|
f.write_c_once("#include <FL/Fl_BMP_Image.H>\n");
|
|
|
|
f.write_c("static const unsigned char %s[] =\n", idata_name);
|
|
|
|
size_t nData = write_static_binary(f, "BMP");
|
|
|
|
f.write_c(";\n");
|
|
|
|
write_initializer(f, "Fl_BMP_Image", "\"%s\", %s, %d", fl_filename_name(name()), idata_name, nData);
|
2022-12-30 21:20:52 +03:00
|
|
|
} else if (img->count() > 1) {
|
2001-11-26 01:51:34 +03:00
|
|
|
// Write Pixmap data...
|
2023-01-26 17:23:43 +03:00
|
|
|
f.write_c("\n");
|
|
|
|
f.write_c_once("#include <FL/Fl_Pixmap.H>\n");
|
|
|
|
f.write_c("static const char *%s[] = {\n", idata_name);
|
|
|
|
f.write_cstring(img->data()[0], (int)strlen(img->data()[0]));
|
2001-11-26 01:51:34 +03:00
|
|
|
|
|
|
|
int i;
|
|
|
|
int ncolors, chars_per_color;
|
|
|
|
sscanf(img->data()[0], "%*d%*d%d%d", &ncolors, &chars_per_color);
|
|
|
|
|
|
|
|
if (ncolors < 0) {
|
2023-01-26 17:23:43 +03:00
|
|
|
f.write_c(",\n");
|
|
|
|
f.write_cstring(img->data()[1], ncolors * -4);
|
2001-11-26 01:51:34 +03:00
|
|
|
i = 2;
|
|
|
|
} else {
|
|
|
|
for (i = 1; i <= ncolors; i ++) {
|
2023-01-26 17:23:43 +03:00
|
|
|
f.write_c(",\n");
|
|
|
|
f.write_cstring(img->data()[i], (int)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 ++) {
|
2023-01-26 17:23:43 +03:00
|
|
|
f.write_c(",\n");
|
|
|
|
f.write_cstring(img->data()[i], img->w() * chars_per_color);
|
1998-10-06 22:21:25 +04:00
|
|
|
}
|
2023-01-26 17:23:43 +03:00
|
|
|
f.write_c("\n};\n");
|
|
|
|
write_initializer(f, "Fl_Pixmap", "%s", idata_name);
|
2001-11-26 01:51:34 +03:00
|
|
|
} else if (img->d() == 0) {
|
|
|
|
// Write Bitmap data...
|
2023-01-26 17:23:43 +03:00
|
|
|
f.write_c("\n");
|
|
|
|
f.write_c_once("#include <FL/Fl_Bitmap.H>\n");
|
|
|
|
f.write_c("static const unsigned char %s[] =\n", idata_name);
|
|
|
|
f.write_cdata(img->data()[0], ((img->w() + 7) / 8) * img->h());
|
|
|
|
f.write_c(";\n");
|
|
|
|
write_initializer(f, "Fl_Bitmap", "%s, %d, %d, %d", idata_name, ((img->w() + 7) / 8) * img->h(), img->w(), img->h());
|
2023-01-21 19:27:58 +03:00
|
|
|
} else if (compressed && fl_ascii_strcasecmp(fl_filename_ext(name()), ".jpg")==0) {
|
2010-02-18 14:38:42 +03:00
|
|
|
// Write jpeg image data...
|
2023-01-26 17:23:43 +03:00
|
|
|
f.write_c("\n");
|
|
|
|
f.write_c_once("#include <FL/Fl_JPEG_Image.H>\n");
|
|
|
|
f.write_c("static const unsigned char %s[] =\n", idata_name);
|
|
|
|
size_t nData = write_static_binary(f, "JPEG");
|
|
|
|
f.write_c(";\n");
|
|
|
|
write_initializer(f, "Fl_JPEG_Image", "\"%s\", %s, %d", fl_filename_name(name()), idata_name, nData);
|
2023-01-21 19:27:58 +03:00
|
|
|
} else if (compressed && fl_ascii_strcasecmp(fl_filename_ext(name()), ".png")==0) {
|
2022-12-30 21:20:52 +03:00
|
|
|
// Write png image data...
|
2023-01-26 17:23:43 +03:00
|
|
|
f.write_c("\n");
|
|
|
|
f.write_c_once("#include <FL/Fl_PNG_Image.H>\n");
|
|
|
|
f.write_c("static const unsigned char %s[] =\n", idata_name);
|
|
|
|
size_t nData = write_static_binary(f, "PNG");
|
|
|
|
f.write_c(";\n");
|
|
|
|
write_initializer(f, "Fl_PNG_Image", "\"%s\", %s, %d", fl_filename_name(name()), idata_name, nData);
|
2023-06-12 17:55:05 +03:00
|
|
|
}
|
|
|
|
#ifdef FLTK_USE_SVG
|
|
|
|
else if (fl_ascii_strcasecmp(fl_filename_ext(name()), ".svg")==0 || fl_ascii_strcasecmp(fl_filename_ext(name()), ".svgz")==0) {
|
2022-11-15 12:09:01 +03:00
|
|
|
bool gzipped = (strcmp(fl_filename_ext(name()), ".svgz") == 0);
|
2022-11-04 15:30:09 +03:00
|
|
|
// Write svg image data...
|
2022-12-30 21:20:52 +03:00
|
|
|
if (compressed) {
|
2023-01-26 17:23:43 +03:00
|
|
|
f.write_c("\n");
|
|
|
|
f.write_c_once("#include <FL/Fl_SVG_Image.H>\n");
|
2022-12-30 21:20:52 +03:00
|
|
|
if (gzipped) {
|
2023-01-26 17:23:43 +03:00
|
|
|
f.write_c("static const unsigned char %s[] =\n", idata_name);
|
|
|
|
size_t nData = write_static_binary(f, "SVGZ");
|
|
|
|
f.write_c(";\n");
|
|
|
|
write_initializer(f, "Fl_SVG_Image", "\"%s\", %s, %ld", fl_filename_name(name()), idata_name, nData);
|
2022-12-30 21:20:52 +03:00
|
|
|
} else {
|
2023-01-26 17:23:43 +03:00
|
|
|
f.write_c("static const char %s[] =\n", idata_name);
|
|
|
|
write_static_text(f, "SVG");
|
|
|
|
f.write_c(";\n");
|
|
|
|
write_initializer(f, "Fl_SVG_Image", "\"%s\", %s", fl_filename_name(name()), idata_name);
|
2022-12-30 21:20:52 +03:00
|
|
|
}
|
2022-11-04 15:30:09 +03:00
|
|
|
} else {
|
2022-12-30 21:20:52 +03:00
|
|
|
// if FLUID runs from the command line, make sure that the image is not
|
2023-01-21 19:27:58 +03:00
|
|
|
// only loaded but also rasterized, so we can write the RGB image data
|
2022-12-30 21:20:52 +03:00
|
|
|
Fl_RGB_Image* rgb_image = NULL;
|
|
|
|
Fl_SVG_Image* svg_image = NULL;
|
|
|
|
if (img->d()>0)
|
|
|
|
rgb_image = (Fl_RGB_Image*)img->image();
|
|
|
|
if (rgb_image)
|
|
|
|
svg_image = rgb_image->as_svg_image();
|
|
|
|
if (svg_image) {
|
|
|
|
svg_image->resize(svg_image->w(), svg_image->h());
|
2023-01-26 17:23:43 +03:00
|
|
|
write_static_rgb(f, idata_name);
|
2022-12-30 21:20:52 +03:00
|
|
|
} else {
|
2023-01-26 17:23:43 +03:00
|
|
|
write_file_error(f, "RGB_from_SVG");
|
2022-11-04 15:30:09 +03:00
|
|
|
}
|
|
|
|
}
|
2023-06-12 17:55:05 +03:00
|
|
|
}
|
|
|
|
#endif // FLTK_USE_SVG
|
|
|
|
else {
|
2023-01-26 17:23:43 +03:00
|
|
|
write_static_rgb(f, idata_name);
|
1998-10-06 22:21:25 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-26 17:23:43 +03:00
|
|
|
void Fluid_Image::write_file_error(Fd_Code_Writer& f, const char *fmt) {
|
|
|
|
f.write_c("#warning Cannot read %s file \"%s\": %s\n", fmt, name(), strerror(errno));
|
2022-11-15 19:48:06 +03:00
|
|
|
enter_project_dir();
|
2023-01-26 17:23:43 +03:00
|
|
|
f.write_c("// Searching in path \"%s\"\n", fl_getcwd(0, FL_PATH_MAX));
|
2022-11-15 19:48:06 +03:00
|
|
|
leave_project_dir();
|
2022-11-10 01:12:55 +03:00
|
|
|
}
|
|
|
|
|
2023-01-26 17:23:43 +03:00
|
|
|
void Fluid_Image::write_initializer(Fd_Code_Writer& f, const char *type_name, const char *format, ...) {
|
2015-12-18 21:56:58 +03:00
|
|
|
/* Outputs code that returns (and initializes if needed) an Fl_Image as follows:
|
|
|
|
static Fl_Image *'function_name_'() {
|
2022-12-17 18:01:35 +03:00
|
|
|
static Fl_Image *image = NULL;
|
|
|
|
if (!image)
|
|
|
|
image = new 'type_name'('product of format and remaining args');
|
2015-12-18 21:56:58 +03:00
|
|
|
return image;
|
|
|
|
} */
|
|
|
|
va_list ap;
|
|
|
|
va_start(ap, format);
|
2023-01-26 17:23:43 +03:00
|
|
|
f.write_c("static Fl_Image *%s() {\n", function_name_);
|
2023-01-21 19:27:58 +03:00
|
|
|
if (is_animated_gif_)
|
2023-01-26 17:23:43 +03:00
|
|
|
f.write_c("%sFl_GIF_Image::animate = true;\n", f.indent(1));
|
|
|
|
f.write_c("%sstatic Fl_Image *image = NULL;\n", f.indent(1));
|
|
|
|
f.write_c("%sif (!image)\n", f.indent(1));
|
|
|
|
f.write_c("%simage = new %s(", f.indent(2), type_name);
|
|
|
|
f.vwrite_c(format, ap);
|
|
|
|
f.write_c(");\n");
|
|
|
|
f.write_c("%sreturn image;\n", f.indent(1));
|
|
|
|
f.write_c("}\n");
|
2015-12-18 21:56:58 +03:00
|
|
|
va_end(ap);
|
|
|
|
}
|
|
|
|
|
2023-01-26 17:23:43 +03:00
|
|
|
void Fluid_Image::write_code(Fd_Code_Writer& f, int bind, const char *var, int inactive) {
|
2015-12-18 21:56:58 +03:00
|
|
|
/* Outputs code that attaches an image to an Fl_Widget or Fl_Menu_Item.
|
|
|
|
This code calls a function output before by Fluid_Image::write_initializer() */
|
2023-01-21 19:27:58 +03:00
|
|
|
if (img) {
|
2023-01-26 17:23:43 +03:00
|
|
|
f.write_c("%s%s->%s%s( %s() );\n", f.indent(), var, bind ? "bind_" : "", inactive ? "deimage" : "image", function_name_);
|
2023-01-21 19:27:58 +03:00
|
|
|
if (is_animated_gif_)
|
2023-01-26 17:23:43 +03:00
|
|
|
f.write_c("%s((Fl_Anim_GIF_Image*)(%s()))->canvas(%s, Fl_Anim_GIF_Image::DONT_RESIZE_CANVAS);\n", f.indent(), function_name_, var);
|
2023-01-21 19:27:58 +03:00
|
|
|
}
|
1998-10-06 22:21:25 +04:00
|
|
|
}
|
|
|
|
|
2023-01-26 17:23:43 +03:00
|
|
|
void Fluid_Image::write_inline(Fd_Code_Writer& f, int inactive) {
|
2021-12-19 03:09:13 +03:00
|
|
|
if (img)
|
2023-01-26 17:23:43 +03:00
|
|
|
f.write_c("%s()", function_name_);
|
2021-12-19 03:09:13 +03: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
|
|
|
|
2002-08-10 02:57:00 +04:00
|
|
|
Fluid_Image* Fluid_Image::find(const char *iname) {
|
|
|
|
if (!iname || !*iname) return 0;
|
1998-10-06 22:21:25 +04:00
|
|
|
|
|
|
|
// first search to see if it exists already:
|
|
|
|
int a = 0;
|
|
|
|
int b = numimages;
|
|
|
|
while (a < b) {
|
|
|
|
int c = (a+b)/2;
|
2002-08-10 02:57:00 +04:00
|
|
|
int i = strcmp(iname,images[c]->name_);
|
1998-10-06 22:21:25 +04:00
|
|
|
if (i < 0) b = c;
|
|
|
|
else if (i > 0) a = c+1;
|
|
|
|
else return images[c];
|
|
|
|
}
|
|
|
|
|
|
|
|
// no, so now see if the file exists:
|
|
|
|
|
2022-11-15 19:48:06 +03:00
|
|
|
enter_project_dir();
|
2010-11-18 23:00:01 +03:00
|
|
|
FILE *f = fl_fopen(iname,"rb");
|
1998-10-06 22:21:25 +04:00
|
|
|
if (!f) {
|
2023-01-26 17:23:43 +03:00
|
|
|
if (batch_mode)
|
|
|
|
fprintf(stderr, "Can't open image file:\n%s\n%s",iname,strerror(errno));
|
|
|
|
else
|
|
|
|
fl_message("Can't open image file:\n%s\n%s",iname,strerror(errno));
|
2022-11-15 19:48:06 +03:00
|
|
|
leave_project_dir();
|
1998-10-06 22:21:25 +04:00
|
|
|
return 0;
|
|
|
|
}
|
2001-11-26 01:51:34 +03:00
|
|
|
fclose(f);
|
1998-10-06 22:21:25 +04:00
|
|
|
|
2002-08-10 02:57:00 +04:00
|
|
|
Fluid_Image *ret = new Fluid_Image(iname);
|
1998-10-06 22:21:25 +04:00
|
|
|
|
2002-08-17 00:10:21 +04:00
|
|
|
if (!ret->img || !ret->img->w() || !ret->img->h()) {
|
2001-11-26 01:51:34 +03:00
|
|
|
delete ret;
|
1998-10-06 22:21:25 +04:00
|
|
|
ret = 0;
|
2023-01-26 17:23:43 +03:00
|
|
|
if (batch_mode)
|
|
|
|
fprintf(stderr, "Can't read image file:\n%s\nunrecognized image format",iname);
|
|
|
|
else
|
|
|
|
fl_message("Can't read image file:\n%s\nunrecognized image format",iname);
|
1998-10-06 22:21:25 +04:00
|
|
|
}
|
2022-11-15 19:48:06 +03:00
|
|
|
leave_project_dir();
|
1998-10-06 22:21:25 +04:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2023-01-21 19:27:58 +03:00
|
|
|
Fluid_Image::Fluid_Image(const char *iname)
|
|
|
|
: is_animated_gif_(false)
|
|
|
|
{
|
2020-07-22 06:15:41 +03:00
|
|
|
name_ = fl_strdup(iname);
|
1998-10-06 22:21:25 +04:00
|
|
|
written = 0;
|
|
|
|
refcount = 0;
|
2002-08-10 02:57:00 +04:00
|
|
|
img = Fl_Shared_Image::get(iname);
|
2023-01-21 19:27:58 +03:00
|
|
|
if (img && iname) {
|
|
|
|
const char *ext = fl_filename_ext(iname);
|
|
|
|
if (fl_ascii_strcasecmp(ext, ".gif")==0) {
|
|
|
|
int fc = Fl_Anim_GIF_Image::frame_count(iname);
|
|
|
|
if (fc > 0) is_animated_gif_ = true;
|
|
|
|
}
|
|
|
|
}
|
2015-12-18 21:56:58 +03:00
|
|
|
function_name_ = NULL;
|
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;
|
2002-08-17 00:10:21 +04:00
|
|
|
if (images) {
|
2022-12-17 18:01:35 +03:00
|
|
|
for (a = 0; a<numimages; a++) {
|
|
|
|
if (images[a] == this) {
|
|
|
|
numimages--;
|
|
|
|
for (; a < numimages; a++) {
|
|
|
|
images[a] = images[a+1];
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2002-08-17 00:10:21 +04:00
|
|
|
}
|
|
|
|
if (img) img->release();
|
1998-10-06 22:21:25 +04:00
|
|
|
free((void*)name_);
|
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
const char *ui_find_image_name;
|
|
|
|
Fluid_Image *ui_find_image(const char *oldname) {
|
2022-11-15 19:48:06 +03:00
|
|
|
enter_project_dir();
|
2005-03-25 21:58:18 +03:00
|
|
|
fl_file_chooser_ok_label("Use Image");
|
2022-11-15 12:09:01 +03:00
|
|
|
const char *name = fl_file_chooser("Image?",
|
|
|
|
"Image Files (*.{bm,bmp,gif,jpg,pbm,pgm,png,ppm,xbm,xpm,svg"
|
|
|
|
#ifdef HAVE_LIBZ
|
|
|
|
",svgz"
|
|
|
|
#endif
|
|
|
|
"})",
|
|
|
|
oldname,1);
|
2005-03-25 21:58:18 +03:00
|
|
|
fl_file_chooser_ok_label(NULL);
|
1998-10-06 22:21:25 +04:00
|
|
|
ui_find_image_name = name;
|
|
|
|
Fluid_Image *ret = (name && *name) ? Fluid_Image::find(name) : 0;
|
2022-11-15 19:48:06 +03:00
|
|
|
leave_project_dir();
|
1998-10-06 22:21:25 +04:00
|
|
|
return ret;
|
|
|
|
}
|