Back to index

plt-scheme  4.2.1
Attrib.c
Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 1989-95 GROUPE BULL
00003  *
00004  * Permission is hereby granted, free of charge, to any person obtaining a copy
00005  * of this software and associated documentation files (the "Software"), to
00006  * deal in the Software without restriction, including without limitation the
00007  * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
00008  * sell copies of the Software, and to permit persons to whom the Software is
00009  * furnished to do so, subject to the following conditions:
00010  *
00011  * The above copyright notice and this permission notice shall be included in
00012  * all copies or substantial portions of the Software.
00013  *
00014  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
00015  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00016  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
00017  * GROUPE BULL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
00018  * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
00019  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
00020  *
00021  * Except as contained in this notice, the name of GROUPE BULL shall not be
00022  * used in advertising or otherwise to promote the sale, use or other dealings
00023  * in this Software without prior written authorization from GROUPE BULL.
00024  */
00025 
00026 /*****************************************************************************\
00027 * Attrib.c:                                                                   *
00028 *                                                                             *
00029 *  XPM library                                                                *
00030 *  Functions related to the XpmAttributes structure                           *
00031 *                                                                             *
00032 *  Developed by Arnaud Le Hors                                                *
00033 \*****************************************************************************/
00034 
00035 #include "xpmP.h"
00036 
00037 /* 3.2 backward compatibility code */
00038 LFUNC(CreateOldColorTable, int, (XpmColor *ct, int ncolors,
00039                              XpmColor ***oldct));
00040 
00041 LFUNC(FreeOldColorTable, void, (XpmColor **colorTable, int ncolors));
00042 
00043 /*
00044  * Create a colortable compatible with the old style colortable
00045  */
00046 static int
00047 CreateOldColorTable(ct, ncolors, oldct)
00048     XpmColor *ct;
00049     int ncolors;
00050     XpmColor ***oldct;
00051 {
00052     XpmColor **colorTable, **color;
00053     int a;
00054 
00055     colorTable = (XpmColor **) XpmMalloc(ncolors * sizeof(XpmColor *));
00056     if (!colorTable) {
00057        *oldct = NULL;
00058        return (XpmNoMemory);
00059     }
00060     for (a = 0, color = colorTable; a < ncolors; a++, color++, ct++)
00061        *color = ct;
00062     *oldct = colorTable;
00063     return (XpmSuccess);
00064 }
00065 
00066 static void
00067 FreeOldColorTable(colorTable, ncolors)
00068     XpmColor **colorTable;
00069     int ncolors;
00070 {
00071     int a, b;
00072     XpmColor **color;
00073     char **sptr;
00074 
00075     if (colorTable) {
00076        for (a = 0, color = colorTable; a < ncolors; a++, color++) {
00077            for (b = 0, sptr = (char **) *color; b <= NKEYS; b++, sptr++)
00078               if (*sptr)
00079                   XpmFree(*sptr);
00080        }
00081        XpmFree(*colorTable);
00082        XpmFree(colorTable);
00083     }
00084 }
00085 
00086 /* end 3.2 bc */
00087 
00088 /*
00089  * Free the computed color table
00090  */
00091 void
00092 xpmFreeColorTable(colorTable, ncolors)
00093     XpmColor *colorTable;
00094     int ncolors;
00095 {
00096     int a, b;
00097     XpmColor *color;
00098     char **sptr;
00099 
00100     if (colorTable) {
00101        for (a = 0, color = colorTable; a < ncolors; a++, color++) {
00102            for (b = 0, sptr = (char **) color; b <= NKEYS; b++, sptr++)
00103               if (*sptr)
00104                   XpmFree(*sptr);
00105        }
00106        XpmFree(colorTable);
00107     }
00108 }
00109 
00110 /*
00111  * Free array of extensions
00112  */
00113 void
00114 XpmFreeExtensions(extensions, nextensions)
00115     XpmExtension *extensions;
00116     int nextensions;
00117 {
00118     unsigned int i, j, nlines;
00119     XpmExtension *ext;
00120     char **sptr;
00121 
00122     if (extensions) {
00123        for (i = 0, ext = extensions; i < nextensions; i++, ext++) {
00124            if (ext->name)
00125               XpmFree(ext->name);
00126            nlines = ext->nlines;
00127            for (j = 0, sptr = ext->lines; j < nlines; j++, sptr++)
00128               if (*sptr)
00129                   XpmFree(*sptr);
00130            if (ext->lines)
00131               XpmFree(ext->lines);
00132        }
00133        XpmFree(extensions);
00134     }
00135 }
00136 
00137 /*
00138  * Return the XpmAttributes structure size
00139  */
00140 
00141 int
00142 XpmAttributesSize()
00143 {
00144     return sizeof(XpmAttributes);
00145 }
00146 
00147 /*
00148  * Init returned data to free safely later on
00149  */
00150 void
00151 xpmInitAttributes(attributes)
00152     XpmAttributes *attributes;
00153 {
00154     if (attributes) {
00155        attributes->pixels = NULL;
00156        attributes->npixels = 0;
00157        attributes->colorTable = NULL;
00158        attributes->ncolors = 0;
00159 /* 3.2 backward compatibility code */
00160        attributes->hints_cmt = NULL;
00161        attributes->colors_cmt = NULL;
00162        attributes->pixels_cmt = NULL;
00163 /* end 3.2 bc */
00164        attributes->extensions = NULL;
00165        attributes->nextensions = 0;
00166        attributes->alloc_pixels = NULL;
00167        attributes->nalloc_pixels = 0;
00168     }
00169 }
00170 
00171 /*
00172  * Fill in the XpmAttributes with the XpmImage and the XpmInfo
00173  */
00174 void
00175 xpmSetAttributes(attributes, image, info)
00176     XpmAttributes *attributes;
00177     XpmImage *image;
00178     XpmInfo *info;
00179 {
00180     if (attributes->valuemask & XpmReturnColorTable) {
00181        attributes->colorTable = image->colorTable;
00182        attributes->ncolors = image->ncolors;
00183 
00184        /* avoid deletion of copied data */
00185        image->ncolors = 0;
00186        image->colorTable = NULL;
00187     }
00188 /* 3.2 backward compatibility code */
00189     else if (attributes->valuemask & XpmReturnInfos) {
00190        int ErrorStatus;
00191 
00192        ErrorStatus = CreateOldColorTable(image->colorTable, image->ncolors,
00193                                      (XpmColor ***)
00194                                      &attributes->colorTable);
00195 
00196        /* if error just say we can't return requested data */
00197        if (ErrorStatus != XpmSuccess) {
00198            attributes->valuemask &= ~XpmReturnInfos;
00199            if (!(attributes->valuemask & XpmReturnPixels)) {
00200               XpmFree(attributes->pixels);
00201               attributes->pixels = NULL;
00202               attributes->npixels = 0;
00203            }
00204            attributes->ncolors = 0;
00205        } else {
00206            attributes->ncolors = image->ncolors;
00207            attributes->hints_cmt = info->hints_cmt;
00208            attributes->colors_cmt = info->colors_cmt;
00209            attributes->pixels_cmt = info->pixels_cmt;
00210 
00211            /* avoid deletion of copied data */
00212            image->ncolors = 0;
00213            image->colorTable = NULL;
00214            info->hints_cmt = NULL;
00215            info->colors_cmt = NULL;
00216            info->pixels_cmt = NULL;
00217        }
00218     }
00219 /* end 3.2 bc */
00220     if (attributes->valuemask & XpmReturnExtensions) {
00221        attributes->extensions = info->extensions;
00222        attributes->nextensions = info->nextensions;
00223 
00224        /* avoid deletion of copied data */
00225        info->extensions = NULL;
00226        info->nextensions = 0;
00227     }
00228     if (info->valuemask & XpmHotspot) {
00229        attributes->valuemask |= XpmHotspot;
00230        attributes->x_hotspot = info->x_hotspot;
00231        attributes->y_hotspot = info->y_hotspot;
00232     }
00233     attributes->valuemask |= XpmCharsPerPixel;
00234     attributes->cpp = image->cpp;
00235     attributes->valuemask |= XpmSize;
00236     attributes->width = image->width;
00237     attributes->height = image->height;
00238 }
00239 
00240 /*
00241  * Free the XpmAttributes structure members
00242  * but the structure itself
00243  */
00244 void
00245 XpmFreeAttributes(attributes)
00246     XpmAttributes *attributes;
00247 {
00248     if (attributes->valuemask & XpmReturnPixels && attributes->npixels) {
00249        XpmFree(attributes->pixels);
00250        attributes->pixels = NULL;
00251        attributes->npixels = 0;
00252     }
00253     if (attributes->valuemask & XpmReturnColorTable) {
00254        xpmFreeColorTable(attributes->colorTable, attributes->ncolors);
00255        attributes->colorTable = NULL;
00256        attributes->ncolors = 0;
00257     }
00258 /* 3.2 backward compatibility code */
00259     else if (attributes->valuemask & XpmInfos) {
00260        if (attributes->colorTable) {
00261            FreeOldColorTable((XpmColor **) attributes->colorTable,
00262                            attributes->ncolors);
00263            attributes->colorTable = NULL;
00264            attributes->ncolors = 0;
00265        }
00266        if (attributes->hints_cmt) {
00267            XpmFree(attributes->hints_cmt);
00268            attributes->hints_cmt = NULL;
00269        }
00270        if (attributes->colors_cmt) {
00271            XpmFree(attributes->colors_cmt);
00272            attributes->colors_cmt = NULL;
00273        }
00274        if (attributes->pixels_cmt) {
00275            XpmFree(attributes->pixels_cmt);
00276            attributes->pixels_cmt = NULL;
00277        }
00278        if (attributes->pixels) {
00279            XpmFree(attributes->pixels);
00280            attributes->pixels = NULL;
00281            attributes->npixels = 0;
00282        }
00283     }
00284 /* end 3.2 bc */
00285     if (attributes->valuemask & XpmReturnExtensions
00286        && attributes->nextensions) {
00287        XpmFreeExtensions(attributes->extensions, attributes->nextensions);
00288        attributes->extensions = NULL;
00289        attributes->nextensions = 0;
00290     }
00291     if (attributes->valuemask & XpmReturnAllocPixels
00292        && attributes->nalloc_pixels) {
00293        XpmFree(attributes->alloc_pixels);
00294        attributes->alloc_pixels = NULL;
00295        attributes->nalloc_pixels = 0;
00296     }
00297     attributes->valuemask = 0;
00298 }