2 * Mesa 3-D graphics library
4 * Copyright 2008 Tungsten Graphics, Inc., Cedar Park, Texas.
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included
14 * in all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
20 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
21 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 * Code to convert compressed/paletted texture images to ordinary images.
26 * See the GL_OES_compressed_paletted_texture spec at
27 * http://khronos.org/registry/gles/extensions/OES/OES_compressed_paletted_texture.txt
29 * XXX this makes it impossible to add hardware support...
34 #include "compiler.h" /* for ASSERT */
36 #include "mfeatures.h"
39 #include "pixelstore.h"
40 #include "texcompress_cpal.h"
46 static const struct cpal_format_info
{
53 { GL_PALETTE4_RGB8_OES
, GL_RGB
, GL_UNSIGNED_BYTE
, 16, 3 },
54 { GL_PALETTE4_RGBA8_OES
, GL_RGBA
, GL_UNSIGNED_BYTE
, 16, 4 },
55 { GL_PALETTE4_R5_G6_B5_OES
, GL_RGB
, GL_UNSIGNED_SHORT_5_6_5
, 16, 2 },
56 { GL_PALETTE4_RGBA4_OES
, GL_RGBA
, GL_UNSIGNED_SHORT_4_4_4_4
, 16, 2 },
57 { GL_PALETTE4_RGB5_A1_OES
, GL_RGBA
, GL_UNSIGNED_SHORT_5_5_5_1
, 16, 2 },
58 { GL_PALETTE8_RGB8_OES
, GL_RGB
, GL_UNSIGNED_BYTE
, 256, 3 },
59 { GL_PALETTE8_RGBA8_OES
, GL_RGBA
, GL_UNSIGNED_BYTE
, 256, 4 },
60 { GL_PALETTE8_R5_G6_B5_OES
, GL_RGB
, GL_UNSIGNED_SHORT_5_6_5
, 256, 2 },
61 { GL_PALETTE8_RGBA4_OES
, GL_RGBA
, GL_UNSIGNED_SHORT_4_4_4_4
, 256, 2 },
62 { GL_PALETTE8_RGB5_A1_OES
, GL_RGBA
, GL_UNSIGNED_SHORT_5_5_5_1
, 256, 2 }
67 * Get a color/entry from the palette.
70 get_palette_entry(const struct cpal_format_info
*info
, const GLubyte
*palette
,
71 GLuint index
, GLubyte
*pixel
)
73 memcpy(pixel
, palette
+ info
->size
* index
, info
->size
);
79 * Convert paletted texture to color texture.
82 paletted_to_color(const struct cpal_format_info
*info
, const GLubyte
*palette
,
83 const void *indices
, GLuint num_pixels
, GLubyte
*image
)
88 if (info
->palette_size
== 16) {
89 /* 4 bits per index */
90 const GLubyte
*ind
= (const GLubyte
*) indices
;
92 /* two pixels per iteration */
93 remain
= num_pixels
% 2;
94 for (i
= 0; i
< num_pixels
/ 2; i
++) {
95 pix
+= get_palette_entry(info
, palette
, (ind
[i
] >> 4) & 0xf, pix
);
96 pix
+= get_palette_entry(info
, palette
, ind
[i
] & 0xf, pix
);
99 get_palette_entry(info
, palette
, (ind
[i
] >> 4) & 0xf, pix
);
103 /* 8 bits per index */
104 const GLubyte
*ind
= (const GLubyte
*) indices
;
105 for (i
= 0; i
< num_pixels
; i
++)
106 pix
+= get_palette_entry(info
, palette
, ind
[i
], pix
);
111 _mesa_cpal_compressed_size(int level
, GLenum internalFormat
,
112 unsigned width
, unsigned height
)
114 const struct cpal_format_info
*info
;
115 const int num_levels
= -level
+ 1;
117 unsigned w
, h
, expect_size
;
119 if (internalFormat
< GL_PALETTE4_RGB8_OES
120 || internalFormat
> GL_PALETTE8_RGB5_A1_OES
) {
124 info
= &formats
[internalFormat
- GL_PALETTE4_RGB8_OES
];
125 ASSERT(info
->cpal_format
== internalFormat
);
127 expect_size
= info
->palette_size
* info
->size
;
128 for (lvl
= 0; lvl
< num_levels
; lvl
++) {
136 if (info
->palette_size
== 16)
137 expect_size
+= (w
* h
+ 1) / 2;
139 expect_size
+= w
* h
;
146 _mesa_cpal_compressed_format_type(GLenum internalFormat
, GLenum
*format
,
149 const struct cpal_format_info
*info
;
151 if (internalFormat
< GL_PALETTE4_RGB8_OES
152 || internalFormat
> GL_PALETTE8_RGB5_A1_OES
) {
156 info
= &formats
[internalFormat
- GL_PALETTE4_RGB8_OES
];
157 *format
= info
->format
;
162 * Convert a call to glCompressedTexImage2D() where internalFormat is a
163 * compressed palette format into a regular GLubyte/RGBA glTexImage2D() call.
166 _mesa_cpal_compressed_teximage2d(GLenum target
, GLint level
,
167 GLenum internalFormat
,
168 GLsizei width
, GLsizei height
,
169 GLsizei imageSize
, const void *palette
)
171 const struct cpal_format_info
*info
;
172 GLint lvl
, num_levels
;
173 const GLubyte
*indices
;
174 GLint saved_align
, align
;
175 GET_CURRENT_CONTEXT(ctx
);
177 /* By this point, the internalFormat should have been validated.
179 assert(internalFormat
>= GL_PALETTE4_RGB8_OES
180 && internalFormat
<= GL_PALETTE8_RGB5_A1_OES
);
182 info
= &formats
[internalFormat
- GL_PALETTE4_RGB8_OES
];
184 num_levels
= -level
+ 1;
186 /* first image follows the palette */
187 indices
= (const GLubyte
*) palette
+ info
->palette_size
* info
->size
;
189 saved_align
= ctx
->Unpack
.Alignment
;
192 for (lvl
= 0; lvl
< num_levels
; lvl
++) {
195 GLubyte
*image
= NULL
;
204 if (w
* info
->size
% align
) {
205 _mesa_PixelStorei(GL_UNPACK_ALIGNMENT
, 1);
209 /* allocate and fill dest image buffer */
211 image
= malloc(num_texels
* info
->size
);
212 paletted_to_color(info
, palette
, indices
, num_texels
, image
);
215 _mesa_TexImage2D(target
, lvl
, info
->format
, w
, h
, 0,
216 info
->format
, info
->type
, image
);
220 /* advance index pointer to point to next src mipmap */
221 if (info
->palette_size
== 16)
222 indices
+= (num_texels
+ 1) / 2;
224 indices
+= num_texels
;
227 if (saved_align
!= align
)
228 _mesa_PixelStorei(GL_UNPACK_ALIGNMENT
, saved_align
);