Spinning Topp Logo BlackTopp Studios
inc
image.cpp
1 // © Copyright 2010 - 2016 BlackTopp Studios Inc.
2 /* This file is part of The Mezzanine Engine.
3 
4  The Mezzanine Engine is free software: you can redistribute it and/or modify
5  it under the terms of the GNU General Public License as published by
6  the Free Software Foundation, either version 3 of the License, or
7  (at your option) any later version.
8 
9  The Mezzanine Engine 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
15  along with The Mezzanine Engine. If not, see <http://www.gnu.org/licenses/>.
16 */
17 /* The original authors have included a copy of the license specified above in the
18  'Docs' folder. See 'gpl.txt'
19 */
20 /* We welcome the use of the Mezzanine engine to anyone, including companies who wish to
21  Build professional software and charge for their product.
22 
23  However there are some practical restrictions, so if your project involves
24  any of the following you should contact us and we will try to work something
25  out:
26  - DRM or Copy Protection of any kind(except Copyrights)
27  - Software Patents You Do Not Wish to Freely License
28  - Any Kind of Linking to Non-GPL licensed Works
29  - Are Currently In Violation of Another Copyright Holder's GPL License
30  - If You want to change our code and not add a few hundred MB of stuff to
31  your distribution
32 
33  These and other limitations could cause serious legal problems if you ignore
34  them, so it is best to simply contact us or the Free Software Foundation, if
35  you have any questions.
36 
37  Joseph Toppi - toppij@gmail.com
38  John Blackwood - makoenergy02@gmail.com
39 */
40 
41 #ifndef _graphicsimage_cpp
42 #define _graphicsimage_cpp
43 
44 #include "Graphics/image.h"
45 #include "Graphics/graphicsutilities.h"
46 
47 #include "stringtool.h"
48 
49 #include "Internal/iostreamwrapper.h.cpp"
50 
51 #include <Ogre.h>
52 
53 namespace Mezzanine
54 {
55  namespace Graphics
56  {
57  ///////////////////////////////////////////////////////////////////////////////
58  /// @class InternalImageData
59  /// @brief This class is used to store the internal structures needed by the Image class.
60  ///////////////////////////////////////
62  {
63  public:
64  /// @internal
65  /// @brief The internal representation of the Image.
66  Ogre::Image GraphicsImage;
67  };//InternalTextureData
68 
69  ///////////////////////////////////////////////////////////////////////////////
70  // Image Methods
71 
73  {
74  this->IID = new InternalImageData();
75  }
76 
77  Image::Image(const String& ResourceName, const String& ResourceGroup)
78  {
79  this->IID = new InternalImageData();
80  this->Load(ResourceName,ResourceGroup);
81  }
82 
83  Image::Image(UInt8* Data, const UInt32 Width, const UInt32 Height, const Graphics::PixelFormat Format, const Boole AutoDelete, const Whole NumFaces, const UInt8 NumMipMaps)
84  {
85  this->IID = new InternalImageData();
86  this->Initialize(Data,Width,Height,Format,AutoDelete,NumFaces,NumMipMaps);
87  }
88 
89  Image::Image(UInt8* Data, const UInt32 Width, const UInt32 Height, const UInt32 Depth, const Graphics::PixelFormat Format, const Boole AutoDelete, const Whole NumFaces, const UInt8 NumMipMaps)
90  {
91  this->IID = new InternalImageData();
92  this->Initialize(Data,Width,Height,Depth,Format,AutoDelete,NumFaces,NumMipMaps);
93  }
94 
96  { delete this->IID; }
97 
98  ///////////////////////////////////////////////////////////////////////////////
99  // Utility Methods
100 
102  { return this->IID->GraphicsImage.getWidth(); }
103 
105  { return this->IID->GraphicsImage.getHeight(); }
106 
108  { return this->IID->GraphicsImage.getDepth(); }
109 
111  { return static_cast<Graphics::PixelFormat>( this->IID->GraphicsImage.getFormat() ); }
112 
114  { return this->IID->GraphicsImage.getBPP(); }
115 
117  { return this->IID->GraphicsImage.getNumFaces(); }
118 
120  { return this->IID->GraphicsImage.getNumMipmaps(); }
121 
123  { return static_cast<UInt8*>( this->IID->GraphicsImage.getData() ); }
124 
125  const UInt8* Image::GetImageData() const
126  { return static_cast<const UInt8*>( this->IID->GraphicsImage.getData() ); }
127 
129  { return this->IID->GraphicsImage.getSize(); }
130 
132  { return this->IID->GraphicsImage.getRowSpan(); }
133 
135  {
136  this->IID->GraphicsImage.flipAroundX();
137  return *this;
138  }
139 
141  {
142  this->IID->GraphicsImage.flipAroundY();
143  return *this;
144  }
145 
146  void Image::SetColourAt(const Whole X, const Whole Y, const Whole Z, const ColourValue& Colour)
147  { this->IID->GraphicsImage.setColourAt(Colour.GetOgreColourValue(),X,Y,Z); }
148 
149  ColourValue Image::GetColourAt(const Whole X, const Whole Y, const Whole Z) const
150  { return this->IID->GraphicsImage.getColourAt(X,Y,Z); }
151 
152  ///////////////////////////////////////////////////////////////////////////////
153  // Initialize Methods
154 
155  Image& Image::Initialize(UInt8* Data, const UInt32 Width, const UInt32 Height, const Graphics::PixelFormat Format, const Boole AutoDelete, const Whole NumFaces, const UInt8 NumMipMaps)
156  {
157  this->IID->GraphicsImage.loadDynamicImage(Data,Width,Height,1,static_cast<Ogre::PixelFormat>(Format),AutoDelete,NumFaces,NumMipMaps);
158  return *this;
159  }
160 
161  Image& Image::Initialize(UInt8* Data, const UInt32 Width, const UInt32 Height, const UInt32 Depth, const Graphics::PixelFormat Format, const Boole AutoDelete, const Whole NumFaces, const UInt8 NumMipMaps)
162  {
163  this->IID->GraphicsImage.loadDynamicImage(Data,Width,Height,Depth,static_cast<Ogre::PixelFormat>(Format),AutoDelete,NumFaces,NumMipMaps);
164  return *this;
165  }
166 
167  ///////////////////////////////////////////////////////////////////////////////
168  // Loading Methods
169 
170  Image& Image::Load(const String& ResourceName, const String& ResourceGroup)
171  {
172  this->IID->GraphicsImage.load(ResourceName,ResourceGroup);
173  return *this;
174  }
175 
176  Image& Image::Load(const String& FilePathAndName)
177  {
178  std::ifstream ImageFileStream;
179  ImageFileStream.open(FilePathAndName.c_str(),std::ifstream::in | std::ifstream::binary);
180  if( ImageFileStream.fail() ) {
181  MEZZ_EXCEPTION(ExceptionBase::IO_EXCEPTION,"Unable to open the file at: \"" + FilePathAndName + "\".");
182  }
183  return this->Load(&ImageFileStream,FilePathAndName.substr(FilePathAndName.find_last_of('.')+1));
184  }
185 
186  Image& Image::Load(std::istream* Stream, const String& ExtensionHint)
187  {
188  Ogre::DataStreamPtr OgreStreamPtr(new Internal::IStreamWrapper(Stream,false));
189  this->IID->GraphicsImage.load(OgreStreamPtr,ExtensionHint);
190  return *this;
191  }
192 
193  ///////////////////////////////////////////////////////////////////////////////
194  // Saving Methods
195 
196  Image& Image::Save(const String& FileName, const String& GroupName)
197  {
198  MEZZ_EXCEPTION(ExceptionBase::NOT_IMPLEMENTED_EXCEPTION,"Saving Images via asset groups is not supported yet.");
199  return *this;
200  }
201 
202  Image& Image::Save(const String& FilePathAndName)
203  {
204  // Ogre does everything for us in a straightforward manner
205  this->IID->GraphicsImage.save(FilePathAndName);
206  return *this;
207  }
208 
209  Image& Image::Save(const Graphics::ImageFileFormat Format, std::ostream* Stream)
210  {
211  // Be lazy
212  return this->Save(Graphics::ConvertImageFileExtension(Format),Stream);
213  }
214 
215  Image& Image::Save(const String& Extension, std::ostream* Stream)
216  {
217  Ogre::DataStreamPtr OgreStream = this->IID->GraphicsImage.encode(Extension);
218  Ogre::MemoryDataStream* RawOgreStream = static_cast<Ogre::MemoryDataStream*>( OgreStream.getPointer() );
219  Stream->write(reinterpret_cast<char*>(RawOgreStream->getPtr()),RawOgreStream->size());
220  return *this;
221  }
222 
223  ///////////////////////////////////////////////////////////////////////////////
224  // Internal Methods
225 
226  Ogre::Image& Image::_GetInternalImage() const
227  { return this->IID->GraphicsImage; }
228  }//Graphics
229 }//Mezzanine
230 
231 #endif
Graphics::ImageFileFormat ConvertImageFileExtension(String Extension)
Converts a String file extension to an ImageFileFormat enum value. an invalid extension string is pro...
bool Boole
Generally acts a single bit, true or false.
Definition: datatypes.h:173
Whole GetRowSpan() const
Gets the size of a row of pixels in this image.
Definition: image.cpp:131
Whole GetNumFaces() const
Gets the number of faces for thie image.
Definition: image.cpp:116
UInt8 * GetImageData()
Gets the raw image data.
Definition: image.cpp:122
ColourValue GetColourAt(const Whole X, const Whole Y, const Whole Z) const
Gets the colour of an individual pixel in this image.
Definition: image.cpp:149
#define MEZZ_EXCEPTION(num, desc)
An easy way to throw exceptions with rich information.
Definition: exception.h:3048
Thrown when we just have not coded a thing yet, but we knew what the API should look like...
Definition: exception.h:117
Image & Save(const String &FileName, const String &GroupName)
Writes this image to the asset group.
Definition: image.cpp:196
Ogre::Image & _GetInternalImage() const
Gets the internal Image this class is based on.
Definition: image.cpp:226
Image & Initialize(UInt8 *Data, const UInt32 Width, const UInt32 Height, const Graphics::PixelFormat Format, const Boole AutoDelete=false, const Whole NumFaces=1, const UInt8 NumMipMaps=0)
Loads an image from a buffer.
Definition: image.cpp:155
UInt32 GetHeight() const
Gets the height of this image in pixels.
Definition: image.cpp:104
uint8_t UInt8
An 8-bit unsigned integer.
Definition: datatypes.h:118
This is a simple class for holding 4 reals representing the colour any give object or lightsource can...
Definition: colourvalue.h:64
UInt32 GetWidth() const
Gets the width of this image in pixels.
Definition: image.cpp:101
~Image()
Class Destructor.
Definition: image.cpp:95
This class is used to store the internal structures needed by the Image class.
Definition: image.cpp:61
Image & Load(const String &ResourceName, const String &ResourceGroup)
Loads an image.
Definition: image.cpp:170
uint32_t UInt32
An 32-bit unsigned integer.
Definition: datatypes.h:126
ProcessDepth Depth
The current process depth as interpretted by Main.
Definition: mezztest.cpp:82
Thrown when there was an issue with IO but very little was known about it.
Definition: exception.h:73
This class represents an image loaded into system memory.
Definition: image.h:63
PixelFormat
This is used to describe how bits are arraged for each pixel in an image.
UInt8 GetBitsPerPixel() const
Gets the number of bits allocated for each pixel in this image.
Definition: image.cpp:113
InternalImageData * IID
A pointer to the internal implementation of the Image.
Definition: image.h:68
Image & FlipAroundXAxis()
Reverses the order of each pixel on the X axis.
Definition: image.cpp:134
Image & FlipAroundYAxis()
Reverses the order of each pixel on the Y axis.
Definition: image.cpp:140
Image()
Blank, no-init constructor.
Definition: image.cpp:72
Ogre::Image GraphicsImage
The internal representation of the Image.
Definition: image.cpp:66
#define MEZZ_LIB
Some platforms require special decorations to denote what is exported/imported in a share library...
UInt32 GetDepth() const
Gets the depth of this image in pixels.
Definition: image.cpp:107
The bulk of the engine components go in this namspace.
Definition: actor.cpp:56
unsigned long Whole
Whole is an unsigned integer, it will be at least 32bits in size.
Definition: datatypes.h:151
ImageFileFormat
This enum describes the various file formats for images supported by the Mezzanine.
Graphics::PixelFormat GetFormat() const
Gets the pixel format of this image.
Definition: image.cpp:110
void SetColourAt(const Whole X, const Whole Y, const Whole Z, const ColourValue &Colour)
Sets the colour of an individual pixel in this image.
Definition: image.cpp:146
CountedPtr< DataStream > DataStreamPtr
This is a convenience type for a data stream in a counted pointer.
Definition: datastream.h:383
Ogre::ColourValue GetOgreColourValue() const
Creates and returns an Ogre ColourValue class with values equal to this one.
Definition: colourvalue.cpp:86
Whole GetSize() const
Gets the size of this image.
Definition: image.cpp:128
std::string String
A datatype used to a series of characters.
Definition: datatypes.h:159
Whole GetNumMipMaps() const
Gets the number of MipMaps that exist for this image.
Definition: image.cpp:119