Tizen 2.0 Release
[framework/uifw/xorg/lib/libxpm.git] / src / Attrib.c
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 */
43 LFUNC(CreateOldColorTable, int, (XpmColor *ct, unsigned int ncolors,
44                                  XpmColor ***oldct));
45
46 LFUNC(FreeOldColorTable, void, (XpmColor **colorTable, unsigned int ncolors));
47
48 /*
49  * Create a colortable compatible with the old style colortable
50  */
51 static int
52 CreateOldColorTable(
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
74 static void
75 FreeOldColorTable(
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  */
99 void
100 xpmFreeColorTable(
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  */
121 void
122 XpmFreeExtensions(
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
149 int
150 XpmAttributesSize(void)
151 {
152     return sizeof(XpmAttributes);
153 }
154
155 /*
156  * Init returned data to free safely later on
157  */
158 void
159 xpmInitAttributes(XpmAttributes *attributes)
160 {
161     if (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;
170 /* end 3.2 bc */
171         if (attributes->valuemask & XpmReturnExtensions) {
172             attributes->extensions = NULL;
173             attributes->nextensions = 0;
174         }
175         if (attributes->valuemask & XpmReturnAllocPixels) {
176             attributes->alloc_pixels = NULL;
177             attributes->nalloc_pixels = 0;
178         }
179     }
180 }
181
182 /*
183  * Fill in the XpmAttributes with the XpmImage and the XpmInfo
184  */
185 void
186 xpmSetAttributes(
187     XpmAttributes       *attributes,
188     XpmImage            *image,
189     XpmInfo             *info)
190 {
191     if (attributes->valuemask & XpmReturnColorTable) {
192         attributes->colorTable = image->colorTable;
193         attributes->ncolors = image->ncolors;
194
195         /* avoid deletion of copied data */
196         image->ncolors = 0;
197         image->colorTable = NULL;
198     }
199 /* 3.2 backward compatibility code */
200     else if (attributes->valuemask & XpmReturnInfos) {
201         int ErrorStatus;
202
203         ErrorStatus = CreateOldColorTable(image->colorTable, image->ncolors,
204                                           (XpmColor ***)
205                                           &attributes->colorTable);
206
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;
214             }
215             attributes->ncolors = 0;
216         } else {
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;
221
222             /* avoid deletion of copied data */
223             image->ncolors = 0;
224             image->colorTable = NULL;
225             info->hints_cmt = NULL;
226             info->colors_cmt = NULL;
227             info->pixels_cmt = NULL;
228         }
229     }
230 /* end 3.2 bc */
231     if (attributes->valuemask & XpmReturnExtensions) {
232         attributes->extensions = info->extensions;
233         attributes->nextensions = info->nextensions;
234
235         /* avoid deletion of copied data */
236         info->extensions = NULL;
237         info->nextensions = 0;
238     }
239     if (info->valuemask & XpmHotspot) {
240         attributes->valuemask |= XpmHotspot;
241         attributes->x_hotspot = info->x_hotspot;
242         attributes->y_hotspot = info->y_hotspot;
243     }
244     attributes->valuemask |= XpmCharsPerPixel;
245     attributes->cpp = image->cpp;
246     attributes->valuemask |= XpmSize;
247     attributes->width = image->width;
248     attributes->height = image->height;
249 }
250
251 /*
252  * Free the XpmAttributes structure members
253  * but the structure itself
254  */
255 void
256 XpmFreeAttributes(XpmAttributes *attributes)
257 {
258     if (attributes->valuemask & XpmReturnPixels && attributes->npixels) {
259         XpmFree(attributes->pixels);
260         attributes->pixels = NULL;
261         attributes->npixels = 0;
262     }
263     if (attributes->valuemask & XpmReturnColorTable) {
264         xpmFreeColorTable(attributes->colorTable, attributes->ncolors);
265         attributes->colorTable = NULL;
266         attributes->ncolors = 0;
267     }
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;
275         }
276         if (attributes->hints_cmt) {
277             XpmFree(attributes->hints_cmt);
278             attributes->hints_cmt = NULL;
279         }
280         if (attributes->colors_cmt) {
281             XpmFree(attributes->colors_cmt);
282             attributes->colors_cmt = NULL;
283         }
284         if (attributes->pixels_cmt) {
285             XpmFree(attributes->pixels_cmt);
286             attributes->pixels_cmt = NULL;
287         }
288         if (attributes->pixels) {
289             XpmFree(attributes->pixels);
290             attributes->pixels = NULL;
291             attributes->npixels = 0;
292         }
293     }
294 /* end 3.2 bc */
295     if (attributes->valuemask & XpmReturnExtensions
296         && attributes->nextensions) {
297         XpmFreeExtensions(attributes->extensions, attributes->nextensions);
298         attributes->extensions = NULL;
299         attributes->nextensions = 0;
300     }
301     if (attributes->valuemask & XpmReturnAllocPixels
302         && attributes->nalloc_pixels) {
303         XpmFree(attributes->alloc_pixels);
304         attributes->alloc_pixels = NULL;
305         attributes->nalloc_pixels = 0;
306     }
307     attributes->valuemask = 0;
308 }