Merge commit 'origin/gallium-0.1' into gallium-0.2
[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 "state.h"
33 #include "teximage.h"
34
35
36 /**
37 * Given an internalFormat token passed to glColorTable,
38 * return the corresponding base format.
39 * Return -1 if invalid token.
40 */
41 static GLint
42 base_colortab_format( GLenum format )
43 {
44 switch (format) {
45 case GL_ALPHA:
46 case GL_ALPHA4:
47 case GL_ALPHA8:
48 case GL_ALPHA12:
49 case GL_ALPHA16:
50 return GL_ALPHA;
51 case GL_LUMINANCE:
52 case GL_LUMINANCE4:
53 case GL_LUMINANCE8:
54 case GL_LUMINANCE12:
55 case GL_LUMINANCE16:
56 return GL_LUMINANCE;
57 case GL_LUMINANCE_ALPHA:
58 case GL_LUMINANCE4_ALPHA4:
59 case GL_LUMINANCE6_ALPHA2:
60 case GL_LUMINANCE8_ALPHA8:
61 case GL_LUMINANCE12_ALPHA4:
62 case GL_LUMINANCE12_ALPHA12:
63 case GL_LUMINANCE16_ALPHA16:
64 return GL_LUMINANCE_ALPHA;
65 case GL_INTENSITY:
66 case GL_INTENSITY4:
67 case GL_INTENSITY8:
68 case GL_INTENSITY12:
69 case GL_INTENSITY16:
70 return GL_INTENSITY;
71 case GL_RGB:
72 case GL_R3_G3_B2:
73 case GL_RGB4:
74 case GL_RGB5:
75 case GL_RGB8:
76 case GL_RGB10:
77 case GL_RGB12:
78 case GL_RGB16:
79 return GL_RGB;
80 case GL_RGBA:
81 case GL_RGBA2:
82 case GL_RGBA4:
83 case GL_RGB5_A1:
84 case GL_RGBA8:
85 case GL_RGB10_A2:
86 case GL_RGBA12:
87 case GL_RGBA16:
88 return GL_RGBA;
89 default:
90 return -1; /* error */
91 }
92 }
93
94
95
96 /**
97 * Examine table's format and set the component sizes accordingly.
98 */
99 static void
100 set_component_sizes( struct gl_color_table *table )
101 {
102 /* assuming the ubyte table */
103 const GLubyte sz = 8;
104
105 switch (table->_BaseFormat) {
106 case GL_ALPHA:
107 table->RedSize = 0;
108 table->GreenSize = 0;
109 table->BlueSize = 0;
110 table->AlphaSize = sz;
111 table->IntensitySize = 0;
112 table->LuminanceSize = 0;
113 break;
114 case GL_LUMINANCE:
115 table->RedSize = 0;
116 table->GreenSize = 0;
117 table->BlueSize = 0;
118 table->AlphaSize = 0;
119 table->IntensitySize = 0;
120 table->LuminanceSize = sz;
121 break;
122 case GL_LUMINANCE_ALPHA:
123 table->RedSize = 0;
124 table->GreenSize = 0;
125 table->BlueSize = 0;
126 table->AlphaSize = sz;
127 table->IntensitySize = 0;
128 table->LuminanceSize = sz;
129 break;
130 case GL_INTENSITY:
131 table->RedSize = 0;
132 table->GreenSize = 0;
133 table->BlueSize = 0;
134 table->AlphaSize = 0;
135 table->IntensitySize = sz;
136 table->LuminanceSize = 0;
137 break;
138 case GL_RGB:
139 table->RedSize = sz;
140 table->GreenSize = sz;
141 table->BlueSize = sz;
142 table->AlphaSize = 0;
143 table->IntensitySize = 0;
144 table->LuminanceSize = 0;
145 break;
146 case GL_RGBA:
147 table->RedSize = sz;
148 table->GreenSize = sz;
149 table->BlueSize = sz;
150 table->AlphaSize = sz;
151 table->IntensitySize = 0;
152 table->LuminanceSize = 0;
153 break;
154 default:
155 _mesa_problem(NULL, "unexpected format in set_component_sizes");
156 }
157 }
158
159
160
161 /**
162 * Update/replace all or part of a color table. Helper function
163 * used by _mesa_ColorTable() and _mesa_ColorSubTable().
164 * The table->Table buffer should already be allocated.
165 * \param start first entry to update
166 * \param count number of entries to update
167 * \param format format of user-provided table data
168 * \param type datatype of user-provided table data
169 * \param data user-provided table data
170 * \param [rgba]Scale - RGBA scale factors
171 * \param [rgba]Bias - RGBA bias factors
172 */
173 static void
174 store_colortable_entries(GLcontext *ctx, struct gl_color_table *table,
175 GLsizei start, GLsizei count,
176 GLenum format, GLenum type, const GLvoid *data,
177 GLfloat rScale, GLfloat rBias,
178 GLfloat gScale, GLfloat gBias,
179 GLfloat bScale, GLfloat bBias,
180 GLfloat aScale, GLfloat aBias)
181 {
182 if (ctx->Unpack.BufferObj->Name) {
183 /* Get/unpack the color table data from a PBO */
184 GLubyte *buf;
185 if (!_mesa_validate_pbo_access(1, &ctx->Unpack, count, 1, 1,
186 format, type, data)) {
187 _mesa_error(ctx, GL_INVALID_OPERATION,
188 "glColor[Sub]Table(bad PBO access)");
189 return;
190 }
191 buf = (GLubyte *) ctx->Driver.MapBuffer(ctx, GL_PIXEL_UNPACK_BUFFER_EXT,
192 GL_READ_ONLY_ARB,
193 ctx->Unpack.BufferObj);
194 if (!buf) {
195 _mesa_error(ctx, GL_INVALID_OPERATION,
196 "glColor[Sub]Table(PBO mapped)");
197 return;
198 }
199 data = ADD_POINTERS(buf, data);
200 }
201
202
203 {
204 /* convert user-provided data to GLfloat values */
205 GLfloat tempTab[MAX_COLOR_TABLE_SIZE * 4];
206 GLfloat *tableF;
207 GLint i;
208
209 _mesa_unpack_color_span_float(ctx,
210 count, /* number of pixels */
211 table->_BaseFormat, /* dest format */
212 tempTab, /* dest address */
213 format, type, /* src format/type */
214 data, /* src data */
215 &ctx->Unpack,
216 IMAGE_CLAMP_BIT); /* transfer ops */
217
218 /* the destination */
219 tableF = table->TableF;
220
221 /* Apply scale & bias & clamp now */
222 switch (table->_BaseFormat) {
223 case GL_INTENSITY:
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_LUMINANCE:
230 for (i = 0; i < count; i++) {
231 GLuint j = start + i;
232 tableF[j] = CLAMP(tempTab[i] * rScale + rBias, 0.0F, 1.0F);
233 }
234 break;
235 case GL_ALPHA:
236 for (i = 0; i < count; i++) {
237 GLuint j = start + i;
238 tableF[j] = CLAMP(tempTab[i] * aScale + aBias, 0.0F, 1.0F);
239 }
240 break;
241 case GL_LUMINANCE_ALPHA:
242 for (i = 0; i < count; i++) {
243 GLuint j = start + i;
244 tableF[j*2+0] = CLAMP(tempTab[i*2+0] * rScale + rBias, 0.0F, 1.0F);
245 tableF[j*2+1] = CLAMP(tempTab[i*2+1] * aScale + aBias, 0.0F, 1.0F);
246 }
247 break;
248 case GL_RGB:
249 for (i = 0; i < count; i++) {
250 GLuint j = start + i;
251 tableF[j*3+0] = CLAMP(tempTab[i*3+0] * rScale + rBias, 0.0F, 1.0F);
252 tableF[j*3+1] = CLAMP(tempTab[i*3+1] * gScale + gBias, 0.0F, 1.0F);
253 tableF[j*3+2] = CLAMP(tempTab[i*3+2] * bScale + bBias, 0.0F, 1.0F);
254 }
255 break;
256 case GL_RGBA:
257 for (i = 0; i < count; i++) {
258 GLuint j = start + i;
259 tableF[j*4+0] = CLAMP(tempTab[i*4+0] * rScale + rBias, 0.0F, 1.0F);
260 tableF[j*4+1] = CLAMP(tempTab[i*4+1] * gScale + gBias, 0.0F, 1.0F);
261 tableF[j*4+2] = CLAMP(tempTab[i*4+2] * bScale + bBias, 0.0F, 1.0F);
262 tableF[j*4+3] = CLAMP(tempTab[i*4+3] * aScale + aBias, 0.0F, 1.0F);
263 }
264 break;
265 default:
266 _mesa_problem(ctx, "Bad format in store_colortable_entries");
267 return;
268 }
269 }
270
271 /* update the ubyte table */
272 {
273 const GLint comps = _mesa_components_in_format(table->_BaseFormat);
274 const GLfloat *tableF = table->TableF + start * comps;
275 GLubyte *tableUB = table->TableUB + start * comps;
276 GLint i;
277 for (i = 0; i < count * comps; i++) {
278 CLAMPED_FLOAT_TO_UBYTE(tableUB[i], tableF[i]);
279 }
280 }
281
282 if (ctx->Unpack.BufferObj->Name) {
283 ctx->Driver.UnmapBuffer(ctx, GL_PIXEL_UNPACK_BUFFER_EXT,
284 ctx->Unpack.BufferObj);
285 }
286 }
287
288
289
290 void GLAPIENTRY
291 _mesa_ColorTable( GLenum target, GLenum internalFormat,
292 GLsizei width, GLenum format, GLenum type,
293 const GLvoid *data )
294 {
295 static const GLfloat one[4] = { 1.0, 1.0, 1.0, 1.0 };
296 static const GLfloat zero[4] = { 0.0, 0.0, 0.0, 0.0 };
297 GET_CURRENT_CONTEXT(ctx);
298 struct gl_texture_unit *texUnit = &ctx->Texture.Unit[ctx->Texture.CurrentUnit];
299 struct gl_texture_object *texObj = NULL;
300 struct gl_color_table *table = NULL;
301 GLboolean proxy = GL_FALSE;
302 GLint baseFormat;
303 const GLfloat *scale = one, *bias = zero;
304 GLint comps;
305
306 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx); /* too complex */
307
308 switch (target) {
309 case GL_SHARED_TEXTURE_PALETTE_EXT:
310 table = &ctx->Texture.Palette;
311 break;
312 case GL_COLOR_TABLE:
313 table = &ctx->ColorTable[COLORTABLE_PRECONVOLUTION];
314 scale = ctx->Pixel.ColorTableScale[COLORTABLE_PRECONVOLUTION];
315 bias = ctx->Pixel.ColorTableBias[COLORTABLE_PRECONVOLUTION];
316 break;
317 case GL_PROXY_COLOR_TABLE:
318 table = &ctx->ProxyColorTable[COLORTABLE_PRECONVOLUTION];
319 proxy = GL_TRUE;
320 break;
321 case GL_TEXTURE_COLOR_TABLE_SGI:
322 if (!ctx->Extensions.SGI_texture_color_table) {
323 _mesa_error(ctx, GL_INVALID_ENUM, "glColorTable(target)");
324 return;
325 }
326 table = &(texUnit->ColorTable);
327 scale = ctx->Pixel.TextureColorTableScale;
328 bias = ctx->Pixel.TextureColorTableBias;
329 break;
330 case GL_PROXY_TEXTURE_COLOR_TABLE_SGI:
331 if (!ctx->Extensions.SGI_texture_color_table) {
332 _mesa_error(ctx, GL_INVALID_ENUM, "glColorTable(target)");
333 return;
334 }
335 table = &(texUnit->ProxyColorTable);
336 proxy = GL_TRUE;
337 break;
338 case GL_POST_CONVOLUTION_COLOR_TABLE:
339 table = &ctx->ColorTable[COLORTABLE_POSTCONVOLUTION];
340 scale = ctx->Pixel.ColorTableScale[COLORTABLE_POSTCONVOLUTION];
341 bias = ctx->Pixel.ColorTableBias[COLORTABLE_POSTCONVOLUTION];
342 break;
343 case GL_PROXY_POST_CONVOLUTION_COLOR_TABLE:
344 table = &ctx->ProxyColorTable[COLORTABLE_POSTCONVOLUTION];
345 proxy = GL_TRUE;
346 break;
347 case GL_POST_COLOR_MATRIX_COLOR_TABLE:
348 table = &ctx->ColorTable[COLORTABLE_POSTCOLORMATRIX];
349 scale = ctx->Pixel.ColorTableScale[COLORTABLE_POSTCOLORMATRIX];
350 bias = ctx->Pixel.ColorTableBias[COLORTABLE_POSTCOLORMATRIX];
351 break;
352 case GL_PROXY_POST_COLOR_MATRIX_COLOR_TABLE:
353 table = &ctx->ProxyColorTable[COLORTABLE_POSTCOLORMATRIX];
354 proxy = GL_TRUE;
355 break;
356 default:
357 /* try texture targets */
358 {
359 struct gl_texture_object *texobj
360 = _mesa_select_tex_object(ctx, texUnit, target);
361 if (texobj) {
362 table = &texobj->Palette;
363 proxy = _mesa_is_proxy_texture(target);
364 }
365 else {
366 _mesa_error(ctx, GL_INVALID_ENUM, "glColorTable(target)");
367 return;
368 }
369 }
370 }
371
372 assert(table);
373
374 if (!_mesa_is_legal_format_and_type(ctx, format, type) ||
375 format == GL_INTENSITY) {
376 _mesa_error(ctx, GL_INVALID_OPERATION, "glColorTable(format or type)");
377 return;
378 }
379
380 baseFormat = base_colortab_format(internalFormat);
381 if (baseFormat < 0) {
382 _mesa_error(ctx, GL_INVALID_ENUM, "glColorTable(internalFormat)");
383 return;
384 }
385
386 if (width < 0 || (width != 0 && _mesa_bitcount(width) != 1)) {
387 /* error */
388 if (proxy) {
389 table->Size = 0;
390 table->InternalFormat = (GLenum) 0;
391 table->_BaseFormat = (GLenum) 0;
392 }
393 else {
394 _mesa_error(ctx, GL_INVALID_VALUE, "glColorTable(width=%d)", width);
395 }
396 return;
397 }
398
399 if (width > (GLsizei) ctx->Const.MaxColorTableSize) {
400 if (proxy) {
401 table->Size = 0;
402 table->InternalFormat = (GLenum) 0;
403 table->_BaseFormat = (GLenum) 0;
404 }
405 else {
406 _mesa_error(ctx, GL_TABLE_TOO_LARGE, "glColorTable(width)");
407 }
408 return;
409 }
410
411 table->Size = width;
412 table->InternalFormat = internalFormat;
413 table->_BaseFormat = (GLenum) baseFormat;
414
415 comps = _mesa_components_in_format(table->_BaseFormat);
416 assert(comps > 0); /* error should have been caught sooner */
417
418 if (!proxy) {
419 _mesa_free_colortable_data(table);
420
421 if (width > 0) {
422 table->TableF = (GLfloat *) _mesa_malloc(comps * width * sizeof(GLfloat));
423 table->TableUB = (GLubyte *) _mesa_malloc(comps * width * sizeof(GLubyte));
424
425 if (!table->TableF || !table->TableUB) {
426 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glColorTable");
427 return;
428 }
429
430 store_colortable_entries(ctx, table,
431 0, width, /* start, count */
432 format, type, data,
433 scale[0], bias[0],
434 scale[1], bias[1],
435 scale[2], bias[2],
436 scale[3], bias[3]);
437 }
438 } /* proxy */
439
440 /* do this after the table's Type and Format are set */
441 set_component_sizes(table);
442
443 if (texObj || target == GL_SHARED_TEXTURE_PALETTE_EXT) {
444 /* texture object palette, texObj==NULL means the shared palette */
445 if (ctx->Driver.UpdateTexturePalette) {
446 (*ctx->Driver.UpdateTexturePalette)( ctx, texObj );
447 }
448 }
449
450 ctx->NewState |= _NEW_PIXEL;
451 }
452
453
454
455 void GLAPIENTRY
456 _mesa_ColorSubTable( GLenum target, GLsizei start,
457 GLsizei count, GLenum format, GLenum type,
458 const GLvoid *data )
459 {
460 static const GLfloat one[4] = { 1.0, 1.0, 1.0, 1.0 };
461 static const GLfloat zero[4] = { 0.0, 0.0, 0.0, 0.0 };
462 GET_CURRENT_CONTEXT(ctx);
463 struct gl_texture_unit *texUnit = &ctx->Texture.Unit[ctx->Texture.CurrentUnit];
464 struct gl_texture_object *texObj = NULL;
465 struct gl_color_table *table = NULL;
466 const GLfloat *scale = one, *bias = zero;
467
468 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
469
470 switch (target) {
471 case GL_SHARED_TEXTURE_PALETTE_EXT:
472 table = &ctx->Texture.Palette;
473 break;
474 case GL_COLOR_TABLE:
475 table = &ctx->ColorTable[COLORTABLE_PRECONVOLUTION];
476 scale = ctx->Pixel.ColorTableScale[COLORTABLE_PRECONVOLUTION];
477 bias = ctx->Pixel.ColorTableBias[COLORTABLE_PRECONVOLUTION];
478 break;
479 case GL_TEXTURE_COLOR_TABLE_SGI:
480 if (!ctx->Extensions.SGI_texture_color_table) {
481 _mesa_error(ctx, GL_INVALID_ENUM, "glColorSubTable(target)");
482 return;
483 }
484 table = &(texUnit->ColorTable);
485 scale = ctx->Pixel.TextureColorTableScale;
486 bias = ctx->Pixel.TextureColorTableBias;
487 break;
488 case GL_POST_CONVOLUTION_COLOR_TABLE:
489 table = &ctx->ColorTable[COLORTABLE_POSTCONVOLUTION];
490 scale = ctx->Pixel.ColorTableScale[COLORTABLE_POSTCONVOLUTION];
491 bias = ctx->Pixel.ColorTableBias[COLORTABLE_POSTCONVOLUTION];
492 break;
493 case GL_POST_COLOR_MATRIX_COLOR_TABLE:
494 table = &ctx->ColorTable[COLORTABLE_POSTCOLORMATRIX];
495 scale = ctx->Pixel.ColorTableScale[COLORTABLE_POSTCOLORMATRIX];
496 bias = ctx->Pixel.ColorTableBias[COLORTABLE_POSTCOLORMATRIX];
497 break;
498 default:
499 /* try texture targets */
500 texObj = _mesa_select_tex_object(ctx, texUnit, target);
501 if (texObj && !_mesa_is_proxy_texture(target)) {
502 table = &texObj->Palette;
503 }
504 else {
505 _mesa_error(ctx, GL_INVALID_ENUM, "glColorSubTable(target)");
506 return;
507 }
508 }
509
510 assert(table);
511
512 if (!_mesa_is_legal_format_and_type(ctx, format, type) ||
513 format == GL_INTENSITY) {
514 _mesa_error(ctx, GL_INVALID_OPERATION, "glColorSubTable(format or type)");
515 return;
516 }
517
518 if (count < 1) {
519 _mesa_error(ctx, GL_INVALID_VALUE, "glColorSubTable(count)");
520 return;
521 }
522
523 /* error should have been caught sooner */
524 assert(_mesa_components_in_format(table->_BaseFormat) > 0);
525
526 if (start + count > (GLint) table->Size) {
527 _mesa_error(ctx, GL_INVALID_VALUE, "glColorSubTable(count)");
528 return;
529 }
530
531 if (!table->TableF || !table->TableUB) {
532 /* a GL_OUT_OF_MEMORY error would have been recorded previously */
533 return;
534 }
535
536 store_colortable_entries(ctx, table, start, count,
537 format, type, data,
538 scale[0], bias[0],
539 scale[1], bias[1],
540 scale[2], bias[2],
541 scale[3], bias[3]);
542
543 if (texObj || target == GL_SHARED_TEXTURE_PALETTE_EXT) {
544 /* per-texture object palette */
545 if (ctx->Driver.UpdateTexturePalette) {
546 (*ctx->Driver.UpdateTexturePalette)( ctx, texObj );
547 }
548 }
549
550 ctx->NewState |= _NEW_PIXEL;
551 }
552
553
554
555 void GLAPIENTRY
556 _mesa_CopyColorTable(GLenum target, GLenum internalformat,
557 GLint x, GLint y, GLsizei width)
558 {
559 GET_CURRENT_CONTEXT(ctx);
560 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
561
562 /* Select buffer to read from */
563 ctx->Driver.CopyColorTable( ctx, target, internalformat, x, y, width );
564 }
565
566
567
568 void GLAPIENTRY
569 _mesa_CopyColorSubTable(GLenum target, GLsizei start,
570 GLint x, GLint y, GLsizei width)
571 {
572 GET_CURRENT_CONTEXT(ctx);
573 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
574
575 ctx->Driver.CopyColorSubTable( ctx, target, start, x, y, width );
576 }
577
578
579
580 void GLAPIENTRY
581 _mesa_GetColorTable( GLenum target, GLenum format,
582 GLenum type, GLvoid *data )
583 {
584 GET_CURRENT_CONTEXT(ctx);
585 struct gl_texture_unit *texUnit = &ctx->Texture.Unit[ctx->Texture.CurrentUnit];
586 struct gl_color_table *table = NULL;
587 GLfloat rgba[MAX_COLOR_TABLE_SIZE][4];
588 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
589
590 if (ctx->NewState) {
591 _mesa_update_state(ctx);
592 }
593
594 switch (target) {
595 case GL_SHARED_TEXTURE_PALETTE_EXT:
596 table = &ctx->Texture.Palette;
597 break;
598 case GL_COLOR_TABLE:
599 table = &ctx->ColorTable[COLORTABLE_PRECONVOLUTION];
600 break;
601 case GL_TEXTURE_COLOR_TABLE_SGI:
602 if (!ctx->Extensions.SGI_texture_color_table) {
603 _mesa_error(ctx, GL_INVALID_ENUM, "glGetColorTable(target)");
604 return;
605 }
606 table = &(texUnit->ColorTable);
607 break;
608 case GL_POST_CONVOLUTION_COLOR_TABLE:
609 table = &ctx->ColorTable[COLORTABLE_POSTCONVOLUTION];
610 break;
611 case GL_POST_COLOR_MATRIX_COLOR_TABLE:
612 table = &ctx->ColorTable[COLORTABLE_POSTCOLORMATRIX];
613 break;
614 default:
615 /* try texture targets */
616 {
617 struct gl_texture_object *texobj
618 = _mesa_select_tex_object(ctx, texUnit, target);
619 if (texobj && !_mesa_is_proxy_texture(target)) {
620 table = &texobj->Palette;
621 }
622 else {
623 _mesa_error(ctx, GL_INVALID_ENUM, "glGetColorTable(target)");
624 return;
625 }
626 }
627 }
628
629 ASSERT(table);
630
631 if (table->Size <= 0) {
632 return;
633 }
634
635 switch (table->_BaseFormat) {
636 case GL_ALPHA:
637 {
638 GLuint i;
639 for (i = 0; i < table->Size; i++) {
640 rgba[i][RCOMP] = 0;
641 rgba[i][GCOMP] = 0;
642 rgba[i][BCOMP] = 0;
643 rgba[i][ACOMP] = table->TableF[i];
644 }
645 }
646 break;
647 case GL_LUMINANCE:
648 {
649 GLuint i;
650 for (i = 0; i < table->Size; i++) {
651 rgba[i][RCOMP] =
652 rgba[i][GCOMP] =
653 rgba[i][BCOMP] = table->TableF[i];
654 rgba[i][ACOMP] = 1.0F;
655 }
656 }
657 break;
658 case GL_LUMINANCE_ALPHA:
659 {
660 GLuint i;
661 for (i = 0; i < table->Size; i++) {
662 rgba[i][RCOMP] =
663 rgba[i][GCOMP] =
664 rgba[i][BCOMP] = table->TableF[i*2+0];
665 rgba[i][ACOMP] = table->TableF[i*2+1];
666 }
667 }
668 break;
669 case GL_INTENSITY:
670 {
671 GLuint i;
672 for (i = 0; i < table->Size; i++) {
673 rgba[i][RCOMP] =
674 rgba[i][GCOMP] =
675 rgba[i][BCOMP] =
676 rgba[i][ACOMP] = table->TableF[i];
677 }
678 }
679 break;
680 case GL_RGB:
681 {
682 GLuint i;
683 for (i = 0; i < table->Size; i++) {
684 rgba[i][RCOMP] = table->TableF[i*3+0];
685 rgba[i][GCOMP] = table->TableF[i*3+1];
686 rgba[i][BCOMP] = table->TableF[i*3+2];
687 rgba[i][ACOMP] = 1.0F;
688 }
689 }
690 break;
691 case GL_RGBA:
692 _mesa_memcpy(rgba, table->TableF, 4 * table->Size * sizeof(GLfloat));
693 break;
694 default:
695 _mesa_problem(ctx, "bad table format in glGetColorTable");
696 return;
697 }
698
699 if (ctx->Pack.BufferObj->Name) {
700 /* pack color table into PBO */
701 GLubyte *buf;
702 if (!_mesa_validate_pbo_access(1, &ctx->Pack, table->Size, 1, 1,
703 format, type, data)) {
704 _mesa_error(ctx, GL_INVALID_OPERATION,
705 "glGetColorTable(invalid PBO access)");
706 return;
707 }
708 buf = (GLubyte *) ctx->Driver.MapBuffer(ctx, GL_PIXEL_PACK_BUFFER_EXT,
709 GL_WRITE_ONLY_ARB,
710 ctx->Pack.BufferObj);
711 if (!buf) {
712 /* buffer is already mapped - that's an error */
713 _mesa_error(ctx, GL_INVALID_OPERATION,
714 "glGetColorTable(PBO is mapped)");
715 return;
716 }
717 data = ADD_POINTERS(buf, data);
718 }
719
720 _mesa_pack_rgba_span_float(ctx, table->Size, rgba,
721 format, type, data, &ctx->Pack, 0x0);
722
723 if (ctx->Pack.BufferObj->Name) {
724 ctx->Driver.UnmapBuffer(ctx, GL_PIXEL_PACK_BUFFER_EXT,
725 ctx->Pack.BufferObj);
726 }
727 }
728
729
730
731 void GLAPIENTRY
732 _mesa_ColorTableParameterfv(GLenum target, GLenum pname, const GLfloat *params)
733 {
734 GLfloat *scale, *bias;
735 GET_CURRENT_CONTEXT(ctx);
736 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
737
738 switch (target) {
739 case GL_COLOR_TABLE_SGI:
740 scale = ctx->Pixel.ColorTableScale[COLORTABLE_PRECONVOLUTION];
741 bias = ctx->Pixel.ColorTableBias[COLORTABLE_PRECONVOLUTION];
742 break;
743 case GL_TEXTURE_COLOR_TABLE_SGI:
744 scale = ctx->Pixel.TextureColorTableScale;
745 bias = ctx->Pixel.TextureColorTableBias;
746 break;
747 case GL_POST_CONVOLUTION_COLOR_TABLE_SGI:
748 scale = ctx->Pixel.ColorTableScale[COLORTABLE_POSTCONVOLUTION];
749 bias = ctx->Pixel.ColorTableBias[COLORTABLE_POSTCONVOLUTION];
750 break;
751 case GL_POST_COLOR_MATRIX_COLOR_TABLE_SGI:
752 scale = ctx->Pixel.ColorTableScale[COLORTABLE_POSTCOLORMATRIX];
753 bias = ctx->Pixel.ColorTableBias[COLORTABLE_POSTCOLORMATRIX];
754 break;
755 default:
756 _mesa_error(ctx, GL_INVALID_ENUM, "glColorTableParameter(target)");
757 return;
758 }
759
760 if (pname == GL_COLOR_TABLE_SCALE_SGI) {
761 COPY_4V(scale, params);
762 }
763 else if (pname == GL_COLOR_TABLE_BIAS_SGI) {
764 COPY_4V(bias, params);
765 }
766 else {
767 _mesa_error(ctx, GL_INVALID_ENUM, "glColorTableParameterfv(pname)");
768 return;
769 }
770
771 ctx->NewState |= _NEW_PIXEL;
772 }
773
774
775
776 void GLAPIENTRY
777 _mesa_ColorTableParameteriv(GLenum target, GLenum pname, const GLint *params)
778 {
779 GLfloat fparams[4];
780 if (pname == GL_COLOR_TABLE_SGI ||
781 pname == GL_TEXTURE_COLOR_TABLE_SGI ||
782 pname == GL_POST_CONVOLUTION_COLOR_TABLE_SGI ||
783 pname == GL_POST_COLOR_MATRIX_COLOR_TABLE_SGI) {
784 /* four values */
785 fparams[0] = (GLfloat) params[0];
786 fparams[1] = (GLfloat) params[1];
787 fparams[2] = (GLfloat) params[2];
788 fparams[3] = (GLfloat) params[3];
789 }
790 else {
791 /* one values */
792 fparams[0] = (GLfloat) params[0];
793 }
794 _mesa_ColorTableParameterfv(target, pname, fparams);
795 }
796
797
798
799 void GLAPIENTRY
800 _mesa_GetColorTableParameterfv( GLenum target, GLenum pname, GLfloat *params )
801 {
802 GET_CURRENT_CONTEXT(ctx);
803 struct gl_texture_unit *texUnit = &ctx->Texture.Unit[ctx->Texture.CurrentUnit];
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_COLOR_TABLE:
812 table = &ctx->ColorTable[COLORTABLE_PRECONVOLUTION];
813 if (pname == GL_COLOR_TABLE_SCALE_SGI) {
814 COPY_4V(params, ctx->Pixel.ColorTableScale[COLORTABLE_PRECONVOLUTION]);
815 return;
816 }
817 else if (pname == GL_COLOR_TABLE_BIAS_SGI) {
818 COPY_4V(params, ctx->Pixel.ColorTableBias[COLORTABLE_PRECONVOLUTION]);
819 return;
820 }
821 break;
822 case GL_PROXY_COLOR_TABLE:
823 table = &ctx->ProxyColorTable[COLORTABLE_PRECONVOLUTION];
824 break;
825 case GL_TEXTURE_COLOR_TABLE_SGI:
826 if (!ctx->Extensions.SGI_texture_color_table) {
827 _mesa_error(ctx, GL_INVALID_ENUM, "glGetColorTableParameter(target)");
828 return;
829 }
830 table = &(texUnit->ColorTable);
831 if (pname == GL_COLOR_TABLE_SCALE_SGI) {
832 COPY_4V(params, ctx->Pixel.TextureColorTableScale);
833 return;
834 }
835 else if (pname == GL_COLOR_TABLE_BIAS_SGI) {
836 COPY_4V(params, ctx->Pixel.TextureColorTableBias);
837 return;
838 }
839 break;
840 case GL_PROXY_TEXTURE_COLOR_TABLE_SGI:
841 if (!ctx->Extensions.SGI_texture_color_table) {
842 _mesa_error(ctx, GL_INVALID_ENUM, "glGetColorTableParameter(target)");
843 return;
844 }
845 table = &(texUnit->ProxyColorTable);
846 break;
847 case GL_POST_CONVOLUTION_COLOR_TABLE:
848 table = &ctx->ColorTable[COLORTABLE_POSTCONVOLUTION];
849 if (pname == GL_COLOR_TABLE_SCALE_SGI) {
850 COPY_4V(params, ctx->Pixel.ColorTableScale[COLORTABLE_POSTCONVOLUTION]);
851 return;
852 }
853 else if (pname == GL_COLOR_TABLE_BIAS_SGI) {
854 COPY_4V(params, ctx->Pixel.ColorTableBias[COLORTABLE_POSTCONVOLUTION]);
855 return;
856 }
857 break;
858 case GL_PROXY_POST_CONVOLUTION_COLOR_TABLE:
859 table = &ctx->ProxyColorTable[COLORTABLE_POSTCONVOLUTION];
860 break;
861 case GL_POST_COLOR_MATRIX_COLOR_TABLE:
862 table = &ctx->ColorTable[COLORTABLE_POSTCOLORMATRIX];
863 if (pname == GL_COLOR_TABLE_SCALE_SGI) {
864 COPY_4V(params, ctx->Pixel.ColorTableScale[COLORTABLE_POSTCOLORMATRIX]);
865 return;
866 }
867 else if (pname == GL_COLOR_TABLE_BIAS_SGI) {
868 COPY_4V(params, ctx->Pixel.ColorTableBias[COLORTABLE_POSTCOLORMATRIX]);
869 return;
870 }
871 break;
872 case GL_PROXY_POST_COLOR_MATRIX_COLOR_TABLE:
873 table = &ctx->ProxyColorTable[COLORTABLE_POSTCOLORMATRIX];
874 break;
875 default:
876 /* try texture targets */
877 {
878 struct gl_texture_object *texobj
879 = _mesa_select_tex_object(ctx, texUnit, target);
880 if (texobj) {
881 table = &texobj->Palette;
882 }
883 else {
884 _mesa_error(ctx, GL_INVALID_ENUM,
885 "glGetColorTableParameterfv(target)");
886 return;
887 }
888 }
889 }
890
891 assert(table);
892
893 switch (pname) {
894 case GL_COLOR_TABLE_FORMAT:
895 *params = (GLfloat) table->InternalFormat;
896 break;
897 case GL_COLOR_TABLE_WIDTH:
898 *params = (GLfloat) table->Size;
899 break;
900 case GL_COLOR_TABLE_RED_SIZE:
901 *params = (GLfloat) table->RedSize;
902 break;
903 case GL_COLOR_TABLE_GREEN_SIZE:
904 *params = (GLfloat) table->GreenSize;
905 break;
906 case GL_COLOR_TABLE_BLUE_SIZE:
907 *params = (GLfloat) table->BlueSize;
908 break;
909 case GL_COLOR_TABLE_ALPHA_SIZE:
910 *params = (GLfloat) table->AlphaSize;
911 break;
912 case GL_COLOR_TABLE_LUMINANCE_SIZE:
913 *params = (GLfloat) table->LuminanceSize;
914 break;
915 case GL_COLOR_TABLE_INTENSITY_SIZE:
916 *params = (GLfloat) table->IntensitySize;
917 break;
918 default:
919 _mesa_error(ctx, GL_INVALID_ENUM, "glGetColorTableParameterfv(pname)" );
920 return;
921 }
922 }
923
924
925
926 void GLAPIENTRY
927 _mesa_GetColorTableParameteriv( GLenum target, GLenum pname, GLint *params )
928 {
929 GET_CURRENT_CONTEXT(ctx);
930 struct gl_texture_unit *texUnit = &ctx->Texture.Unit[ctx->Texture.CurrentUnit];
931 struct gl_color_table *table = NULL;
932 ASSERT_OUTSIDE_BEGIN_END(ctx);
933
934 switch (target) {
935 case GL_SHARED_TEXTURE_PALETTE_EXT:
936 table = &ctx->Texture.Palette;
937 break;
938 case GL_COLOR_TABLE:
939 table = &ctx->ColorTable[COLORTABLE_PRECONVOLUTION];
940 if (pname == GL_COLOR_TABLE_SCALE_SGI) {
941 GLfloat *scale = ctx->Pixel.ColorTableScale[COLORTABLE_PRECONVOLUTION];
942 params[0] = (GLint) scale[0];
943 params[1] = (GLint) scale[1];
944 params[2] = (GLint) scale[2];
945 params[3] = (GLint) scale[3];
946 return;
947 }
948 else if (pname == GL_COLOR_TABLE_BIAS_SGI) {
949 GLfloat *bias = ctx->Pixel.ColorTableBias[COLORTABLE_PRECONVOLUTION];
950 params[0] = (GLint) bias[0];
951 params[1] = (GLint) bias[1];
952 params[2] = (GLint) bias[2];
953 params[3] = (GLint) bias[3];
954 return;
955 }
956 break;
957 case GL_PROXY_COLOR_TABLE:
958 table = &ctx->ProxyColorTable[COLORTABLE_PRECONVOLUTION];
959 break;
960 case GL_TEXTURE_COLOR_TABLE_SGI:
961 if (!ctx->Extensions.SGI_texture_color_table) {
962 _mesa_error(ctx, GL_INVALID_ENUM, "glGetColorTableParameter(target)");
963 return;
964 }
965 table = &(texUnit->ColorTable);
966 if (pname == GL_COLOR_TABLE_SCALE_SGI) {
967 params[0] = (GLint) ctx->Pixel.TextureColorTableScale[0];
968 params[1] = (GLint) ctx->Pixel.TextureColorTableScale[1];
969 params[2] = (GLint) ctx->Pixel.TextureColorTableScale[2];
970 params[3] = (GLint) ctx->Pixel.TextureColorTableScale[3];
971 return;
972 }
973 else if (pname == GL_COLOR_TABLE_BIAS_SGI) {
974 params[0] = (GLint) ctx->Pixel.TextureColorTableBias[0];
975 params[1] = (GLint) ctx->Pixel.TextureColorTableBias[1];
976 params[2] = (GLint) ctx->Pixel.TextureColorTableBias[2];
977 params[3] = (GLint) ctx->Pixel.TextureColorTableBias[3];
978 return;
979 }
980 break;
981 case GL_PROXY_TEXTURE_COLOR_TABLE_SGI:
982 if (!ctx->Extensions.SGI_texture_color_table) {
983 _mesa_error(ctx, GL_INVALID_ENUM, "glGetColorTableParameter(target)");
984 return;
985 }
986 table = &(texUnit->ProxyColorTable);
987 break;
988 case GL_POST_CONVOLUTION_COLOR_TABLE:
989 table = &ctx->ColorTable[COLORTABLE_POSTCONVOLUTION];
990 if (pname == GL_COLOR_TABLE_SCALE_SGI) {
991 GLfloat *scale = ctx->Pixel.ColorTableScale[COLORTABLE_POSTCONVOLUTION];
992 params[0] = (GLint) scale[0];
993 params[1] = (GLint) scale[1];
994 params[2] = (GLint) scale[2];
995 params[3] = (GLint) scale[3];
996 return;
997 }
998 else if (pname == GL_COLOR_TABLE_BIAS_SGI) {
999 GLfloat *bias = ctx->Pixel.ColorTableBias[COLORTABLE_POSTCONVOLUTION];
1000 params[0] = (GLint) bias[0];
1001 params[1] = (GLint) bias[1];
1002 params[2] = (GLint) bias[2];
1003 params[3] = (GLint) bias[3];
1004 return;
1005 }
1006 break;
1007 case GL_PROXY_POST_CONVOLUTION_COLOR_TABLE:
1008 table = &ctx->ProxyColorTable[COLORTABLE_POSTCONVOLUTION];
1009 break;
1010 case GL_POST_COLOR_MATRIX_COLOR_TABLE:
1011 table = &ctx->ColorTable[COLORTABLE_POSTCOLORMATRIX];
1012 if (pname == GL_COLOR_TABLE_SCALE_SGI) {
1013 GLfloat *scale = ctx->Pixel.ColorTableScale[COLORTABLE_POSTCOLORMATRIX];
1014 params[0] = (GLint) scale[0];
1015 params[0] = (GLint) scale[1];
1016 params[0] = (GLint) scale[2];
1017 params[0] = (GLint) scale[3];
1018 return;
1019 }
1020 else if (pname == GL_COLOR_TABLE_BIAS_SGI) {
1021 GLfloat *bias = ctx->Pixel.ColorTableScale[COLORTABLE_POSTCOLORMATRIX];
1022 params[0] = (GLint) bias[0];
1023 params[1] = (GLint) bias[1];
1024 params[2] = (GLint) bias[2];
1025 params[3] = (GLint) bias[3];
1026 return;
1027 }
1028 break;
1029 case GL_PROXY_POST_COLOR_MATRIX_COLOR_TABLE:
1030 table = &ctx->ProxyColorTable[COLORTABLE_POSTCOLORMATRIX];
1031 break;
1032 default:
1033 /* Try texture targets */
1034 {
1035 struct gl_texture_object *texobj
1036 = _mesa_select_tex_object(ctx, texUnit, target);
1037 if (texobj) {
1038 table = &texobj->Palette;
1039 }
1040 else {
1041 _mesa_error(ctx, GL_INVALID_ENUM,
1042 "glGetColorTableParameteriv(target)");
1043 return;
1044 }
1045 }
1046 }
1047
1048 assert(table);
1049
1050 switch (pname) {
1051 case GL_COLOR_TABLE_FORMAT:
1052 *params = table->InternalFormat;
1053 break;
1054 case GL_COLOR_TABLE_WIDTH:
1055 *params = table->Size;
1056 break;
1057 case GL_COLOR_TABLE_RED_SIZE:
1058 *params = table->RedSize;
1059 break;
1060 case GL_COLOR_TABLE_GREEN_SIZE:
1061 *params = table->GreenSize;
1062 break;
1063 case GL_COLOR_TABLE_BLUE_SIZE:
1064 *params = table->BlueSize;
1065 break;
1066 case GL_COLOR_TABLE_ALPHA_SIZE:
1067 *params = table->AlphaSize;
1068 break;
1069 case GL_COLOR_TABLE_LUMINANCE_SIZE:
1070 *params = table->LuminanceSize;
1071 break;
1072 case GL_COLOR_TABLE_INTENSITY_SIZE:
1073 *params = table->IntensitySize;
1074 break;
1075 default:
1076 _mesa_error(ctx, GL_INVALID_ENUM, "glGetColorTableParameteriv(pname)" );
1077 return;
1078 }
1079 }
1080
1081 /**********************************************************************/
1082 /***** Initialization *****/
1083 /**********************************************************************/
1084
1085
1086 void
1087 _mesa_init_colortable( struct gl_color_table *p )
1088 {
1089 p->TableF = NULL;
1090 p->TableUB = NULL;
1091 p->Size = 0;
1092 p->InternalFormat = GL_RGBA;
1093 }
1094
1095
1096
1097 void
1098 _mesa_free_colortable_data( struct gl_color_table *p )
1099 {
1100 if (p->TableF) {
1101 _mesa_free(p->TableF);
1102 p->TableF = NULL;
1103 }
1104 if (p->TableUB) {
1105 _mesa_free(p->TableUB);
1106 p->TableUB = NULL;
1107 }
1108 }
1109
1110
1111 /*
1112 * Initialize all colortables for a context.
1113 */
1114 void
1115 _mesa_init_colortables( GLcontext * ctx )
1116 {
1117 GLuint i;
1118 for (i = 0; i < COLORTABLE_MAX; i++) {
1119 _mesa_init_colortable(&ctx->ColorTable[i]);
1120 _mesa_init_colortable(&ctx->ProxyColorTable[i]);
1121 }
1122 }
1123
1124
1125 /*
1126 * Free all colortable data for a context
1127 */
1128 void
1129 _mesa_free_colortables_data( GLcontext *ctx )
1130 {
1131 GLuint i;
1132 for (i = 0; i < COLORTABLE_MAX; i++) {
1133 _mesa_free_colortable_data(&ctx->ColorTable[i]);
1134 _mesa_free_colortable_data(&ctx->ProxyColorTable[i]);
1135 }
1136 }