fltk  1.3.5-source
About: FLTK (Fast Light Tool Kit) is a cross-platform C++ GUI toolkit for UNIX/Linux (X11), Microsoft Windows, and MacOS X.
  Fossies Dox: fltk-1.3.5-source.tar.bz2  ("inofficial" and yet experimental doxygen-generated source code documentation)  

Fluid_Image.cxx
Go to the documentation of this file.
1 //
2 // "$Id$"
3 //
4 // Pixmap (and other images) label support for the Fast Light Tool Kit (FLTK).
5 //
6 // Copyright 1998-2016 by Bill Spitzak and others.
7 //
8 // This library is free software. Distribution and use rights are outlined in
9 // the file "COPYING" which should have been included with this file. If this
10 // file is missing or damaged, see the license at:
11 //
12 // http://www.fltk.org/COPYING.php
13 //
14 // Please report all bugs and problems on the following page:
15 //
16 // http://www.fltk.org/str.php
17 //
18 
19 #include <FL/Fl.H>
20 #include <FL/Fl_Widget.H>
21 #include "Fl_Type.h"
22 #include "Fluid_Image.h"
23 #include "../src/flstring.h"
24 #include <stdio.h>
25 #include <errno.h>
26 #include <stdlib.h>
27 #include <stdarg.h>
28 #include <FL/filename.H>
29 
30 extern void goto_source_dir(); // in fluid.cxx
31 extern void leave_source_dir(); // in fluid.cxx
32 
34  if (o->window() != o) o->image(img);
35 }
36 
38  if (o->window() != o) o->deimage(img);
39 }
40 
41 static int pixmap_header_written = 0;
42 static int bitmap_header_written = 0;
43 static int image_header_written = 0;
44 static int jpeg_header_written = 0;
45 
47  if (!img) return;
48  const char *idata_name = unique_id(this, "idata", fl_filename_name(name()), 0);
49  function_name_ = unique_id(this, "image", fl_filename_name(name()), 0);
50  if (img->count() > 1) {
51  // Write Pixmap data...
52  write_c("\n");
54  write_c("#include <FL/Fl_Pixmap.H>\n");
56  }
57  write_c("static const char *%s[] = {\n", idata_name);
58  write_cstring(img->data()[0], strlen(img->data()[0]));
59 
60  int i;
61  int ncolors, chars_per_color;
62  sscanf(img->data()[0], "%*d%*d%d%d", &ncolors, &chars_per_color);
63 
64  if (ncolors < 0) {
65  write_c(",\n");
66  write_cstring(img->data()[1], ncolors * -4);
67  i = 2;
68  } else {
69  for (i = 1; i <= ncolors; i ++) {
70  write_c(",\n");
71  write_cstring(img->data()[i], strlen(img->data()[i]));
72  }
73  }
74  for (; i < img->count(); i ++) {
75  write_c(",\n");
76  write_cstring(img->data()[i], img->w() * chars_per_color);
77  }
78  write_c("\n};\n");
79  write_initializer("Fl_Pixmap", "%s", idata_name);
80  } else if (img->d() == 0) {
81  // Write Bitmap data...
82  write_c("\n");
84  write_c("#include <FL/Fl_Bitmap.H>\n");
86  }
87  write_c("static const unsigned char %s[] =\n", idata_name);
88  write_cdata(img->data()[0], ((img->w() + 7) / 8) * img->h());
89  write_c(";\n");
90  write_initializer( "Fl_Bitmap", "%s, %d, %d", idata_name, img->w(), img->h());
91  } else if (strcmp(fl_filename_ext(name()), ".jpg")==0) {
92  // Write jpeg image data...
93  write_c("\n");
95  write_c("#include <FL/Fl_JPEG_Image.H>\n");
97  }
98  write_c("static const unsigned char %s[] =\n", idata_name);
99 
100  FILE *f = fl_fopen(name(), "rb");
101  if (!f) {
102  // message = "Can't include binary file. Can't open";
103  } else {
104  fseek(f, 0, SEEK_END);
105  size_t nData = ftell(f);
106  fseek(f, 0, SEEK_SET);
107  if (nData) {
108  char *data = (char*)calloc(nData, 1);
109  if (fread(data, nData, 1, f)==0) { /* ignore */ }
110  write_cdata(data, nData);
111  free(data);
112  }
113  fclose(f);
114  }
115 
116  write_c(";\n");
117  write_initializer("Fl_JPEG_Image", "\"%s\", %s", fl_filename_name(name()), idata_name);
118  } else {
119  // Write image data...
120  write_c("\n");
122  write_c("#include <FL/Fl_Image.H>\n");
124  }
125  write_c("static const unsigned char %s[] =\n", idata_name);
126  const int extra_data = img->ld() ? (img->ld()-img->w()*img->d()) : 0;
127  write_cdata(img->data()[0], (img->w() * img->d() + extra_data) * img->h());
128  write_c(";\n");
129  write_initializer("Fl_RGB_Image", "%s, %d, %d, %d, %d", idata_name, img->w(), img->h(), img->d(), img->ld());
130  }
131 }
132 
133 void Fluid_Image::write_initializer(const char *type_name, const char *format, ...) {
134  /* Outputs code that returns (and initializes if needed) an Fl_Image as follows:
135  static Fl_Image *'function_name_'() {
136  static Fl_Image *image = new 'type_name'('product of format and remaining args');
137  return image;
138  } */
139  va_list ap;
140  va_start(ap, format);
141  write_c("static Fl_Image *%s() {\n static Fl_Image *image = new %s(", function_name_, type_name);
142  vwrite_c(format, ap);
143  write_c(");\n return image;\n}\n");
144  va_end(ap);
145 }
146 
147 void Fluid_Image::write_code(const char *var, int inactive) {
148  /* Outputs code that attaches an image to an Fl_Widget or Fl_Menu_Item.
149  This code calls a function output before by Fluid_Image::write_initializer() */
150  if (img) write_c("%s%s->%s( %s() );\n", indent(), var, inactive ? "deimage" : "image", function_name_);
151 }
152 
153 
155 
156 static Fluid_Image** images = 0; // sorted list
157 static int numimages = 0;
158 static int tablesize = 0;
159 
160 Fluid_Image* Fluid_Image::find(const char *iname) {
161  if (!iname || !*iname) return 0;
162 
163  // first search to see if it exists already:
164  int a = 0;
165  int b = numimages;
166  while (a < b) {
167  int c = (a+b)/2;
168  int i = strcmp(iname,images[c]->name_);
169  if (i < 0) b = c;
170  else if (i > 0) a = c+1;
171  else return images[c];
172  }
173 
174  // no, so now see if the file exists:
175 
176  goto_source_dir();
177  FILE *f = fl_fopen(iname,"rb");
178  if (!f) {
179  read_error("%s : %s",iname,strerror(errno));
181  return 0;
182  }
183  fclose(f);
184 
185  Fluid_Image *ret = new Fluid_Image(iname);
186 
187  if (!ret->img || !ret->img->w() || !ret->img->h()) {
188  delete ret;
189  ret = 0;
190  read_error("%s : unrecognized image format", iname);
191  }
193  if (!ret) return 0;
194 
195  // make a new entry in the table:
196  numimages++;
197  if (numimages > tablesize) {
198  tablesize = tablesize ? 2*tablesize : 16;
199  if (images) images = (Fluid_Image**)realloc(images, tablesize*sizeof(Fluid_Image*));
200  else images = (Fluid_Image**)malloc(tablesize*sizeof(Fluid_Image*));
201  }
202  for (b = numimages-1; b > a; b--) images[b] = images[b-1];
203  images[a] = ret;
204 
205  return ret;
206 }
207 
208 Fluid_Image::Fluid_Image(const char *iname) {
209  name_ = strdup(iname);
210  written = 0;
211  refcount = 0;
212  img = Fl_Shared_Image::get(iname);
214 }
215 
217  ++refcount;
218 }
219 
221  --refcount;
222  if (refcount > 0) return;
223  delete this;
224 }
225 
227  int a;
228  if (images) {
229  for (a = 0;; a++) if (images[a] == this) break;
230  numimages--;
231  for (; a < numimages; a++) images[a] = images[a+1];
232  }
233  if (img) img->release();
234  free((void*)name_);
235 }
236 
238 
239 #include <FL/Fl_File_Chooser.H>
240 
241 const char *ui_find_image_name;
242 Fluid_Image *ui_find_image(const char *oldname) {
243  goto_source_dir();
244  fl_file_chooser_ok_label("Use Image");
245  const char *name = fl_file_chooser("Image?","Image Files (*.{bm,bmp,gif,jpg,pbm,pgm,png,ppm,xbm,xpm})",oldname,1);
248  Fluid_Image *ret = (name && *name) ? Fluid_Image::find(name) : 0;
250  return ret;
251 }
252 
253 
254 //
255 // End of "$Id$".
256 //
Fl_Widget.H
Fl_Image::d
void d(int D)
Definition: Fl_Image.H:84
goto_source_dir
void goto_source_dir()
Definition: fluid.cxx:126
Fl.H
Fluid_Image.h
Fl_File_Chooser::fl_file_chooser_ok_label
void fl_file_chooser_ok_label(const char *l)
Definition: fl_file_dir.cxx:70
Fluid_Image::Fluid_Image
Fluid_Image(const char *name)
Definition: Fluid_Image.cxx:208
SEEK_END
#define SEEK_END
Definition: zconf.h:486
ui_find_image
Fluid_Image * ui_find_image(const char *oldname)
Definition: Fluid_Image.cxx:242
Fluid_Image::write_static
void write_static()
Definition: Fluid_Image.cxx:46
Fl_Image::data
void data(const char *const *p, int c)
Definition: Fl_Image.H:100
leave_source_dir
void leave_source_dir()
Definition: fluid.cxx:143
Fl_Image::h
void h(int H)
Definition: Fl_Image.H:80
Fl_File_Chooser::fl_file_chooser
char * fl_file_chooser(const char *message, const char *pat, const char *fname, int relative)
Definition: fl_file_dir.cxx:89
bitmap_header_written
static int bitmap_header_written
Definition: Fluid_Image.cxx:42
ncolors
static int ncolors
Definition: fl_draw_pixmap.cxx:41
Fluid_Image::~Fluid_Image
~Fluid_Image()
Definition: Fluid_Image.cxx:226
Fluid_Image::write_code
void write_code(const char *var, int inactive=0)
Definition: Fluid_Image.cxx:147
read_error
void read_error(const char *format,...)
Definition: file.cxx:151
jpeg_header_written
static int jpeg_header_written
Definition: Fluid_Image.cxx:44
Fl_Image::count
int count() const
Definition: Fl_Image.H:133
Fl_Widget::window
Fl_Window * window() const
Definition: Fl_Window.cxx:118
Fluid_Image::img
Fl_Shared_Image * img
Definition: Fluid_Image.h:32
filename.H
free
void free()
Fluid_Image::name_
const char * name_
Definition: Fluid_Image.h:30
NULL
#define NULL
Definition: forms.H:34
image_header_written
static int image_header_written
Definition: Fluid_Image.cxx:43
Fluid_Image::written
int written
Definition: Fluid_Image.h:38
Fl_Image::w
void w(int W)
Definition: Fl_Image.H:76
write_c
void write_c(const char *format,...)
Definition: code.cxx:265
fl_fopen
FILE * fl_fopen(const char *f, const char *mode)
Definition: fl_utf8.cxx:498
b
long b
Definition: jpegint.h:397
numimages
static int numimages
Definition: Fluid_Image.cxx:157
Fluid_Image::refcount
int refcount
Definition: Fluid_Image.h:31
write_cstring
void write_cstring(const char *s, int length)
Definition: code.cxx:154
Fl_Type.h
SEEK_SET
#define SEEK_SET
Definition: zconf.h:484
vwrite_c
void vwrite_c(const char *format, va_list args)
Definition: code.cxx:257
Fluid_Image::function_name_
const char * function_name_
Definition: Fluid_Image.h:33
fl_filename_name
const char * fl_filename_name(const char *filename)
Definition: Fl_win32.cxx:2179
Fl_File_Chooser.H
indent
const char * indent()
Definition: code.cxx:99
Fl_Widget
Definition: Fl_Widget.H:101
Fluid_Image::increment
void increment()
Definition: Fluid_Image.cxx:216
Fluid_Image::decrement
void decrement()
Definition: Fluid_Image.cxx:220
Fl_Shared_Image::get
static Fl_Shared_Image * get(const char *name, int W=0, int H=0)
Definition: Fl_Shared_Image.cxx:541
unique_id
const char * unique_id(void *o, const char *type, const char *name, const char *label)
Definition: code.cxx:63
write_number
int write_number
Definition: code.cxx:281
calloc
voidp calloc()
f
Fl_Box_Draw_F * f
Definition: fl_boxtype.cxx:285
malloc
voidp malloc()
images
static Fluid_Image ** images
Definition: Fluid_Image.cxx:156
Fl_Image::ld
void ld(int LD)
Definition: Fl_Image.H:96
pixmap_header_written
static int pixmap_header_written
Definition: Fluid_Image.cxx:41
Fluid_Image::find
static Fluid_Image * find(const char *)
Definition: Fluid_Image.cxx:160
Fluid_Image::write_initializer
void write_initializer(const char *type_name, const char *format,...)
Definition: Fluid_Image.cxx:133
ui_find_image_name
const char * ui_find_image_name
Definition: Fluid_Image.cxx:241
Fl_Widget::deimage
Fl_Image * deimage()
Definition: Fl_Widget.H:533
name
static const char * name
Definition: Fl_arg.cxx:53
Fluid_Image::image
void image(Fl_Widget *)
Definition: Fluid_Image.cxx:33
write_cdata
void write_cdata(const char *s, int length)
Definition: code.cxx:225
Fluid_Image
Definition: Fluid_Image.h:29
Fluid_Image::name
const char * name() const
Definition: Fluid_Image.h:47
Fluid_Image::deimage
void deimage(Fl_Widget *)
Definition: Fluid_Image.cxx:37
fl_filename_ext
const char * fl_filename_ext(const char *buf)
Definition: filename_ext.cxx:34
tablesize
static int tablesize
Definition: Fluid_Image.cxx:158
Fl_Shared_Image::release
void release()
Definition: Fl_Shared_Image.cxx:230
Fl_Widget::image
Fl_Image * image()
Definition: Fl_Widget.H:514