summaryrefslogtreecommitdiff
path: root/src/mesa/main/colortab.c
blob: 7597d10773f916c966dea0fb1065b4c3cfffe0af (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
/* $Id: colortab.c,v 1.7 1999/11/19 22:51:29 brianp Exp $ */

/*
 * Mesa 3-D graphics library
 * Version:  3.3
 * 
 * Copyright (C) 1999  Brian Paul   All Rights Reserved.
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included
 * in all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */


#ifdef PC_HEADER
#include "all.h"
#else
#include "glheader.h"
#include "colortab.h"
#include "context.h"
#include "image.h"
#include "macros.h"
#endif



/*
 * Return GL_TRUE if k is a power of two, else return GL_FALSE.
 */
static GLboolean
power_of_two( GLint k )
{
   GLint i, m = 1;
   for (i=0; i<32; i++) {
      if (k == m)
         return GL_TRUE;
      m = m << 1;
   }
   return GL_FALSE;
}


static GLint
decode_internal_format( GLint format )
{
   switch (format) {
      case GL_ALPHA:
      case GL_ALPHA4:
      case GL_ALPHA8:
      case GL_ALPHA12:
      case GL_ALPHA16:
         return GL_ALPHA;
      case 1:
      case GL_LUMINANCE:
      case GL_LUMINANCE4:
      case GL_LUMINANCE8:
      case GL_LUMINANCE12:
      case GL_LUMINANCE16:
         return GL_LUMINANCE;
      case 2:
      case GL_LUMINANCE_ALPHA:
      case GL_LUMINANCE4_ALPHA4:
      case GL_LUMINANCE6_ALPHA2:
      case GL_LUMINANCE8_ALPHA8:
      case GL_LUMINANCE12_ALPHA4:
      case GL_LUMINANCE12_ALPHA12:
      case GL_LUMINANCE16_ALPHA16:
         return GL_LUMINANCE_ALPHA;
      case GL_INTENSITY:
      case GL_INTENSITY4:
      case GL_INTENSITY8:
      case GL_INTENSITY12:
      case GL_INTENSITY16:
         return GL_INTENSITY;
      case 3:
      case GL_RGB:
      case GL_R3_G3_B2:
      case GL_RGB4:
      case GL_RGB5:
      case GL_RGB8:
      case GL_RGB10:
      case GL_RGB12:
      case GL_RGB16:
         return GL_RGB;
      case 4:
      case GL_RGBA:
      case GL_RGBA2:
      case GL_RGBA4:
      case GL_RGB5_A1:
      case GL_RGBA8:
      case GL_RGB10_A2:
      case GL_RGBA12:
      case GL_RGBA16:
         return GL_RGBA;
      default:
         return -1;  /* error */
   }
}


void 
_mesa_ColorTableEXT( GLenum target, GLenum internalFormat,
                     GLsizei width, GLenum format, GLenum type,
                     const GLvoid *table )
{
   GET_CURRENT_CONTEXT(ctx);
   struct gl_texture_unit *texUnit = &ctx->Texture.Unit[ctx->Texture.CurrentUnit];
   struct gl_texture_object *texObj;
   struct gl_color_table *palette;
   GLboolean proxy = GL_FALSE;

   ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx, "glColorTableEXT");

   switch (target) {
      case GL_TEXTURE_1D:
         texObj = texUnit->CurrentD[1];
         palette = &texObj->Palette;
         break;
      case GL_TEXTURE_2D:
         texObj = texUnit->CurrentD[2];
         palette = &texObj->Palette;
         break;
      case GL_TEXTURE_3D:
         texObj = texUnit->CurrentD[3];
         palette = &texObj->Palette;
         break;
      case GL_PROXY_TEXTURE_1D:
         texObj = ctx->Texture.Proxy1D;
         palette = &texObj->Palette;
         proxy = GL_TRUE;
         break;
      case GL_PROXY_TEXTURE_2D:
         texObj = ctx->Texture.Proxy2D;
         palette = &texObj->Palette;
         proxy = GL_TRUE;
         break;
      case GL_PROXY_TEXTURE_3D:
         texObj = ctx->Texture.Proxy3D;
         palette = &texObj->Palette;
         proxy = GL_TRUE;
         break;
      case GL_SHARED_TEXTURE_PALETTE_EXT:
         texObj = NULL;
         palette = &ctx->Texture.Palette;
         break;
      default:
         gl_error(ctx, GL_INVALID_ENUM, "glColorTableEXT(target)");
         return;
   }

   assert(palette);

   if (!gl_is_legal_format_and_type(format, type)) {
      gl_error(ctx, GL_INVALID_ENUM, "glColorTableEXT(format or type)");
      return;
   }

   if (decode_internal_format(internalFormat) < 0) {
      gl_error( ctx, GL_INVALID_ENUM, "glColorTableEXT(internalFormat)" );
      return;
   }

   if (width < 1 || width > MAX_TEXTURE_PALETTE_SIZE || !power_of_two(width)) {
      gl_error(ctx, GL_INVALID_VALUE, "glColorTableEXT(width)");
      if (proxy) {
         palette->Size = 0;
         palette->IntFormat = (GLenum) 0;
         palette->Format = (GLenum) 0;
      }
      return;
   }

   palette->Size = width;
   palette->IntFormat = internalFormat;
   palette->Format = (GLenum) decode_internal_format(internalFormat);
   if (!proxy) {
      _mesa_unpack_ubyte_color_span(ctx, width, palette->Format,
                                    palette->Table,  /* dest */
                                    format, type, table,
                                    &ctx->Unpack, GL_FALSE);
   }
   if (texObj) {
      /* per-texture object palette */
      if (ctx->Driver.UpdateTexturePalette) {
         (*ctx->Driver.UpdateTexturePalette)( ctx, texObj );
      }
   }
   else {
      /* shared texture palette */
      if (ctx->Driver.UpdateTexturePalette) {
         (*ctx->Driver.UpdateTexturePalette)( ctx, NULL );
      }
   }
}



