class EE::Graphics::Image

Overview

A simple image class to manipulate them. More…

#include <image.hpp>

class Image {
public:
    // enums

    enum PixelFormat;
    enum ResamplerFilter;
    enum SaveType;

    // classes

    class FormatConfiguration;

    // construction

    Image();
    Image(Graphics::Image* image);
    Image(Uint8* data, const unsigned int& width, const unsigned int& height, const unsigned int& channels);
    Image(const Uint8* data, const unsigned int& width, const unsigned int& height, const unsigned int& channels);
    Image(const Uint32& width, const Uint32& height, const Uint32& channels, const Color& DefaultColor = Color(0, 0, 0, 0), const bool& initWithDefaultColor = true);
    Image(std::string Path, const unsigned int& forceChannels = 0, const FormatConfiguration& formatConfiguration = FormatConfiguration());
    Image(const Uint8* imageData, const unsigned int& imageDataSize, const unsigned int& forceChannels = 0, const FormatConfiguration& formatConfiguration = FormatConfiguration());
    Image(Pack* Pack, std::string FilePackPath, const unsigned int& forceChannels = 0, const FormatConfiguration& formatConfiguration = FormatConfiguration());
    Image(IOStream& stream, const unsigned int& forceChannels = 0, const FormatConfiguration& formatConfiguration = FormatConfiguration());
    virtual ~Image();

    // methods

    static std::string saveTypeToExtension(const Int32& Format);
    static SaveType extensionToSaveType(const std::string& Extension);
    static PixelFormat channelsToPixelFormat(const Uint32& channels);
    static bool getInfo(const std::string& path, int* width, int* height, int* channels, const FormatConfiguration& imageFormatConfiguration = FormatConfiguration());
    static bool getInfoFromMemory(const unsigned char* data, const size_t& dataSize, int* width, int* height, int* channels, const FormatConfiguration& imageFormatConfiguration = FormatConfiguration());
    static bool isImage(const std::string& path);
    static bool isImageExtension(const std::string& path);
    static std::string getLastFailureReason();
    static Image* New();
    static Image* New(Graphics::Image* image);
    static Image* New(Uint8* data, const unsigned int& width, const unsigned int& height, const unsigned int& channels);
    static Image* New(const Uint8* data, const unsigned int& width, const unsigned int& height, const unsigned int& channels);
    static Image* New(const Uint32& width, const Uint32& height, const Uint32& channels, const Color& DefaultColor = Color(0, 0, 0, 0), const bool& initWithDefaultColor = true);
    static Image* New(std::string Path, const unsigned int& forceChannels = 0, const FormatConfiguration& formatConfiguration = FormatConfiguration());
    static Image* New(const Uint8* imageData, const unsigned int& imageDataSize, const unsigned int& forceChannels = 0, const FormatConfiguration& formatConfiguration = FormatConfiguration());
    static Image* New(Pack* Pack, std::string FilePackPath, const unsigned int& forceChannels = 0, const FormatConfiguration& formatConfiguration = FormatConfiguration());
    static Image* New(IOStream& stream, const unsigned int& forceChannels = 0, const FormatConfiguration& formatConfiguration = FormatConfiguration());
    void create(const Uint32& width, const Uint32& height, const Uint32& channels, const Color& DefaultColor = Color(0, 0, 0, 0), const bool& initWithDefaultColor = true);
    virtual Color getPixel(const unsigned int& x, const unsigned int& y);
    virtual void setPixel(const unsigned int& x, const unsigned int& y, const Color& Color);
    virtual void setPixels(const Uint8* data);
    virtual const Uint8* getPixelsPtr();
    Uint8* getPixels() const;
    void setWidth(const unsigned int& width);
    unsigned int getWidth() const;
    void setHeight(const unsigned int& height);
    unsigned int getHeight() const;
    unsigned int getChannels() const;
    void setChannels(const unsigned int& setChannels);
    void clearCache();
    unsigned int getMemSize() const;
    Sizei getSize();
    virtual bool saveToFile(const std::string& filepath, const SaveType& Format);
    virtual void createMaskFromColor(const Color& ColorKey, Uint8 Alpha);
    void createMaskFromColor(const RGB& ColorKey, Uint8 Alpha);
    virtual void replaceColor(const Color& ColorKey, const Color& NewColor);
    virtual void fillWithColor(const Color& Color);
    virtual void copyImage(Graphics::Image* image, const Uint32& x = 0, const Uint32& y = 0);
    virtual void scale(const Float& scale, ResamplerFilter filter = ResamplerFilter::RESAMPLER_LANCZOS4);
    virtual void resize(const Uint32& newWidth, const Uint32& newHeight, ResamplerFilter filter = ResamplerFilter::RESAMPLER_LANCZOS4);
    virtual void flip();
    Graphics::Image* thumbnail(const Uint32& maxWidth, const Uint32& maxHeight, ResamplerFilter filter = ResamplerFilter::RESAMPLER_LANCZOS4);
    Graphics::Image* crop(Rect rect);
    void avoidFreeImage(const bool& AvoidFree);
    void blit(Graphics::Image* image, const Uint32& x = 0, const Uint32& y = 0);
    Graphics::Image* copy();
    Graphics::Image& operator=(const Image& right);
    void setImageFormatConfiguration(const FormatConfiguration& imageFormatConfiguration);
    const FormatConfiguration& getImageFormatConfiguration() const;
};

