Gorgon Game Engine
basic_Image< T_ > Class Template Reference

This class is a container for image data. More...

Public Member Functions

 basic_Image ()
 Constructs an empty image data. More...
 
 basic_Image (basic_Image &&data)
 Move constructor. More...
 
 basic_Image (const basic_Image &)=delete
 Copy constructor is disabled. More...
 
 basic_Image (const Geometry::Size &size, Graphics::ColorMode mode)
 Constructs a new image data with the given width, height and color mode. More...
 
 ~basic_Image ()
 Destructor. More...
 
void Assign (Byte *newdata)
 Assigns the image to the copy of the given data. More...
 
void Assign (Byte *newdata, const Geometry::Size &size, Graphics::ColorMode mode)
 Assigns the image to the copy of the given data. More...
 
void Assume (Byte *newdata)
 Assumes the ownership of the given data. More...
 
void Assume (Byte *newdata, const Geometry::Size &size, Graphics::ColorMode mode)
 Assumes the ownership of the given data. More...
 
void ChangeMode (Graphics::ColorMode value)
 Changes the color mode of the image. More...
 
void Clear ()
 Cleans the contents of the buffer by setting every byte it contains to 0. More...
 
void ConvertToRGBA ()
 Converts this image data to RGBA buffer. More...
 
bool CopyTo (basic_Image &dest, Geometry::Bounds source, Geometry::Point target={0, 0}) const
 Copies data from one image to another. More...
 
bool CopyTo (basic_Image &dest, Geometry::Point target={0, 0}) const
 Copies data from one image to another. More...
 
void CopyToBGRABuffer (Byte *buffer) const
 Copies this image to a RGBA buffer, buffer should be resize before calling this function. More...
 
void CopyToBGRABufferLong (unsigned long *buffer) const
 Copies this image to a RGBA buffer, buffer should be resize before calling this function. More...
 
void CopyToRGBABuffer (Byte *buffer) const
 Copies this image to a RGBA buffer, buffer should be resize before calling this function. More...
 
void Destroy ()
 Destroys this image by setting width and height to 0 and freeing the memory used by its data. More...
 
basic_Image Duplicate () const
 Duplicates this image, essentially performing the work of copy constructor. More...
 
bool ExportBMP (const std::string &filename, bool usev4=false, bool dib=false)
 Exports the image as a bitmap. More...
 
bool ExportBMP (std::ostream &file, bool usev4=false, bool dib=false)
 Exports the image as a bitmap. More...
 
Byte Get (const Geometry::Point &p, Byte def, unsigned component=0) const
 Provides access to the given component in x and y coordinates. More...
 
Byte Get (const Geometry::Point &p, unsigned component=0) const
 Provides access to the given component in x and y coordinates. More...
 
Byte Get (int x, int y, unsigned component=0) const
 Provides access to the given component in x and y coordinates. More...
 
Byte GetAlphaAt (int x, int y) const
 Returns the alpha at the given location. More...
 
int GetAlphaIndex () const
 Returns the index of alpha channel. Value of -1 denotes no alpha channel. More...
 
unsigned GetChannelsPerPixel () const
 Returns the number units occupied by a single pixel of this image. More...
 
int GetHeight () const
 Returns the height of the image. More...
 
Graphics::ColorMode GetMode () const
 Returns the color mode of the image. More...
 
Graphics::RGBA GetRGBAAt (Geometry::Point p) const
 Returns the alpha at the given location. More...
 
Graphics::RGBA GetRGBAAt (int x, int y) const
 Returns the alpha at the given location. More...
 
Geometry::Size GetSize () const
 Returns the size of the image. More...
 
unsigned long GetTotalSize () const
 Total size of this image in number units. More...
 
int GetWidth () const
 Returns the width of the image. More...
 
bool HasAlpha () const
 Returns if this image has alpha channel. More...
 
bool ImportBMP (const std::string &filename, bool dib=false)
 Imports a given bitmap file. BMP RLE compression and colorspaces are not supported. More...
 
bool ImportBMP (std::istream &file, bool dib=false)
 Imports a given bitmap file. BMP RLE compression and colorspaces are not supported. More...
 
Byteoperator() (const Geometry::Point &p, unsigned component=0)
 Provides access to the given component in x and y coordinates. More...
 
Byte operator() (const Geometry::Point &p, unsigned component=0) const
 Provides access to the given component in x and y coordinates. More...
 
Byteoperator() (int x, int y, unsigned component=0)
 Provides access to the given component in x and y coordinates. More...
 
