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 libraries 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.
46 #include "ilcontext.h"
49 /* Functions in /ilc/ilobject.c :
51 /* Called by ilCreateContext() to create object data in the given context.
52 Returns: true if add was ok, else error: caller should free all.
54 IL_EXTERN ilBool _ilObjectInitContext (
58 /* Called by ilDestroyContext() to destroy object data in the given context.
60 IL_EXTERN void _ilObjectDestroyContext (
65 /* ------------------------ ilInternalCreateContext ------------------------- */
66 /* Called by the macro IL_CREATE_CONTEXT() which passes in the version
67 check number. Only check the low-order 16 bits of the versionCheck;
68 that way backwards-compatible versions can be differentiated by the
69 upper 16 bits (which might signal the library to do something different).
72 ilError ilInternalCreateContext (
74 ilContext *pContextReturn, /* RETURNED */
75 unsigned long mustBeZero
78 register ilContextPtr pContext;
80 /* If internal version # > the version # the library was built with, then
81 error; if <, old IL program using new library: supported.
83 if ((versionCheck & 0xffff) > IL_INTERNAL_VERSION)
84 return IL_ERROR_VERSION_MISMATCH;
86 return IL_ERROR_PAR_NOT_ZERO;
88 pContext = (ilContextPtr)IL_MALLOC_ZERO (sizeof (ilContextRec));
90 return IL_ERROR_MALLOC;
92 if (!_ilObjectInitContext (pContext)) {
94 return IL_ERROR_MALLOC;
97 pContext->p.error = 0;
98 pContext->p.errorInfo = 0;
100 /* Init private type code to start at standard image types.
102 pContext->privateType = IL_MAX_TYPE + 1;
103 *pContextReturn = (ilContext)pContext;
108 #ifdef IL_GARBAGE_MALLOC
110 /* --------------------- ilMallocAndInitWithGarbage ------------------------ */
111 /* Referenced by IL_MALLOC() macro when IL_GARBAGE_MALLOC defined (should be
112 defined only during test/debug - not in production product.)
113 malloc the given nBytes, fill it with garbage and return ptr to it.
115 IL_PRIVATE void *_ilMallocAndInitWithGarbage (
119 register ilPtr p, pMalloc;
121 pMalloc = (ilPtr)malloc (nBytes);
126 return (void *)pMalloc;
131 /* ----------------------- ilGetPrivateType ----------------------- */
132 /* Public function; see spec.
135 unsigned int ilGetPrivateType (
139 register ilContextPtr pContext;
141 /* Increment code (but not if it has wrapped to zero! and return it.
143 pContext = IL_CONTEXT_PTR (context);
144 if (pContext->privateType != 0)
145 pContext->privateType++;
146 return pContext->privateType;
150 /* ------------------------ ilDestroyContext ---------------------------- */
151 /* Public function; see spec.
154 ilBool ilDestroyContext (
158 register ilContextPtr pContext;
161 /* Destroy all objects associated with this context, then free
162 any data pointed to in pAlloc array.
164 pContext = IL_CONTEXT_PTR (context);
165 _ilObjectDestroyContext (pContext);
167 for (i = 0; i < IL_CONTEXT_MAX_ALLOC; i++) {
168 if (pContext->pAlloc[i])
169 IL_FREE (pContext->pAlloc[i]);
176 /* ------------------------ ilIntersectRect ------------------------------ */
177 /* Intersect the rectangle "*pSrcRect" with the rect "*pDstRect",
178 storing the result in "*pDstRect".
179 Not in this file for any particular reason; no other logical place for it.
181 IL_PRIVATE void _ilIntersectRect (
182 register ilRect *pSrcRect,
183 register ilRect *pDstRect
186 long left, top, right, bottom, i;
188 /* Change to non-inclusive coords for easier compare.
191 right = left + pSrcRect->width;
193 bottom = top + pSrcRect->height;
195 if (pDstRect->x > left)
197 if (pDstRect->y > top)
199 i = pDstRect->x + pDstRect->width; /* pDstRect right */
202 i = pDstRect->y + pDstRect->height; /* pDstRect bottom */
206 /* Store result into pDstRect, with width/height always >= 0.
210 pDstRect->width = (right <= left) ? 0 : right - left;
211 pDstRect->height = (bottom <= top) ? 0 : bottom - top;