summaryrefslogtreecommitdiff
path: root/src/mesa/es/main/es_cpaltex.c
blob: 0d6f7410c3074740ee58cfb8c8ba54f6eeb24396 (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
/**************************************************************************
 *
 * Copyright 2008 Tungsten Graphics, Inc., Cedar Park, Texas.
 * All Rights Reserved.
 *
 **************************************************************************/


/**
 * Code to convert compressed/paletted texture images to ordinary 4-byte RGBA.
 * See the GL_OES_compressed_paletted_texture spec at
 * http://khronos.org/registry/gles/extensions/OES/OES_compressed_paletted_texture.txt
 */


#include <stdlib.h>
#include <assert.h>
#include "GLES/gl.h"
#include "GLES/glext.h"


void GL_APIENTRY _es_CompressedTexImage2D(GLenum target, GLint level, GLenum internalFormat, GLsizei width, GLsizei height, GLint border, GLsizei imageSize, const GLvoid *data);

void GL_APIENTRY _mesa_TexImage2D(GLenum target, GLint level, GLint internalFormat, GLsizei width, GLsizei height, GLint border, GLenum format, GLenum type, const GLvoid *pixels);
void GL_APIENTRY _mesa_CompressedTexImage2DARB(GLenum target, GLint level, GLenum internalFormat, GLsizei width, GLsizei height, GLint border, GLsizei imageSize, const GLvoid *data);


static const struct {
   GLenum format;
   GLuint palette_size;
   GLuint size;
} formats[] = {
   { GL_PALETTE4_RGB8_OES,      16, 3 },
   { GL_PALETTE4_RGBA8_OES,     16, 4 },
   { GL_PALETTE4_R5_G6_B5_OES,  16, 2 },
   { GL_PALETTE4_RGBA4_OES,     16, 2 },
   { GL_PALETTE4_RGB5_A1_OES,   16, 2 },
   { GL_PALETTE8_RGB8_OES,     256, 3 },
   { GL_PALETTE8_RGBA8_OES,    256, 4 },
   { GL_PALETTE8_R5_G6_B5_OES, 256, 2 },
   { GL_PALETTE8_RGBA4_OES,    256, 2 },
   { GL_PALETTE8_RGB5_A1_OES,  256, 2 }
};


/**
 * Get a color/entry from the palette.  Convert to GLubyte/RGBA format.
 */
static void
get_palette_entry(GLenum format, const void *palette, GLuint index,
                  GLubyte rgba[4])
{
   switch (format) {
   case GL_PALETTE4_RGB8_OES:
   case GL_PALETTE8_RGB8_OES:
      {
         const GLubyte *pal = (const GLubyte *) palette;
         rgba[0] = pal[index * 3 + 0];
         rgba[1] = pal[index * 3 + 1];
         rgba[2] = pal[index * 3 + 2];
         rgba[3] = 255;
      }
      break;
   case GL_PALETTE4_RGBA8_OES:
   case GL_PALETTE8_RGBA8_OES:
      {
         const GLubyte *pal = (const GLubyte *) palette;
         rgba[0] = pal[index * 4 + 0];
         rgba[1] = pal[index * 4 + 1];
         rgba[2] = pal[index * 4 + 2];
         rgba[3] = pal[index * 4 + 3];
      }
      break;
   case GL_PALETTE4_R5_G6_B5_OES:
   case GL_PALETTE8_R5_G6_B5_OES:
      {
         const GLushort *pal = (const GLushort *) palette;
         const GLushort color = pal[index];
         rgba[0] = ((color >> 8) & 0xf8) | ((color >> 11) & 0x3);
         rgba[1] = ((color >> 3) & 0xfc) | ((color >> 1 ) & 0x3);
         rgba[2] = ((color << 3) & 0xf8) | ((color      ) & 0x7);
         rgba[3] = 255;
      }
      break;
   case GL_PALETTE4_RGBA4_OES:
   case GL_PALETTE8_RGBA4_OES:
      {
         const GLushort *pal = (const GLushort *) palette;
         const GLushort color = pal[index];
         rgba[0] = ((color & 0xf000) >> 8) | ((color & 0xf000) >> 12);
         rgba[1] = ((color & 0x0f00) >> 4) | ((color & 0x0f00) >>  8);
         rgba[2] = ((color & 0x00f0)     ) | ((color & 0x00f0) >>  4);
         rgba[3] = ((color & 0x000f) << 4) | ((color & 0x000f)      );
      }
      break;
   case GL_PALETTE4_RGB5_A1_OES:
   case GL_PALETTE8_RGB5_A1_OES:
      {
         const GLushort *pal = (const GLushort *) palette;
         const GLushort color = pal[index];
         rgba[0] = ((color >> 8) & 0xf8) | ((color >> 11) & 0x7);
         rgba[1] = ((color >> 3) & 0xf8) | ((color >>  6) & 0x7);
         rgba[2] = ((color << 2) & 0xf8) | ((color >>  1) & 0x7);
         rgba[3] = (color & 0x1) * 255;
      }
      break;
   default:
      assert(0);
   }
}


/**
 * Convert paletted texture to simple GLubyte/RGBA format.
 */
static void
paletted_to_rgba(GLenum src_format,
                 const void *palette,
                 const void *indexes,
                 GLsizei width, GLsizei height,
                 GLubyte *rgba)
{
   GLuint pal_ents, i;

   assert(src_format >= GL_PALETTE4_RGB8_OES);
   assert(src_format <= GL_PALETTE8_RGB5_A1_OES);
   assert(formats[src_format - GL_PALETTE4_RGB8_OES].format == src_format);

   pal_ents = formats[src_format - GL_PALETTE4_RGB8_OES].palette_size;

   if (pal_ents == 16) {
      /* 4 bits per index */
      const GLubyte *ind = (const GLubyte *) indexes;

      if (width * height == 1) {
         /* special case the only odd-sized image */
         GLuint index0 = ind[0] >> 4;
         get_palette_entry(src_format, palette, index0, rgba);
         return;
      }
      /* two pixels per iteration */
      for (i = 0; i < width * height / 2; i++) {
         GLuint index0 = ind[i] >> 4;
         GLuint index1 = ind[i] & 0xf;
         get_palette_entry(src_format, palette, index0, rgba + i * 8);
         get_palette_entry(src_format, palette, index1, rgba + i * 8 + 4);
      }
   }
   else {
      /* 8 bits per index */
      const GLubyte *ind = (const GLubyte *) indexes;
      for (i = 0; i < width * height; i++) {
         GLuint index = ind[i];
         get_palette_entry(src_format, palette, index, rgba + i * 4);
      }
   }
}


/**
 * Convert a call to glCompressedTexImage2D() where internalFormat is a
 *  compressed palette format into a regular GLubyte/RGBA glTexImage2D() call.
 */
static void
cpal_compressed_teximage2d(GLenum target, GLint level,
                           GLenum internalFormat,
                           GLsizei width, GLsizei height,
                           const void *pixels)
{
   GLuint pal_ents, pal_ent_size, pal_bytes;
   const GLint num_levels = level + 1;
   GLint lvl;
   const GLubyte *indexes;

   assert(internalFormat >= GL_PALETTE4_RGB8_OES);
   assert(internalFormat <= GL_PALETTE8_RGB5_A1_OES);
   assert(formats[internalFormat - GL_PALETTE4_RGB8_OES].format == internalFormat);

   pal_ents = formats[internalFormat - GL_PALETTE4_RGB8_OES].palette_size;
   pal_ent_size = formats[internalFormat - GL_PALETTE4_RGB8_OES].size;
   pal_bytes = pal_ents * pal_ent_size;

   /* first image follows the palette */
   indexes = (const GLubyte *) pixels + pal_bytes;

   /* No worries about glPixelStore state since the only supported parameter is
    * GL_UNPACK_ALIGNMENT and it doesn't matter when unpacking GLubyte/RGBA.
    */

   for (lvl = 0; lvl < num_levels; lvl++) {
      /* Allocate GLubyte/RGBA dest image buffer */
      GLubyte *rgba = (GLubyte *) malloc(width * height * 4);

      if (pixels)
         paletted_to_rgba(internalFormat, pixels, indexes, width, height, rgba);

      _mesa_TexImage2D(target, lvl, GL_RGBA, width, height, 0,
                       GL_RGBA, GL_UNSIGNED_BYTE, rgba);

      free(rgba);

      /* advance index pointer to point to next src mipmap */
      if (pal_ents == 4)
         indexes += width * height / 2;
      else
         indexes += width * height;

      /* next mipmap level size */
      if (width > 1)
         width /= 2;
      if (height > 1)
         height /= 2;
   }
}


void GL_APIENTRY
_es_CompressedTexImage2D(GLenum target, GLint level, GLenum internalFormat,
                       GLsizei width, GLsizei height, GLint border,
                       GLsizei imageSize, const GLvoid *data)
{
   switch (internalFormat) {
   case GL_PALETTE4_RGB8_OES:
   case GL_PALETTE4_RGBA8_OES:
   case GL_PALETTE4_R5_G6_B5_OES:
   case GL_PALETTE4_RGBA4_OES:
   case GL_PALETTE4_RGB5_A1_OES:
   case GL_PALETTE8_RGB8_OES:
   case GL_PALETTE8_RGBA8_OES:
   case GL_PALETTE8_R5_G6_B5_OES:
   case GL_PALETTE8_RGBA4_OES:
   case GL_PALETTE8_RGB5_A1_OES:
      cpal_compressed_teximage2d(target, level, internalFormat,
                                 width, height, data);
      break;
   default:
      _mesa_CompressedTexImage2DARB(target, level, internalFormat,
                                    width, height, border, imageSize, data);
   }
}