Byte operator() (int x, int y, unsigned component=0) const
 Provides access to the given component in x and y coordinates. More...
 
basic_Imageoperator= (basic_Image &&other)
 Move assignment. More...
 
basic_Imageoperator= (const basic_Image &)=delete
 Copy assignment is disabled. More...
 
ByteRawData ()
 Returns the raw data pointer. More...
 
const ByteRawData () const
 Returns the raw data pointer. More...
 
ByteRelease ()
 Returns and disowns the current data buffer. If image is empty, this method will return a nullptr. More...
 
void Resize (const Geometry::Size &size, Graphics::ColorMode mode)
 Resizes the image to the given size and color mode. More...
 
void SetRGBAAt (Geometry::Point p, Graphics::RGBA color)
 Sets the color at the given location to the specified RGBA value. More...
 
void SetRGBAAt (int x, int y, Graphics::RGBA color)
 Sets the color at the given location to the specified RGBA value. More...
 
void Swap (basic_Image &other)
 Swaps this image with another. This function is used to implement move semantics. More...
 

Protected Attributes

int alphaloc
 Location of the alpha channel, -1 means it does not exits. More...
 
unsigned cpp
 Channels per pixel information. More...
 
T_ * data
 Data that stores pixels of the image. More...
 
Graphics::ColorMode mode
 Color mode of the image. More...
 
Geometry::Size size
 Width of the image. More...
 

Detailed Description

template<class T_>
class Gorgon::Containers::basic_Image< T_ >

This class is a container for image data.

It supports different color modes and access to the underlying data through () operator. This object implements move semantics. Since copy constructor is expensive, it is deleted against accidental use. If a copy of the object is required, use Duplicate function.

Constructor & Destructor Documentation

◆ basic_Image() [1/4]

Constructs an empty image data.

◆ basic_Image() [2/4]

basic_Image ( const Geometry::Size size,
Graphics::ColorMode  mode 
)

Constructs a new image data with the given width, height and color mode.

This constructor does not initialize data inside the image

References basic_Image< T_ >::alphaloc, basic_Size< T_ >::Area(), basic_Image< T_ >::cpp, basic_Image< T_ >::data, Gorgon::Graphics::GetAlphaIndex(), Gorgon::Graphics::GetChannelsPerPixel(), Gorgon::Graphics::HasAlpha(), basic_Image< T_ >::mode, and basic_Image< T_ >::size.

◆ basic_Image() [3/4]

basic_Image ( const basic_Image< T_ > &  )
delete

Copy constructor is disabled.

◆ basic_Image() [4/4]

basic_Image ( basic_Image< T_ > &&  data)

Move constructor.

References basic_Image< T_ >::data, and basic_Image< T_ >::Swap().

◆ ~basic_Image()

Destructor.

References basic_Image< T_ >::Destroy().

Member Function Documentation

◆ Assign() [1/2]

void Assign ( Byte newdata)

Assigns the image to the copy of the given data.

Ownership of the given data is not transferred. If the given data is not required elsewhere, consider using Assume function. The size and color mode of the image stays the same. The given data should have the size of width*height*Graphics::GetBytesPerPixel(mode)*sizeof(T_). This function does not perform any checks for the data size while copying it.t

References basic_Size< T_ >::Area(), basic_Image< T_ >::cpp, basic_Image< T_ >::data, and basic_Image< T_ >::size.

◆ Assign() [2/2]

void Assign ( Byte newdata,
const Geometry::Size size,
Graphics::ColorMode  mode 
)

Assigns the image to the copy of the given data.

Ownership of the given data is not transferred. If the given data is not required elsewhere, consider using Assume function. This variant performs resize and copy at the same time. The given data should have the size of width*height*Graphics::GetBytesPerPixel(mode)*sizeof(T_). This function does not perform any checks for the data size while copying it. If width or height is 0, the newdata is not accessed and this method effectively Destroys the current image. In this case, both width and height should be specified as 0.

References basic_Image< T_ >::alphaloc, basic_Size< T_ >::Area(), basic_Image< T_ >::cpp, basic_Image< T_ >::data, Gorgon::Graphics::GetAlphaIndex(), Gorgon::Graphics::GetChannelsPerPixel(), Gorgon::Graphics::HasAlpha(), basic_Size< T_ >::IsValid(), basic_Image< T_ >::mode, and basic_Image< T_ >::size.

◆ Assume() [1/2]

void Assume ( Byte newdata)

Assumes the ownership of the given data.

The size and color mode of the image stays the same. The given data should have the size of width*height*Graphics::GetBytesPerPixel(mode)*sizeof(T_). This function does not perform any checks for the data size while assuming it.

