Images Manual

About

The MAXON API provides several interfaces to handle image data. All these interfaces are based on maxon::ImageBaseInterface:

Note
Typically these image classes represent uncompressed image data. But it is also possible that an image could store compressed or non-RGB data.
A maxon::ImageRef can be obtained from a BaseBitmap using BaseBitmap::GetImageRef(). See BaseBitmap Manual.
// This example creates a new image and fills it with random RBG data.
// The image is then stored in a PNG file.
// create ImageRef implementation of ImageInterface
const maxon::ImageRef image = maxon::ImageClasses::IMAGE().Create() iferr_return;
// init image
const maxon::PixelFormat rgbFormat = maxon::PixelFormats::RGB::U8();
const auto storageType = maxon::ImagePixelStorageClasses::Normal();
image.Init(width, height, storageType, rgbFormat) iferr_return;
// get handler to fill image
const maxon::ChannelOffsets& offset = rgbFormat.GetChannelOffsets();
const maxon::SetPixelHandlerStruct destHandler = image.SetPixelHandler(rgbFormat, offset, maxon::ColorProfile(), flags) iferr_return;
// random number generator for random colors
random.Init(0);
// array to store RGB data of a single pixel
pixelArray.Resize(3) iferr_return;
const maxon::BITS bitsPerPixel = rgbFormat.GetBitsPerPixel(); // size per pixel
// for each pixel generate a random color and store it in the image
for (maxon::Int y = 0; y < height; ++y)
{
for (maxon::Int x = 0; x < width; ++x)
{
auto GetRandomValue = [&random]() -> maxon::UChar
{
// get random value from 0 to 255
// use 255.9 to make sure all values have equal probability
const maxon::Float randomValue = random.Get01() * 255.9;
return maxon::SafeConvert<maxon::UChar>(randomValue);
};
// create random RGB data
pixelArray[0] = GetRandomValue();
pixelArray[1] = GetRandomValue();
pixelArray[2] = GetRandomValue();
// prepare data
maxon::UChar* data = pixelArray.GetFirst();
const maxon::PixelMutableBuffer buffer(data, bitsPerPixel); // pixel data
// set pixel data
const maxon::ImagePos pos { x, y, 1 }; // pixel position and count
}
}
// save image data to file
// create MediaOutputUrlRef from maxon::ImageSaverClasses::Png published object.
const maxon::MediaOutputUrlRef png = maxon::ImageSaverClasses::Png().Create() iferr_return;
// prepare MediaSession
maxon::MediaSessionRef session;
// prepare ImageTextureRef
const maxon::ImageTextureRef textureRef = maxon::ImageTextureClasses::TEXTURE().Create() iferr_return;
// add the prepared ImageRef as a child
textureRef.AddChildren(maxon::IMAGEHIERARCHY::IMAGE, image, maxon::ImageBaseRef()) iferr_return;
// save to target URL as PNG using the given MediaSession
textureRef.Save(target, png, maxon::MEDIASESSIONFLAGS::NONE, &session) iferr_return;
// use MediaSession save the image to a file
session.Close() iferr_return;

ImageBaseInterface

maxon::ImageBaseInterface is the base interface of all image interfaces.

An image typically stores bitmap data. This data is accessed with:

An image can have multiple channels / components:

Changes to the image can be detected with this observable:

  • ObservableRegionChanged: Is fired as soon as anybody modifies the pixel data.

maxon::ImageBaseInterface is based on

// This example loads an image file, inverts the RGB data and saves it to a new image file.
// load image
const maxon::ImageTextureRef sourceImage = maxon::ImageTextureClasses::TEXTURE().Create() iferr_return;
// read data
const maxon::PixelFormat format = maxon::PixelFormats::RGB::U8();
const auto offset = format.GetChannelOffsets();
const maxon::GetPixelHandlerStruct read = sourceImage.GetPixelHandler(format, offset, maxon::ColorProfile(), maxon::GETPIXELHANDLERFLAGS::NONE, nullptr) iferr_return;
const maxon::Int width = sourceImage.GetWidth();
const maxon::Int height = sourceImage.GetHeight();
// prepare buffer
const maxon::Int size = width * format.GetChannelCount();
data.Resize(size) iferr_return;
// load data into buffer
const maxon::BITS bitsPerPixel = format.GetBitsPerPixel(); // size per pixel
const maxon::PixelMutableBuffer buffer(data.GetFirst(), bitsPerPixel); // pixel data
// store in Image
// create ImageRef implementation of ImageInterface
const maxon::ImageRef invertedImage = maxon::ImageClasses::IMAGE().Create() iferr_return;
// init image
const auto storageType = maxon::ImagePixelStorageClasses::Normal();
invertedImage.Init(width, height, storageType, format) iferr_return;
// get handler to fill image
const maxon::ColorProfile colorProfile { };
const maxon::SetPixelHandlerStruct destHandler = invertedImage.SetPixelHandler(format, offset, colorProfile, handlerFlags) iferr_return;
// loop through each line
// invert each component
for (maxon::Int y = 0; y < height; ++y)
{
const maxon::ImagePos pos { 0, y, width };
[&data](maxon::Int i)
{
data[i] = 255 - data[i];
});
}
// save to file
const maxon::ImageTextureRef targetImage = maxon::ImageTextureClasses::TEXTURE().Create() iferr_return;
targetImage.AddChildren(maxon::IMAGEHIERARCHY::IMAGE, invertedImage, maxon::ImageBaseRef()) iferr_return;
const maxon::MediaOutputUrlRef png = maxon::ImageSaverClasses::Png().Create() iferr_return;
targetImage.Save(targetUrl, png, maxon::MEDIASESSIONFLAGS::NONE) iferr_return;

