2 Copyright © 1995-2011, The AROS Development Team. All rights reserved.
5 Desc: Graphics colormap class implementation.
9 /****************************************************************************************/
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>
21 #include <graphics/text.h>
23 #include <hidd/graphics.h>
25 #include "graphics_intern.h"
28 #include <aros/debug.h>
30 /*****************************************************************************************
33 aoHidd_ColorMap_NumEntries
42 Number of colors in the colormap.
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
;
62 struct TagItem
*tag
, *tstate
;
65 EnterFunc(bug("ColorMap::New()\n"));
68 for (tstate
= msg
->attrList
; (tag
= NextTagItem(&tstate
)); )
72 if (IS_COLORMAP_ATTR(tag
->ti_Tag
, 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"));
89 /* Create the object */
91 o
= (OOP_Object
*)OOP_DoSuperMethod(cl
, o
, (OOP_Msg
)msg
);
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
)
109 dispose_mid
= OOP_GetMethodID(IID_Root
, moRoot_Dispose
);
110 OOP_CoerceMethod(cl
, o
, (OOP_Msg
)&dispose_mid
);
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
);
138 /****************************************************************************************/
140 VOID
CM__Root__Get(OOP_Class
*cl
, OOP_Object
*o
, struct pRoot_Get
*msg
)
142 struct colormap_data
*data
;
145 EnterFunc(bug("ColorMap::Get()\n"));
146 data
= OOP_INST_DATA(cl
, o
);
148 if (IS_COLORMAP_ATTR(msg
->attrID
, idx
))
152 case aoHidd_ColorMap_NumEntries
:
153 *msg
->storage
= data
->clut
.entries
;
157 D(bug("!!! Unknown colormap attr in ColorMap::Get()\n"));
158 OOP_DoSuperMethod(cl
, o
, (OOP_Msg
)msg
);
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
);
194 color
->pixval
= MAP_RGBA(red
, green
, blue
, alpha
, pf
);
197 return color
->pixval
;
200 /*****************************************************************************************
203 moHidd_ColorMap_SetColors
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);
235 *****************************************************************************************/
237 BOOL
CM__Hidd_ColorMap__SetColors(OOP_Class
*cl
, OOP_Object
*o
,
238 struct pHidd_ColorMap_SetColors
*msg
)
240 struct colormap_data
*data
;
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 */
257 newmap
= AllocMem(sizeof (*newmap
) * numnew
, MEMF_ANY
);
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
++)
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
);
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"
293 , col->red, col->green, col->blue, col->alpha
294 , msg->colors[i].pixval);
305 /*****************************************************************************************
308 moHidd_ColorMap_GetPixel
311 HIDDT_Pixel OOP_DoMethod(OOP_Object *obj, struct pHidd_ColorMap_GetPixel *msg);
313 HIDDT_Pixel HIDD_CM_GetPixel(OOP_Object *obj, ULONG pixelNo);
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",
349 data
->clut
.entries
));
351 // *((ULONG *)0) = 0;
352 return (HIDDT_Pixel
)-1;
356 return data
->clut
.colors
[msg
->pixelNo
].pixval
;
359 /*****************************************************************************************
362 moHidd_ColorMap_GetColor
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);
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",
404 data
->clut
.entries
));
409 *msg
->colorReturn
= data
->clut
.colors
[msg
->colorNo
];