2 * Mesa 3-D graphics library
5 * Copyright (C) 1999-2007 Brian Paul All Rights Reserved.
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the "Software"),
9 * to deal in the Software without restriction, including without limitation
10 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11 * and/or sell copies of the Software, and to permit persons to whom the
12 * Software is furnished to do so, subject to the following conditions:
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
21 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27 #include "bufferobj.h"
32 #include "mfeatures.h"
38 #include "main/dispatch.h"
41 #if FEATURE_colortable
45 * Given an internalFormat token passed to glColorTable,
46 * return the corresponding base format.
47 * Return -1 if invalid token.
50 base_colortab_format( GLenum format
)
65 case GL_LUMINANCE_ALPHA
:
66 case GL_LUMINANCE4_ALPHA4
:
67 case GL_LUMINANCE6_ALPHA2
:
68 case GL_LUMINANCE8_ALPHA8
:
69 case GL_LUMINANCE12_ALPHA4
:
70 case GL_LUMINANCE12_ALPHA12
:
71 case GL_LUMINANCE16_ALPHA16
:
72 return GL_LUMINANCE_ALPHA
;
98 return -1; /* error */
105 * Examine table's format and set the component sizes accordingly.
108 set_component_sizes( struct gl_color_table
*table
)
110 /* assuming the ubyte table */
111 const GLubyte sz
= 8;
113 switch (table
->_BaseFormat
) {
116 table
->GreenSize
= 0;
118 table
->AlphaSize
= sz
;
119 table
->IntensitySize
= 0;
120 table
->LuminanceSize
= 0;
124 table
->GreenSize
= 0;
126 table
->AlphaSize
= 0;
127 table
->IntensitySize
= 0;
128 table
->LuminanceSize
= sz
;
130 case GL_LUMINANCE_ALPHA
:
132 table
->GreenSize
= 0;
134 table
->AlphaSize
= sz
;
135 table
->IntensitySize
= 0;
136 table
->LuminanceSize
= sz
;
140 table
->GreenSize
= 0;
142 table
->AlphaSize
= 0;
143 table
->IntensitySize
= sz
;
144 table
->LuminanceSize
= 0;
148 table
->GreenSize
= sz
;
149 table
->BlueSize
= sz
;
150 table
->AlphaSize
= 0;
151 table
->IntensitySize
= 0;
152 table
->LuminanceSize
= 0;
156 table
->GreenSize
= sz
;
157 table
->BlueSize
= sz
;
158 table
->AlphaSize
= sz
;
159 table
->IntensitySize
= 0;
160 table
->LuminanceSize
= 0;
163 _mesa_problem(NULL
, "unexpected format in set_component_sizes");
170 * Update/replace all or part of a color table. Helper function
171 * used by _mesa_ColorTable() and _mesa_ColorSubTable().
172 * The table->Table buffer should already be allocated.
173 * \param start first entry to update
174 * \param count number of entries to update
175 * \param format format of user-provided table data
176 * \param type datatype of user-provided table data
177 * \param data user-provided table data
178 * \param [rgba]Scale - RGBA scale factors
179 * \param [rgba]Bias - RGBA bias factors
182 store_colortable_entries(struct gl_context
*ctx
, struct gl_color_table
*table
,
183 GLsizei start
, GLsizei count
,
184 GLenum format
, GLenum type
, const GLvoid
*data
,
185 GLfloat rScale
, GLfloat rBias
,
186 GLfloat gScale
, GLfloat gBias
,
187 GLfloat bScale
, GLfloat bBias
,
188 GLfloat aScale
, GLfloat aBias
)
190 data
= _mesa_map_validate_pbo_source(ctx
,
191 1, &ctx
->Unpack
, count
, 1, 1,
193 "glColor[Sub]Table");
198 /* convert user-provided data to GLfloat values */
199 GLfloat tempTab
[MAX_COLOR_TABLE_SIZE
* 4];
203 _mesa_unpack_color_span_float(ctx
,
204 count
, /* number of pixels */
205 table
->_BaseFormat
, /* dest format */
206 tempTab
, /* dest address */
207 format
, type
, /* src format/type */
210 IMAGE_CLAMP_BIT
); /* transfer ops */
212 /* the destination */
213 tableF
= table
->TableF
;
215 /* Apply scale & bias & clamp now */
216 switch (table
->_BaseFormat
) {
218 for (i
= 0; i
< count
; i
++) {
219 GLuint j
= start
+ i
;
220 tableF
[j
] = CLAMP(tempTab
[i
] * rScale
+ rBias
, 0.0F
, 1.0F
);
224 for (i
= 0; i
< count
; i
++) {
225 GLuint j
= start
+ i
;
226 tableF
[j
] = CLAMP(tempTab
[i
] * rScale
+ rBias
, 0.0F
, 1.0F
);
230 for (i
= 0; i
< count
; i
++) {
231 GLuint j
= start
+ i
;
232 tableF
[j
] = CLAMP(tempTab
[i
] * aScale
+ aBias
, 0.0F
, 1.0F
);
235 case GL_LUMINANCE_ALPHA
:
236 for (i
= 0; i
< count
; i
++) {
237 GLuint j
= start
+ i
;
238 tableF
[j
*2+0] = CLAMP(tempTab
[i
*2+0] * rScale
+ rBias
, 0.0F
, 1.0F
);
239 tableF
[j
*2+1] = CLAMP(tempTab
[i
*2+1] * aScale
+ aBias
, 0.0F
, 1.0F
);
243 for (i
= 0; i
< count
; i
++) {
244 GLuint j
= start
+ i
;
245 tableF
[j
*3+0] = CLAMP(tempTab
[i
*3+0] * rScale
+ rBias
, 0.0F
, 1.0F
);
246 tableF
[j
*3+1] = CLAMP(tempTab
[i
*3+1] * gScale
+ gBias
, 0.0F
, 1.0F
);
247 tableF
[j
*3+2] = CLAMP(tempTab
[i
*3+2] * bScale
+ bBias
, 0.0F
, 1.0F
);
251 for (i
= 0; i
< count
; i
++) {
252 GLuint j
= start
+ i
;
253 tableF
[j
*4+0] = CLAMP(tempTab
[i
*4+0] * rScale
+ rBias
, 0.0F
, 1.0F
);
254 tableF
[j
*4+1] = CLAMP(tempTab
[i
*4+1] * gScale
+ gBias
, 0.0F
, 1.0F
);
255 tableF
[j
*4+2] = CLAMP(tempTab
[i
*4+2] * bScale
+ bBias
, 0.0F
, 1.0F
);
256 tableF
[j
*4+3] = CLAMP(tempTab
[i
*4+3] * aScale
+ aBias
, 0.0F
, 1.0F
);
260 _mesa_problem(ctx
, "Bad format in store_colortable_entries");
265 /* update the ubyte table */
267 const GLint comps
= _mesa_components_in_format(table
->_BaseFormat
);
268 const GLfloat
*tableF
= table
->TableF
+ start
* comps
;
269 GLubyte
*tableUB
= table
->TableUB
+ start
* comps
;
271 for (i
= 0; i
< count
* comps
; i
++) {
272 CLAMPED_FLOAT_TO_UBYTE(tableUB
[i
], tableF
[i
]);
276 _mesa_unmap_pbo_source(ctx
, &ctx
->Unpack
);
282 _mesa_ColorTable( GLenum target
, GLenum internalFormat
,
283 GLsizei width
, GLenum format
, GLenum type
,
286 static const GLfloat one
[4] = { 1.0, 1.0, 1.0, 1.0 };
287 static const GLfloat zero
[4] = { 0.0, 0.0, 0.0, 0.0 };
288 GET_CURRENT_CONTEXT(ctx
);
289 struct gl_texture_unit
*texUnit
= _mesa_get_current_tex_unit(ctx
);
290 struct gl_texture_object
*texObj
= NULL
;
291 struct gl_color_table
*table
= NULL
;
292 GLboolean proxy
= GL_FALSE
;
294 const GLfloat
*scale
= one
, *bias
= zero
;
297 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
); /* too complex */
300 case GL_SHARED_TEXTURE_PALETTE_EXT
:
301 table
= &ctx
->Texture
.Palette
;
304 /* try texture targets */
306 struct gl_texture_object
*texobj
307 = _mesa_select_tex_object(ctx
, texUnit
, target
);
309 table
= &texobj
->Palette
;
310 proxy
= _mesa_is_proxy_texture(target
);
313 _mesa_error(ctx
, GL_INVALID_ENUM
, "glColorTable(target)");
321 if (!_mesa_is_legal_format_and_type(ctx
, format
, type
) ||
322 format
== GL_INTENSITY
) {
323 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glColorTable(format or type)");
327 baseFormat
= base_colortab_format(internalFormat
);
328 if (baseFormat
< 0) {
329 _mesa_error(ctx
, GL_INVALID_ENUM
, "glColorTable(internalFormat)");
333 if (width
< 0 || (width
!= 0 && !_mesa_is_pow_two(width
))) {
337 table
->InternalFormat
= (GLenum
) 0;
338 table
->_BaseFormat
= (GLenum
) 0;
341 _mesa_error(ctx
, GL_INVALID_VALUE
, "glColorTable(width=%d)", width
);
346 if (width
> (GLsizei
) ctx
->Const
.MaxColorTableSize
) {
349 table
->InternalFormat
= (GLenum
) 0;
350 table
->_BaseFormat
= (GLenum
) 0;
353 _mesa_error(ctx
, GL_TABLE_TOO_LARGE
, "glColorTable(width)");
359 table
->InternalFormat
= internalFormat
;
360 table
->_BaseFormat
= (GLenum
) baseFormat
;
362 comps
= _mesa_components_in_format(table
->_BaseFormat
);
363 assert(comps
> 0); /* error should have been caught sooner */
366 _mesa_free_colortable_data(table
);
369 table
->TableF
= (GLfloat
*) malloc(comps
* width
* sizeof(GLfloat
));
370 table
->TableUB
= (GLubyte
*) malloc(comps
* width
* sizeof(GLubyte
));
372 if (!table
->TableF
|| !table
->TableUB
) {
373 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glColorTable");
377 store_colortable_entries(ctx
, table
,
378 0, width
, /* start, count */
387 /* do this after the table's Type and Format are set */
388 set_component_sizes(table
);
390 if (texObj
|| target
== GL_SHARED_TEXTURE_PALETTE_EXT
) {
391 /* texture object palette, texObj==NULL means the shared palette */
392 if (ctx
->Driver
.UpdateTexturePalette
) {
393 (*ctx
->Driver
.UpdateTexturePalette
)( ctx
, texObj
);
397 ctx
->NewState
|= _NEW_PIXEL
;
403 _mesa_ColorSubTable( GLenum target
, GLsizei start
,
404 GLsizei count
, GLenum format
, GLenum type
,
407 static const GLfloat one
[4] = { 1.0, 1.0, 1.0, 1.0 };
408 static const GLfloat zero
[4] = { 0.0, 0.0, 0.0, 0.0 };
409 GET_CURRENT_CONTEXT(ctx
);
410 struct gl_texture_unit
*texUnit
= _mesa_get_current_tex_unit(ctx
);
411 struct gl_texture_object
*texObj
= NULL
;
412 struct gl_color_table
*table
= NULL
;
413 const GLfloat
*scale
= one
, *bias
= zero
;
415 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
418 case GL_SHARED_TEXTURE_PALETTE_EXT
:
419 table
= &ctx
->Texture
.Palette
;
422 /* try texture targets */
423 texObj
= _mesa_select_tex_object(ctx
, texUnit
, target
);
424 if (texObj
&& !_mesa_is_proxy_texture(target
)) {
425 table
= &texObj
->Palette
;
428 _mesa_error(ctx
, GL_INVALID_ENUM
, "glColorSubTable(target)");
435 if (!_mesa_is_legal_format_and_type(ctx
, format
, type
) ||
436 format
== GL_INTENSITY
) {
437 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glColorSubTable(format or type)");
442 _mesa_error(ctx
, GL_INVALID_VALUE
, "glColorSubTable(count)");
446 /* error should have been caught sooner */
447 assert(_mesa_components_in_format(table
->_BaseFormat
) > 0);
449 if (start
+ count
> (GLint
) table
->Size
) {
450 _mesa_error(ctx
, GL_INVALID_VALUE
, "glColorSubTable(count)");
454 if (!table
->TableF
|| !table
->TableUB
) {
455 /* a GL_OUT_OF_MEMORY error would have been recorded previously */
459 store_colortable_entries(ctx
, table
, start
, count
,
466 if (texObj
|| target
== GL_SHARED_TEXTURE_PALETTE_EXT
) {
467 /* per-texture object palette */
468 if (ctx
->Driver
.UpdateTexturePalette
) {
469 (*ctx
->Driver
.UpdateTexturePalette
)( ctx
, texObj
);
473 ctx
->NewState
|= _NEW_PIXEL
;
478 static void GLAPIENTRY
479 _mesa_CopyColorTable(GLenum target
, GLenum internalformat
,
480 GLint x
, GLint y
, GLsizei width
)
482 GET_CURRENT_CONTEXT(ctx
);
483 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
485 if (!ctx
->ReadBuffer
->_ColorReadBuffer
) {
486 return; /* no readbuffer - OK */
489 ctx
->Driver
.CopyColorTable( ctx
, target
, internalformat
, x
, y
, width
);
494 static void GLAPIENTRY
495 _mesa_CopyColorSubTable(GLenum target
, GLsizei start
,
496 GLint x
, GLint y
, GLsizei width
)
498 GET_CURRENT_CONTEXT(ctx
);
499 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
501 if (!ctx
->ReadBuffer
->_ColorReadBuffer
) {
502 return; /* no readbuffer - OK */
505 ctx
->Driver
.CopyColorSubTable( ctx
, target
, start
, x
, y
, width
);
510 static void GLAPIENTRY
511 _mesa_GetColorTable( GLenum target
, GLenum format
,
512 GLenum type
, GLvoid
*data
)
514 GET_CURRENT_CONTEXT(ctx
);
515 struct gl_texture_unit
*texUnit
= _mesa_get_current_tex_unit(ctx
);
516 struct gl_color_table
*table
= NULL
;
517 GLfloat rgba
[MAX_COLOR_TABLE_SIZE
][4];
518 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
521 _mesa_update_state(ctx
);
525 case GL_SHARED_TEXTURE_PALETTE_EXT
:
526 table
= &ctx
->Texture
.Palette
;
529 /* try texture targets */
531 struct gl_texture_object
*texobj
532 = _mesa_select_tex_object(ctx
, texUnit
, target
);
533 if (texobj
&& !_mesa_is_proxy_texture(target
)) {
534 table
= &texobj
->Palette
;
537 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetColorTable(target)");
545 if (table
->Size
<= 0) {
549 switch (table
->_BaseFormat
) {
553 for (i
= 0; i
< table
->Size
; i
++) {
557 rgba
[i
][ACOMP
] = table
->TableF
[i
];
564 for (i
= 0; i
< table
->Size
; i
++) {
567 rgba
[i
][BCOMP
] = table
->TableF
[i
];
568 rgba
[i
][ACOMP
] = 1.0F
;
572 case GL_LUMINANCE_ALPHA
:
575 for (i
= 0; i
< table
->Size
; i
++) {
578 rgba
[i
][BCOMP
] = table
->TableF
[i
*2+0];
579 rgba
[i
][ACOMP
] = table
->TableF
[i
*2+1];
586 for (i
= 0; i
< table
->Size
; i
++) {
590 rgba
[i
][ACOMP
] = table
->TableF
[i
];
597 for (i
= 0; i
< table
->Size
; i
++) {
598 rgba
[i
][RCOMP
] = table
->TableF
[i
*3+0];
599 rgba
[i
][GCOMP
] = table
->TableF
[i
*3+1];
600 rgba
[i
][BCOMP
] = table
->TableF
[i
*3+2];
601 rgba
[i
][ACOMP
] = 1.0F
;
606 memcpy(rgba
, table
->TableF
, 4 * table
->Size
* sizeof(GLfloat
));
609 _mesa_problem(ctx
, "bad table format in glGetColorTable");
613 data
= _mesa_map_validate_pbo_dest(ctx
,
614 1, &ctx
->Pack
, table
->Size
, 1, 1,
620 _mesa_pack_rgba_span_float(ctx
, table
->Size
, rgba
,
621 format
, type
, data
, &ctx
->Pack
, 0x0);
623 _mesa_unmap_pbo_dest(ctx
, &ctx
->Pack
);
628 static void GLAPIENTRY
629 _mesa_ColorTableParameterfv(GLenum target
, GLenum pname
, const GLfloat
*params
)
631 GLfloat
*scale
, *bias
;
632 GET_CURRENT_CONTEXT(ctx
);
633 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
637 _mesa_error(ctx
, GL_INVALID_ENUM
, "glColorTableParameter(target)");
641 if (pname
== GL_COLOR_TABLE_SCALE_SGI
) {
642 COPY_4V(scale
, params
);
644 else if (pname
== GL_COLOR_TABLE_BIAS_SGI
) {
645 COPY_4V(bias
, params
);
648 _mesa_error(ctx
, GL_INVALID_ENUM
, "glColorTableParameterfv(pname)");
652 ctx
->NewState
|= _NEW_PIXEL
;
657 static void GLAPIENTRY
658 _mesa_ColorTableParameteriv(GLenum target
, GLenum pname
, const GLint
*params
)
661 fparams
[0] = (GLfloat
) params
[0];
662 _mesa_ColorTableParameterfv(target
, pname
, fparams
);
667 static void GLAPIENTRY
668 _mesa_GetColorTableParameterfv( GLenum target
, GLenum pname
, GLfloat
*params
)
670 GET_CURRENT_CONTEXT(ctx
);
671 struct gl_texture_unit
*texUnit
= _mesa_get_current_tex_unit(ctx
);
672 struct gl_color_table
*table
= NULL
;
673 ASSERT_OUTSIDE_BEGIN_END(ctx
);
676 case GL_SHARED_TEXTURE_PALETTE_EXT
:
677 table
= &ctx
->Texture
.Palette
;
680 /* try texture targets */
682 struct gl_texture_object
*texobj
683 = _mesa_select_tex_object(ctx
, texUnit
, target
);
685 table
= &texobj
->Palette
;
688 _mesa_error(ctx
, GL_INVALID_ENUM
,
689 "glGetColorTableParameterfv(target)");
698 case GL_COLOR_TABLE_FORMAT
:
699 *params
= (GLfloat
) table
->InternalFormat
;
701 case GL_COLOR_TABLE_WIDTH
:
702 *params
= (GLfloat
) table
->Size
;
704 case GL_COLOR_TABLE_RED_SIZE
:
705 *params
= (GLfloat
) table
->RedSize
;
707 case GL_COLOR_TABLE_GREEN_SIZE
:
708 *params
= (GLfloat
) table
->GreenSize
;
710 case GL_COLOR_TABLE_BLUE_SIZE
:
711 *params
= (GLfloat
) table
->BlueSize
;
713 case GL_COLOR_TABLE_ALPHA_SIZE
:
714 *params
= (GLfloat
) table
->AlphaSize
;
716 case GL_COLOR_TABLE_LUMINANCE_SIZE
:
717 *params
= (GLfloat
) table
->LuminanceSize
;
719 case GL_COLOR_TABLE_INTENSITY_SIZE
:
720 *params
= (GLfloat
) table
->IntensitySize
;
723 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetColorTableParameterfv(pname)" );
730 static void GLAPIENTRY
731 _mesa_GetColorTableParameteriv( GLenum target
, GLenum pname
, GLint
*params
)
733 GET_CURRENT_CONTEXT(ctx
);
734 struct gl_texture_unit
*texUnit
= _mesa_get_current_tex_unit(ctx
);
735 struct gl_color_table
*table
= NULL
;
736 ASSERT_OUTSIDE_BEGIN_END(ctx
);
739 case GL_SHARED_TEXTURE_PALETTE_EXT
:
740 table
= &ctx
->Texture
.Palette
;
743 /* Try texture targets */
745 struct gl_texture_object
*texobj
746 = _mesa_select_tex_object(ctx
, texUnit
, target
);
748 table
= &texobj
->Palette
;
751 _mesa_error(ctx
, GL_INVALID_ENUM
,
752 "glGetColorTableParameteriv(target)");
761 case GL_COLOR_TABLE_FORMAT
:
762 *params
= table
->InternalFormat
;
764 case GL_COLOR_TABLE_WIDTH
:
765 *params
= table
->Size
;
767 case GL_COLOR_TABLE_RED_SIZE
:
768 *params
= table
->RedSize
;
770 case GL_COLOR_TABLE_GREEN_SIZE
:
771 *params
= table
->GreenSize
;
773 case GL_COLOR_TABLE_BLUE_SIZE
:
774 *params
= table
->BlueSize
;
776 case GL_COLOR_TABLE_ALPHA_SIZE
:
777 *params
= table
->AlphaSize
;
779 case GL_COLOR_TABLE_LUMINANCE_SIZE
:
780 *params
= table
->LuminanceSize
;
782 case GL_COLOR_TABLE_INTENSITY_SIZE
:
783 *params
= table
->IntensitySize
;
786 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetColorTableParameteriv(pname)" );
793 _mesa_init_colortable_dispatch(struct _glapi_table
*disp
)
795 SET_ColorSubTable(disp
, _mesa_ColorSubTable
);
796 SET_ColorTable(disp
, _mesa_ColorTable
);
797 SET_ColorTableParameterfv(disp
, _mesa_ColorTableParameterfv
);
798 SET_ColorTableParameteriv(disp
, _mesa_ColorTableParameteriv
);
799 SET_CopyColorSubTable(disp
, _mesa_CopyColorSubTable
);
800 SET_CopyColorTable(disp
, _mesa_CopyColorTable
);
801 SET_GetColorTable(disp
, _mesa_GetColorTable
);
802 SET_GetColorTableParameterfv(disp
, _mesa_GetColorTableParameterfv
);
803 SET_GetColorTableParameteriv(disp
, _mesa_GetColorTableParameteriv
);
807 #endif /* FEATURE_colortable */
810 /**********************************************************************/
811 /***** Initialization *****/
812 /**********************************************************************/
816 _mesa_init_colortable( struct gl_color_table
*p
)
821 p
->InternalFormat
= GL_RGBA
;
827 _mesa_free_colortable_data( struct gl_color_table
*p
)