pal.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  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. #ifndef PAL_H
  19. #define PAL_H
  20. ///////////////////////////////////////////////////////////////////////////////
  21. //
  22. // PAL.H
  23. //
  24. //
  25. // 10/30/96 JMI Broke CPal and some of its associates out of image.h and
  26. // imagetyp.h.
  27. //
  28. // 10/30/96 JMI Changed:
  29. // Old label: New label:
  30. // ========= =========
  31. // CNFile RFile
  32. // CPal RPal
  33. // ULONG ulType RPal::Type ulType
  34. // m_bCanDestroyData m_sCanDestroyData
  35. //
  36. // 11/01/96 JMI Changed all members to be preceded by m_ (e.g., sDepth
  37. // m_sDepth). Changed ulType to m_type.
  38. //
  39. // 12/08/96 MJR Added Red(), Green() and Blue() functions to give users
  40. // access to those components without needed to know the palette
  41. // type. Implimented entirely in this header. See functions
  42. // for more information.
  43. //
  44. // 04/16/97 JMI Added operator= overload.
  45. //
  46. ///////////////////////////////////////////////////////////////////////////////
  47. //
  48. // This file contains RPal and its associates. See Image.h/cpp for more info
  49. // including info on conversions and types.
  50. //
  51. ///////////////////////////////////////////////////////////////////////////////
  52. // Blue include files
  53. #include "Blue.h"
  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. #ifdef PATHS_IN_INCLUDES
  62. // Orange include files
  63. #include "ORANGE/File/file.h"
  64. #else
  65. // Orange include files
  66. #include "file.h"
  67. #endif // PATHS_IN_INCLUDES
  68. ///////////////////////////////////////////////////////////////////////////////
  69. // Macros.
  70. ///////////////////////////////////////////////////////////////////////////////
  71. #define PAL_CURRENT_VERSION 3 // Current file verison of RPal
  72. #define PAL_COOKIE 0x4c415043 // Looks like "CPAL" in the file
  73. ///////////////////////////////////////////////////////////////////////////////
  74. // Typedefs.
  75. ///////////////////////////////////////////////////////////////////////////////
  76. typedef struct
  77. {
  78. UCHAR rgbtBlue;
  79. UCHAR rgbtGreen;
  80. UCHAR rgbtRed;
  81. } IM_RGBTRIPLE;
  82. typedef struct
  83. {
  84. UCHAR rgbBlue;
  85. UCHAR rgbGreen;
  86. UCHAR rgbRed;
  87. UCHAR rgbReserved;
  88. } IM_RGBQUAD, *IM_PRGBQUAD;
  89. //////////////////////////////////////////////////////////////////////
  90. //
  91. // RPal class
  92. //
  93. // RPal is a simple wrapper around various types of palettes.
  94. // This class does not understand any particular palette types.
  95. // I'm not sure that the above sentence is true anymore....?
  96. //
  97. //////////////////////////////////////////////////////////////////////
  98. class RPal
  99. {
  100. public: // Typedefs and Enums.
  101. // These palette types are set to correspond to the appropriate image type
  102. // in CImage::ImageType. For example, PDIB is the same number as BMP8
  103. // which is the image that uses PDIB. If you are adding your own palette
  104. // type, put it at the end of the list. If you are creating a new image
  105. // type to go with the new palette type, insert it in the appropriate
  106. // place in the eImageTypes list in CImage::ImageType (Image.h).
  107. // Also remember to add the name of your type to the ms_astrTypeNames
  108. // array in pal.cpp and the size of palette entries of your type to the
  109. // aPalEntrySizes array in pal.cpp.
  110. typedef enum ePaletteTypes
  111. {
  112. NO_PALETTE,
  113. PDIB, // BMP8 DIB format(RGBQUAD = BGR 888 + Reserved)
  114. PSYS, // SYSTEM8 System Palette - platform dependant
  115. P555, // SCREEE8_555 RGB 555 palette format
  116. P565, // SCREEN8_565 RGB 565 palette format
  117. P888, // SCREEN8_888 BGR 888 palette format - MJR 10/15/96 - "BGR" is correct order!
  118. PFLX, // FLX8_888 RGB (in that order) 888 palette
  119. // format. 03/06/96 JMI
  120. END_REG_PAL // End of registered palette types
  121. } Type;
  122. public: // Member vars.
  123. Type m_type; // Palette type
  124. ULONG m_ulSize; // Size of data
  125. short m_sStartIndex; // Starting index
  126. short m_sNumEntries; // Number of entries
  127. short m_sPalEntrySize; // Number of bytes in each palette entry
  128. UCHAR* m_pData; // Pointer to data
  129. // This array of type names should correspond to the above list of
  130. // enumerated types. Whenever you add an image type and an enum,
  131. // you need to also insert that name into the corresponding place
  132. // in this array in pal.cpp.
  133. // Note that this uses END_REG_PAL enum item to size the array.
  134. static char* ms_astrTypeNames[END_REG_PAL];
  135. // This array gives the size in bytes of each palette entry
  136. // based on the palette type. This is used by RPal::GetPalEntrySize
  137. // to return the size of any registered palette type.
  138. // Note that this uses END_REG_PAL enum item to size the array.
  139. static short ms_asPalEntrySizes[END_REG_PAL];
  140. private:
  141. short m_sCanDestroyData;// Flags whether DestroyData can destroy
  142. // the data.
  143. public:
  144. RPal();
  145. ~RPal();
  146. // Create default palette of the specified type.
  147. short CreatePalette(
  148. Type ulNewType);
  149. // Static member function that will tell you the number of
  150. // bytes per palette entry for any registered palette type
  151. static short GetPalEntrySize(Type type);
  152. // Will tell you the number of bytes per palette entry for the
  153. // this instance of the palette
  154. short GetPalEntrySize() {return m_sPalEntrySize;};
  155. // Create PAL's data using the specified values.
  156. short CreateData( // Returns 0 if successful
  157. ULONG ulSize); // Size of data
  158. short CreateData(
  159. ULONG ulSize, // Size of data
  160. Type type, // Palette type
  161. short sPalEntrySize, // Size in bytes of each Pal entry
  162. short sStartIndex, // Starting index of colors
  163. short sNumEntries); // Number of significant palette entries
  164. // Destroy PAL's data
  165. short DestroyData();
  166. // Allow the user to set the data pointer.
  167. short SetData(void* pData);
  168. // Save the palette to the given file
  169. short Save(char* pszFilename);
  170. // Save the palette to the open RFile
  171. short Save(RFile* pcf);
  172. // Load palette data from the given file
  173. short Load(char* pszFilename);
  174. // Load palette data from the open RFile
  175. short Load(RFile* pcf);
  176. // Convert the palette to the new palette type
  177. Type Convert(Type typeNew);
  178. // Detach the data from the Palette. This function returns a pointer
  179. // to the memory buffer which can and should be freed by whoever
  180. // detached it. This function is usefull when doing conversions between
  181. // palette types. You can detach the buffer from the palette, have the
  182. // palette create a new buffer (for the converted data) and then free
  183. // the detached buffer when you're done with the conversion.
  184. UCHAR* DetachData();
  185. // Gets a pointer to the red, green or blue component of the specified
  186. // palette entry. This presents a method of accessing this data in a
  187. // generic manner, without needing to know the palette type. That said,
  188. // it's unfortunate that this only works with palettes that are based
  189. // around the concept of one byte per component. In an attempt to make
  190. // these functions more usable, no result code is returned. Instead,
  191. // the returned pointers will be 0 if the current palette type is not
  192. // supported, and a TRACE message will be generated. It's far from
  193. // perfect, but it's better than having everyone implimenting some
  194. // version of these functions when they need to access the palette, in
  195. // which case they are already making assumptions about its format.
  196. unsigned char* Red(
  197. short sStart = 0) // In: Starting palette entry
  198. {
  199. // Calculate pointer to specified entry
  200. unsigned char* pucDst = m_pData + ((sStart - m_sStartIndex) * m_sPalEntrySize);
  201. // If it's a supported palette type, return the pointer. Otherwise, return 0.
  202. if (m_type == PDIB) // BGR888+reserved
  203. return pucDst+2;
  204. else if (m_type == P888) // BGR888
  205. return pucDst+2;
  206. else if (m_type == PFLX) // RGB888
  207. return pucDst+0;
  208. TRACE("RPal::Red(): Unsupported palette format - returning NULL!\n");
  209. return 0;
  210. }
  211. unsigned char* Green(
  212. short sStart = 0) // In: Starting palette entry
  213. {
  214. // Calculate pointer to specified entry
  215. unsigned char* pucDst = m_pData + ((sStart - m_sStartIndex) * m_sPalEntrySize);
  216. // If it's a supported palette type, return the pointer. Otherwise, return 0.
  217. if (m_type == PDIB) // BGR888+reserved
  218. return pucDst+1;
  219. else if (m_type == P888) // BGR888
  220. return pucDst+1;
  221. else if (m_type == PFLX) // RGB888
  222. return pucDst+1;
  223. TRACE("RPal::Green(): Unsupported palette format - returning NULL!\n");
  224. return 0;
  225. }
  226. unsigned char* Blue(
  227. short sStart = 0) // In: Starting palette entry
  228. {
  229. // Calculate pointer to specified entry
  230. unsigned char* pucDst = m_pData + ((sStart - m_sStartIndex) * m_sPalEntrySize);
  231. // If it's a supported palette type, return the pointer. Otherwise, return 0.
  232. if (m_type == PDIB) // BGR888+reserved
  233. return pucDst+0;
  234. else if (m_type == P888) // BGR888
  235. return pucDst+0;
  236. else if (m_type == PFLX) // RGB888
  237. return pucDst+2;
  238. TRACE("RPal::Blue(): Unsupported palette format - returning NULL!\n");
  239. return 0;
  240. }
  241. // Get RGB entries from palette
  242. short GetEntries(
  243. short sStart, // In: Starting palette entry
  244. short sCount, // In: Number of entries to do
  245. unsigned char* pDstRed, // Out: Starting destination red value
  246. unsigned char* pDstGreen, // Out: Starting destination green value
  247. unsigned char* pDstBlue, // Out: Starting destination blue value
  248. long lAddToPointers); // In: What to add to pointers to move to next value
  249. short SetEntries(
  250. short sStart, // In: Starting palette entry
  251. short sCount, // In: Number of entries to do
  252. unsigned char* pSrcRed, // In: Starting source red value
  253. unsigned char* pSrcGreen, // In: Starting source green value
  254. unsigned char* pSrcBlue, // In: Starting source blue value
  255. long lAddToPointers); // In: What to add to pointers to move to next value
  256. // Copy operator overload.
  257. // Note that this function could fail.
  258. RPal& operator=(RPal &palSrc);
  259. };
  260. #endif // PAL_H
  261. ///////////////////////////////////////////////////////////////////////////////
  262. // EOF
  263. ///////////////////////////////////////////////////////////////////////////////