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