summaryrefslogtreecommitdiff
path: root/Build/source/utils/asymptote/prc/PRCTools/iPRCFile.cc
blob: 02967a5478b11e80f38d0422f6b281d461ebaaf3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
/************
*
*   This file is part of a tool for reading 3D content in the PRC format.
*   Copyright (C) 2008 Orest Shardt <shardtor (at) gmail dot com>
*
*   This program is free software: you can redistribute it and/or modify
*   it under the terms of the GNU Lesser General Public License as published by
*   the Free Software Foundation, either version 3 of the License, or
*   (at your option) any later version.
*
*   This program is distributed in the hope that it will be useful,
*   but WITHOUT ANY WARRANTY; without even the implied warranty of
*   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
*   GNU Lesser General Public License for more details.
*
*   You should have received a copy of the GNU Lesser General Public License
*   along with this program.  If not, see <http://www.gnu.org/licenses/>.
*
*************/

#include "bitData.h"
#include "iPRCFile.h"
#include "describePRC.h"

using std::vector; using std::istream; using std::ios;
using std::cout; using std::endl; using std::cerr;
using std::string;

using std::ofstream;
using std::ostringstream;

void iPRCFile::dumpSections(string prefix)
{
  ofstream out;

  for(unsigned int i = 0; i < fileStructures.size(); ++i)
  {
    ostringstream name;
    name << prefix << "Structure" << i;

    out.open((name.str()+"-Globals.bin").c_str());
    out.write(fileStructures[i].sections[GLOBALS_SECTION],fileStructures[i].sectionLengths[GLOBALS_SECTION]);
    out.close();

    out.open((name.str()+"-Tree.bin").c_str());
    out.write(fileStructures[i].sections[TREE_SECTION],fileStructures[i].sectionLengths[TREE_SECTION]);
    out.close();

    out.open((name.str()+"-Tessellation.bin").c_str());
    out.write(fileStructures[i].sections[TESSELLATION_SECTION],fileStructures[i].sectionLengths[TESSELLATION_SECTION]);
    out.close();

    out.open((name.str()+"-Geometry.bin").c_str());
    out.write(fileStructures[i].sections[GEOMETRY_SECTION],fileStructures[i].sectionLengths[GEOMETRY_SECTION]);
    out.close();

    out.open((name.str()+"-ExtraGeometry.bin").c_str());
    out.write(fileStructures[i].sections[EXTRA_GEOMETRY_SECTION],fileStructures[i].sectionLengths[EXTRA_GEOMETRY_SECTION]);
    out.close();
  }
  out.open((prefix+"-ModelFile.bin").c_str());
  out.write(modelFileData,modelFileLength);
  out.close();
}

void iPRCFile::describe()
{
  /*
  for(int i = 0; i < modelFileLength; ++i)
  {
    cout << ' ' << std::hex << std::setw(2) << std::setfill('0') << static_cast<unsigned int>(static_cast<unsigned char>(mfd.readChar()));
    if(i%16 == 15)
      cout << endl;
  }
  cout << endl;
  */

  unFlushSerialization();
  for(unsigned int i = 0; i < fileStructures.size(); ++i)
  {
    cout << "File Structure " << i << ":" << endl;

    //describe header
    char *header = buffer + fileStructureInfos[i].offsets[0];
    cout << "--Header Section--" << endl;
    cout << "  Signature " << header[0] << header[1] << header[2] << endl;
    cout << "  Minimal version for read " << *(unsigned int*)(header+3) << endl;
    cout << "  Authoring version " << *(unsigned int*)(header+7) << endl;
    cout << std::hex;
    cout << "  File structure UUID " << *(unsigned int*)(header+11) << ' ' << *(unsigned int*)(header+15) << ' ' 
        << *(unsigned int*)(header+19) << ' ' << *(unsigned int*)(header+23) << endl;
    cout << "  Application UUID " << *(unsigned int*)(header+27) << ' ' << *(unsigned int*)(header+31) << ' ' 
        << *(unsigned int*)(header+35) << ' ' << *(unsigned int*)(header+39) << endl;
    cout << std::dec;
    // uncompressed files
    unsigned int numberOfUncompressedFiles = *(unsigned int*)(header+43);
    cout << "Number of uncompressed files " << numberOfUncompressedFiles << endl;
    char *position = header+47;
    for(unsigned int j = 0; j < numberOfUncompressedFiles; ++j)
    {
      cout << "Uncompressed file " << j << ":" << endl;
      unsigned int size = *(unsigned int*)position;
      cout << "  size " << size << " bytes" << endl;
      position += size+sizeof(unsigned int);
    }

    BitByBitData fileStruct(fileStructures[i].sections[GLOBALS_SECTION],fileStructures[i].sectionLengths[GLOBALS_SECTION]);
    describeSchema(fileStruct);
    describeGlobals(fileStruct);
    unFlushSerialization();

    fileStruct = BitByBitData(fileStructures[i].sections[TREE_SECTION],fileStructures[i].sectionLengths[TREE_SECTION]);
    describeTree(fileStruct);
    unFlushSerialization();

    fileStruct = BitByBitData(fileStructures[i].sections[TESSELLATION_SECTION],fileStructures[i].sectionLengths[TESSELLATION_SECTION]);
    describeTessellation(fileStruct);
    unFlushSerialization();

    fileStruct = BitByBitData(fileStructures[i].sections[GEOMETRY_SECTION],fileStructures[i].sectionLengths[GEOMETRY_SECTION]);
    describeGeometry(fileStruct);
    unFlushSerialization();

    fileStruct = BitByBitData(fileStructures[i].sections[EXTRA_GEOMETRY_SECTION],fileStructures[i].sectionLengths[EXTRA_GEOMETRY_SECTION]);
    describeExtraGeometry(fileStruct);
    unFlushSerialization();
  }

  BitByBitData mfd(modelFileData,modelFileLength);

  describeSchema(mfd);
  describeModelFileData(mfd,fileStructures.size());
  unFlushSerialization();
}

