Blender V2.61 - r43446

BlockDXT.h

Go to the documentation of this file.
00001 /*
00002  * ***** BEGIN GPL LICENSE BLOCK *****
00003  *
00004  * This program is free software; you can redistribute it and/or
00005  * modify it under the terms of the GNU General Public License
00006  * as published by the Free Software Foundation; either version 2
00007  * of the License, or (at your option) any later version.
00008  *
00009  * This program is distributed in the hope that it will be useful,
00010  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012  * GNU General Public License for more details.
00013  *
00014  * You should have received a copy of the GNU General Public License
00015  * along with this program; if not, write to the Free Software Foundation,
00016  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
00017  *
00018  * Contributors: Amorilia (amorilia@users.sourceforge.net)
00019  *
00020  * ***** END GPL LICENSE BLOCK *****
00021  */
00022 
00028 /*
00029  * This file is based on a similar file from the NVIDIA texture tools
00030  * (http://nvidia-texture-tools.googlecode.com/)
00031  *
00032  * Original license from NVIDIA follows.
00033  */
00034 
00035 // Copyright NVIDIA Corporation 2007 -- Ignacio Castano <icastano@nvidia.com>
00036 // 
00037 // Permission is hereby granted, free of charge, to any person
00038 // obtaining a copy of this software and associated documentation
00039 // files (the "Software"), to deal in the Software without
00040 // restriction, including without limitation the rights to use,
00041 // copy, modify, merge, publish, distribute, sublicense, and/or sell
00042 // copies of the Software, and to permit persons to whom the
00043 // Software is furnished to do so, subject to the following
00044 // conditions:
00045 // 
00046 // The above copyright notice and this permission notice shall be
00047 // included in all copies or substantial portions of the Software.
00048 // 
00049 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
00050 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
00051 // OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
00052 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
00053 // HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
00054 // WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
00055 // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
00056 // OTHER DEALINGS IN THE SOFTWARE.
00057 
00058 #ifndef _DDS_BLOCKDXT_H
00059 #define _DDS_BLOCKDXT_H
00060 
00061 #include <Common.h>
00062 #include <Color.h>
00063 #include <ColorBlock.h>
00064 #include <Stream.h>
00065 
00067 struct BlockDXT1
00068 {
00069     Color16 col0;
00070     Color16 col1;
00071     union {
00072         uint8 row[4];
00073         uint indices;
00074     };
00075 
00076     bool isFourColorMode() const;
00077 
00078     uint evaluatePalette(Color32 color_array[4]) const;
00079     uint evaluatePaletteNV5x(Color32 color_array[4]) const;
00080 
00081     void evaluatePalette3(Color32 color_array[4]) const;
00082     void evaluatePalette4(Color32 color_array[4]) const;
00083     
00084     void decodeBlock(ColorBlock * block) const;
00085     void decodeBlockNV5x(ColorBlock * block) const;
00086     
00087     void setIndices(int * idx);
00088 
00089     void flip4();
00090     void flip2();
00091 };
00092 
00094 inline bool BlockDXT1::isFourColorMode() const
00095 {
00096     return col0.u > col1.u;
00097 }
00098 
00099 
00101 struct AlphaBlockDXT3
00102 {
00103     union {
00104         struct {
00105             uint alpha0 : 4;
00106             uint alpha1 : 4;
00107             uint alpha2 : 4;
00108             uint alpha3 : 4;
00109             uint alpha4 : 4;
00110             uint alpha5 : 4;
00111             uint alpha6 : 4;
00112             uint alpha7 : 4;
00113             uint alpha8 : 4;
00114             uint alpha9 : 4;
00115             uint alphaA : 4;
00116             uint alphaB : 4;
00117             uint alphaC : 4;
00118             uint alphaD : 4;
00119             uint alphaE : 4;
00120             uint alphaF : 4;
00121         };
00122         uint16 row[4];
00123     };
00124     
00125     void decodeBlock(ColorBlock * block) const;
00126     
00127     void flip4();
00128     void flip2();
00129 };
00130 
00131 
00133 struct BlockDXT3
00134 {
00135     AlphaBlockDXT3 alpha;
00136     BlockDXT1 color;
00137     
00138     void decodeBlock(ColorBlock * block) const;
00139     void decodeBlockNV5x(ColorBlock * block) const;
00140     
00141     void flip4();
00142     void flip2();
00143 };
00144 
00145 
00147 struct AlphaBlockDXT5
00148 {
00149     // uint64 unions do not compile on all platforms
00150     /*
00151     union {
00152         struct {
00153             uint64 alpha0 : 8;  // 8
00154             uint64 alpha1 : 8;  // 16
00155             uint64 bits0 : 3;       // 3 - 19
00156             uint64 bits1 : 3;   // 6 - 22
00157             uint64 bits2 : 3;   // 9 - 25
00158             uint64 bits3 : 3;       // 12 - 28
00159             uint64 bits4 : 3;       // 15 - 31
00160             uint64 bits5 : 3;       // 18 - 34
00161             uint64 bits6 : 3;       // 21 - 37
00162             uint64 bits7 : 3;       // 24 - 40
00163             uint64 bits8 : 3;       // 27 - 43
00164             uint64 bits9 : 3;   // 30 - 46
00165             uint64 bitsA : 3;   // 33 - 49
00166             uint64 bitsB : 3;       // 36 - 52
00167             uint64 bitsC : 3;       // 39 - 55
00168             uint64 bitsD : 3;       // 42 - 58
00169             uint64 bitsE : 3;       // 45 - 61
00170             uint64 bitsF : 3;       // 48 - 64
00171         };
00172         uint64 u;
00173     };
00174     */
00175     uint64 u;
00176     uint8 alpha0() const { return u & 0xffLL; };
00177     uint8 alpha1() const { return (u >> 8) & 0xffLL; };
00178     uint8 bits0() const { return (u >> 16) & 0x7LL; };
00179     uint8 bits1() const { return (u >> 19) & 0x7LL; };
00180     uint8 bits2() const { return (u >> 22) & 0x7LL; };
00181     uint8 bits3() const { return (u >> 25) & 0x7LL; };
00182     uint8 bits4() const { return (u >> 28) & 0x7LL; };
00183     uint8 bits5() const { return (u >> 31) & 0x7LL; };
00184     uint8 bits6() const { return (u >> 34) & 0x7LL; };
00185     uint8 bits7() const { return (u >> 37) & 0x7LL; };
00186     uint8 bits8() const { return (u >> 40) & 0x7LL; };
00187     uint8 bits9() const { return (u >> 43) & 0x7LL; };
00188     uint8 bitsA() const { return (u >> 46) & 0x7LL; };
00189     uint8 bitsB() const { return (u >> 49) & 0x7LL; };
00190     uint8 bitsC() const { return (u >> 52) & 0x7LL; };
00191     uint8 bitsD() const { return (u >> 55) & 0x7LL; };
00192     uint8 bitsE() const { return (u >> 58) & 0x7LL; };
00193     uint8 bitsF() const { return (u >> 61) & 0x7LL; };
00194     
00195     void evaluatePalette(uint8 alpha[8]) const;
00196     void evaluatePalette8(uint8 alpha[8]) const;
00197     void evaluatePalette6(uint8 alpha[8]) const;
00198     void indices(uint8 index_array[16]) const;
00199 
00200     uint index(uint index) const;
00201     void setIndex(uint index, uint value);
00202     
00203     void decodeBlock(ColorBlock * block) const;
00204     
00205     void flip4();
00206     void flip2();
00207 };
00208 
00209 
00211 struct BlockDXT5
00212 {
00213     AlphaBlockDXT5 alpha;
00214     BlockDXT1 color;
00215     
00216     void decodeBlock(ColorBlock * block) const;
00217     void decodeBlockNV5x(ColorBlock * block) const;
00218     
00219     void flip4();
00220     void flip2();
00221 };
00222 
00224 struct BlockATI1
00225 {
00226     AlphaBlockDXT5 alpha;
00227     
00228     void decodeBlock(ColorBlock * block) const;
00229     
00230     void flip4();
00231     void flip2();
00232 };
00233 
00235 struct BlockATI2
00236 {
00237     AlphaBlockDXT5 x;
00238     AlphaBlockDXT5 y;
00239     
00240     void decodeBlock(ColorBlock * block) const;
00241     
00242     void flip4();
00243     void flip2();
00244 };
00245 
00247 struct BlockCTX1
00248 {
00249     uint8 col0[2];
00250     uint8 col1[2];
00251     union {
00252         uint8 row[4];
00253         uint indices;
00254     };
00255 
00256     void evaluatePalette(Color32 color_array[4]) const;
00257     void setIndices(int * idx);
00258 
00259     void decodeBlock(ColorBlock * block) const;
00260     
00261     void flip4();
00262     void flip2();
00263 };
00264 
00265 void mem_read(Stream & mem, BlockDXT1 & block);
00266 void mem_read(Stream & mem, AlphaBlockDXT3 & block);
00267 void mem_read(Stream & mem, BlockDXT3 & block);
00268 void mem_read(Stream & mem, AlphaBlockDXT5 & block);
00269 void mem_read(Stream & mem, BlockDXT5 & block);
00270 void mem_read(Stream & mem, BlockATI1 & block);
00271 void mem_read(Stream & mem, BlockATI2 & block);
00272 void mem_read(Stream & mem, BlockCTX1 & block);
00273 
00274 #endif // _DDS_BLOCKDXT_H