PixelLightAPI  .
ImageBuffer.h
Go to the documentation of this file.
00001 /*********************************************************\
00002  *  File: ImageBuffer.h                                  *
00003  *
00004  *  Copyright (C) 2002-2012 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_IMAGEBUFFER_H__
00024 #define __PLGRAPHICS_IMAGEBUFFER_H__
00025 #pragma once
00026 
00027 
00028 //[-------------------------------------------------------]
00029 //[ Includes                                              ]
00030 //[-------------------------------------------------------]
00031 #include <PLMath/Vector3i.h>
00032 #include "PLGraphics/PLGraphics.h"
00033 
00034 
00035 //[-------------------------------------------------------]
00036 //[ Namespace                                             ]
00037 //[-------------------------------------------------------]
00038 namespace PLGraphics {
00039 
00040 
00041 //[-------------------------------------------------------]
00042 //[ Forward declarations                                  ]
00043 //[-------------------------------------------------------]
00044 class ImageData;
00045 class ImagePalette;
00046 class ImageEffect;
00047 
00048 
00049 //[-------------------------------------------------------]
00050 //[ Classes                                               ]
00051 //[-------------------------------------------------------]
00052 /**
00053 *  @brief
00054 *    Image buffer class
00055 *
00056 *  @remarks
00057 *    The image buffer class uses a 'copy on change' technique - therefore copying one image
00058 *    buffer into another is quite performant because the internal image buffer data is shared
00059 *    as long as a image buffer doesn't change.
00060 *
00061 *  @note
00062 *    - The origin (0, 0) is on the upper-left ("top-down")
00063 *    - The image components are stored in a linear byte array. RGB example:\n
00064 *      R1G1B1R2G2B2R3G3B3...
00065 *    - Implementation of the proxy design pattern, this class is the proxy
00066 */
00067 class ImageBuffer {
00068 
00069 
00070     //[-------------------------------------------------------]
00071     //[ Public static functions                               ]
00072     //[-------------------------------------------------------]
00073     public:
00074         /**
00075         *  @brief
00076         *    Returns the number of components per pixel
00077         *
00078         *  @param[in] nColorFormat
00079         *    Color format
00080         *
00081         *  @return
00082         *    The number of components per pixel
00083         */
00084         static PLGRAPHICS_API PLCore::uint32 GetComponentsPerPixel(EColorFormat nColorFormat);
00085 
00086         /**
00087         *  @brief
00088         *    Returns the number of bytes per pixel component
00089         *
00090         *  @param[in] nDataFormat
00091         *    Data format
00092         *
00093         *  @return
00094         *    The number of bytes per pixel component
00095         */
00096         static PLGRAPHICS_API PLCore::uint32 GetBytesPerPixelComponent(EDataFormat nDataFormat);
00097 
00098         /**
00099         *  @brief
00100         *    Returns the number of bytes per pixel
00101         *
00102         *  @param[in] nDataFormat
00103         *    Data format
00104         *  @param[in] nColorFormat
00105         *    Color format
00106         *
00107         *  @return
00108         *    The number of bytes per pixel
00109         */
00110         static inline PLCore::uint32 GetBytesPerPixel(EDataFormat nDataFormat, EColorFormat nColorFormat);
00111 
00112         /**
00113         *  @brief
00114         *    Returns the size of a certain mipmap
00115         *
00116         *  @param[in] nBaseSize
00117         *    Size of the base level
00118         *  @param[in] nMipmap
00119         *    Mipmap level
00120         *
00121         *  @return
00122         *    The size of a certain mipmap
00123         */
00124         static PLGRAPHICS_API PLCore::uint32 GetMipmapSize(PLCore::uint32 nBaseSize, PLCore::uint32 nMipmap);
00125 
00126         /**
00127         *  @brief
00128         *    Returns whether or not the given color format has an alpha channel
00129         *
00130         *  @param[in] nColorFormat
00131         *    Color format to check
00132         *
00133         *  @return
00134         *    'true' if the given color format has an alpha channel, else 'false'
00135         */
00136         static inline bool IsAlphaChannelColorFormat(EColorFormat nColorFormat);
00137 
00138         /**
00139         *  @brief
00140         *    Returns a version with alpha channel of the given color format
00141         *
00142         *  @param[in] nColorFormat
00143         *    Color format to return a color format with alpha channel from
00144         *
00145         *  @return
00146         *    Color format with alpha channel on success, same color format channel if the given color format
00147         *    has already a alpha channel or when there's no version of the given color format with alpha channel
00148         */
00149         static PLGRAPHICS_API EColorFormat GetAlphaChannelColorFormat(EColorFormat nColorFormat);
00150 
00151         /**
00152         *  @brief
00153         *    Returns a version without alpha channel of the given color format
00154         *
00155         *  @param[in] nColorFormat
00156         *    Color format to return a color format without alpha channel from
00157         *
00158         *  @return
00159         *    Color format without alpha channel on success, same color format channel if the given color format
00160         *    has already no alpha channel or when there's no version of the given color format without alpha channel
00161         */
00162         static PLGRAPHICS_API EColorFormat GetNoneAlphaChannelColorFormat(EColorFormat nColorFormat);
00163 
00164 
00165     //[-------------------------------------------------------]
00166     //[ Public functions                                      ]
00167     //[-------------------------------------------------------]
00168     public:
00169         /**
00170         *  @brief
00171         *    Constructor
00172         */
00173         inline ImageBuffer();
00174 
00175         /**
00176         *  @brief
00177         *    Copy constructor
00178         *
00179         *  @param[in] cSource
00180         *    Source to copy from
00181         */
00182         inline ImageBuffer(const ImageBuffer &cSource);
00183 
00184         /**
00185         *  @brief
00186         *    Destructor
00187         */
00188         inline ~ImageBuffer();
00189 
00190         /**
00191         *  @brief
00192         *    Assignment operator
00193         *
00194         *  @param[in] cSource
00195         *    Source to copy from
00196         *
00197         *  @return
00198         *    Reference to this object
00199         */
00200         inline ImageBuffer &operator =(const ImageBuffer &cSource);
00201 
00202         /**
00203         *  @brief
00204         *    Create image
00205         *
00206         *  @param[in] nDataFormat
00207         *    Desired data format (for example "DataByte")
00208         *  @param[in] nColorFormat
00209         *    Desired color format (for example "ColorRGB")
00210         *  @param[in] vSize
00211         *    Image size (for example "PLMath::Vector3i(64, 32, 1)")
00212         *  @param[in] nCompression
00213         *    Compression type (for example "CompressionNone")
00214         *
00215         *  @remarks
00216         *    Please note that a call to CreateImage() will not immediately create the image buffer itself.
00217         *    The image buffer is created on the first call to GetData() or GetCompressedData() respectively.
00218         */
00219         inline void CreateImage(EDataFormat nDataFormat, EColorFormat nColorFormat, const PLMath::Vector3i &vSize, ECompression nCompression = CompressionNone);
00220 
00221         /**
00222         *  @brief
00223         *    Clear data
00224         */
00225         inline void Clear();
00226 
00227         /**
00228         *  @brief
00229         *    Apply image effect
00230         *
00231         *  @param[in] cEffect
00232         *    Image effect
00233         */
00234         PLGRAPHICS_API void ApplyEffect(const ImageEffect &cEffect);
00235 
00236         /**
00237         *  @brief
00238         *    Check image for possible problems
00239         *
00240         *  @return
00241         *    Error code describing any problem found, 'CheckOk' if everything is fine
00242         */
00243         PLGRAPHICS_API ECheckConsistency CheckConsistency() const;
00244 
00245         /**
00246         *  @brief
00247         *    Create a test image
00248         *
00249         *  @param[in] nTestImage
00250         *    ID of test image
00251         *
00252         *  @remarks
00253         *    Only 2D and 3D test images are allowed here. For images containing more than
00254         *    one image buffer, you have to use Image::CreateTestImage().
00255         */
00256         inline void CreateTestImage(ETestImage nTestImage = TestImage2DSimple);
00257 
00258         /**
00259         *  @brief
00260         *    Get data format
00261         *
00262         *  @return
00263         *    Data format
00264         */
00265         inline EDataFormat GetDataFormat() const;
00266 
00267         /**
00268         *  @brief
00269         *    Get color format
00270         *
00271         *  @return
00272         *    Color format
00273         */
00274         inline EColorFormat GetColorFormat() const;
00275 
00276         /**
00277         *  @brief
00278         *    Returns the number of components per pixel
00279         *
00280         *  @return
00281         *    The number of components per pixel
00282         */
00283         inline PLCore::uint32 GetComponentsPerPixel() const;
00284 
00285         /**
00286         *  @brief
00287         *    Returns the number of bytes per pixel component
00288         *
00289         *  @return
00290         *    The number of bytes per pixel component
00291         */
00292         inline PLCore::uint32 GetBytesPerPixelComponent() const;
00293 
00294         /**
00295         *  @brief
00296         *    Returns the number of bytes per pixel
00297         *
00298         *  @return
00299         *    The number of bytes per pixel
00300         */
00301         inline PLCore::uint32 GetBytesPerPixel() const;
00302 
00303         /**
00304         *  @brief
00305         *    Get compression type
00306         *
00307         *  @return
00308         *    Compression type
00309         */
00310         inline ECompression GetCompression() const;
00311 
00312         /**
00313         *  @brief
00314         *    Set compression type
00315         *
00316         *  @param[in] nCompression
00317         *    Compression type
00318         *
00319         *  @remarks
00320         *    This will invalidate the compressed image data buffer
00321         */
00322         inline void SetCompression(ECompression nCompression);
00323 
00324         /**
00325         *  @brief
00326         *    Get image size
00327         *
00328         *  @return
00329         *    Size of image
00330         */
00331         inline PLMath::Vector3i GetSize() const;
00332 
00333         /**
00334         *  @brief
00335         *    Returns the number of pixels
00336         *
00337         *  @return
00338         *    The number of pixels
00339         */
00340         inline PLCore::uint32 GetNumOfPixels() const;
00341 
00342         /**
00343         *  @brief
00344         *    Check if uncompressed or compressed data is available
00345         *
00346         *  @return
00347         *    'true' if image data is available, else 'false'
00348         */
00349         inline bool HasAnyData() const;
00350 
00351         /**
00352         *  @brief
00353         *    Check if uncompressed data is available
00354         *
00355         *  @return
00356         *    'true' if image data is available, else 'false'
00357         */
00358         inline bool HasData() const;
00359 
00360         /**
00361         *  @brief
00362         *    Get size of image data in bytes
00363         *
00364         *  @return
00365         *    Size of image data in bytes
00366         */
00367         inline PLCore::uint32 GetDataSize() const;
00368 
00369         /**
00370         *  @brief
00371         *    Get size of one row of image data in bytes
00372         *
00373         *  @return
00374         *    Size of one row in bytes
00375         */
00376         inline PLCore::uint32 GetRowSize() const;
00377 
00378         /**
00379         *  @brief
00380         *    Get image data
00381         *
00382         *  @return
00383         *    Pointer to image data, a null pointer on error
00384         *
00385         *  @remarks
00386         *    If the image contains no uncompressed data, but compressed image data is available the data,
00387         *    will be decompressed automatically and stored in the decompressed image buffer this method returns.
00388         *
00389         *  @note
00390         *    - Lookout! This method is dangerous and must be used with care! Do always ensure that you never read or write over the data buffer boundings!
00391         */
00392         inline const PLCore::uint8 *GetData() const;
00393         inline PLCore::uint8 *GetData();
00394 
00395         /**
00396         *  @brief
00397         *    Check if compressed data is available
00398         *
00399         *  @return
00400         *    'true' if compressed image data is available, else 'false'
00401         */
00402         inline bool HasCompressedData() const;
00403 
00404         /**
00405         *  @brief
00406         *    Get size of compressed image data in bytes
00407         *
00408         *  @return
00409         *    Size of compressed image data in bytes
00410         */
00411         inline PLCore::uint32 GetCompressedDataSize() const;
00412 
00413         /**
00414         *  @brief
00415         *    Get compressed image data
00416         *
00417         *  @return
00418         *    Pointer to compressed image data, a null pointer on error
00419         *
00420         *  @note
00421         *    - Lookout! This method is dangerous and must be used with care! Do always ensure that you never read or write over the data buffer boundings!
00422         */
00423         inline const PLCore::uint8 *GetCompressedData() const;
00424         inline PLCore::uint8 *GetCompressedData();
00425 
00426         /**
00427         *  @brief
00428         *    Compress image data
00429         *
00430         *  @remarks
00431         *    If the image contains uncompressed image data, the data will be
00432         *    compressed and stored in the compressed image buffer.
00433         */
00434         inline bool Compress();
00435 
00436         /**
00437         *  @brief
00438         *    Decompress image data
00439         *
00440         *  @remarks
00441         *    If the image contains compressed image data, the data will be
00442         *    decompressed and stored in the decompressed image buffer.
00443         */
00444         inline bool Decompress();
00445 
00446         /**
00447         *  @brief
00448         *    Set color palette
00449         *
00450         *  @param[in] pPalette
00451         *    Pointer to color palette, or a null pointer if the image has no palette
00452         */
00453         inline void SetPalette(ImagePalette *pPalette);
00454 
00455         /**
00456         *  @brief
00457         *    Get color palette
00458         *
00459         *  @return
00460         *    Pointer to color palette, or a null pointer if the image has no palette
00461         */
00462         inline const ImagePalette *GetPalette() const;
00463         inline ImagePalette *GetPalette();
00464 
00465         /**
00466         *  @brief
00467         *    Copy provided uncompressed image data into this image buffer
00468         *
00469         *  @param[in] pnData
00470         *    Data to copy into this image buffer, must have enough bytes to fill the whole image! If null pointer, this method does nothing.
00471         *
00472         *  @note
00473         *    - Lookout! This method is dangerous and must be used with care! Do always ensure that your given image data has enough bytes for this image buffer!
00474         */
00475         inline void CopyData(const PLCore::uint8 *pnData);
00476 
00477         /**
00478         *  @brief
00479         *    Let this image buffer takeover provided uncompressed image data
00480         *
00481         *  @param[in] pnData
00482         *    Pointer to the image data to be taken over by this image buffer, must have enough bytes to fill the whole image buffer! If null pointer, this method does nothing.
00483         *
00484         *  @note
00485         *    - "Takeover" means that this image buffer will destroy the provided data when it's no longer used!
00486         *    - Lookout! This method is dangerous and must be used with care! Do always ensure that your given image data has enough bytes for this image buffer!
00487         */
00488         inline void TakeoverData(PLCore::uint8 *pnData);
00489 
00490         /**
00491         *  @brief
00492         *    Let this image buffer share provided uncompressed image data
00493         *
00494         *  @param[in] pnData
00495         *    Pointer to the image data to be shared by this image buffer, must have enough bytes to fill the whole image buffer! If null pointer, this method does nothing.
00496         *
00497         *  @note
00498         *    - "Share" means that this image buffer will not destroy the provided data when it's no longer used!
00499         *    - Please be aware that your provided image data must stay valid during the lifetime of the image buffer instance (and potential image buffer copies!)
00500         *    - While this method is quite efficient, it's also quite error prone, so be really careful when using this method
00501         *    - Lookout! This method is dangerous and must be used with care! Do always ensure that your given image data has enough bytes for this image buffer!
00502         */
00503         inline void ShareData(PLCore::uint8 *pnData);
00504 
00505 
00506     //[-------------------------------------------------------]
00507     //[ Private functions                                     ]
00508     //[-------------------------------------------------------]
00509     private:
00510         /**
00511         *  @brief
00512         *    Make new empty image data
00513         */
00514         PLGRAPHICS_API void MakeNewBuffer();
00515 
00516         /**
00517         *  @brief
00518         *    Make image data unique
00519         */
00520         PLGRAPHICS_API void MakeBufferUnique();
00521 
00522 
00523     //[-------------------------------------------------------]
00524     //[ Private data                                          ]
00525     //[-------------------------------------------------------]
00526     private:
00527         ImageData *m_pImageData;    /**< Shared image data, always valid! */
00528 
00529 
00530 };
00531 
00532 
00533 //[-------------------------------------------------------]
00534 //[ Namespace                                             ]
00535 //[-------------------------------------------------------]
00536 } // PLGraphics
00537 
00538 
00539 //[-------------------------------------------------------]
00540 //[ Implementation                                        ]
00541 //[-------------------------------------------------------]
00542 #include "PLGraphics/Image/ImageBuffer.inl"
00543 
00544 
00545 #endif // __PLGRAPHICS_IMAGEBUFFER_H__


PixelLight PixelLight 0.9.11-R1
Copyright (C) 2002-2012 by The PixelLight Team
Last modified Thu Feb 23 2012 14:08:56
The content of this PixelLight document is published under the
Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported