class PixelUtility

Utility methods for converting and managing pixel data and formats.

Public

Methods

staticGetElementByteCount

static u32 GetElementByteCount(PixelFormat format)

Returns the size of a single pixel of the provided pixel format, in bytes.

staticGetBlockSize

static u32 GetBlockSize(PixelFormat format)

Returns the size of a single compressed block, in bytes.

Returns pixel size if the format is not block compressed.

staticGetBlockDimensions

static Vector2I GetBlockDimensions(PixelFormat format)

Returns the dimensions of a single compressed block, in number of pixels.

Returns 1x1 for non-block-compressed formats.

staticGetElementBitCount

static u32 GetElementBitCount(PixelFormat format)

Returns the size of a single pixel of the provided pixel format, in bits.

staticGetMemorySize

static u32 GetMemorySize(u32 width, u32 height, u32 depth, PixelFormat format)

Returns the size of the memory region required to hold pixels of the provided size ana format.

staticGetSizeForMipLevel

static void GetSizeForMipLevel(u32 width, u32 height, u32 depth, u32 mipLevel, u32 &mipWidth, u32 &mipHeight, u32 &mipDepth)

Calculates the size of a mip level of a texture with the provided size.

staticGetPitch

static void GetPitch(u32 width, u32 height, u32 depth, PixelFormat format, u32 &rowPitch, u32 &depthPitch)

Calculates row and depth pitch for a texture surface of the specified size and format.

For most formats row pitch will equal the number of bytes required for storing "width" pixels, and slice pitch will equal the number of bytes required for storing "width*height" pixels. But some texture formats (especially compressed ones) might require extra padding. Input width/height/depth values are in pixels, while output pitch values are in bytes.

staticGetFlags

static u32 GetFlags(PixelFormat format)

Returns property flags for this pixel format.

staticHasAlpha

static bool HasAlpha(PixelFormat format)

Checks if the provided pixel format has an alpha channel.

staticIsFloatingPoint

static bool IsFloatingPoint(PixelFormat format)

Checks is the provided pixel format a floating point format.

staticIsCompressed

static bool IsCompressed(PixelFormat format)

Checks is the provided pixel format compressed.

staticIsDepth

static bool IsDepth(PixelFormat format)

Checks is the provided pixel format a depth/stencil buffer format.

staticIsNormalized

static bool IsNormalized(PixelFormat format)

Checks does the provided format store data in normalized range.

staticCheckFormat

static bool CheckFormat(PixelFormat &format, TextureType textureType, TextureUsageFlags usage)

Checks is the provided format valid for the texture type and usage.

Caller should still check for platform-specific unsupported formats.

format
Format to check. If format is not valid the method will update this with the closest relevant format.
textureType
Type of the texture the format will be used for.
usage
A set of TextureUsage flags that define how will a texture be used.

Returns: True if the format is valid, false if not.

staticIsValidExtent

static bool IsValidExtent(u32 width, u32 height, u32 depth, PixelFormat format)

Checks are the provided dimensions valid for the specified pixel format.

Some formats (like BC) require width/height to be multiples of four and some formats dont allow depth larger than 1.

staticGetBitDepths

static void GetBitDepths(PixelFormat format, int (&rgba)[4])

Returns the number of bits per each element in the provided pixel format.

This will return all zero for compressed and depth/stencil formats.

staticGetBitMasks

static void GetBitMasks(PixelFormat format, u32 (&rgba)[4])

Returns bit masks that determine in what bit range is each channel stored.

staticGetBitShifts

static void GetBitShifts(PixelFormat format, u8 (&rgba)[4])

Returns number of bits you need to shift a pixel element in order to move it to the start of the data type.

staticGetFormatName

static String GetFormatName(PixelFormat format)

Returns the name of the pixel format.

staticIsAccessible

static bool IsAccessible(PixelFormat format)

Returns true if the pixel data in the format can be directly accessed and read.

This is generally not true for compressed formats.

staticGetElementType

static PixelComponentType GetElementType(PixelFormat format)

Returns the type of an individual pixel element in the provided format.

staticGetElementCount

static u32 GetElementCount(PixelFormat format)

Returns the number of pixel elements in the provided format.

staticGetMipmapCount

static u32 GetMipmapCount(u32 width, u32 height, u32 depth, PixelFormat format)

Returns the maximum number of mip maps that can be generated until we reach the minimum size possible.

This does not count the base level.

staticPackColor

static void PackColor(const Color &color, PixelFormat format, void *dest)

Writes the color to the provided memory location.

staticPackColor

