2 Copyright © 1995-2001, 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 /****************************************************************************************/
32 OOP_Object
*CM__Root__New(OOP_Class
*cl
, OOP_Object
*o
, struct pRoot_New
*msg
)
34 struct colormap_data
*data
;
36 struct TagItem
*tag
, *tstate
;
39 EnterFunc(bug("ColorMap::New()\n"));
42 for (tstate
= msg
->attrList
; (tag
= NextTagItem((const struct TagItem
**)&tstate
)); )
46 if (IS_COLORMAP_ATTR(tag
->ti_Tag
, 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"));
63 /* Create the object */
65 o
= (OOP_Object
*)OOP_DoSuperMethod(cl
, o
, (OOP_Msg
)msg
);
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
)
83 dispose_mid
= OOP_GetMethodID(IID_Root
, moRoot_Dispose
);
84 OOP_CoerceMethod(cl
, o
, (OOP_Msg
)&dispose_mid
);
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
);
112 /****************************************************************************************/
114 VOID
CM__Root__Get(OOP_Class
*cl
, OOP_Object
*o
, struct pRoot_Get
*msg
)
116 struct colormap_data
*data
;
119 EnterFunc(bug("ColorMap::Get()\n"));
120 data
= OOP_INST_DATA(cl
, o
);
122 if (IS_COLORMAP_ATTR(msg
->attrID
, idx
))
126 case aoHidd_ColorMap_NumEntries
:
127 *msg
->storage
= data
->clut
.entries
;
131 D(bug("!!! Unknow colormap attr in ColorMap::Get()\n"));
132 OOP_DoSuperMethod(cl
, o
, (OOP_Msg
)msg
);
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
;
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 */
166 newmap
= AllocMem(sizeof (*newmap
) * numnew
, MEMF_ANY
);
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
++)
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
);
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"
202 , col->red, col->green, col->blue, col->alpha
203 , msg->colors[i].pixval);
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
);;
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",
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",
281 data
->clut
.entries
));
286 *msg
->colorReturn
= data
->clut
.colors
[msg
->colorNo
];
291 /****************************************************************************************/