Image Handling

#include <abstractions/image.h>
using namespace abstractions;
Expected<double> abstractions::CompareImagesAbsDiff(const Expected<Image> &first, const Expected<Image> &second)

Compare two images using an L1-norm (absolute difference).

Parameters:
  • first – first image

  • second – second image

Returns:

the pixel-wise L1-norm, or an error if the calculation failed

Expected<double> abstractions::CompareImagesSquaredDiff(const Expected<Image> &first, const Expected<Image> &second)

Compare two images using an L2-norm (squared difference).

Parameters:
  • first – first image

  • second – second image

Returns:

the pixel-wise L2-norm, or an error if the calculation failed

class Image

A 32-bit, RGB image with an optional alpha channel.

The Image class is a thin wrapper on top of a Blend2D BLImage instance.

Public Functions

Image(const BLImage &image)

Create a new Image from an existing Blend2D BLImage instance.

Parameters:

image – a BLImage instance

int Height() const

Image height, in pixels.

int Width() const

Image width, in pixels.

int Depth() const

The total number of bits per pixel.

PixelFormat Format() const

The pixel format.

Note

This is different from the value returned by Depth(). The Depth() method returns the number of bits used for storage while the Format() method returns how the bits are interpreted.

PixelData Pixels() const

Get read-only access to the underlying pixel data.

Returns:

An object for accessing the pixel data.

Error ScaleToFit(int dim)

Scale the image to fit within the given dimensions.

The image is scaled to fit into a NxN box defined by the dim argument. If both dimensions are already smaller than this, then no resizing happens.

Parameters:

dim – the maximum size of the image’s larger dimension

Returns:

an Error if the resize operation failed

Error Save(const std::filesystem::path &path) const

Save the image to a file.

Parameters:

path – file path

Returns:

an Error if the save operation failed

inline operator BLImage&()

Convert the image to its underlying BLImage buffer.

Public Static Functions

static Expected<Image> Load(const std::filesystem::path &path)

Load an image from a file.

Parameters:

path – file path

Returns:

the loaded image or an error if it failed to load

static Expected<Image> New(const int width, const int height, const bool with_alpha = false)

Create a new image with the given size.

Parameters:
  • width – width, in pixels

  • height – height, in pixels

  • with_alpha – if true the alpha channel will not be ignored

Returns:

the new image or an error if it could not be created

enum class abstractions::PixelFormat

Supported image pixel storage formats.

Values:

enumerator RGB

Pixels only contain RGB data.

Note

The internal image storage will always have room for an alpha channel.

enumerator RGBWithPremultAlpha

Pixels contain premultiplied ARGB data.

enumerator Unknown

The internal Blend2D representation isn’t one that abstractions supports.

class Pixel

Provides easy access to the contents of a 32-bit ABGR pixel value.

The Pixel class is interchangeable with a uint32_t. A Pixel instance can be compared to an unsigned integer and vice-versa. There is also suppport for direct conversion.

Public Functions

inline Pixel(const uint32_t pixel)

Create a new pixel instance.

Parameters:

pixel – packed 32-bit pixel

inline Pixel(const uint8_t r, const uint8_t g, const uint8_t b, const uint8_t a = 0xff)

Create a new pixel instance.

Parameters:
  • r – red value

  • g – green value

  • b – blue value

  • a – alpha value

Pixel(const Pixel&) = default
Pixel(Pixel&&) = default
Pixel &operator=(const Pixel&) = default
Pixel &operator=(Pixel&&) = default
inline bool operator==(const Pixel &other) const
inline bool operator==(const uint32_t other) const
inline operator uint32_t() const
inline operator int() const
inline uint8_t Red() const
inline uint8_t Green() const
inline uint8_t Blue() const
inline uint8_t Alpha() const
class PixelData

Provides read-only access to the underlying Image pixel data.

Public Functions

const uint32_t *Row(const int y) const

Get a pointer to the start of an image row.

The row pointer will contain PixelData::Height() elements. The individual pixel values can be extracted with something like,

auto row = pixels.Row(y);
auto pixel = Pixel(row[x]);
Parameters:

y – row’s y-coordinate

Returns:

row pointer

Pixel Get(const int x, const int y) const

Get a pixel at the given coordinate.

Parameters:
  • x – x-coordinate

  • y – y-coordinate

Returns:

pixel value

int Width() const

Image width.

int Height() const

Image height.

intptr_t Stride() const

The stride between image rows.