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)  

Fl_XPM_Image.cxx
Go to the documentation of this file.
1 //
2 // "$Id$"
3 //
4 // Fl_XPM_Image routines.
5 //
6 // Copyright 1997-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 // Contents:
19 //
20 //
21 
22 //
23 // Include necessary header files...
24 //
25 
26 #include <FL/Fl.H>
27 #include <FL/Fl_XPM_Image.H>
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <FL/fl_utf8.h>
31 #include "flstring.h"
32 
33 
34 //
35 // 'hexdigit()' - Convert a hex digit to an integer.
36 //
37 
38 static int hexdigit(int x) { // I - Hex digit...
39  if (isdigit(x)) return x-'0';
40  if (isupper(x)) return x-'A'+10;
41  if (islower(x)) return x-'a'+10;
42  return 20;
43 }
44 
45 #define MAXSIZE 2048
46 #define INITIALLINES 256
47 
53 Fl_XPM_Image::Fl_XPM_Image(const char *name) : Fl_Pixmap((char *const*)0) {
54  FILE *f;
55 
56  if ((f = fl_fopen(name, "rb")) == NULL) return;
57 
58  // read all the c-strings out of the file:
59  char** new_data = new char *[INITIALLINES];
60  char** temp_data;
61  int malloc_size = INITIALLINES;
62  char buffer[MAXSIZE+20];
63  int i = 0;
65  while (fgets(buffer,MAXSIZE+20,f)) {
66  if (buffer[0] != '\"') continue;
67  char *myp = buffer;
68  char *q = buffer+1;
69  while (*q != '\"' && myp < buffer+MAXSIZE) {
70  if (*q == '\\') switch (*++q) {
71  case '\r':
72  case '\n':
73  if (!fgets(q,(int) (buffer+MAXSIZE+20-q),f)) { /* no problem if we hit EOF */ } break;
74  case 0:
75  break;
76  case 'x': {
77  q++;
78  int n = 0;
79  for (int x = 0; x < 2; x++) {
80  int xd = hexdigit(*q);
81  if (xd > 15) break;
82  n = (n<<4)+xd;
83  q++;
84  }
85  *myp++ = n;
86  } break;
87  default: {
88  int c = *q++;
89  if (c>='0' && c<='7') {
90  c -= '0';
91  for (int x=0; x<2; x++) {
92  int xd = hexdigit(*q);
93  if (xd>7) break;
94  c = (c<<3)+xd;
95  q++;
96  }
97  }
98  *myp++ = c;
99  } break;
100  } else {
101  *myp++ = *q++;
102  }
103  }
104  *myp++ = 0;
105  if (i >= malloc_size) {
106  temp_data = new char *[malloc_size + INITIALLINES];
107  memcpy(temp_data, new_data, sizeof(char *) * malloc_size);
108  delete[] new_data;
109  new_data = temp_data;
110  malloc_size += INITIALLINES;
111  }
112  // first line has 4 ints: width, height, ncolors, chars_per_pixel
113  // followed by color segment:
114  // if ncolors < 0 this is FLTK (non standard) compressed colormap - all colors coded in single line of 4*ncolors bytes
115  // otherwise - ncolor lines of at least chars_per_pixel bytes
116  // followed by pic segment: H lines of at least chars_per_pixel*W bytes
117  // next line: would have loved to use measure_pixmap, but it doesn't return all the data!
118  if ((!i) && (sscanf(buffer,"%d%d%d%d", &W, &H, &ncolors, &chars_per_pixel) < 4)) goto bad_data; // first line
119  else if ((i > (ncolors<0?1:ncolors)) && (myp-buffer-1<W*chars_per_pixel)) goto bad_data; // pic segment
120  else if (myp-buffer-1<(ncolors<0?-ncolors*4:chars_per_pixel)) goto bad_data; // color segment
121  new_data[i] = new char[myp-buffer+1];
122  memcpy(new_data[i], buffer,myp-buffer);
123  new_data[i][myp-buffer] = 0;
124  i++;
125  }
126 
127  fclose(f);
128  f = NULL;
129  if ((!i) || (i<1+(ncolors<0?1:ncolors)+H)) goto bad_data;
130  data((const char **)new_data, i);
131  alloc_data = 1;
132 
133  measure();
134  return;
135  // dealloc and close as needed when bad data was found
136 bad_data:
137  while (i > 0) delete[] new_data[--i];
138  delete[] new_data;
139  if (f) fclose(f);
140 }
141 
142 
143 //
144 // End of "$Id$".
145 //
Fl.H
MAXSIZE
#define MAXSIZE
Definition: Fl_XPM_Image.cxx:45
chars_per_pixel
static int chars_per_pixel
Definition: fl_draw_pixmap.cxx:41
Fl_Image::data
const char *const * data() const
Definition: Fl_Image.H:138
ncolors
static int ncolors
Definition: fl_draw_pixmap.cxx:41
Fl_XPM_Image.H
H
static int H
Definition: Fl_Tooltip.cxx:76
NULL
#define NULL
Definition: forms.H:34
fl_fopen
FILE * fl_fopen(const char *f, const char *mode)
Definition: fl_utf8.cxx:498
buffer
static char * buffer
Definition: file.cxx:215
fl_utf8.h
header for Unicode and UTF-8 character handling
Fl_Pixmap
Definition: Fl_Pixmap.H:41
INITIALLINES
#define INITIALLINES
Definition: Fl_XPM_Image.cxx:46
hexdigit
static int hexdigit(int x)
Definition: Fl_XPM_Image.cxx:38
Fl_XPM_Image::Fl_XPM_Image
Fl_XPM_Image(const char *filename)
Definition: Fl_XPM_Image.cxx:53
Fl_Pixmap::measure
void measure()
Definition: Fl_Pixmap.cxx:62
x
int x
Definition: test.c:73
Fl_Pixmap::alloc_data
int alloc_data
Definition: Fl_Pixmap.H:58
f
Fl_Box_Draw_F * f
Definition: fl_boxtype.cxx:285
flstring.h
name
static const char * name
Definition: Fl_arg.cxx:53
const
#define const
Definition: zconf.h:226