revert between 56095 -> 55830 in arch
[AROS.git] / rom / hidds / gfx / gfx_colormapclass.c
blob02981542da12e361e665b4d19c1b083dd6c1808d
1 /*
2 Copyright © 1995-2018, The AROS Development Team. All rights reserved.
3 $Id$
5 Desc: Graphics colormap class implementation.
6 Lang: english
7 */
9 /****************************************************************************************/
11 #include "gfx_debug.h"
13 #include <string.h>
15 #include <proto/exec.h>
16 #include <proto/utility.h>
17 #include <proto/oop.h>
18 #include <proto/arossupport.h>
20 #include <exec/memory.h>
21 #include <utility/tagitem.h>
22 #include <oop/oop.h>
23 #include <graphics/text.h>
25 #include <hidd/gfx.h>
27 #include "gfx_intern.h"
29 /*****************************************************************************************
31 NAME
32 aoHidd_ColorMap_NumEntries
34 SYNOPSIS
35 [I.G], ULONG
37 LOCATION
38 CLID_Hidd_ColorMap
40 FUNCTION
41 Number of colors in the colormap.
43 NOTES
45 EXAMPLE
47 BUGS
49 SEE ALSO
51 INTERNALS
53 *****************************************************************************************/
55 OOP_Object *CM__Root__New(OOP_Class *cl, OOP_Object *o, struct pRoot_New *msg)
57 struct Library *UtilityBase = CSD(cl)->cs_UtilityBase;
58 struct Library *OOPBase = CSD(cl)->cs_OOPBase;
59 struct colormap_data *data;
60 ULONG numentries;
61 struct TagItem *tag, *tstate;
62 BOOL ok = FALSE;
64 EnterFunc(bug("ColorMap::New()\n"));
65 numentries = 256;
67 for (tstate = msg->attrList; (tag = NextTagItem(&tstate)); )
69 ULONG idx;
71 if (IS_COLORMAP_ATTR(tag->ti_Tag, idx))
73 switch (idx)
75 case aoHidd_ColorMap_NumEntries:
76 numentries = tag->ti_Data;
77 if (numentries > 256 || numentries < 0)
79 D(bug("!!! ILLEGAL value for NumEntries in ColorMap::New()\n"));
81 break;
83 } /* switch */
88 /* Create the object */
90 o = (OOP_Object *)OOP_DoSuperMethod(cl, o, (OOP_Msg)msg);
91 if (NULL == o)
92 return NULL;
94 data = OOP_INST_DATA(cl, o);
96 data->clut.entries = numentries;
98 data->clut.colors = AllocMem(sizeof (HIDDT_Color) * data->clut.entries, MEMF_CLEAR);
99 if (NULL != data->clut.colors)
101 ok = TRUE;
104 if (!ok)
106 ULONG dispose_mid;
108 dispose_mid = OOP_GetMethodID(IID_Root, moRoot_Dispose);
109 OOP_CoerceMethod(cl, o, (OOP_Msg)&dispose_mid);
110 o = NULL;
113 ReturnPtr("ColorMap::New", OOP_Object *, o);
116 /****************************************************************************************/
118 VOID CM__Root__Dispose(OOP_Class *cl, OOP_Object *o, OOP_Msg msg)
120 struct colormap_data *data;
122 data = OOP_INST_DATA(cl, o);
124 if (NULL != data->clut.colors)
126 FreeMem(data->clut.colors, data->clut.entries * sizeof (HIDDT_Color));
128 /* To detect use of already freed mem */
129 data->clut.colors = (void *)0xDEADBEEF;
132 OOP_DoSuperMethod(cl, o, msg);
134 return;
137 /****************************************************************************************/
139 VOID CM__Root__Get(OOP_Class *cl, OOP_Object *o, struct pRoot_Get *msg)
141 struct colormap_data *data;
142 ULONG idx;
144 EnterFunc(bug("ColorMap::Get()\n"));
145 data = OOP_INST_DATA(cl, o);
147 if (IS_COLORMAP_ATTR(msg->attrID, idx))
149 switch (idx)
151 case aoHidd_ColorMap_NumEntries:
152 *msg->storage = data->clut.entries;
153 break;
155 default:
156 D(bug("!!! Unknown colormap attr in ColorMap::Get()\n"));
157 OOP_DoSuperMethod(cl, o, (OOP_Msg)msg);
158 break;
161 else
163 OOP_DoSuperMethod(cl, o, (OOP_Msg)msg);
166 ReturnVoid("ColorMap::Get");
169 /****************************************************************************************/
171 static inline HIDDT_Pixel int_map_truecolor(HIDDT_Color *color, HIDDT_PixelFormat *pf)
173 HIDDT_Pixel red = color->red;
174 HIDDT_Pixel green = color->green;
175 HIDDT_Pixel blue = color->blue;
176 HIDDT_Pixel alpha = color->alpha;
178 /* This code assumes that sizeof(HIDDT_Pixel) is a multiple of sizeof(col->#?),
179 which should be true for most (all?) systems. (I have never heard of any
180 system with for example 3 byte types.)
183 if (HIDD_PF_SWAPPIXELBYTES(pf))
185 /* FIXME: int_map_truecolor assuming that SwapPixelBytes flag only set for 2-byte/16-bit pixel formats */
187 HIDDT_Pixel pixel = MAP_RGBA(red, green, blue, alpha, pf);
189 color->pixval = SWAPBYTES_WORD(pixel);
191 else
193 color->pixval = MAP_RGBA(red, green, blue, alpha, pf);
196 return color->pixval;
199 /*****************************************************************************************
201 NAME
202 moHidd_ColorMap_SetColors
204 SYNOPSIS
205 BOOL OOP_DoMethod(OOP_Object *obj, struct pHidd_ColorMap_SetColors *msg);
207 BOOL HIDD_CM_SetColors(OOP_Object *obj, HIDDT_Color *colors, ULONG firstColor,
208 ULONG numColors, OOP_Object *pixFmt);
210 LOCATION
211 CLID_Hidd_ColorMap
213 FUNCTION
215 INPUTS
216 obj -
217 colors -
218 firstColor -
219 numColors -
220 pixFmt -
222 RESULT
224 NOTES
226 EXAMPLE
228 BUGS
230 SEE ALSO
232 INTERNALS
234 *****************************************************************************************/
236 BOOL CM__Hidd_ColorMap__SetColors(OOP_Class *cl, OOP_Object *o,
237 struct pHidd_ColorMap_SetColors *msg)
239 struct colormap_data *data;
240 ULONG numnew;
241 ULONG i, col_idx;
242 HIDDT_Color *col;
243 HIDDT_PixelFormat *pf;
245 data = OOP_INST_DATA(cl, o);
247 numnew = msg->firstColor + msg->numColors;
249 /* See if there is enough space in the array */
251 if (numnew > data->clut.entries)
253 /* Reallocate and copy */
254 HIDDT_Color *newmap;
256 newmap = AllocMem(sizeof (*newmap) * numnew, MEMF_ANY);
257 if (NULL == newmap)
258 return FALSE;
260 memcpy(newmap, data->clut.colors, sizeof (*newmap) * data->clut.entries);
262 FreeMem(data->clut.colors, sizeof (*newmap) * data->clut.entries);
264 data->clut.colors = newmap;
265 data->clut.entries = numnew;
268 /* Insert the new colors */
269 col_idx = msg->firstColor;
270 col = &data->clut.colors[msg->firstColor];
271 pf = (HIDDT_PixelFormat *)msg->pixFmt;
273 for (i = 0; i < msg->numColors; i ++)
275 /* Set the color */
276 *col = msg->colors[i];
278 /* Set the pixval using the supplied pixel format */
279 if (IS_TRUECOLOR(pf))
281 /* Map the color to a HIDDT_Pixel */
282 msg->colors[i].pixval = col->pixval = int_map_truecolor(col, pf);
285 else
287 msg->colors[i].pixval = col->pixval = (HIDDT_Pixel)col_idx;
290 /* bug("ColMap::SetColors: col %d (%x %x %x %x) mapped to %x\n"
291 , col_idx
292 , col->red, col->green, col->blue, col->alpha
293 , msg->colors[i].pixval);
297 col ++;
298 col_idx ++;
301 return TRUE;
304 /*****************************************************************************************
306 NAME
307 moHidd_ColorMap_GetPixel
309 SYNOPSIS
310 HIDDT_Pixel OOP_DoMethod(OOP_Object *obj, struct pHidd_ColorMap_GetPixel *msg);
312 HIDDT_Pixel HIDD_CM_GetPixel(OOP_Object *obj, ULONG pixelNo);
314 LOCATION
315 CLID_Hidd_ColorMap
317 FUNCTION
319 INPUTS
320 obj -
321 pixelNo -
323 RESULT
325 NOTES
327 EXAMPLE
329 BUGS
331 SEE ALSO
333 INTERNALS
335 *****************************************************************************************/
337 HIDDT_Pixel CM__Hidd_ColorMap__GetPixel(OOP_Class *cl, OOP_Object *o,
338 struct pHidd_ColorMap_GetPixel *msg)
340 struct colormap_data *data;
342 data = OOP_INST_DATA(cl, o);
344 if (msg->pixelNo < 0 || msg->pixelNo >= data->clut.entries)
346 D(bug("!!! Invalid msg->pixelNo (%d) in ColorMap::GetPixel(). clutentries = %d\n",
347 msg->pixelNo,
348 data->clut.entries));
350 // *((ULONG *)0) = 0;
351 return (HIDDT_Pixel)-1;
355 return data->clut.colors[msg->pixelNo].pixval;
358 /*****************************************************************************************
360 NAME
361 moHidd_ColorMap_GetColor
363 SYNOPSIS
364 BOOL OOP_DoMethod(OOP_Object *o, struct pHidd_ColorMap_GetColor *msg);
366 BOOL HIDD_CM_GetColor(OOP_Object *obj, ULONG colorNo, HIDDT_Color *colorReturn);
368 LOCATION
369 CLID_Hidd_ColorMap
371 FUNCTION
373 INPUTS
374 obj -
375 colorNo -
376 colorReturn -
378 RESULT
380 NOTES
382 EXAMPLE
384 BUGS
386 SEE ALSO
388 INTERNALS
390 *****************************************************************************************/
392 BOOL CM__Hidd_ColorMap__GetColor(OOP_Class *cl, OOP_Object *o,
393 struct pHidd_ColorMap_GetColor *msg)
395 struct colormap_data *data;
397 data = OOP_INST_DATA(cl, o);
399 if (msg->colorNo < 0 || msg->colorNo >= data->clut.entries)
401 D(bug("!!! Invalid msg->colorNo (%d) in ColorMap::GetPixel(). clutentries = %d\n",
402 msg->colorNo,
403 data->clut.entries));
405 return FALSE;
408 *msg->colorReturn = data->clut.colors[msg->colorNo];
410 return TRUE;