1 /* $Id: colortab.c,v 1.10 2000/03/21 01:03:40 brianp Exp $ */
4 * Mesa 3-D graphics library
7 * Copyright (C) 1999-2000 Brian Paul All Rights Reserved.
9 * Permission is hereby granted, free of charge, to any person obtaining a
10 * copy of this software and associated documentation files (the "Software"),
11 * to deal in the Software without restriction, including without limitation
12 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
13 * and/or sell copies of the Software, and to permit persons to whom the
14 * Software is furnished to do so, subject to the following conditions:
16 * The above copyright notice and this permission notice shall be included
17 * in all copies or substantial portions of the Software.
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
20 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
22 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
23 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
24 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
41 * Return GL_TRUE if k is a power of two, else return GL_FALSE.
44 power_of_two( GLint k
)
47 for (i
=0; i
<32; i
++) {
57 decode_internal_format( GLint format
)
74 case GL_LUMINANCE_ALPHA
:
75 case GL_LUMINANCE4_ALPHA4
:
76 case GL_LUMINANCE6_ALPHA2
:
77 case GL_LUMINANCE8_ALPHA8
:
78 case GL_LUMINANCE12_ALPHA4
:
79 case GL_LUMINANCE12_ALPHA12
:
80 case GL_LUMINANCE16_ALPHA16
:
81 return GL_LUMINANCE_ALPHA
;
109 return -1; /* error */
115 _mesa_ColorTable( GLenum target
, GLenum internalFormat
,
116 GLsizei width
, GLenum format
, GLenum type
,
117 const GLvoid
*table
)
119 GET_CURRENT_CONTEXT(ctx
);
120 struct gl_texture_unit
*texUnit
= &ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
];
121 struct gl_texture_object
*texObj
;
122 struct gl_color_table
*palette
;
123 GLboolean proxy
= GL_FALSE
;
125 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
, "glColorTable");
129 texObj
= texUnit
->CurrentD
[1];
130 palette
= &texObj
->Palette
;
133 texObj
= texUnit
->CurrentD
[2];
134 palette
= &texObj
->Palette
;
137 texObj
= texUnit
->CurrentD
[3];
138 palette
= &texObj
->Palette
;
140 case GL_PROXY_TEXTURE_1D
:
141 texObj
= ctx
->Texture
.Proxy1D
;
142 palette
= &texObj
->Palette
;
145 case GL_PROXY_TEXTURE_2D
:
146 texObj
= ctx
->Texture
.Proxy2D
;
147 palette
= &texObj
->Palette
;
150 case GL_PROXY_TEXTURE_3D
:
151 texObj
= ctx
->Texture
.Proxy3D
;
152 palette
= &texObj
->Palette
;
155 case GL_SHARED_TEXTURE_PALETTE_EXT
:
157 palette
= &ctx
->Texture
.Palette
;
160 gl_error(ctx
, GL_INVALID_ENUM
, "glColorTable(target)");
166 if (!_mesa_is_legal_format_and_type(format
, type
)) {
167 gl_error(ctx
, GL_INVALID_ENUM
, "glColorTable(format or type)");
171 if (decode_internal_format(internalFormat
) < 0) {
172 gl_error( ctx
, GL_INVALID_ENUM
, "glColorTable(internalFormat)" );
176 if (width
< 1 || width
> MAX_TEXTURE_PALETTE_SIZE
|| !power_of_two(width
)) {
177 gl_error(ctx
, GL_INVALID_VALUE
, "glColorTable(width)");
180 palette
->IntFormat
= (GLenum
) 0;
181 palette
->Format
= (GLenum
) 0;
186 palette
->Size
= width
;
187 palette
->IntFormat
= internalFormat
;
188 palette
->Format
= (GLenum
) decode_internal_format(internalFormat
);
190 _mesa_unpack_ubyte_color_span(ctx
, width
, palette
->Format
,
191 palette
->Table
, /* dest */
193 &ctx
->Unpack
, GL_FALSE
);
196 /* per-texture object palette */
197 if (ctx
->Driver
.UpdateTexturePalette
) {
198 (*ctx
->Driver
.UpdateTexturePalette
)( ctx
, texObj
);
202 /* shared texture palette */
203 if (ctx
->Driver
.UpdateTexturePalette
) {
204 (*ctx
->Driver
.UpdateTexturePalette
)( ctx
, NULL
);
212 _mesa_ColorSubTable( GLenum target
, GLsizei start
,
213 GLsizei count
, GLenum format
, GLenum type
,
214 const GLvoid
*table
)
216 GET_CURRENT_CONTEXT(ctx
);
217 struct gl_texture_unit
*texUnit
= &ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
];
218 struct gl_texture_object
*texObj
;
219 struct gl_color_table
*palette
;
223 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
, "glColorSubTable");
227 texObj
= texUnit
->CurrentD
[1];
228 palette
= &texObj
->Palette
;
231 texObj
= texUnit
->CurrentD
[2];
232 palette
= &texObj
->Palette
;
235 texObj
= texUnit
->CurrentD
[3];
236 palette
= &texObj
->Palette
;
238 case GL_SHARED_TEXTURE_PALETTE_EXT
:
240 palette
= &ctx
->Texture
.Palette
;
243 gl_error(ctx
, GL_INVALID_ENUM
, "glColorSubTable(target)");
249 if (!_mesa_is_legal_format_and_type(format
, type
)) {
250 gl_error(ctx
, GL_INVALID_ENUM
, "glColorSubTable(format or type)");
255 gl_error(ctx
, GL_INVALID_VALUE
, "glColorSubTable(count)");
259 comps
= _mesa_components_in_format(format
);
260 assert(comps
> 0); /* error should be caught sooner */
262 if (start
+ count
> palette
->Size
) {
263 gl_error(ctx
, GL_INVALID_VALUE
, "glColorSubTable(count)");
266 dest
= palette
->Table
+ start
* comps
* sizeof(GLubyte
);
267 _mesa_unpack_ubyte_color_span(ctx
, count
, palette
->Format
, dest
,
269 &ctx
->Unpack
, GL_FALSE
);
272 /* per-texture object palette */
273 if (ctx
->Driver
.UpdateTexturePalette
) {
274 (*ctx
->Driver
.UpdateTexturePalette
)( ctx
, texObj
);
278 /* shared texture palette */
279 if (ctx
->Driver
.UpdateTexturePalette
) {
280 (*ctx
->Driver
.UpdateTexturePalette
)( ctx
, NULL
);
288 _mesa_GetColorTable( GLenum target
, GLenum format
,
289 GLenum type
, GLvoid
*table
)
291 GET_CURRENT_CONTEXT(ctx
);
292 struct gl_texture_unit
*texUnit
= &ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
];
293 struct gl_color_table
*palette
;
294 GLubyte rgba
[MAX_TEXTURE_PALETTE_SIZE
][4];
297 ASSERT_OUTSIDE_BEGIN_END(ctx
, "glGetColorTable");
301 palette
= &texUnit
->CurrentD
[1]->Palette
;
304 palette
= &texUnit
->CurrentD
[2]->Palette
;
307 palette
= &texUnit
->CurrentD
[3]->Palette
;
309 case GL_SHARED_TEXTURE_PALETTE_EXT
:
310 palette
= &ctx
->Texture
.Palette
;
313 gl_error(ctx
, GL_INVALID_ENUM
, "glGetColorTable(target)");
319 switch (palette
->Format
) {
321 for (i
= 0; i
< palette
->Size
; i
++) {
325 rgba
[i
][ACOMP
] = palette
->Table
[i
];
329 for (i
= 0; i
< palette
->Size
; i
++) {
330 rgba
[i
][RCOMP
] = palette
->Table
[i
];
331 rgba
[i
][GCOMP
] = palette
->Table
[i
];
332 rgba
[i
][BCOMP
] = palette
->Table
[i
];
333 rgba
[i
][ACOMP
] = 255;
336 case GL_LUMINANCE_ALPHA
:
337 for (i
= 0; i
< palette
->Size
; i
++) {
338 rgba
[i
][RCOMP
] = palette
->Table
[i
*2+0];
339 rgba
[i
][GCOMP
] = palette
->Table
[i
*2+0];
340 rgba
[i
][BCOMP
] = palette
->Table
[i
*2+0];
341 rgba
[i
][ACOMP
] = palette
->Table
[i
*2+1];
345 for (i
= 0; i
< palette
->Size
; i
++) {
346 rgba
[i
][RCOMP
] = palette
->Table
[i
];
347 rgba
[i
][GCOMP
] = palette
->Table
[i
];
348 rgba
[i
][BCOMP
] = palette
->Table
[i
];
349 rgba
[i
][ACOMP
] = 255;
353 for (i
= 0; i
< palette
->Size
; i
++) {
354 rgba
[i
][RCOMP
] = palette
->Table
[i
*3+0];
355 rgba
[i
][GCOMP
] = palette
->Table
[i
*3+1];
356 rgba
[i
][BCOMP
] = palette
->Table
[i
*3+2];
357 rgba
[i
][ACOMP
] = 255;
361 for (i
= 0; i
< palette
->Size
; i
++) {
362 rgba
[i
][RCOMP
] = palette
->Table
[i
*4+0];
363 rgba
[i
][GCOMP
] = palette
->Table
[i
*4+1];
364 rgba
[i
][BCOMP
] = palette
->Table
[i
*4+2];
365 rgba
[i
][ACOMP
] = palette
->Table
[i
*4+3];
369 gl_problem(ctx
, "bad palette format in glGetColorTable");
373 _mesa_pack_rgba_span(ctx
, palette
->Size
, (const GLubyte (*)[]) rgba
,
374 format
, type
, table
, &ctx
->Pack
, GL_FALSE
);
384 _mesa_GetColorTableParameterfv( GLenum target
, GLenum pname
, GLfloat
*params
)
387 _mesa_GetColorTableParameteriv( target
, pname
, iparams
);
388 *params
= (GLfloat
) iparams
[0];
394 _mesa_GetColorTableParameteriv( GLenum target
, GLenum pname
, GLint
*params
)
396 GET_CURRENT_CONTEXT(ctx
);
397 struct gl_texture_unit
*texUnit
= &ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
];
398 struct gl_color_table
*palette
;
400 ASSERT_OUTSIDE_BEGIN_END(ctx
, "glGetColorTableParameter");
404 palette
= &texUnit
->CurrentD
[1]->Palette
;
407 palette
= &texUnit
->CurrentD
[2]->Palette
;
410 palette
= &texUnit
->CurrentD
[3]->Palette
;
412 case GL_PROXY_TEXTURE_1D
:
413 palette
= &ctx
->Texture
.Proxy1D
->Palette
;
415 case GL_PROXY_TEXTURE_2D
:
416 palette
= &ctx
->Texture
.Proxy2D
->Palette
;
418 case GL_PROXY_TEXTURE_3D
:
419 palette
= &ctx
->Texture
.Proxy3D
->Palette
;
421 case GL_SHARED_TEXTURE_PALETTE_EXT
:
422 palette
= &ctx
->Texture
.Palette
;
425 gl_error(ctx
, GL_INVALID_ENUM
, "glGetColorTableParameter(target)");
430 case GL_COLOR_TABLE_FORMAT
:
431 *params
= palette
->IntFormat
;
433 case GL_COLOR_TABLE_WIDTH
:
434 *params
= palette
->Size
;
436 case GL_COLOR_TABLE_RED_SIZE
:
439 case GL_COLOR_TABLE_GREEN_SIZE
:
442 case GL_COLOR_TABLE_BLUE_SIZE
:
445 case GL_COLOR_TABLE_ALPHA_SIZE
:
448 case GL_COLOR_TABLE_LUMINANCE_SIZE
:
451 case GL_COLOR_TABLE_INTENSITY_SIZE
:
455 gl_error(ctx
, GL_INVALID_ENUM
, "glGetColorTableParameter" );