QGearsPaletteFileSerializer.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. /*
  2. * Copyright (C) 2022 The V-Gears Team
  3. *
  4. * This file is part of V-Gears
  5. *
  6. * V-Gears is free software: you can redistribute it and/or modify it under
  7. * terms of the GNU General Public License as published by the Free Software
  8. * Foundation, version 3.0 (GPLv3) of the License.
  9. *
  10. * V-Gears is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. */
  15. #pragma once
  16. #include <OgrePixelFormat.h>
  17. #include "common/TypeDefine.h"
  18. #include "QGearsPaletteFile.h"
  19. #include "QGearsSerializer.h"
  20. namespace QGears{
  21. /**
  22. * Handles the serialization of colour palette files.
  23. */
  24. class PaletteFileSerializer : public Serializer{
  25. public:
  26. /**
  27. * Constructor.
  28. */
  29. PaletteFileSerializer();
  30. /**
  31. * Destructor.
  32. */
  33. virtual ~PaletteFileSerializer();
  34. /**
  35. * Imports a colour palette file.
  36. *
  37. * @param stream[in] The contents of the palette file.
  38. * @param dest[out] The formed palette file.
  39. */
  40. virtual void ImportPaletteFile(
  41. Ogre::DataStreamPtr &stream, PaletteFile* dest
  42. );
  43. enum {
  44. /**
  45. * Bitmask for red colour.
  46. */
  47. BIT_MASK_RED = 0x001F,
  48. /**
  49. * Bitmask for green colour.
  50. */
  51. BIT_MASK_GREEN = 0x03E0,
  52. /**
  53. * Bitmask for blie colour.
  54. */
  55. BIT_MASK_BLUE = 0x7C00,
  56. /**
  57. * Bitmask for palette size.
  58. */
  59. BIT_SIZE = 0x001F,
  60. /**
  61. * Bitmask for RGB colour.
  62. */
  63. BIT_MASK_RGB = BIT_MASK_BLUE | BIT_MASK_GREEN | BIT_MASK_RED,
  64. /**
  65. * Bitmask for alpha component.
  66. */
  67. BIT_MASK_ALPHA = 0x8000
  68. };
  69. /**
  70. * A colour palette file header.
  71. */
  72. struct Header{
  73. /**
  74. * The size of the file.
  75. */
  76. uint32 file_size;
  77. /**
  78. * The X coordinate of the palette.
  79. *
  80. * @todo Units?
  81. */
  82. uint16 pal_x;
  83. /**
  84. * The Y coordinate of the palette.
  85. *
  86. * @todo Units?
  87. */
  88. uint16 pal_y;
  89. /**
  90. * The number of colurs per palette page.
  91. */
  92. uint16 colors_per_page;
  93. /**
  94. * The number of palette pages.
  95. */
  96. uint16 page_count;
  97. };
  98. typedef PaletteFile::Color Color;
  99. typedef PaletteFile::Page Page;
  100. protected:
  101. /**
  102. * Reads a file header and sets the instance data.
  103. *
  104. * @param stream[in] The contents of the HRC file.
  105. */
  106. virtual void ReadFileHeader(Ogre::DataStreamPtr &stream);
  107. /**
  108. * Reads an object as a colour.
  109. *
  110. * @param stream[in] Input data.
  111. * @param dest[out] The formed colour data.
  112. */
  113. virtual void readObject(Ogre::DataStreamPtr &stream, Color &dest);
  114. /**
  115. * Reads an object as a palette page.
  116. *
  117. * @param stream[in] Input data.
  118. * @param dest[out] The formed page data.
  119. */
  120. virtual void readObject(Ogre::DataStreamPtr &stream, Page &dest);
  121. using Serializer::readObject;
  122. /**
  123. * Converts a colour.
  124. *
  125. * @param [in|out] Colour to convert.
  126. * @todo What kind of conversion is it doing?
  127. */
  128. virtual void ConvertColour(uint16 &colour) const;
  129. /**
  130. * A pixel format.
  131. */
  132. static const Ogre::PixelFormat PIXEL_FORMAT;
  133. /**
  134. * Reads a stream as a vector.
  135. *
  136. * @param stream[in] The input stream.
  137. * @param dest[out] The vector data will be loaded here.
  138. * @param count[in] Data units to copy.
  139. */
  140. template<typename ValueType> void ReadVector(
  141. Ogre::DataStreamPtr &stream, std::vector<ValueType> &dest,
  142. size_t count
  143. ){
  144. dest.clear();
  145. dest.reserve(count);
  146. for (size_t i(count); i --;){
  147. ValueType in_tmp;
  148. readObject(stream, in_tmp);
  149. dest.push_back(in_tmp);
  150. }
  151. }
  152. private:
  153. /**
  154. * The file header.
  155. */
  156. Header header_;
  157. };
  158. }