1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
|
/*
* Created: zov : 17-Apr-1998 : Loads XWD image from file
* Note: Adapted from Xw_load_image.c
*
XW_EXT_IMAGEDATA* Xw_load_xwd_image (awindow,aimageinfo,fimage,ppcolors,pncolors)
XW_EXT_WINDOW *awindow
XW_USERDATA *aimageinfo
XColor **ppcolors - address of pointer to array of used colors to return
int *pncolors - address of a variable to return length of the array in
Gets an image from a file.
Note, that the file must be XWD formatted.
Returns the image descriptor address if successful; or NULL if failed.
*/
#include <Xw_Extension.h>
#include <fcntl.h>
#define SWAPTEST (*(char*)&swaptest)
static unsigned long swaptest = 1;
static void SwapLong (register char* bp,register unsigned n)
/*
Swap long ints depending on CPU
*/
{
register char c;
register char *ep = bp + n;
register char *sp;
while (bp < ep) {
sp = bp + 3;
c = *sp;
*sp = *bp;
*bp++ = c;
sp = bp + 1;
c = *sp;
*sp = *bp;
*bp++ = c;
bp += 2;
}
}
static void SwapShort (register char* bp, register unsigned n)
/*
Swap short ints depending on CPU
*/
{
register char c;
register char *ep = bp + n;
while (bp < ep) {
c = *bp;
*bp = *(bp + 1);
bp++;
*bp++ = c;
}
}
XW_EXT_IMAGEDATA* Xw_load_xwd_image (void *awindow,void *aimageinfo,char *filename,int fimage,
XColor **ppcolors, int *pncolors)
{
// XW_EXT_WINDOW *pwindow = (XW_EXT_WINDOW *)awindow;
XW_EXT_IMAGEDATA *pimage;
// XW_STATUS status;
XWDFileHeader *pheader;
XImage *pximage;
XColor *pcolors = NULL;
char *wname = NULL, *pidata;
int i, isize;
isize = sizeof (XWDFileHeader);
if (!(pheader = (_xwd_file_header*) Xw_malloc (isize))) {
/*ERROR*Bad Image allocation*/
Xw_set_error (60,"Xw_load_xwd_image",0) ;
return (NULL) ;
}
if (read(fimage,pheader,isize) != isize) {
/*ERROR*Unable to read XWD file header*/
Xw_set_error (56,"Xw_load_xwd_image",filename);
Xw_free (pheader);
return (NULL);
}
if (SWAPTEST) SwapLong ((char*) pheader, isize);
/* Is it XWD file format ? */
if (pheader->file_version != XWD_FILE_VERSION) {
/*ERROR*Bad XWD file format*/
Xw_set_error (57,"Xw_load_xwd_image",filename);
Xw_free (pheader);
return (NULL);
}
if (pheader->header_size < (unsigned int ) isize) {
/*ERROR*Bad XWD file format*/
Xw_set_error (57,"Xw_load_xwd_image",filename);
Xw_free (pheader);
return (NULL);
}
/* Check for image format */
if (pheader->pixmap_format != ZPixmap) {
/*ERROR*Bad XWD Pixmap format*/
Xw_set_error (58,"Xw_load_xwd_image",filename);
Xw_free (pheader);
return (NULL);
}
/* Read a window name */
isize = pheader->header_size - isize ;
if (isize > 0) {
if (! (wname = (char*) Xw_malloc(isize))) {
/*ERROR*Bad Image allocation*/
Xw_set_error(60,"Xw_load_xwd_image",0);
Xw_free(pheader);
return (NULL);
}
if(read (fimage, wname, isize) != isize) {
/*ERROR*Unable to read Image data*/
Xw_set_error(61,"Xw_load_xwd_image",filename) ;
Xw_free (wname);
Xw_free (pheader);
return (NULL) ;
}
}
/* Allocate the Image structure */
if (! (pximage = (XImage*) Xw_malloc(sizeof(XImage)))) {
/*ERROR*Bad Image allocation*/
Xw_set_error(60,"Xw_load_xwd_image",0);
if (wname) Xw_free(wname);
Xw_free(pheader) ;
return (NULL);
}
if (pheader->ncolors > 0) {
/* Allocate the Color Table entries */
if (! (pcolors = (XColor*) Xw_calloc(pheader->ncolors, sizeof(XColor)))) {
/*ERROR*Bad Image allocation*/
Xw_set_error (60,"Xw_load_xwd_image",0);
if (wname) Xw_free(wname);
Xw_free (pximage);
Xw_free (pheader);
return (NULL);
}
/* Read the Color Table entries */
isize = sizeof(XWDColor) * pheader->ncolors;
if (read (fimage, (char *)pcolors, isize) != isize) {
/*ERROR*Unable to read Image data*/
Xw_set_error(61,"Xw_load_xwd_image",filename);
if (wname) Xw_free(wname);
Xw_free (pximage);
Xw_free (pcolors);
Xw_free (pheader);
return (NULL) ;
}
/* 32->64 bits conversion if any*/
if (sizeof(XColor) > sizeof(XWDColor)) {
XWDColor *qcolors = (XWDColor*) pcolors ;
for( i=pheader->ncolors-1 ; i>=0 ; --i ) {
pcolors[i].pad = qcolors[i].pad;
pcolors[i].flags = qcolors[i].flags;
pcolors[i].blue = qcolors[i].blue;
pcolors[i].green = qcolors[i].green;
pcolors[i].red = qcolors[i].red;
pcolors[i].pixel = qcolors[i].pixel;
}
}
if (SWAPTEST) {
for (i=0 ; (unsigned int ) i<pheader->ncolors ; i++) {
SwapLong((char*) &pcolors[i].pixel,sizeof(long));
SwapShort((char*) &pcolors[i].red,3*sizeof(short));
}
}
} /* end if # colors > 0 */
/* Allocate the pixel buffer */
isize = pheader->bytes_per_line*pheader->pixmap_height;
if (! (pidata = (char *) Xw_malloc(isize))) {
/*ERROR*Bad Image allocation*/
Xw_set_error (60,"Xw_load_xwd_image",0);
if (wname) Xw_free(wname);
Xw_free (pximage);
if (pcolors) Xw_free(pcolors);
Xw_free(pheader);
return (NULL);
}
/* Read the pixel buffer */
if (read (fimage,pidata,isize) != isize) {
/*ERROR*Unable to read Image data*/
Xw_set_error (61,"Xw_load_xwd_image",filename);
if (wname) Xw_free(wname);
Xw_free (pidata);
Xw_free (pximage);
if (pcolors) Xw_free (pcolors);
Xw_free (pheader);
return (NULL);
}
/* Allocate the Extended Image structure */
if(! (pimage = Xw_add_imagedata_structure(sizeof(XW_EXT_IMAGEDATA)))) {
if (wname) Xw_free(wname);
Xw_free(pidata);
Xw_free(pximage);
if (pcolors) Xw_free(pcolors);
Xw_free(pheader);
return (NULL);
}
/* Initialize the input image */
pimage->pimageinfo = aimageinfo;
_IIMAGE = pximage;
_IIMAGE->data = pidata;
_IIMAGE->width = (int) pheader->pixmap_width;
_IIMAGE->height = (int) pheader->pixmap_height;
_IIMAGE->xoffset = (int) pheader->xoffset;
_IIMAGE->format = (int) pheader->pixmap_format;
_IIMAGE->byte_order = (int) pheader->byte_order;
_IIMAGE->bitmap_unit = (int) pheader->bitmap_unit;
_IIMAGE->bitmap_bit_order = (int) pheader->bitmap_bit_order;
_IIMAGE->bitmap_pad = (int) pheader->bitmap_pad;
_IIMAGE->depth = (int) pheader->pixmap_depth;
_IIMAGE->bits_per_pixel = (int) pheader->bits_per_pixel;
_IIMAGE->bytes_per_line = (int) pheader->bytes_per_line;
_IIMAGE->red_mask = pheader->red_mask;
_IIMAGE->green_mask = pheader->green_mask;
_IIMAGE->blue_mask = pheader->blue_mask;
_IIMAGE->obdata = NULL ;
_XInitImageFuncPtrs (_IIMAGE);
/* Return array of colors */
*ppcolors = pcolors;
*pncolors = pheader->ncolors;
if (wname) Xw_free(wname);
if (pheader) Xw_free(pheader);
return (pimage);
}
|