Detailed Documentation

A simple image class to manipulate them.

Construction

Image(Graphics::Image* image)

Copy a image data to create the new image

Image(Uint8* data, const unsigned int& width, const unsigned int& height, const unsigned int& channels)

Use an existing image ( and appropriates the data passed )

Image(const Uint8* data, const unsigned int& width, const unsigned int& height, const unsigned int& channels)

Copy a image data to create the image

Image(const Uint32& width, const Uint32& height, const Uint32& channels, const Color& DefaultColor = Color(0, 0, 0, 0), const bool& initWithDefaultColor = true)

Create an empty image

Image(std::string Path, const unsigned int& forceChannels = 0, const FormatConfiguration& formatConfiguration = FormatConfiguration())

Load an image from path

Parameters:

Path

The path to the file.

forceChannels

Number of channels to use for the image, default 0 means that it use the default image channels.

formatConfiguration

The specific image format configuration to use when decoding the image.

Image(const Uint8* imageData, const unsigned int& imageDataSize, const unsigned int& forceChannels = 0, const FormatConfiguration& formatConfiguration = FormatConfiguration())

Load a compressed image from memory

Parameters:

imageData

The image data

imageDataSize

The image size

forceChannels

Number of channels to use for the image, default 0 means that it use the default image channels.

formatConfiguration

The specific image format configuration to use when decoding the image.

Image(Pack* Pack, std::string FilePackPath, const unsigned int& forceChannels = 0, const FormatConfiguration& formatConfiguration = FormatConfiguration())

Load an image from pack

Parameters:

Pack

The pack file to use to load the image.

FilePackPath

The path of the file inside the pack file.

forceChannels

Number of channels to use for the image, default 0 means that it use the default image channels.

formatConfiguration

The specific image format configuration to use when decoding the image.

Image(IOStream& stream, const unsigned int& forceChannels = 0, const FormatConfiguration& formatConfiguration = FormatConfiguration())

Load an image from stream

Parameters:

stream

The stream to read the image

forceChannels

Number of channels to use for the image, default 0 means that it use the default image channels.

formatConfiguration

The specific image format configuration to use when decoding the image.

Methods

static std::string saveTypeToExtension(const Int32& Format)

Returns:

The File Extension of a Save Type

static SaveType extensionToSaveType(const std::string& Extension)

Returns:

The save type from a given extension ( example: “png” => SaveType::SAVE_TYPE_PNG )

static PixelFormat channelsToPixelFormat(const Uint32& channels)

Returns:

Convert the number of channels to a pixel format

static bool getInfo(const std::string& path, int* width, int* height, int* channels, const FormatConfiguration& imageFormatConfiguration = FormatConfiguration())

Parameters:

path

the image path

width

the var to store the image width

height

the var to store the image height

channels

the var to store the image channels count

imageFormatConfiguration

The specific image format configuration to use when decoding the image.

Returns:

True if success to get the info.

static bool getInfoFromMemory(const unsigned char* data, const size_t& dataSize, int* width, int* height, int* channels, const FormatConfiguration& imageFormatConfiguration = FormatConfiguration())

