11#include " DDS.h"
2+ #include " File.h"
23
34#include < iostream>
45#include < fstream>
@@ -10,6 +11,7 @@ void ConvertDDSToPCD(const char* filePath)
1011 // If there was a failure to open the file we must exit
1112 if (!ifs.good ())
1213 {
14+ std::cout << " Error: failed to open the file!" << std::endl;
1315 ifs.close ();
1416 return ;
1517 }
@@ -22,8 +24,17 @@ void ConvertDDSToPCD(const char* filePath)
2224 // Load texture data header
2325 char * fileBuffer = new char [sizeof (DDSHeader)];
2426 ifs.read (fileBuffer, sizeof (DDSHeader));
27+
2528 DDSHeader* ddsHeader = nullptr ;
2629 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+
2738 char * textureData = new char [fileSize-0x80 ];
2839 ifs.seekg (0x80 , SEEK_SET);
2940 ifs.read (textureData, fileSize - 0x80 );
@@ -35,22 +46,6 @@ void ConvertDDSToPCD(const char* filePath)
3546
3647 std::ofstream ofs (nameBuff, std::ios::binary);
3748
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-
5449 //
5550 std::string path (filePath);
5651 std::string filename;
@@ -71,27 +66,24 @@ void ConvertDDSToPCD(const char* filePath)
7166 unsigned int hash;
7267 sscanf (filename.c_str (), " %x" , &hash);
7368
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 ));
9587
9688 ofs.flush ();
9789 ofs.close ();
0 commit comments