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