2 Copyright © 1995-2018, The AROS Development Team. All rights reserved.
5 Desc: Graphics colormap class implementation.
9 /****************************************************************************************/
11 #include "gfx_debug.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>
23 #include <graphics/text.h>
27 #include "gfx_intern.h"
29 /*****************************************************************************************
32 aoHidd_ColorMap_NumEntries
41 Number of colors in the colormap.
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
;
61 struct TagItem
*tag
, *tstate
;
64 EnterFunc(bug("ColorMap::New()\n"));
67 for (tstate
= msg
->attrList
; (tag
= NextTagItem(&tstate
)); )
71 if (IS_COLORMAP_ATTR(tag
->ti_Tag
, 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"));
88 /* Create the object */
90 o
= (OOP_Object
*)OOP_DoSuperMethod(cl
, o
, (OOP_Msg
)msg
);
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
)
108 dispose_mid
= OOP_GetMethodID(IID_Root
, moRoot_Dispose
);
109 OOP_CoerceMethod(cl
, o
, (OOP_Msg
)&dispose_mid
);
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
);
137 /****************************************************************************************/
139 VOID
CM__Root__Get(OOP_Class
*cl
, OOP_Object
*o
, struct pRoot_Get
*msg
)
141 struct colormap_data
*data
;
144 EnterFunc(bug("ColorMap::Get()\n"));
145 data
= OOP_INST_DATA(cl
, o
);
147 if (IS_COLORMAP_ATTR(msg
->attrID
, idx
))
151 case aoHidd_ColorMap_NumEntries
:
152 *msg
->storage
= data
->clut
.entries
;
156 D(bug("!!! Unknown colormap attr in ColorMap::Get()\n"));
157 OOP_DoSuperMethod(cl
, o
, (OOP_Msg
)msg
);
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
);
193 color
->pixval
= MAP_RGBA(red
, green
, blue
, alpha
, pf
);
196 return color
->pixval
;
199 /*****************************************************************************************
202 moHidd_ColorMap_SetColors
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);
234 *****************************************************************************************/
236 BOOL
CM__Hidd_ColorMap__SetColors(OOP_Class
*cl
, OOP_Object
*o
,
237 struct pHidd_ColorMap_SetColors
*msg
)
239 struct colormap_data
*data
;
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 */
256 newmap
= AllocMem(sizeof (*newmap
) * numnew
, MEMF_ANY
);
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
++)
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
);
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"
292 , col->red, col->green, col->blue, col->alpha
293 , msg->colors[i].pixval);
304 /*****************************************************************************************
307 moHidd_ColorMap_GetPixel
310 HIDDT_Pixel OOP_DoMethod(OOP_Object *obj, struct pHidd_ColorMap_GetPixel *msg);
312 HIDDT_Pixel HIDD_CM_GetPixel(OOP_Object *obj, ULONG pixelNo);
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",
348 data
->clut
.entries
));
350 // *((ULONG *)0) = 0;
351 return (HIDDT_Pixel
)-1;
355 return data
->clut
.colors
[msg
->pixelNo
].pixval
;
358 /*****************************************************************************************
361 moHidd_ColorMap_GetColor
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);
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",
403 data
->clut
.entries
));
408 *msg
->colorReturn
= data
->clut
.colors
[msg
->colorNo
];