summaryrefslogtreecommitdiff
path: root/src/mesa/main/colortab.c
blob: f456b691ccd24392199dd317ed33713c10642287 (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
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
/* $Id: colortab.c,v 1.11 2000/04/11 15:07:48 brianp Exp $ */

/*
 * Mesa 3-D graphics library
 * Version:  3.3
 * 
 * Copyright (C) 1999-2000  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_ColorTable( 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 *table;
   GLboolean proxy = GL_FALSE;

   ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx, "glColorTable");

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

   assert(table);

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

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

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


   table->Size = width;
   table->IntFormat = internalFormat;
   table->Format = (GLenum) decode_internal_format(internalFormat);
   if (!proxy) {
      _mesa_unpack_ubyte_color_span(ctx, width, table->Format,
                                    table->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_ColorSubTable( 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, "glColorSubTable");

   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, "glColorSubTable(target)");
         return;
   }

   assert(palette);

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

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

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

   if (start + count > palette->Size) {
      gl_error(ctx, GL_INVALID_VALUE, "glColorSubTable(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_GetColorTable( 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, "glGetColorTable");

   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, "glGetColorTable(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 glGetColorTable");
         return;
   }

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

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



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



void
_mesa_GetColorTableParameteriv( 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, "glGetColorTableParameter");

   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_PROXY_TEXTURE_1D:
         palette = &ctx->Texture.Proxy1D->Palette;
         break;
      case GL_PROXY_TEXTURE_2D:
         palette = &ctx->Texture.Proxy2D->Palette;
         break;
      case GL_PROXY_TEXTURE_3D:
         palette = &ctx->Texture.Proxy3D->Palette;
         break;
      case GL_SHARED_TEXTURE_PALETTE_EXT:
         palette = &ctx->Texture.Palette;
         break;
      default:
         gl_error(ctx, GL_INVALID_ENUM, "glGetColorTableParameter(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, "glGetColorTableParameter" );
         return;
   }
}