Parameters:

data

the pointer containing the image raw data

dataSize

the point data size

width

the var to store the image width

height

the var to store the image height

channels

the var to store the image channels count

imageFormatConfiguration

The specific image format configuration to use when decoding the image.

Returns:

True if success to get the info.

static bool isImage(const std::string& path)

Parameters:

path

the image path

Returns:

True if the file is a valid image ( reads the file header to know if the file is an image file format supported )

static bool isImageExtension(const std::string& path)

Parameters:

path

the image path or file name

Returns:

If the path or file name has a supported image file extension

static std::string getLastFailureReason()

Returns:

The last failure image loading/info reason

void create(const Uint32& width, const Uint32& height, const Uint32& channels, const Color& DefaultColor = Color(0, 0, 0, 0), const bool& initWithDefaultColor = true)

Create an empty image data

virtual Color getPixel(const unsigned int& x, const unsigned int& y)

Return the pixel color from the image.

You must have a copy of the image on local memory. For that you need to Lock the image first.

virtual void setPixel(const unsigned int& x, const unsigned int& y, const Color& Color)

Set the pixel color to the image.

You must have a copy of the image on local memory. For that you need to Lock the image first.

virtual void setPixels(const Uint8* data)

Assign a new array of pixels to the image in local memory ( it has to be exactly of the same size of the image )

virtual const Uint8* getPixelsPtr()

Returns:

A pointer to the first pixel of the image.

Uint8* getPixels() const

Return the pointer to the array containing the image

void setWidth(const unsigned int& width)

Set the image Width

unsigned int getWidth() const

Returns:

The image Width

void setHeight(const unsigned int& height)

Set the image Height

unsigned int getHeight() const

Returns:

The image Height

unsigned int getChannels() const

Returns:

The number of channels used by the image

void setChannels(const unsigned int& setChannels)

Set the number of channels of the image

void clearCache()

Clears the current image cache if exists

unsigned int getMemSize() const

Returns:

The Image Size on Memory (in bytes)

Sizei getSize()

Returns:

The image dimensions

virtual bool saveToFile(const std::string& filepath, const SaveType& Format)

Save the Image to a new File in a specific format

virtual void createMaskFromColor(const Color& ColorKey, Uint8 Alpha)

Create an Alpha mask from a Color

void createMaskFromColor(const RGB& ColorKey, Uint8 Alpha)

Create an Alpha mask from a Color

virtual void replaceColor(const Color& ColorKey, const Color& NewColor)

Replace a color on the image

virtual void fillWithColor(const Color& Color)

Fill the image with a color

virtual void copyImage(Graphics::Image* image, const Uint32& x = 0, const Uint32& y = 0)

Copy the image to this image data, starting from the position x,y

virtual void scale(const Float& scale, ResamplerFilter filter = ResamplerFilter::RESAMPLER_LANCZOS4)

Scale the image

virtual void resize(const Uint32& newWidth, const Uint32& newHeight, ResamplerFilter filter = ResamplerFilter::RESAMPLER_LANCZOS4)

Resize the image

virtual void flip()

Flip the image ( rotate the image 90º )

Graphics::Image* thumbnail(const Uint32& maxWidth, const Uint32& maxHeight, ResamplerFilter filter = ResamplerFilter::RESAMPLER_LANCZOS4)

Create a thumnail of the image

Graphics::Image* crop(Rect rect)

Creates a cropped image from the current image

void avoidFreeImage(const bool& AvoidFree)

Set as true if you dont want to free the image data in the Image destruction ( false as default ).

void blit(Graphics::Image* image, const Uint32& x = 0, const Uint32& y = 0)

Blit the image passed onto the current image

Parameters:

image

The source image to blit onto the image

x

The x position to start drawing the image

y

The y position to start drawing the image

Graphics::Image* copy()

Returns:

A copy of the original image

Graphics::Image& operator=(const Image& right)

Overload the assigment operator to ensure the image copy

void setImageFormatConfiguration(const FormatConfiguration& imageFormatConfiguration)

Set the image format configuration

const FormatConfiguration& getImageFormatConfiguration() const

Returns:

The image format configuration