void
_mesa_ColorSubTableEXT( GLenum target, GLsizei start,
                        GLsizei count, GLenum format, GLenum type,
                        const GLvoid *table )
{
   GET_CURRENT_CONTEXT(ctx);
   struct gl_texture_unit *texUnit = &ctx->Texture.Unit[ctx->Texture.CurrentUnit];
   struct gl_texture_object *texObj;
   struct gl_color_table *palette;
   GLint comps;
   GLubyte *dest;

   ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx, "glColorSubTableEXT");

   switch (target) {
      case GL_TEXTURE_1D:
         texObj = texUnit->CurrentD[1];
         palette = &texObj->Palette;
         break;
      case GL_TEXTURE_2D:
         texObj = texUnit->CurrentD[2];
         palette = &texObj->Palette;
         break;
      case GL_TEXTURE_3D:
         texObj = texUnit->CurrentD[3];
         palette = &texObj->Palette;
         break;
      case GL_SHARED_TEXTURE_PALETTE_EXT:
         texObj = NULL;
         palette = &ctx->Texture.Palette;
         break;
      default:
         gl_error(ctx, GL_INVALID_ENUM, "glColorSubTableEXT(target)");
         return;
   }

   assert(palette);

   if (!gl_is_legal_format_and_type(format, type)) {
      gl_error(ctx, GL_INVALID_ENUM, "glColorSubTableEXT(format or type)");
      return;
   }

   if (count < 1) {
      gl_error(ctx, GL_INVALID_VALUE, "glColorSubTableEXT(count)");
      return;
   }

   comps = gl_components_in_format(format);
   assert(comps > 0);  /* error should be caught sooner */

   if (start + count > palette->Size) {
      gl_error(ctx, GL_INVALID_VALUE, "glColorSubTableEXT(count)");
      return;
   }
   dest = palette->Table + start * comps * sizeof(GLubyte);
   _mesa_unpack_ubyte_color_span(ctx, count, palette->Format, dest,
                                 format, type, table,
                                 &ctx->Unpack, GL_FALSE);

   if (texObj) {
      /* per-texture object palette */
      if (ctx->Driver.UpdateTexturePalette) {
         (*ctx->Driver.UpdateTexturePalette)( ctx, texObj );
      }
   }
   else {
      /* shared texture palette */
      if (ctx->Driver.UpdateTexturePalette) {
         (*ctx->Driver.UpdateTexturePalette)( ctx, NULL );
      }
   }
}