ImageTextureInterface

maxon::ImageTextureInterface represents a texture object with one or multiple images and layers.

The namespace maxon::ImageTextureClasses contains published objects with implementations of maxon::ImageTextureInterface. The default implementation is maxon::ImageTextureClasses::TEXTURE in gfx_image.h.

// This example loads an image file into a ImageTextureRef
// and saves the image data to a new JPG file.
// load image file
const maxon::ImageTextureRef image = maxon::ImageTextureClasses::TEXTURE().Create() iferr_return;
// convert to JPG
const maxon::MediaOutputUrlRef jpg = maxon::ImageSaverClasses::Jpg().Create() iferr_return;
// define JPG settings
maxon::DataDictionary exportSettings;
exportSettings.Set(maxon::MEDIASESSION::EXPORT::QUALITY, 0.8_f) iferr_return;
image.Set(maxon::IMAGEPROPERTIES::IMAGE::EXPORTSETTINGS, exportSettings) iferr_return;
// save
image.Save(targetUrl, jpg, maxon::MEDIASESSIONFLAGS::NONE) iferr_return;

ImageInterface

maxon::ImageInterface represents a single image of a maxon::ImageTextureInterface.

The namespace maxon::ImageClasses contains published objects with implementations of maxon::ImageInterface. The default implementation is maxon::ImageClasses::IMAGE in gfx_image.h.

ImageLayerInterface

maxon::ImageLayerInterface represents a single layer within a maxon::ImageInterface. The namespace maxon::ImageLayerClasses contains published objects with implementations of maxon::ImageLayerInterface. The default implementation is maxon::ImageLayerClasses::RASTER in gfx_image.h.

Utility

Utility functions to handle images:

Further Reading

maxon::SETPIXELFLAGS::NONE
@ NONE
No options set.
maxon::BITS
This class represents the number of bits in a pixel format.
Definition: gfx_image_bits.h:14
maxon::BaseArray::Resize
ResultMem Resize(Int newCnt, COLLECTION_RESIZE_FLAGS resizeFlags=COLLECTION_RESIZE_FLAGS::DEFAULT)
Definition: basearray.h:1077
maxon::BaseArray::GetFirst
const MAXON_ATTRIBUTE_FORCE_INLINE T * GetFirst() const
Definition: basearray.h:1034
iferr_return
#define iferr_return
Definition: resultbase.h:1434
maxon::SetPixelHandlerStruct
Definition: gfx_image_pixelhandler.h:133
maxon::Float
Float64 Float
Definition: apibase.h:193
maxon::SETPIXELHANDLERFLAGS::NONE
@ NONE
No options set.
maxon::GetPixelHandlerStruct
Definition: gfx_image_pixelhandler.h:31
maxon::BaseArray
Definition: basearray.h:366
maxon::ParallelFor::Dynamic
static auto Dynamic(FROMTYPE from, INDEXTYPE to, const LOOP &obj, Int threadCnt=PARALLELFOR_USEMAXIMUMTHREADS, const Int granularity=PARALLELFOR_DEFAULTGRANULARITY, JobQueueInterface *queue=JOBQUEUE_CURRENT) -> decltype(obj(to))
Definition: parallelfor.h:238
maxon::GETPIXELHANDLERFLAGS::NONE
@ NONE
no options set.
maxon::GetPixelHandlerStruct::GetPixel
MAXON_ATTRIBUTE_FORCE_INLINE Result< void > GetPixel(const ImagePos &pos, const PixelMutableBuffer &buffer, GETPIXELFLAGS flags) const
Definition: gfx_image_pixelhandler.h:96
maxon::Int
Int64 Int
signed 32/64 bit int, size depends on the platform
Definition: apibase.h:184
maxon::LinearCongruentialRandom
Definition: lib_math.h:18
maxon::LinearCongruentialRandom::Init
void Init(UInt32 seed)
maxon::GETPIXELFLAGS::NONE
@ NONE
no options set.
maxon::SETPIXELHANDLERFLAGS
SETPIXELHANDLERFLAGS
Flags to control the SetPixelHandler functions.
Definition: gfx_image_pixelformat.h:64
maxon::SetPixelHandlerStruct::SetPixel
MAXON_ATTRIBUTE_FORCE_INLINE Result< void > SetPixel(const ImagePos &pos, const PixelConstBuffer &buffer, SETPIXELFLAGS flags) const
Definition: gfx_image_pixelhandler.h:191
maxon::TimeValue
The TimeValue class encapsulates a timer value.
Definition: timevalue.h:33
maxon::ImagePos
Several functions use this helper structure to pass the position within an image and number of pixels...
Definition: gfx_image_pixelhandler.h:17
maxon::UChar
unsigned char UChar
unsigned 8 bit character
Definition: apibase.h:181
maxon::MEDIASESSIONFLAGS::NONE
@ NONE
maxon::PixelMutableBuffer
Several functions use this helper structure to pass the image data to functions.
Definition: gfx_image_pixelformat.h:134
maxon::LinearCongruentialRandom::Get01
FLOAT Get01()
Returns the next random value in the range of [0..1].
maxon::IMAGEHIERARCHY::IMAGE
@ IMAGE
Adds a subImage to a texture. Images are only allowed under Textures.
maxon::Block< const BITS >