dynamically allocate color table data, uses less memory
[mesa.git] / src / mesa / main / colortab.c
1 /* $Id: colortab.c,v 1.15 2000/04/17 17:57:04 brianp Exp $ */
2
3 /*
4 * Mesa 3-D graphics library
5 * Version: 3.3
6 *
7 * Copyright (C) 1999-2000 Brian Paul All Rights Reserved.
8 *
9 * Permission is hereby granted, free of charge, to any person obtaining a
10 * copy of this software and associated documentation files (the "Software"),
11 * to deal in the Software without restriction, including without limitation
12 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
13 * and/or sell copies of the Software, and to permit persons to whom the
14 * Software is furnished to do so, subject to the following conditions:
15 *
16 * The above copyright notice and this permission notice shall be included
17 * in all copies or substantial portions of the Software.
18 *
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
20 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
22 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
23 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
24 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 */
26
27
28 #ifdef PC_HEADER
29 #include "all.h"
30 #else
31 #include "glheader.h"
32 #include "colortab.h"
33 #include "context.h"
34 #include "image.h"
35 #include "macros.h"
36 #include "mem.h"
37 #include "mmath.h"
38 #include "span.h"
39 #include "teximage.h"
40 #endif
41
42
43
44 void
45 _mesa_init_colortable( struct gl_color_table *p )
46 {
47 p->TableType = GL_UNSIGNED_BYTE;
48 /* allocate a width=1 table by default */
49 p->Table = CALLOC(4 * sizeof(GLubyte));
50 if (p->Table) {
51 GLubyte *t = (GLubyte *) p->Table;
52 t[0] = 255;
53 t[1] = 255;
54 t[2] = 255;
55 t[3] = 255;
56 }
57 p->Size = 1;
58 p->IntFormat = GL_RGBA;
59 p->Format = GL_RGBA;
60 p->RedSize = 8;
61 p->GreenSize = 8;
62 p->BlueSize = 8;
63 p->AlphaSize = 8;
64 p->IntensitySize = 0;
65 p->LuminanceSize = 0;
66 }
67
68
69
70 void
71 _mesa_free_colortable_data( struct gl_color_table *p )
72 {
73 if (p->Table) {
74 FREE(p->Table);
75 p->Table = NULL;
76 }
77 }
78
79
80 /*
81 * Examine table's format and set the component sizes accordingly.
82 */
83 static void
84 set_component_sizes( struct gl_color_table *table )
85 {
86 switch (table->Format) {
87 case GL_ALPHA:
88 table->RedSize = 0;
89 table->GreenSize = 0;
90 table->BlueSize = 0;
91 table->AlphaSize = 8;
92 table->IntensitySize = 0;
93 table->LuminanceSize = 0;
94 break;
95 case GL_LUMINANCE:
96 table->RedSize = 0;
97 table->GreenSize = 0;
98 table->BlueSize = 0;
99 table->AlphaSize = 0;
100 table->IntensitySize = 0;
101 table->LuminanceSize = 8;
102 break;
103 case GL_LUMINANCE_ALPHA:
104 table->RedSize = 0;
105 table->GreenSize = 0;
106 table->BlueSize = 0;
107 table->AlphaSize = 8;
108 table->IntensitySize = 0;
109 table->LuminanceSize = 8;
110 break;
111 case GL_INTENSITY:
112 table->RedSize = 0;
113 table->GreenSize = 0;
114 table->BlueSize = 0;
115 table->AlphaSize = 0;
116 table->IntensitySize = 8;
117 table->LuminanceSize = 0;
118 break;
119 case GL_RGB:
120 table->RedSize = 8;
121 table->GreenSize = 8;
122 table->BlueSize = 8;
123 table->AlphaSize = 0;
124 table->IntensitySize = 0;
125 table->LuminanceSize = 0;
126 break;
127 case GL_RGBA:
128 table->RedSize = 8;
129 table->GreenSize = 8;
130 table->BlueSize = 8;
131 table->AlphaSize = 8;
132 table->IntensitySize = 0;
133 table->LuminanceSize = 0;
134 break;
135 default:
136 gl_problem(NULL, "unexpected format in set_component_sizes");
137 }
138 }
139
140
141
142 void
143 _mesa_ColorTable( GLenum target, GLenum internalFormat,
144 GLsizei width, GLenum format, GLenum type,
145 const GLvoid *data )
146 {
147 GET_CURRENT_CONTEXT(ctx);
148 struct gl_texture_unit *texUnit = &ctx->Texture.Unit[ctx->Texture.CurrentUnit];
149 struct gl_texture_object *texObj = NULL;
150 struct gl_color_table *table = NULL;
151 GLboolean proxy = GL_FALSE;
152 GLint baseFormat;
153 GLfloat rScale = 1.0, gScale = 1.0, bScale = 1.0, aScale = 1.0;
154 GLfloat rBias = 0.0, gBias = 0.0, bBias = 0.0, aBias = 0.0;
155 GLboolean floatTable = GL_FALSE;
156
157 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx, "glColorTable");
158
159 switch (target) {
160 case GL_TEXTURE_1D:
161 texObj = texUnit->CurrentD[1];
162 table = &texObj->Palette;
163 break;
164 case GL_TEXTURE_2D:
165 texObj = texUnit->CurrentD[2];
166 table = &texObj->Palette;
167 break;
168 case GL_TEXTURE_3D:
169 texObj = texUnit->CurrentD[3];
170 table = &texObj->Palette;
171 break;
172 case GL_PROXY_TEXTURE_1D:
173 texObj = ctx->Texture.Proxy1D;
174 table = &texObj->Palette;
175 proxy = GL_TRUE;
176 break;
177 case GL_PROXY_TEXTURE_2D:
178 texObj = ctx->Texture.Proxy2D;
179 table = &texObj->Palette;
180 proxy = GL_TRUE;
181 break;
182 case GL_PROXY_TEXTURE_3D:
183 texObj = ctx->Texture.Proxy3D;
184 table = &texObj->Palette;
185 proxy = GL_TRUE;
186 break;
187 case GL_SHARED_TEXTURE_PALETTE_EXT:
188 table = &ctx->Texture.Palette;
189 break;
190 case GL_COLOR_TABLE:
191 table = &ctx->ColorTable;
192 floatTable = GL_TRUE;
193 rScale = ctx->Pixel.ColorTableScale[0];
194 gScale = ctx->Pixel.ColorTableScale[1];
195 bScale = ctx->Pixel.ColorTableScale[2];
196 aScale = ctx->Pixel.ColorTableScale[3];
197 rBias = ctx->Pixel.ColorTableBias[0];
198 gBias = ctx->Pixel.ColorTableBias[1];
199 bBias = ctx->Pixel.ColorTableBias[2];
200 aBias = ctx->Pixel.ColorTableBias[3];
201 break;
202 case GL_PROXY_COLOR_TABLE:
203 table = &ctx->ProxyColorTable;
204 proxy = GL_TRUE;
205 break;
206 case GL_POST_CONVOLUTION_COLOR_TABLE:
207 table = &ctx->PostConvolutionColorTable;
208 floatTable = GL_TRUE;
209 rScale = ctx->Pixel.PCCTscale[0];
210 gScale = ctx->Pixel.PCCTscale[1];
211 bScale = ctx->Pixel.PCCTscale[2];
212 aScale = ctx->Pixel.PCCTscale[3];
213 rBias = ctx->Pixel.PCCTbias[0];
214 gBias = ctx->Pixel.PCCTbias[1];
215 bBias = ctx->Pixel.PCCTbias[2];
216 aBias = ctx->Pixel.PCCTbias[3];
217 break;
218 case GL_PROXY_POST_CONVOLUTION_COLOR_TABLE:
219 table = &ctx->ProxyPostConvolutionColorTable;
220 proxy = GL_TRUE;
221 break;
222 case GL_POST_COLOR_MATRIX_COLOR_TABLE:
223 table = &ctx->PostColorMatrixColorTable;
224 floatTable = GL_TRUE;
225 rScale = ctx->Pixel.PCMCTscale[0];
226 gScale = ctx->Pixel.PCMCTscale[1];
227 bScale = ctx->Pixel.PCMCTscale[2];
228 aScale = ctx->Pixel.PCMCTscale[3];
229 rBias = ctx->Pixel.PCMCTbias[0];
230 gBias = ctx->Pixel.PCMCTbias[1];
231 bBias = ctx->Pixel.PCMCTbias[2];
232 aBias = ctx->Pixel.PCMCTbias[3];
233 break;
234 case GL_PROXY_POST_COLOR_MATRIX_COLOR_TABLE:
235 table = &ctx->ProxyPostColorMatrixColorTable;
236 proxy = GL_TRUE;
237 break;
238 default:
239 gl_error(ctx, GL_INVALID_ENUM, "glColorTable(target)");
240 return;
241 }
242
243 assert(table);
244
245 if (!_mesa_is_legal_format_and_type(format, type)) {
246 gl_error(ctx, GL_INVALID_ENUM, "glColorTable(format or type)");
247 return;
248 }
249
250 baseFormat = _mesa_base_tex_format(internalFormat);
251 if (baseFormat < 0 || baseFormat == GL_COLOR_INDEX) {
252 gl_error(ctx, GL_INVALID_ENUM, "glColorTable(internalFormat)");
253 return;
254 }
255
256 if (width < 1 || width > ctx->Const.MaxColorTableSize
257 || _mesa_bitcount(width) != 1) {
258 if (width > ctx->Const.MaxColorTableSize)
259 gl_error(ctx, GL_TABLE_TOO_LARGE, "glColorTable(width)");
260 else
261 gl_error(ctx, GL_INVALID_VALUE, "glColorTable(width)");
262 if (proxy) {
263 table->Size = 0;
264 table->IntFormat = (GLenum) 0;
265 table->Format = (GLenum) 0;
266 }
267 return;
268 }
269
270
271 table->Size = width;
272 table->IntFormat = internalFormat;
273 table->Format = (GLenum) baseFormat;
274 set_component_sizes(table);
275
276 if (!proxy) {
277 /* free old table, if any */
278 if (table->Table) {
279 FREE(table->Table);
280 }
281 if (floatTable) {
282 GLubyte tableUB[MAX_COLOR_TABLE_SIZE * 4];
283 GLfloat *tableF;
284 GLuint i;
285
286 _mesa_unpack_ubyte_color_span(ctx, width, table->Format,
287 tableUB, /* dest */
288 format, type, data,
289 &ctx->Unpack, GL_TRUE);
290
291 table->TableType = GL_FLOAT;
292 table->Table = MALLOC(4 * width * sizeof(GLfloat));
293 if (!table->Table) {
294 gl_error(ctx, GL_OUT_OF_MEMORY, "glColorTable");
295 return;
296 }
297
298 /* Apply scale and bias and convert GLubyte values to GLfloats
299 * in [0, 1]. Store results in the tableF[].
300 */
301 rScale /= 255.0;
302 gScale /= 255.0;
303 bScale /= 255.0;
304 aScale /= 255.0;
305 tableF = (GLfloat *) table->Table;
306
307 switch (table->Format) {
308 case GL_INTENSITY:
309 for (i = 0; i < width; i++) {
310 tableF[i] = tableUB[i] * rScale + rBias;
311 }
312 break;
313 case GL_LUMINANCE:
314 for (i = 0; i < width; i++) {
315 tableF[i] = tableUB[i] * rScale + rBias;
316 }
317 break;
318 case GL_ALPHA:
319 for (i = 0; i < width; i++) {
320 tableF[i] = tableUB[i] * aScale + aBias;
321 }
322 break;
323 case GL_LUMINANCE_ALPHA:
324 for (i = 0; i < width; i++) {
325 tableF[i*2+0] = tableUB[i*2+0] * rScale + rBias;
326 tableF[i*2+1] = tableUB[i*2+1] * aScale + aBias;
327 }
328 break;
329 case GL_RGB:
330 for (i = 0; i < width; i++) {
331 tableF[i*3+0] = tableUB[i*3+0] * rScale + rBias;
332 tableF[i*3+1] = tableUB[i*3+1] * gScale + gBias;
333 tableF[i*3+2] = tableUB[i*3+2] * bScale + bBias;
334 }
335 break;
336 case GL_RGBA:
337 for (i = 0; i < width; i++) {
338 tableF[i*4+0] = tableUB[i*4+0] * rScale + rBias;
339 tableF[i*4+1] = tableUB[i*4+1] * gScale + gBias;
340 tableF[i*4+2] = tableUB[i*4+2] * bScale + bBias;
341 tableF[i*4+3] = tableUB[i*4+3] * aScale + aBias;
342 }
343 break;
344 default:
345 gl_problem(ctx, "Bad format in _mesa_ColorTable");
346 return;
347 }
348 }
349 else {
350 /* store GLubyte table */
351 table->TableType = GL_UNSIGNED_BYTE;
352 table->Table = MALLOC(4 * width * sizeof(GLubyte));
353 if (!table->Table) {
354 gl_error(ctx, GL_OUT_OF_MEMORY, "glColorTable");
355 return;
356 }
357 _mesa_unpack_ubyte_color_span(ctx, width, table->Format,
358 table->Table, /* dest */
359 format, type, data,
360 &ctx->Unpack, GL_TRUE);
361 } /* floatTable */
362 } /* proxy */
363
364 if (texObj || target == GL_SHARED_TEXTURE_PALETTE_EXT) {
365 /* texture object palette, texObj==NULL means the shared palette */
366 if (ctx->Driver.UpdateTexturePalette) {
367 (*ctx->Driver.UpdateTexturePalette)( ctx, texObj );
368 }
369 }
370
371 }
372
373
374
375 void
376 _mesa_ColorSubTable( GLenum target, GLsizei start,
377 GLsizei count, GLenum format, GLenum type,
378 const GLvoid *data )
379 {
380 GET_CURRENT_CONTEXT(ctx);
381 struct gl_texture_unit *texUnit = &ctx->Texture.Unit[ctx->Texture.CurrentUnit];
382 struct gl_texture_object *texObj = NULL;
383 struct gl_color_table *table = NULL;
384 GLint comps;
385 GLubyte *dest;
386
387 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx, "glColorSubTable");
388
389 switch (target) {
390 case GL_TEXTURE_1D:
391 texObj = texUnit->CurrentD[1];
392 table = &texObj->Palette;
393 break;
394 case GL_TEXTURE_2D:
395 texObj = texUnit->CurrentD[2];
396 table = &texObj->Palette;
397 break;
398 case GL_TEXTURE_3D:
399 texObj = texUnit->CurrentD[3];
400 table = &texObj->Palette;
401 break;
402 case GL_SHARED_TEXTURE_PALETTE_EXT:
403 table = &ctx->Texture.Palette;
404 break;
405 case GL_COLOR_TABLE:
406 table = &ctx->ColorTable;
407 break;
408 case GL_POST_CONVOLUTION_COLOR_TABLE:
409 table = &ctx->PostConvolutionColorTable;
410 break;
411 case GL_POST_COLOR_MATRIX_COLOR_TABLE:
412 table = &ctx->PostColorMatrixColorTable;
413 break;
414 default:
415 gl_error(ctx, GL_INVALID_ENUM, "glColorSubTable(target)");
416 return;
417 }
418
419 assert(table);
420
421 if (!_mesa_is_legal_format_and_type(format, type)) {
422 gl_error(ctx, GL_INVALID_ENUM, "glColorSubTable(format or type)");
423 return;
424 }
425
426 if (count < 1) {
427 gl_error(ctx, GL_INVALID_VALUE, "glColorSubTable(count)");
428 return;
429 }
430
431 comps = _mesa_components_in_format(table->Format);
432 assert(comps > 0); /* error should have been caught sooner */
433
434 if (start + count > table->Size) {
435 gl_error(ctx, GL_INVALID_VALUE, "glColorSubTable(count)");
436 return;
437 }
438
439 if (!table->Table) {
440 gl_error(ctx, GL_OUT_OF_MEMORY, "glColorSubTable");
441 return;
442 }
443
444 if (table->TableType == GL_UNSIGNED_BYTE) {
445 dest = (GLubyte *) table->Table + start * comps * sizeof(GLubyte);
446 _mesa_unpack_ubyte_color_span(ctx, count, table->Format, dest,
447 format, type, data,
448 &ctx->Unpack, GL_TRUE);
449 }
450 else {
451 ASSERT(table->TableType == GL_FLOAT);
452 /* XXX todo */
453 }
454
455
456 if (texObj || target == GL_SHARED_TEXTURE_PALETTE_EXT) {
457 /* per-texture object palette */
458 if (ctx->Driver.UpdateTexturePalette) {
459 (*ctx->Driver.UpdateTexturePalette)( ctx, texObj );
460 }
461 }
462 }
463
464
465
466 /* XXX not tested */
467 void
468 _mesa_CopyColorTable(GLenum target, GLenum internalformat,
469 GLint x, GLint y, GLsizei width)
470 {
471 GLubyte data[MAX_WIDTH][4];
472 GET_CURRENT_CONTEXT(ctx);
473 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx, "glCopyColorTable");
474
475 /* Select buffer to read from */
476 (*ctx->Driver.SetReadBuffer)( ctx, ctx->ReadBuffer,
477 ctx->Pixel.DriverReadBuffer );
478
479 if (width > MAX_WIDTH)
480 width = MAX_WIDTH;
481
482 /* read the data from framebuffer */
483 gl_read_rgba_span( ctx, ctx->ReadBuffer, width, x, y, data );
484
485 /* Restore reading from draw buffer (the default) */
486 (*ctx->Driver.SetReadBuffer)( ctx, ctx->DrawBuffer,
487 ctx->Color.DriverDrawBuffer );
488
489 _mesa_ColorTable(target, internalformat, width,
490 GL_RGBA, GL_UNSIGNED_BYTE, data);
491 }
492
493
494
495 /* XXX not tested */
496 void
497 _mesa_CopyColorSubTable(GLenum target, GLsizei start,
498 GLint x, GLint y, GLsizei width)
499 {
500 GLubyte data[MAX_WIDTH][4];
501 GET_CURRENT_CONTEXT(ctx);
502 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx, "glCopyColorSubTable");
503
504 /* Select buffer to read from */
505 (*ctx->Driver.SetReadBuffer)( ctx, ctx->ReadBuffer,
506 ctx->Pixel.DriverReadBuffer );
507
508 if (width > MAX_WIDTH)
509 width = MAX_WIDTH;
510
511 /* read the data from framebuffer */
512 gl_read_rgba_span( ctx, ctx->ReadBuffer, width, x, y, data );
513
514 /* Restore reading from draw buffer (the default) */
515 (*ctx->Driver.SetReadBuffer)( ctx, ctx->DrawBuffer,
516 ctx->Color.DriverDrawBuffer );
517
518 _mesa_ColorSubTable(target, start, width, GL_RGBA, GL_UNSIGNED_BYTE, data);
519 }
520
521
522
523 void
524 _mesa_GetColorTable( GLenum target, GLenum format,
525 GLenum type, GLvoid *data )
526 {
527 GET_CURRENT_CONTEXT(ctx);
528 struct gl_texture_unit *texUnit = &ctx->Texture.Unit[ctx->Texture.CurrentUnit];
529 struct gl_color_table *table = NULL;
530 GLubyte rgba[MAX_COLOR_TABLE_SIZE][4];
531 GLint i;
532
533 ASSERT_OUTSIDE_BEGIN_END(ctx, "glGetColorTable");
534
535 switch (target) {
536 case GL_TEXTURE_1D:
537 table = &texUnit->CurrentD[1]->Palette;
538 break;
539 case GL_TEXTURE_2D:
540 table = &texUnit->CurrentD[2]->Palette;
541 break;
542 case GL_TEXTURE_3D:
543 table = &texUnit->CurrentD[3]->Palette;
544 break;
545 case GL_SHARED_TEXTURE_PALETTE_EXT:
546 table = &ctx->Texture.Palette;
547 break;
548 case GL_COLOR_TABLE:
549 table = &ctx->ColorTable;
550 break;
551 case GL_POST_CONVOLUTION_COLOR_TABLE:
552 table = &ctx->PostConvolutionColorTable;
553 break;
554 case GL_POST_COLOR_MATRIX_COLOR_TABLE:
555 table = &ctx->PostColorMatrixColorTable;
556 break;
557 default:
558 gl_error(ctx, GL_INVALID_ENUM, "glGetColorTable(target)");
559 return;
560 }
561
562 assert(table);
563
564 switch (table->Format) {
565 case GL_ALPHA:
566 if (table->TableType == GL_FLOAT) {
567 const GLfloat *tableF = (const GLfloat *) table->Table;
568 for (i = 0; i < table->Size; i++) {
569 rgba[i][RCOMP] = 0;
570 rgba[i][GCOMP] = 0;
571 rgba[i][BCOMP] = 0;
572 rgba[i][ACOMP] = (GLint) (tableF[i] * 255.0F);
573 }
574 }
575 else {
576 const GLubyte *tableUB = (const GLubyte *) table->Table;
577 for (i = 0; i < table->Size; i++) {
578 rgba[i][RCOMP] = 0;
579 rgba[i][GCOMP] = 0;
580 rgba[i][BCOMP] = 0;
581 rgba[i][ACOMP] = tableUB[i];
582 }
583 }
584 break;
585 case GL_LUMINANCE:
586 if (table->TableType == GL_FLOAT) {
587 const GLfloat *tableF = (const GLfloat *) table->Table;
588 for (i = 0; i < table->Size; i++) {
589 rgba[i][RCOMP] = (GLint) (tableF[i] * 255.0F);
590 rgba[i][GCOMP] = (GLint) (tableF[i] * 255.0F);
591 rgba[i][BCOMP] = (GLint) (tableF[i] * 255.0F);
592 rgba[i][ACOMP] = 255;
593 }
594 }
595 else {
596 const GLubyte *tableUB = (const GLubyte *) table->Table;
597 for (i = 0; i < table->Size; i++) {
598 rgba[i][RCOMP] = tableUB[i];
599 rgba[i][GCOMP] = tableUB[i];
600 rgba[i][BCOMP] = tableUB[i];
601 rgba[i][ACOMP] = 255;
602 }
603 }
604 break;
605 case GL_LUMINANCE_ALPHA:
606 if (table->TableType == GL_FLOAT) {
607 const GLfloat *tableF = (const GLfloat *) table->Table;
608 for (i = 0; i < table->Size; i++) {
609 rgba[i][RCOMP] = (GLint) (tableF[i*2+0] * 255.0F);
610 rgba[i][GCOMP] = (GLint) (tableF[i*2+0] * 255.0F);
611 rgba[i][BCOMP] = (GLint) (tableF[i*2+0] * 255.0F);
612 rgba[i][ACOMP] = (GLint) (tableF[i*2+1] * 255.0F);
613 }
614 }
615 else {
616 const GLubyte *tableUB = (const GLubyte *) table->Table;
617 for (i = 0; i < table->Size; i++) {
618 rgba[i][RCOMP] = tableUB[i*2+0];
619 rgba[i][GCOMP] = tableUB[i*2+0];
620 rgba[i][BCOMP] = tableUB[i*2+0];
621 rgba[i][ACOMP] = tableUB[i*2+1];
622 }
623 }
624 break;
625 case GL_INTENSITY:
626 if (table->TableType == GL_FLOAT) {
627 const GLfloat *tableF = (const GLfloat *) table->Table;
628 for (i = 0; i < table->Size; i++) {
629 rgba[i][RCOMP] = (GLint) (tableF[i] * 255.0F);
630 rgba[i][GCOMP] = (GLint) (tableF[i] * 255.0F);
631 rgba[i][BCOMP] = (GLint) (tableF[i] * 255.0F);
632 rgba[i][ACOMP] = (GLint) (tableF[i] * 255.0F);
633 }
634 }
635 else {
636 const GLubyte *tableUB = (const GLubyte *) table->Table;
637 for (i = 0; i < table->Size; i++) {
638 rgba[i][RCOMP] = tableUB[i];
639 rgba[i][GCOMP] = tableUB[i];
640 rgba[i][BCOMP] = tableUB[i];
641 rgba[i][ACOMP] = tableUB[i];
642 }
643 }
644 break;
645 case GL_RGB:
646 if (table->TableType == GL_FLOAT) {
647 const GLfloat *tableF = (const GLfloat *) table->Table;
648 for (i = 0; i < table->Size; i++) {
649 rgba[i][RCOMP] = (GLint) (tableF[i*3+0] * 255.0F);
650 rgba[i][GCOMP] = (GLint) (tableF[i*3+1] * 255.0F);
651 rgba[i][BCOMP] = (GLint) (tableF[i*3+2] * 255.0F);
652 rgba[i][ACOMP] = 255;
653 }
654 }
655 else {
656 const GLubyte *tableUB = (const GLubyte *) table->Table;
657 for (i = 0; i < table->Size; i++) {
658 rgba[i][RCOMP] = tableUB[i*3+0];
659 rgba[i][GCOMP] = tableUB[i*3+1];
660 rgba[i][BCOMP] = tableUB[i*3+2];
661 rgba[i][ACOMP] = 255;
662 }
663 }
664 break;
665 case GL_RGBA:
666 if (table->TableType == GL_FLOAT) {
667 const GLfloat *tableF = (const GLfloat *) table->Table;
668 for (i = 0; i < table->Size; i++) {
669 rgba[i][RCOMP] = (GLint) (tableF[i*4+0] * 255.0F);
670 rgba[i][GCOMP] = (GLint) (tableF[i*4+1] * 255.0F);
671 rgba[i][BCOMP] = (GLint) (tableF[i*4+2] * 255.0F);
672 rgba[i][ACOMP] = (GLint) (tableF[i*4+3] * 255.0F);
673 }
674 }
675 else {
676 const GLubyte *tableUB = (const GLubyte *) table->Table;
677 for (i = 0; i < table->Size; i++) {
678 rgba[i][RCOMP] = tableUB[i*4+0];
679 rgba[i][GCOMP] = tableUB[i*4+1];
680 rgba[i][BCOMP] = tableUB[i*4+2];
681 rgba[i][ACOMP] = tableUB[i*4+3];
682 }
683 }
684 break;
685 default:
686 gl_problem(ctx, "bad table format in glGetColorTable");
687 return;
688 }
689
690 _mesa_pack_rgba_span(ctx, table->Size, (const GLubyte (*)[]) rgba,
691 format, type, data, &ctx->Pack, GL_FALSE);
692 }
693
694
695
696 void
697 _mesa_ColorTableParameterfv(GLenum target, GLenum pname, const GLfloat *params)
698 {
699 GET_CURRENT_CONTEXT(ctx);
700 ASSERT_OUTSIDE_BEGIN_END(ctx, "glColorTableParameterfv");
701
702 switch (target) {
703 case GL_COLOR_TABLE_SGI:
704 if (pname == GL_COLOR_TABLE_SCALE_SGI) {
705 ctx->Pixel.ColorTableScale[0] = params[0];
706 ctx->Pixel.ColorTableScale[1] = params[1];
707 ctx->Pixel.ColorTableScale[2] = params[2];
708 ctx->Pixel.ColorTableScale[3] = params[3];
709 }
710 else if (pname == GL_COLOR_TABLE_BIAS_SGI) {
711 ctx->Pixel.ColorTableBias[0] = params[0];
712 ctx->Pixel.ColorTableBias[1] = params[1];
713 ctx->Pixel.ColorTableBias[2] = params[2];
714 ctx->Pixel.ColorTableBias[3] = params[3];
715 }
716 else {
717 gl_error(ctx, GL_INVALID_ENUM, "glColorTableParameterfv(pname)");
718 return;
719 }
720 break;
721 case GL_POST_CONVOLUTION_COLOR_TABLE_SGI:
722 if (pname == GL_COLOR_TABLE_SCALE_SGI) {
723 ctx->Pixel.PCCTscale[0] = params[0];
724 ctx->Pixel.PCCTscale[1] = params[1];
725 ctx->Pixel.PCCTscale[2] = params[2];
726 ctx->Pixel.PCCTscale[3] = params[3];
727 }
728 else if (pname == GL_COLOR_TABLE_BIAS_SGI) {
729 ctx->Pixel.PCCTbias[0] = params[0];
730 ctx->Pixel.PCCTbias[1] = params[1];
731 ctx->Pixel.PCCTbias[2] = params[2];
732 ctx->Pixel.PCCTbias[3] = params[3];
733 }
734 else {
735 gl_error(ctx, GL_INVALID_ENUM, "glColorTableParameterfv(pname)");
736 return;
737 }
738 break;
739 case GL_POST_COLOR_MATRIX_COLOR_TABLE_SGI:
740 if (pname == GL_COLOR_TABLE_SCALE_SGI) {
741 ctx->Pixel.PCMCTscale[0] = params[0];
742 ctx->Pixel.PCMCTscale[1] = params[1];
743 ctx->Pixel.PCMCTscale[2] = params[2];
744 ctx->Pixel.PCMCTscale[3] = params[3];
745 }
746 else if (pname == GL_COLOR_TABLE_BIAS_SGI) {
747 ctx->Pixel.PCMCTbias[0] = params[0];
748 ctx->Pixel.PCMCTbias[1] = params[1];
749 ctx->Pixel.PCMCTbias[2] = params[2];
750 ctx->Pixel.PCMCTbias[3] = params[3];
751 }
752 else {
753 gl_error(ctx, GL_INVALID_ENUM, "glColorTableParameterfv(pname)");
754 return;
755 }
756 break;
757 default:
758 gl_error(ctx, GL_INVALID_ENUM, "glColorTableParameter(target)");
759 return;
760 }
761 }
762
763
764
765 void
766 _mesa_ColorTableParameteriv(GLenum target, GLenum pname, const GLint *params)
767 {
768 GLfloat fparams[4];
769 if (pname == GL_COLOR_TABLE_SGI ||
770 pname == GL_POST_CONVOLUTION_COLOR_TABLE_SGI ||
771 pname == GL_POST_COLOR_MATRIX_COLOR_TABLE_SGI) {
772 /* four values */
773 fparams[0] = (GLfloat) params[0];
774 fparams[1] = (GLfloat) params[1];
775 fparams[2] = (GLfloat) params[2];
776 fparams[3] = (GLfloat) params[3];
777 }
778 else {
779 /* one values */
780 fparams[0] = (GLfloat) params[0];
781 }
782 _mesa_ColorTableParameterfv(target, pname, fparams);
783 }
784
785
786
787 void
788 _mesa_GetColorTableParameterfv( GLenum target, GLenum pname, GLfloat *params )
789 {
790 GET_CURRENT_CONTEXT(ctx);
791 struct gl_texture_unit *texUnit = &ctx->Texture.Unit[ctx->Texture.CurrentUnit];
792 struct gl_color_table *table = NULL;
793
794 ASSERT_OUTSIDE_BEGIN_END(ctx, "glGetColorTableParameterfv");
795
796 switch (target) {
797 case GL_TEXTURE_1D:
798 table = &texUnit->CurrentD[1]->Palette;
799 break;
800 case GL_TEXTURE_2D:
801 table = &texUnit->CurrentD[2]->Palette;
802 break;
803 case GL_TEXTURE_3D:
804 table = &texUnit->CurrentD[3]->Palette;
805 break;
806 case GL_PROXY_TEXTURE_1D:
807 table = &ctx->Texture.Proxy1D->Palette;
808 break;
809 case GL_PROXY_TEXTURE_2D:
810 table = &ctx->Texture.Proxy2D->Palette;
811 break;
812 case GL_PROXY_TEXTURE_3D:
813 table = &ctx->Texture.Proxy3D->Palette;
814 break;
815 case GL_SHARED_TEXTURE_PALETTE_EXT:
816 table = &ctx->Texture.Palette;
817 break;
818 case GL_COLOR_TABLE:
819 table = &ctx->ColorTable;
820 if (pname == GL_COLOR_TABLE_SCALE_SGI) {
821 params[0] = ctx->Pixel.ColorTableScale[0];
822 params[1] = ctx->Pixel.ColorTableScale[1];
823 params[2] = ctx->Pixel.ColorTableScale[2];
824 params[3] = ctx->Pixel.ColorTableScale[3];
825 return;
826 }
827 else if (pname == GL_COLOR_TABLE_BIAS_SGI) {
828 params[0] = ctx->Pixel.ColorTableBias[0];
829 params[1] = ctx->Pixel.ColorTableBias[1];
830 params[2] = ctx->Pixel.ColorTableBias[2];
831 params[3] = ctx->Pixel.ColorTableBias[3];
832 return;
833 }
834 break;
835 case GL_PROXY_COLOR_TABLE:
836 table = &ctx->ProxyColorTable;
837 break;
838 case GL_POST_CONVOLUTION_COLOR_TABLE:
839 table = &ctx->PostConvolutionColorTable;
840 if (pname == GL_COLOR_TABLE_SCALE_SGI) {
841 params[0] = ctx->Pixel.PCCTscale[0];
842 params[1] = ctx->Pixel.PCCTscale[1];
843 params[2] = ctx->Pixel.PCCTscale[2];
844 params[3] = ctx->Pixel.PCCTscale[3];
845 return;
846 }
847 else if (pname == GL_COLOR_TABLE_BIAS_SGI) {
848 params[0] = ctx->Pixel.PCCTbias[0];
849 params[1] = ctx->Pixel.PCCTbias[1];
850 params[2] = ctx->Pixel.PCCTbias[2];
851 params[3] = ctx->Pixel.PCCTbias[3];
852 return;
853 }
854 break;
855 case GL_PROXY_POST_CONVOLUTION_COLOR_TABLE:
856 table = &ctx->ProxyPostConvolutionColorTable;
857 break;
858 case GL_POST_COLOR_MATRIX_COLOR_TABLE:
859 table = &ctx->PostColorMatrixColorTable;
860 if (pname == GL_COLOR_TABLE_SCALE_SGI) {
861 params[0] = ctx->Pixel.PCMCTscale[0];
862 params[1] = ctx->Pixel.PCMCTscale[1];
863 params[2] = ctx->Pixel.PCMCTscale[2];
864 params[3] = ctx->Pixel.PCMCTscale[3];
865 return;
866 }
867 else if (pname == GL_COLOR_TABLE_BIAS_SGI) {
868 params[0] = ctx->Pixel.PCMCTbias[0];
869 params[1] = ctx->Pixel.PCMCTbias[1];
870 params[2] = ctx->Pixel.PCMCTbias[2];
871 params[3] = ctx->Pixel.PCMCTbias[3];
872 return;
873 }
874 break;
875 case GL_PROXY_POST_COLOR_MATRIX_COLOR_TABLE:
876 table = &ctx->ProxyPostColorMatrixColorTable;
877 break;
878 default:
879 gl_error(ctx, GL_INVALID_ENUM, "glGetColorTableParameterfv(target)");
880 return;
881 }
882
883 assert(table);
884
885 switch (pname) {
886 case GL_COLOR_TABLE_FORMAT:
887 *params = table->IntFormat;
888 break;
889 case GL_COLOR_TABLE_WIDTH:
890 *params = table->Size;
891 break;
892 case GL_COLOR_TABLE_RED_SIZE:
893 *params = table->RedSize;
894 break;
895 case GL_COLOR_TABLE_GREEN_SIZE:
896 *params = table->GreenSize;
897 break;
898 case GL_COLOR_TABLE_BLUE_SIZE:
899 *params = table->BlueSize;
900 break;
901 case GL_COLOR_TABLE_ALPHA_SIZE:
902 *params = table->AlphaSize;
903 break;
904 case GL_COLOR_TABLE_LUMINANCE_SIZE:
905 *params = table->LuminanceSize;
906 break;
907 case GL_COLOR_TABLE_INTENSITY_SIZE:
908 *params = table->IntensitySize;
909 break;
910 default:
911 gl_error(ctx, GL_INVALID_ENUM, "glGetColorTableParameterfv(pname)" );
912 return;
913 }
914 }
915
916
917
918 void
919 _mesa_GetColorTableParameteriv( GLenum target, GLenum pname, GLint *params )
920 {
921 GET_CURRENT_CONTEXT(ctx);
922 struct gl_texture_unit *texUnit = &ctx->Texture.Unit[ctx->Texture.CurrentUnit];
923 struct gl_color_table *table = NULL;
924
925 ASSERT_OUTSIDE_BEGIN_END(ctx, "glGetColorTableParameteriv");
926
927 switch (target) {
928 case GL_TEXTURE_1D:
929 table = &texUnit->CurrentD[1]->Palette;
930 break;
931 case GL_TEXTURE_2D:
932 table = &texUnit->CurrentD[2]->Palette;
933 break;
934 case GL_TEXTURE_3D:
935 table = &texUnit->CurrentD[3]->Palette;
936 break;
937 case GL_PROXY_TEXTURE_1D:
938 table = &ctx->Texture.Proxy1D->Palette;
939 break;
940 case GL_PROXY_TEXTURE_2D:
941 table = &ctx->Texture.Proxy2D->Palette;
942 break;
943 case GL_PROXY_TEXTURE_3D:
944 table = &ctx->Texture.Proxy3D->Palette;
945 break;
946 case GL_SHARED_TEXTURE_PALETTE_EXT:
947 table = &ctx->Texture.Palette;
948 break;
949 case GL_COLOR_TABLE:
950 table = &ctx->ColorTable;
951 if (pname == GL_COLOR_TABLE_SCALE_SGI) {
952 params[0] = (GLint) ctx->Pixel.ColorTableScale[0];
953 params[1] = (GLint) ctx->Pixel.ColorTableScale[1];
954 params[2] = (GLint) ctx->Pixel.ColorTableScale[2];
955 params[3] = (GLint) ctx->Pixel.ColorTableScale[3];
956 return;
957 }
958 else if (pname == GL_COLOR_TABLE_BIAS_SGI) {
959 params[0] = (GLint) ctx->Pixel.ColorTableBias[0];
960 params[1] = (GLint) ctx->Pixel.ColorTableBias[1];
961 params[2] = (GLint) ctx->Pixel.ColorTableBias[2];
962 params[3] = (GLint) ctx->Pixel.ColorTableBias[3];
963 return;
964 }
965 break;
966 case GL_PROXY_COLOR_TABLE:
967 table = &ctx->ProxyColorTable;
968 break;
969 case GL_POST_CONVOLUTION_COLOR_TABLE:
970 table = &ctx->PostConvolutionColorTable;
971 if (pname == GL_COLOR_TABLE_SCALE_SGI) {
972 params[0] = (GLint) ctx->Pixel.PCCTscale[0];
973 params[1] = (GLint) ctx->Pixel.PCCTscale[1];
974 params[2] = (GLint) ctx->Pixel.PCCTscale[2];
975 params[3] = (GLint) ctx->Pixel.PCCTscale[3];
976 return;
977 }
978 else if (pname == GL_COLOR_TABLE_BIAS_SGI) {
979 params[0] = (GLint) ctx->Pixel.PCCTbias[0];
980 params[1] = (GLint) ctx->Pixel.PCCTbias[1];
981 params[2] = (GLint) ctx->Pixel.PCCTbias[2];
982 params[3] = (GLint) ctx->Pixel.PCCTbias[3];
983 return;
984 }
985 break;
986 case GL_PROXY_POST_CONVOLUTION_COLOR_TABLE:
987 table = &ctx->ProxyPostConvolutionColorTable;
988 break;
989 case GL_POST_COLOR_MATRIX_COLOR_TABLE:
990 table = &ctx->PostColorMatrixColorTable;
991 if (pname == GL_COLOR_TABLE_SCALE_SGI) {
992 params[0] = (GLint) ctx->Pixel.PCMCTscale[0];
993 params[1] = (GLint) ctx->Pixel.PCMCTscale[1];
994 params[2] = (GLint) ctx->Pixel.PCMCTscale[2];
995 params[3] = (GLint) ctx->Pixel.PCMCTscale[3];
996 return;
997 }
998 else if (pname == GL_COLOR_TABLE_BIAS_SGI) {
999 params[0] = (GLint) ctx->Pixel.PCMCTbias[0];
1000 params[1] = (GLint) ctx->Pixel.PCMCTbias[1];
1001 params[2] = (GLint) ctx->Pixel.PCMCTbias[2];
1002 params[3] = (GLint) ctx->Pixel.PCMCTbias[3];
1003 return;
1004 }
1005 break;
1006 case GL_PROXY_POST_COLOR_MATRIX_COLOR_TABLE:
1007 table = &ctx->ProxyPostColorMatrixColorTable;
1008 break;
1009 default:
1010 gl_error(ctx, GL_INVALID_ENUM, "glGetColorTableParameteriv(target)");
1011 return;
1012 }
1013
1014 assert(table);
1015
1016 switch (pname) {
1017 case GL_COLOR_TABLE_FORMAT:
1018 *params = table->IntFormat;
1019 break;
1020 case GL_COLOR_TABLE_WIDTH:
1021 *params = table->Size;
1022 break;
1023 case GL_COLOR_TABLE_RED_SIZE:
1024 *params = table->RedSize;
1025 break;
1026 case GL_COLOR_TABLE_GREEN_SIZE:
1027 *params = table->GreenSize;
1028 break;
1029 case GL_COLOR_TABLE_BLUE_SIZE:
1030 *params = table->BlueSize;
1031 break;
1032 case GL_COLOR_TABLE_ALPHA_SIZE:
1033 *params = table->AlphaSize;
1034 break;
1035 case GL_COLOR_TABLE_LUMINANCE_SIZE:
1036 *params = table->LuminanceSize;
1037 break;
1038 case GL_COLOR_TABLE_INTENSITY_SIZE:
1039 *params = table->IntensitySize;
1040 break;
1041 default:
1042 gl_error(ctx, GL_INVALID_ENUM, "glGetColorTableParameteriv(pname)" );
1043 return;
1044 }
1045 }