References basic_Image< T_ >::data.

◆ Assume() [2/2]

void Assume ( Byte newdata,
const Geometry::Size size,
Graphics::ColorMode  mode 
)

Assumes the ownership of the given data.

This variant changes the size and color mode of the image. The given data should have the size of width*height*Graphics::GetBytesPerPixel(mode)*sizeof(T_). This function does not perform any checks for the data size while assuming it. newdata could be nullptr however, in this case width, height should be 0. mode is not assumed to be ColorMode::Invalid while the image is empty, therefore it could be specified as any value.

References basic_Image< T_ >::alphaloc, basic_Image< T_ >::cpp, basic_Image< T_ >::data, Gorgon::Graphics::GetAlphaIndex(), Gorgon::Graphics::GetChannelsPerPixel(), Gorgon::Graphics::HasAlpha(), basic_Size< T_ >::IsValid(), basic_Image< T_ >::mode, and basic_Image< T_ >::size.

◆ ChangeMode()

void ChangeMode ( Graphics::ColorMode  value)

Changes the color mode of the image.

Only works if the bits/pixel of the target mode is the same as the original

References basic_Image< T_ >::alphaloc, Gorgon::Graphics::GetAlphaIndex(), Gorgon::Graphics::GetChannelsPerPixel(), Gorgon::Graphics::HasAlpha(), and basic_Image< T_ >::mode.

◆ Clear()

void Clear ( )

Cleans the contents of the buffer by setting every byte it contains to 0.

References basic_Size< T_ >::Area(), basic_Image< T_ >::cpp, basic_Image< T_ >::data, and basic_Image< T_ >::size.

◆ ConvertToRGBA()

◆ CopyTo() [1/2]

bool CopyTo ( basic_Image< T_ > &  dest,
Geometry::Bounds  source,
Geometry::Point  target = {0, 0} 
) const

Copies data from one image to another.

This operation does not perform blending. Additionally, color modes should be the same. However, this function will do clipping. Source bounds should be within the image. Will return false if nothing is copied.

◆ CopyTo() [2/2]

bool CopyTo ( basic_Image< T_ > &  dest,
Geometry::Point  target = {0, 0} 
) const

Copies data from one image to another.

This operation does not perform blending. Additionally, color modes should be the same. However, this function will do clipping for overflows. Do not use negative values for target. Will return false if nothing is copied.

◆ CopyToBGRABuffer()

void CopyToBGRABuffer ( Byte buffer) const

Copies this image to a RGBA buffer, buffer should be resize before calling this function.

This function is here mostly to create icon for Win32

References Gorgon::Graphics::Alpha, basic_Size< T_ >::Area(), basic_Image< T_ >::data, Gorgon::Graphics::Grayscale, Gorgon::Graphics::Invalid, basic_Image< T_ >::mode, Gorgon::Graphics::RGB, Gorgon::Graphics::RGBA, and basic_Image< T_ >::size.

◆ CopyToBGRABufferLong()

void CopyToBGRABufferLong ( unsigned long *  buffer) const

Copies this image to a RGBA buffer, buffer should be resize before calling this function.

This function is here mostly to create icon for X11

References Gorgon::Graphics::Alpha, basic_Size< T_ >::Area(), basic_Image< T_ >::data, Gorgon::Graphics::Grayscale, Gorgon::Graphics::Invalid, basic_Image< T_ >::mode, Gorgon::Graphics::RGB, Gorgon::Graphics::RGBA, and basic_Image< T_ >::size.

◆ CopyToRGBABuffer()

void CopyToRGBABuffer ( Byte buffer) const

◆ Destroy()

void Destroy ( )

Destroys this image by setting width and height to 0 and freeing the memory used by its data.

Also color mode is set to ColorMode::Invalid

References basic_Image< T_ >::cpp, basic_Image< T_ >::data, Gorgon::Graphics::Invalid, basic_Image< T_ >::mode, and basic_Image< T_ >::size.

◆ Duplicate()

basic_Image Duplicate ( ) const

Duplicates this image, essentially performing the work of copy constructor.

References basic_Image< T_ >::Assign(), basic_Image< T_ >::data, basic_Image< T_ >::mode, and basic_Image< T_ >::size.

◆ ExportBMP() [1/2]

bool ExportBMP ( const std::string &  filename,
bool  usev4 = false,
bool  dib = false 
)

Exports the image as a bitmap.

RGB is exported as 24-bit, RGBA, BGR, BGRA is exported as 32-bit, Grayscale exported as 8-bit, Grayscale alpha, alpha only is exported as 16-bit

