r4493@vps: verhaegs | 2007-04-19 14:44:00 -0400
[AROS.git] / rom / graphics / attachpalextra.c
blobb80d14f16836b318fd277e09ed4540c0a08f1177
1 /*
2 Copyright © 1995-2001, The AROS Development Team. All rights reserved.
3 $Id$
5 Desc: Graphics function AttachPalExtra()
6 Lang: english
7 */
9 #include <proto/exec.h>
10 #include <graphics/view.h>
11 #include "graphics_intern.h"
13 /*****************************************************************************
15 NAME */
16 #include <proto/graphics.h>
18 AROS_LH2(LONG, AttachPalExtra,
20 /* SYNOPSIS */
21 AROS_LHA(struct ColorMap *, cm, A0),
22 AROS_LHA(struct ViewPort *, vp, A1),
24 /* LOCATION */
25 struct GfxBase *, GfxBase, 139, Graphics)
27 /* FUNCTION
28 Allocates a PalExtra structure and attaches it to the
29 given ColorMap. This function must be called prior to palette
30 sharing. The PalExtra structure will be freed by FreeColorMap().
32 INPUTS
33 cm - Pointer to a color map structure
34 vp - Pointer to the viewport associated with the ColorMap
36 RESULT
37 0 - success
38 1 - out of memory
40 NOTES
42 EXAMPLE
44 BUGS
46 SEE ALSO
48 INTERNALS
50 HISTORY
52 *****************************************************************************/
54 AROS_LIBFUNC_INIT
55 AROS_LIBBASE_EXT_DECL(struct GfxBase *,GfxBase)
57 struct PaletteExtra * pe;
59 if (NULL != cm->PalExtra)
60 return 0;
62 pe = AllocMem(sizeof(struct PaletteExtra), MEMF_CLEAR|MEMF_PUBLIC);
63 if (NULL != pe)
65 /*
66 ** if you change the number of byte allocated here then you
67 ** must also make chnages to FreeColorMap()!
69 pe->pe_RefCnt = AllocMem(cm->Count * sizeof(PalExtra_RefCnt_Type), MEMF_CLEAR);
70 pe->pe_AllocList = AllocMem(cm->Count * sizeof(PalExtra_AllocList_Type), MEMF_ANY);
72 if (NULL != pe->pe_RefCnt && NULL != pe->pe_AllocList)
74 UWORD sharablecolors, bmdepth;
76 sharablecolors = cm->Count;
78 /* cm->Count may contain more entries than 2 ^ bitmapdepth,
79 for pointer sprite colors, etc. Sharablecolors OTOH is
80 limited to 2 ^ bitmapdepth */
82 bmdepth = GetBitMapAttr(vp->RasInfo->BitMap, BMA_DEPTH);
83 if (bmdepth < 8)
85 if ((1L << bmdepth) < sharablecolors)
87 sharablecolors = 1L << bmdepth;
91 /* initialize the AllocList BYTE-array */
92 ULONG i = 0;
94 /* CHECKME: Should probably say "i < sharablecolors", but might
95 not actually cause anything bad either, even if it doesn't. */
96 while (i < cm->Count)
98 PALEXTRA_ALLOCLIST(pe, i) = (PalExtra_AllocList_Type)i-1;
99 i++;
102 /* connect the PaletteExtra structure to the ColorMap */
103 cm ->PalExtra = pe;
105 /* initialize the Palette Extra structure */
106 InitSemaphore(&pe->pe_Semaphore);
107 pe->pe_ViewPort = vp;
109 pe->pe_FirstFree = sharablecolors-1;
110 pe->pe_NFree = sharablecolors;
111 pe->pe_FirstShared = (UWORD)-1;
112 pe->pe_NShared = 0;
114 /* set all entries in the color table to be shareable
115 pe_SharableColors is not the number of colors but the last color index! */
117 pe->pe_SharableColors = sharablecolors;
119 } /* if (NULL != pe->pe_RefCnt && NULL != pe->pe_AllocList) */
120 else
122 /* some memory allocation failed */
123 if (pe->pe_RefCnt)
124 FreeMem(pe->pe_RefCnt, cm->Count * sizeof(PalExtra_RefCnt_Type));
125 if (pe->pe_AllocList)
126 FreeMem(pe->pe_AllocList, cm->Count* sizeof(PalExtra_AllocList_Type));
127 FreeMem(pe, sizeof(struct PaletteExtra));
128 return 1;
131 } /* if (NULL != pe) */
132 else
133 return 1;
135 return 0;
137 AROS_LIBFUNC_EXIT
139 } /* AttachPalExtra */