source: rtems-graphics-toolkit/fltk-1.1.10/fluid/Fluid_Image.cxx @ 513eea1

Last change on this file since 513eea1 was 513eea1, checked in by Joel Sherrill <joel.sherrill@…>, on 01/09/10 at 22:43:24

2010-01-08 Joel Sherrill <joel.sherrill@…>

fltk 1.1.10. imported

  • ORIGIN: Updated.
  • Property mode set to 100644
File size: 6.4 KB
Line 
1//
2// "$Id$"
3//
4// Pixmap label support for the Fast Light Tool Kit (FLTK).
5//
6// Copyright 1998-2006 by Bill Spitzak and others.
7//
8// This library is free software; you can redistribute it and/or
9// modify it under the terms of the GNU Library General Public
10// License as published by the Free Software Foundation; either
11// version 2 of the License, or (at your option) any later version.
12//
13// This library is distributed in the hope that it will be useful,
14// but WITHOUT ANY WARRANTY; without even the implied warranty of
15// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16// Library General Public License for more details.
17//
18// You should have received a copy of the GNU Library General Public
19// License along with this library; if not, write to the Free Software
20// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
21// USA.
22//
23// Please report all bugs and problems on the following page:
24//
25//     http://www.fltk.org/str.php
26//
27
28#include <FL/Fl.H>
29#include <FL/Fl_Widget.H>
30#include "Fl_Type.h"
31#include "Fluid_Image.h"
32#include "../src/flstring.h"
33#include <stdio.h>
34#include <errno.h>
35#include <stdlib.h>
36#include <FL/filename.H>
37
38extern void goto_source_dir(); // in fluid.C
39extern void leave_source_dir(); // in fluid.C
40
41void Fluid_Image::image(Fl_Widget *o) {
42  if (o->window() != o) o->image(img);
43}
44
45void Fluid_Image::deimage(Fl_Widget *o) {
46  if (o->window() != o) o->deimage(img);
47}
48
49static int pixmap_header_written = 0;
50static int bitmap_header_written = 0;
51static int image_header_written = 0;
52
53void Fluid_Image::write_static() {
54  if (!img) return;
55  if (img->count() > 1) {
56    // Write Pixmap data...
57    write_c("\n");
58    if (pixmap_header_written != write_number) {
59      write_c("#include <FL/Fl_Pixmap.H>\n");
60      pixmap_header_written = write_number;
61    }
62    write_c("static const char *%s[] = {\n",
63            unique_id(this, "idata", fl_filename_name(name()), 0));
64    write_cstring(img->data()[0], strlen(img->data()[0]));
65
66    int i;
67    int ncolors, chars_per_color;
68    sscanf(img->data()[0], "%*d%*d%d%d", &ncolors, &chars_per_color);
69
70    if (ncolors < 0) {
71      write_c(",\n");
72      write_cstring(img->data()[1], ncolors * -4);
73      i = 2;
74    } else {
75      for (i = 1; i <= ncolors; i ++) {
76        write_c(",\n");
77        write_cstring(img->data()[i], strlen(img->data()[i]));
78      }
79    }
80    for (; i < img->count(); i ++) {
81      write_c(",\n");
82      write_cstring(img->data()[i], img->w() * chars_per_color);
83    }
84    write_c("\n};\n");
85    write_c("static Fl_Pixmap %s(%s);\n",
86            unique_id(this, "image", fl_filename_name(name()), 0),
87            unique_id(this, "idata", fl_filename_name(name()), 0));
88  } else if (img->d() == 0) {
89    // Write Bitmap data...
90    write_c("\n");
91    if (bitmap_header_written != write_number) {
92      write_c("#include <FL/Fl_Bitmap.H>\n");
93      bitmap_header_written = write_number;
94    }
95    write_c("static unsigned char %s[] =\n",
96            unique_id(this, "idata", fl_filename_name(name()), 0));
97    write_cdata(img->data()[0], ((img->w() + 7) / 8) * img->h());
98    write_c(";\n");
99    write_c("static Fl_Bitmap %s(%s, %d, %d);\n",
100            unique_id(this, "image", fl_filename_name(name()), 0),
101            unique_id(this, "idata", fl_filename_name(name()), 0),
102            img->w(), img->h());
103  } else {
104    // Write image data...
105    write_c("\n");
106    if (image_header_written != write_number) {
107      write_c("#include <FL/Fl_Image.H>\n");
108      image_header_written = write_number;
109    }
110    write_c("static unsigned char %s[] =\n",
111            unique_id(this, "idata", fl_filename_name(name()), 0));
112    write_cdata(img->data()[0], (img->w() * img->d() + img->ld()) * img->h());
113    write_c(";\n");
114    write_c("static Fl_RGB_Image %s(%s, %d, %d, %d, %d);\n",
115            unique_id(this, "image", fl_filename_name(name()), 0),
116            unique_id(this, "idata", fl_filename_name(name()), 0),
117            img->w(), img->h(), img->d(), img->ld());
118  }
119}
120
121void Fluid_Image::write_code(const char *var, int inactive) {
122  if (!img) return;
123  write_c("%s%s->%s(%s);\n", indent(), var, inactive ? "deimage" : "image",
124          unique_id(this, "image", fl_filename_name(name()), 0));
125}
126
127
128////////////////////////////////////////////////////////////////
129
130static Fluid_Image** images = 0; // sorted list
131static int numimages = 0;
132static int tablesize = 0;
133
134Fluid_Image* Fluid_Image::find(const char *iname) {
135  if (!iname || !*iname) return 0;
136
137  // first search to see if it exists already:
138  int a = 0;
139  int b = numimages;
140  while (a < b) {
141    int c = (a+b)/2;
142    int i = strcmp(iname,images[c]->name_);
143    if (i < 0) b = c;
144    else if (i > 0) a = c+1;
145    else return images[c];
146  }
147
148  // no, so now see if the file exists:
149
150  goto_source_dir();
151  FILE *f = fopen(iname,"rb");
152  if (!f) {
153    read_error("%s : %s",iname,strerror(errno));
154    leave_source_dir();
155    return 0;
156  }
157  fclose(f);
158
159  Fluid_Image *ret = new Fluid_Image(iname);
160
161  if (!ret->img || !ret->img->w() || !ret->img->h()) {
162    delete ret;
163    ret = 0;
164    read_error("%s : unrecognized image format", iname);
165  }
166  leave_source_dir();
167  if (!ret) return 0;
168
169  // make a new entry in the table:
170  numimages++;
171  if (numimages > tablesize) {
172    tablesize = tablesize ? 2*tablesize : 16;
173    if (images) images = (Fluid_Image**)realloc(images, tablesize*sizeof(Fluid_Image*));
174    else images = (Fluid_Image**)malloc(tablesize*sizeof(Fluid_Image*));
175  }
176  for (b = numimages-1; b > a; b--) images[b] = images[b-1];
177  images[a] = ret;
178
179  return ret;
180}
181
182Fluid_Image::Fluid_Image(const char *iname) {
183  name_ = strdup(iname);
184  written = 0;
185  refcount = 0;
186  img = Fl_Shared_Image::get(iname);
187}
188
189void Fluid_Image::increment() {
190  ++refcount;
191}
192
193void Fluid_Image::decrement() {
194  --refcount;
195  if (refcount > 0) return;
196  delete this;
197}
198
199Fluid_Image::~Fluid_Image() {
200  int a;
201  if (images) {
202    for (a = 0;; a++) if (images[a] == this) break;
203    numimages--;
204    for (; a < numimages; a++) images[a] = images[a+1];
205  }
206  if (img) img->release();
207  free((void*)name_);
208}
209
210////////////////////////////////////////////////////////////////
211
212#include <FL/Fl_File_Chooser.H>
213
214const char *ui_find_image_name;
215Fluid_Image *ui_find_image(const char *oldname) {
216  goto_source_dir();
217  fl_file_chooser_ok_label("Use Image");
218  const char *name = fl_file_chooser("Image?","Image Files (*.{bm,bmp,gif,jpg,pbm,pgm,png,ppm,xbm,xpm})",oldname,1);
219  fl_file_chooser_ok_label(NULL);
220  ui_find_image_name = name;
221  Fluid_Image *ret = (name && *name) ? Fluid_Image::find(name) : 0;
222  leave_source_dir();
223  return ret;
224}
225
226
227//
228// End of "$Id$".
229//
Note: See TracBrowser for help on using the repository browser.