]>
git.saurik.com Git - wxWidgets.git/blob - src/xpm/misc.c
2 * Copyright (C) 1989-94 GROUPE BULL
4 * Permission is hereby granted, free of charge, to any person obtaining a copy
5 * of this software and associated documentation files (the "Software"), to
6 * deal in the Software without restriction, including without limitation the
7 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
8 * sell copies of the Software, and to permit persons to whom the Software is
9 * furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17 * GROUPE BULL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
18 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
19 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21 * Except as contained in this notice, the name of GROUPE BULL shall not be
22 * used in advertising or otherwise to promote the sale, use or other dealings
23 * in this Software without prior written authorization from GROUPE BULL.
26 /*****************************************************************************\
30 * Miscellaneous utilities *
32 * Developed by Arnaud Le Hors *
33 \*****************************************************************************/
36 * The code related to FOR_MSW has been added by
37 * HeDu (hedu@cul-ipn.uni-kiel.de) 4/94
42 #include "sys$library:stat.h"
43 #include "sys$library:fcntl.h"
45 #include <sys/types.h>
56 /* 3.2 backward compatibility code */
57 LFUNC(CreateOldColorTable
, int, (XpmColor
*ct
, int ncolors
,
60 LFUNC(FreeOldColorTable
, void, (XpmColor
**colorTable
, int ncolors
));
63 * Create a colortable compatible with the old style colortable
66 CreateOldColorTable(XpmColor
*ct
, int ncolors
, XpmColor
***oldct
)
68 XpmColor
**colorTable
, **color
;
71 colorTable
= (XpmColor
**) XpmMalloc(ncolors
* sizeof(XpmColor
*));
76 for (a
= 0, color
= colorTable
; a
< ncolors
; a
++, color
++, ct
++)
83 FreeOldColorTable(XpmColor
**colorTable
, int ncolors
)
90 for (a
= 0, color
= colorTable
; a
< ncolors
; a
++, color
++) {
91 for (b
= 0, sptr
= (char **) *color
; b
<= NKEYS
; b
++, sptr
++)
104 * Free the computed color table
107 xpmFreeColorTable(XpmColor
*colorTable
, int ncolors
)
114 for (a
= 0, color
= colorTable
; a
< ncolors
; a
++, color
++) {
115 for (b
= 0, sptr
= (char **) color
; b
<= NKEYS
; b
++, sptr
++)
124 * Free array of extensions
127 XpmFreeExtensions(XpmExtension
*extensions
, int nextensions
)
129 unsigned int i
, j
, nlines
;
134 for (i
= 0, ext
= extensions
; i
< nextensions
; i
++, ext
++) {
137 nlines
= ext
->nlines
;
138 for (j
= 0, sptr
= ext
->lines
; j
< nlines
; j
++, sptr
++)
150 * Return the XpmAttributes structure size
156 return sizeof(XpmAttributes
);
160 * Init returned data to free safely later on
163 xpmInitAttributes(XpmAttributes
*attributes
)
166 attributes
->pixels
= NULL
;
167 attributes
->npixels
= 0;
168 attributes
->colorTable
= NULL
;
169 attributes
->ncolors
= 0;
170 /* 3.2 backward compatibility code */
171 attributes
->hints_cmt
= NULL
;
172 attributes
->colors_cmt
= NULL
;
173 attributes
->pixels_cmt
= NULL
;
175 attributes
->extensions
= NULL
;
176 attributes
->nextensions
= 0;
181 * Fill in the XpmAttributes with the XpmImage and the XpmInfo
184 xpmSetAttributes(XpmAttributes
*attributes
, XpmImage
*image
, XpmInfo
*info
)
186 if (attributes
->valuemask
& XpmReturnColorTable
) {
187 attributes
->colorTable
= image
->colorTable
;
188 attributes
->ncolors
= image
->ncolors
;
190 /* avoid deletion of copied data */
192 image
->colorTable
= NULL
;
194 /* 3.2 backward compatibility code */
195 else if (attributes
->valuemask
& XpmReturnInfos
) {
198 ErrorStatus
= CreateOldColorTable(image
->colorTable
, image
->ncolors
,
200 &attributes
->colorTable
);
202 /* if error just say we can't return requested data */
203 if (ErrorStatus
!= XpmSuccess
) {
204 attributes
->valuemask
&= ~XpmReturnInfos
;
205 if (!(attributes
->valuemask
& XpmReturnPixels
)) {
206 XpmFree(attributes
->pixels
);
207 attributes
->pixels
= NULL
;
208 attributes
->npixels
= 0;
210 attributes
->ncolors
= 0;
212 attributes
->ncolors
= image
->ncolors
;
213 attributes
->hints_cmt
= info
->hints_cmt
;
214 attributes
->colors_cmt
= info
->colors_cmt
;
215 attributes
->pixels_cmt
= info
->pixels_cmt
;
217 /* avoid deletion of copied data */
219 image
->colorTable
= NULL
;
220 info
->hints_cmt
= NULL
;
221 info
->colors_cmt
= NULL
;
222 info
->pixels_cmt
= NULL
;
226 if (attributes
->valuemask
& XpmReturnExtensions
) {
227 attributes
->extensions
= info
->extensions
;
228 attributes
->nextensions
= info
->nextensions
;
230 /* avoid deletion of copied data */
231 info
->extensions
= NULL
;
232 info
->nextensions
= 0;
234 if (info
->valuemask
& XpmHotspot
) {
235 attributes
->valuemask
|= XpmHotspot
;
236 attributes
->x_hotspot
= info
->x_hotspot
;
237 attributes
->y_hotspot
= info
->y_hotspot
;
239 attributes
->valuemask
|= XpmCharsPerPixel
;
240 attributes
->cpp
= image
->cpp
;
241 attributes
->valuemask
|= XpmSize
;
242 attributes
->width
= image
->width
;
243 attributes
->height
= image
->height
;
247 * Free the XpmAttributes structure members
248 * but the structure itself
251 XpmFreeAttributes(XpmAttributes
*attributes
)
253 if (attributes
->valuemask
& XpmReturnPixels
&& attributes
->npixels
) {
254 XpmFree(attributes
->pixels
);
255 attributes
->pixels
= NULL
;
256 attributes
->npixels
= 0;
258 if (attributes
->valuemask
& XpmReturnColorTable
) {
259 xpmFreeColorTable(attributes
->colorTable
, attributes
->ncolors
);
260 attributes
->colorTable
= NULL
;
261 attributes
->ncolors
= 0;
263 /* 3.2 backward compatibility code */
264 else if (attributes
->valuemask
& XpmInfos
) {
265 if (attributes
->colorTable
) {
266 FreeOldColorTable((XpmColor
**) attributes
->colorTable
,
267 attributes
->ncolors
);
268 attributes
->colorTable
= NULL
;
269 attributes
->ncolors
= 0;
271 if (attributes
->hints_cmt
) {
272 XpmFree(attributes
->hints_cmt
);
273 attributes
->hints_cmt
= NULL
;
275 if (attributes
->colors_cmt
) {
276 XpmFree(attributes
->colors_cmt
);
277 attributes
->colors_cmt
= NULL
;
279 if (attributes
->pixels_cmt
) {
280 XpmFree(attributes
->pixels_cmt
);
281 attributes
->pixels_cmt
= NULL
;
283 if (attributes
->pixels
) {
284 XpmFree(attributes
->pixels
);
285 attributes
->pixels
= NULL
;
286 attributes
->npixels
= 0;
290 if (attributes
->valuemask
& XpmReturnExtensions
291 && attributes
->nextensions
) {
292 XpmFreeExtensions(attributes
->extensions
, attributes
->nextensions
);
293 attributes
->extensions
= NULL
;
294 attributes
->nextensions
= 0;
296 attributes
->valuemask
= 0;
300 * Init returned data to free safely later on
303 xpmInitXpmImage(XpmImage
*image
)
306 image
->colorTable
= NULL
;
311 * Free the XpmImage data which have been allocated
314 XpmFreeXpmImage(XpmImage
*image
)
316 if (image
->colorTable
)
317 xpmFreeColorTable(image
->colorTable
, image
->ncolors
);
318 XpmFree(image
->data
);
323 * Init returned data to free safely later on
326 xpmInitXpmInfo(XpmInfo
*info
)
329 info
->hints_cmt
= NULL
;
330 info
->colors_cmt
= NULL
;
331 info
->pixels_cmt
= NULL
;
332 info
->extensions
= NULL
;
333 info
->nextensions
= 0;
338 * Free the XpmInfo data which have been allocated
341 XpmFreeXpmInfo(XpmInfo
*info
)
344 if (info
->valuemask
& XpmComments
) {
345 if (info
->hints_cmt
) {
346 XpmFree(info
->hints_cmt
);
347 info
->hints_cmt
= NULL
;
349 if (info
->colors_cmt
) {
350 XpmFree(info
->colors_cmt
);
351 info
->colors_cmt
= NULL
;
353 if (info
->pixels_cmt
) {
354 XpmFree(info
->pixels_cmt
);
355 info
->pixels_cmt
= NULL
;
358 if (info
->valuemask
& XpmReturnExtensions
&& info
->nextensions
) {
359 XpmFreeExtensions(info
->extensions
, info
->nextensions
);
360 info
->extensions
= NULL
;
361 info
->nextensions
= 0;
368 * Set the XpmInfo valuemask to retrieve required info
371 xpmSetInfoMask(XpmInfo
*info
, XpmAttributes
*attributes
)
374 if (attributes
->valuemask
& XpmReturnInfos
)
375 info
->valuemask
|= XpmReturnComments
;
376 if (attributes
->valuemask
& XpmReturnExtensions
)
377 info
->valuemask
|= XpmReturnExtensions
;
381 * Fill in the XpmInfo with the XpmAttributes
384 xpmSetInfo(XpmInfo
*info
, XpmAttributes
*attributes
)
387 if (attributes
->valuemask
& XpmInfos
) {
388 info
->valuemask
|= XpmComments
| XpmColorTable
;
389 info
->hints_cmt
= attributes
->hints_cmt
;
390 info
->colors_cmt
= attributes
->colors_cmt
;
391 info
->pixels_cmt
= attributes
->pixels_cmt
;
393 if (attributes
->valuemask
& XpmExtensions
) {
394 info
->valuemask
|= XpmExtensions
;
395 info
->extensions
= attributes
->extensions
;
396 info
->nextensions
= attributes
->nextensions
;
398 if (attributes
->valuemask
& XpmHotspot
) {
399 info
->valuemask
|= XpmHotspot
;
400 info
->x_hotspot
= attributes
->x_hotspot
;
401 info
->y_hotspot
= attributes
->y_hotspot
;
408 * in case strdup is not provided by the system here is one
409 * which does the trick
415 int l
= strlen(s1
) + 1;
417 if (s2
= (char *) XpmMalloc(l
))
425 atoui(register char *p
, unsigned int l
, unsigned int *ui_return
)
427 register unsigned int n
, i
;
430 for (i
= 0; i
< l
; i
++)
431 if (*p
>= '0' && *p
<= '9')
432 n
= n
* 10 + *p
++ - '0';
436 if (i
!= 0 && i
== l
) {
445 * File / Buffer utilities
448 XpmReadFileToBuffer(char *filename
, char **buffer_return
)
455 *buffer_return
= NULL
;
457 fd
= open(filename
, O_RDONLY
);
459 return XpmOpenFailed
;
461 if (fstat(fd
, &stats
)) {
463 return XpmOpenFailed
;
465 fp
= fdopen(fd
, "r");
468 return XpmOpenFailed
;
470 len
= (int) stats
.st_size
;
471 ptr
= (char *) XpmMalloc(len
+ 1);
476 fcheck
= fread(ptr
, len
, 1, fp
);
480 return XpmOpenFailed
;
483 *buffer_return
= ptr
;
488 XpmWriteFileFromBuffer(char *filename
, char *buffer
)
491 FILE *fp
= fopen(filename
, "w");
494 return XpmOpenFailed
;
496 len
= strlen(buffer
);
497 fcheck
= fwrite(buffer
, len
, 1, fp
);
500 return XpmOpenFailed
;
507 * Small utility function
510 XpmGetErrorString(int errcode
)
514 return ("XpmColorError");
516 return ("XpmSuccess");
518 return ("XpmOpenFailed");
520 return ("XpmFileInvalid");
522 return ("XpmNoMemory");
524 return ("XpmColorFailed");
526 return ("Invalid XpmError");
531 * The following function provides a way to figure out if the linked library is
532 * newer or older than the one with which a program has been first compiled.
537 return XpmIncludeVersion
;
543 xpmCreatePixmapFromImage(Display
*display
, Drawable d
, XImage
*ximage
, Pixmap
*pixmap_return
)
547 *pixmap_return
= XCreatePixmap(display
, d
, ximage
->width
,
548 ximage
->height
, ximage
->depth
);
549 gc
= XCreateGC(display
, *pixmap_return
, 0, NULL
);
551 XPutImage(display
, *pixmap_return
, gc
, ximage
, 0, 0, 0, 0,
552 ximage
->width
, ximage
->height
);
554 XFreeGC(display
, gc
);
558 xpmCreateImageFromPixmap(Display
*display
, Pixmap pixmap
, XImage
**ximage_return
, unsigned int *width
, unsigned int *height
)
564 if (*width
== 0 && *height
== 0)
565 XGetGeometry(display
, pixmap
, &win
, &dummy
, &dummy
,
566 width
, height
, &dum
, &dum
);
568 *ximage_return
= XGetImage(display
, pixmap
, 0, 0, *width
, *height
,