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 OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
20 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22 * OTHER DEALINGS IN THE SOFTWARE.
26 * Code to convert compressed/paletted texture images to ordinary images.
27 * See the GL_OES_compressed_paletted_texture spec at
28 * http://khronos.org/registry/gles/extensions/OES/OES_compressed_paletted_texture.txt
30 * XXX this makes it impossible to add hardware support...
35 #include "compiler.h" /* for ASSERT */
39 #include "pixelstore.h"
40 #include "texcompress_cpal.h"
44 static const struct cpal_format_info
{
51 { GL_PALETTE4_RGB8_OES
, GL_RGB
, GL_UNSIGNED_BYTE
, 16, 3 },
52 { GL_PALETTE4_RGBA8_OES
, GL_RGBA
, GL_UNSIGNED_BYTE
, 16, 4 },
53 { GL_PALETTE4_R5_G6_B5_OES
, GL_RGB
, GL_UNSIGNED_SHORT_5_6_5
, 16, 2 },
54 { GL_PALETTE4_RGBA4_OES
, GL_RGBA
, GL_UNSIGNED_SHORT_4_4_4_4
, 16, 2 },
55 { GL_PALETTE4_RGB5_A1_OES
, GL_RGBA
, GL_UNSIGNED_SHORT_5_5_5_1
, 16, 2 },
56 { GL_PALETTE8_RGB8_OES
, GL_RGB
, GL_UNSIGNED_BYTE
, 256, 3 },
57 { GL_PALETTE8_RGBA8_OES
, GL_RGBA
, GL_UNSIGNED_BYTE
, 256, 4 },
58 { GL_PALETTE8_R5_G6_B5_OES
, GL_RGB
, GL_UNSIGNED_SHORT_5_6_5
, 256, 2 },
59 { GL_PALETTE8_RGBA4_OES
, GL_RGBA
, GL_UNSIGNED_SHORT_4_4_4_4
, 256, 2 },
60 { GL_PALETTE8_RGB5_A1_OES
, GL_RGBA
, GL_UNSIGNED_SHORT_5_5_5_1
, 256, 2 }
65 * Get a color/entry from the palette.
68 get_palette_entry(const struct cpal_format_info
*info
, const GLubyte
*palette
,
69 GLuint index
, GLubyte
*pixel
)
71 memcpy(pixel
, palette
+ info
->size
* index
, info
->size
);
77 * Convert paletted texture to color texture.
80 paletted_to_color(const struct cpal_format_info
*info
, const GLubyte
*palette
,
81 const void *indices
, GLuint num_pixels
, GLubyte
*image
)
86 if (info
->palette_size
== 16) {
87 /* 4 bits per index */
88 const GLubyte
*ind
= (const GLubyte
*) indices
;
90 /* two pixels per iteration */
91 remain
= num_pixels
% 2;
92 for (i
= 0; i
< num_pixels
/ 2; i
++) {
93 pix
+= get_palette_entry(info
, palette
, (ind
[i
] >> 4) & 0xf, pix
);
94 pix
+= get_palette_entry(info
, palette
, ind
[i
] & 0xf, pix
);
97 get_palette_entry(info
, palette
, (ind
[i
] >> 4) & 0xf, pix
);
101 /* 8 bits per index */
102 const GLubyte
*ind
= (const GLubyte
*) indices
;
103 for (i
= 0; i
< num_pixels
; i
++)
104 pix
+= get_palette_entry(info
, palette
, ind
[i
], pix
);
109 _mesa_cpal_compressed_size(int level
, GLenum internalFormat
,
110 unsigned width
, unsigned height
)
112 const struct cpal_format_info
*info
;
113 const int num_levels
= -level
+ 1;
115 unsigned w
, h
, expect_size
;
117 if (internalFormat
< GL_PALETTE4_RGB8_OES
118 || internalFormat
> GL_PALETTE8_RGB5_A1_OES
) {
122 info
= &formats
[internalFormat
- GL_PALETTE4_RGB8_OES
];
123 ASSERT(info
->cpal_format
== internalFormat
);
125 expect_size
= info
->palette_size
* info
->size
;
126 for (lvl
= 0; lvl
< num_levels
; lvl
++) {
134 if (info
->palette_size
== 16)
135 expect_size
+= (w
* h
+ 1) / 2;
137 expect_size
+= w
* h
;
145 * Convert a call to glCompressedTexImage2D() where internalFormat is a
146 * compressed palette format into a regular GLubyte/RGBA glTexImage2D() call.
149 _mesa_cpal_compressed_teximage2d(GLenum target
, GLint level
,
150 GLenum internalFormat
,
151 GLsizei width
, GLsizei height
,
152 GLsizei imageSize
, const void *palette
)
154 const struct cpal_format_info
*info
;
155 GLint lvl
, num_levels
;
156 const GLubyte
*indices
;
157 GLint saved_align
, align
;
158 GET_CURRENT_CONTEXT(ctx
);
160 /* By this point, the internalFormat should have been validated.
162 assert(internalFormat
>= GL_PALETTE4_RGB8_OES
163 && internalFormat
<= GL_PALETTE8_RGB5_A1_OES
);
165 info
= &formats
[internalFormat
- GL_PALETTE4_RGB8_OES
];
167 num_levels
= -level
+ 1;
169 /* first image follows the palette */
170 indices
= (const GLubyte
*) palette
+ info
->palette_size
* info
->size
;
172 saved_align
= ctx
->Unpack
.Alignment
;
175 for (lvl
= 0; lvl
< num_levels
; lvl
++) {
178 GLubyte
*image
= NULL
;
187 if (w
* info
->size
% align
) {
188 _mesa_PixelStorei(GL_UNPACK_ALIGNMENT
, 1);
192 /* allocate and fill dest image buffer */
194 image
= malloc(num_texels
* info
->size
);
195 paletted_to_color(info
, palette
, indices
, num_texels
, image
);
198 _mesa_TexImage2D(target
, lvl
, info
->format
, w
, h
, 0,
199 info
->format
, info
->type
, image
);
202 /* advance index pointer to point to next src mipmap */
203 if (info
->palette_size
== 16)
204 indices
+= (num_texels
+ 1) / 2;
206 indices
+= num_texels
;
209 if (saved_align
!= align
)
210 _mesa_PixelStorei(GL_UNPACK_ALIGNMENT
, saved_align
);