void
_mesa_GetColorTableEXT( GLenum target, GLenum format,
                        GLenum type, GLvoid *table )
{
   GET_CURRENT_CONTEXT(ctx);
   struct gl_texture_unit *texUnit = &ctx->Texture.Unit[ctx->Texture.CurrentUnit];
   struct gl_color_table *palette;
   GLubyte rgba[MAX_TEXTURE_PALETTE_SIZE][4];
   GLint i;

   ASSERT_OUTSIDE_BEGIN_END(ctx, "glGetColorTableEXT");

   switch (target) {
      case GL_TEXTURE_1D:
         palette = &texUnit->CurrentD[1]->Palette;
         break;
      case GL_TEXTURE_2D:
         palette = &texUnit->CurrentD[2]->Palette;
         break;
      case GL_TEXTURE_3D:
         palette = &texUnit->CurrentD[3]->Palette;
         break;
      case GL_SHARED_TEXTURE_PALETTE_EXT:
         palette = &ctx->Texture.Palette;
         break;
      default:
         gl_error(ctx, GL_INVALID_ENUM, "glGetColorTableEXT(target)");
         return;
   }

   assert(palette);

   switch (palette->Format) {
      case GL_ALPHA:
         for (i = 0; i < palette->Size; i++) {
            rgba[i][RCOMP] = 0;
            rgba[i][GCOMP] = 0;
            rgba[i][BCOMP] = 0;
            rgba[i][ACOMP] = palette->Table[i];
         }
         break;
      case GL_LUMINANCE:
         for (i = 0; i < palette->Size; i++) {
            rgba[i][RCOMP] = palette->Table[i];
            rgba[i][GCOMP] = palette->Table[i];
            rgba[i][BCOMP] = palette->Table[i];
            rgba[i][ACOMP] = 255;
         }
         break;
      case GL_LUMINANCE_ALPHA:
         for (i = 0; i < palette->Size; i++) {
            rgba[i][RCOMP] = palette->Table[i*2+0];
            rgba[i][GCOMP] = palette->Table[i*2+0];
            rgba[i][BCOMP] = palette->Table[i*2+0];
            rgba[i][ACOMP] = palette->Table[i*2+1];
         }
         break;
      case GL_INTENSITY:
         for (i = 0; i < palette->Size; i++) {
            rgba[i][RCOMP] = palette->Table[i];
            rgba[i][GCOMP] = palette->Table[i];
            rgba[i][BCOMP] = palette->Table[i];
            rgba[i][ACOMP] = 255;
         }
         break;
      case GL_RGB:
         for (i = 0; i < palette->Size; i++) {
            rgba[i][RCOMP] = palette->Table[i*3+0];
            rgba[i][GCOMP] = palette->Table[i*3+1];
            rgba[i][BCOMP] = palette->Table[i*3+2];
            rgba[i][ACOMP] = 255;
         }
         break;
      case GL_RGBA:
         for (i = 0; i < palette->Size; i++) {
            rgba[i][RCOMP] = palette->Table[i*4+0];
            rgba[i][GCOMP] = palette->Table[i*4+1];
            rgba[i][BCOMP] = palette->Table[i*4+2];
            rgba[i][ACOMP] = palette->Table[i*4+3];
         }
         break;
      default:
         gl_problem(ctx, "bad palette format in glGetColorTableEXT");
         return;
   }

   gl_pack_rgba_span(ctx, palette->Size, (const GLubyte (*)[]) rgba,
                     format, type, table, &ctx->Pack, GL_FALSE);

   (void) format;
   (void) type;
   (void) table;
}



void
_mesa_GetColorTableParameterfvEXT( GLenum target, GLenum pname, GLfloat *params )
{
   GLint iparams[10];
   _mesa_GetColorTableParameterivEXT( target, pname, iparams );
   *params = (GLfloat) iparams[0];
}



void
_mesa_GetColorTableParameterivEXT( GLenum target, GLenum pname, GLint *params )
{
   GET_CURRENT_CONTEXT(ctx);
   struct gl_texture_unit *texUnit = &ctx->Texture.Unit[ctx->Texture.CurrentUnit];
   struct gl_color_table *palette;

   ASSERT_OUTSIDE_BEGIN_END(ctx, "glGetColorTableParameterEXT");

   switch (target) {
      case GL_TEXTURE_1D:
         palette = &texUnit->CurrentD[1]->Palette;
         break;
      case GL_TEXTURE_2D:
         palette = &texUnit->CurrentD[2]->Palette;
         break;
      case GL_TEXTURE_3D:
         palette = &texUnit->CurrentD[3]->Palette;
         break;
      case GL_SHARED_TEXTURE_PALETTE_EXT:
         palette = &ctx->Texture.Palette;
         break;
      default:
         gl_error(ctx, GL_INVALID_ENUM, "glGetColorTableParameterEXT(target)");
         return;
   }

   switch (pname) {
      case GL_COLOR_TABLE_FORMAT:
         *params = palette->IntFormat;
         break;
      case GL_COLOR_TABLE_WIDTH:
         *params = palette->Size;
         break;
      case GL_COLOR_TABLE_RED_SIZE:
         *params = 8;
         break;
      case GL_COLOR_TABLE_GREEN_SIZE:
         *params = 8;
         break;
      case GL_COLOR_TABLE_BLUE_SIZE:
         *params = 8;
         break;
      case GL_COLOR_TABLE_ALPHA_SIZE:
         *params = 8;
         break;
      case GL_COLOR_TABLE_LUMINANCE_SIZE:
         *params = 8;
         break;
      case GL_COLOR_TABLE_INTENSITY_SIZE:
         *params = 8;
         break;
      default:
         gl_error(ctx, GL_INVALID_ENUM, "glGetColorTableParameterEXT" );
         return;
   }
}