PixelLightAPI  .
ImagePalette.h
Go to the documentation of this file.
00001 /*********************************************************\
00002  *  File: ImagePalette.h                                 *
00003  *
00004  *  Copyright (C) 2002-2011 The PixelLight Team (http://www.pixellight.org/)
00005  *
00006  *  This file is part of PixelLight.
00007  *
00008  *  PixelLight is free software: you can redistribute it and/or modify
00009  *  it under the terms of the GNU Lesser General Public License as published by
00010  *  the Free Software Foundation, either version 3 of the License, or
00011  *  (at your option) any later version.
00012  *
00013  *  PixelLight is distributed in the hope that it will be useful,
00014  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00015  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
00016  *  GNU Lesser General Public License for more details.
00017  *
00018  *  You should have received a copy of the GNU Lesser General Public License
00019  *  along with PixelLight. If not, see <http://www.gnu.org/licenses/>.
00020 \*********************************************************/
00021 
00022 
00023 #ifndef __PLGRAPHICS_IMAGEPALETTE_H__
00024 #define __PLGRAPHICS_IMAGEPALETTE_H__
00025 #pragma once
00026 
00027 
00028 //[-------------------------------------------------------]
00029 //[ Includes                                              ]
00030 //[-------------------------------------------------------]
00031 #include <PLCore/Container/HashMap.h>
00032 #include "PLGraphics/Color/Color3.h"
00033 
00034 
00035 //[-------------------------------------------------------]
00036 //[ Namespace                                             ]
00037 //[-------------------------------------------------------]
00038 namespace PLGraphics {
00039 
00040 
00041 //[-------------------------------------------------------]
00042 //[ Classes                                               ]
00043 //[-------------------------------------------------------]
00044 /**
00045 *  @brief
00046 *    Color palette (also known as "color index")
00047 */
00048 class ImagePalette {
00049 
00050 
00051     //[-------------------------------------------------------]
00052     //[ Public functions                                      ]
00053     //[-------------------------------------------------------]
00054     public:
00055         /**
00056         *  @brief
00057         *    Constructor
00058         */
00059         inline ImagePalette();
00060 
00061         /**
00062         *  @brief
00063         *    Copy constructor
00064         *
00065         *  @param[in] cSource
00066         *    Source to copy from
00067         */
00068         PLGRAPHICS_API ImagePalette(const ImagePalette &cSource);
00069 
00070         /**
00071         *  @brief
00072         *    Destructor
00073         */
00074         inline ~ImagePalette();
00075 
00076         /**
00077         *  @brief
00078         *    Assignment operator
00079         *
00080         *  @param[in] cSource
00081         *    Source to copy from
00082         *
00083         *  @return
00084         *    Reference to this object
00085         */
00086         PLGRAPHICS_API ImagePalette &operator =(const ImagePalette &cSource);
00087 
00088         /**
00089         *  @brief
00090         *    Clear data
00091         */
00092         inline void Clear();
00093 
00094         /**
00095         *  @brief
00096         *    Create palette
00097         *
00098         *  @param[in] nColors
00099         *    Number of colors
00100         */
00101         PLGRAPHICS_API void Create(PLCore::uint32 nColors);
00102 
00103         /**
00104         *  @brief
00105         *    Get number of colors
00106         *
00107         *  @return
00108         *    Number of colors
00109         */
00110         inline PLCore::uint32 GetNumOfColors() const;
00111 
00112         /**
00113         *  @brief
00114         *    Get color
00115         *
00116         *  @param[in] nIndex
00117         *    Index of color
00118         *
00119         *  @return
00120         *    Color, if index is invalid, Color3::Null (-1 -1 -1) is returned
00121         */
00122         inline Color3 GetColor(PLCore::uint32 nIndex) const;
00123 
00124         /**
00125         *  @brief
00126         *    Get index of color
00127         *
00128         *  @param[in] cColor
00129         *    Color
00130         *
00131         *  @return
00132         *    Index of that color, or -1 if color is not found
00133         *
00134         *  @remarks
00135         *    When this function is called for the first time, a hash-list of color->index is created to speed
00136         *    up any subsequent call to this function
00137         */
00138         PLGRAPHICS_API int GetColorIndex(const Color3 &cColor) const;
00139 
00140         /**
00141         *  @brief
00142         *    Set color
00143         *
00144         *  @param[in] nIndex
00145         *    Index of color
00146         *  @param[in] cColor
00147         *    Color
00148         *
00149         *  @remarks
00150         *    If nIndex is greater than the palette size, it is automatically extended
00151         */
00152         PLGRAPHICS_API void SetColor(PLCore::uint32 nIndex, const Color3 &cColor);
00153 
00154         /**
00155         *  @brief
00156         *    Add color
00157         *
00158         *  @param[in] cColor
00159         *    Color
00160         *
00161         *  @return
00162         *    Index of new color
00163         */
00164         inline PLCore::uint32 AddColor(const Color3 &cColor);
00165 
00166         /**
00167         *  @brief
00168         *    Rebuild color index
00169         *
00170         *  @remarks
00171         *    Use this function to invalidate the color index hash map (see GetColorIndex).
00172         *    This only needs to be done if you changed the image data by yourself, e.g.
00173         *    by calling GetData() and manipulation the palette directly. If you use
00174         *    SetColor() or AddColor() instead, this will be done for you automatically.
00175         */
00176         inline void RebuildColorIndex();
00177 
00178         /**
00179         *  @brief
00180         *    Get color palette data
00181         *
00182         *  @return
00183         *    Pointer to color palette, a null pointer on error
00184         *
00185         *  @remarks
00186         *    If you use this function to change the palette data by yourself, be sure to call
00187         *    RebuildColorIndex() to invalidate the index->color hash map!
00188         */
00189         inline PLCore::uint8 *GetData() const;
00190 
00191 
00192     //[-------------------------------------------------------]
00193     //[ Private functions                                     ]
00194     //[-------------------------------------------------------]
00195     private:
00196         /**
00197         *  @brief
00198         *    Resize palette
00199         *
00200         *  @param[in] nColors
00201         *    Number of colors
00202         */
00203         void Resize(PLCore::uint32 nColors);
00204 
00205 
00206     //[-------------------------------------------------------]
00207     //[ Private data                                          ]
00208     //[-------------------------------------------------------]
00209     private:
00210         PLCore::uint8                                *m_pData;      /**< Palette data, can be a null pointer */
00211         PLCore::uint32                                m_nSize;      /**< Actual size of color array (Number of colors, not bytes!) */
00212         PLCore::uint32                                m_nColors;    /**< Number of colors (<= m_nSize) */
00213         mutable PLCore::HashMap<PLCore::uint32, int>  m_mapColors;  /**< Hash map: Color -> Index */
00214 
00215 
00216 };
00217 
00218 
00219 //[-------------------------------------------------------]
00220 //[ Namespace                                             ]
00221 //[-------------------------------------------------------]
00222 } // PLGraphics
00223 
00224 
00225 //[-------------------------------------------------------]
00226 //[ Implementation                                        ]
00227 //[-------------------------------------------------------]
00228 #include "PLGraphics/Image/ImagePalette.inl"
00229 
00230 
00231 #endif // __PLGRAPHICS_IMAGEPALETTE_H__


PixelLight PixelLight 0.9.10-R1
Copyright (C) 2002-2011 by The PixelLight Team
Last modified Fri Dec 23 2011 15:50:57
The content of this PixelLight document is published under the
Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported