mesa: convert macros to inline functions
[mesa.git] / src / mesa / main / colortab.c
1 /*
2 * Mesa 3-D graphics library
3 * Version: 7.1
4 *
5 * Copyright (C) 1999-2007 Brian Paul All Rights Reserved.
6 *
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:
13 *
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
16 *
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.
23 */
24
25
26 #include "glheader.h"
27 #include "bufferobj.h"
28 #include "colortab.h"
29 #include "context.h"
30 #include "image.h"
31 #include "macros.h"
32 #include "mfeatures.h"
33 #include "mtypes.h"
34 #include "pack.h"
35 #include "state.h"
36 #include "teximage.h"
37 #include "texstate.h"
38 #include "main/dispatch.h"
39
40
41 #if FEATURE_colortable
42
43
44 /**
45 * Given an internalFormat token passed to glColorTable,
46 * return the corresponding base format.
47 * Return -1 if invalid token.
48 */
49 static GLint
50 base_colortab_format( GLenum format )
51 {
52 switch (format) {
53 case GL_ALPHA:
54 case GL_ALPHA4:
55 case GL_ALPHA8:
56 case GL_ALPHA12:
57 case GL_ALPHA16:
58 return GL_ALPHA;
59 case GL_LUMINANCE:
60 case GL_LUMINANCE4:
61 case GL_LUMINANCE8:
62 case GL_LUMINANCE12:
63 case GL_LUMINANCE16:
64 return GL_LUMINANCE;
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;
73 case GL_INTENSITY:
74 case GL_INTENSITY4:
75 case GL_INTENSITY8:
76 case GL_INTENSITY12:
77 case GL_INTENSITY16:
78 return GL_INTENSITY;
79 case GL_RGB:
80 case GL_R3_G3_B2:
81 case GL_RGB4:
82 case GL_RGB5:
83 case GL_RGB8:
84 case GL_RGB10:
85 case GL_RGB12:
86 case GL_RGB16:
87 return GL_RGB;
88 case GL_RGBA:
89 case GL_RGBA2:
90 case GL_RGBA4:
91 case GL_RGB5_A1:
92 case GL_RGBA8:
93 case GL_RGB10_A2:
94 case GL_RGBA12:
95 case GL_RGBA16:
96 return GL_RGBA;
97 default:
98 return -1; /* error */
99 }
100 }
101
102
103
104 /**
105 * Examine table's format and set the component sizes accordingly.
106 */
107 static void
108 set_component_sizes( struct gl_color_table *table )
109 {
110 /* assuming the ubyte table */
111 const GLubyte sz = 8;
112
113 switch (table->_BaseFormat) {
114 case GL_ALPHA:
115 table->RedSize = 0;
116 table->GreenSize = 0;
117 table->BlueSize = 0;
118 table->AlphaSize = sz;
119 table->IntensitySize = 0;
120 table->LuminanceSize = 0;
121 break;
122 case GL_LUMINANCE:
123 table->RedSize = 0;
124 table->GreenSize = 0;
125 table->BlueSize = 0;
126 table->AlphaSize = 0;
127 table->IntensitySize = 0;
128 table->LuminanceSize = sz;
129 break;
130 case GL_LUMINANCE_ALPHA:
131 table->RedSize = 0;
132 table->GreenSize = 0;
133 table->BlueSize = 0;
134 table->AlphaSize = sz;
135 table->IntensitySize = 0;
136 table->LuminanceSize = sz;
137 break;
138 case GL_INTENSITY:
139 table->RedSize = 0;
140 table->GreenSize = 0;
141 table->BlueSize = 0;
142 table->AlphaSize = 0;
143 table->IntensitySize = sz;
144 table->LuminanceSize = 0;
145 break;
146 case GL_RGB:
147 table->RedSize = sz;
148 table->GreenSize = sz;
149 table->BlueSize = sz;
150 table->AlphaSize = 0;
151 table->IntensitySize = 0;
152 table->LuminanceSize = 0;
153 break;
154 case GL_RGBA:
155 table->RedSize = sz;
156 table->GreenSize = sz;
157 table->BlueSize = sz;
158 table->AlphaSize = sz;
159 table->IntensitySize = 0;
160 table->LuminanceSize = 0;
161 break;
162 default:
163 _mesa_problem(NULL, "unexpected format in set_component_sizes");
164 }
165 }
166
167
168
169 /**
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
180 */
181 static void
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)
189 {
190 data = _mesa_map_validate_pbo_source(ctx,
191 1, &ctx->Unpack, count, 1, 1,
192 format, type, data,
193 "glColor[Sub]Table");
194 if (!data)
195 return;
196
197 {
198 /* convert user-provided data to GLfloat values */
199 GLfloat tempTab[MAX_COLOR_TABLE_SIZE * 4];
200 GLfloat *tableF;
201 GLint i;
202
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 */
208 data, /* src data */
209 &ctx->Unpack,
210 IMAGE_CLAMP_BIT); /* transfer ops */
211
212 /* the destination */
213 tableF = table->TableF;
214
215 /* Apply scale & bias & clamp now */
216 switch (table->_BaseFormat) {
217 case GL_INTENSITY:
218 for (i = 0; i < count; i++) {
219 GLuint j = start + i;
220 tableF[j] = CLAMP(tempTab[i] * rScale + rBias, 0.0F, 1.0F);
221 }
222 break;
223 case GL_LUMINANCE:
224 for (i = 0; i < count; i++) {
225 GLuint j = start + i;
226 tableF[j] = CLAMP(tempTab[i] * rScale + rBias, 0.0F, 1.0F);
227 }
228 break;
229 case GL_ALPHA:
230 for (i = 0; i < count; i++) {
231 GLuint j = start + i;
232 tableF[j] = CLAMP(tempTab[i] * aScale + aBias, 0.0F, 1.0F);
233 }
234 break;
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);
240 }
241 break;
242 case GL_RGB:
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);
248 }
249 break;
250 case GL_RGBA:
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);
257 }
258 break;
259 default:
260 _mesa_problem(ctx, "Bad format in store_colortable_entries");
261 return;
262 }
263 }
264
265 /* update the ubyte table */
266 {
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;
270 GLint i;
271 for (i = 0; i < count * comps; i++) {
272 CLAMPED_FLOAT_TO_UBYTE(tableUB[i], tableF[i]);
273 }
274 }
275
276 _mesa_unmap_pbo_source(ctx, &ctx->Unpack);
277 }
278
279
280
281 void GLAPIENTRY
282 _mesa_ColorTable( GLenum target, GLenum internalFormat,
283 GLsizei width, GLenum format, GLenum type,
284 const GLvoid *data )
285 {
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;
293 GLint baseFormat;
294 const GLfloat *scale = one, *bias = zero;
295 GLint comps;
296
297 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx); /* too complex */
298
299 switch (target) {
300 case GL_SHARED_TEXTURE_PALETTE_EXT:
301 table = &ctx->Texture.Palette;
302 break;
303 case GL_TEXTURE_COLOR_TABLE_SGI:
304 if (!ctx->Extensions.SGI_texture_color_table) {
305 _mesa_error(ctx, GL_INVALID_ENUM, "glColorTable(target)");
306 return;
307 }
308 table = &(texUnit->ColorTable);
309 scale = ctx->Pixel.TextureColorTableScale;
310 bias = ctx->Pixel.TextureColorTableBias;
311 break;
312 case GL_PROXY_TEXTURE_COLOR_TABLE_SGI:
313 if (!ctx->Extensions.SGI_texture_color_table) {
314 _mesa_error(ctx, GL_INVALID_ENUM, "glColorTable(target)");
315 return;
316 }
317 table = &(texUnit->ProxyColorTable);
318 proxy = GL_TRUE;
319 break;
320 default:
321 /* try texture targets */
322 {
323 struct gl_texture_object *texobj
324 = _mesa_select_tex_object(ctx, texUnit, target);
325 if (texobj) {
326 table = &texobj->Palette;
327 proxy = _mesa_is_proxy_texture(target);
328 }
329 else {
330 _mesa_error(ctx, GL_INVALID_ENUM, "glColorTable(target)");
331 return;
332 }
333 }
334 }
335
336 assert(table);
337
338 if (!_mesa_is_legal_format_and_type(ctx, format, type) ||
339 format == GL_INTENSITY) {
340 _mesa_error(ctx, GL_INVALID_OPERATION, "glColorTable(format or type)");
341 return;
342 }
343
344 baseFormat = base_colortab_format(internalFormat);
345 if (baseFormat < 0) {
346 _mesa_error(ctx, GL_INVALID_ENUM, "glColorTable(internalFormat)");
347 return;
348 }
349
350 if (width < 0 || (width != 0 && !_mesa_is_pow_two(width))) {
351 /* error */
352 if (proxy) {
353 table->Size = 0;
354 table->InternalFormat = (GLenum) 0;
355 table->_BaseFormat = (GLenum) 0;
356 }
357 else {
358 _mesa_error(ctx, GL_INVALID_VALUE, "glColorTable(width=%d)", width);
359 }
360 return;
361 }
362
363 if (width > (GLsizei) ctx->Const.MaxColorTableSize) {
364 if (proxy) {
365 table->Size = 0;
366 table->InternalFormat = (GLenum) 0;
367 table->_BaseFormat = (GLenum) 0;
368 }
369 else {
370 _mesa_error(ctx, GL_TABLE_TOO_LARGE, "glColorTable(width)");
371 }
372 return;
373 }
374
375 table->Size = width;
376 table->InternalFormat = internalFormat;
377 table->_BaseFormat = (GLenum) baseFormat;
378
379 comps = _mesa_components_in_format(table->_BaseFormat);
380 assert(comps > 0); /* error should have been caught sooner */
381
382 if (!proxy) {
383 _mesa_free_colortable_data(table);
384
385 if (width > 0) {
386 table->TableF = (GLfloat *) malloc(comps * width * sizeof(GLfloat));
387 table->TableUB = (GLubyte *) malloc(comps * width * sizeof(GLubyte));
388
389 if (!table->TableF || !table->TableUB) {
390 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glColorTable");
391 return;
392 }
393
394 store_colortable_entries(ctx, table,
395 0, width, /* start, count */
396 format, type, data,
397 scale[0], bias[0],
398 scale[1], bias[1],
399 scale[2], bias[2],
400 scale[3], bias[3]);
401 }
402 } /* proxy */
403
404 /* do this after the table's Type and Format are set */
405 set_component_sizes(table);
406
407 if (texObj || target == GL_SHARED_TEXTURE_PALETTE_EXT) {
408 /* texture object palette, texObj==NULL means the shared palette */
409 if (ctx->Driver.UpdateTexturePalette) {
410 (*ctx->Driver.UpdateTexturePalette)( ctx, texObj );
411 }
412 }
413
414 ctx->NewState |= _NEW_PIXEL;
415 }
416
417
418
419 void GLAPIENTRY
420 _mesa_ColorSubTable( GLenum target, GLsizei start,
421 GLsizei count, GLenum format, GLenum type,
422 const GLvoid *data )
423 {
424 static const GLfloat one[4] = { 1.0, 1.0, 1.0, 1.0 };
425 static const GLfloat zero[4] = { 0.0, 0.0, 0.0, 0.0 };
426 GET_CURRENT_CONTEXT(ctx);
427 struct gl_texture_unit *texUnit = _mesa_get_current_tex_unit(ctx);
428 struct gl_texture_object *texObj = NULL;
429 struct gl_color_table *table = NULL;
430 const GLfloat *scale = one, *bias = zero;
431
432 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
433
434 switch (target) {
435 case GL_SHARED_TEXTURE_PALETTE_EXT:
436 table = &ctx->Texture.Palette;
437 break;
438 case GL_TEXTURE_COLOR_TABLE_SGI:
439 if (!ctx->Extensions.SGI_texture_color_table) {
440 _mesa_error(ctx, GL_INVALID_ENUM, "glColorSubTable(target)");
441 return;
442 }
443 table = &(texUnit->ColorTable);
444 scale = ctx->Pixel.TextureColorTableScale;
445 bias = ctx->Pixel.TextureColorTableBias;
446 break;
447 default:
448 /* try texture targets */
449 texObj = _mesa_select_tex_object(ctx, texUnit, target);
450 if (texObj && !_mesa_is_proxy_texture(target)) {
451 table = &texObj->Palette;
452 }
453 else {
454 _mesa_error(ctx, GL_INVALID_ENUM, "glColorSubTable(target)");
455 return;
456 }
457 }
458
459 assert(table);
460
461 if (!_mesa_is_legal_format_and_type(ctx, format, type) ||
462 format == GL_INTENSITY) {
463 _mesa_error(ctx, GL_INVALID_OPERATION, "glColorSubTable(format or type)");
464 return;
465 }
466
467 if (count < 1) {
468 _mesa_error(ctx, GL_INVALID_VALUE, "glColorSubTable(count)");
469 return;
470 }
471
472 /* error should have been caught sooner */
473 assert(_mesa_components_in_format(table->_BaseFormat) > 0);
474
475 if (start + count > (GLint) table->Size) {
476 _mesa_error(ctx, GL_INVALID_VALUE, "glColorSubTable(count)");
477 return;
478 }
479
480 if (!table->TableF || !table->TableUB) {
481 /* a GL_OUT_OF_MEMORY error would have been recorded previously */
482 return;
483 }
484
485 store_colortable_entries(ctx, table, start, count,
486 format, type, data,
487 scale[0], bias[0],
488 scale[1], bias[1],
489 scale[2], bias[2],
490 scale[3], bias[3]);
491
492 if (texObj || target == GL_SHARED_TEXTURE_PALETTE_EXT) {
493 /* per-texture object palette */
494 if (ctx->Driver.UpdateTexturePalette) {
495 (*ctx->Driver.UpdateTexturePalette)( ctx, texObj );
496 }
497 }
498
499 ctx->NewState |= _NEW_PIXEL;
500 }
501
502
503
504 static void GLAPIENTRY
505 _mesa_CopyColorTable(GLenum target, GLenum internalformat,
506 GLint x, GLint y, GLsizei width)
507 {
508 GET_CURRENT_CONTEXT(ctx);
509 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
510
511 if (!ctx->ReadBuffer->_ColorReadBuffer) {
512 return; /* no readbuffer - OK */
513 }
514
515 ctx->Driver.CopyColorTable( ctx, target, internalformat, x, y, width );
516 }
517
518
519
520 static void GLAPIENTRY
521 _mesa_CopyColorSubTable(GLenum target, GLsizei start,
522 GLint x, GLint y, GLsizei width)
523 {
524 GET_CURRENT_CONTEXT(ctx);
525 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
526
527 if (!ctx->ReadBuffer->_ColorReadBuffer) {
528 return; /* no readbuffer - OK */
529 }
530
531 ctx->Driver.CopyColorSubTable( ctx, target, start, x, y, width );
532 }
533
534
535
536 static void GLAPIENTRY
537 _mesa_GetColorTable( GLenum target, GLenum format,
538 GLenum type, GLvoid *data )
539 {
540 GET_CURRENT_CONTEXT(ctx);
541 struct gl_texture_unit *texUnit = _mesa_get_current_tex_unit(ctx);
542 struct gl_color_table *table = NULL;
543 GLfloat rgba[MAX_COLOR_TABLE_SIZE][4];
544 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
545
546 if (ctx->NewState) {
547 _mesa_update_state(ctx);
548 }
549
550 switch (target) {
551 case GL_SHARED_TEXTURE_PALETTE_EXT:
552 table = &ctx->Texture.Palette;
553 break;
554 case GL_TEXTURE_COLOR_TABLE_SGI:
555 if (!ctx->Extensions.SGI_texture_color_table) {
556 _mesa_error(ctx, GL_INVALID_ENUM, "glGetColorTable(target)");
557 return;
558 }
559 table = &(texUnit->ColorTable);
560 break;
561 default:
562 /* try texture targets */
563 {
564 struct gl_texture_object *texobj
565 = _mesa_select_tex_object(ctx, texUnit, target);
566 if (texobj && !_mesa_is_proxy_texture(target)) {
567 table = &texobj->Palette;
568 }
569 else {
570 _mesa_error(ctx, GL_INVALID_ENUM, "glGetColorTable(target)");
571 return;
572 }
573 }
574 }
575
576 ASSERT(table);
577
578 if (table->Size <= 0) {
579 return;
580 }
581
582 switch (table->_BaseFormat) {
583 case GL_ALPHA:
584 {
585 GLuint i;
586 for (i = 0; i < table->Size; i++) {
587 rgba[i][RCOMP] = 0;
588 rgba[i][GCOMP] = 0;
589 rgba[i][BCOMP] = 0;
590 rgba[i][ACOMP] = table->TableF[i];
591 }
592 }
593 break;
594 case GL_LUMINANCE:
595 {
596 GLuint i;
597 for (i = 0; i < table->Size; i++) {
598 rgba[i][RCOMP] =
599 rgba[i][GCOMP] =
600 rgba[i][BCOMP] = table->TableF[i];
601 rgba[i][ACOMP] = 1.0F;
602 }
603 }
604 break;
605 case GL_LUMINANCE_ALPHA:
606 {
607 GLuint i;
608 for (i = 0; i < table->Size; i++) {
609 rgba[i][RCOMP] =
610 rgba[i][GCOMP] =
611 rgba[i][BCOMP] = table->TableF[i*2+0];
612 rgba[i][ACOMP] = table->TableF[i*2+1];
613 }
614 }
615 break;
616 case GL_INTENSITY:
617 {
618 GLuint i;
619 for (i = 0; i < table->Size; i++) {
620 rgba[i][RCOMP] =
621 rgba[i][GCOMP] =
622 rgba[i][BCOMP] =
623 rgba[i][ACOMP] = table->TableF[i];
624 }
625 }
626 break;
627 case GL_RGB:
628 {
629 GLuint i;
630 for (i = 0; i < table->Size; i++) {
631 rgba[i][RCOMP] = table->TableF[i*3+0];
632 rgba[i][GCOMP] = table->TableF[i*3+1];
633 rgba[i][BCOMP] = table->TableF[i*3+2];
634 rgba[i][ACOMP] = 1.0F;
635 }
636 }
637 break;
638 case GL_RGBA:
639 memcpy(rgba, table->TableF, 4 * table->Size * sizeof(GLfloat));
640 break;
641 default:
642 _mesa_problem(ctx, "bad table format in glGetColorTable");
643 return;
644 }
645
646 data = _mesa_map_validate_pbo_dest(ctx,
647 1, &ctx->Pack, table->Size, 1, 1,
648 format, type, data,
649 "glGetColorTable");
650 if (!data)
651 return;
652
653 _mesa_pack_rgba_span_float(ctx, table->Size, rgba,
654 format, type, data, &ctx->Pack, 0x0);
655
656 _mesa_unmap_pbo_dest(ctx, &ctx->Pack);
657 }
658
659
660
661 static void GLAPIENTRY
662 _mesa_ColorTableParameterfv(GLenum target, GLenum pname, const GLfloat *params)
663 {
664 GLfloat *scale, *bias;
665 GET_CURRENT_CONTEXT(ctx);
666 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
667
668 switch (target) {
669 case GL_TEXTURE_COLOR_TABLE_SGI:
670 scale = ctx->Pixel.TextureColorTableScale;
671 bias = ctx->Pixel.TextureColorTableBias;
672 break;
673 default:
674 _mesa_error(ctx, GL_INVALID_ENUM, "glColorTableParameter(target)");
675 return;
676 }
677
678 if (pname == GL_COLOR_TABLE_SCALE_SGI) {
679 COPY_4V(scale, params);
680 }
681 else if (pname == GL_COLOR_TABLE_BIAS_SGI) {
682 COPY_4V(bias, params);
683 }
684 else {
685 _mesa_error(ctx, GL_INVALID_ENUM, "glColorTableParameterfv(pname)");
686 return;
687 }
688
689 ctx->NewState |= _NEW_PIXEL;
690 }
691
692
693
694 static void GLAPIENTRY
695 _mesa_ColorTableParameteriv(GLenum target, GLenum pname, const GLint *params)
696 {
697 GLfloat fparams[4];
698 if (pname == GL_TEXTURE_COLOR_TABLE_SGI) {
699 /* four values */
700 fparams[0] = (GLfloat) params[0];
701 fparams[1] = (GLfloat) params[1];
702 fparams[2] = (GLfloat) params[2];
703 fparams[3] = (GLfloat) params[3];
704 }
705 else {
706 /* one values */
707 fparams[0] = (GLfloat) params[0];
708 }
709 _mesa_ColorTableParameterfv(target, pname, fparams);
710 }
711
712
713
714 static void GLAPIENTRY
715 _mesa_GetColorTableParameterfv( GLenum target, GLenum pname, GLfloat *params )
716 {
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);
721
722 switch (target) {
723 case GL_SHARED_TEXTURE_PALETTE_EXT:
724 table = &ctx->Texture.Palette;
725 break;
726 case GL_TEXTURE_COLOR_TABLE_SGI:
727 if (!ctx->Extensions.SGI_texture_color_table) {
728 _mesa_error(ctx, GL_INVALID_ENUM, "glGetColorTableParameter(target)");
729 return;
730 }
731 table = &(texUnit->ColorTable);
732 if (pname == GL_COLOR_TABLE_SCALE_SGI) {
733 COPY_4V(params, ctx->Pixel.TextureColorTableScale);
734 return;
735 }
736 else if (pname == GL_COLOR_TABLE_BIAS_SGI) {
737 COPY_4V(params, ctx->Pixel.TextureColorTableBias);
738 return;
739 }
740 break;
741 case GL_PROXY_TEXTURE_COLOR_TABLE_SGI:
742 if (!ctx->Extensions.SGI_texture_color_table) {
743 _mesa_error(ctx, GL_INVALID_ENUM, "glGetColorTableParameter(target)");
744 return;
745 }
746 table = &(texUnit->ProxyColorTable);
747 break;
748 default:
749 /* try texture targets */
750 {
751 struct gl_texture_object *texobj
752 = _mesa_select_tex_object(ctx, texUnit, target);
753 if (texobj) {
754 table = &texobj->Palette;
755 }
756 else {
757 _mesa_error(ctx, GL_INVALID_ENUM,
758 "glGetColorTableParameterfv(target)");
759 return;
760 }
761 }
762 }
763
764 assert(table);
765
766 switch (pname) {
767 case GL_COLOR_TABLE_FORMAT:
768 *params = (GLfloat) table->InternalFormat;
769 break;
770 case GL_COLOR_TABLE_WIDTH:
771 *params = (GLfloat) table->Size;
772 break;
773 case GL_COLOR_TABLE_RED_SIZE:
774 *params = (GLfloat) table->RedSize;
775 break;
776 case GL_COLOR_TABLE_GREEN_SIZE:
777 *params = (GLfloat) table->GreenSize;
778 break;
779 case GL_COLOR_TABLE_BLUE_SIZE:
780 *params = (GLfloat) table->BlueSize;
781 break;
782 case GL_COLOR_TABLE_ALPHA_SIZE:
783 *params = (GLfloat) table->AlphaSize;
784 break;
785 case GL_COLOR_TABLE_LUMINANCE_SIZE:
786 *params = (GLfloat) table->LuminanceSize;
787 break;
788 case GL_COLOR_TABLE_INTENSITY_SIZE:
789 *params = (GLfloat) table->IntensitySize;
790 break;
791 default:
792 _mesa_error(ctx, GL_INVALID_ENUM, "glGetColorTableParameterfv(pname)" );
793 return;
794 }
795 }
796
797
798
799 static void GLAPIENTRY
800 _mesa_GetColorTableParameteriv( GLenum target, GLenum pname, GLint *params )
801 {
802 GET_CURRENT_CONTEXT(ctx);
803 struct gl_texture_unit *texUnit = _mesa_get_current_tex_unit(ctx);
804 struct gl_color_table *table = NULL;
805 ASSERT_OUTSIDE_BEGIN_END(ctx);
806
807 switch (target) {
808 case GL_SHARED_TEXTURE_PALETTE_EXT:
809 table = &ctx->Texture.Palette;
810 break;
811 case GL_TEXTURE_COLOR_TABLE_SGI:
812 if (!ctx->Extensions.SGI_texture_color_table) {
813 _mesa_error(ctx, GL_INVALID_ENUM, "glGetColorTableParameter(target)");
814 return;
815 }
816 table = &(texUnit->ColorTable);
817 if (pname == GL_COLOR_TABLE_SCALE_SGI) {
818 params[0] = (GLint) ctx->Pixel.TextureColorTableScale[0];
819 params[1] = (GLint) ctx->Pixel.TextureColorTableScale[1];
820 params[2] = (GLint) ctx->Pixel.TextureColorTableScale[2];
821 params[3] = (GLint) ctx->Pixel.TextureColorTableScale[3];
822 return;
823 }
824 else if (pname == GL_COLOR_TABLE_BIAS_SGI) {
825 params[0] = (GLint) ctx->Pixel.TextureColorTableBias[0];
826 params[1] = (GLint) ctx->Pixel.TextureColorTableBias[1];
827 params[2] = (GLint) ctx->Pixel.TextureColorTableBias[2];
828 params[3] = (GLint) ctx->Pixel.TextureColorTableBias[3];
829 return;
830 }
831 break;
832 case GL_PROXY_TEXTURE_COLOR_TABLE_SGI:
833 if (!ctx->Extensions.SGI_texture_color_table) {
834 _mesa_error(ctx, GL_INVALID_ENUM, "glGetColorTableParameter(target)");
835 return;
836 }
837 table = &(texUnit->ProxyColorTable);
838 break;
839 default:
840 /* Try texture targets */
841 {
842 struct gl_texture_object *texobj
843 = _mesa_select_tex_object(ctx, texUnit, target);
844 if (texobj) {
845 table = &texobj->Palette;
846 }
847 else {
848 _mesa_error(ctx, GL_INVALID_ENUM,
849 "glGetColorTableParameteriv(target)");
850 return;
851 }
852 }
853 }
854
855 assert(table);
856
857 switch (pname) {
858 case GL_COLOR_TABLE_FORMAT:
859 *params = table->InternalFormat;
860 break;
861 case GL_COLOR_TABLE_WIDTH:
862 *params = table->Size;
863 break;
864 case GL_COLOR_TABLE_RED_SIZE:
865 *params = table->RedSize;
866 break;
867 case GL_COLOR_TABLE_GREEN_SIZE:
868 *params = table->GreenSize;
869 break;
870 case GL_COLOR_TABLE_BLUE_SIZE:
871 *params = table->BlueSize;
872 break;
873 case GL_COLOR_TABLE_ALPHA_SIZE:
874 *params = table->AlphaSize;
875 break;
876 case GL_COLOR_TABLE_LUMINANCE_SIZE:
877 *params = table->LuminanceSize;
878 break;
879 case GL_COLOR_TABLE_INTENSITY_SIZE:
880 *params = table->IntensitySize;
881 break;
882 default:
883 _mesa_error(ctx, GL_INVALID_ENUM, "glGetColorTableParameteriv(pname)" );
884 return;
885 }
886 }
887
888
889 void
890 _mesa_init_colortable_dispatch(struct _glapi_table *disp)
891 {
892 SET_ColorSubTable(disp, _mesa_ColorSubTable);
893 SET_ColorTable(disp, _mesa_ColorTable);
894 SET_ColorTableParameterfv(disp, _mesa_ColorTableParameterfv);
895 SET_ColorTableParameteriv(disp, _mesa_ColorTableParameteriv);
896 SET_CopyColorSubTable(disp, _mesa_CopyColorSubTable);
897 SET_CopyColorTable(disp, _mesa_CopyColorTable);
898 SET_GetColorTable(disp, _mesa_GetColorTable);
899 SET_GetColorTableParameterfv(disp, _mesa_GetColorTableParameterfv);
900 SET_GetColorTableParameteriv(disp, _mesa_GetColorTableParameteriv);
901 }
902
903
904 #endif /* FEATURE_colortable */
905
906
907 /**********************************************************************/
908 /***** Initialization *****/
909 /**********************************************************************/
910
911
912 void
913 _mesa_init_colortable( struct gl_color_table *p )
914 {
915 p->TableF = NULL;
916 p->TableUB = NULL;
917 p->Size = 0;
918 p->InternalFormat = GL_RGBA;
919 }
920
921
922
923 void
924 _mesa_free_colortable_data( struct gl_color_table *p )
925 {
926 if (p->TableF) {
927 free(p->TableF);
928 p->TableF = NULL;
929 }
930 if (p->TableUB) {
931 free(p->TableUB);
932 p->TableUB = NULL;
933 }
934 }