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"
39 #include "main/dispatch.h"
42 #if FEATURE_colortable
46 * Given an internalFormat token passed to glColorTable,
47 * return the corresponding base format.
48 * Return -1 if invalid token.
51 base_colortab_format( GLenum format )
66 case GL_LUMINANCE_ALPHA:
67 case GL_LUMINANCE4_ALPHA4:
68 case GL_LUMINANCE6_ALPHA2:
69 case GL_LUMINANCE8_ALPHA8:
70 case GL_LUMINANCE12_ALPHA4:
71 case GL_LUMINANCE12_ALPHA12:
72 case GL_LUMINANCE16_ALPHA16:
73 return GL_LUMINANCE_ALPHA;
99 return -1; /* error */
106 * Examine table's format and set the component sizes accordingly.
109 set_component_sizes( struct gl_color_table *table )
111 /* assuming the ubyte table */
112 const GLubyte sz = 8;
114 switch (table->_BaseFormat) {
117 table->GreenSize = 0;
119 table->AlphaSize = sz;
120 table->IntensitySize = 0;
121 table->LuminanceSize = 0;
125 table->GreenSize = 0;
127 table->AlphaSize = 0;
128 table->IntensitySize = 0;
129 table->LuminanceSize = sz;
131 case GL_LUMINANCE_ALPHA:
133 table->GreenSize = 0;
135 table->AlphaSize = sz;
136 table->IntensitySize = 0;
137 table->LuminanceSize = sz;
141 table->GreenSize = 0;
143 table->AlphaSize = 0;
144 table->IntensitySize = sz;
145 table->LuminanceSize = 0;
149 table->GreenSize = sz;
150 table->BlueSize = sz;
151 table->AlphaSize = 0;
152 table->IntensitySize = 0;
153 table->LuminanceSize = 0;
157 table->GreenSize = sz;
158 table->BlueSize = sz;
159 table->AlphaSize = sz;
160 table->IntensitySize = 0;
161 table->LuminanceSize = 0;
164 _mesa_problem(NULL, "unexpected format in set_component_sizes");
171 * Update/replace all or part of a color table. Helper function
172 * used by _mesa_ColorTable() and _mesa_ColorSubTable().
173 * The table->Table buffer should already be allocated.
174 * \param start first entry to update
175 * \param count number of entries to update
176 * \param format format of user-provided table data
177 * \param type datatype of user-provided table data
178 * \param data user-provided table data
179 * \param [rgba]Scale - RGBA scale factors
180 * \param [rgba]Bias - RGBA bias factors
183 store_colortable_entries(struct gl_context *ctx, struct gl_color_table *table,
184 GLsizei start, GLsizei count,
185 GLenum format, GLenum type, const GLvoid *data,
186 GLfloat rScale, GLfloat rBias,
187 GLfloat gScale, GLfloat gBias,
188 GLfloat bScale, GLfloat bBias,
189 GLfloat aScale, GLfloat aBias)
191 data = _mesa_map_validate_pbo_source(ctx,
192 1, &ctx->Unpack, count, 1, 1,
194 "glColor[Sub]Table");
199 /* convert user-provided data to GLfloat values */
200 GLfloat tempTab[MAX_COLOR_TABLE_SIZE * 4];
204 _mesa_unpack_color_span_float(ctx,
205 count, /* number of pixels */
206 table->_BaseFormat, /* dest format */
207 tempTab, /* dest address */
208 format, type, /* src format/type */
211 IMAGE_CLAMP_BIT); /* transfer ops */
213 /* the destination */
214 tableF = table->TableF;
216 /* Apply scale & bias & clamp now */
217 switch (table->_BaseFormat) {
219 for (i = 0; i < count; i++) {
220 GLuint j = start + i;
221 tableF[j] = CLAMP(tempTab[i] * rScale + rBias, 0.0F, 1.0F);
225 for (i = 0; i < count; i++) {
226 GLuint j = start + i;
227 tableF[j] = CLAMP(tempTab[i] * rScale + rBias, 0.0F, 1.0F);
231 for (i = 0; i < count; i++) {
232 GLuint j = start + i;
233 tableF[j] = CLAMP(tempTab[i] * aScale + aBias, 0.0F, 1.0F);
236 case GL_LUMINANCE_ALPHA:
237 for (i = 0; i < count; i++) {
238 GLuint j = start + i;
239 tableF[j*2+0] = CLAMP(tempTab[i*2+0] * rScale + rBias, 0.0F, 1.0F);
240 tableF[j*2+1] = CLAMP(tempTab[i*2+1] * aScale + aBias, 0.0F, 1.0F);
244 for (i = 0; i < count; i++) {
245 GLuint j = start + i;
246 tableF[j*3+0] = CLAMP(tempTab[i*3+0] * rScale + rBias, 0.0F, 1.0F);
247 tableF[j*3+1] = CLAMP(tempTab[i*3+1] * gScale + gBias, 0.0F, 1.0F);
248 tableF[j*3+2] = CLAMP(tempTab[i*3+2] * bScale + bBias, 0.0F, 1.0F);
252 for (i = 0; i < count; i++) {
253 GLuint j = start + i;
254 tableF[j*4+0] = CLAMP(tempTab[i*4+0] * rScale + rBias, 0.0F, 1.0F);
255 tableF[j*4+1] = CLAMP(tempTab[i*4+1] * gScale + gBias, 0.0F, 1.0F);
256 tableF[j*4+2] = CLAMP(tempTab[i*4+2] * bScale + bBias, 0.0F, 1.0F);
257 tableF[j*4+3] = CLAMP(tempTab[i*4+3] * aScale + aBias, 0.0F, 1.0F);
261 _mesa_problem(ctx, "Bad format in store_colortable_entries");
266 /* update the ubyte table */
268 const GLint comps = _mesa_components_in_format(table->_BaseFormat);
269 const GLfloat *tableF = table->TableF + start * comps;
270 GLubyte *tableUB = table->TableUB + start * comps;
272 for (i = 0; i < count * comps; i++) {
273 CLAMPED_FLOAT_TO_UBYTE(tableUB[i], tableF[i]);
277 _mesa_unmap_pbo_source(ctx, &ctx->Unpack);
283 _mesa_ColorTable( GLenum target, GLenum internalFormat,
284 GLsizei width, GLenum format, GLenum type,
287 static const GLfloat one[4] = { 1.0, 1.0, 1.0, 1.0 };
288 static const GLfloat zero[4] = { 0.0, 0.0, 0.0, 0.0 };
289 GET_CURRENT_CONTEXT(ctx);
290 struct gl_texture_unit *texUnit = _mesa_get_current_tex_unit(ctx);
291 struct gl_texture_object *texObj = NULL;
292 struct gl_color_table *table = NULL;
293 GLboolean proxy = GL_FALSE;
295 const GLfloat *scale = one, *bias = zero;
298 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx); /* too complex */
301 case GL_SHARED_TEXTURE_PALETTE_EXT:
302 table = &ctx->Texture.Palette;
305 /* try texture targets */
307 struct gl_texture_object *texobj
308 = _mesa_select_tex_object(ctx, texUnit, target);
310 table = &texobj->Palette;
311 proxy = _mesa_is_proxy_texture(target);
314 _mesa_error(ctx, GL_INVALID_ENUM, "glColorTable(target)");
322 if (!_mesa_is_legal_format_and_type(ctx, format, type) ||
323 format == GL_INTENSITY) {
324 _mesa_error(ctx, GL_INVALID_OPERATION, "glColorTable(format or type)");
328 baseFormat = base_colortab_format(internalFormat);
329 if (baseFormat < 0) {
330 _mesa_error(ctx, GL_INVALID_ENUM, "glColorTable(internalFormat)");
334 if (width < 0 || (width != 0 && !_mesa_is_pow_two(width))) {
338 table->InternalFormat = (GLenum) 0;
339 table->_BaseFormat = (GLenum) 0;
342 _mesa_error(ctx, GL_INVALID_VALUE, "glColorTable(width=%d)", width);
347 if (width > (GLsizei) ctx->Const.MaxColorTableSize) {
350 table->InternalFormat = (GLenum) 0;
351 table->_BaseFormat = (GLenum) 0;
354 _mesa_error(ctx, GL_TABLE_TOO_LARGE, "glColorTable(width)");
360 table->InternalFormat = internalFormat;
361 table->_BaseFormat = (GLenum) baseFormat;
363 comps = _mesa_components_in_format(table->_BaseFormat);
364 assert(comps > 0); /* error should have been caught sooner */
367 _mesa_free_colortable_data(table);
370 table->TableF = (GLfloat *) malloc(comps * width * sizeof(GLfloat));
371 table->TableUB = (GLubyte *) malloc(comps * width * sizeof(GLubyte));
373 if (!table->TableF || !table->TableUB) {
374 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glColorTable");
378 store_colortable_entries(ctx, table,
379 0, width, /* start, count */
388 /* do this after the table's Type and Format are set */
389 set_component_sizes(table);
391 if (texObj || target == GL_SHARED_TEXTURE_PALETTE_EXT) {
392 /* texture object palette, texObj==NULL means the shared palette */
393 if (ctx->Driver.UpdateTexturePalette) {
394 (*ctx->Driver.UpdateTexturePalette)( ctx, texObj );
398 ctx->NewState |= _NEW_PIXEL;
404 _mesa_ColorSubTable( GLenum target, GLsizei start,
405 GLsizei count, GLenum format, GLenum type,
408 static const GLfloat one[4] = { 1.0, 1.0, 1.0, 1.0 };
409 static const GLfloat zero[4] = { 0.0, 0.0, 0.0, 0.0 };
410 GET_CURRENT_CONTEXT(ctx);
411 struct gl_texture_unit *texUnit = _mesa_get_current_tex_unit(ctx);
412 struct gl_texture_object *texObj = NULL;
413 struct gl_color_table *table = NULL;
414 const GLfloat *scale = one, *bias = zero;
416 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
419 case GL_SHARED_TEXTURE_PALETTE_EXT:
420 table = &ctx->Texture.Palette;
423 /* try texture targets */
424 texObj = _mesa_select_tex_object(ctx, texUnit, target);
425 if (texObj && !_mesa_is_proxy_texture(target)) {
426 table = &texObj->Palette;
429 _mesa_error(ctx, GL_INVALID_ENUM, "glColorSubTable(target)");
436 if (!_mesa_is_legal_format_and_type(ctx, format, type) ||
437 format == GL_INTENSITY) {
438 _mesa_error(ctx, GL_INVALID_OPERATION, "glColorSubTable(format or type)");
443 _mesa_error(ctx, GL_INVALID_VALUE, "glColorSubTable(count)");
447 /* error should have been caught sooner */
448 assert(_mesa_components_in_format(table->_BaseFormat) > 0);
450 if (start + count > (GLint) table->Size) {
451 _mesa_error(ctx, GL_INVALID_VALUE, "glColorSubTable(count)");
455 if (!table->TableF || !table->TableUB) {
456 /* a GL_OUT_OF_MEMORY error would have been recorded previously */
460 store_colortable_entries(ctx, table, start, count,
467 if (texObj || target == GL_SHARED_TEXTURE_PALETTE_EXT) {
468 /* per-texture object palette */
469 if (ctx->Driver.UpdateTexturePalette) {
470 (*ctx->Driver.UpdateTexturePalette)( ctx, texObj );
474 ctx->NewState |= _NEW_PIXEL;
479 static void GLAPIENTRY
480 _mesa_CopyColorTable(GLenum target, GLenum internalformat,
481 GLint x, GLint y, GLsizei width)
483 GET_CURRENT_CONTEXT(ctx);
484 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
486 if (!ctx->ReadBuffer->_ColorReadBuffer) {
487 return; /* no readbuffer - OK */
490 ctx->Driver.CopyColorTable( ctx, target, internalformat, x, y, width );
495 static void GLAPIENTRY
496 _mesa_CopyColorSubTable(GLenum target, GLsizei start,
497 GLint x, GLint y, GLsizei width)
499 GET_CURRENT_CONTEXT(ctx);
500 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
502 if (!ctx->ReadBuffer->_ColorReadBuffer) {
503 return; /* no readbuffer - OK */
506 ctx->Driver.CopyColorSubTable( ctx, target, start, x, y, width );
511 static void GLAPIENTRY
512 _mesa_GetColorTable( GLenum target, GLenum format,
513 GLenum type, GLvoid *data )
515 GET_CURRENT_CONTEXT(ctx);
516 struct gl_texture_unit *texUnit = _mesa_get_current_tex_unit(ctx);
517 struct gl_color_table *table = NULL;
518 GLfloat rgba[MAX_COLOR_TABLE_SIZE][4];
519 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
522 _mesa_update_state(ctx);
526 case GL_SHARED_TEXTURE_PALETTE_EXT:
527 table = &ctx->Texture.Palette;
530 /* try texture targets */
532 struct gl_texture_object *texobj
533 = _mesa_select_tex_object(ctx, texUnit, target);
534 if (texobj && !_mesa_is_proxy_texture(target)) {
535 table = &texobj->Palette;
538 _mesa_error(ctx, GL_INVALID_ENUM, "glGetColorTable(target)");
546 if (table->Size <= 0) {
550 switch (table->_BaseFormat) {
554 for (i = 0; i < table->Size; i++) {
558 rgba[i][ACOMP] = table->TableF[i];
565 for (i = 0; i < table->Size; i++) {
568 rgba[i][BCOMP] = table->TableF[i];
569 rgba[i][ACOMP] = 1.0F;
573 case GL_LUMINANCE_ALPHA:
576 for (i = 0; i < table->Size; i++) {
579 rgba[i][BCOMP] = table->TableF[i*2+0];
580 rgba[i][ACOMP] = table->TableF[i*2+1];
587 for (i = 0; i < table->Size; i++) {
591 rgba[i][ACOMP] = table->TableF[i];
598 for (i = 0; i < table->Size; i++) {
599 rgba[i][RCOMP] = table->TableF[i*3+0];
600 rgba[i][GCOMP] = table->TableF[i*3+1];
601 rgba[i][BCOMP] = table->TableF[i*3+2];
602 rgba[i][ACOMP] = 1.0F;
607 memcpy(rgba, table->TableF, 4 * table->Size * sizeof(GLfloat));
610 _mesa_problem(ctx, "bad table format in glGetColorTable");
614 data = _mesa_map_validate_pbo_dest(ctx,
615 1, &ctx->Pack, table->Size, 1, 1,
621 _mesa_pack_rgba_span_float(ctx, table->Size, rgba,
622 format, type, data, &ctx->Pack, 0x0);
624 _mesa_unmap_pbo_dest(ctx, &ctx->Pack);
629 static void GLAPIENTRY
630 _mesa_ColorTableParameterfv(GLenum target, GLenum pname, const GLfloat *params)
632 /* no extensions use this function */
633 GET_CURRENT_CONTEXT(ctx);
634 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
635 _mesa_error(ctx, GL_INVALID_ENUM, "glColorTableParameterfv(target)");
640 static void GLAPIENTRY
641 _mesa_ColorTableParameteriv(GLenum target, GLenum pname, const GLint *params)
643 /* no extensions use this function */
644 GET_CURRENT_CONTEXT(ctx);
645 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
646 _mesa_error(ctx, GL_INVALID_ENUM, "glColorTableParameteriv(target)");
651 static void GLAPIENTRY
652 _mesa_GetColorTableParameterfv( GLenum target, GLenum pname, GLfloat *params )
654 GET_CURRENT_CONTEXT(ctx);
655 struct gl_texture_unit *texUnit = _mesa_get_current_tex_unit(ctx);
656 struct gl_color_table *table = NULL;
657 ASSERT_OUTSIDE_BEGIN_END(ctx);
660 case GL_SHARED_TEXTURE_PALETTE_EXT:
661 table = &ctx->Texture.Palette;
664 /* try texture targets */
666 struct gl_texture_object *texobj
667 = _mesa_select_tex_object(ctx, texUnit, target);
669 table = &texobj->Palette;
672 _mesa_error(ctx, GL_INVALID_ENUM,
673 "glGetColorTableParameterfv(target)");
682 case GL_COLOR_TABLE_FORMAT:
683 *params = (GLfloat) table->InternalFormat;
685 case GL_COLOR_TABLE_WIDTH:
686 *params = (GLfloat) table->Size;
688 case GL_COLOR_TABLE_RED_SIZE:
689 *params = (GLfloat) table->RedSize;
691 case GL_COLOR_TABLE_GREEN_SIZE:
692 *params = (GLfloat) table->GreenSize;
694 case GL_COLOR_TABLE_BLUE_SIZE:
695 *params = (GLfloat) table->BlueSize;
697 case GL_COLOR_TABLE_ALPHA_SIZE:
698 *params = (GLfloat) table->AlphaSize;
700 case GL_COLOR_TABLE_LUMINANCE_SIZE:
701 *params = (GLfloat) table->LuminanceSize;
703 case GL_COLOR_TABLE_INTENSITY_SIZE:
704 *params = (GLfloat) table->IntensitySize;
707 _mesa_error(ctx, GL_INVALID_ENUM, "glGetColorTableParameterfv(pname)" );
714 static void GLAPIENTRY
715 _mesa_GetColorTableParameteriv( GLenum target, GLenum pname, GLint *params )
717 GET_CURRENT_CONTEXT(ctx);
718 struct gl_texture_unit *texUnit = _mesa_get_current_tex_unit(ctx);
719 struct gl_color_table *table = NULL;
720 ASSERT_OUTSIDE_BEGIN_END(ctx);
723 case GL_SHARED_TEXTURE_PALETTE_EXT:
724 table = &ctx->Texture.Palette;
727 /* Try texture targets */
729 struct gl_texture_object *texobj
730 = _mesa_select_tex_object(ctx, texUnit, target);
732 table = &texobj->Palette;
735 _mesa_error(ctx, GL_INVALID_ENUM,
736 "glGetColorTableParameteriv(target)");
745 case GL_COLOR_TABLE_FORMAT:
746 *params = table->InternalFormat;
748 case GL_COLOR_TABLE_WIDTH:
749 *params = table->Size;
751 case GL_COLOR_TABLE_RED_SIZE:
752 *params = table->RedSize;
754 case GL_COLOR_TABLE_GREEN_SIZE:
755 *params = table->GreenSize;
757 case GL_COLOR_TABLE_BLUE_SIZE:
758 *params = table->BlueSize;
760 case GL_COLOR_TABLE_ALPHA_SIZE:
761 *params = table->AlphaSize;
763 case GL_COLOR_TABLE_LUMINANCE_SIZE:
764 *params = table->LuminanceSize;
766 case GL_COLOR_TABLE_INTENSITY_SIZE:
767 *params = table->IntensitySize;
770 _mesa_error(ctx, GL_INVALID_ENUM, "glGetColorTableParameteriv(pname)" );
777 _mesa_init_colortable_dispatch(struct _glapi_table *disp)
779 SET_ColorSubTable(disp, _mesa_ColorSubTable);
780 SET_ColorTable(disp, _mesa_ColorTable);
781 SET_ColorTableParameterfv(disp, _mesa_ColorTableParameterfv);
782 SET_ColorTableParameteriv(disp, _mesa_ColorTableParameteriv);
783 SET_CopyColorSubTable(disp, _mesa_CopyColorSubTable);
784 SET_CopyColorTable(disp, _mesa_CopyColorTable);
785 SET_GetColorTable(disp, _mesa_GetColorTable);
786 SET_GetColorTableParameterfv(disp, _mesa_GetColorTableParameterfv);
787 SET_GetColorTableParameteriv(disp, _mesa_GetColorTableParameteriv);
791 #endif /* FEATURE_colortable */
794 /**********************************************************************/
795 /***** Initialization *****/
796 /**********************************************************************/
800 _mesa_init_colortable( struct gl_color_table *p )
805 p->InternalFormat = GL_RGBA;
811 _mesa_free_colortable_data( struct gl_color_table *p )