2 * Mesa 3-D graphics library
4 * Copyright 2008 VMware, Inc.
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...
38 #include "pixelstore.h"
39 #include "texcompress_cpal.h"
43 static const struct cpal_format_info
{
50 { GL_PALETTE4_RGB8_OES
, GL_RGB
, GL_UNSIGNED_BYTE
, 16, 3 },
51 { GL_PALETTE4_RGBA8_OES
, GL_RGBA
, GL_UNSIGNED_BYTE
, 16, 4 },
52 { GL_PALETTE4_R5_G6_B5_OES
, GL_RGB
, GL_UNSIGNED_SHORT_5_6_5
, 16, 2 },
53 { GL_PALETTE4_RGBA4_OES
, GL_RGBA
, GL_UNSIGNED_SHORT_4_4_4_4
, 16, 2 },
54 { GL_PALETTE4_RGB5_A1_OES
, GL_RGBA
, GL_UNSIGNED_SHORT_5_5_5_1
, 16, 2 },
55 { GL_PALETTE8_RGB8_OES
, GL_RGB
, GL_UNSIGNED_BYTE
, 256, 3 },
56 { GL_PALETTE8_RGBA8_OES
, GL_RGBA
, GL_UNSIGNED_BYTE
, 256, 4 },
57 { GL_PALETTE8_R5_G6_B5_OES
, GL_RGB
, GL_UNSIGNED_SHORT_5_6_5
, 256, 2 },
58 { GL_PALETTE8_RGBA4_OES
, GL_RGBA
, GL_UNSIGNED_SHORT_4_4_4_4
, 256, 2 },
59 { GL_PALETTE8_RGB5_A1_OES
, GL_RGBA
, GL_UNSIGNED_SHORT_5_5_5_1
, 256, 2 }
64 * Get a color/entry from the palette.
67 get_palette_entry(const struct cpal_format_info
*info
, const GLubyte
*palette
,
68 GLuint index
, GLubyte
*pixel
)
70 memcpy(pixel
, palette
+ info
->size
* index
, info
->size
);
76 * Convert paletted texture to color texture.
79 paletted_to_color(const struct cpal_format_info
*info
, const GLubyte
*palette
,
80 const void *indices
, GLuint num_pixels
, GLubyte
*image
)
85 if (info
->palette_size
== 16) {
86 /* 4 bits per index */
87 const GLubyte
*ind
= (const GLubyte
*) indices
;
89 /* two pixels per iteration */
90 remain
= num_pixels
% 2;
91 for (i
= 0; i
< num_pixels
/ 2; i
++) {
92 pix
+= get_palette_entry(info
, palette
, (ind
[i
] >> 4) & 0xf, pix
);
93 pix
+= get_palette_entry(info
, palette
, ind
[i
] & 0xf, pix
);
96 get_palette_entry(info
, palette
, (ind
[i
] >> 4) & 0xf, pix
);
100 /* 8 bits per index */
101 const GLubyte
*ind
= (const GLubyte
*) indices
;
102 for (i
= 0; i
< num_pixels
; i
++)
103 pix
+= get_palette_entry(info
, palette
, ind
[i
], pix
);
108 _mesa_cpal_compressed_size(int level
, GLenum internalFormat
,
109 unsigned width
, unsigned height
)
111 const struct cpal_format_info
*info
;
112 const int num_levels
= -level
+ 1;
114 unsigned w
, h
, expect_size
;
116 if (internalFormat
< GL_PALETTE4_RGB8_OES
117 || internalFormat
> GL_PALETTE8_RGB5_A1_OES
) {
121 info
= &formats
[internalFormat
- GL_PALETTE4_RGB8_OES
];
122 assert(info
->cpal_format
== internalFormat
);
124 expect_size
= info
->palette_size
* info
->size
;
125 for (lvl
= 0; lvl
< num_levels
; lvl
++) {
133 if (info
->palette_size
== 16)
134 expect_size
+= (w
* h
+ 1) / 2;
136 expect_size
+= w
* h
;
144 * Convert a call to glCompressedTexImage2D() where internalFormat is a
145 * compressed palette format into a regular GLubyte/RGBA glTexImage2D() call.
148 _mesa_cpal_compressed_teximage2d(GLenum target
, GLint level
,
149 GLenum internalFormat
,
150 GLsizei width
, GLsizei height
,
151 GLsizei imageSize
, const void *palette
)
153 const struct cpal_format_info
*info
;
154 GLint lvl
, num_levels
;
155 const GLubyte
*indices
;
156 GLint saved_align
, align
;
157 GET_CURRENT_CONTEXT(ctx
);
159 /* By this point, the internalFormat should have been validated.
161 assert(internalFormat
>= GL_PALETTE4_RGB8_OES
162 && internalFormat
<= GL_PALETTE8_RGB5_A1_OES
);
164 info
= &formats
[internalFormat
- GL_PALETTE4_RGB8_OES
];
166 num_levels
= -level
+ 1;
168 /* first image follows the palette */
169 indices
= (const GLubyte
*) palette
+ info
->palette_size
* info
->size
;
171 saved_align
= ctx
->Unpack
.Alignment
;
174 for (lvl
= 0; lvl
< num_levels
; lvl
++) {
177 GLubyte
*image
= NULL
;
186 if (w
* info
->size
% align
) {
187 _mesa_PixelStorei(GL_UNPACK_ALIGNMENT
, 1);
191 /* allocate and fill dest image buffer */
193 image
= malloc(num_texels
* info
->size
);
194 paletted_to_color(info
, palette
, indices
, num_texels
, image
);
197 _mesa_TexImage2D(target
, lvl
, info
->format
, w
, h
, 0,
198 info
->format
, info
->type
, image
);
201 /* advance index pointer to point to next src mipmap */
202 if (info
->palette_size
== 16)
203 indices
+= (num_texels
+ 1) / 2;
205 indices
+= num_texels
;
208 if (saved_align
!= align
)
209 _mesa_PixelStorei(GL_UNPACK_ALIGNMENT
, saved_align
);