o3dgcBinaryStream.h 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433
  1. /*
  2. Copyright (c) 2013 Khaled Mammou - Advanced Micro Devices, Inc.
  3. Permission is hereby granted, free of charge, to any person obtaining a copy
  4. of this software and associated documentation files (the "Software"), to deal
  5. in the Software without restriction, including without limitation the rights
  6. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  7. copies of the Software, and to permit persons to whom the Software is
  8. furnished to do so, subject to the following conditions:
  9. The above copyright notice and this permission notice shall be included in
  10. all copies or substantial portions of the Software.
  11. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  12. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  13. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  14. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  15. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  16. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  17. THE SOFTWARE.
  18. */
  19. #pragma once
  20. #ifndef O3DGC_BINARY_STREAM_H
  21. #define O3DGC_BINARY_STREAM_H
  22. #include "o3dgcCommon.h"
  23. #include "o3dgcVector.h"
  24. namespace o3dgc
  25. {
  26. const unsigned long O3DGC_BINARY_STREAM_DEFAULT_SIZE = 4096;
  27. const unsigned long O3DGC_BINARY_STREAM_BITS_PER_SYMBOL0 = 7;
  28. const unsigned long O3DGC_BINARY_STREAM_MAX_SYMBOL0 = (1 << O3DGC_BINARY_STREAM_BITS_PER_SYMBOL0) - 1;
  29. const unsigned long O3DGC_BINARY_STREAM_BITS_PER_SYMBOL1 = 6;
  30. const unsigned long O3DGC_BINARY_STREAM_MAX_SYMBOL1 = (1 << O3DGC_BINARY_STREAM_BITS_PER_SYMBOL1) - 1;
  31. const unsigned long O3DGC_BINARY_STREAM_NUM_SYMBOLS_UINT32 = (32+O3DGC_BINARY_STREAM_BITS_PER_SYMBOL0-1) /
  32. O3DGC_BINARY_STREAM_BITS_PER_SYMBOL0;
  33. //!
  34. class BinaryStream
  35. {
  36. public:
  37. //! Constructor.
  38. BinaryStream(unsigned long size = O3DGC_BINARY_STREAM_DEFAULT_SIZE)
  39. {
  40. m_endianness = SystemEndianness();
  41. m_stream.Allocate(size);
  42. };
  43. //! Destructor.
  44. ~BinaryStream(void){};
  45. void WriteFloat32(float value, O3DGCStreamType streamType)
  46. {
  47. if (streamType == O3DGC_STREAM_TYPE_ASCII)
  48. {
  49. WriteFloat32ASCII(value);
  50. }
  51. else
  52. {
  53. WriteFloat32Bin(value);
  54. }
  55. }
  56. void WriteUInt32(unsigned long position, unsigned long value, O3DGCStreamType streamType)
  57. {
  58. if (streamType == O3DGC_STREAM_TYPE_ASCII)
  59. {
  60. WriteUInt32ASCII(position, value);
  61. }
  62. else
  63. {
  64. WriteUInt32Bin(position, value);
  65. }
  66. }
  67. void WriteUInt32(unsigned long value, O3DGCStreamType streamType)
  68. {
  69. if (streamType == O3DGC_STREAM_TYPE_ASCII)
  70. {
  71. WriteUInt32ASCII(value);
  72. }
  73. else
  74. {
  75. WriteUInt32Bin(value);
  76. }
  77. }
  78. void WriteUChar(unsigned int position, unsigned char value, O3DGCStreamType streamType)
  79. {
  80. if (streamType == O3DGC_STREAM_TYPE_ASCII)
  81. {
  82. WriteUInt32ASCII(position, value);
  83. }
  84. else
  85. {
  86. WriteUInt32Bin(position, value);
  87. }
  88. }
  89. void WriteUChar(unsigned char value, O3DGCStreamType streamType)
  90. {
  91. if (streamType == O3DGC_STREAM_TYPE_ASCII)
  92. {
  93. WriteUCharASCII(value);
  94. }
  95. else
  96. {
  97. WriteUChar8Bin(value);
  98. }
  99. }
  100. float ReadFloat32(unsigned long & position, O3DGCStreamType streamType) const
  101. {
  102. float value;
  103. if (streamType == O3DGC_STREAM_TYPE_ASCII)
  104. {
  105. value = ReadFloat32ASCII(position);
  106. }
  107. else
  108. {
  109. value = ReadFloat32Bin(position);
  110. }
  111. return value;
  112. }
  113. unsigned long ReadUInt32(unsigned long & position, O3DGCStreamType streamType) const
  114. {
  115. unsigned long value;
  116. if (streamType == O3DGC_STREAM_TYPE_ASCII)
  117. {
  118. value = ReadUInt32ASCII(position);
  119. }
  120. else
  121. {
  122. value = ReadUInt32Bin(position);
  123. }
  124. return value;
  125. }
  126. unsigned char ReadUChar(unsigned long & position, O3DGCStreamType streamType) const
  127. {
  128. unsigned char value;
  129. if (streamType == O3DGC_STREAM_TYPE_ASCII)
  130. {
  131. value = ReadUCharASCII(position);
  132. }
  133. else
  134. {
  135. value = ReadUChar8Bin(position);
  136. }
  137. return value;
  138. }
  139. void WriteFloat32Bin(unsigned long position, float value)
  140. {
  141. assert(position < m_stream.GetSize() - 4);
  142. unsigned char * ptr = (unsigned char *) (&value);
  143. if (m_endianness == O3DGC_BIG_ENDIAN)
  144. {
  145. m_stream[position++] = ptr[3];
  146. m_stream[position++] = ptr[2];
  147. m_stream[position++] = ptr[1];
  148. m_stream[position ] = ptr[0];
  149. }
  150. else
  151. {
  152. m_stream[position++] = ptr[0];
  153. m_stream[position++] = ptr[1];
  154. m_stream[position++] = ptr[2];
  155. m_stream[position ] = ptr[3];
  156. }
  157. }
  158. void WriteFloat32Bin(float value)
  159. {
  160. unsigned char * ptr = (unsigned char *) (&value);
  161. if (m_endianness == O3DGC_BIG_ENDIAN)
  162. {
  163. m_stream.PushBack(ptr[3]);
  164. m_stream.PushBack(ptr[2]);
  165. m_stream.PushBack(ptr[1]);
  166. m_stream.PushBack(ptr[0]);
  167. }
  168. else
  169. {
  170. m_stream.PushBack(ptr[0]);
  171. m_stream.PushBack(ptr[1]);
  172. m_stream.PushBack(ptr[2]);
  173. m_stream.PushBack(ptr[3]);
  174. }
  175. }
  176. void WriteUInt32Bin(unsigned long position, unsigned long value)
  177. {
  178. assert(position < m_stream.GetSize() - 4);
  179. unsigned char * ptr = (unsigned char *) (&value);
  180. if (m_endianness == O3DGC_BIG_ENDIAN)
  181. {
  182. m_stream[position++] = ptr[3];
  183. m_stream[position++] = ptr[2];
  184. m_stream[position++] = ptr[1];
  185. m_stream[position ] = ptr[0];
  186. }
  187. else
  188. {
  189. m_stream[position++] = ptr[0];
  190. m_stream[position++] = ptr[1];
  191. m_stream[position++] = ptr[2];
  192. m_stream[position ] = ptr[3];
  193. }
  194. }
  195. void WriteUInt32Bin(unsigned long value)
  196. {
  197. unsigned char * ptr = (unsigned char *) (&value);
  198. if (m_endianness == O3DGC_BIG_ENDIAN)
  199. {
  200. m_stream.PushBack(ptr[3]);
  201. m_stream.PushBack(ptr[2]);
  202. m_stream.PushBack(ptr[1]);
  203. m_stream.PushBack(ptr[0]);
  204. }
  205. else
  206. {
  207. m_stream.PushBack(ptr[0]);
  208. m_stream.PushBack(ptr[1]);
  209. m_stream.PushBack(ptr[2]);
  210. m_stream.PushBack(ptr[3]);
  211. }
  212. }
  213. void WriteUChar8Bin(unsigned int position, unsigned char value)
  214. {
  215. m_stream[position] = value;
  216. }
  217. void WriteUChar8Bin(unsigned char value)
  218. {
  219. m_stream.PushBack(value);
  220. }
  221. float ReadFloat32Bin(unsigned long & position) const
  222. {
  223. unsigned long value = ReadUInt32Bin(position);
  224. float fvalue;
  225. memcpy(&fvalue, &value, 4);
  226. return fvalue;
  227. }
  228. unsigned long ReadUInt32Bin(unsigned long & position) const
  229. {
  230. assert(position < m_stream.GetSize() - 4);
  231. unsigned long value = 0;
  232. if (m_endianness == O3DGC_BIG_ENDIAN)
  233. {
  234. value += (m_stream[position++]<<24);
  235. value += (m_stream[position++]<<16);
  236. value += (m_stream[position++]<<8);
  237. value += (m_stream[position++]);
  238. }
  239. else
  240. {
  241. value += (m_stream[position++]);
  242. value += (m_stream[position++]<<8);
  243. value += (m_stream[position++]<<16);
  244. value += (m_stream[position++]<<24);
  245. }
  246. return value;
  247. }
  248. unsigned char ReadUChar8Bin(unsigned long & position) const
  249. {
  250. return m_stream[position++];
  251. }
  252. void WriteFloat32ASCII(float value)
  253. {
  254. unsigned long uiValue;
  255. memcpy(&uiValue, &value, 4);
  256. WriteUInt32ASCII(uiValue);
  257. }
  258. void WriteUInt32ASCII(unsigned long position, unsigned long value)
  259. {
  260. assert(position < m_stream.GetSize() - O3DGC_BINARY_STREAM_NUM_SYMBOLS_UINT32);
  261. unsigned long value0 = value;
  262. for(unsigned long i = 0; i < O3DGC_BINARY_STREAM_NUM_SYMBOLS_UINT32; ++i)
  263. {
  264. m_stream[position++] = (value0 & O3DGC_BINARY_STREAM_MAX_SYMBOL0);
  265. value0 >>= O3DGC_BINARY_STREAM_BITS_PER_SYMBOL0;
  266. }
  267. }
  268. void WriteUInt32ASCII(unsigned long value)
  269. {
  270. for(unsigned long i = 0; i < O3DGC_BINARY_STREAM_NUM_SYMBOLS_UINT32; ++i)
  271. {
  272. m_stream.PushBack(value & O3DGC_BINARY_STREAM_MAX_SYMBOL0);
  273. value >>= O3DGC_BINARY_STREAM_BITS_PER_SYMBOL0;
  274. }
  275. }
  276. void WriteIntASCII(long value)
  277. {
  278. WriteUIntASCII(IntToUInt(value));
  279. }
  280. void WriteUIntASCII(unsigned long value)
  281. {
  282. if (value >= O3DGC_BINARY_STREAM_MAX_SYMBOL0)
  283. {
  284. m_stream.PushBack(O3DGC_BINARY_STREAM_MAX_SYMBOL0);
  285. value -= O3DGC_BINARY_STREAM_MAX_SYMBOL0;
  286. unsigned char a, b;
  287. do
  288. {
  289. a = ((value & O3DGC_BINARY_STREAM_MAX_SYMBOL1) << 1);
  290. b = ( (value >>= O3DGC_BINARY_STREAM_BITS_PER_SYMBOL1) > 0);
  291. a += b;
  292. m_stream.PushBack(a);
  293. } while (b);
  294. }
  295. else
  296. {
  297. m_stream.PushBack((unsigned char) value);
  298. }
  299. }
  300. void WriteUCharASCII(unsigned char value)
  301. {
  302. assert(value <= O3DGC_BINARY_STREAM_MAX_SYMBOL0);
  303. m_stream.PushBack(value);
  304. }
  305. float ReadFloat32ASCII(unsigned long & position) const
  306. {
  307. unsigned long value = ReadUInt32ASCII(position);
  308. float fvalue;
  309. memcpy(&fvalue, &value, 4);
  310. return fvalue;
  311. }
  312. unsigned long ReadUInt32ASCII(unsigned long & position) const
  313. {
  314. assert(position < m_stream.GetSize() - O3DGC_BINARY_STREAM_NUM_SYMBOLS_UINT32);
  315. unsigned long value = 0;
  316. unsigned long shift = 0;
  317. for(unsigned long i = 0; i < O3DGC_BINARY_STREAM_NUM_SYMBOLS_UINT32; ++i)
  318. {
  319. value += (m_stream[position++] << shift);
  320. shift += O3DGC_BINARY_STREAM_BITS_PER_SYMBOL0;
  321. }
  322. return value;
  323. }
  324. long ReadIntASCII(unsigned long & position) const
  325. {
  326. return UIntToInt(ReadUIntASCII(position));
  327. }
  328. unsigned long ReadUIntASCII(unsigned long & position) const
  329. {
  330. unsigned long value = m_stream[position++];
  331. if (value == O3DGC_BINARY_STREAM_MAX_SYMBOL0)
  332. {
  333. long x;
  334. unsigned long i = 0;
  335. do
  336. {
  337. x = m_stream[position++];
  338. value += ( (x>>1) << i);
  339. i += O3DGC_BINARY_STREAM_BITS_PER_SYMBOL1;
  340. } while (x & 1);
  341. }
  342. return value;
  343. }
  344. unsigned char ReadUCharASCII(unsigned long & position) const
  345. {
  346. return m_stream[position++];
  347. }
  348. O3DGCErrorCode Save(const char * const fileName)
  349. {
  350. FILE * fout = fopen(fileName, "wb");
  351. if (!fout)
  352. {
  353. return O3DGC_ERROR_CREATE_FILE;
  354. }
  355. fwrite(m_stream.GetBuffer(), 1, m_stream.GetSize(), fout);
  356. fclose(fout);
  357. return O3DGC_OK;
  358. }
  359. O3DGCErrorCode Load(const char * const fileName)
  360. {
  361. FILE * fin = fopen(fileName, "rb");
  362. if (!fin)
  363. {
  364. return O3DGC_ERROR_OPEN_FILE;
  365. }
  366. fseek(fin, 0, SEEK_END);
  367. unsigned long size = ftell(fin);
  368. m_stream.Allocate(size);
  369. rewind(fin);
  370. unsigned int nread = (unsigned int) fread((void *) m_stream.GetBuffer(), 1, size, fin);
  371. m_stream.SetSize(size);
  372. if (nread != size)
  373. {
  374. return O3DGC_ERROR_READ_FILE;
  375. }
  376. fclose(fin);
  377. return O3DGC_OK;
  378. }
  379. O3DGCErrorCode LoadFromBuffer(unsigned char * buffer, unsigned long bufferSize)
  380. {
  381. m_stream.Allocate(bufferSize);
  382. memcpy(m_stream.GetBuffer(), buffer, bufferSize);
  383. m_stream.SetSize(bufferSize);
  384. return O3DGC_OK;
  385. }
  386. unsigned long GetSize() const
  387. {
  388. return m_stream.GetSize();
  389. }
  390. const unsigned char * GetBuffer(unsigned long position) const
  391. {
  392. return m_stream.GetBuffer() + position;
  393. }
  394. unsigned char * GetBuffer(unsigned long position)
  395. {
  396. return (m_stream.GetBuffer() + position);
  397. }
  398. unsigned char * GetBuffer()
  399. {
  400. return m_stream.GetBuffer();
  401. }
  402. void GetBuffer(unsigned long position, unsigned char * & buffer) const
  403. {
  404. buffer = (unsigned char *) (m_stream.GetBuffer() + position); // fix me: ugly!
  405. }
  406. void SetSize(unsigned long size)
  407. {
  408. m_stream.SetSize(size);
  409. };
  410. void Allocate(unsigned long size)
  411. {
  412. m_stream.Allocate(size);
  413. }
  414. private:
  415. Vector<unsigned char> m_stream;
  416. O3DGCEndianness m_endianness;
  417. };
  418. }
  419. #endif // O3DGC_BINARY_STREAM_H