static void PackColor(u8 r, u8 g, u8 b, u8 a, PixelFormat format, void *dest)

Writes the color to the provided memory location.

If the destination format is floating point, the byte values will be converted into [0.0, 1.0] range.

staticPackColor

static void PackColor(float r, float g, float b, float a, const PixelFormat format, void *dest)

Writes the color to the provided memory location.

If the destination format in non-floating point, the float values will be assumed to be in [0.0, 1.0] which will be converted to integer range. ([0, 255] in the case of bytes)

staticUnpackColor

static void UnpackColor(Color *color, PixelFormat format, const void *src)

Reads the color from the provided memory location and stores it into the provided color object.

staticUnpackColor

static void UnpackColor(u8 *r, u8 *g, u8 *b, u8 *a, PixelFormat format, const void *src)

Reads the color from the provided memory location and stores it into the provided color elements, as bytes clamped to [0, 255] range.

staticUnpackColor

static void UnpackColor(float *r, float *g, float *b, float *a, PixelFormat format, const void *src)

Reads the color from the provided memory location and stores it into the provided color elements.

If the format is not natively floating point a conversion is done in such a way that returned values range [0.0, 1.0].

staticPackDepth

static void PackDepth(float depth, const PixelFormat format, void *dest)

Writes a depth value to the provided memory location.

Depth should be in range [0, 1].

staticUnpackDepth

static float UnpackDepth(PixelFormat format, void *src)

Reads the depth from the provided memory location.

Value ranges in [0, 1].

staticBulkPixelConversion

static void BulkPixelConversion(const PixelData &source, PixelData &destination)

Converts pixels from one format to another.

Provided pixel data objects must have previously allocated buffers of adequate size and their sizes must match.

staticFlipComponentOrder

static void FlipComponentOrder(PixelData &data)

Flips the order of components in each individual pixel.

For example RGBA -> ABGR.

staticConvertFormat

static SPtr<PixelData> ConvertFormat(const SPtr<PixelData> &source, PixelFormat format)

staticCompress

static void Compress(const PixelData &source, PixelData &destination, const CompressionOptions &options)

Compresses the provided data using the specified compression options.

staticCompress

static SPtr<PixelData> Compress(const SPtr<PixelData> &source, const CompressionOptions &options)

staticGenerateMipmaps

static Vector<SPtr<PixelData>> GenerateMipmaps(const SPtr<PixelData> &source, const MipMapGenOptions &options)

Generates mip-maps from the provided source data using the specified compression options.

Returned list includes the base level.

Returns: A list of calculated mip-map data. First entry is the largest mip and other follow in order from largest to smallest.

staticScale

static void Scale(const PixelData &source, PixelData &destination, ScaleFilter filter = ScaleFilter::Linear)

Scales pixel data in the source buffer and stores the scaled data in the destination buffer.

Provided pixel data objects must have previously allocated buffers of adequate size. You may also provided a filtering method to use when scaling.

staticScale

static SPtr<PixelData> Scale(const SPtr<PixelData> &source, const Size3UI &size, ScaleFilter filter = ScaleFilter::Linear)

Scales pixel data in the source buffer according to the provided size and filtering method.

staticMirror

static void Mirror(PixelData &pixelData, MirrorMode mode)

Mirrors the contents of the provided object along the X, Y and/or Z axes.

staticCopy

static void Copy(const PixelData &src, PixelData &dst, u32 offsetX = 0, u32 offsetY = 0, u32 offsetZ = 0)

Copies the contents of the buffer into the buffer.

The size of the copied contents is determined by the size of the buffer. First pixel copied from is determined by offset provided in ,

and parameters.

staticLinearToSrgb

static SPtr<PixelData> LinearToSrgb(const SPtr<PixelData> &input)

Converts pixel data in linear space to one in sRGB space.

Only converts the RGB components.

staticSRGBToLinear

static SPtr<PixelData> SRGBToLinear(const SPtr<PixelData> &input)

Converts pixel data in sRGB space to one in linear space.

Only converts the RGB components.

staticSaveImage

static bool SaveImage(const SPtr<PixelData> &pixelData, const Path &outputPath, ImageFormat format, bool ignoreAlpha = false)

Saves pixel data to an image file.

pixelData
Pixel data to save. Must be non-null with valid dimensions.
outputPath
Output file path. Extension will be set based on format.
format
Image format to save as (PNG, JPG, BMP, TGA).
ignoreAlpha
If true, alpha channel will be set to fully opaque (255) in the output.

Returns: True if save succeeded, false on error (with log message).