root/OpenSceneGraph/trunk/src/osg/dxtctool.h @ 3301

Revision 3301, 9.9 kB (checked in by robert, 10 years ago)

Rejigged the int* definitions to try and get round Mingw compile errors

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1// dxtctool.h: interface for the DXTC tools.
2//
3//////////////////////////////////////////////////////////////////////
4//
5//  Copyright (C) 2002 Tanguy Fautré.
6//
7//  This software is provided 'as-is', without any express or implied
8//  warranty.  In no event will the authors be held liable for any damages
9//  arising from the use of this software.
10//
11//  Permission is granted to anyone to use this software for any purpose,
12//  including commercial applications, and to alter it and redistribute it
13//  freely, subject to the following restrictions:
14//
15//  1. The origin of this software must not be misrepresented; you must not
16//     claim that you wrote the original software. If you use this software
17//     in a product, an acknowledgment in the product documentation would be
18//     appreciated but is not required.
19//  2. Altered source versions must be plainly marked as such, and must not be
20//     misrepresented as being the original software.
21//  3. This notice may not be removed or altered from any source distribution.
22//
23//  Tanguy Fautré
24//  softdev@telenet.be
25//
26//////////////////////////////////////////////////////////////////////
27//
28//                        DXTC Tools: Vertical Flip.
29//                        *************************
30//
31// Current version: 1.00 BETA 1 (27/08/2002)
32//
33// Comment: Only works with DXTC mode supported by OpenGL.
34//          (currently: DXT1/DXT3/DXT5)
35//
36// History: -
37//
38//////////////////////////////////////////////////////////////////////
39
40#ifndef DXTCTOOL_H
41#define DXTCTOOL_H 
42
43#include <osg/GL>
44#include <osg/Texture>
45
46#if defined(_MSC_VER)
47
48    typedef __int8  dxtc_int8;
49    typedef __int16 dxtc_int16;
50    typedef __int32 dxtc_int32;
51    typedef __int64 dxtc_int64;
52
53    #define HEX_0x000000000000FFFF 0x000000000000FFFF
54    #define HEX_0x000000000FFF0000 0x000000000FFF0000
55    #define HEX_0x000000FFF0000000 0x000000FFF0000000
56    #define HEX_0x000FFF0000000000 0x000FFF0000000000
57    #define HEX_0xFFF0000000000000 0xFFF0000000000000
58
59#else
60
61    typedef char        dxtc_int8;
62    typedef short       dxtc_int16;
63    typedef int         dxtc_int32;
64    typedef long long   dxtc_int64;
65
66    #define HEX_0x000000000000FFFF 0x000000000000FFFFll
67    #define HEX_0x000000000FFF0000 0x000000000FFF0000ll
68    #define HEX_0x000000FFF0000000 0x000000FFF0000000ll
69    #define HEX_0x000FFF0000000000 0x000FFF0000000000ll
70    #define HEX_0xFFF0000000000000 0xFFF0000000000000ll
71
72#endif
73
74namespace dxtc_tool {
75
76
77
78
79// C-like function wrappers
80bool VerticalFlip(size_t Width, size_t Height, GLenum Format, void * pPixels);
81
82
83
84// Class holding reference to DXTC image pixels
85class dxtc_pixels
86{
87public:
88    dxtc_pixels(size_t Width, size_t Height, GLenum Format, void * pPixels);
89
90    // Vertically flip the whole picture
91    bool VFlip() const;
92
93protected:
94
95    // Limitation check functions
96    bool DXT1() const;
97    bool DXT3() const;
98    bool DXT5() const;
99    bool OpenGLSize() const;
100    bool SupportedFormat() const;
101
102    // Vertical flipping functions
103    void VFlip_DXT1() const;   
104    void VFlip_DXT3() const;
105    void VFlip_DXT5() const;
106
107    // Block vertical flipping functions
108    void BVF_Color_H2(void * const pBlock) const;                            // V. flip one color block with its virtual height == 2
109    void BVF_Color_H4(void * const pBlock) const;                            // V. flip one color block with its virtual height == 4
110    void BVF_Color(void * const pBlock1, void * const pBlock2) const;        // V. flip and swap two color blocks, with their virtual height == 4
111    void BVF_Alpha_DXT3_H2(void * const pBlock) const;                        // V. flip one alpha (DXT3) block with its virtual height == 2
112    void BVF_Alpha_DXT3_H4(void * const pBlock) const;                        // V. flip one alpha (DXT3) block with its virtual height == 4
113    void BVF_Alpha_DXT3(void * const pBlock1, void * const pBlock2) const;    // V. flip and swap two alpha (DXT3) blocks, with their virtual height == 4
114    void BVF_Alpha_DXT5_H2(void * const pBlock) const;                        // V. flip one alpha (DXT5) block with its virtual height == 2
115    void BVF_Alpha_DXT5_H4(void * const pBlock) const;                        // V. flip one alpha (DXT5) block with its virtual height == 4
116    void BVF_Alpha_DXT5(void * const pBlock1, void * const pBlock2) const;    // V. flip and swap two alpha (DXT5) blocks, with their virtual height == 4
117   
118    // Block localization functions
119    void * GetBlock(size_t i, size_t j, size_t BlockSize) const;
120
121    // mighty const and var
122    static const size_t BSIZE_DXT1;
123    static const size_t BSIZE_DXT3;
124    static const size_t BSIZE_DXT5;
125    static const size_t BSIZE_ALPHA_DXT3;
126    static const size_t BSIZE_ALPHA_DXT5;
127
128    const size_t    m_Width;
129    const size_t    m_Height;
130    const GLenum    m_Format;
131    void * const    m_pPixels;
132};
133
134
135
136
137//////////////////////////////////////////////////////////////////////
138// C-Like Function Wrappers
139//////////////////////////////////////////////////////////////////////
140
141inline bool VerticalFlip(size_t Width, size_t Height, GLenum Format, void * pPixels) {
142    return (dxtc_pixels(Width, Height, Format, pPixels)).VFlip();
143}
144
145
146
147//////////////////////////////////////////////////////////////////////
148// dxtc_pixels Inline Functions
149//////////////////////////////////////////////////////////////////////
150
151inline dxtc_pixels::dxtc_pixels(size_t Width, size_t Height, GLenum Format, void * pPixels) :
152    m_Width(Width), m_Height(Height), m_Format(Format), m_pPixels(pPixels) { }
153
154
155inline bool dxtc_pixels::DXT1() const {
156    return ((m_Format == GL_COMPRESSED_RGBA_S3TC_DXT1_EXT) || (m_Format == GL_COMPRESSED_RGB_S3TC_DXT1_EXT));
157}
158
159
160inline bool dxtc_pixels::DXT3() const {
161    return (m_Format == GL_COMPRESSED_RGBA_S3TC_DXT3_EXT);
162}
163
164
165inline bool dxtc_pixels::DXT5() const {
166    return (m_Format == GL_COMPRESSED_RGBA_S3TC_DXT5_EXT);
167}
168
169
170inline bool dxtc_pixels::SupportedFormat() const {
171    return (DXT1() || DXT3() || DXT5());
172}
173
174
175inline void dxtc_pixels::BVF_Color_H2(void * const pBlock) const {
176    // Swap the two first row of pixels
177    dxtc_int8 * const pP = ((dxtc_int8 * const) pBlock) + 4;
178
179    std::swap(pP[0], pP[1]);
180}
181
182
183inline void dxtc_pixels::BVF_Color_H4(void * const pBlock) const {
184    // Swap the the first row of pixels with the last one, then the 2 middle row of pixels
185    dxtc_int8 * const pP = ((dxtc_int8 * const) pBlock) + 4;
186
187    std::swap(pP[0], pP[3]);
188    std::swap(pP[1], pP[2]);
189}
190
191
192inline void dxtc_pixels::BVF_Color(void * const pBlock1, void * const pBlock2) const {
193    // Swap the "2 colors" header (32bits each header)
194    dxtc_int32 * const pHdr1 = (dxtc_int32 * const) pBlock1;
195    dxtc_int32 * const pHdr2 = (dxtc_int32 * const) pBlock2;
196
197    std::swap(* pHdr1, * pHdr2);
198
199    // Now swap the pixel values
200    dxtc_int8 * const pP1 = ((dxtc_int8 * const) pBlock1) + 4;
201    dxtc_int8 * const pP2 = ((dxtc_int8 * const) pBlock2) + 4;
202
203    std::swap(pP1[0], pP2[3]);
204    std::swap(pP1[1], pP2[2]);
205    std::swap(pP1[2], pP2[1]);
206    std::swap(pP1[3], pP2[0]);
207}
208
209
210inline void dxtc_pixels::BVF_Alpha_DXT3_H2(void * const pBlock) const {
211    // Swap the two first row of pixels
212    dxtc_int16 * const pP = (dxtc_int16 * const) pBlock;
213
214    std::swap(pP[0], pP[1]);
215}
216
217
218inline void dxtc_pixels::BVF_Alpha_DXT3_H4(void * const pBlock) const {
219    // Swap the the first row of pixels with the last one, then the 2 middle row of pixels
220    dxtc_int16 * const pP = (dxtc_int16 * const) pBlock;
221
222    std::swap(pP[0], pP[3]);
223    std::swap(pP[1], pP[2]);
224}
225
226
227inline void dxtc_pixels::BVF_Alpha_DXT3(void * const pBlock1, void * const pBlock2) const {
228    // Swap all the pixel values
229    dxtc_int16 * const pP1 = (dxtc_int16 * const) pBlock1;
230    dxtc_int16 * const pP2 = (dxtc_int16 * const) pBlock2;
231
232    std::swap(pP1[0], pP2[3]);
233    std::swap(pP1[1], pP2[2]);
234    std::swap(pP1[2], pP2[1]);
235    std::swap(pP1[3], pP2[0]);
236}
237
238
239inline void dxtc_pixels::BVF_Alpha_DXT5_H2(void * const pBlock) const {
240    // Swap the two first row of pixels (kinda tricky with DXT5 unaligned encoding)
241    dxtc_int32 * const pP = (dxtc_int32 * const) (((dxtc_int8 * const) pBlock) + 2);
242
243    dxtc_int32 TmpDWord = (pP[0] & 0xFF000000);
244    TmpDWord |=    (pP[0] & 0x00000FFF) << 12;
245    TmpDWord |= (pP[0] & 0x00FFF000) >> 12;
246    pP[0] = TmpDWord;
247}
248
249
250
251
252
253
254inline void dxtc_pixels::BVF_Alpha_DXT5_H4(void * const pBlock) const {
255    // Swap the the first row of pixels with the last one, then the 2 middle row of pixels (tricky again)
256    dxtc_int64 * const pB = (dxtc_int64 * const) pBlock;
257
258    dxtc_int64 TmpQWord = (pB[0] & HEX_0x000000000000FFFF);
259    TmpQWord |= (pB[0] & HEX_0x000000000FFF0000) << 36;
260    TmpQWord |= (pB[0] & HEX_0x000000FFF0000000) << 12;
261    TmpQWord |= (pB[0] & HEX_0x000FFF0000000000) >> 12;
262    TmpQWord |= (pB[0] & HEX_0xFFF0000000000000) >> 36;
263    pB[0] = TmpQWord;
264}
265
266
267inline void dxtc_pixels::BVF_Alpha_DXT5(void * const pBlock1, void * const pBlock2) const {
268    // Swap all the pixel values (same trick for DXT5)
269    dxtc_int64 * const pB1 = (dxtc_int64 * const) pBlock1;
270    dxtc_int64 * const pB2 = (dxtc_int64 * const) pBlock2;
271
272    dxtc_int64 TmpQWord1 = (pB1[0] & HEX_0x000000000000FFFF);
273    TmpQWord1 |= (pB1[0] & HEX_0x000000000FFF0000) << 36;
274    TmpQWord1 |= (pB1[0] & HEX_0x000000FFF0000000) << 12;
275    TmpQWord1 |= (pB1[0] & HEX_0x000FFF0000000000) >> 12;
276    TmpQWord1 |= (pB1[0] & HEX_0xFFF0000000000000) >> 36;
277
278    dxtc_int64 TmpQWord2 = (pB2[0] & HEX_0x000000000000FFFF);
279    TmpQWord2 |= (pB2[0] & HEX_0x000000000FFF0000) << 36;
280    TmpQWord2 |= (pB2[0] & HEX_0x000000FFF0000000) << 12;
281    TmpQWord2 |= (pB2[0] & HEX_0x000FFF0000000000) >> 12;
282    TmpQWord2 |= (pB2[0] & HEX_0xFFF0000000000000) >> 36;
283
284    pB1[0] = TmpQWord2;
285    pB2[0] = TmpQWord1;
286}
287
288
289inline void * dxtc_pixels::GetBlock(size_t i, size_t j, size_t BlockSize) const {
290    const dxtc_int8 * pPixels = (const dxtc_int8 *) m_pPixels;
291
292    return (void *) (pPixels + i * ((m_Width + 3) / 4) * BlockSize + j * BlockSize);
293}
294
295
296
297
298} // namespace dxtc_tool
299
300#endif // DXTCTOOL_H
Note: See TracBrowser for help on using the browser.