grub2: bring back build of aros-side grub2 tools
[AROS.git] / rom / hidds / graphics / CM_Class.c
blob02e4ad0fd143d8b79dace542d27375cba9e0d897
1 /*
2 Copyright © 1995-2011, 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 NAME
33 aoHidd_ColorMap_NumEntries
35 SYNOPSIS
36 [I.G], ULONG
38 LOCATION
39 CLID_Hidd_ColorMap
41 FUNCTION
42 Number of colors in the colormap.
44 NOTES
46 EXAMPLE
48 BUGS
50 SEE ALSO
52 INTERNALS
54 *****************************************************************************************/
56 OOP_Object *CM__Root__New(OOP_Class *cl, OOP_Object *o, struct pRoot_New *msg)
58 struct Library *UtilityBase = CSD(cl)->cs_UtilityBase;
59 struct Library *OOPBase = CSD(cl)->cs_OOPBase;
60 struct colormap_data *data;
61 ULONG numentries;
62 struct TagItem *tag, *tstate;
63 BOOL ok = FALSE;
65 EnterFunc(bug("ColorMap::New()\n"));
66 numentries = 256;
68 for (tstate = msg->attrList; (tag = NextTagItem(&tstate)); )
70 ULONG idx;
72 if (IS_COLORMAP_ATTR(tag->ti_Tag, idx))
74 switch (idx)
76 case aoHidd_ColorMap_NumEntries:
77 numentries = tag->ti_Data;
78 if (numentries > 256 || numentries < 0)
80 D(bug("!!! ILLEGAL value for NumEntries in ColorMap::New()\n"));
82 break;
84 } /* switch */
89 /* Create the object */
91 o = (OOP_Object *)OOP_DoSuperMethod(cl, o, (OOP_Msg)msg);
92 if (NULL == o)
93 return NULL;
95 data = OOP_INST_DATA(cl, o);
97 data->clut.entries = numentries;
99 data->clut.colors = AllocMem(sizeof (HIDDT_Color) * data->clut.entries, MEMF_CLEAR);
100 if (NULL != data->clut.colors)
102 ok = TRUE;
105 if (!ok)
107 ULONG dispose_mid;
109 dispose_mid = OOP_GetMethodID(IID_Root, moRoot_Dispose);
110 OOP_CoerceMethod(cl, o, (OOP_Msg)&dispose_mid);
111 o = NULL;
114 ReturnPtr("ColorMap::New", OOP_Object *, o);
117 /****************************************************************************************/
119 VOID CM__Root__Dispose(OOP_Class *cl, OOP_Object *o, OOP_Msg msg)
121 struct colormap_data *data;
123 data = OOP_INST_DATA(cl, o);
125 if (NULL != data->clut.colors)
127 FreeMem(data->clut.colors, data->clut.entries * sizeof (HIDDT_Color));
129 /* To detect use of already freed mem */
130 data->clut.colors = (void *)0xDEADBEEF;
133 OOP_DoSuperMethod(cl, o, msg);
135 return;
138 /****************************************************************************************/
140 VOID CM__Root__Get(OOP_Class *cl, OOP_Object *o, struct pRoot_Get *msg)
142 struct colormap_data *data;
143 ULONG idx;
145 EnterFunc(bug("ColorMap::Get()\n"));
146 data = OOP_INST_DATA(cl, o);
148 if (IS_COLORMAP_ATTR(msg->attrID, idx))
150 switch (idx)
152 case aoHidd_ColorMap_NumEntries:
153 *msg->storage = data->clut.entries;
154 break;
156 default:
157 D(bug("!!! Unknown colormap attr in ColorMap::Get()\n"));
158 OOP_DoSuperMethod(cl, o, (OOP_Msg)msg);
159 break;
162 else
164 OOP_DoSuperMethod(cl, o, (OOP_Msg)msg);
167 ReturnVoid("ColorMap::Get");
170 /****************************************************************************************/
172 static inline HIDDT_Pixel int_map_truecolor(HIDDT_Color *color, HIDDT_PixelFormat *pf)
174 HIDDT_Pixel red = color->red;
175 HIDDT_Pixel green = color->green;
176 HIDDT_Pixel blue = color->blue;
177 HIDDT_Pixel alpha = color->alpha;
179 /* This code assumes that sizeof(HIDDT_Pixel) is a multiple of sizeof(col->#?),
180 which should be true for most (all?) systems. (I have never heard of any
181 system with for example 3 byte types.)
184 if (HIDD_PF_SWAPPIXELBYTES(pf))
186 /* FIXME: int_map_truecolor assuming that SwapPixelBytes flag only set for 2-byte/16-bit pixel formats */
188 HIDDT_Pixel pixel = MAP_RGBA(red, green, blue, alpha, pf);
190 color->pixval = SWAPBYTES_WORD(pixel);
192 else
194 color->pixval = MAP_RGBA(red, green, blue, alpha, pf);
197 return color->pixval;
200 /*****************************************************************************************
202 NAME
203 moHidd_ColorMap_SetColors
205 SYNOPSIS
206 BOOL OOP_DoMethod(OOP_Object *obj, struct pHidd_ColorMap_SetColors *msg);
208 BOOL HIDD_CM_SetColors(OOP_Object *obj, HIDDT_Color *colors, ULONG firstColor,
209 ULONG numColors, OOP_Object *pixFmt);
211 LOCATION
212 CLID_Hidd_ColorMap
214 FUNCTION
216 INPUTS
217 obj -
218 colors -
219 firstColor -
220 numColors -
221 pixFmt -
223 RESULT
225 NOTES
227 EXAMPLE
229 BUGS
231 SEE ALSO
233 INTERNALS
235 *****************************************************************************************/
237 BOOL CM__Hidd_ColorMap__SetColors(OOP_Class *cl, OOP_Object *o,
238 struct pHidd_ColorMap_SetColors *msg)
240 struct colormap_data *data;
241 ULONG numnew;
242 ULONG i, col_idx;
243 HIDDT_Color *col;
244 HIDDT_PixelFormat *pf;
246 data = OOP_INST_DATA(cl, o);
248 numnew = msg->firstColor + msg->numColors;
250 /* See if there is enough space in the array */
252 if (numnew > data->clut.entries)
254 /* Reallocate and copy */
255 HIDDT_Color *newmap;
257 newmap = AllocMem(sizeof (*newmap) * numnew, MEMF_ANY);
258 if (NULL == newmap)
259 return FALSE;
261 memcpy(newmap, data->clut.colors, sizeof (*newmap) * data->clut.entries);
263 FreeMem(data->clut.colors, sizeof (*newmap) * data->clut.entries);
265 data->clut.colors = newmap;
266 data->clut.entries = numnew;
269 /* Insert the new colors */
270 col_idx = msg->firstColor;
271 col = &data->clut.colors[msg->firstColor];
272 pf = (HIDDT_PixelFormat *)msg->pixFmt;
274 for (i = 0; i < msg->numColors; i ++)
276 /* Set the color */
277 *col = msg->colors[i];
279 /* Set the pixval using the supplied pixel format */
280 if (IS_TRUECOLOR(pf))
282 /* Map the color to a HIDDT_Pixel */
283 msg->colors[i].pixval = col->pixval = int_map_truecolor(col, pf);
286 else
288 msg->colors[i].pixval = col->pixval = (HIDDT_Pixel)col_idx;
291 /* bug("ColMap::SetColors: col %d (%x %x %x %x) mapped to %x\n"
292 , col_idx
293 , col->red, col->green, col->blue, col->alpha
294 , msg->colors[i].pixval);
298 col ++;
299 col_idx ++;
302 return TRUE;
305 /*****************************************************************************************
307 NAME
308 moHidd_ColorMap_GetPixel
310 SYNOPSIS
311 HIDDT_Pixel OOP_DoMethod(OOP_Object *obj, struct pHidd_ColorMap_GetPixel *msg);
313 HIDDT_Pixel HIDD_CM_GetPixel(OOP_Object *obj, ULONG pixelNo);
315 LOCATION
316 CLID_Hidd_ColorMap
318 FUNCTION
320 INPUTS
321 obj -
322 pixelNo -
324 RESULT
326 NOTES
328 EXAMPLE
330 BUGS
332 SEE ALSO
334 INTERNALS
336 *****************************************************************************************/
338 HIDDT_Pixel CM__Hidd_ColorMap__GetPixel(OOP_Class *cl, OOP_Object *o,
339 struct pHidd_ColorMap_GetPixel *msg)
341 struct colormap_data *data;
343 data = OOP_INST_DATA(cl, o);
345 if (msg->pixelNo < 0 || msg->pixelNo >= data->clut.entries)
347 D(bug("!!! Unvalid msg->pixelNo (%d) in ColorMap::GetPixel(). clutentries = %d\n",
348 msg->pixelNo,
349 data->clut.entries));
351 // *((ULONG *)0) = 0;
352 return (HIDDT_Pixel)-1;
356 return data->clut.colors[msg->pixelNo].pixval;
359 /*****************************************************************************************
361 NAME
362 moHidd_ColorMap_GetColor
364 SYNOPSIS
365 BOOL OOP_DoMethod(OOP_Object *o, struct pHidd_ColorMap_GetColor *msg);
367 BOOL HIDD_CM_GetColor(OOP_Object *obj, ULONG colorNo, HIDDT_Color *colorReturn);
369 LOCATION
370 CLID_Hidd_ColorMap
372 FUNCTION
374 INPUTS
375 obj -
376 colorNo -
377 colorReturn -
379 RESULT
381 NOTES
383 EXAMPLE
385 BUGS
387 SEE ALSO
389 INTERNALS
391 *****************************************************************************************/
393 BOOL CM__Hidd_ColorMap__GetColor(OOP_Class *cl, OOP_Object *o,
394 struct pHidd_ColorMap_GetColor *msg)
396 struct colormap_data *data;
398 data = OOP_INST_DATA(cl, o);
400 if (msg->colorNo < 0 || msg->colorNo >= data->clut.entries)
402 D(bug("!!! Unvalid msg->colorNo (%d) in ColorMap::GetPixel(). clutentries = %d\n",
403 msg->colorNo,
404 data->clut.entries));
406 return FALSE;
409 *msg->colorReturn = data->clut.colors[msg->colorNo];
411 return TRUE;