1 /* $XConsortium: ilcontext.c /main/3 1995/10/23 15:43:53 rswiston $ */
2 /**---------------------------------------------------------------------
4 *** (c)Copyright 1991 Hewlett-Packard Co.
6 *** RESTRICTED RIGHTS LEGEND
7 *** Use, duplication, or disclosure by the U.S. Government is subject to
8 *** restrictions as set forth in sub-paragraph (c)(1)(ii) of the Rights in
9 *** Technical Data and Computer Software clause in DFARS 252.227-7013.
10 *** Hewlett-Packard Company
11 *** 3000 Hanover Street
12 *** Palo Alto, CA 94304 U.S.A.
13 *** Rights for non-DOD U.S. Government Departments and Agencies are as set
14 *** forth in FAR 52.227-19(c)(1,2).
16 ***-------------------------------------------------------------------*/
18 /* ilcontext.c - Contains ilCreate/DestroyContext() and related code.
22 #include "ilcontext.h"
25 /* Functions in /ilc/ilobject.c :
27 /* Called by ilCreateContext() to create object data in the given context.
28 Returns: true if add was ok, else error: caller should free all.
30 IL_EXTERN ilBool _ilObjectInitContext (
34 /* Called by ilDestroyContext() to destroy object data in the given context.
36 IL_EXTERN void _ilObjectDestroyContext (
41 /* ------------------------ ilInternalCreateContext ------------------------- */
42 /* Called by the macro IL_CREATE_CONTEXT() which passes in the version
43 check number. Only check the low-order 16 bits of the versionCheck;
44 that way backwards-compatible versions can be differentiated by the
45 upper 16 bits (which might signal the library to do something different).
48 ilError ilInternalCreateContext (
50 ilContext *pContextReturn, /* RETURNED */
51 unsigned long mustBeZero
54 register ilContextPtr pContext;
56 /* If internal version # > the version # the library was built with, then
57 error; if <, old IL program using new library: supported.
59 if ((versionCheck & 0xffff) > IL_INTERNAL_VERSION)
60 return IL_ERROR_VERSION_MISMATCH;
62 return IL_ERROR_PAR_NOT_ZERO;
64 pContext = (ilContextPtr)IL_MALLOC_ZERO (sizeof (ilContextRec));
66 return IL_ERROR_MALLOC;
68 if (!_ilObjectInitContext (pContext)) {
70 return IL_ERROR_MALLOC;
73 pContext->p.error = 0;
74 pContext->p.errorInfo = 0;
76 /* Init private type code to start at standard image types.
78 pContext->privateType = IL_MAX_TYPE + 1;
79 *pContextReturn = (ilContext)pContext;
84 #ifdef IL_GARBAGE_MALLOC
86 /* --------------------- ilMallocAndInitWithGarbage ------------------------ */
87 /* Referenced by IL_MALLOC() macro when IL_GARBAGE_MALLOC defined (should be
88 defined only during test/debug - not in production product.)
89 malloc the given nBytes, fill it with garbage and return ptr to it.
91 IL_PRIVATE void *_ilMallocAndInitWithGarbage (
95 register ilPtr p, pMalloc;
97 pMalloc = (ilPtr)malloc (nBytes);
102 return (void *)pMalloc;
107 /* ----------------------- ilGetPrivateType ----------------------- */
108 /* Public function; see spec.
111 unsigned int ilGetPrivateType (
115 register ilContextPtr pContext;
117 /* Increment code (but not if it has wrapped to zero! and return it.
119 pContext = IL_CONTEXT_PTR (context);
120 if (pContext->privateType != 0)
121 pContext->privateType++;
122 return pContext->privateType;
126 /* ------------------------ ilDestroyContext ---------------------------- */
127 /* Public function; see spec.
130 ilBool ilDestroyContext (
134 register ilContextPtr pContext;
137 /* Destroy all objects associated with this context, then free
138 any data pointed to in pAlloc array.
140 pContext = IL_CONTEXT_PTR (context);
141 _ilObjectDestroyContext (pContext);
143 for (i = 0; i < IL_CONTEXT_MAX_ALLOC; i++) {
144 if (pContext->pAlloc[i])
145 IL_FREE (pContext->pAlloc[i]);
152 /* ------------------------ ilIntersectRect ------------------------------ */
153 /* Intersect the rectangle "*pSrcRect" with the rect "*pDstRect",
154 storing the result in "*pDstRect".
155 Not in this file for any particular reason; no other logical place for it.
157 IL_PRIVATE void _ilIntersectRect (
158 register ilRect *pSrcRect,
159 register ilRect *pDstRect
162 long left, top, right, bottom, i;
164 /* Change to non-inclusive coords for easier compare.
167 right = left + pSrcRect->width;
169 bottom = top + pSrcRect->height;
171 if (pDstRect->x > left)
173 if (pDstRect->y > top)
175 i = pDstRect->x + pDstRect->width; /* pDstRect right */
178 i = pDstRect->y + pDstRect->height; /* pDstRect bottom */
182 /* Store result into pDstRect, with width/height always >= 0.
186 pDstRect->width = (right <= left) ? 0 : right - left;
187 pDstRect->height = (bottom <= top) ? 0 : bottom - top;