Wasabi
Public Member Functions | Static Public Member Functions | Friends | List of all members
WImage Class Reference

#include <WImage.hpp>

Inheritance diagram for WImage:
WFileAsset WBase

Public Member Functions

virtual std::string GetTypeName () const override
 
virtual void SetID (uint32_t newID) override
 
virtual void SetName (std::string newName) override
 
 WImage (class Wasabi *const app, uint32_t ID=0)
 
WError CreateFromPixelsArray (void *pixels, uint32_t width, uint32_t height, VkFormat format, W_IMAGE_CREATE_FLAGS flags=W_IMAGE_CREATE_TEXTURE)
 
WError CreateFromPixelsArray (void *pixels, uint32_t width, uint32_t height, uint32_t depth, VkFormat format, uint32_t arraySize=1, W_IMAGE_CREATE_FLAGS flags=W_IMAGE_CREATE_TEXTURE)
 
WError Load (std::string filename, W_IMAGE_CREATE_FLAGS flags=W_IMAGE_CREATE_TEXTURE)
 
WError CopyFrom (WImage *const image, W_IMAGE_CREATE_FLAGS flags=W_IMAGE_CREATE_TEXTURE)
 
WError MapPixels (void **const pixels, W_MAP_FLAGS flags)
 
void UnmapPixels ()
 
VkImageView GetView () const
 
VkImageLayout GetViewLayout () const
 
void TransitionLayoutTo (VkCommandBuffer cmdBuf, VkImageLayout newLayout)
 
VkFormat GetFormat () const
 
uint32_t GetWidth () const
 
uint32_t GetHeight () const
 
uint32_t GetDepth () const
 
uint32_t GetArraySize () const
 
virtual bool Valid () const override
 
size_t GetPixelSize () const
 
virtual WError SaveToStream (WFile *file, std::ostream &outputStream) override
 
virtual WError LoadFromStream (WFile *file, std::istream &inputStream, std::vector< void * > &args, std::string nameSuffix) override
 
- Public Member Functions inherited from WFileAsset
 WFileAsset (class Wasabi *const app, uint32_t ID=0)
 
virtual WError LoadFromStream (class WFile *file, std::istream &inputStream, vector< void * > &args, std::string nameSuffix)=0
 
- Public Member Functions inherited from WBase
 WBase (class Wasabi *const app, uint32_t ID=0)
 
uint32_t GetID () const
 
std::string GetName () const
 
class WasabiGetAppPtr () const
 
void AddReference ()
 
void RemoveReference ()
 

Static Public Member Functions

static std::string _GetTypeName ()
 
static std::vector< void * > LoadArgs (W_IMAGE_CREATE_FLAGS flags=W_IMAGE_CREATE_TEXTURE)
 

Friends

class WRenderTarget
 
class WImageManager
 
class WFileAsset
 

Additional Inherited Members

- Protected Attributes inherited from WBase
class Wasabim_app
 
uint32_t m_ID
 
std::string m_name
 

Detailed Description

This class represents an image, or texture, used by Wasabi.

Member Function Documentation

◆ _GetTypeName()

static std::string WImage::_GetTypeName ( )
static

Returns "Image" string.

Returns
Returns "Image" string

◆ CopyFrom()

WError WImage::CopyFrom ( WImage *const  image,
W_IMAGE_CREATE_FLAGS  flags = W_IMAGE_CREATE_TEXTURE 
)

Copy another WImage. Only images created with bDynamic == true can be copied.

Parameters
imagePointer to the (dynamic) image to copy from
flagsImage creation flags, see W_IMAGE_CREATE_FLAGS
Returns
Error code, see WError.h

◆ CreateFromPixelsArray() [1/2]

WError WImage::CreateFromPixelsArray ( void *  pixels,
uint32_t  width,
uint32_t  height,
uint32_t  depth,
VkFormat  format,
uint32_t  arraySize = 1,
W_IMAGE_CREATE_FLAGS  flags = W_IMAGE_CREATE_TEXTURE 
)

See CreateFromPixelsArray()

Parameters
depthDepth of the image
arraySizeCan be used to crteate an array of images

◆ CreateFromPixelsArray() [2/2]

WError WImage::CreateFromPixelsArray ( void *  pixels,
uint32_t  width,
uint32_t  height,
VkFormat  format,
W_IMAGE_CREATE_FLAGS  flags = W_IMAGE_CREATE_TEXTURE 
)

Creates the image from an array of pixels. The array of pixels need to be in the same format specified, using the same component size and the same number of components.

Examples: A 64x64 image with a 4-component floating-point pixel (R32G32B32A32).

WImage* img = new WImage(this);
float* pixels = new float[64*64*4]; // (64x64 image, 4 components each)
for (int y = 0; y < 64; y++) {
for (int x = 0; x < 64; x++) {
// Each 4 floats in pixels is one pixel. So pixels[0] to pixels[3] is
// the first pixel, pixels[4] to pixels[7] is the second, etc...
// So to get the index into the pixels using x and y coordinates, we
// use the regular "index = y * width + x" but we multiply it by 4 to
// have "index = y * width * 4 + x * 4" so that we account for having
// 4 components each pixel
pixels[(y*64+x)*4 + 0] = 0.1f; // red component at (x, y)
pixels[(y*64+x)*4 + 1] = 0.4f; // green component at (x, y)
pixels[(y*64+x)*4 + 2] = 1.0f; // blue component at (x, y)
pixels[(y*64+x)*4 + 3] = 1.0f; // alpha component at (x, y)
}
}
img->CreateFromPixelsArray(pixels, 64, 64, VK_FORMAT_R32G32B32A32_SFLOAT);
delete[] pixels;

