dafe58a40a7cd0f495b7a93717bcc7aae7588f71
[blender.git] / source / blender / imbuf / intern / dds / DirectDrawSurface.cpp
1 /**
2  * $Id$
3  *
4  * ***** BEGIN GPL/BL DUAL LICENSE BLOCK *****
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version. The Blender
10  * Foundation also sells licenses for use in proprietary software under
11  * the Blender License.  See http://www.blender.org/BL/ for information
12  * about this.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software Foundation,
21  * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
22  *
23  * Contributors: Amorilia (amorilia@gamebox.net)
24  *
25  * ***** END GPL/BL DUAL LICENSE BLOCK *****
26  */
27
28 /*
29  * This file is based on a similar file from the NVIDIA texture tools
30  * (http://nvidia-texture-tools.googlecode.com/)
31  *
32  * Original license from NVIDIA follows.
33  */
34
35 // Copyright NVIDIA Corporation 2007 -- Ignacio Castano <icastano@nvidia.com>
36 // 
37 // Permission is hereby granted, free of charge, to any person
38 // obtaining a copy of this software and associated documentation
39 // files (the "Software"), to deal in the Software without
40 // restriction, including without limitation the rights to use,
41 // copy, modify, merge, publish, distribute, sublicense, and/or sell
42 // copies of the Software, and to permit persons to whom the
43 // Software is furnished to do so, subject to the following
44 // conditions:
45 // 
46 // The above copyright notice and this permission notice shall be
47 // included in all copies or substantial portions of the Software.
48 // 
49 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
50 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
51 // OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
52 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
53 // HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
54 // WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
55 // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
56 // OTHER DEALINGS IN THE SOFTWARE.
57
58 #include <DirectDrawSurface.h>
59 #include <BlockDXT.h>
60 #include <PixelFormat.h>
61
62 #include <stdio.h> // printf
63 #include <math.h>  // sqrt
64
65 /*** declarations ***/
66
67 #if !defined(MAKEFOURCC)
68 #       define MAKEFOURCC(ch0, ch1, ch2, ch3) \
69                 ((uint)((unsigned char)(ch0)) | \
70                 ((uint)((unsigned char)(ch1)) << 8) | \
71                 ((uint)((unsigned char)(ch2)) << 16) | \
72                 ((uint)((unsigned char)(ch3)) << 24 ))
73 #endif
74
75 static const uint FOURCC_DDS = MAKEFOURCC('D', 'D', 'S', ' ');
76 static const uint FOURCC_DXT1 = MAKEFOURCC('D', 'X', 'T', '1');
77 static const uint FOURCC_DXT2 = MAKEFOURCC('D', 'X', 'T', '2');
78 static const uint FOURCC_DXT3 = MAKEFOURCC('D', 'X', 'T', '3');
79 static const uint FOURCC_DXT4 = MAKEFOURCC('D', 'X', 'T', '4');
80 static const uint FOURCC_DXT5 = MAKEFOURCC('D', 'X', 'T', '5');
81 static const uint FOURCC_RXGB = MAKEFOURCC('R', 'X', 'G', 'B');
82 static const uint FOURCC_ATI1 = MAKEFOURCC('A', 'T', 'I', '1');
83 static const uint FOURCC_ATI2 = MAKEFOURCC('A', 'T', 'I', '2');
84
85 // 32 bit RGB formats.
86 static const uint D3DFMT_R8G8B8 = 20;
87 static const uint D3DFMT_A8R8G8B8 = 21;
88 static const uint D3DFMT_X8R8G8B8 = 22;
89 static const uint D3DFMT_R5G6B5 = 23;
90 static const uint D3DFMT_X1R5G5B5 = 24;
91 static const uint D3DFMT_A1R5G5B5 = 25;
92 static const uint D3DFMT_A4R4G4B4 = 26;
93 static const uint D3DFMT_R3G3B2 = 27;
94 static const uint D3DFMT_A8 = 28;
95 static const uint D3DFMT_A8R3G3B2 = 29;
96 static const uint D3DFMT_X4R4G4B4 = 30;
97 static const uint D3DFMT_A2B10G10R10 = 31;
98 static const uint D3DFMT_A8B8G8R8 = 32;
99 static const uint D3DFMT_X8B8G8R8 = 33;
100 static const uint D3DFMT_G16R16 = 34;
101 static const uint D3DFMT_A2R10G10B10 = 35;
102
103 static const uint D3DFMT_A16B16G16R16 = 36;
104
105 // Palette formats.
106 static const uint D3DFMT_A8P8 = 40;
107 static const uint D3DFMT_P8 = 41;
108         
109 // Luminance formats.
110 static const uint D3DFMT_L8 = 50;
111 static const uint D3DFMT_A8L8 = 51;
112 static const uint D3DFMT_A4L4 = 52;
113 static const uint D3DFMT_L16 = 81;
114
115 // Floating point formats
116 static const uint D3DFMT_R16F = 111;
117 static const uint D3DFMT_G16R16F = 112;
118 static const uint D3DFMT_A16B16G16R16F = 113;
119 static const uint D3DFMT_R32F = 114;
120 static const uint D3DFMT_G32R32F = 115;
121 static const uint D3DFMT_A32B32G32R32F = 116;
122         
123 static const uint DDSD_CAPS = 0x00000001U;
124 static const uint DDSD_PIXELFORMAT = 0x00001000U;
125 static const uint DDSD_WIDTH = 0x00000004U;
126 static const uint DDSD_HEIGHT = 0x00000002U;
127 static const uint DDSD_PITCH = 0x00000008U;
128 static const uint DDSD_MIPMAPCOUNT = 0x00020000U;
129 static const uint DDSD_LINEARSIZE = 0x00080000U;
130 static const uint DDSD_DEPTH = 0x00800000U;
131         
132 static const uint DDSCAPS_COMPLEX = 0x00000008U;
133 static const uint DDSCAPS_TEXTURE = 0x00001000U;
134 static const uint DDSCAPS_MIPMAP = 0x00400000U;
135 static const uint DDSCAPS2_VOLUME = 0x00200000U;
136 static const uint DDSCAPS2_CUBEMAP = 0x00000200U;
137
138 static const uint DDSCAPS2_CUBEMAP_POSITIVEX = 0x00000400U;
139 static const uint DDSCAPS2_CUBEMAP_NEGATIVEX = 0x00000800U;
140 static const uint DDSCAPS2_CUBEMAP_POSITIVEY = 0x00001000U;
141 static const uint DDSCAPS2_CUBEMAP_NEGATIVEY = 0x00002000U;
142 static const uint DDSCAPS2_CUBEMAP_POSITIVEZ = 0x00004000U;
143 static const uint DDSCAPS2_CUBEMAP_NEGATIVEZ = 0x00008000U;
144 static const uint DDSCAPS2_CUBEMAP_ALL_FACES = 0x0000FC00U;
145
146 static const uint DDPF_ALPHAPIXELS = 0x00000001U;
147 static const uint DDPF_ALPHA = 0x00000002U;
148 static const uint DDPF_FOURCC = 0x00000004U;
149 static const uint DDPF_RGB = 0x00000040U;
150 static const uint DDPF_PALETTEINDEXED1 = 0x00000800U;
151 static const uint DDPF_PALETTEINDEXED2 = 0x00001000U;
152 static const uint DDPF_PALETTEINDEXED4 = 0x00000008U;
153 static const uint DDPF_PALETTEINDEXED8 = 0x00000020U;
154 static const uint DDPF_LUMINANCE = 0x00020000U;
155 static const uint DDPF_ALPHAPREMULT = 0x00008000U;
156 static const uint DDPF_NORMAL = 0x80000000U;    // @@ Custom nv flag.
157
158         // DX10 formats.
159         enum DXGI_FORMAT
160         {
161                 DXGI_FORMAT_UNKNOWN = 0,
162                 
163                 DXGI_FORMAT_R32G32B32A32_TYPELESS = 1,
164                 DXGI_FORMAT_R32G32B32A32_FLOAT = 2,
165                 DXGI_FORMAT_R32G32B32A32_UINT = 3,
166                 DXGI_FORMAT_R32G32B32A32_SINT = 4,
167                 
168                 DXGI_FORMAT_R32G32B32_TYPELESS = 5,
169                 DXGI_FORMAT_R32G32B32_FLOAT = 6,
170                 DXGI_FORMAT_R32G32B32_UINT = 7,
171                 DXGI_FORMAT_R32G32B32_SINT = 8,
172                 
173                 DXGI_FORMAT_R16G16B16A16_TYPELESS = 9,
174                 DXGI_FORMAT_R16G16B16A16_FLOAT = 10,
175                 DXGI_FORMAT_R16G16B16A16_UNORM = 11,
176                 DXGI_FORMAT_R16G16B16A16_UINT = 12,
177                 DXGI_FORMAT_R16G16B16A16_SNORM = 13,
178                 DXGI_FORMAT_R16G16B16A16_SINT = 14,
179                 
180                 DXGI_FORMAT_R32G32_TYPELESS = 15,
181                 DXGI_FORMAT_R32G32_FLOAT = 16,
182                 DXGI_FORMAT_R32G32_UINT = 17,
183                 DXGI_FORMAT_R32G32_SINT = 18,
184                 
185                 DXGI_FORMAT_R32G8X24_TYPELESS = 19,
186                 DXGI_FORMAT_D32_FLOAT_S8X24_UINT = 20,
187                 DXGI_FORMAT_R32_FLOAT_X8X24_TYPELESS = 21,
188                 DXGI_FORMAT_X32_TYPELESS_G8X24_UINT = 22,
189                 
190                 DXGI_FORMAT_R10G10B10A2_TYPELESS = 23,
191                 DXGI_FORMAT_R10G10B10A2_UNORM = 24,
192                 DXGI_FORMAT_R10G10B10A2_UINT = 25,
193                 
194                 DXGI_FORMAT_R11G11B10_FLOAT = 26,
195                 
196                 DXGI_FORMAT_R8G8B8A8_TYPELESS = 27,
197                 DXGI_FORMAT_R8G8B8A8_UNORM = 28,
198                 DXGI_FORMAT_R8G8B8A8_UNORM_SRGB = 29,
199                 DXGI_FORMAT_R8G8B8A8_UINT = 30,
200                 DXGI_FORMAT_R8G8B8A8_SNORM = 31,
201                 DXGI_FORMAT_R8G8B8A8_SINT = 32,
202                 
203                 DXGI_FORMAT_R16G16_TYPELESS = 33,
204                 DXGI_FORMAT_R16G16_FLOAT = 34,
205                 DXGI_FORMAT_R16G16_UNORM = 35,
206                 DXGI_FORMAT_R16G16_UINT = 36,
207                 DXGI_FORMAT_R16G16_SNORM = 37,
208                 DXGI_FORMAT_R16G16_SINT = 38,
209                 
210                 DXGI_FORMAT_R32_TYPELESS = 39,
211                 DXGI_FORMAT_D32_FLOAT = 40,
212                 DXGI_FORMAT_R32_FLOAT = 41,
213                 DXGI_FORMAT_R32_UINT = 42,
214                 DXGI_FORMAT_R32_SINT = 43,
215                 
216                 DXGI_FORMAT_R24G8_TYPELESS = 44,
217                 DXGI_FORMAT_D24_UNORM_S8_UINT = 45,
218                 DXGI_FORMAT_R24_UNORM_X8_TYPELESS = 46,
219                 DXGI_FORMAT_X24_TYPELESS_G8_UINT = 47,
220                 
221                 DXGI_FORMAT_R8G8_TYPELESS = 48,
222                 DXGI_FORMAT_R8G8_UNORM = 49,
223                 DXGI_FORMAT_R8G8_UINT = 50,
224                 DXGI_FORMAT_R8G8_SNORM = 51,
225                 DXGI_FORMAT_R8G8_SINT = 52,
226                 
227                 DXGI_FORMAT_R16_TYPELESS = 53,
228                 DXGI_FORMAT_R16_FLOAT = 54,
229                 DXGI_FORMAT_D16_UNORM = 55,
230                 DXGI_FORMAT_R16_UNORM = 56,
231                 DXGI_FORMAT_R16_UINT = 57,
232                 DXGI_FORMAT_R16_SNORM = 58,
233                 DXGI_FORMAT_R16_SINT = 59,
234                 
235                 DXGI_FORMAT_R8_TYPELESS = 60,
236                 DXGI_FORMAT_R8_UNORM = 61,
237                 DXGI_FORMAT_R8_UINT = 62,
238                 DXGI_FORMAT_R8_SNORM = 63,
239                 DXGI_FORMAT_R8_SINT = 64,
240                 DXGI_FORMAT_A8_UNORM = 65,
241                 
242                 DXGI_FORMAT_R1_UNORM = 66,
243                 
244                 DXGI_FORMAT_R9G9B9E5_SHAREDEXP = 67,
245                 
246                 DXGI_FORMAT_R8G8_B8G8_UNORM = 68,
247                 DXGI_FORMAT_G8R8_G8B8_UNORM = 69,
248                 
249                 DXGI_FORMAT_BC1_TYPELESS = 70,
250                 DXGI_FORMAT_BC1_UNORM = 71,
251                 DXGI_FORMAT_BC1_UNORM_SRGB = 72,
252                 
253                 DXGI_FORMAT_BC2_TYPELESS = 73,
254                 DXGI_FORMAT_BC2_UNORM = 74,
255                 DXGI_FORMAT_BC2_UNORM_SRGB = 75,
256                 
257                 DXGI_FORMAT_BC3_TYPELESS = 76,
258                 DXGI_FORMAT_BC3_UNORM = 77,
259                 DXGI_FORMAT_BC3_UNORM_SRGB = 78,
260                 
261                 DXGI_FORMAT_BC4_TYPELESS = 79,
262                 DXGI_FORMAT_BC4_UNORM = 80,
263                 DXGI_FORMAT_BC4_SNORM = 81,
264                 
265                 DXGI_FORMAT_BC5_TYPELESS = 82,
266                 DXGI_FORMAT_BC5_UNORM = 83,
267                 DXGI_FORMAT_BC5_SNORM = 84,
268                 
269                 DXGI_FORMAT_B5G6R5_UNORM = 85,
270                 DXGI_FORMAT_B5G5R5A1_UNORM = 86,
271                 DXGI_FORMAT_B8G8R8A8_UNORM = 87,
272                 DXGI_FORMAT_B8G8R8X8_UNORM = 88,
273         };
274
275         enum D3D10_RESOURCE_DIMENSION
276         {
277                 D3D10_RESOURCE_DIMENSION_UNKNOWN = 0,
278                 D3D10_RESOURCE_DIMENSION_BUFFER = 1,
279                 D3D10_RESOURCE_DIMENSION_TEXTURE1D = 2,
280                 D3D10_RESOURCE_DIMENSION_TEXTURE2D = 3,
281                 D3D10_RESOURCE_DIMENSION_TEXTURE3D = 4,
282         };
283
284 /*** implementation ***/
285
286 void mem_read(Stream & mem, DDSPixelFormat & pf)
287 {
288         mem_read(mem, pf.size);
289         mem_read(mem, pf.flags);
290         mem_read(mem, pf.fourcc);
291         mem_read(mem, pf.bitcount);
292         mem_read(mem, pf.rmask);
293         mem_read(mem, pf.gmask);
294         mem_read(mem, pf.bmask);
295         mem_read(mem, pf.amask);
296 }
297
298 void mem_read(Stream & mem, DDSCaps & caps)
299 {
300         mem_read(mem, caps.caps1);
301         mem_read(mem, caps.caps2);
302         mem_read(mem, caps.caps3);
303         mem_read(mem, caps.caps4);
304 }
305
306 void mem_read(Stream & mem, DDSHeader10 & header)
307 {
308         mem_read(mem, header.dxgiFormat);
309         mem_read(mem, header.resourceDimension);
310         mem_read(mem, header.miscFlag);
311         mem_read(mem, header.arraySize);
312         mem_read(mem, header.reserved);
313 }
314
315 void mem_read(Stream & mem, DDSHeader & header)
316 {
317         mem_read(mem, header.fourcc);
318         mem_read(mem, header.size);
319         mem_read(mem, header.flags);
320         mem_read(mem, header.height);
321         mem_read(mem, header.width);
322         mem_read(mem, header.pitch);
323         mem_read(mem, header.depth);
324         mem_read(mem, header.mipmapcount);
325         for (uint i = 0; i < 11; i++) mem_read(mem, header.reserved[i]);
326         mem_read(mem, header.pf);
327         mem_read(mem, header.caps);
328         mem_read(mem, header.notused);
329
330         if (header.hasDX10Header())
331         {
332                 mem_read(mem, header.header10);
333         }
334 }
335
336
337
338 DDSHeader::DDSHeader()
339 {
340         this->fourcc = FOURCC_DDS;
341         this->size = 124;
342         this->flags  = (DDSD_CAPS|DDSD_PIXELFORMAT);
343         this->height = 0;
344         this->width = 0;
345         this->pitch = 0;
346         this->depth = 0;
347         this->mipmapcount = 0;
348         for (uint i = 0; i < 11; i++) this->reserved[i] = 0;
349
350         // Store version information on the reserved header attributes.
351         this->reserved[9] = MAKEFOURCC('N', 'V', 'T', 'T');
352         this->reserved[10] = (0 << 16) | (9 << 8) | (5);        // major.minor.revision
353
354         this->pf.size = 32;
355         this->pf.flags = 0;
356         this->pf.fourcc = 0;
357         this->pf.bitcount = 0;
358         this->pf.rmask = 0;
359         this->pf.gmask = 0;
360         this->pf.bmask = 0;
361         this->pf.amask = 0;
362         this->caps.caps1 = DDSCAPS_TEXTURE;
363         this->caps.caps2 = 0;
364         this->caps.caps3 = 0;
365         this->caps.caps4 = 0;
366         this->notused = 0;
367
368         this->header10.dxgiFormat = DXGI_FORMAT_UNKNOWN;
369         this->header10.resourceDimension = D3D10_RESOURCE_DIMENSION_UNKNOWN;
370         this->header10.miscFlag = 0;
371         this->header10.arraySize = 0;
372         this->header10.reserved = 0;
373 }
374
375 void DDSHeader::setWidth(uint w)
376 {
377         this->flags |= DDSD_WIDTH;
378         this->width = w;
379 }
380
381 void DDSHeader::setHeight(uint h)
382 {
383         this->flags |= DDSD_HEIGHT;
384         this->height = h;
385 }
386
387 void DDSHeader::setDepth(uint d)
388 {
389         this->flags |= DDSD_DEPTH;
390         this->height = d;
391 }
392
393 void DDSHeader::setMipmapCount(uint count)
394 {
395         if (count == 0 || count == 1)
396         {
397                 this->flags &= ~DDSD_MIPMAPCOUNT;
398                 this->mipmapcount = 0;
399
400                 if (this->caps.caps2 == 0) {
401                         this->caps.caps1 = DDSCAPS_TEXTURE;
402                 }
403                 else {
404                         this->caps.caps1 = DDSCAPS_TEXTURE | DDSCAPS_COMPLEX;
405                 }
406         }
407         else
408         {
409                 this->flags |= DDSD_MIPMAPCOUNT;
410                 this->mipmapcount = count;
411
412                 this->caps.caps1 |= DDSCAPS_COMPLEX | DDSCAPS_MIPMAP;
413         }
414 }
415
416 void DDSHeader::setTexture2D()
417 {
418         this->header10.resourceDimension = D3D10_RESOURCE_DIMENSION_TEXTURE2D;
419 }
420
421 void DDSHeader::setTexture3D()
422 {
423         this->caps.caps2 = DDSCAPS2_VOLUME;
424
425         this->header10.resourceDimension = D3D10_RESOURCE_DIMENSION_TEXTURE3D;
426 }
427
428 void DDSHeader::setTextureCube()
429 {
430         this->caps.caps1 |= DDSCAPS_COMPLEX;
431         this->caps.caps2 = DDSCAPS2_CUBEMAP | DDSCAPS2_CUBEMAP_ALL_FACES;
432
433         this->header10.resourceDimension = D3D10_RESOURCE_DIMENSION_TEXTURE2D;
434         this->header10.arraySize = 6;
435 }
436
437 void DDSHeader::setLinearSize(uint size)
438 {
439         this->flags &= ~DDSD_PITCH;
440         this->flags |= DDSD_LINEARSIZE;
441         this->pitch = size;
442 }
443
444 void DDSHeader::setPitch(uint pitch)
445 {
446         this->flags &= ~DDSD_LINEARSIZE;
447         this->flags |= DDSD_PITCH;
448         this->pitch = pitch;
449 }
450
451 void DDSHeader::setFourCC(uint8 c0, uint8 c1, uint8 c2, uint8 c3)
452 {
453         // set fourcc pixel format.
454         this->pf.flags = DDPF_FOURCC;
455         this->pf.fourcc = MAKEFOURCC(c0, c1, c2, c3);
456         this->pf.bitcount = 0;
457         this->pf.rmask = 0;
458         this->pf.gmask = 0;
459         this->pf.bmask = 0;
460         this->pf.amask = 0;
461 }
462
463 void DDSHeader::setPixelFormat(uint bitcount, uint rmask, uint gmask, uint bmask, uint amask)
464 {
465         // Make sure the masks are correct.
466         if ((rmask & gmask) || \
467                 (rmask & bmask) || \
468                 (rmask & amask) || \
469                 (gmask & bmask) || \
470                 (gmask & amask) || \
471                 (bmask & amask)) {
472                 printf("DDS: bad RGBA masks, pixel format not set\n");
473                 return;
474         }
475
476         this->pf.flags = DDPF_RGB;
477
478         if (amask != 0) {
479                 this->pf.flags |= DDPF_ALPHAPIXELS;
480         }
481
482         if (bitcount == 0)
483         {
484                 // Compute bit count from the masks.
485                 uint total = rmask | gmask | bmask | amask;
486                 while(total != 0) {
487                         bitcount++;
488                         total >>= 1;
489                 }
490         }
491
492         if (!(bitcount > 0 && bitcount <= 32)) {
493                 printf("DDS: bad bit count, pixel format not set\n");
494                 return;
495         }
496
497         // Align to 8.
498         if (bitcount < 8) bitcount = 8;
499         else if (bitcount < 16) bitcount = 16;
500         else if (bitcount < 24) bitcount = 24;
501         else bitcount = 32;
502
503         this->pf.fourcc = 0; //findD3D9Format(bitcount, rmask, gmask, bmask, amask);
504         this->pf.bitcount = bitcount;
505         this->pf.rmask = rmask;
506         this->pf.gmask = gmask;
507         this->pf.bmask = bmask;
508         this->pf.amask = amask;
509 }
510
511 void DDSHeader::setDX10Format(uint format)
512 {
513         this->pf.flags = 0;
514         this->header10.dxgiFormat = format;
515 }
516
517 void DDSHeader::setNormalFlag(bool b)
518 {
519         if (b) this->pf.flags |= DDPF_NORMAL;
520         else this->pf.flags &= ~DDPF_NORMAL;
521 }
522
523 bool DDSHeader::hasDX10Header() const
524 {
525         return this->pf.flags == 0;
526 }
527
528 DirectDrawSurface::DirectDrawSurface(unsigned char *mem, uint size) : stream(mem, size), header()
529 {
530         mem_read(stream, header);
531 }
532
533 DirectDrawSurface::~DirectDrawSurface()
534 {
535 }
536
537 bool DirectDrawSurface::isValid() const
538 {
539         if (header.fourcc != FOURCC_DDS || header.size != 124)
540         {
541                 return false;
542         }
543         
544         const uint required = (DDSD_WIDTH|DDSD_HEIGHT|DDSD_CAPS|DDSD_PIXELFORMAT);
545         if( (header.flags & required) != required ) {
546                 return false;
547         }
548         
549         if (header.pf.size != 32) {
550                 return false;
551         }
552
553         /* in some files DDSCAPS_TEXTURE is missing: silently ignore */
554         /*
555         if( !(header.caps.caps1 & DDSCAPS_TEXTURE) ) {
556                 return false;
557         }
558         */
559
560         return true;
561 }
562
563 bool DirectDrawSurface::isSupported() const
564 {
565         if (header.pf.flags & DDPF_FOURCC)
566         {
567                 if (header.pf.fourcc != FOURCC_DXT1 &&
568                     header.pf.fourcc != FOURCC_DXT2 &&
569                     header.pf.fourcc != FOURCC_DXT3 &&
570                     header.pf.fourcc != FOURCC_DXT4 &&
571                     header.pf.fourcc != FOURCC_DXT5 &&
572                     header.pf.fourcc != FOURCC_RXGB &&
573                     header.pf.fourcc != FOURCC_ATI1 &&
574                     header.pf.fourcc != FOURCC_ATI2)
575                 {
576                         // Unknown fourcc code.
577                         return false;
578                 }
579         }
580         else if (header.pf.flags & DDPF_RGB)
581         {
582                 // All RGB formats are supported now.
583         }
584         else
585         {
586                 return false;
587         }
588         
589         if (isTextureCube() && (header.caps.caps2 & DDSCAPS2_CUBEMAP_ALL_FACES) != DDSCAPS2_CUBEMAP_ALL_FACES)
590         {
591                 // Cubemaps must contain all faces.
592                 return false;
593         }
594         
595         if (isTexture3D())
596         {
597                 // @@ 3D textures not supported yet.
598                 return false;
599         }
600         
601         return true;
602 }
603
604
605 uint DirectDrawSurface::mipmapCount() const
606 {
607         if (header.flags & DDSD_MIPMAPCOUNT) return header.mipmapcount;
608         else return 0;
609 }
610
611
612 uint DirectDrawSurface::width() const
613 {
614         if (header.flags & DDSD_WIDTH) return header.width;
615         else return 1;
616 }
617
618 uint DirectDrawSurface::height() const
619 {
620         if (header.flags & DDSD_HEIGHT) return header.height;
621         else return 1;
622 }
623
624 uint DirectDrawSurface::depth() const
625 {
626         if (header.flags & DDSD_DEPTH) return header.depth;
627         else return 1;
628 }
629
630 bool DirectDrawSurface::hasAlpha() const
631 {
632         if ((header.pf.flags & DDPF_RGB) && (header.pf.amask == 0))
633         {
634                 return false;
635         }
636         else if (header.pf.fourcc == FOURCC_DXT1)
637         {
638                 return false;
639         }
640         else
641         {
642                 return true;
643         }
644 }
645
646 bool DirectDrawSurface::isTexture2D() const
647 {
648         return !isTexture3D() && !isTextureCube();
649 }
650
651 bool DirectDrawSurface::isTexture3D() const
652 {
653         return (header.caps.caps2 & DDSCAPS2_VOLUME) != 0;
654 }
655
656 bool DirectDrawSurface::isTextureCube() const
657 {
658         return (header.caps.caps2 & DDSCAPS2_CUBEMAP) != 0;
659 }
660
661 void DirectDrawSurface::mipmap(Image * img, uint face, uint mipmap)
662 {
663         stream.seek(offset(face, mipmap));
664         
665         uint w = width();
666         uint h = height();
667         
668         // Compute width and height.
669         for (uint m = 0; m < mipmap; m++)
670         {
671                 w = max(1U, w / 2);
672                 h = max(1U, h / 2);
673         }
674         
675         img->allocate(w, h);
676         
677         if (header.pf.flags & DDPF_RGB) 
678         {
679                 readLinearImage(img);
680         }
681         else if (header.pf.flags & DDPF_FOURCC)
682         {
683                 readBlockImage(img);
684         }
685 }
686
687 void DirectDrawSurface::readLinearImage(Image * img)
688 {
689         
690         const uint w = img->width();
691         const uint h = img->height();
692         
693         uint rshift, rsize;
694         PixelFormat::maskShiftAndSize(header.pf.rmask, &rshift, &rsize);
695         
696         uint gshift, gsize;
697         PixelFormat::maskShiftAndSize(header.pf.gmask, &gshift, &gsize);
698         
699         uint bshift, bsize;
700         PixelFormat::maskShiftAndSize(header.pf.bmask, &bshift, &bsize);
701         
702         uint ashift, asize;
703         PixelFormat::maskShiftAndSize(header.pf.amask, &ashift, &asize);
704
705         uint byteCount = (header.pf.bitcount + 7) / 8;
706
707         if (byteCount > 4)
708         {
709                 /* just in case... we could have segfaults later on if byteCount > 4 */
710                 printf("DDS: bitcount too large");
711                 return;
712         }
713
714         if (header.pf.amask != 0)
715         {
716                 img->setFormat(Image::Format_ARGB);
717         }
718
719         // Read linear RGB images.
720         for (uint y = 0; y < h; y++)
721         {
722                 for (uint x = 0; x < w; x++)
723                 {
724                         uint c = 0;
725                         mem_read(stream, (unsigned char *)(&c), byteCount);
726
727                         Color32 pixel(0, 0, 0, 0xFF);
728                         pixel.r = PixelFormat::convert(c >> rshift, rsize, 8);
729                         pixel.g = PixelFormat::convert(c >> gshift, gsize, 8);
730                         pixel.b = PixelFormat::convert(c >> bshift, bsize, 8);
731                         pixel.a = PixelFormat::convert(c >> ashift, asize, 8);
732
733                         img->pixel(x, y) = pixel;
734                 }
735         }
736 }
737
738 void DirectDrawSurface::readBlockImage(Image * img)
739 {
740         const uint w = img->width();
741         const uint h = img->height();
742         
743         const uint bw = (w + 3) / 4;
744         const uint bh = (h + 3) / 4;
745         
746         for (uint by = 0; by < bh; by++)
747         {
748                 for (uint bx = 0; bx < bw; bx++)
749                 {
750                         ColorBlock block;
751                         
752                         // Read color block.
753                         readBlock(&block);
754                         
755                         // Write color block.
756                         for (uint y = 0; y < min(4U, h-4*by); y++)
757                         {
758                                 for (uint x = 0; x < min(4U, w-4*bx); x++)
759                                 {
760                                         img->pixel(4*bx+x, 4*by+y) = block.color(x, y);
761                                 }
762                         }
763                 }
764         }
765 }
766
767 static Color32 buildNormal(uint8 x, uint8 y)
768 {
769         float nx = 2 * (x / 255.0f) - 1;
770         float ny = 2 * (y / 255.0f) - 1;
771         float nz = 0.0f;
772         if (1 - nx*nx - ny*ny > 0) nz = sqrtf(1 - nx*nx - ny*ny);
773         uint8 z = clamp(int(255.0f * (nz + 1) / 2.0f), 0, 255);
774         
775         return Color32(x, y, z);
776 }
777
778
779 void DirectDrawSurface::readBlock(ColorBlock * rgba)
780 {
781         if (header.pf.fourcc == FOURCC_DXT1)
782         {
783                 BlockDXT1 block;
784                 mem_read(stream, block);
785                 block.decodeBlock(rgba);
786         }
787         else if (header.pf.fourcc == FOURCC_DXT2 ||
788             header.pf.fourcc == FOURCC_DXT3)
789         {
790                 BlockDXT3 block;
791                 mem_read(stream, block);
792                 block.decodeBlock(rgba);
793         }
794         else if (header.pf.fourcc == FOURCC_DXT4 ||
795             header.pf.fourcc == FOURCC_DXT5 ||
796             header.pf.fourcc == FOURCC_RXGB)
797         {
798                 BlockDXT5 block;
799                 mem_read(stream, block);
800                 block.decodeBlock(rgba);
801                 
802                 if (header.pf.fourcc == FOURCC_RXGB)
803                 {
804                         // Swap R & A.
805                         for (int i = 0; i < 16; i++)
806                         {
807                                 Color32 & c = rgba->color(i);
808                                 uint tmp = c.r;
809                                 c.r = c.a;
810                                 c.a = tmp;
811                         }
812                 }
813         }
814         else if (header.pf.fourcc == FOURCC_ATI1)
815         {
816                 BlockATI1 block;
817                 mem_read(stream, block);
818                 block.decodeBlock(rgba);
819         }
820         else if (header.pf.fourcc == FOURCC_ATI2)
821         {
822                 BlockATI2 block;
823                 mem_read(stream, block);
824                 block.decodeBlock(rgba);
825         }
826         
827         // If normal flag set, convert to normal.
828         if (header.pf.flags & DDPF_NORMAL)
829         {
830                 if (header.pf.fourcc == FOURCC_ATI2)
831                 {
832                         for (int i = 0; i < 16; i++)
833                         {
834                                 Color32 & c = rgba->color(i);
835                                 c = buildNormal(c.r, c.g);
836                         }
837                 }
838                 else if (header.pf.fourcc == FOURCC_DXT5)
839                 {
840                         for (int i = 0; i < 16; i++)
841                         {
842                                 Color32 & c = rgba->color(i);
843                                 c = buildNormal(c.a, c.g);
844                         }
845                 }
846         }
847 }
848
849
850 uint DirectDrawSurface::blockSize() const
851 {
852         switch(header.pf.fourcc)
853         {
854                 case FOURCC_DXT1:
855                 case FOURCC_ATI1:
856                         return 8;
857                 case FOURCC_DXT2:
858                 case FOURCC_DXT3:
859                 case FOURCC_DXT4:
860                 case FOURCC_DXT5:
861                 case FOURCC_RXGB:
862                 case FOURCC_ATI2:
863                         return 16;
864         };
865
866         // Not a block image.
867         return 0;
868 }
869
870 uint DirectDrawSurface::mipmapSize(uint mipmap) const
871 {
872         uint w = width();
873         uint h = height();
874         uint d = depth();
875         
876         for (uint m = 0; m < mipmap; m++)
877         {
878                 w = max(1U, w / 2);
879                 h = max(1U, h / 2);
880                 d = max(1U, d / 2);
881         }
882
883         if (header.pf.flags & DDPF_FOURCC)
884         {
885                 // @@ How are 3D textures aligned?
886                 w = (w + 3) / 4;
887                 h = (h + 3) / 4;
888                 return blockSize() * w * h;
889         }
890         else if (header.pf.flags & DDPF_RGB)
891         {
892                 // Align pixels to bytes.
893                 uint byteCount = (header.pf.bitcount + 7) / 8;
894                 
895                 // Align pitch to 4 bytes.
896                 uint pitch = 4 * ((w * byteCount + 3) / 4);
897                 
898                 return pitch * h * d;
899         }
900         else {
901                 printf("DDS: mipmap format not supported\n");
902                 return(0);
903         };
904 }
905
906 uint DirectDrawSurface::faceSize() const
907 {
908         const uint count = mipmapCount();
909         uint size = 0;
910         
911         for (uint m = 0; m < count; m++)
912         {
913                 size += mipmapSize(m);
914         }
915         
916         return size;
917 }
918
919 uint DirectDrawSurface::offset(const uint face, const uint mipmap)
920 {
921         uint size = 128; //sizeof(DDSHeader);
922         
923         if (face != 0)
924         {
925                 size += face * faceSize();
926         }
927         
928         for (uint m = 0; m < mipmap; m++)
929         {
930                 size += mipmapSize(m);
931         }
932         
933         return size;
934 }
935
936
937 void DirectDrawSurface::printInfo() const
938 {
939         /* printf("FOURCC: %c%c%c%c\n", ((unsigned char *)&header.fourcc)[0], ((unsigned char *)&header.fourcc)[1], ((unsigned char *)&header.fourcc)[2], ((unsigned char *)&header.fourcc)[3]); */
940         printf("Flags: 0x%.8X\n", header.flags);
941         if (header.flags & DDSD_CAPS) printf("\tDDSD_CAPS\n");
942         if (header.flags & DDSD_PIXELFORMAT) printf("\tDDSD_PIXELFORMAT\n");
943         if (header.flags & DDSD_WIDTH) printf("\tDDSD_WIDTH\n");
944         if (header.flags & DDSD_HEIGHT) printf("\tDDSD_HEIGHT\n");
945         if (header.flags & DDSD_DEPTH) printf("\tDDSD_DEPTH\n");
946         if (header.flags & DDSD_PITCH) printf("\tDDSD_PITCH\n");
947         if (header.flags & DDSD_LINEARSIZE) printf("\tDDSD_LINEARSIZE\n");
948         if (header.flags & DDSD_MIPMAPCOUNT) printf("\tDDSD_MIPMAPCOUNT\n");
949
950         printf("Height: %d\n", header.height);
951         printf("Width: %d\n", header.width);
952         printf("Depth: %d\n", header.depth);
953         if (header.flags & DDSD_PITCH) printf("Pitch: %d\n", header.pitch);
954         else if (header.flags & DDSD_LINEARSIZE) printf("Linear size: %d\n", header.pitch);
955         printf("Mipmap count: %d\n", header.mipmapcount);
956         
957         printf("Pixel Format:\n");
958         /* printf("\tSize: %d\n", header.pf.size); */
959         printf("\tFlags: 0x%.8X\n", header.pf.flags);
960         if (header.pf.flags & DDPF_RGB) printf("\t\tDDPF_RGB\n");
961         if (header.pf.flags & DDPF_FOURCC) printf("\t\tDDPF_FOURCC\n");
962         if (header.pf.flags & DDPF_ALPHAPIXELS) printf("\t\tDDPF_ALPHAPIXELS\n");
963         if (header.pf.flags & DDPF_ALPHA) printf("\t\tDDPF_ALPHA\n");
964         if (header.pf.flags & DDPF_PALETTEINDEXED1) printf("\t\tDDPF_PALETTEINDEXED1\n");
965         if (header.pf.flags & DDPF_PALETTEINDEXED2) printf("\t\tDDPF_PALETTEINDEXED2\n");
966         if (header.pf.flags & DDPF_PALETTEINDEXED4) printf("\t\tDDPF_PALETTEINDEXED4\n");
967         if (header.pf.flags & DDPF_PALETTEINDEXED8) printf("\t\tDDPF_PALETTEINDEXED8\n");
968         if (header.pf.flags & DDPF_ALPHAPREMULT) printf("\t\tDDPF_ALPHAPREMULT\n");
969         if (header.pf.flags & DDPF_NORMAL) printf("\t\tDDPF_NORMAL\n");
970         
971         printf("\tFourCC: '%c%c%c%c'\n", ((header.pf.fourcc >> 0) & 0xFF), ((header.pf.fourcc >> 8) & 0xFF), ((header.pf.fourcc >> 16) & 0xFF), ((header.pf.fourcc >> 24) & 0xFF));
972         printf("\tBit count: %d\n", header.pf.bitcount);
973         printf("\tRed mask: 0x%.8X\n", header.pf.rmask);
974         printf("\tGreen mask: 0x%.8X\n", header.pf.gmask);
975         printf("\tBlue mask: 0x%.8X\n", header.pf.bmask);
976         printf("\tAlpha mask: 0x%.8X\n", header.pf.amask);
977
978         printf("Caps:\n");
979         printf("\tCaps 1: 0x%.8X\n", header.caps.caps1);
980         if (header.caps.caps1 & DDSCAPS_COMPLEX) printf("\t\tDDSCAPS_COMPLEX\n");
981         if (header.caps.caps1 & DDSCAPS_TEXTURE) printf("\t\tDDSCAPS_TEXTURE\n");
982         if (header.caps.caps1 & DDSCAPS_MIPMAP) printf("\t\tDDSCAPS_MIPMAP\n");
983
984         printf("\tCaps 2: 0x%.8X\n", header.caps.caps2);
985         if (header.caps.caps2 & DDSCAPS2_VOLUME) printf("\t\tDDSCAPS2_VOLUME\n");
986         else if (header.caps.caps2 & DDSCAPS2_CUBEMAP)
987         {
988                 printf("\t\tDDSCAPS2_CUBEMAP\n");
989                 if ((header.caps.caps2 & DDSCAPS2_CUBEMAP_ALL_FACES) == DDSCAPS2_CUBEMAP_ALL_FACES) printf("\t\tDDSCAPS2_CUBEMAP_ALL_FACES\n");
990                 else {
991                         if (header.caps.caps2 & DDSCAPS2_CUBEMAP_POSITIVEX) printf("\t\tDDSCAPS2_CUBEMAP_POSITIVEX\n");
992                         if (header.caps.caps2 & DDSCAPS2_CUBEMAP_NEGATIVEX) printf("\t\tDDSCAPS2_CUBEMAP_NEGATIVEX\n");
993                         if (header.caps.caps2 & DDSCAPS2_CUBEMAP_POSITIVEY) printf("\t\tDDSCAPS2_CUBEMAP_POSITIVEY\n");
994                         if (header.caps.caps2 & DDSCAPS2_CUBEMAP_NEGATIVEY) printf("\t\tDDSCAPS2_CUBEMAP_NEGATIVEY\n");
995                         if (header.caps.caps2 & DDSCAPS2_CUBEMAP_POSITIVEZ) printf("\t\tDDSCAPS2_CUBEMAP_POSITIVEZ\n");
996                         if (header.caps.caps2 & DDSCAPS2_CUBEMAP_NEGATIVEZ) printf("\t\tDDSCAPS2_CUBEMAP_NEGATIVEZ\n");
997                 }
998         }
999
1000         printf("\tCaps 3: 0x%.8X\n", header.caps.caps3);
1001         printf("\tCaps 4: 0x%.8X\n", header.caps.caps4);
1002
1003         if (header.pf.flags == 0)
1004         {
1005                 printf("DX10 Header:\n");
1006                 printf("\tDXGI Format: %u\n", header.header10.dxgiFormat);
1007                 printf("\tResource dimension: %u\n", header.header10.resourceDimension);
1008                 printf("\tMisc flag: %u\n", header.header10.miscFlag);
1009                 printf("\tArray size: %u\n", header.header10.arraySize);
1010         }
1011         
1012         if (header.reserved[9] == MAKEFOURCC('N', 'V', 'T', 'T'))
1013         {
1014                 int major = (header.reserved[10] >> 16) & 0xFF;
1015                 int minor = (header.reserved[10] >> 8) & 0xFF;
1016                 int revision= header.reserved[10] & 0xFF;
1017                 
1018                 printf("Version:\n");
1019                 printf("\tNVIDIA Texture Tools %d.%d.%d\n", major, minor, revision);
1020         }
1021 }
1022