◆ ExportBMP() [2/2]

bool ExportBMP ( std::ostream &  file,
bool  usev4 = false,
bool  dib = false 
)

Exports the image as a bitmap.

RGB is exported as 24-bit, RGBA, BGR, BGRA is exported as 32-bit, Grayscale exported as 8-bit, Grayscale alpha, alpha only is exported as 16-bit

References basic_Image< T_ >::data, basic_Size< T_ >::Height, basic_Image< T_ >::mode, basic_Image< T_ >::size, basic_Size< T_ >::Width, Gorgon::IO::WriteArray(), Gorgon::IO::WriteInt32(), Gorgon::IO::WriteString(), Gorgon::IO::WriteUInt16(), Gorgon::IO::WriteUInt32(), and Gorgon::IO::WriteUInt8().

◆ Get() [1/3]

Byte Get ( const Geometry::Point p,
Byte  def,
unsigned  component = 0 
) const

Provides access to the given component in x and y coordinates.

This function returns 0 if the given coordinates are out of bounds. This function works slower than the () operator.

References basic_Image< T_ >::cpp, basic_Image< T_ >::data, basic_Size< T_ >::Height, basic_Image< T_ >::size, basic_Size< T_ >::Width, basic_Point< T_ >::X, and basic_Point< T_ >::Y.

◆ Get() [2/3]

Byte Get ( const Geometry::Point p,
unsigned  component = 0 
) const

Provides access to the given component in x and y coordinates.

This function returns 0 if the given coordinates are out of bounds. This function works slower than the () operator.

References basic_Image< T_ >::cpp, basic_Image< T_ >::data, basic_Size< T_ >::Height, basic_Image< T_ >::size, basic_Size< T_ >::Width, basic_Point< T_ >::X, and basic_Point< T_ >::Y.

◆ Get() [3/3]

Byte Get ( int  x,
int  y,
unsigned  component = 0 
) const

Provides access to the given component in x and y coordinates.

This function returns 0 if the given coordinates are out of bounds. This function works slower than the () operator.

References basic_Image< T_ >::cpp, basic_Image< T_ >::data, basic_Size< T_ >::Height, basic_Image< T_ >::size, and basic_Size< T_ >::Width.

◆ GetAlphaAt()

Byte GetAlphaAt ( int  x,
int  y 
) const

Returns the alpha at the given location.

If the given location does not exits this function will return 0. If there is no alpha channel, image is assumed to be opaque.

References basic_Image< T_ >::alphaloc, basic_Image< T_ >::cpp, basic_Image< T_ >::data, basic_Size< T_ >::Height, basic_Image< T_ >::size, and basic_Size< T_ >::Width.

◆ GetAlphaIndex()

int GetAlphaIndex ( ) const

Returns the index of alpha channel. Value of -1 denotes no alpha channel.

References basic_Image< T_ >::alphaloc.

◆ GetChannelsPerPixel()

unsigned GetChannelsPerPixel ( ) const

Returns the number units occupied by a single pixel of this image.

References basic_Image< T_ >::cpp.

◆ GetHeight()

int GetHeight ( ) const

Returns the height of the image.

References basic_Size< T_ >::Height, and basic_Image< T_ >::size.

◆ GetMode()

Graphics::ColorMode GetMode ( ) const

Returns the color mode of the image.

References basic_Image< T_ >::mode.

◆ GetRGBAAt() [1/2]

Graphics::RGBA GetRGBAAt ( Geometry::Point  p) const

Returns the alpha at the given location.

If the given location does not exits this function will return 0. If there is no alpha channel, image is assumed to be opaque.

References basic_Image< T_ >::GetRGBAAt(), basic_Point< T_ >::X, and basic_Point< T_ >::Y.

◆ GetRGBAAt() [2/2]

Graphics::RGBA GetRGBAAt ( int  x,
int  y 
) const

Returns the alpha at the given location.

If the given location does not exits this function will return 0. If there is no alpha channel, image is assumed to be opaque.

References Gorgon::Graphics::Alpha, Gorgon::Graphics::Grayscale, basic_Size< T_ >::Height, Gorgon::Graphics::Invalid, basic_Image< T_ >::mode, Gorgon::Graphics::RGB, Gorgon::Graphics::RGBA, basic_Image< T_ >::size, and basic_Size< T_ >::Width.

◆ GetSize()

Geometry::Size GetSize ( ) const

Returns the size of the image.

References basic_Image< T_ >::size.

◆ GetTotalSize()

unsigned long GetTotalSize ( ) const

