PalFile.cpp 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. ////////////////////////////////////////////////////////////////////////////////
  2. //
  3. // Copyright 2016 RWS Inc, All Rights Reserved
  4. //
  5. // This program is free software; you can redistribute it and/or modify
  6. // it under the terms of version 2 of the GNU General Public License as published by
  7. // the Free Software Foundation
  8. //
  9. // This program is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU General Public License along
  15. // with this program; if not, write to the Free Software Foundation, Inc.,
  16. // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  17. //
  18. //////////////////////////////////////////////////////////////////////////////
  19. //
  20. // PalFile.CPP
  21. //
  22. // History:
  23. // 12/11/96 JMI Started.
  24. //
  25. // 02/04/97 JMI Changed #include "ImageLoad.h" to "PalFile.h".
  26. //
  27. //////////////////////////////////////////////////////////////////////////////
  28. //
  29. // Handles file formats for RPal. Some of these older formats will not be
  30. // useful to developers of new code. To include the ability to use a certain
  31. // loader, define one of the macros PAL_LOAD_# where # is the number of the
  32. // format desired OR define the macro PAL_LOAD_ALL for all available
  33. // support.
  34. //
  35. // NOTES TO THOSE ADDING NEW FILE VERSIONS:
  36. // Please follow these steps when adding a new version Load or Save:
  37. // o Define a new static short RPalFile::LoadVersion#(RPal*, RFile*)
  38. // where # is the number of the new version.
  39. //
  40. // o Call your new LoadVersion#(...) in the switch statement in
  41. // RPalFile::Load().
  42. //
  43. // o Make sure the last versions' case in the switch is surrounded by a
  44. // #if defined(PAL_LOAD_num) #endif block where 'num' is the last version
  45. // number.
  46. //
  47. //
  48. //////////////////////////////////////////////////////////////////////////////
  49. //////////////////////////////////////////////////////////////////////////////
  50. // C Headers -- Must be included before RSPiX.h b/c RSPiX utilizes SHMalloc.
  51. //////////////////////////////////////////////////////////////////////////////
  52. ///////////////////////////////////////////////////////////////////////////////
  53. // RSPiX Headers.
  54. // If PATHS_IN_INCLUDES macro is defined, we can utilize relative
  55. // paths to a header file. In this case we generally go off of our
  56. // RSPiX root directory. System.h MUST be included before this macro
  57. // is evaluated. System.h is the header that, based on the current
  58. // platform (or more so in this case on the compiler), defines
  59. // PATHS_IN_INCLUDES. Blue.h includes system.h so you can include that
  60. // instead.
  61. ///////////////////////////////////////////////////////////////////////////////
  62. #include "System.h"
  63. #ifdef PATHS_IN_INCLUDES
  64. #include "GREEN/Image/Image.h"
  65. #include "GREEN/Image/PalFile.h"
  66. #else
  67. #include "Image.h"
  68. #include "PalFile.h"
  69. #endif
  70. //////////////////////////////////////////////////////////////////////////////
  71. // Module specific macros.
  72. //////////////////////////////////////////////////////////////////////////////
  73. //////////////////////////////////////////////////////////////////////////////
  74. // Module specific typedefs.
  75. //////////////////////////////////////////////////////////////////////////////
  76. //////////////////////////////////////////////////////////////////////////////
  77. // Exported (extern) variables.
  78. //////////////////////////////////////////////////////////////////////////////
  79. //////////////////////////////////////////////////////////////////////////////
  80. // Module specific (static) variables / Instantiate class statics.
  81. //////////////////////////////////////////////////////////////////////////////
  82. //////////////////////////////////////////////////////////////////////////////
  83. // Module specific (static) protos.
  84. //////////////////////////////////////////////////////////////////////////////
  85. //////////////////////////////////////////////////////////////////////////////
  86. // Internal Functions.
  87. //////////////////////////////////////////////////////////////////////////////
  88. //////////////////////////////////////////////////////////////////////////////
  89. //
  90. // Loads an RPal with no file version into ppal from pfile.
  91. //
  92. //////////////////////////////////////////////////////////////////////////////
  93. short RPalFile::LoadNoVersion( // Returns SUCCESS on success or FAILURE on
  94. // failure.
  95. RPal* ppal, // Pal to load into.
  96. RFile* pfile) // File to load from.
  97. {
  98. short sRes = SUCCESS; // Assume success.
  99. // No RFile support for RPal::Type, so we used a U32.
  100. U32 u32Temp = 0;
  101. pfile->Read(&u32Temp);
  102. ppal->m_type = (RPal::Type)u32Temp;
  103. pfile->Read(&ppal->m_ulSize);
  104. pfile->Read(&ppal->m_sStartIndex);
  105. pfile->Read(&ppal->m_sNumEntries);
  106. pfile->Read(&ppal->m_sPalEntrySize);
  107. if (ppal->CreateData(ppal->m_ulSize) == SUCCESS)
  108. {
  109. pfile->Read(ppal->m_pData, ppal->m_ulSize);
  110. }
  111. else
  112. {
  113. TRACE("RPalFile::LoadNoVersion - Unable to allocate memory for the palette data\n");
  114. sRes = FAILURE;
  115. }
  116. if (pfile->Error())
  117. {
  118. TRACE("RPalFile::LoadNoVersion - Error reading palette\n");
  119. sRes = FAILURE;
  120. }
  121. return sRes;
  122. }
  123. //////////////////////////////////////////////////////////////////////////////
  124. //
  125. // Loads an RPal with file version 1 into ppal from pfile.
  126. //
  127. //////////////////////////////////////////////////////////////////////////////
  128. short RPalFile::LoadVersion1( // Returns SUCCESS on success or FAILURE on
  129. // failure.
  130. RPal* /*ppal*/, // Pal to load into.
  131. RFile* /*pfile*/) // File to load from.
  132. {
  133. short sRes = SUCCESS; // Assume success.
  134. TRACE("LoadVersion1(): No current support for version 1 RPal.\n");
  135. sRes = FAILURE;
  136. return sRes;
  137. }
  138. //////////////////////////////////////////////////////////////////////////////
  139. //
  140. // Loads an RPal with file version 2 into ppal from pfile.
  141. //
  142. //////////////////////////////////////////////////////////////////////////////
  143. short RPalFile::LoadVersion2( // Returns SUCCESS on success or FAILURE on
  144. // failure.
  145. RPal* /*ppal*/, // Pal to load into.
  146. RFile* /*pfile*/) // File to load from.
  147. {
  148. short sRes = SUCCESS; // Assume success.
  149. TRACE("LoadVersion2(): No current support for version 2 RPal.\n");
  150. sRes = FAILURE;
  151. return sRes;
  152. }
  153. //////////////////////////////////////////////////////////////////////////////
  154. //
  155. // Loads an RPal with file version 3 into ppal from pfile.
  156. //
  157. //////////////////////////////////////////////////////////////////////////////
  158. short RPalFile::LoadVersion3( // Returns SUCCESS on success or FAILURE on
  159. // failure.
  160. RPal* ppal, // Pal to load into.
  161. RFile* pfile) // File to load from.
  162. {
  163. short sRes = SUCCESS; // Assume success.
  164. // No RFile support for RPal::Type, so we used a U32.
  165. U32 u32Temp = 0;
  166. pfile->Read(&u32Temp);
  167. ppal->m_type = (RPal::Type)u32Temp;
  168. pfile->Read(&ppal->m_ulSize);
  169. pfile->Read(&ppal->m_sStartIndex);
  170. pfile->Read(&ppal->m_sNumEntries);
  171. pfile->Read(&ppal->m_sPalEntrySize);
  172. USHORT usFlag = 2;
  173. pfile->Read(&usFlag);
  174. if (usFlag == 1)
  175. {
  176. if (ppal->CreateData(ppal->m_ulSize) == SUCCESS)
  177. {
  178. pfile->Read(ppal->m_pData, ppal->m_ulSize);
  179. }
  180. else
  181. {
  182. TRACE("RPalFile::LoadVersion3 - Unable to allocate memory for the palette data\n");
  183. sRes = FAILURE;
  184. }
  185. }
  186. if (pfile->Error())
  187. {
  188. TRACE("RPalFile::LoadVersion3 - Error reading palette\n");
  189. sRes = FAILURE;
  190. }
  191. return sRes;
  192. }
  193. //////////////////////////////////////////////////////////////////////////////
  194. // External Functions.
  195. //////////////////////////////////////////////////////////////////////////////
  196. //////////////////////////////////////////////////////////////////////////////
  197. //
  198. // Maps a particular file load onto the appropriate function, if available.
  199. //
  200. //////////////////////////////////////////////////////////////////////////////
  201. short RPalFile::Load( // Returns SUCCESS on success or FAILURE on failure.
  202. RPal* ppal, // Pal to load into.
  203. RFile* pfile) // File to load from.
  204. {
  205. short sRes = SUCCESS; // Assume success.
  206. // Get finger print . . .
  207. ULONG ulFinger;
  208. if (pfile->Read(&ulFinger) == 1)
  209. {
  210. if (ulFinger == PAL_COOKIE)
  211. {
  212. ULONG ulVersion;
  213. if (pfile->Read(&ulVersion) == 1)
  214. {
  215. switch (ulVersion)
  216. {
  217. #if defined(PAL_LOAD_1) || defined(PAL_LOAD_ALL)
  218. case 1:
  219. sRes = LoadVersion1(ppal, pfile);
  220. break;
  221. #endif // PAL_LOAD_1
  222. #if defined(PAL_LOAD_2) || defined(PAL_LOAD_ALL)
  223. case 2:
  224. sRes = LoadVersion2(ppal, pfile);
  225. break;
  226. #endif // PAL_LOAD_2
  227. // #if defined(PAL_LOAD_3) || defined(PAL_LOAD_ALL)
  228. case 3:
  229. sRes = LoadVersion3(ppal, pfile);
  230. break;
  231. // #endif // PAL_LOAD_3
  232. default: // No current support.
  233. TRACE("RPal::Load - Error: Unsupported version.\n");
  234. TRACE("RPal::Load - Current RPal version %d\n", PAL_CURRENT_VERSION);
  235. TRACE("RPal::Load - This file's version %lu\n", ulVersion);
  236. TRACE("RPal::Load - See PalFile.cpp for details on "
  237. "supporting older RPal formats.\n");
  238. sRes = FAILURE;
  239. break;
  240. }
  241. }
  242. else
  243. {
  244. TRACE("RPal::Load - Error reading file version\n");
  245. sRes = FAILURE;
  246. }
  247. }
  248. else
  249. {
  250. TRACE("RPal::Load - Error: Wrong filetype, RPal files should start with"
  251. "\"CPAL\"\n");
  252. TRACE("RPal::Load - Note that some older RPal formats had no filetype, and,\n");
  253. TRACE("therefore, can no longer be loaded unless in an RImage file.\n");
  254. sRes = FAILURE;
  255. }
  256. }
  257. else
  258. {
  259. TRACE("ImageLoad(): Error reading RPal format finger print.\n");
  260. sRes = FAILURE;
  261. }
  262. return sRes;
  263. }
  264. ///////////////////////////////////////////////////////////////////////////////
  265. // EOF
  266. ///////////////////////////////////////////////////////////////////////////////