1
1
#include " DDS.h"
2
+ #include " File.h"
2
3
3
4
#include < iostream>
4
5
#include < fstream>
@@ -10,6 +11,7 @@ void ConvertDDSToPCD(const char* filePath)
10
11
// If there was a failure to open the file we must exit
11
12
if (!ifs.good ())
12
13
{
14
+ std::cout << " Error: failed to open the file!" << std::endl;
13
15
ifs.close ();
14
16
return ;
15
17
}
@@ -22,8 +24,17 @@ void ConvertDDSToPCD(const char* filePath)
22
24
// Load texture data header
23
25
char * fileBuffer = new char [sizeof (DDSHeader)];
24
26
ifs.read (fileBuffer, sizeof (DDSHeader));
27
+
25
28
DDSHeader* ddsHeader = nullptr ;
26
29
ddsHeader = (DDSHeader*)fileBuffer;
30
+ if (ddsHeader->m_magic != DDS_MAGIC)
31
+ {
32
+ std::cout << " Error: DDS magic mis-match!" << std::endl;
33
+ ifs.close ();
34
+ delete[] fileBuffer;
35
+ return ;
36
+ }
37
+
27
38
char * textureData = new char [fileSize-0x80 ];
28
39
ifs.seekg (0x80 , SEEK_SET);
29
40
ifs.read (textureData, fileSize - 0x80 );
@@ -35,22 +46,6 @@ void ConvertDDSToPCD(const char* filePath)
35
46
36
47
std::ofstream ofs (nameBuff, std::ios::binary);
37
48
38
- unsigned int pcdMagic = 0x39444350 ;
39
- unsigned int pcdFormat = ddsHeader->m_format ;
40
- unsigned int pcdTextureDataSize = fileSize - 0x80 ;
41
- unsigned int pcdPaletteDataSize = 0 ;
42
- unsigned short pcdWidth = ddsHeader->m_width ;// /@TODO assert max unsigned short
43
- unsigned short pcdHeight = ddsHeader->m_height ;// /@TODO assert max unsigned short
44
- unsigned char pcdDepth = ddsHeader->m_depth ;// /@TODO assert max unsigned char
45
- unsigned char pcdMipCount = ddsHeader->m_mipCount ;// /@TODO assert max unsigned char
46
- unsigned short pcdFlags = 0x3 ; // ??FILTER_ANISOTROPIC_1X = 0x3,
47
-
48
- // Write section header
49
- unsigned int sectionMagic = 0x54434553 ;
50
- unsigned int sectionFileSize = pcdTextureDataSize + 0x18 ;
51
- unsigned int Type = 0x5 ;
52
- unsigned int headerSize = 0 ;
53
-
54
49
//
55
50
std::string path (filePath);
56
51
std::string filename;
@@ -71,27 +66,24 @@ void ConvertDDSToPCD(const char* filePath)
71
66
unsigned int hash;
72
67
sscanf (filename.c_str (), " %x" , &hash);
73
68
74
- unsigned int lang = 0xFFFFFFFF ;
75
- ofs.write ((char *)§ionMagic, sizeof (unsigned int ));
76
- ofs.write ((char *)§ionFileSize, sizeof (unsigned int ));
77
- ofs.write ((char *)&Type, sizeof (unsigned int ));
78
- ofs.write ((char *)&headerSize, sizeof (unsigned int ));
79
- ofs.write ((char *)&hash, sizeof (unsigned int ));
80
- ofs.write ((char *)&lang, sizeof (unsigned int ));
81
-
82
- ofs.write ((char *)&pcdMagic, sizeof (unsigned int ));
83
- ofs.write ((char *)&pcdFormat, sizeof (unsigned int ));
84
- ofs.write ((char *)&pcdTextureDataSize, sizeof (unsigned int ));
85
- ofs.write ((char *)&pcdPaletteDataSize, sizeof (unsigned int ));
86
-
87
- ofs.write ((char *)&pcdWidth, sizeof (unsigned short ));
88
- ofs.write ((char *)&pcdHeight, sizeof (unsigned short ));
89
-
90
- ofs.write ((char *)&pcdDepth, sizeof (unsigned char ));
91
- ofs.write ((char *)&pcdMipCount, sizeof (unsigned char ));
92
- ofs.write ((char *)&pcdFlags, sizeof (unsigned short ));
93
-
94
- ofs.write (textureData, fileSize - 0x80 );
69
+ WriteUInt (ofs, SECTION_MAGIC);
70
+ WriteUInt (ofs, ((fileSize-0x80 ) + 0x18 ));
71
+ WriteUInt (ofs, (TEXTURE_SECTION_TYPE));
72
+ WriteUInt (ofs, 0 );
73
+ WriteUInt (ofs, hash);
74
+ WriteUInt (ofs, 0xFFFFFFFF );
75
+
76
+ WriteUInt (ofs, PCD_MAGIC);
77
+ WriteUInt (ofs, ddsHeader->m_format );
78
+ WriteUInt (ofs, (fileSize - 0x80 ));
79
+ WriteUInt (ofs, 0 );
80
+ WriteUShort (ofs, ddsHeader->m_width );
81
+ WriteUShort (ofs, ddsHeader->m_height );
82
+ WriteUByte (ofs, ddsHeader->m_depth );
83
+ WriteUByte (ofs, ddsHeader->m_mipCount );
84
+ WriteUShort (ofs, 3 );
85
+
86
+ ofs.write (textureData, (fileSize - 0x80 ));
95
87
96
88
ofs.flush ();
97
89
ofs.close ();
0 commit comments