Updated PCI IDs to latest snapshot.
[tangerine.git] / workbench / hidds / graphics / CM_Class.c
blob6406fe3cc65f2092a90cd55a61484803edf33db1
1 /*
2 Copyright © 1995-2001, The AROS Development Team. All rights reserved.
3 $Id$
5 Desc: Graphics colormap class implementation.
6 Lang: english
7 */
9 /****************************************************************************************/
11 #include <string.h>
13 #include <proto/exec.h>
14 #include <proto/utility.h>
15 #include <proto/oop.h>
16 #include <proto/arossupport.h>
18 #include <exec/memory.h>
19 #include <utility/tagitem.h>
20 #include <oop/oop.h>
21 #include <graphics/text.h>
23 #include <hidd/graphics.h>
25 #include "graphics_intern.h"
27 #define DEBUG 0
28 #include <aros/debug.h>
30 /****************************************************************************************/
32 OOP_Object *CM__Root__New(OOP_Class *cl, OOP_Object *o, struct pRoot_New *msg)
34 struct colormap_data *data;
35 ULONG numentries;
36 struct TagItem *tag, *tstate;
37 BOOL ok = FALSE;
39 EnterFunc(bug("ColorMap::New()\n"));
40 numentries = 256;
42 for (tstate = msg->attrList; (tag = NextTagItem((const struct TagItem **)&tstate)); )
44 ULONG idx;
46 if (IS_COLORMAP_ATTR(tag->ti_Tag, idx))
48 switch (idx)
50 case aoHidd_ColorMap_NumEntries:
51 numentries = tag->ti_Data;
52 if (numentries > 256 || numentries < 0)
54 D(bug("!!! ILLEGAL value for NumEntries in ColorMap::New()\n"));
56 break;
58 } /* switch */
63 /* Create the object */
65 o = (OOP_Object *)OOP_DoSuperMethod(cl, o, (OOP_Msg)msg);
66 if (NULL == o)
67 return NULL;
69 data = OOP_INST_DATA(cl, o);
71 data->clut.entries = numentries;
73 data->clut.colors = AllocMem(sizeof (HIDDT_Color) * data->clut.entries, MEMF_CLEAR);
74 if (NULL != data->clut.colors)
76 ok = TRUE;
79 if (!ok)
81 ULONG dispose_mid;
83 dispose_mid = OOP_GetMethodID(IID_Root, moRoot_Dispose);
84 OOP_CoerceMethod(cl, o, (OOP_Msg)&dispose_mid);
85 o = NULL;
88 ReturnPtr("ColorMap::New", OOP_Object *, o);
91 /****************************************************************************************/
93 VOID CM__Root__Dispose(OOP_Class *cl, OOP_Object *o, OOP_Msg msg)
95 struct colormap_data *data;
97 data = OOP_INST_DATA(cl, o);
99 if (NULL != data->clut.colors)
101 FreeMem(data->clut.colors, data->clut.entries * sizeof (HIDDT_Color));
103 /* To detect use of allready freed mem */
104 data->clut.colors = 0xDEADBEEF;
107 OOP_DoSuperMethod(cl, o, msg);
109 return;
112 /****************************************************************************************/
114 VOID CM__Root__Get(OOP_Class *cl, OOP_Object *o, struct pRoot_Get *msg)
116 struct colormap_data *data;
117 ULONG idx;
119 EnterFunc(bug("ColorMap::Get()\n"));
120 data = OOP_INST_DATA(cl, o);
122 if (IS_COLORMAP_ATTR(msg->attrID, idx))
124 switch (idx)
126 case aoHidd_ColorMap_NumEntries:
127 *msg->storage = data->clut.entries;
128 break;
130 default:
131 D(bug("!!! Unknow colormap attr in ColorMap::Get()\n"));
132 OOP_DoSuperMethod(cl, o, (OOP_Msg)msg);
133 break;
136 else
138 OOP_DoSuperMethod(cl, o, (OOP_Msg)msg);
141 ReturnVoid("ColorMap::Get");
144 /****************************************************************************************/
146 BOOL CM__Hidd_ColorMap__SetColors(OOP_Class *cl, OOP_Object *o,
147 struct pHidd_ColorMap_SetColors *msg)
149 struct colormap_data *data;
150 ULONG numnew;
151 ULONG i, col_idx;
152 HIDDT_Color *col;
153 HIDDT_PixelFormat *pf;
155 data = OOP_INST_DATA(cl, o);
157 numnew = msg->firstColor + msg->numColors;
159 /* See if there is enpugh space in the array */
161 if (numnew > data->clut.entries)
163 /* Reallocate and copy */
164 HIDDT_Color *newmap;
166 newmap = AllocMem(sizeof (*newmap) * numnew, MEMF_ANY);
167 if (NULL == newmap)
168 return FALSE;
170 memcpy(newmap, data->clut.colors, sizeof (*newmap) * data->clut.entries);
172 FreeMem(data->clut.colors, sizeof (*newmap) * data->clut.entries);
174 data->clut.colors = newmap;
175 data->clut.entries = numnew;
178 /* Insert the new colors */
179 col_idx = msg->firstColor;
180 col = &data->clut.colors[msg->firstColor];
181 pf = (HIDDT_PixelFormat *)msg->pixFmt;
183 for (i = 0; i < msg->numColors; i ++)
185 /* Set the color */
186 *col = msg->colors[i];
188 /* Set the pixval using the supplied pixel format */
189 if (IS_TRUECOLOR(pf))
191 /* Map the color to a HIDDT_Pixel */
192 msg->colors[i].pixval = col->pixval = int_map_truecolor(col, pf);
195 else
197 msg->colors[i].pixval = col->pixval = (HIDDT_Pixel)col_idx;
200 /* bug("ColMap::SetColors: col %d (%x %x %x %x) mapped to %x\n"
201 , col_idx
202 , col->red, col->green, col->blue, col->alpha
203 , msg->colors[i].pixval);
207 col ++;
208 col_idx ++;
211 return TRUE;
214 /****************************************************************************************/
216 inline HIDDT_Pixel int_map_truecolor(HIDDT_Color *color, HIDDT_PixelFormat *pf)
218 HIDDT_Pixel red = color->red;
219 HIDDT_Pixel green = color->green;
220 HIDDT_Pixel blue = color->blue;
221 HIDDT_Pixel alpha = color->alpha;
224 /* This code assumes that sizeof (HIDDT_Pixel is a multimple of sizeof(col->#?)
225 which should be true for most (all ?) systems. (I have never heard
226 of any system with for example 3 byte types.
229 if (HIDD_PF_SWAPPIXELBYTES(pf))
231 #warning "int_map_truecolor assuming that SwapPixelBytes flag only set for 2-byte/16-bit pixel formats"
233 HIDDT_Pixel pixel = MAP_RGBA(red, green, blue, alpha, pf);
235 color->pixval = SWAPBYTES_WORD(pixel);;
237 else
239 color->pixval = MAP_RGBA(red, green, blue, alpha, pf);
242 return color->pixval;
245 /****************************************************************************************/
247 HIDDT_Pixel CM__Hidd_ColorMap__GetPixel(OOP_Class *cl, OOP_Object *o,
248 struct pHidd_ColorMap_GetPixel *msg)
250 struct colormap_data *data;
252 data = OOP_INST_DATA(cl, o);
254 if (msg->pixelNo < 0 || msg->pixelNo >= data->clut.entries)
256 D(bug("!!! Unvalid msg->pixelNo (%d) in ColorMap::GetPixel(). clutentries = %d\n",
257 msg->pixelNo,
258 data->clut.entries));
260 // *((ULONG *)0) = 0;
261 return (HIDDT_Pixel)-1;
265 return data->clut.colors[msg->pixelNo].pixval;
268 /****************************************************************************************/
270 BOOL CM__Hidd_ColorMap__GetColor(OOP_Class *cl, OOP_Object *o,
271 struct pHidd_ColorMap_GetColor *msg)
273 struct colormap_data *data;
275 data = OOP_INST_DATA(cl, o);
277 if (msg->colorNo < 0 || msg->colorNo >= data->clut.entries)
279 D(bug("!!! Unvalid msg->colorNo (%d) in ColorMap::GetPixel(). clutentries = %d\n",
280 msg->colorNo,
281 data->clut.entries));
283 return FALSE;
286 *msg->colorReturn = data->clut.colors[msg->colorNo];
288 return TRUE;
291 /****************************************************************************************/