iPRCFile::iPRCFile(istream& in)
{
  char PRC[3];
  in.read(PRC,3);
  if(PRC[0] != 'P' || PRC[1] != 'R' || PRC[2] != 'C')
  {
    cerr << "Error: Invalid file format: PRC not found." << endl;
  }
  unsigned int versionForRead,authoringVersion;
  in.read((char*)&versionForRead,sizeof(versionForRead));
  in.read((char*)&authoringVersion,sizeof(authoringVersion));
  cout << "Version for reading " << versionForRead << endl;
  cout << "Authoring version " << authoringVersion << endl;
  unsigned int fileStructureUUID[4];
  in.read((char*)fileStructureUUID,sizeof(fileStructureUUID));
  //(void*) is for formatting
  cout << "File structure UUID " << (void*)(fileStructureUUID[0]) << ' ' << (void*)(fileStructureUUID[1]) << ' '
      << (void*)(fileStructureUUID[2]) << ' ' << (void*)(fileStructureUUID[3]) << endl;
  unsigned int applicationUUID[4];
  in.read((char*)applicationUUID,sizeof(applicationUUID));
  cout << "Application UUID " << (void*)(applicationUUID[0]) << ' ' << (void*)(applicationUUID[1]) << ' '
      << (void*)(applicationUUID[2]) << ' ' << (void*)(applicationUUID[3]) << endl;
  unsigned int numberOfFileStructures;
  in.read((char*)&numberOfFileStructures,sizeof(numberOfFileStructures));
  cout << "number of file structures " << numberOfFileStructures << endl;

  // load fileStructureInformation
  for(unsigned int fsi = 0; fsi < numberOfFileStructures; ++fsi)
  {
    FileStructureInformation info;
    in.read((char*)&info.UUID,sizeof(info.UUID));
    cout << "\tFile structure UUID " << (void*)(info.UUID[0]) << ' ' << (void*)(info.UUID[1]) << ' '
        << (void*)(info.UUID[2]) << ' ' << (void*)(info.UUID[3]) << endl;

    in.read((char*)&info.reserved,sizeof(info.reserved));
    cout << "\tReserved " << info.reserved << endl;

    unsigned int numberOfOffsets;
    in.read((char*)&numberOfOffsets,sizeof(numberOfOffsets));
    cout << "\tNumber of Offsets " << numberOfOffsets << endl;

    for(unsigned int oi = 0; oi < numberOfOffsets; ++oi)
    {
      unsigned int offset;
      in.read((char*)&offset,sizeof(offset));
      info.offsets.push_back(offset);
      cout << "\t\tOffset " << offset << endl;
    }
    fileStructureInfos.push_back(info);
  }
  in.read((char*)&modelFileOffset,sizeof(modelFileOffset));
  cout << "Model file offset " << modelFileOffset << endl;
  in.read((char*)&fileSize,sizeof(fileSize)); // this is not documented
  cout << "File size " << fileSize << endl;

  in.read((char*)&numberOfUncompressedFiles,sizeof(numberOfUncompressedFiles));
  cout << "Number of uncompressed files " << numberOfUncompressedFiles << endl;
  for(unsigned int ufi = 0; ufi < numberOfUncompressedFiles; ++ufi)
  {
    unsigned int size;
    in.read((char*)&size,sizeof(size));
    in.seekg(size,ios::cur);
  }

  //read the whole file into memory
  in.seekg(0,ios::beg);
  buffer = new char[fileSize];
  if(!buffer) cerr << "Couldn't get memory." << endl;
  in.read(buffer,fileSize);
  //decompress fileStructures
  for(unsigned int fs = 0; fs < fileStructureInfos.size(); ++fs)
  {
    fileStructures.push_back(FileStructure());
    for(unsigned int i = 1; i < fileStructureInfos[fs].offsets.size(); ++i) // start at 1 since header is decompressed
    {
      fileStructures[fs].sections[i-1] = NULL;
      unsigned int offset = fileStructureInfos[fs].offsets[i];
      fileStructures[fs].sectionLengths[i-1] = decompress(buffer+offset,fileSize-offset,fileStructures[fs].sections[i-1]);
    }
  }

  //decompress modelFileData
  modelFileData = NULL;
  modelFileLength = decompress(buffer+modelFileOffset,fileSize-modelFileOffset,modelFileData);
}