Total size of this image in number units.

References basic_Size< T_ >::Area(), basic_Image< T_ >::cpp, and basic_Image< T_ >::size.

◆ GetWidth()

int GetWidth ( ) const

Returns the width of the image.

References basic_Image< T_ >::size, and basic_Size< T_ >::Width.

◆ HasAlpha()

bool HasAlpha ( ) const

Returns if this image has alpha channel.

References basic_Image< T_ >::alphaloc.

◆ ImportBMP() [1/2]

bool ImportBMP ( const std::string &  filename,
bool  dib = false 
)

Imports a given bitmap file. BMP RLE compression and colorspaces are not supported.

◆ ImportBMP() [2/2]

◆ operator()() [1/4]

Byte& operator() ( const Geometry::Point p,
unsigned  component = 0 
)

Provides access to the given component in x and y coordinates.

This function performs bounds checking only on debug mode.

References basic_Image< T_ >::cpp, basic_Image< T_ >::data, basic_Size< T_ >::Height, basic_Image< T_ >::size, basic_Size< T_ >::Width, basic_Point< T_ >::X, and basic_Point< T_ >::Y.

◆ operator()() [2/4]

Byte operator() ( const Geometry::Point p,
unsigned  component = 0 
) const

Provides access to the given component in x and y coordinates.

This function performs bounds checking only on debug mode.

References basic_Image< T_ >::cpp, basic_Image< T_ >::data, basic_Size< T_ >::Height, basic_Image< T_ >::size, basic_Size< T_ >::Width, basic_Point< T_ >::X, and basic_Point< T_ >::Y.

◆ operator()() [3/4]

Byte& operator() ( int  x,
int  y,
unsigned  component = 0 
)

Provides access to the given component in x and y coordinates.

This function performs bounds checking only on debug mode.

References basic_Image< T_ >::cpp, basic_Image< T_ >::data, basic_Size< T_ >::Height, basic_Image< T_ >::size, and basic_Size< T_ >::Width.

◆ operator()() [4/4]

Byte operator() ( int  x,
int  y,
unsigned  component = 0 
) const

Provides access to the given component in x and y coordinates.

This function performs bounds checking only on debug mode.

References basic_Image< T_ >::cpp, basic_Image< T_ >::data, basic_Size< T_ >::Height, basic_Image< T_ >::size, and basic_Size< T_ >::Width.

◆ operator=() [1/2]

basic_Image& operator= ( basic_Image< T_ > &&  other)

Move assignment.

References basic_Image< T_ >::Destroy(), and basic_Image< T_ >::Swap().

◆ operator=() [2/2]

basic_Image& operator= ( const basic_Image< T_ > &  )
delete

Copy assignment is disabled.

◆ RawData() [1/2]

Byte* RawData ( )

Returns the raw data pointer.

References basic_Image< T_ >::data.

◆ RawData() [2/2]

const Byte* RawData ( ) const

Returns the raw data pointer.

References basic_Image< T_ >::data.

◆ Release()

Byte* Release ( )

Returns and disowns the current data buffer. If image is empty, this method will return a nullptr.

References basic_Image< T_ >::data, and basic_Image< T_ >::Destroy().

◆ Resize()

void Resize ( const Geometry::Size size,
Graphics::ColorMode  mode 
)

◆ SetRGBAAt() [1/2]

void SetRGBAAt ( Geometry::Point  p,
Graphics::RGBA  color 
)

Sets the color at the given location to the specified RGBA value.

If pixel does not exists, the call will be ignored.

References basic_Image< T_ >::SetRGBAAt(), basic_Point< T_ >::X, and basic_Point< T_ >::Y.

◆ SetRGBAAt() [2/2]

void SetRGBAAt ( int  x,
int  y,
Graphics::RGBA  color 
)

◆ Swap()

void Swap ( basic_Image< T_ > &  other)

Swaps this image with another. This function is used to implement move semantics.

References basic_Image< T_ >::alphaloc, basic_Image< T_ >::cpp, basic_Image< T_ >::data, basic_Image< T_ >::mode, basic_Image< T_ >::size, and Gorgon::Containers::swap().

Member Data Documentation

◆ alphaloc

int alphaloc
protected

Location of the alpha channel, -1 means it does not exits.

◆ cpp

unsigned cpp
protected

Channels per pixel information.

◆ data

T_* data
protected

Data that stores pixels of the image.

◆ mode

Graphics::ColorMode mode
protected

Color mode of the image.

◆ size

Geometry::Size size
protected

Width of the image.


The documentation for this class was generated from the following file: