home *** CD-ROM | disk | FTP | other *** search
/ Xentax forum attachments archive / xentax.7z / 5257 / source.7z / xentax.h < prev    next >
Encoding:
C/C++ Source or Header  |  2012-03-28  |  12.6 KB  |  622 lines

  1. #ifndef __XENTAX_H
  2. #define __XENTAX_H
  3.  
  4. // Windows Headers
  5. #define NOMINMAX
  6. #pragma warning (disable : 4307)
  7. #pragma warning (disable : 4996)
  8. #include<windows.h>
  9. #include<windowsx.h>
  10. #include<commctrl.h>
  11. #include<shlobj.h>
  12. #include<tchar.h>
  13. #include<ddraw.h>
  14. #include<atlbase.h>
  15. #include<comdef.h>
  16. #include<msxml6.h>
  17. #pragma comment(lib, "comctl32.lib")
  18. #pragma comment(lib, "msxml6.lib")
  19.  
  20. // Standard Headers
  21. #ifndef RC_INVOKED
  22. #include<iostream>
  23. #include<iomanip>
  24. #include<fstream>
  25. #include<sstream>
  26. #include<algorithm>
  27. #include<deque>
  28. #include<map>
  29. #include<set>
  30. #include<vector>
  31. #include<string>
  32. #endif
  33.  
  34. // Boost Headers
  35. #ifndef RC_INVOKED
  36. #include<boost/shared_array.hpp>
  37. #include<boost/shared_ptr.hpp>
  38. #include<boost/bimap.hpp>
  39. #endif
  40.  
  41. // ZLIB Headers
  42. #ifndef RC_INVOKED
  43. #include "zlib.h"
  44. #pragma comment(lib, "zdll.lib")
  45. #endif
  46.  
  47. // Data Types
  48. #ifndef RC_INVOKED
  49. typedef __int8  sint08;
  50. typedef __int16 sint16;
  51. typedef __int32 sint32;
  52. typedef __int64 sint64;
  53. typedef unsigned __int8  uint08;
  54. typedef unsigned __int16 uint16;
  55. typedef unsigned __int32 uint32;
  56. typedef unsigned __int64 uint64;
  57. typedef float real32;
  58. typedef double real64;
  59. #endif
  60.  
  61. /*
  62. ** MESSAGE FUNCTIONS
  63. */
  64. #ifndef RC_INVOKED
  65.  
  66. inline bool error(const char* str)
  67. {
  68.  std::cout << str << std::endl;
  69.  return false;
  70. }
  71.  
  72. inline bool message(const char* str)
  73. {
  74.  std::cout << str << std::endl;
  75.  return true;
  76. }
  77.  
  78. template<class T>
  79. inline T clamp(T value, T v0, T v1) 
  80. {
  81.  if(value < v0) return v0;
  82.  else if(v1 < value) return v1;
  83.  return value;
  84. }
  85.  
  86. #endif
  87.  
  88. /*
  89. ** BYTE ORDER FUNCTIONS
  90. */
  91. #ifndef RC_INVOKED
  92.  
  93. template<class T>
  94. inline void reverse_byte_order(T* data)
  95. {
  96.  unsigned char* ptr = reinterpret_cast<unsigned char*>(data);
  97.  std::reverse(ptr, ptr + sizeof(T)); 
  98. }
  99.  
  100. template<class T>
  101. inline void reverse_byte_order(T* data, size_t elem)
  102. {
  103.  for(size_t i = 0; i < elem; i++) {
  104.      unsigned char* ptr = reinterpret_cast<unsigned char*>(&data[i]);
  105.      std::reverse(ptr, ptr + sizeof(T));
  106.     }
  107. }
  108.  
  109. #endif
  110.  
  111. /*
  112. ** DATA CONVERSION FUNCTIONS
  113. */
  114. #ifndef RC_INVOKED
  115.  
  116. real32 float_16_to_32(unsigned short value);
  117.  
  118. inline uint32 interpret_as_uint32(real32 x)
  119. {
  120.  return *(reinterpret_cast<uint32*>(&x));
  121. }
  122.  
  123. inline uint64 interpret_as_uint64(real64 x)
  124. {
  125.  return *(reinterpret_cast<uint64*>(&x));
  126. }
  127.  
  128. inline real32 interpret_as_real32(uint32 x)
  129. {
  130.  return *(reinterpret_cast<real32*>(&x));
  131. }
  132.  
  133. inline real64 interpret_as_real64(uint64 x)
  134. {
  135.  return *(reinterpret_cast<real64*>(&x));
  136. }
  137.  
  138. #endif
  139.  
  140. /*
  141. ** ENDIAN FUNCTIONS
  142. */
  143. #ifndef RC_INVOKED
  144.  
  145. inline sint08 LE_read_sint08(std::istream& ifile)
  146. {
  147.  sint08 temp;
  148.  ifile.read((char*)&temp, sizeof(temp));
  149.  return temp;
  150. }
  151.  
  152. inline sint08 BE_read_sint08(std::istream& ifile)
  153. {
  154.  sint08 temp;
  155.  ifile.read((char*)&temp, sizeof(temp));
  156.  return temp;
  157. }
  158.  
  159. inline uint08 LE_read_uint08(std::istream& ifile)
  160. {
  161.  uint08 temp;
  162.  ifile.read((char*)&temp, sizeof(temp));
  163.  return temp;
  164. }
  165.  
  166. inline uint08 BE_read_uint08(std::istream& ifile)
  167. {
  168.  uint08 temp;
  169.  ifile.read((char*)&temp, sizeof(temp));
  170.  return temp;
  171. }
  172.  
  173. inline sint16 LE_read_sint16(std::istream& ifile)
  174. {
  175.  sint16 temp;
  176.  ifile.read((char*)&temp, sizeof(temp));
  177.  return temp;
  178. }
  179.  
  180. inline sint16 BE_read_sint16(std::istream& ifile)
  181. {
  182.  sint16 temp;
  183.  ifile.read((char*)&temp, sizeof(temp));
  184.  reverse_byte_order(&temp);
  185.  return temp;
  186. }
  187.  
  188. inline uint16 LE_read_uint16(std::istream& ifile)
  189. {
  190.  uint16 temp;
  191.  ifile.read((char*)&temp, sizeof(temp));
  192.  return temp;
  193. }
  194.  
  195. inline uint16 BE_read_uint16(std::istream& ifile)
  196. {
  197.  uint16 temp;
  198.  ifile.read((char*)&temp, sizeof(temp));
  199.  reverse_byte_order(&temp);
  200.  return temp;
  201. }
  202.  
  203. inline sint32 LE_read_sint32(std::istream& ifile)
  204. {
  205.  sint32 temp;
  206.  ifile.read((char*)&temp, sizeof(temp));
  207.  return temp;
  208. }
  209.  
  210. inline sint32 BE_read_sint32(std::istream& ifile)
  211. {
  212.  sint32 temp;
  213.  ifile.read((char*)&temp, sizeof(temp));
  214.  reverse_byte_order(&temp);
  215.  return temp;
  216. }
  217.  
  218. inline uint32 LE_read_uint32(std::istream& ifile)
  219. {
  220.  uint32 temp;
  221.  ifile.read((char*)&temp, sizeof(temp));
  222.  return temp;
  223. }
  224.  
  225. inline uint32 BE_read_uint32(std::istream& ifile)
  226. {
  227.  uint32 temp;
  228.  ifile.read((char*)&temp, sizeof(temp));
  229.  reverse_byte_order(&temp);
  230.  return temp;
  231. }
  232.  
  233. inline sint64 LE_read_sint64(std::istream& ifile)
  234. {
  235.  sint64 temp;
  236.  ifile.read((char*)&temp, sizeof(temp));
  237.  return temp;
  238. }
  239.  
  240. inline sint64 BE_read_sint64(std::istream& ifile)
  241. {
  242.  sint64 temp;
  243.  ifile.read((char*)&temp, sizeof(temp));
  244.  reverse_byte_order(&temp);
  245.  return temp;
  246. }
  247.  
  248. inline uint64 LE_read_uint64(std::istream& ifile)
  249. {
  250.  uint64 temp;
  251.  ifile.read((char*)&temp, sizeof(temp));
  252.  return temp;
  253. }
  254.  
  255. inline uint64 BE_read_uint64(std::istream& ifile)
  256. {
  257.  uint64 temp;
  258.  ifile.read((char*)&temp, sizeof(temp));
  259.  reverse_byte_order(&temp);
  260.  return temp;
  261. }
  262.  
  263. inline sint08 LE_read_sint08(std::istream& ifile, unsigned int offset)
  264. {
  265.  ifile.seekg(offset);
  266.  return LE_read_sint08(ifile);
  267. }
  268.  
  269. inline sint08 BE_read_sint08(std::istream& ifile, unsigned int offset)
  270. {
  271.  ifile.seekg(offset);
  272.  return BE_read_sint08(ifile);
  273. }
  274.  
  275. inline uint08 LE_read_uint08(std::istream& ifile, unsigned int offset)
  276. {
  277.  ifile.seekg(offset);
  278.  return LE_read_uint08(ifile);
  279. }
  280.  
  281. inline uint08 BE_read_uint08(std::istream& ifile, unsigned int offset)
  282. {
  283.  ifile.seekg(offset);
  284.  return BE_read_uint08(ifile);
  285. }
  286.  
  287. inline sint16 LE_read_sint16(std::istream& ifile, unsigned int offset)
  288. {
  289.  ifile.seekg(offset);
  290.  return LE_read_sint16(ifile);
  291. }
  292.  
  293. inline sint16 BE_read_sint16(std::istream& ifile, unsigned int offset)
  294. {
  295.  ifile.seekg(offset);
  296.  return BE_read_sint16(ifile);
  297. }
  298.  
  299. inline uint16 LE_read_uint16(std::istream& ifile, unsigned int offset)
  300. {
  301.  ifile.seekg(offset);
  302.  return LE_read_uint16(ifile);
  303. }
  304.  
  305. inline uint16 BE_read_uint16(std::istream& ifile, unsigned int offset)
  306. {
  307.  ifile.seekg(offset);
  308.  return BE_read_uint16(ifile);
  309. }
  310.  
  311. inline sint32 LE_read_sint32(std::istream& ifile, unsigned int offset)
  312. {
  313.  ifile.seekg(offset);
  314.  return LE_read_sint32(ifile);
  315. }
  316.  
  317. inline sint32 BE_read_sint32(std::istream& ifile, unsigned int offset)
  318. {
  319.  ifile.seekg(offset);
  320.  return BE_read_sint32(ifile);
  321. }
  322.  
  323. inline uint32 LE_read_uint32(std::istream& ifile, unsigned int offset)
  324. {
  325.  ifile.seekg(offset);
  326.  return LE_read_uint32(ifile);
  327. }
  328.  
  329. inline uint32 BE_read_uint32(std::istream& ifile, unsigned int offset)
  330. {
  331.  ifile.seekg(offset);
  332.  return BE_read_uint32(ifile);
  333. }
  334.  
  335. inline sint64 LE_read_sint64(std::istream& ifile, unsigned int offset)
  336. {
  337.  ifile.seekg(offset);
  338.  return LE_read_sint64(ifile);
  339. }
  340.  
  341. inline sint64 BE_read_sint64(std::istream& ifile, unsigned int offset)
  342. {
  343.  ifile.seekg(offset);
  344.  return BE_read_sint64(ifile);
  345. }
  346.  
  347. inline uint64 LE_read_uint64(std::istream& ifile, unsigned int offset)
  348. {
  349.  ifile.seekg(offset);
  350.  return LE_read_uint64(ifile);
  351. }
  352.  
  353. inline uint64 BE_read_uint64(std::istream& ifile, unsigned int offset)
  354. {
  355.  ifile.seekg(offset);
  356.  return BE_read_uint64(ifile);
  357. }
  358.  
  359. inline real32 LE_read_real16(std::istream& ifile)
  360. {
  361.  unsigned short temp;
  362.  ifile.read((char*)&temp, sizeof(temp));
  363.  return float_16_to_32(temp);
  364. }
  365.  
  366. inline real32 BE_read_real16(std::istream& ifile)
  367. {
  368.  unsigned short temp = LE_read_uint16(ifile);
  369.  reverse_byte_order(&temp);
  370.  return float_16_to_32(temp);
  371. }
  372.  
  373. inline real32 LE_read_real32(std::istream& ifile)
  374. {
  375.  real32 temp;
  376.  ifile.read((char*)&temp, sizeof(temp));
  377.  return temp;
  378. }
  379.  
  380. inline real32 BE_read_real32(std::istream& ifile)
  381. {
  382.  real32 temp = LE_read_real32(ifile);
  383.  reverse_byte_order(&temp);
  384.  return temp;
  385. }
  386.  
  387. inline real64 LE_read_real64(std::istream& ifile)
  388. {
  389.  real64 temp;
  390.  ifile.read((char*)&temp, sizeof(temp));
  391.  return temp;
  392. }
  393.  
  394. inline real64 BE_read_real64(std::istream& ifile)
  395. {
  396.  real64 temp = LE_read_real64(ifile);
  397.  reverse_byte_order(&temp);
  398.  return temp;
  399. }
  400.  
  401. inline real32 LE_read_real16(std::istream& ifile, unsigned int offset)
  402. {
  403.  ifile.seekg(offset);
  404.  return LE_read_real16(ifile);
  405. }
  406.  
  407. inline real32 BE_read_real16(std::istream& ifile, unsigned int offset)
  408. {
  409.  ifile.seekg(offset);
  410.  return BE_read_real16(ifile);
  411. }
  412.  
  413. inline real32 LE_read_real32(std::istream& ifile, unsigned int offset)
  414. {
  415.  ifile.seekg(offset);
  416.  return LE_read_real32(ifile);
  417. }
  418.  
  419. inline real32 BE_read_real32(std::istream& ifile, unsigned int offset)
  420. {
  421.  ifile.seekg(offset);
  422.  return BE_read_real32(ifile);
  423. }
  424.  
  425. inline real64 LE_read_real64(std::istream& ifile, unsigned int offset)
  426. {
  427.  ifile.seekg(offset);
  428.  return LE_read_real64(ifile);
  429. }
  430.  
  431. inline real64 BE_read_real64(std::istream& ifile, unsigned int offset)
  432. {
  433.  ifile.seekg(offset);
  434.  return BE_read_real64(ifile);
  435. }
  436.  
  437. template<class T>
  438. inline bool LE_read_array(std::istream& ifile, T* data, size_t n)
  439. {
  440.  ifile.read((char*)&data[0], n*sizeof(T));
  441.  if(ifile.fail()) return false;
  442.  return true;
  443. }
  444.  
  445. template<class T>
  446. inline bool BE_read_array(std::istream& ifile, T* data, size_t n)
  447. {
  448.  ifile.read((char*)&data[0], n*sizeof(T));
  449.  if(ifile.fail()) return false;
  450.  reverse_byte_order(data, n);
  451.  return true;
  452. }
  453.  
  454. template<class T>
  455. inline bool LE_read_array(std::istream& ifile, unsigned int offset, T* data, unsigned int n)
  456. {
  457.  ifile.seekg(offset);
  458.  if(ifile.fail()) return false;
  459.  return LE_read_array(ifile, data, n);
  460. }
  461.  
  462. template<class T>
  463. inline bool BE_read_array(std::istream& ifile, unsigned int offset, T* data, unsigned int n)
  464. {
  465.  ifile.seekg(offset);
  466.  if(ifile.fail()) return false;
  467.  return BE_read_array(ifile, data, n);
  468. }
  469.  
  470. inline void LE_write_uint08(std::ostream& ofile, uint08 value)
  471. {
  472.  ofile.write((char*)&value, sizeof(value));
  473. }
  474.  
  475. inline void BE_write_uint08(std::ostream& ofile, uint08 value)
  476. {
  477.  reverse_byte_order(&value);
  478.  ofile.write((char*)&value, sizeof(value));
  479. }
  480.  
  481. inline void LE_write_uint16(std::ostream& ofile, uint16 value)
  482. {
  483.  ofile.write((char*)&value, sizeof(value));
  484. }
  485.  
  486. inline void BE_write_uint16(std::ostream& ofile, uint16 value)
  487. {
  488.  reverse_byte_order(&value);
  489.  ofile.write((char*)&value, sizeof(value));
  490. }
  491.  
  492. inline void LE_write_uint32(std::ostream& ofile, uint32 value)
  493. {
  494.  ofile.write((char*)&value, sizeof(value));
  495. }
  496.  
  497. inline void BE_write_uint32(std::ostream& ofile, uint32 value)
  498. {
  499.  reverse_byte_order(&value);
  500.  ofile.write((char*)&value, sizeof(value));
  501. }
  502.  
  503. inline void LE_write_uint64(std::ostream& ofile, uint64 value)
  504. {
  505.  ofile.write((char*)&value, sizeof(value));
  506. }
  507.  
  508. inline void BE_write_uint64(std::ostream& ofile, uint64 value)
  509. {
  510.  reverse_byte_order(&value);
  511.  ofile.write((char*)&value, sizeof(value));
  512. }
  513.  
  514. inline void LE_write_real32(std::ostream& ofile, real32 value)
  515. {
  516.  ofile.write((char*)&value, sizeof(value));
  517. }
  518.  
  519. inline void BE_write_real32(std::ostream& ofile, real32 value)
  520. {
  521.  reverse_byte_order(&value);
  522.  ofile.write((char*)&value, sizeof(value));
  523. }
  524.  
  525. template<class T>
  526. inline bool BE_write_array(std::ostream& ofile, T* data, size_t n)
  527. {
  528.  // copy and reverse data
  529.  boost::shared_array<T> copy(new T[n]);
  530.  for(size_t i = 0; i < n; i++) copy[i] = data[i];
  531.  reverse_byte_order(copy.get(), n);
  532.  // save data
  533.  ofile.write((char*)&data[0], n*sizeof(T));
  534.  if(ofile.fail()) return false;
  535.  return true;
  536. }
  537.  
  538. #endif
  539.  
  540. /*
  541. ** DATA ALIGNMENT FUNCTIONS
  542. */
  543. #ifndef RC_INVOKED
  544.  
  545. template<class T>
  546. inline T align02(T value)
  547. {
  548.  return ((value + 0x01) & (~(0x01)));
  549. }
  550.  
  551. template<class T>
  552. inline T align04(T value)
  553. {
  554.  return ((value + 0x03) & (~(0x03)));
  555. }
  556.  
  557. template<class T>
  558. inline T align08(T value)
  559. {
  560.  return ((value + 0x07) & (~(0x07)));
  561. }
  562.  
  563. template<class T>
  564. inline T align16(T value)
  565. {
  566.  return ((value + 0x0F) & (~(0x0F)));
  567. }
  568.  
  569. template<class T>
  570. inline T align32(T value)
  571. {
  572.  return ((value + 0x1F) & (~(0x1F)));
  573. }
  574.  
  575. template<class T>
  576. inline T align64(T value)
  577. {
  578.  return ((value + 0x3F) & (~(0x3F)));
  579. }
  580.  
  581. #endif
  582.  
  583. /*
  584. ** STRING FUNCTIONS
  585. */
  586. #ifndef RC_INVOKED
  587.  
  588. bool read_string(std::istream& ifile, char* data, size_t size);
  589. bool read_string(std::istream& ifile, char* data, size_t size, char delimiter);
  590. bool write_aligned_string_02(std::ofstream& ofile, const char* str);
  591. bool write_aligned_string_04(std::ofstream& ofile, const char* str);
  592.  
  593. inline boost::shared_array<char> string_to_shared_array(const std::string& str)
  594. {
  595.  if(str.length() == 0) return boost::shared_array<char>();
  596.  boost::shared_array<char> retval(new char[str.length() + 1]);
  597.  memmove(retval.get(), str.c_str(), str.length() + 1);
  598.  return retval;
  599. }
  600.  
  601. #endif
  602.  
  603. /*
  604. ** MORTON COORDINATE FUNCTIONS
  605. */
  606.  
  607. inline uint32 dilate_2(uint32 r)
  608. {
  609.  r = (r | (r << 8)) & 0x00FF00FF;
  610.  r = (r | (r << 4)) & 0x0F0F0F0F;
  611.  r = (r | (r << 2)) & 0x33333333;
  612.  r = (r | (r << 1)) & 0x55555555;
  613.  return r;
  614. }
  615.  
  616. inline uint32 array_to_morton(uint32 row, uint32 col) 
  617. {
  618.  return (dilate_2(col) | (dilate_2(row) << 1));
  619. }
  620.  
  621. #endif
  622.