blob: cb35c1b5caa270ddc91b968a032293eedbe221c7 [file] [log] [blame]
DRC2ff39b82011-07-28 08:38:59 +00001//
2// "$Id: Fl_XBM_Image.cxx 7903 2010-11-28 21:06:39Z matt $"
3//
4// Fl_XBM_Image routines.
5//
6// Copyright 1997-2010 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// Contents:
28//
29// Fl_XBM_Image::Fl_XBM_Image() - Load an XBM file.
30//
31
32//
33// Include necessary header files...
34//
35
36#include <FL/Fl.H>
37#include <FL/Fl_XBM_Image.H>
38#include <stdio.h>
39#include <stdlib.h>
40#include <FL/fl_utf8.h>
41#include "flstring.h"
42
43//
44// 'Fl_XBM_Image::Fl_XBM_Image()' - Load an XBM file.
45//
46
47/**
48 The constructor loads the named XBM file from the given name filename.
49 <P>The destructor free all memory and server resources that are used by
50 the image.
51*/
52Fl_XBM_Image::Fl_XBM_Image(const char *name) : Fl_Bitmap((const char *)0,0,0) {
53 FILE *f;
54 uchar *ptr;
55
56 if ((f = fl_fopen(name, "rb")) == NULL) return;
57
58 char buffer[1024];
59 char junk[1024];
60 int wh[2]; // width and height
61 int i;
62 for (i = 0; i<2; i++) {
63 for (;;) {
64 if (!fgets(buffer,1024,f)) {
65 fclose(f);
66 return;
67 }
68 int r = sscanf(buffer,"#define %s %d",junk,&wh[i]);
69 if (r >= 2) break;
70 }
71 }
72
73 // skip to data array:
74 for (;;) {
75 if (!fgets(buffer,1024,f)) {
76 fclose(f);
77 return;
78 }
79 if (!strncmp(buffer,"static ",7)) break;
80 }
81
82 // Allocate memory...
83 w(wh[0]);
84 h(wh[1]);
85
86 int n = ((wh[0]+7)/8)*wh[1];
87 array = new uchar[n];
88
89 // read the data:
90 for (i = 0, ptr = (uchar *)array; i < n;) {
91 if (!fgets(buffer,1024,f)) {
92 fclose(f);
93 return;
94 }
95 const char *a = buffer;
96 while (*a && i<n) {
97 unsigned int t;
98 if (sscanf(a," 0x%x",&t)>0) {
99 *ptr++ = (uchar)t;
100 i ++;
101 }
102 while (*a && *a++ != ',');
103 }
104 }
105
106 fclose(f);
107}
108
109
110//
111// End of "$Id: Fl_XBM_Image.cxx 7903 2010-11-28 21:06:39Z matt $".
112//