Tizen 2.0 Release
[profile/ivi/osmesa.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 "pbo.h"
36 #include "state.h"
37 #include "teximage.h"
38 #include "texstate.h"
39 #include "main/dispatch.h"
40
41
42 #if FEATURE_colortable
43
44
45 /**
46  * Given an internalFormat token passed to glColorTable,
47  * return the corresponding base format.
48  * Return -1 if invalid token.
49  */
50 static GLint
51 base_colortab_format( GLenum format )
52 {
53    switch (format) {
54       case GL_ALPHA:
55       case GL_ALPHA4:
56       case GL_ALPHA8:
57       case GL_ALPHA12:
58       case GL_ALPHA16:
59          return GL_ALPHA;
60       case GL_LUMINANCE:
61       case GL_LUMINANCE4:
62       case GL_LUMINANCE8:
63       case GL_LUMINANCE12:
64       case GL_LUMINANCE16:
65          return GL_LUMINANCE;
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;
74       case GL_INTENSITY:
75       case GL_INTENSITY4:
76       case GL_INTENSITY8:
77       case GL_INTENSITY12:
78       case GL_INTENSITY16:
79          return GL_INTENSITY;
80       case GL_RGB:
81       case GL_R3_G3_B2:
82       case GL_RGB4:
83       case GL_RGB5:
84       case GL_RGB8:
85       case GL_RGB10:
86       case GL_RGB12:
87       case GL_RGB16:
88          return GL_RGB;
89       case GL_RGBA:
90       case GL_RGBA2:
91       case GL_RGBA4:
92       case GL_RGB5_A1:
93       case GL_RGBA8:
94       case GL_RGB10_A2:
95       case GL_RGBA12:
96       case GL_RGBA16:
97          return GL_RGBA;
98       default:
99          return -1;  /* error */
100    }
101 }
102
103
104
105 /**
106  * Examine table's format and set the component sizes accordingly.
107  */
108 static void
109 set_component_sizes( struct gl_color_table *table )
110 {
111    /* assuming the ubyte table */
112    const GLubyte sz = 8;
113
114    switch (table->_BaseFormat) {
115       case GL_ALPHA:
116          table->RedSize = 0;
117          table->GreenSize = 0;
118          table->BlueSize = 0;
119          table->AlphaSize = sz;
120          table->IntensitySize = 0;
121          table->LuminanceSize = 0;
122          break;
123       case GL_LUMINANCE:
124          table->RedSize = 0;
125          table->GreenSize = 0;
126          table->BlueSize = 0;
127          table->AlphaSize = 0;
128          table->IntensitySize = 0;
129          table->LuminanceSize = sz;
130          break;
131       case GL_LUMINANCE_ALPHA:
132          table->RedSize = 0;
133          table->GreenSize = 0;
134          table->BlueSize = 0;
135          table->AlphaSize = sz;
136          table->IntensitySize = 0;
137          table->LuminanceSize = sz;
138          break;
139       case GL_INTENSITY:
140          table->RedSize = 0;
141          table->GreenSize = 0;
142          table->BlueSize = 0;
143          table->AlphaSize = 0;
144          table->IntensitySize = sz;
145          table->LuminanceSize = 0;
146          break;
147       case GL_RGB:
148          table->RedSize = sz;
149          table->GreenSize = sz;
150          table->BlueSize = sz;
151          table->AlphaSize = 0;
152          table->IntensitySize = 0;
153          table->LuminanceSize = 0;
154          break;
155       case GL_RGBA:
156          table->RedSize = sz;
157          table->GreenSize = sz;
158          table->BlueSize = sz;
159          table->AlphaSize = sz;
160          table->IntensitySize = 0;
161          table->LuminanceSize = 0;
162          break;
163       default:
164          _mesa_problem(NULL, "unexpected format in set_component_sizes");
165    }
166 }
167
168
169
170 /**
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
181  */
182 static void
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)
190 {
191    data = _mesa_map_validate_pbo_source(ctx, 
192                                         1, &ctx->Unpack, count, 1, 1,
193                                         format, type, INT_MAX, data,
194                                         "glColor[Sub]Table");
195    if (!data)
196       return;
197
198    {
199       /* convert user-provided data to GLfloat values */
200       GLfloat tempTab[MAX_COLOR_TABLE_SIZE * 4];
201       GLfloat *tableF;
202       GLint i;
203
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 */
209                                     data,          /* src data */
210                                     &ctx->Unpack,
211                                     IMAGE_CLAMP_BIT); /* transfer ops */
212
213       /* the destination */
214       tableF = table->TableF;
215
216       /* Apply scale & bias & clamp now */
217       switch (table->_BaseFormat) {
218          case GL_INTENSITY:
219             for (i = 0; i < count; i++) {
220                GLuint j = start + i;
221                tableF[j] = CLAMP(tempTab[i] * rScale + rBias, 0.0F, 1.0F);
222             }
223             break;
224          case GL_LUMINANCE:
225             for (i = 0; i < count; i++) {
226                GLuint j = start + i;
227                tableF[j] = CLAMP(tempTab[i] * rScale + rBias, 0.0F, 1.0F);
228             }
229             break;
230          case GL_ALPHA:
231             for (i = 0; i < count; i++) {
232                GLuint j = start + i;
233                tableF[j] = CLAMP(tempTab[i] * aScale + aBias, 0.0F, 1.0F);
234             }
235             break;
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);
241             }
242             break;
243          case GL_RGB:
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);
249             }
250             break;
251          case GL_RGBA:
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);
258             }
259             break;
260          default:
261             _mesa_problem(ctx, "Bad format in store_colortable_entries");
262             return;
263          }
264    }
265
266    /* update the ubyte table */
267    {
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;
271       GLint i;
272       for (i = 0; i < count * comps; i++) {
273          CLAMPED_FLOAT_TO_UBYTE(tableUB[i], tableF[i]);
274       }
275    }
276
277    _mesa_unmap_pbo_source(ctx, &ctx->Unpack);
278 }
279
280
281
282 void GLAPIENTRY
283 _mesa_ColorTable( GLenum target, GLenum internalFormat,
284                   GLsizei width, GLenum format, GLenum type,
285                   const GLvoid *data )
286 {
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;
294    GLint baseFormat;
295    const GLfloat *scale = one, *bias = zero;
296    GLint comps;
297
298    ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx); /* too complex */
299
300    switch (target) {
301       case GL_SHARED_TEXTURE_PALETTE_EXT:
302          table = &ctx->Texture.Palette;
303          break;
304       default:
305          /* try texture targets */
306          {
307             struct gl_texture_object *texobj
308                = _mesa_select_tex_object(ctx, texUnit, target);
309             if (texobj) {
310                table = &texobj->Palette;
311                proxy = _mesa_is_proxy_texture(target);
312             }
313             else {
314                _mesa_error(ctx, GL_INVALID_ENUM, "glColorTable(target)");
315                return;
316             }
317          }
318    }
319
320    assert(table);
321
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)");
325       return;
326    }
327
328    baseFormat = base_colortab_format(internalFormat);
329    if (baseFormat < 0) {
330       _mesa_error(ctx, GL_INVALID_ENUM, "glColorTable(internalFormat)");
331       return;
332    }
333
334    if (width < 0 || (width != 0 && !_mesa_is_pow_two(width))) {
335       /* error */
336       if (proxy) {
337          table->Size = 0;
338          table->InternalFormat = (GLenum) 0;
339          table->_BaseFormat = (GLenum) 0;
340       }
341       else {
342          _mesa_error(ctx, GL_INVALID_VALUE, "glColorTable(width=%d)", width);
343       }
344       return;
345    }
346
347    if (width > (GLsizei) ctx->Const.MaxColorTableSize) {
348       if (proxy) {
349          table->Size = 0;
350          table->InternalFormat = (GLenum) 0;
351          table->_BaseFormat = (GLenum) 0;
352       }
353       else {
354          _mesa_error(ctx, GL_TABLE_TOO_LARGE, "glColorTable(width)");
355       }
356       return;
357    }
358
359    table->Size = width;
360    table->InternalFormat = internalFormat;
361    table->_BaseFormat = (GLenum) baseFormat;
362
363    comps = _mesa_components_in_format(table->_BaseFormat);
364    assert(comps > 0);  /* error should have been caught sooner */
365
366    if (!proxy) {
367       _mesa_free_colortable_data(table);
368
369       if (width > 0) {
370          table->TableF = (GLfloat *) malloc(comps * width * sizeof(GLfloat));
371          table->TableUB = (GLubyte *) malloc(comps * width * sizeof(GLubyte));
372
373          if (!table->TableF || !table->TableUB) {
374             _mesa_error(ctx, GL_OUT_OF_MEMORY, "glColorTable");
375             return;
376          }
377
378          store_colortable_entries(ctx, table,
379                                   0, width,  /* start, count */
380                                   format, type, data,
381                                   scale[0], bias[0],
382                                   scale[1], bias[1],
383                                   scale[2], bias[2],
384                                   scale[3], bias[3]);
385       }
386    } /* proxy */
387
388    /* do this after the table's Type and Format are set */
389    set_component_sizes(table);
390
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 );
395       }
396    }
397
398    ctx->NewState |= _NEW_PIXEL;
399 }
400
401
402
403 void GLAPIENTRY
404 _mesa_ColorSubTable( GLenum target, GLsizei start,
405                      GLsizei count, GLenum format, GLenum type,
406                      const GLvoid *data )
407 {
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;
415
416    ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
417
418    switch (target) {
419       case GL_SHARED_TEXTURE_PALETTE_EXT:
420          table = &ctx->Texture.Palette;
421          break;
422       default:
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;
427          }
428          else {
429             _mesa_error(ctx, GL_INVALID_ENUM, "glColorSubTable(target)");
430             return;
431          }
432    }
433
434    assert(table);
435
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)");
439       return;
440    }
441
442    if (count < 1) {
443       _mesa_error(ctx, GL_INVALID_VALUE, "glColorSubTable(count)");
444       return;
445    }
446
447    /* error should have been caught sooner */
448    assert(_mesa_components_in_format(table->_BaseFormat) > 0);
449
450    if (start + count > (GLint) table->Size) {
451       _mesa_error(ctx, GL_INVALID_VALUE, "glColorSubTable(count)");
452       return;
453    }
454
455    if (!table->TableF || !table->TableUB) {
456       /* a GL_OUT_OF_MEMORY error would have been recorded previously */
457       return;
458    }
459
460    store_colortable_entries(ctx, table, start, count,
461                             format, type, data,
462                             scale[0], bias[0],
463                             scale[1], bias[1],
464                             scale[2], bias[2],
465                             scale[3], bias[3]);
466
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 );
471       }
472    }
473
474    ctx->NewState |= _NEW_PIXEL;
475 }
476
477
478
479 static void GLAPIENTRY
480 _mesa_CopyColorTable(GLenum target, GLenum internalformat,
481                      GLint x, GLint y, GLsizei width)
482 {
483    GET_CURRENT_CONTEXT(ctx);
484    ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
485
486    if (!ctx->ReadBuffer->_ColorReadBuffer) {
487       return;      /* no readbuffer - OK */
488    }
489
490    ctx->Driver.CopyColorTable( ctx, target, internalformat, x, y, width );
491 }
492
493
494
495 static void GLAPIENTRY
496 _mesa_CopyColorSubTable(GLenum target, GLsizei start,
497                         GLint x, GLint y, GLsizei width)
498 {
499    GET_CURRENT_CONTEXT(ctx);
500    ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
501
502    if (!ctx->ReadBuffer->_ColorReadBuffer) {
503       return;      /* no readbuffer - OK */
504    }
505
506    ctx->Driver.CopyColorSubTable( ctx, target, start, x, y, width );
507 }
508
509
510
511 static void GLAPIENTRY
512 _mesa_GetnColorTableARB( GLenum target, GLenum format, GLenum type,
513                          GLsizei bufSize, GLvoid *data )
514 {
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    GLbitfield transferOps = 0;
520    ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
521
522    if (ctx->NewState) {
523       _mesa_update_state(ctx);
524    }
525
526    switch (target) {
527       case GL_SHARED_TEXTURE_PALETTE_EXT:
528          table = &ctx->Texture.Palette;
529          break;
530       default:
531          /* try texture targets */
532          {
533             struct gl_texture_object *texobj
534                = _mesa_select_tex_object(ctx, texUnit, target);
535             if (texobj && !_mesa_is_proxy_texture(target)) {
536                table = &texobj->Palette;
537             }
538             else {
539                _mesa_error(ctx, GL_INVALID_ENUM, "glGetColorTable(target)");
540                return;
541             }
542          }
543    }
544
545    ASSERT(table);
546
547    if (table->Size <= 0) {
548       return;
549    }
550
551    switch (table->_BaseFormat) {
552    case GL_ALPHA:
553       {
554          GLuint i;
555          for (i = 0; i < table->Size; i++) {
556             rgba[i][RCOMP] = 0;
557             rgba[i][GCOMP] = 0;
558             rgba[i][BCOMP] = 0;
559             rgba[i][ACOMP] = table->TableF[i];
560          }
561       }
562       break;
563    case GL_LUMINANCE:
564       {
565          GLuint i;
566          for (i = 0; i < table->Size; i++) {
567             rgba[i][RCOMP] =
568             rgba[i][GCOMP] =
569             rgba[i][BCOMP] = table->TableF[i];
570             rgba[i][ACOMP] = 1.0F;
571          }
572       }
573       break;
574    case GL_LUMINANCE_ALPHA:
575       {
576          GLuint i;
577          for (i = 0; i < table->Size; i++) {
578             rgba[i][RCOMP] =
579             rgba[i][GCOMP] =
580             rgba[i][BCOMP] = table->TableF[i*2+0];
581             rgba[i][ACOMP] = table->TableF[i*2+1];
582          }
583       }
584       break;
585    case GL_INTENSITY:
586       {
587          GLuint i;
588          for (i = 0; i < table->Size; i++) {
589             rgba[i][RCOMP] =
590             rgba[i][GCOMP] =
591             rgba[i][BCOMP] =
592             rgba[i][ACOMP] = table->TableF[i];
593          }
594       }
595       break;
596    case GL_RGB:
597       {
598          GLuint i;
599          for (i = 0; i < table->Size; i++) {
600             rgba[i][RCOMP] = table->TableF[i*3+0];
601             rgba[i][GCOMP] = table->TableF[i*3+1];
602             rgba[i][BCOMP] = table->TableF[i*3+2];
603             rgba[i][ACOMP] = 1.0F;
604          }
605       }
606       break;
607    case GL_RGBA:
608       memcpy(rgba, table->TableF, 4 * table->Size * sizeof(GLfloat));
609       break;
610    default:
611       _mesa_problem(ctx, "bad table format in glGetColorTable");
612       return;
613    }
614
615    data = _mesa_map_validate_pbo_dest(ctx, 
616                                       1, &ctx->Pack, table->Size, 1, 1,
617                                       format, type, bufSize, data,
618                                       "glGetColorTable");
619    if (!data)
620       return;
621
622    /* TODO: is this correct? */
623    if(ctx->Color._ClampReadColor)
624       transferOps |= IMAGE_CLAMP_BIT;
625
626    _mesa_pack_rgba_span_float(ctx, table->Size, rgba,
627                               format, type, data, &ctx->Pack, transferOps);
628
629    _mesa_unmap_pbo_dest(ctx, &ctx->Pack);
630 }
631
632
633 static void GLAPIENTRY
634 _mesa_GetColorTable( GLenum target, GLenum format,
635                      GLenum type, GLvoid *data )
636 {
637    _mesa_GetnColorTableARB(target, format, type, INT_MAX, data);
638 }
639
640
641 static void GLAPIENTRY
642 _mesa_ColorTableParameterfv(GLenum target, GLenum pname, const GLfloat *params)
643 {
644    /* no extensions use this function */
645    GET_CURRENT_CONTEXT(ctx);
646    ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
647    _mesa_error(ctx, GL_INVALID_ENUM, "glColorTableParameterfv(target)");
648 }
649
650
651
652 static void GLAPIENTRY
653 _mesa_ColorTableParameteriv(GLenum target, GLenum pname, const GLint *params)
654 {
655    /* no extensions use this function */
656    GET_CURRENT_CONTEXT(ctx);
657    ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
658    _mesa_error(ctx, GL_INVALID_ENUM, "glColorTableParameteriv(target)");
659 }
660
661
662
663 static void GLAPIENTRY
664 _mesa_GetColorTableParameterfv( GLenum target, GLenum pname, GLfloat *params )
665 {
666    GET_CURRENT_CONTEXT(ctx);
667    struct gl_texture_unit *texUnit = _mesa_get_current_tex_unit(ctx);
668    struct gl_color_table *table = NULL;
669    ASSERT_OUTSIDE_BEGIN_END(ctx);
670
671    switch (target) {
672       case GL_SHARED_TEXTURE_PALETTE_EXT:
673          table = &ctx->Texture.Palette;
674          break;
675       default:
676          /* try texture targets */
677          {
678             struct gl_texture_object *texobj
679                = _mesa_select_tex_object(ctx, texUnit, target);
680             if (texobj) {
681                table = &texobj->Palette;
682             }
683             else {
684                _mesa_error(ctx, GL_INVALID_ENUM,
685                            "glGetColorTableParameterfv(target)");
686                return;
687             }
688          }
689    }
690
691    assert(table);
692
693    switch (pname) {
694       case GL_COLOR_TABLE_FORMAT:
695          *params = (GLfloat) table->InternalFormat;
696          break;
697       case GL_COLOR_TABLE_WIDTH:
698          *params = (GLfloat) table->Size;
699          break;
700       case GL_COLOR_TABLE_RED_SIZE:
701          *params = (GLfloat) table->RedSize;
702          break;
703       case GL_COLOR_TABLE_GREEN_SIZE:
704          *params = (GLfloat) table->GreenSize;
705          break;
706       case GL_COLOR_TABLE_BLUE_SIZE:
707          *params = (GLfloat) table->BlueSize;
708          break;
709       case GL_COLOR_TABLE_ALPHA_SIZE:
710          *params = (GLfloat) table->AlphaSize;
711          break;
712       case GL_COLOR_TABLE_LUMINANCE_SIZE:
713          *params = (GLfloat) table->LuminanceSize;
714          break;
715       case GL_COLOR_TABLE_INTENSITY_SIZE:
716          *params = (GLfloat) table->IntensitySize;
717          break;
718       default:
719          _mesa_error(ctx, GL_INVALID_ENUM, "glGetColorTableParameterfv(pname)" );
720          return;
721    }
722 }
723
724
725
726 static void GLAPIENTRY
727 _mesa_GetColorTableParameteriv( GLenum target, GLenum pname, GLint *params )
728 {
729    GET_CURRENT_CONTEXT(ctx);
730    struct gl_texture_unit *texUnit = _mesa_get_current_tex_unit(ctx);
731    struct gl_color_table *table = NULL;
732    ASSERT_OUTSIDE_BEGIN_END(ctx);
733
734    switch (target) {
735       case GL_SHARED_TEXTURE_PALETTE_EXT:
736          table = &ctx->Texture.Palette;
737          break;
738       default:
739          /* Try texture targets */
740          {
741             struct gl_texture_object *texobj
742                = _mesa_select_tex_object(ctx, texUnit, target);
743             if (texobj) {
744                table = &texobj->Palette;
745             }
746             else {
747                _mesa_error(ctx, GL_INVALID_ENUM,
748                            "glGetColorTableParameteriv(target)");
749                return;
750             }
751          }
752    }
753
754    assert(table);
755
756    switch (pname) {
757       case GL_COLOR_TABLE_FORMAT:
758          *params = table->InternalFormat;
759          break;
760       case GL_COLOR_TABLE_WIDTH:
761          *params = table->Size;
762          break;
763       case GL_COLOR_TABLE_RED_SIZE:
764          *params = table->RedSize;
765          break;
766       case GL_COLOR_TABLE_GREEN_SIZE:
767          *params = table->GreenSize;
768          break;
769       case GL_COLOR_TABLE_BLUE_SIZE:
770          *params = table->BlueSize;
771          break;
772       case GL_COLOR_TABLE_ALPHA_SIZE:
773          *params = table->AlphaSize;
774          break;
775       case GL_COLOR_TABLE_LUMINANCE_SIZE:
776          *params = table->LuminanceSize;
777          break;
778       case GL_COLOR_TABLE_INTENSITY_SIZE:
779          *params = table->IntensitySize;
780          break;
781       default:
782          _mesa_error(ctx, GL_INVALID_ENUM, "glGetColorTableParameteriv(pname)" );
783          return;
784    }
785 }
786
787
788 void
789 _mesa_init_colortable_dispatch(struct _glapi_table *disp)
790 {
791    SET_ColorSubTable(disp, _mesa_ColorSubTable);
792    SET_ColorTable(disp, _mesa_ColorTable);
793    SET_ColorTableParameterfv(disp, _mesa_ColorTableParameterfv);
794    SET_ColorTableParameteriv(disp, _mesa_ColorTableParameteriv);
795    SET_CopyColorSubTable(disp, _mesa_CopyColorSubTable);
796    SET_CopyColorTable(disp, _mesa_CopyColorTable);
797    SET_GetColorTable(disp, _mesa_GetColorTable);
798    SET_GetColorTableParameterfv(disp, _mesa_GetColorTableParameterfv);
799    SET_GetColorTableParameteriv(disp, _mesa_GetColorTableParameteriv);
800
801    /* GL_ARB_robustness */
802    SET_GetnColorTableARB(disp, _mesa_GetnColorTableARB);
803 }
804
805
806 #endif /* FEATURE_colortable */
807
808
809 /**********************************************************************/
810 /*****                      Initialization                        *****/
811 /**********************************************************************/
812
813
814 void
815 _mesa_init_colortable( struct gl_color_table *p )
816 {
817    p->TableF = NULL;
818    p->TableUB = NULL;
819    p->Size = 0;
820    p->InternalFormat = GL_RGBA;
821 }
822
823
824
825 void
826 _mesa_free_colortable_data( struct gl_color_table *p )
827 {
828    if (p->TableF) {
829       free(p->TableF);
830       p->TableF = NULL;
831    }
832    if (p->TableUB) {
833       free(p->TableUB);
834       p->TableUB = NULL;
835    }
836 }