Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download

open-axiom repository from github

24005 views
1
/*
2
Copyright (C) 1991-2002, The Numerical Algorithms Group Ltd.
3
All rights reserved.
4
Copyright (C) 2007-2010, Gabriel Dos Reis.
5
All rights reserved.
6
7
Redistribution and use in source and binary forms, with or without
8
modification, are permitted provided that the following conditions are
9
met:
10
11
- Redistributions of source code must retain the above copyright
12
notice, this list of conditions and the following disclaimer.
13
14
- Redistributions in binary form must reproduce the above copyright
15
notice, this list of conditions and the following disclaimer in
16
the documentation and/or other materials provided with the
17
distribution.
18
19
- Neither the name of The Numerical Algorithms Group Ltd. nor the
20
names of its contributors may be used to endorse or promote products
21
derived from this software without specific prior written permission.
22
23
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
24
IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
25
TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
26
PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
27
OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
28
EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
29
PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
30
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
31
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
32
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
33
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34
*/
35
36
#include "openaxiom-c-macros.h"
37
#include "debug.h"
38
#include "halloc.h"
39
#include "sockio.h"
40
#include "hyper.h"
41
42
static int read_hot(FILE * fd , char Line[] , int * x_hot , int * y_hot);
43
static int read_w_and_h(FILE * fd , unsigned int * width , unsigned int * height);
44
45
#define MAXLINE 256
46
47
/*
48
* This file was produced by J.M. Wiley with some help from the bitmap editor
49
* routine. It reads in a bitmap file, and calls XCreatePixmapFromBitmapData
50
* to transform it into a Pixmap. He did this because the routine
51
* XReadBitmapFile does not seeem to work to well (whatecer that means).
52
*/
53
54
XImage *
55
HTReadBitmapFile(Display *display,int screen,char * filename,
56
int *width, int *height)
57
{
58
XImage *image;
59
FILE *fd;
60
char Line[256], Buff[256];
61
int num_chars;
62
char *ptr;
63
int rch;
64
int version;
65
int padding, chars_line, file_chars_line, file_chars;
66
int bytes;
67
int x_hot, y_hot;
68
69
70
image = XCreateImage(display, DefaultVisual(display, screen), 1,
71
XYBitmap, 0, NULL, 0, 0, 8, 0);
72
73
74
(image)->byte_order = LSBFirst; /* byte_order */
75
(image)->bitmap_unit = 8; /* bitmap-unit */
76
(image)->bitmap_bit_order = LSBFirst; /* bitmap-bit-order */
77
78
if (!(fd = zzopen(filename, "r"))) {
79
fprintf(stderr, "ReadBitmapFile: File >%s< not found\n", filename);
80
exit(-1);
81
}
82
83
/*
84
* Once it is open, lets get the width and height
85
*/
86
87
if ((read_w_and_h(fd, (unsigned int *)width,(unsigned int *) height)) < 0) {
88
fprintf(stderr, "ReadBitmapFile: Bad file format in %s\n", filename);
89
exit(-1);
90
}
91
92
93
/*
94
* Now get the next line, and see if it is hot spots or bits
95
*/
96
if (fgets(Line, MAXLINE, fd) == NULL) {
97
fprintf(stderr, "ReadBitmapFile: Bad file format in %s\n", filename);
98
exit(-1);
99
}
100
101
/*
102
* Now check the first character to see if it is a # or an s
103
*/
104
105
if (Line[0] == '#') {
106
if ((read_hot(fd, Line, &x_hot, &y_hot)) < 0) {
107
fprintf(stderr, "ReadBitmapFile: Bad file format in %s\n", filename);
108
exit(-1);
109
}
110
}
111
112
(image)->width = *width;
113
(image)->height = *height;
114
115
/*
116
* figure out what version
117
*/
118
119
if (sscanf(Line, "static short %s = {", Buff) == 1)
120
version = 10;
121
else if (sscanf(Line, "static unsigned char %s = {", Buff) == 1)
122
version = 11;
123
else if (sscanf(Line, "static char %s = {", Buff) == 1)
124
version = 11;
125
else {
126
fprintf(stderr, "ReadBitmapFile: Bad file format in %s\n", filename);
127
exit(-1);
128
}
129
130
padding = 0;
131
if ((*width % 16) && ((*width % 16) < 9) && (version == 10))
132
padding = 1;
133
134
(image)->bytes_per_line = chars_line = (*width + 7) / 8;
135
file_chars_line = chars_line + padding;
136
137
num_chars = chars_line * (*height);
138
file_chars = file_chars_line * (*height);
139
(image)->data = (char *) halloc((image)->bytes_per_line * (image)->height,
140
"Read Pixmap--Image data");
141
142
/*
143
* Since we are just holding the first line of the declaration, we can
144
* just start reading from fd
145
*/
146
147
if (version == 10)
148
for (bytes = 0, ptr = (image)->data; bytes < file_chars; (bytes += 2)) {
149
if (fscanf(fd, " 0x%x%*[,}]%*[ \n]", &rch) != 1) {
150
fprintf(stderr, "ReadBitmapFile: Bad file format in %s\n", filename);
151
exit(-1);
152
}
153
*(ptr++) = rch & 0xff;
154
if (!padding || ((bytes + 2) % file_chars_line))
155
*(ptr++) = rch >> 8;
156
}
157
else
158
for (bytes = 0, ptr = (image)->data; bytes < file_chars; bytes++, ptr++) {
159
if (fscanf(fd, " 0x%x%*[,}]%*[ \n]", &rch) != 1) {
160
fprintf(stderr, "ReadBitmapFile: Bad file format in %s\n", filename);
161
exit(-1);
162
}
163
*ptr = rch;
164
}
165
166
fclose(fd);
167
168
return image;
169
}
170
171
static int
172
read_hot(FILE *fd,char Line[],int *x_hot,int *y_hot)
173
{
174
char Buff[256];
175
176
/*
177
* Works much the same as get width and height, just new variables
178
*/
179
180
if (sscanf(Line, "#define %s %d", Buff, x_hot) != 2)
181
return -1;
182
183
if (fgets(Line, MAXLINE, fd) == NULL)
184
return -1;
185
186
if (sscanf(Line, "#define %s %d", Buff, y_hot) != 2)
187
return -1;
188
189
if (fgets(Line, MAXLINE, fd) == NULL)
190
return -1;
191
return 1;
192
}
193
194
static int
195
read_w_and_h(FILE *fd,unsigned int *width,unsigned int *height)
196
{
197
char Line[256], Buff[256];
198
199
if (fgets(Line, MAXLINE, fd) == NULL)
200
return -1;
201
202
/*
203
* Once we have the line, scan it for the width
204
*/
205
206
if (sscanf(Line, "#define %s %d", Buff, width) != 2)
207
return -1;
208
209
/*
210
* Hopefully we have the width, now get the height the same way
211
*/
212
213
if (fgets(Line, MAXLINE, fd) == NULL)
214
return -1;
215
216
217
/*
218
* Once we have the line, scan it for the height
219
*/
220
221
if (sscanf(Line, "#define %s %d", Buff, height) != 2)
222
return -1;
223
224
return 1;
225
}
226
227
228
/* read a bitmap file into memory */
229
230
ImageStruct *
231
insert_image_struct(char *filename)
232
{
233
int bm_width, bm_height;
234
XImage *im;
235
ImageStruct *image;
236
237
if (*filename == ' ')
238
filename++;
239
if ((image = (ImageStruct *) hash_find(&gImageHashTable, filename)) == NULL) {
240
im = HTReadBitmapFile(gXDisplay, gXScreenNumber, filename,
241
&bm_width, &bm_height);
242
243
/*
244
* now add the image to the gImageHashTable
245
*/
246
247
image = (ImageStruct *) halloc(sizeof(ImageStruct), "ImageStruct");
248
image->image.xi = im;
249
image->width = image->image.xi->width;
250
image->height = image->image.xi->height;
251
image->filename = (char *) halloc(sizeof(char) * strlen(filename) +1,
252
"insert_image--filename");
253
254
/* strcpy(image->filename, filename); */
255
256
sprintf(image->filename, "%s", filename);
257
hash_insert(&gImageHashTable,(char *) image, image->filename);
258
}
259
return image;
260
}
261
262