2 * CDE - Common Desktop Environment
4 * Copyright (c) 1993-2012, The Open Group. All rights reserved.
6 * These libraries and programs are free software; you can
7 * redistribute them and/or modify them under the terms of the GNU
8 * Lesser General Public License as published by the Free Software
9 * Foundation; either version 2 of the License, or (at your option)
12 * These libraries and programs are distributed in the hope that
13 * they will be useful, but WITHOUT ANY WARRANTY; without even the
14 * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 * PURPOSE. See the GNU Lesser General Public License for more
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with these librararies and programs; if not, write
20 * to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
21 * Floor, Boston, MA 02110-1301 USA
23 /* $XConsortium: ilcontext.c /main/3 1995/10/23 15:43:53 rswiston $ */
24 /**---------------------------------------------------------------------
26 *** (c)Copyright 1991 Hewlett-Packard Co.
28 *** RESTRICTED RIGHTS LEGEND
29 *** Use, duplication, or disclosure by the U.S. Government is subject to
30 *** restrictions as set forth in sub-paragraph (c)(1)(ii) of the Rights in
31 *** Technical Data and Computer Software clause in DFARS 252.227-7013.
32 *** Hewlett-Packard Company
33 *** 3000 Hanover Street
34 *** Palo Alto, CA 94304 U.S.A.
35 *** Rights for non-DOD U.S. Government Departments and Agencies are as set
36 *** forth in FAR 52.227-19(c)(1,2).
38 ***-------------------------------------------------------------------*/
40 /* ilcontext.c - Contains ilCreate/DestroyContext() and related code.
44 #include "ilcontext.h"
47 /* Functions in /ilc/ilobject.c :
49 /* Called by ilCreateContext() to create object data in the given context.
50 Returns: true if add was ok, else error: caller should free all.
52 IL_EXTERN ilBool _ilObjectInitContext (
56 /* Called by ilDestroyContext() to destroy object data in the given context.
58 IL_EXTERN void _ilObjectDestroyContext (
63 /* ------------------------ ilInternalCreateContext ------------------------- */
64 /* Called by the macro IL_CREATE_CONTEXT() which passes in the version
65 check number. Only check the low-order 16 bits of the versionCheck;
66 that way backwards-compatible versions can be differentiated by the
67 upper 16 bits (which might signal the library to do something different).
70 ilError ilInternalCreateContext (
72 ilContext *pContextReturn, /* RETURNED */
73 unsigned long mustBeZero
76 register ilContextPtr pContext;
78 /* If internal version # > the version # the library was built with, then
79 error; if <, old IL program using new library: supported.
81 if ((versionCheck & 0xffff) > IL_INTERNAL_VERSION)
82 return IL_ERROR_VERSION_MISMATCH;
84 return IL_ERROR_PAR_NOT_ZERO;
86 pContext = (ilContextPtr)IL_MALLOC_ZERO (sizeof (ilContextRec));
88 return IL_ERROR_MALLOC;
90 if (!_ilObjectInitContext (pContext)) {
92 return IL_ERROR_MALLOC;
95 pContext->p.error = 0;
96 pContext->p.errorInfo = 0;
98 /* Init private type code to start at standard image types.
100 pContext->privateType = IL_MAX_TYPE + 1;
101 *pContextReturn = (ilContext)pContext;
106 #ifdef IL_GARBAGE_MALLOC
108 /* --------------------- ilMallocAndInitWithGarbage ------------------------ */
109 /* Referenced by IL_MALLOC() macro when IL_GARBAGE_MALLOC defined (should be
110 defined only during test/debug - not in production product.)
111 malloc the given nBytes, fill it with garbage and return ptr to it.
113 IL_PRIVATE void *_ilMallocAndInitWithGarbage (
117 register ilPtr p, pMalloc;
119 pMalloc = (ilPtr)malloc (nBytes);
124 return (void *)pMalloc;
129 /* ----------------------- ilGetPrivateType ----------------------- */
130 /* Public function; see spec.
133 unsigned int ilGetPrivateType (
137 register ilContextPtr pContext;
139 /* Increment code (but not if it has wrapped to zero! and return it.
141 pContext = IL_CONTEXT_PTR (context);
142 if (pContext->privateType != 0)
143 pContext->privateType++;
144 return pContext->privateType;
148 /* ------------------------ ilDestroyContext ---------------------------- */
149 /* Public function; see spec.
152 ilBool ilDestroyContext (
156 register ilContextPtr pContext;
159 /* Destroy all objects associated with this context, then free
160 any data pointed to in pAlloc array.
162 pContext = IL_CONTEXT_PTR (context);
163 _ilObjectDestroyContext (pContext);
165 for (i = 0; i < IL_CONTEXT_MAX_ALLOC; i++) {
166 if (pContext->pAlloc[i])
167 IL_FREE (pContext->pAlloc[i]);
174 /* ------------------------ ilIntersectRect ------------------------------ */
175 /* Intersect the rectangle "*pSrcRect" with the rect "*pDstRect",
176 storing the result in "*pDstRect".
177 Not in this file for any particular reason; no other logical place for it.
179 IL_PRIVATE void _ilIntersectRect (
180 register ilRect *pSrcRect,
181 register ilRect *pDstRect
184 long left, top, right, bottom, i;
186 /* Change to non-inclusive coords for easier compare.
189 right = left + pSrcRect->width;
191 bottom = top + pSrcRect->height;
193 if (pDstRect->x > left)
195 if (pDstRect->y > top)
197 i = pDstRect->x + pDstRect->width; /* pDstRect right */
200 i = pDstRect->y + pDstRect->height; /* pDstRect bottom */
204 /* Store result into pDstRect, with width/height always >= 0.
208 pDstRect->width = (right <= left) ? 0 : right - left;
209 pDstRect->height = (bottom <= top) ? 0 : bottom - top;