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-2013, 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
38
#include <X11/Xlib.h>
39
#include <X11/Xutil.h>
40
#include <X11/Xos.h>
41
#include <X11/xpm.h>
42
#include <stdlib.h>
43
#include <stdio.h>
44
#include <netinet/in.h>
45
46
#define yes 1
47
#define no 0
48
49
50
51
#include "spadcolors.h"
52
#include "halloc.h"
53
#include "pixmap.h"
54
#include "spadcolors.h"
55
56
57
58
/* returns true if the file exists */
59
60
int
61
file_exists(char *file)
62
{
63
FILE *f;
64
65
if ((f = fopen(file, "r")) != NULL) {
66
fclose(f);
67
return 1;
68
}
69
return 0;
70
}
71
72
FILE *
73
zzopen(char *file, const char* mode)
74
{
75
char com[512], zfile[512];
76
77
if (file_exists(file))
78
return fopen(file, mode);
79
sprintf(zfile, "%s.Z", file);
80
if (file_exists(zfile)) {
81
sprintf(com, "gunzip -c %s.Z 2>/dev/null", file);
82
return popen(com, mode);
83
}
84
return NULL;
85
}
86
#ifdef OLD
87
88
/*******************************************************************
89
KF 6/14/90
90
write_pixmap_file(display, filename, pm, width, height)
91
and
92
write_pixmap_file_xy(display, filename, pm, x, y, width, height)
93
has been merged into one function.
94
95
INPUT: display dsp, screen s, file name fn to write the file in,
96
window id wid where pixmap is,
97
upper left corner x, y of original pixmap,
98
width and height of pixmap
99
OUTPUT: binary file with data
100
PURPOSE: write_pixmap_file gets the image structure of the input
101
pixmap, convert the image data with the permutation color
102
vector, writes the image structure out to filename.
103
104
Note that writing out a Z pixmap is 8x faster than XY pixmap.
105
This is because XY writes out each pixel value per plane, thus
106
number of bits; Z writes out each pixel, or 8 bits at a time.
107
108
The XY format may have been chosen for a reason -- I don't know.
109
110
********************************************************************/
111
void
112
write_pixmap_file(Display *dsp, int scr, char *fn,
113
Window wid, int x, int y, int width,int height)
114
{
115
XImage *xi;
116
FILE *file;
117
int *permVector;
118
int num;
119
int num_colors;
120
121
/* get color map and permutation vector */
122
if ((num_colors = makePermVector(dsp, scr,(unsigned long **)&permVector)) < 0) {
123
printf("num_colors < 0!!\n");
124
exit(-1);
125
}
126
127
/* reads image structure in ZPixmap format */
128
xi = XGetImage(dsp, wid, x, y, width, height, AllPlanes, ZPixmap);
129
file = fopen(fn, "wb");
130
if (file == NULL) {
131
perror("opening pixmap file for write");
132
exit(-1);
133
}
134
135
#define PUTW(a,b) putw(htonl(a),b)
136
137
138
PUTW(xi->width, file);
139
PUTW(xi->height, file);
140
PUTW(xi->xoffset, file);
141
PUTW(xi->format, file);
142
PUTW(xi->byte_order, file);
143
PUTW(xi->bitmap_unit, file);
144
PUTW(xi->bitmap_bit_order, file);
145
PUTW(xi->bitmap_pad, file);
146
PUTW(xi->depth, file);
147
PUTW(xi->bytes_per_line, file);
148
PUTW(xi->bits_per_pixel, file);
149
PUTW(xi->red_mask, file);
150
PUTW(xi->green_mask, file);
151
PUTW(xi->blue_mask, file);
152
153
num = xi->bytes_per_line * height; /* total number of pixels in pixmap */
154
155
/* store value from permutation */
156
{
157
int ii, jj;
158
159
for (ii = 0; ii < width; ii++)
160
for (jj = 0; jj < height; jj++) {
161
XPutPixel(xi, ii, jj, permVector[(int) XGetPixel(xi, ii, jj)]);
162
}
163
}
164
fwrite(xi->data, 1, num, file);
165
fclose(file);
166
}
167
168
/*******************************************************************
169
KF 6/14/90
170
171
INPUT: display, screen, filename to read the pixmap data from,
172
OUTPUT: ximage structure xi, width and height of pixmap
173
PURPOSE: read_pixmap_file reads an Ximage data structure from
174
the input file.
175
This routine can handle pixmaps of both XYPixmap and
176
ZPixmap. If a pixmap has ZPixmap format, then the image
177
data, read in as spadColor index, is converted to the
178
pixel value using spadColor.
179
180
Note that reading in Z format takes less space and time too.
181
182
********************************************************************/
183
int
184
read_pixmap_file(Display *display, int screen, char *filename,
185
XImage **xi, int *width, int *height)
186
{
187
FILE *file;
188
int wi, h, num, num_colors, read_this_time, offset;
189
Colormap cmap;
190
int ts;
191
unsigned long *spadColors;
192
193
/* colormap is necessary to call makeColors */
194
cmap = DefaultColormap(display, screen);
195
if ((num_colors = makeColors(display, screen, &cmap, &spadColors, &ts)) < 0) {
196
return(-1);
197
}
198
file = zzopen(filename, "r");
199
if (file == NULL) {
200
printf("couldn't open %s\n", filename);
201
return BitmapOpenFailed;
202
}
203
#define GETW(f) ntohl(getw(f))
204
*width = wi = GETW(file);
205
*height = h = GETW(file);
206
(*xi) = XCreateImage(display, DefaultVisual(display, screen),
207
DisplayPlanes(display, screen),
208
ZPixmap, 0, NULL, wi, h, 16, 0); /* handles both XY & Z */
209
if ((*xi) == NULL) {
210
fprintf(stderr, "Unable to create image\n");
211
return(-1);
212
}
213
(*xi)->width = wi;
214
(*xi)->height = h;
215
(*xi)->xoffset = GETW(file);
216
(*xi)->format = GETW(file);
217
(*xi)->byte_order = GETW(file);
218
(*xi)->bitmap_unit = GETW(file);
219
(*xi)->bitmap_bit_order = GETW(file);
220
(*xi)->bitmap_pad = GETW(file);
221
(*xi)->depth = GETW(file);
222
(*xi)->bytes_per_line = GETW(file);
223
(*xi)->bits_per_pixel = GETW(file);
224
(*xi)->red_mask = GETW(file);
225
(*xi)->green_mask = GETW(file);
226
(*xi)->blue_mask = GETW(file);
227
228
/* program will bomb if XYPixmap is not allocated enough space */
229
if ((*xi)->format == XYPixmap) {
230
/* printf("picture is in XYPixmap format.\n"); */
231
num = (*xi)->bytes_per_line * h * (*xi)->depth;
232
}
233
else /* ZPixmap */
234
num = (*xi)->bytes_per_line * h;
235
(*xi)->data = (void*)halloc(num, "Ximage data");
236
237
offset = 0;
238
while (offset < num) {
239
read_this_time = fread(((*xi)->data + offset), 1, num - offset, file);
240
offset = offset + read_this_time;
241
}
242
fclose(file);
243
244
/*
245
* pixmap data in ZPixmap format are spadColor indices; pixmap data in
246
* XYPixmap format are pixel values
247
*/
248
if ((*xi)->format == ZPixmap) {
249
250
int ii, jj;
251
252
for (ii = 0; ii < wi; ii++)
253
for (jj = 0; jj < h; jj++) {
254
XPutPixel(*xi, ii, jj, spadColors[(int) XGetPixel(*xi, ii, jj)]);
255
}
256
257
258
}
259
260
return 0;
261
}
262
263
264
#else /*OLD*/
265
266
267
268
int
269
read_pixmap_file(Display *display, int screen, char *filename,
270
XImage **xi, int *width, int *height)
271
{
272
XpmAttributes attr;
273
XImage *xireturn;
274
275
attr.valuemask = 0;
276
277
attr.bitmap_format=ZPixmap; /* instead of XYPixmap */
278
attr.valuemask |= XpmBitmapFormat;
279
attr.valuemask |= XpmSize; /* we want feedback on width,height */
280
attr.valuemask |= XpmCharsPerPixel; /* and cpp */
281
attr.valuemask |= XpmReturnPixels; /* and pixels, npixels */
282
attr.valuemask |= XpmReturnAllocPixels; /* and alloc_pixels, nalloc_pixels */
283
attr.exactColors = False;
284
attr.valuemask |= XpmExactColors; /* we don't want exact colors*/
285
attr.closeness = 30000;
286
attr.valuemask |= XpmCloseness; /* we specify closeness*/
287
attr.alloc_close_colors = False;
288
attr.valuemask |= XpmAllocCloseColors; /* we don't allocate close colors*/
289
290
291
XpmReadFileToImage(display,filename,xi,&xireturn, &attr );
292
*width= (*xi)->width;
293
*height=(*xi)->height;
294
#ifdef DEBUG
295
fprintf(stderr,"image file:%s\n",filename);
296
fprintf(stderr,"\twidth:%d\theight:%d\tcpp:%d\n",attr.width,attr.height,attr.cpp);
297
fprintf(stderr,"\tused/alloc'ed color pixels:%d/%d\n",attr.npixels,attr.nalloc_pixels);
298
#endif
299
return 0;
300
}
301
302
303
void
304
write_pixmap_file(Display *dsp, int scr, char *fn,
305
Window wid, int x, int y, int width,int height)
306
{
307
XImage *xi;
308
309
/* reads image structure in ZPixmap format */
310
xi = XGetImage(dsp, wid, x, y, width, height, AllPlanes, ZPixmap);
311
if (xi==0) return ;
312
XpmWriteFileFromImage(dsp,fn,xi,0,0);
313
314
}
315
316
317
#endif
318
319
320
321
322