2 * Copyright (C) 1989-95 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 * Functions related to the XpmAttributes structure *
32 * Developed by Arnaud Le Hors *
33 \*****************************************************************************/
35 /* October 2004, source code review by Thomas Biege <thomas@suse.de> */
42 /* 3.2 backward compatibility code */
43 LFUNC(CreateOldColorTable, int, (XpmColor *ct, unsigned int ncolors,
46 LFUNC(FreeOldColorTable, void, (XpmColor **colorTable, unsigned int ncolors));
49 * Create a colortable compatible with the old style colortable
57 XpmColor **colorTable, **color;
60 if (ncolors >= UINT_MAX / sizeof(XpmColor *))
63 colorTable = (XpmColor **) XpmMalloc(ncolors * sizeof(XpmColor *));
68 for (a = 0, color = colorTable; a < ncolors; a++, color++, ct++)
76 XpmColor **colorTable,
84 for (a = 0, color = colorTable; a < ncolors; a++, color++) {
85 for (b = 0, sptr = (char **) *color; b <= NKEYS; b++, sptr++)
97 * Free the computed color table
101 XpmColor *colorTable,
109 for (a = 0, color = colorTable; a < ncolors; a++, color++) {
110 for (b = 0, sptr = (char **) color; b <= NKEYS; b++, sptr++)
119 * Free array of extensions
123 XpmExtension *extensions,
126 unsigned int i, j, nlines;
130 if (extensions && nextensions > 0) {
131 for (i = 0, ext = extensions; i < nextensions; i++, ext++) {
134 nlines = ext->nlines;
135 for (j = 0, sptr = ext->lines; j < nlines; j++, sptr++)
146 * Return the XpmAttributes structure size
150 XpmAttributesSize(void)
152 return sizeof(XpmAttributes);
156 * Init returned data to free safely later on
159 xpmInitAttributes(XpmAttributes *attributes)
162 attributes->pixels = NULL;
163 attributes->npixels = 0;
164 attributes->colorTable = NULL;
165 attributes->ncolors = 0;
166 /* 3.2 backward compatibility code */
167 attributes->hints_cmt = NULL;
168 attributes->colors_cmt = NULL;
169 attributes->pixels_cmt = NULL;
171 if (attributes->valuemask & XpmReturnExtensions) {
172 attributes->extensions = NULL;
173 attributes->nextensions = 0;
175 if (attributes->valuemask & XpmReturnAllocPixels) {
176 attributes->alloc_pixels = NULL;
177 attributes->nalloc_pixels = 0;
183 * Fill in the XpmAttributes with the XpmImage and the XpmInfo
187 XpmAttributes *attributes,
191 if (attributes->valuemask & XpmReturnColorTable) {
192 attributes->colorTable = image->colorTable;
193 attributes->ncolors = image->ncolors;
195 /* avoid deletion of copied data */
197 image->colorTable = NULL;
199 /* 3.2 backward compatibility code */
200 else if (attributes->valuemask & XpmReturnInfos) {
203 ErrorStatus = CreateOldColorTable(image->colorTable, image->ncolors,
205 &attributes->colorTable);
207 /* if error just say we can't return requested data */
208 if (ErrorStatus != XpmSuccess) {
209 attributes->valuemask &= ~XpmReturnInfos;
210 if (!(attributes->valuemask & XpmReturnPixels)) {
211 XpmFree(attributes->pixels);
212 attributes->pixels = NULL;
213 attributes->npixels = 0;
215 attributes->ncolors = 0;
217 attributes->ncolors = image->ncolors;
218 attributes->hints_cmt = info->hints_cmt;
219 attributes->colors_cmt = info->colors_cmt;
220 attributes->pixels_cmt = info->pixels_cmt;
222 /* avoid deletion of copied data */
224 image->colorTable = NULL;
225 info->hints_cmt = NULL;
226 info->colors_cmt = NULL;
227 info->pixels_cmt = NULL;
231 if (attributes->valuemask & XpmReturnExtensions) {
232 attributes->extensions = info->extensions;
233 attributes->nextensions = info->nextensions;
235 /* avoid deletion of copied data */
236 info->extensions = NULL;
237 info->nextensions = 0;
239 if (info->valuemask & XpmHotspot) {
240 attributes->valuemask |= XpmHotspot;
241 attributes->x_hotspot = info->x_hotspot;
242 attributes->y_hotspot = info->y_hotspot;
244 attributes->valuemask |= XpmCharsPerPixel;
245 attributes->cpp = image->cpp;
246 attributes->valuemask |= XpmSize;
247 attributes->width = image->width;
248 attributes->height = image->height;
252 * Free the XpmAttributes structure members
253 * but the structure itself
256 XpmFreeAttributes(XpmAttributes *attributes)
258 if (attributes->valuemask & XpmReturnPixels && attributes->npixels) {
259 XpmFree(attributes->pixels);
260 attributes->pixels = NULL;
261 attributes->npixels = 0;
263 if (attributes->valuemask & XpmReturnColorTable) {
264 xpmFreeColorTable(attributes->colorTable, attributes->ncolors);
265 attributes->colorTable = NULL;
266 attributes->ncolors = 0;
268 /* 3.2 backward compatibility code */
269 else if (attributes->valuemask & XpmInfos) {
270 if (attributes->colorTable) {
271 FreeOldColorTable((XpmColor **) attributes->colorTable,
272 attributes->ncolors);
273 attributes->colorTable = NULL;
274 attributes->ncolors = 0;
276 if (attributes->hints_cmt) {
277 XpmFree(attributes->hints_cmt);
278 attributes->hints_cmt = NULL;
280 if (attributes->colors_cmt) {
281 XpmFree(attributes->colors_cmt);
282 attributes->colors_cmt = NULL;
284 if (attributes->pixels_cmt) {
285 XpmFree(attributes->pixels_cmt);
286 attributes->pixels_cmt = NULL;
288 if (attributes->pixels) {
289 XpmFree(attributes->pixels);
290 attributes->pixels = NULL;
291 attributes->npixels = 0;
295 if (attributes->valuemask & XpmReturnExtensions
296 && attributes->nextensions) {
297 XpmFreeExtensions(attributes->extensions, attributes->nextensions);
298 attributes->extensions = NULL;
299 attributes->nextensions = 0;
301 if (attributes->valuemask & XpmReturnAllocPixels
302 && attributes->nalloc_pixels) {
303 XpmFree(attributes->alloc_pixels);
304 attributes->alloc_pixels = NULL;
305 attributes->nalloc_pixels = 0;
307 attributes->valuemask = 0;