Same as the above example, but using WColor for easier access and more readability.

WImage* img = new WImage(this);
WColor* pixels = new WColor[64*64];
for (int y = 0; y < 64; y++) {
for (int x = 0; x < 64; x++) {
pixels[y*64+x] = WColor(0.1f, 0.4f, 1.0f, 1.0f);
}
}
img->CreateFromPixelsArray(pixels, 64, 64, VK_FORMAT_R32G32B32A32_SFLOAT);
delete[] pixels;

Creating a 32x32, 2 component UNORM format image.

WImage* img = new WImage(this);
char* pixels = new char[32*32*2];
for (int y = 0; y < 32; y++) {
for (int x = 0; x < 32; x++) {
pixels[(y*64+x)*2 + 0] = 0;
pixels[(y*64+x)*2 + 1] = 255;
}
}
// 2 components, each one is 1 byte and the format is
// VK_FORMAT_R8G8_UNORM (so it is 0-255 in memory and 0.0-1.0 when passed
// to the GPU).
img->CreateFromPixelsArray(pixels, 32, 32, VK_FORMAT_R8G8_UNORM);
delete[] pixels;
Parameters
pixelsA pointer to the memory containing the pixels. If NULL, the image will not have initial data.
widthWidth of the image
heightHeight of the image
formatImage format
flagsImage creation flags, see W_IMAGE_CREATE_FLAGS
Returns
Error code, see WError.h

◆ GetArraySize()

uint32_t WImage::GetArraySize ( ) const

Retrieves the size of the array of images (if used, default is 1).

Returns
Size of the image array

◆ GetDepth()

uint32_t WImage::GetDepth ( ) const

Retrieves the depth of the image.

Returns
Depth of the image, in pixels

◆ GetFormat()

VkFormat WImage::GetFormat ( ) const

Retrieves the Vulkan format used for this image.

Returns
The format of the image

◆ GetHeight()

uint32_t WImage::GetHeight ( ) const

Retrieves the height of the image.

Returns
Height of the image, in pixels

◆ GetPixelSize()

size_t WImage::GetPixelSize ( ) const

Retrieves the size of a pixel in this image.

Returns
Size of a pixel, in bytes

◆ GetTypeName()

virtual std::string WImage::GetTypeName ( ) const
overridevirtual

This function must be implemented by a child class. This is used for debugging, in which a class should return its name.

Returns
The name of the class

Implements WBase.

◆ GetView()

VkImageView WImage::GetView ( ) const

Retrieves the Vulkan image view object for this image.

Returns
The image view

◆ GetViewLayout()

VkImageLayout WImage::GetViewLayout ( ) const

Retrieves the Vulkan image layout for the image view.

Returns
The image view layout

◆ GetWidth()

uint32_t WImage::GetWidth ( ) const

Retrieves the width of the image.

Returns
Width of the image, in pixels

◆ Load()

WError WImage::Load ( std::string  filename,
W_IMAGE_CREATE_FLAGS  flags = W_IMAGE_CREATE_TEXTURE 
)

Loads an image from a file. The image format can be any of the formats supported by the stb library (includes .png, .jpg, .tga, .bmp).

Parameters
filenameName of the file to load
flagsImage creation flags, see W_IMAGE_CREATE_FLAGS
Returns
Error code, see WError.h

◆ MapPixels()

WError WImage::MapPixels ( void **const  pixels,
W_MAP_FLAGS  flags 
)

Maps the pixels of the image for reading or writing. Only images created with bDynamic == true can be mapped.

Examples:

WColor* pixels; // Assuming the image is 4 components float-point format
img->MapPixels((void**)&pixels);
// Change the pixel at index 10 to full red
pixels[10] = WColor(1.0f, 0.0f, 0.0f, 1.0f);
img->UnmapPixels();
Parameters
pixelsThe address of a pointer to have it point to the mapped memory of the pixels
flagsMap flags (bitwise OR'd), specifying read/write intention
Returns
Error code, see WError.h

◆ SetID()

virtual void WImage::SetID ( uint32_t  newID)
overridevirtual

Sets the ID of this object and notifies its manager.

Parameters
newIDNew ID

Implements WBase.

◆ SetName()

virtual void WImage::SetName ( std::string  newName)
overridevirtual

Sets the name of this object.

Parameters
nameNew name for the object

Implements WBase.

◆ TransitionLayoutTo()

void WImage::TransitionLayoutTo ( VkCommandBuffer  cmdBuf,
VkImageLayout  newLayout 
)

Transitions the layout of the currently buffered image to the specified layout

Parameters
cmdBufCommand buffer to perform the transition in
newLayoutNew Vulkan layout for the underlying image

◆ UnmapPixels()

void WImage::UnmapPixels ( )

Unmaps pixels from a previous MapPixels() call. If MapPixels was called with bReadOnly set to false, this will apply the changes to the image.

◆ Valid()

virtual bool WImage::Valid ( ) const
overridevirtual

Returns true if the image is valid. The image is valid if it has a usable Vulkan image view.

Returns
true if the image is valid, false otherwise

Implements WBase.


The documentation for this class was generated from the following file:
WImage::CreateFromPixelsArray
WError CreateFromPixelsArray(void *pixels, uint32_t width, uint32_t height, VkFormat format, W_IMAGE_CREATE_FLAGS flags=W_IMAGE_CREATE_TEXTURE)
WImage::MapPixels
WError MapPixels(void **const pixels, W_MAP_FLAGS flags)
WImage::UnmapPixels
void UnmapPixels()
WImage
Definition: WImage.hpp:47
WColor
Definition: WMath.hpp:26