5 * Copyright (C) 2004 Roland Scheidegger All Rights Reserved.
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the "Software"),
9 * to deal in the Software without restriction, including without limitation
10 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11 * and/or sell copies of the Software, and to permit persons to whom the
12 * Software is furnished to do so, subject to the following conditions:
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
21 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28 #define EXP5TO8R(packedcol) \
29 ((((packedcol) >> 8) & 0xf8) | (((packedcol) >> 13) & 0x7))
31 #define EXP6TO8G(packedcol) \
32 ((((packedcol) >> 3) & 0xfc) | (((packedcol) >> 9) & 0x3))
34 #define EXP5TO8B(packedcol) \
35 ((((packedcol) << 3) & 0xf8) | (((packedcol) >> 2) & 0x7))
37 #define EXP4TO8(col) \
38 ((col) | ((col) << 4))
40 /* inefficient. To be efficient, it would be necessary to decode 16 pixels at once */
42 static void dxt135_decode_imageblock ( const GLubyte
*img_block_src
,
43 GLint i
, GLint j
, GLuint dxt_type
, GLvoid
*texel
) {
44 GLchan
*rgba
= (GLchan
*) texel
;
45 const GLushort color0
= img_block_src
[0] | (img_block_src
[1] << 8);
46 const GLushort color1
= img_block_src
[2] | (img_block_src
[3] << 8);
47 const GLuint bits
= img_block_src
[4] | (img_block_src
[5] << 8) |
48 (img_block_src
[6] << 16) | (img_block_src
[7] << 24);
49 /* What about big/little endian? */
50 GLubyte bit_pos
= 2 * (j
* 4 + i
) ;
51 GLubyte code
= (GLubyte
) ((bits
>> bit_pos
) & 3);
53 rgba
[ACOMP
] = CHAN_MAX
;
56 rgba
[RCOMP
] = UBYTE_TO_CHAN( EXP5TO8R(color0
) );
57 rgba
[GCOMP
] = UBYTE_TO_CHAN( EXP6TO8G(color0
) );
58 rgba
[BCOMP
] = UBYTE_TO_CHAN( EXP5TO8B(color0
) );
61 rgba
[RCOMP
] = UBYTE_TO_CHAN( EXP5TO8R(color1
) );
62 rgba
[GCOMP
] = UBYTE_TO_CHAN( EXP6TO8G(color1
) );
63 rgba
[BCOMP
] = UBYTE_TO_CHAN( EXP5TO8B(color1
) );
66 if ((dxt_type
> 1) || (color0
> color1
)) {
67 rgba
[RCOMP
] = UBYTE_TO_CHAN( ((EXP5TO8R(color0
) * 2 + EXP5TO8R(color1
)) / 3) );
68 rgba
[GCOMP
] = UBYTE_TO_CHAN( ((EXP6TO8G(color0
) * 2 + EXP6TO8G(color1
)) / 3) );
69 rgba
[BCOMP
] = UBYTE_TO_CHAN( ((EXP5TO8B(color0
) * 2 + EXP5TO8B(color1
)) / 3) );
72 rgba
[RCOMP
] = UBYTE_TO_CHAN( ((EXP5TO8R(color0
) + EXP5TO8R(color1
)) / 2) );
73 rgba
[GCOMP
] = UBYTE_TO_CHAN( ((EXP6TO8G(color0
) + EXP6TO8G(color1
)) / 2) );
74 rgba
[BCOMP
] = UBYTE_TO_CHAN( ((EXP5TO8B(color0
) + EXP5TO8B(color1
)) / 2) );
78 if ((dxt_type
> 1) || (color0
> color1
)) {
79 rgba
[RCOMP
] = UBYTE_TO_CHAN( ((EXP5TO8R(color0
) + EXP5TO8R(color1
) * 2) / 3) );
80 rgba
[GCOMP
] = UBYTE_TO_CHAN( ((EXP6TO8G(color0
) + EXP6TO8G(color1
) * 2) / 3) );
81 rgba
[BCOMP
] = UBYTE_TO_CHAN( ((EXP5TO8B(color0
) + EXP5TO8B(color1
) * 2) / 3) );
87 if (dxt_type
== 1) rgba
[ACOMP
] = UBYTE_TO_CHAN(0);
91 /* CANNOT happen (I hope) */
97 void fetch_2d_texel_rgb_dxt1(GLint srcRowStride
, const GLubyte
*pixdata
,
98 GLint i
, GLint j
, GLvoid
*texel
)
100 /* Extract the (i,j) pixel from pixdata and return it
101 * in texel[RCOMP], texel[GCOMP], texel[BCOMP], texel[ACOMP].
104 const GLubyte
*blksrc
= (pixdata
+ ((srcRowStride
+ 3) / 4 * (j
/ 4) + (i
/ 4)) * 8);
105 dxt135_decode_imageblock(blksrc
, (i
&3), (j
&3), 0, texel
);
109 void fetch_2d_texel_rgba_dxt1(GLint srcRowStride
, const GLubyte
*pixdata
,
110 GLint i
, GLint j
, GLvoid
*texel
)
112 /* Extract the (i,j) pixel from pixdata and return it
113 * in texel[RCOMP], texel[GCOMP], texel[BCOMP], texel[ACOMP].
116 const GLubyte
*blksrc
= (pixdata
+ ((srcRowStride
+ 3) / 4 * (j
/ 4) + (i
/ 4)) * 8);
117 dxt135_decode_imageblock(blksrc
, (i
&3), (j
&3), 1, texel
);
120 void fetch_2d_texel_rgba_dxt3(GLint srcRowStride
, const GLubyte
*pixdata
,
121 GLint i
, GLint j
, GLvoid
*texel
) {
123 /* Extract the (i,j) pixel from pixdata and return it
124 * in texel[RCOMP], texel[GCOMP], texel[BCOMP], texel[ACOMP].
127 GLchan
*rgba
= (GLchan
*) texel
;
128 const GLubyte
*blksrc
= (pixdata
+ ((srcRowStride
+ 3) / 4 * (j
/ 4) + (i
/ 4)) * 16);
130 /* Simple 32bit version. */
131 /* that's pretty brain-dead for a single pixel, isn't it? */
132 const GLubyte bit_pos
= 4 * ((j
&3) * 4 + (i
&3));
133 const GLuint alpha_low
= blksrc
[0] | (blksrc
[1] << 8) | (blksrc
[2] << 16) | (blksrc
[3] << 24);
134 const GLuint alpha_high
= blksrc
[4] | (blksrc
[5] << 8) | (blksrc
[6] << 16) | (blksrc
[7] << 24);
136 dxt135_decode_imageblock(blksrc
+ 8, (i
&3), (j
&3), 2, texel
);
138 rgba
[ACOMP
] = UBYTE_TO_CHAN( (GLubyte
)(EXP4TO8((alpha_low
>> bit_pos
) & 15)) );
140 rgba
[ACOMP
] = UBYTE_TO_CHAN( (GLubyte
)(EXP4TO8((alpha_high
>> (bit_pos
- 32)) & 15)) );
143 /* TODO test this! */
144 const GLubyte anibble
= (blksrc
[((j
&3) * 4 + (i
&3)) / 2] >> (4 * (i
&1))) & 0xf;
145 dxt135_decode_imageblock(blksrc
+ 8, (i
&3), (j
&3), 2, texel
);
146 rgba
[ACOMP
] = UBYTE_TO_CHAN( (GLubyte
)(EXP4TO8(anibble
)) );
151 void fetch_2d_texel_rgba_dxt5(GLint srcRowStride
, const GLubyte
*pixdata
,
152 GLint i
, GLint j
, GLvoid
*texel
) {
154 /* Extract the (i,j) pixel from pixdata and return it
155 * in texel[RCOMP], texel[GCOMP], texel[BCOMP], texel[ACOMP].
158 GLchan
*rgba
= (GLchan
*) texel
;
159 const GLubyte
*blksrc
= (pixdata
+ ((srcRowStride
+ 3) / 4 * (j
/ 4) + (i
/ 4)) * 16);
160 const GLubyte alpha0
= blksrc
[0];
161 const GLubyte alpha1
= blksrc
[1];
163 const GLubyte bit_pos
= 3 * ((j
&3) * 4 + (i
&3));
164 /* simple 32bit version */
165 const GLuint bits_low
= blksrc
[2] | (blksrc
[3] << 8) | (blksrc
[4] << 16) | (blksrc
[5] << 24);
166 const GLuint bits_high
= blksrc
[6] | (blksrc
[7] << 8);
170 code
= (GLubyte
) ((bits_low
>> bit_pos
) & 7);
171 else if (bit_pos
== 30)
172 code
= (GLubyte
) ((bits_low
>> 30) & 3) | ((bits_high
<< 2) & 4);
174 code
= (GLubyte
) ((bits_high
>> (bit_pos
- 32)) & 7);
177 /* TODO test this! */
178 const GLubyte bit_pos
= ((j
&3) * 4 + (i
&3)) * 3;
179 const GLubyte acodelow
= blksrc
[2 + bit_pos
/ 8];
180 const GLubyte acodehigh
= blksrc
[3 + bit_pos
/ 8];
181 const GLubyte code
= (acodelow
>> (bit_pos
& 0x7) |
182 (acodehigh
<< (8 - (bit_pos
& 0x7)))) & 0x7;
184 dxt135_decode_imageblock(blksrc
+ 8, (i
&3), (j
&3), 2, texel
);
186 if (alpha0
> alpha1
) {
189 rgba
[ACOMP
] = UBYTE_TO_CHAN( alpha0
);
192 rgba
[ACOMP
] = UBYTE_TO_CHAN( alpha1
);
200 rgba
[ACOMP
] = UBYTE_TO_CHAN( ((alpha0
* (8 - code
) + (alpha1
* (code
- 1))) / 7) );
207 rgba
[ACOMP
] = UBYTE_TO_CHAN( alpha0
);
210 rgba
[ACOMP
] = UBYTE_TO_CHAN( alpha1
);
216 rgba
[ACOMP
] = UBYTE_TO_CHAN( ((alpha0
* (6 - code
) + (alpha1
* (code
- 1))) / 5) );
222 rgba
[ACOMP
] = CHAN_MAX
;
227 /* not sure. Which version is faster? */
231 rgba
[ACOMP
] = UBYTE_TO_CHAN( alpha0
);
233 rgba
[ACOMP
] = UBYTE_TO_CHAN( alpha1
);
234 else if (alpha0
> alpha1
)
235 rgba
[ACOMP
] = UBYTE_TO_CHAN( ((alpha0
* (8 - code
) + (alpha1
* (code
- 1))) / 7) );
237 rgba
[ACOMP
] = UBYTE_TO_CHAN( ((alpha0
* (6 - code
) + (alpha1
* (code
- 1))) / 5) );
241 rgba
[ACOMP
] = CHAN_MAX
;