Attrib.c revision a966c04f
1/*
2 * Copyright (C) 1989-95 GROUPE BULL
3 *
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:
10 *
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
13 *
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.
20 *
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.
24 */
25
26/*****************************************************************************\
27* Attrib.c:                                                                   *
28*                                                                             *
29*  XPM library                                                                *
30*  Functions related to the XpmAttributes structure                           *
31*                                                                             *
32*  Developed by Arnaud Le Hors                                                *
33\*****************************************************************************/
34
35/* October 2004, source code review by Thomas Biege <thomas@suse.de> */
36
37#ifdef HAVE_CONFIG_H
38#include <config.h>
39#endif
40#include "XpmI.h"
41
42/* 3.2 backward compatibility code */
43LFUNC(CreateOldColorTable, int, (XpmColor *ct, unsigned int ncolors,
44				 XpmColor ***oldct));
45
46LFUNC(FreeOldColorTable, void, (XpmColor **colorTable, unsigned int ncolors));
47
48/*
49 * Create a colortable compatible with the old style colortable
50 */
51static int
52CreateOldColorTable(ct, ncolors, oldct)
53    XpmColor *ct;
54    unsigned int ncolors;
55    XpmColor ***oldct;
56{
57    XpmColor **colorTable, **color;
58    unsigned int a;
59
60    if (ncolors >= UINT_MAX / sizeof(XpmColor *))
61	return XpmNoMemory;
62
63    colorTable = (XpmColor **) XpmMalloc(ncolors * sizeof(XpmColor *));
64    if (!colorTable) {
65	*oldct = NULL;
66	return (XpmNoMemory);
67    }
68    for (a = 0, color = colorTable; a < ncolors; a++, color++, ct++)
69	*color = ct;
70    *oldct = colorTable;
71    return (XpmSuccess);
72}
73
74static void
75FreeOldColorTable(colorTable, ncolors)
76    XpmColor **colorTable;
77    unsigned int ncolors;
78{
79    unsigned int a, b;
80    XpmColor **color;
81    char **sptr;
82
83    if (colorTable) {
84	for (a = 0, color = colorTable; a < ncolors; a++, color++) {
85	    for (b = 0, sptr = (char **) *color; b <= NKEYS; b++, sptr++)
86		if (*sptr)
87		    XpmFree(*sptr);
88	}
89	XpmFree(*colorTable);
90	XpmFree(colorTable);
91    }
92}
93
94/* end 3.2 bc */
95
96/*
97 * Free the computed color table
98 */
99void
100xpmFreeColorTable(colorTable, ncolors)
101    XpmColor *colorTable;
102    int ncolors;
103{
104    int a, b;
105    XpmColor *color;
106    char **sptr;
107
108    if (colorTable) {
109	for (a = 0, color = colorTable; a < ncolors; a++, color++) {
110	    for (b = 0, sptr = (char **) color; b <= NKEYS; b++, sptr++)
111		if (*sptr)
112		    XpmFree(*sptr);
113	}
114	XpmFree(colorTable);
115    }
116}
117
118/*
119 * Free array of extensions
120 */
121void
122XpmFreeExtensions(extensions, nextensions)
123    XpmExtension *extensions;
124    int nextensions;
125{
126    unsigned int i, j, nlines;
127    XpmExtension *ext;
128    char **sptr;
129
130    if (extensions  && nextensions > 0) {
131	for (i = 0, ext = extensions; i < nextensions; i++, ext++) {
132	    if (ext->name)
133		XpmFree(ext->name);
134	    nlines = ext->nlines;
135	    for (j = 0, sptr = ext->lines; j < nlines; j++, sptr++)
136		if (sptr && *sptr)
137		    XpmFree(*sptr);
138	    if (ext->lines)
139		XpmFree(ext->lines);
140	}
141	XpmFree(extensions);
142    }
143}
144
145/*
146 * Return the XpmAttributes structure size
147 */
148
149int
150XpmAttributesSize()
151{
152    return sizeof(XpmAttributes);
153}
154
155/*
156 * Init returned data to free safely later on
157 */
158void
159xpmInitAttributes(attributes)
160    XpmAttributes *attributes;
161{
162    if (attributes) {
163	attributes->pixels = NULL;
164	attributes->npixels = 0;
165	attributes->colorTable = NULL;
166	attributes->ncolors = 0;
167/* 3.2 backward compatibility code */
168	attributes->hints_cmt = NULL;
169	attributes->colors_cmt = NULL;
170	attributes->pixels_cmt = NULL;
171/* end 3.2 bc */
172	if (attributes->valuemask & XpmReturnExtensions) {
173	    attributes->extensions = NULL;
174	    attributes->nextensions = 0;
175	}
176	if (attributes->valuemask & XpmReturnAllocPixels) {
177	    attributes->alloc_pixels = NULL;
178	    attributes->nalloc_pixels = 0;
179	}
180    }
181}
182
183/*
184 * Fill in the XpmAttributes with the XpmImage and the XpmInfo
185 */
186void
187xpmSetAttributes(attributes, image, info)
188    XpmAttributes *attributes;
189    XpmImage *image;
190    XpmInfo *info;
191{
192    if (attributes->valuemask & XpmReturnColorTable) {
193	attributes->colorTable = image->colorTable;
194	attributes->ncolors = image->ncolors;
195
196	/* avoid deletion of copied data */
197	image->ncolors = 0;
198	image->colorTable = NULL;
199    }
200/* 3.2 backward compatibility code */
201    else if (attributes->valuemask & XpmReturnInfos) {
202	int ErrorStatus;
203
204	ErrorStatus = CreateOldColorTable(image->colorTable, image->ncolors,
205					  (XpmColor ***)
206					  &attributes->colorTable);
207
208	/* if error just say we can't return requested data */
209	if (ErrorStatus != XpmSuccess) {
210	    attributes->valuemask &= ~XpmReturnInfos;
211	    if (!(attributes->valuemask & XpmReturnPixels)) {
212		XpmFree(attributes->pixels);
213		attributes->pixels = NULL;
214		attributes->npixels = 0;
215	    }
216	    attributes->ncolors = 0;
217	} else {
218	    attributes->ncolors = image->ncolors;
219	    attributes->hints_cmt = info->hints_cmt;
220	    attributes->colors_cmt = info->colors_cmt;
221	    attributes->pixels_cmt = info->pixels_cmt;
222
223	    /* avoid deletion of copied data */
224	    image->ncolors = 0;
225	    image->colorTable = NULL;
226	    info->hints_cmt = NULL;
227	    info->colors_cmt = NULL;
228	    info->pixels_cmt = NULL;
229	}
230    }
231/* end 3.2 bc */
232    if (attributes->valuemask & XpmReturnExtensions) {
233	attributes->extensions = info->extensions;
234	attributes->nextensions = info->nextensions;
235
236	/* avoid deletion of copied data */
237	info->extensions = NULL;
238	info->nextensions = 0;
239    }
240    if (info->valuemask & XpmHotspot) {
241	attributes->valuemask |= XpmHotspot;
242	attributes->x_hotspot = info->x_hotspot;
243	attributes->y_hotspot = info->y_hotspot;
244    }
245    attributes->valuemask |= XpmCharsPerPixel;
246    attributes->cpp = image->cpp;
247    attributes->valuemask |= XpmSize;
248    attributes->width = image->width;
249    attributes->height = image->height;
250}
251
252/*
253 * Free the XpmAttributes structure members
254 * but the structure itself
255 */
256void
257XpmFreeAttributes(attributes)
258    XpmAttributes *attributes;
259{
260    if (attributes->valuemask & XpmReturnPixels && attributes->npixels) {
261	XpmFree(attributes->pixels);
262	attributes->pixels = NULL;
263	attributes->npixels = 0;
264    }
265    if (attributes->valuemask & XpmReturnColorTable) {
266	xpmFreeColorTable(attributes->colorTable, attributes->ncolors);
267	attributes->colorTable = NULL;
268	attributes->ncolors = 0;
269    }
270/* 3.2 backward compatibility code */
271    else if (attributes->valuemask & XpmInfos) {
272	if (attributes->colorTable) {
273	    FreeOldColorTable((XpmColor **) attributes->colorTable,
274			      attributes->ncolors);
275	    attributes->colorTable = NULL;
276	    attributes->ncolors = 0;
277	}
278	if (attributes->hints_cmt) {
279	    XpmFree(attributes->hints_cmt);
280	    attributes->hints_cmt = NULL;
281	}
282	if (attributes->colors_cmt) {
283	    XpmFree(attributes->colors_cmt);
284	    attributes->colors_cmt = NULL;
285	}
286	if (attributes->pixels_cmt) {
287	    XpmFree(attributes->pixels_cmt);
288	    attributes->pixels_cmt = NULL;
289	}
290	if (attributes->pixels) {
291	    XpmFree(attributes->pixels);
292	    attributes->pixels = NULL;
293	    attributes->npixels = 0;
294	}
295    }
296/* end 3.2 bc */
297    if (attributes->valuemask & XpmReturnExtensions
298	&& attributes->nextensions) {
299	XpmFreeExtensions(attributes->extensions, attributes->nextensions);
300	attributes->extensions = NULL;
301	attributes->nextensions = 0;
302    }
303    if (attributes->valuemask & XpmReturnAllocPixels
304	&& attributes->nalloc_pixels) {
305	XpmFree(attributes->alloc_pixels);
306	attributes->alloc_pixels = NULL;
307	attributes->nalloc_pixels = 0;
308    }
309    attributes->valuemask = 0;
310}
311