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

Revision 3290, 9.2 kB (checked in by robert, 10 years ago)

Added a handling of different hex long long handling for VS and non
VS.

  • 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 char        __int8;
49    typedef short       __int16;
50    typedef int         __int32;
51    typedef long long   __int64;
52
53    #define HEX_0x000000000000FFFF 0x000000000000FFFFll
54    #define HEX_0x000000000FFF0000 0x000000000FFF0000ll
55    #define HEX_0x000000FFF0000000 0x000000FFF0000000ll
56    #define HEX_0x000FFF0000000000 0x000FFF0000000000ll
57    #define HEX_0xFFF0000000000000 0xFFF0000000000000ll
58
59#else
60
61    #define HEX_0x000000000000FFFF 0x000000000000FFFF
62    #define HEX_0x000000000FFF0000 0x000000000FFF0000
63    #define HEX_0x000000FFF0000000 0x000000FFF0000000
64    #define HEX_0x000FFF0000000000 0x000FFF0000000000
65    #define HEX_0xFFF0000000000000 0xFFF0000000000000
66
67#endif
68
69namespace dxtc_tool {
70
71
72
73
74// C-like function wrappers
75bool VerticalFlip(size_t Width, size_t Height, GLenum Format, void * pPixels);
76
77
78
79// Class holding reference to DXTC image pixels
80class dxtc_pixels
81{
82public:
83        dxtc_pixels(size_t Width, size_t Height, GLenum Format, void * pPixels);
84
85        // Vertically flip the whole picture
86        bool VFlip() const;
87
88protected:
89
90        // Limitation check functions
91        bool DXT1() const;
92        bool DXT3() const;
93        bool DXT5() const;
94        bool OpenGLSize() const;
95        bool SupportedFormat() const;
96
97        // Vertical flipping functions
98        void VFlip_DXT1() const;       
99        void VFlip_DXT3() const;
100        void VFlip_DXT5() const;
101
102        // Block vertical flipping functions
103        void BVF_Color_H2(void * const pBlock) const;                                                   // V. flip one color block with its virtual height == 2
104        void BVF_Color_H4(void * const pBlock) const;                                                   // V. flip one color block with its virtual height == 4
105        void BVF_Color(void * const pBlock1, void * const pBlock2) const;               // V. flip and swap two color blocks, with their virtual height == 4
106        void BVF_Alpha_DXT3_H2(void * const pBlock) const;                                              // V. flip one alpha (DXT3) block with its virtual height == 2
107        void BVF_Alpha_DXT3_H4(void * const pBlock) const;                                              // V. flip one alpha (DXT3) block with its virtual height == 4
108        void BVF_Alpha_DXT3(void * const pBlock1, void * const pBlock2) const// V. flip and swap two alpha (DXT3) blocks, with their virtual height == 4
109        void BVF_Alpha_DXT5_H2(void * const pBlock) const;                                              // V. flip one alpha (DXT5) block with its virtual height == 2
110        void BVF_Alpha_DXT5_H4(void * const pBlock) const;                                              // V. flip one alpha (DXT5) block with its virtual height == 4
111        void BVF_Alpha_DXT5(void * const pBlock1, void * const pBlock2) const// V. flip and swap two alpha (DXT5) blocks, with their virtual height == 4
112       
113        // Block localization functions
114        void * GetBlock(size_t i, size_t j, size_t BlockSize) const;
115
116        // mighty const and var
117        static const size_t BSIZE_DXT1 = 8;
118        static const size_t BSIZE_DXT3 = 16;
119        static const size_t BSIZE_DXT5 = 16;
120        static const size_t BSIZE_ALPHA_DXT3 = 8;
121        static const size_t BSIZE_ALPHA_DXT5 = 8;
122
123        const size_t    m_Width;
124        const size_t    m_Height;
125        const GLenum    m_Format;
126        void * const    m_pPixels;
127};
128
129
130
131
132//////////////////////////////////////////////////////////////////////
133// C-Like Function Wrappers
134//////////////////////////////////////////////////////////////////////
135
136inline bool VerticalFlip(size_t Width, size_t Height, GLenum Format, void * pPixels) {
137        return (dxtc_pixels(Width, Height, Format, pPixels)).VFlip();
138}
139
140
141
142//////////////////////////////////////////////////////////////////////
143// dxtc_pixels Inline Functions
144//////////////////////////////////////////////////////////////////////
145
146inline dxtc_pixels::dxtc_pixels(size_t Width, size_t Height, GLenum Format, void * pPixels) :
147        m_Width(Width), m_Height(Height), m_Format(Format), m_pPixels(pPixels) { }
148
149
150inline bool dxtc_pixels::DXT1() const {
151        return ((m_Format == GL_COMPRESSED_RGBA_S3TC_DXT1_EXT) || (m_Format == GL_COMPRESSED_RGB_S3TC_DXT1_EXT));
152}
153
154
155inline bool dxtc_pixels::DXT3() const {
156        return (m_Format == GL_COMPRESSED_RGBA_S3TC_DXT3_EXT);
157}
158
159
160inline bool dxtc_pixels::DXT5() const {
161        return (m_Format == GL_COMPRESSED_RGBA_S3TC_DXT5_EXT);
162}
163
164
165inline bool dxtc_pixels::SupportedFormat() const {
166        return (DXT1() || DXT3() || DXT5());
167}
168
169
170inline void dxtc_pixels::BVF_Color_H2(void * const pBlock) const {
171        // Swap the two first row of pixels
172        __int8 * const pP = ((__int8 * const) pBlock) + 4;
173
174        std::swap(pP[0], pP[1]);
175}
176
177
178inline void dxtc_pixels::BVF_Color_H4(void * const pBlock) const {
179        // Swap the the first row of pixels with the last one, then the 2 middle row of pixels
180        __int8 * const pP = ((__int8 * const) pBlock) + 4;
181
182        std::swap(pP[0], pP[3]);
183        std::swap(pP[1], pP[2]);
184}
185
186
187inline void dxtc_pixels::BVF_Color(void * const pBlock1, void * const pBlock2) const {
188        // Swap the "2 colors" header (32bits each header)
189        __int32 * const pHdr1 = (__int32 * const) pBlock1;
190        __int32 * const pHdr2 = (__int32 * const) pBlock2;
191
192        std::swap(* pHdr1, * pHdr2);
193
194        // Now swap the pixel values
195        __int8 * const pP1 = ((__int8 * const) pBlock1) + 4;
196        __int8 * const pP2 = ((__int8 * const) pBlock2) + 4;
197
198        std::swap(pP1[0], pP2[3]);
199        std::swap(pP1[1], pP2[2]);
200        std::swap(pP1[2], pP2[1]);
201        std::swap(pP1[3], pP2[0]);
202}
203
204
205inline void dxtc_pixels::BVF_Alpha_DXT3_H2(void * const pBlock) const {
206        // Swap the two first row of pixels
207        __int16 * const pP = (__int16 * const) pBlock;
208
209        std::swap(pP[0], pP[1]);
210}
211
212
213inline void dxtc_pixels::BVF_Alpha_DXT3_H4(void * const pBlock) const {
214        // Swap the the first row of pixels with the last one, then the 2 middle row of pixels
215        __int16 * const pP = (__int16 * const) pBlock;
216
217        std::swap(pP[0], pP[3]);
218        std::swap(pP[1], pP[2]);
219}
220
221
222inline void dxtc_pixels::BVF_Alpha_DXT3(void * const pBlock1, void * const pBlock2) const {
223        // Swap all the pixel values
224        __int16 * const pP1 = (__int16 * const) pBlock1;
225        __int16 * const pP2 = (__int16 * const) pBlock2;
226
227        std::swap(pP1[0], pP2[3]);
228        std::swap(pP1[1], pP2[2]);
229        std::swap(pP1[2], pP2[1]);
230        std::swap(pP1[3], pP2[0]);
231}
232
233
234inline void dxtc_pixels::BVF_Alpha_DXT5_H2(void * const pBlock) const {
235        // Swap the two first row of pixels (kinda tricky with DXT5 unaligned encoding)
236        __int32 * const pP = (__int32 * const) (((__int8 * const) pBlock) + 2);
237
238        __int32 TmpDWord = (pP[0] & 0xFF000000);
239        TmpDWord |=     (pP[0] & 0x00000FFF) << 12;
240        TmpDWord |= (pP[0] & 0x00FFF000) >> 12;
241        pP[0] = TmpDWord;
242}
243
244
245
246
247
248
249inline void dxtc_pixels::BVF_Alpha_DXT5_H4(void * const pBlock) const {
250        // Swap the the first row of pixels with the last one, then the 2 middle row of pixels (tricky again)
251        __int64 * const pB = (__int64 * const) pBlock;
252
253        __int64 TmpQWord = (pB[0] & HEX_0x000000000000FFFF);
254        TmpQWord |= (pB[0] & HEX_0x000000000FFF0000) << 36;
255        TmpQWord |= (pB[0] & HEX_0x000000FFF0000000) << 12;
256        TmpQWord |= (pB[0] & HEX_0x000FFF0000000000) >> 12;
257        TmpQWord |= (pB[0] & HEX_0xFFF0000000000000) >> 36;
258        pB[0] = TmpQWord;
259}
260
261
262inline void dxtc_pixels::BVF_Alpha_DXT5(void * const pBlock1, void * const pBlock2) const {
263        // Swap all the pixel values (same trick for DXT5)
264        __int64 * const pB1 = (__int64 * const) pBlock1;
265        __int64 * const pB2 = (__int64 * const) pBlock2;
266
267        __int64 TmpQWord1 = (pB1[0] & HEX_0x000000000000FFFF);
268        TmpQWord1 |= (pB1[0] & HEX_0x000000000FFF0000) << 36;
269        TmpQWord1 |= (pB1[0] & HEX_0x000000FFF0000000) << 12;
270        TmpQWord1 |= (pB1[0] & HEX_0x000FFF0000000000) >> 12;
271        TmpQWord1 |= (pB1[0] & HEX_0xFFF0000000000000) >> 36;
272
273        __int64 TmpQWord2 = (pB2[0] & HEX_0x000000000000FFFF);
274        TmpQWord2 |= (pB2[0] & HEX_0x000000000FFF0000) << 36;
275        TmpQWord2 |= (pB2[0] & HEX_0x000000FFF0000000) << 12;
276        TmpQWord2 |= (pB2[0] & HEX_0x000FFF0000000000) >> 12;
277        TmpQWord2 |= (pB2[0] & HEX_0xFFF0000000000000) >> 36;
278
279        pB1[0] = TmpQWord2;
280        pB2[0] = TmpQWord1;
281}
282
283
284inline void * dxtc_pixels::GetBlock(size_t i, size_t j, size_t BlockSize) const {
285        const __int8 * pPixels = (const __int8 *) m_pPixels;
286
287        return (void *) (pPixels + i * ((m_Width + 3) / 4) * BlockSize + j * BlockSize);
288}
289
290
291
292
293} // namespace dxtc_tool
294
295#endif // DXTCTOOL_H
Note: See TracBrowser for help on using the browser.