gPalette.cc 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2013 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #include "io/stream.h"
  23. #include "io/fileStream.h"
  24. #include "graphics/gPalette.h"
  25. const U32 GPalette::csm_fileVersion = 1;
  26. GPalette::GPalette()
  27. : m_paletteType(RGB)
  28. {
  29. //
  30. }
  31. GPalette::~GPalette()
  32. {
  33. //
  34. }
  35. //-------------------------------------- Supplimentary I/O
  36. bool
  37. GPalette::readMSPalette(const char* in_pFileName)
  38. {
  39. AssertFatal(in_pFileName != NULL, "GPalette::readMSPalette: NULL FileName");
  40. FileStream frs;
  41. if (frs.open(in_pFileName, FileStream::Read) == false) {
  42. return false;
  43. } else {
  44. bool success = readMSPalette(frs);
  45. frs.close();
  46. return success;
  47. }
  48. }
  49. bool
  50. GPalette::writeMSPalette(const char* in_pFileName) const
  51. {
  52. AssertFatal(in_pFileName != NULL, "GPalette::writeMSPalette: NULL FileName");
  53. FileStream fws;
  54. if (fws.open(in_pFileName, FileStream::Write) == false) {
  55. return false;
  56. } else {
  57. bool success = writeMSPalette(fws);
  58. fws.close();
  59. return success;
  60. }
  61. }
  62. bool
  63. GPalette::readMSPalette(Stream& io_rStream)
  64. {
  65. AssertFatal(io_rStream.getStatus() != Stream::Closed,
  66. "GPalette::writeMSPalette: can't write to a closed stream!");
  67. U32 data;
  68. U32 size;
  69. io_rStream.read(&data);
  70. io_rStream.read(&size);
  71. if (data == makeFourCCTag('R', 'I', 'F', 'F')) {
  72. io_rStream.read(&data);
  73. io_rStream.read(&size);
  74. }
  75. if (data == makeFourCCTag('P', 'A', 'L', ' ')) {
  76. io_rStream.read(&data); // get number of colors (ignored)
  77. io_rStream.read(&data); // skip the version number.
  78. // Read the colors...
  79. io_rStream.read(256 * sizeof(ColorI), m_pColors);
  80. // With MS Pals, we assume that the type is RGB, clear out all the alpha
  81. // members so the palette keys are consistent across multiple palettes
  82. //
  83. for (U32 i = 0; i < 256; i++)
  84. m_pColors[i].alpha = 0;
  85. m_paletteType = RGB;
  86. return (io_rStream.getStatus() == Stream::Ok);
  87. }
  88. AssertWarn(false, "GPalette::readMSPalette: not a MS Palette");
  89. return false;
  90. }
  91. bool
  92. GPalette::writeMSPalette(Stream& io_rStream) const
  93. {
  94. AssertFatal(io_rStream.getStatus() != Stream::Closed,
  95. "GPalette::writeMSPalette: can't write to a closed stream!");
  96. io_rStream.write(U32(makeFourCCTag('R', 'I', 'F', 'F')));
  97. io_rStream.write(U32((256 * sizeof(ColorI)) + 8 + 4 + 4));
  98. io_rStream.write(U32(makeFourCCTag('P', 'A', 'L', ' ')));
  99. io_rStream.write(U32(makeFourCCTag('d', 'a', 't', 'a')));
  100. io_rStream.write(U32(0x0404)); // Number of colors + 4
  101. io_rStream.write(U16(0x300)); // version
  102. io_rStream.write(U16(256)); // num colors...
  103. io_rStream.write(256 * sizeof(ColorI), m_pColors);
  104. return (io_rStream.getStatus() == Stream::Ok);
  105. }
  106. //------------------------------------------------------------------------------
  107. //-------------------------------------- Persistent I/O
  108. //
  109. bool
  110. GPalette::read(Stream& io_rStream)
  111. {
  112. // Handle versioning
  113. U32 version;
  114. io_rStream.read(&version);
  115. AssertFatal(version == csm_fileVersion, "Palette::read: wrong file version...");
  116. U32 type;
  117. io_rStream.read(&type);
  118. m_paletteType = PaletteType(type);
  119. io_rStream.read(256 * sizeof(ColorI), m_pColors);
  120. return (io_rStream.getStatus() == Stream::Ok);
  121. }
  122. bool
  123. GPalette::write(Stream& io_rStream) const
  124. {
  125. // Handle versioning...
  126. io_rStream.write(csm_fileVersion);
  127. io_rStream.write(U32(m_paletteType));
  128. io_rStream.write(256 * sizeof(ColorI), m_pColors);
  129. return (io_rStream.getStatus() == Stream::Ok);
  130. }