66
77void ConvertPCDToDDS (const char * filePath)
88{
9+ #ifndef PS3
910 std::ifstream ifs (filePath, std::ios::binary);
1011
1112 // If there was a failure to open the file we must exit
@@ -45,18 +46,18 @@ void ConvertPCDToDDS(const char* filePath)
4546 ifs.seekg (24 , SEEK_SET);
4647
4748 // Load texture data header
48- char * fileBuffer = new char [sizeof (PCTextureDataHeader )];
49- ifs.read (fileBuffer, sizeof (PCTextureDataHeader ));
50-
51- PCTextureDataHeader * pcdHeader = nullptr ;
52- pcdHeader = (PCTextureDataHeader*)fileBuffer;
49+ char * fileBuffer = new char [sizeof (cdc::PC::Texture::Header )];
50+ ifs.read (fileBuffer, sizeof (cdc::PC::Texture::Header ));
51+
52+ cdc::PC::Texture::Header * pcdHeader = reinterpret_cast <cdc::PC::Texture::Header*>(fileBuffer) ;
53+
5354 if (pcdHeader->m_magic != PCD_MAGIC)
5455 {
5556 std::cout << " Error PCD magic mis-match!" << std::endl;
5657 ifs.close ();
5758 return ;
5859 }
59-
60+
6061 char * textureData = new char [pcdHeader->m_textureDataSize ];
6162 ifs.read (textureData, pcdHeader->m_textureDataSize );
6263 ifs.close ();
@@ -104,4 +105,129 @@ void ConvertPCDToDDS(const char* filePath)
104105 ofs.close ();
105106 delete[] fileBuffer;
106107 delete[] textureData;
107- }
108+ #else
109+ std::ifstream ifs (filePath, std::ios::binary);
110+
111+ // If there was a failure to open the file we must exit
112+ if (!ifs.good ())
113+ {
114+ std::cout << " Error: failed to open the file!" << std::endl;
115+ ifs.close ();
116+ return ;
117+ }
118+
119+ // Check to see if file has valid section header
120+ unsigned int sectionMagic = ReadUInt (ifs);
121+
122+ if (sectionMagic != SECTION_MAGIC)
123+ {
124+ std::cout << " Error: section magic mis-match!" << std::endl;
125+ ifs.close ();
126+ return ;
127+ }
128+
129+ unsigned int sectionSize = ReadUInt (ifs);
130+ if (sectionSize <= 0 )
131+ {
132+ std::cout << " Error: section corrupt, size is <= 0!" << std::endl;
133+ ifs.close ();
134+ return ;
135+ }
136+
137+ unsigned char sectionType = ReadUByte (ifs);
138+ if (sectionType != TEXTURE_SECTION_TYPE)
139+ {
140+ std::cout << " Error: invalid section type!" << std::endl;
141+ ifs.close ();
142+ return ;
143+ }
144+
145+ // Skip past section header
146+ ifs.seekg (24 , SEEK_SET);
147+
148+ // Load texture data header
149+ char * fileBuffer = new char [sizeof (cdc::ps3::Texture::Header)];
150+ ifs.read (fileBuffer, sizeof (cdc::ps3::Texture::Header));
151+
152+ cdc::ps3::Texture::Header* ps3tHeader = reinterpret_cast <cdc::ps3::Texture::Header*>(fileBuffer);
153+
154+ // Endian swap
155+ *&ps3tHeader->m_magic = ReverseUInt (*&ps3tHeader->m_magic );
156+ *&ps3tHeader->m_textureDataSize = ReverseUInt (*&ps3tHeader->m_textureDataSize );
157+ *&ps3tHeader->m_width = ReverseUShort (*&ps3tHeader->m_width );
158+ *&ps3tHeader->m_height = ReverseUShort (*&ps3tHeader->m_height );
159+
160+ // Swap
161+ if (ps3tHeader->m_magic != PS3T_MAGIC)
162+ {
163+ std::cout << " Error PS3T magic mis-match!" << std::endl;
164+ ifs.close ();
165+ return ;
166+ }
167+
168+ char * textureData = new char [ps3tHeader->m_textureDataSize ];
169+ ifs.read (textureData, ps3tHeader->m_textureDataSize );
170+ ifs.close ();
171+
172+ char nameBuff[128 ];
173+ memset (nameBuff, 0 , 128 );
174+ sprintf_s (nameBuff, " %s%s" , filePath, " .dds" );
175+
176+ std::ofstream ofs (nameBuff, std::ios::binary);
177+
178+ unsigned int ddsSize = 0x7C ;
179+ unsigned int ddsFlags = 0x000A1007 ;
180+ unsigned int ddsPitchOrLinearSize = 0x00010000 ;
181+ unsigned int ddsDummy = 0 ;
182+ unsigned int ddsHeight = ps3tHeader->m_height ;
183+ unsigned int ddsWidth = ps3tHeader->m_width ;
184+ unsigned int ddsDepth = 0 ;// ;ps3tHeader->m_depth;
185+ unsigned int ddsMipCount = 0 ;// ps3tHeader->m_numMipMaps;
186+ unsigned int ddsFormat = cdc::ps3::Texture::getFormat (ps3tHeader->m_format );
187+ unsigned int ddsUnk00 = 0x00401008 ;
188+
189+ WriteUInt (ofs, DDS_MAGIC);
190+ WriteUInt (ofs, ddsSize);
191+ WriteUInt (ofs, ddsFlags);
192+ WriteUInt (ofs, ddsHeight);
193+
194+ WriteUInt (ofs, ddsWidth);
195+ WriteUInt (ofs, ddsPitchOrLinearSize);
196+ WriteUInt (ofs, ddsDummy);
197+ WriteUInt (ofs, ddsDepth);
198+
199+ WriteUInt (ofs, ddsMipCount);
200+
201+ // Reserved
202+ ofs.seekp (12 * sizeof (unsigned int ), SEEK_CUR);
203+
204+ WriteUInt (ofs, ddsFormat);
205+ ofs.seekp (0x14 , SEEK_CUR);
206+ WriteUInt (ofs, ddsUnk00);
207+ ofs.seekp (0x10 , SEEK_CUR);
208+
209+ ofs.write (textureData, ps3tHeader->m_textureDataSize );
210+
211+ ofs.flush ();
212+ ofs.close ();
213+ delete[] fileBuffer;
214+ delete[] textureData;
215+ #endif
216+ }
217+
218+ unsigned int cdc::ps3::Texture::getFormat (unsigned int format)
219+ {
220+ switch (format)
221+ {
222+ case cdc::ps3::Texture::kFormat ::DXT1:
223+ return 0x31545844 ;
224+ break ;
225+ case cdc::ps3::Texture::kFormat ::DXT5:
226+ return 0x35545844 ;
227+ default :
228+ assert (false );
229+ break ;
230+ }
231+
232+ return -1 ;
233+ }
0 commit comments