Logo de Aster Système

"Image" class

This class is the main class used to handle image.
Contenu

Basic image manipulation

Create an image

The "Image" class is made to use images in C++. It can uses a lot of constructor, to make a blank image, or to load a pre-existing image The image is stored in the memory with the Bytes_Set class, from SCLS Foundation. It is in general an RGB image, with 8 bits by components. You can also add an alpha canal to it.

Modify the image

The most simple image manipulation is the editing of a single pixel. It is easily made with the "set_pixel" methods. By combining a lot of "set_pixel", you can draw a rectangle with the "draw_rect" methods. You can also fill it with "fill_rect". If you prefer 1 dimension shape, you can draw a line with the "draw_line" methods. If you prefer triangle, you can fill a triangle with the "fill_triangle" methods. However, if you want to edit all the image directly, use the "fill" methods. You can also couple more than one image, with the "paste" methods. An image can also be flipped, horizontally with "flip_x" and vertically with "flip_y".

Contenu

Saving and loading an image

Load an image

If you want to use an already existing image, you will have to load it from a pre-existing file. For now, only "png" format can be loaded, with the "load_from_path" method, or with the "Image(std::string path)" constructor.

Saving an image

With only this librairie used, you can't display the image to the screen directly. However, if you want to see the result of an image, you can save it and use your photo app to see it. For now, the image can only be saved in the "png" format, with the "save_png" method.

Contenu

Exemples of using

Chess

We will create a chess board using SCLS Image. First of all, let's create the base of a C++ / SCLS project (main file, SCLS_INIT...). After that, we start to configure some datas about the board : the RGB code of the white part, the RGB code of the black part and the width / height of the image. Then, we create the image containing the board, with the same background color as the white part. To finish, we create the loops, made to draw each black cases of the board. With all that done, we can save the picture, at the path "chess.png".

// Start the file #include "scls_image.h"
SCLS_INIT
using namespace scls;

// Main function
int main() {
    // Define the needed datas
    Color black_color(0, 0, 0);
    int image_width = 1000;
    Color white_color(255, 204, 153);

    // Create the image
    Image img(image_width, image_width, white_color);

    // Draw each black cases
    int case_width = image_width / 8;
    for(int i = 0;i < 8;i++) {
        for(int j = 0;j < 4;j++) {
            img.fill_rect(case_width * (j * 2 + i % 2), case_width * i, case_width, case_width, black_color);
        }
    }

    // Save the image
    img.save_png("chess.png");
}
contenu

Documentation

The "Color" class

Color(unsigned char red, unsigned char green, unsigned char blue, unsigned char alpha = 255)

Construct a simple color, with the data RGBA("red", "green", "blue", "alpha").

inline unsigned char alpha() const

Returns the alpha component of the color.

inline unsigned char blue() const

Returns the blue component of the color.

static Color from_std_string(std::string source)

Returns the color described by the std::string in "source". In the "source", the text is formated until there is no "(", ")" or space at the begin and at the end of the text. Moreover, in the text, each colors are expected to be separated by a comma. The first, second and third colors are the red, green and blue colors (between 0 and 255). If a fourth color is provided, it is used as alpha channel.

inline unsigned char green() const

Returns the green component of the color.

inline unsigned char red() const

Returns the red component of the color.

inline void set_alpha(unsigned char new_alpha)

Sets the alpha component of the color at "alpha".

inline void set_blue(unsigned char new_blue)

Sets the blue component of the color at "blue".

inline void set_green(unsigned char new_green)

Sets the green component of the color at "green".

inline void set_red(unsigned char new_red)

Sets the red component of the color at "red".

inline void set_rgb(unsigned char new_red, unsigned char new_green, unsigned char new_blue)

Sets the red, blue and green components of the color at "red", "blue" and "green".

inline void set_rgba(unsigned char new_red, unsigned char new_green, unsigned char new_blue, unsigned char new_alpha)

Sets the red, green, blue and alpha components of the color at "red", "green", "blue" and "alpha".

"Image" handling

Image()

Construct a simple image, without any datas in it. Directly using this constructor is not recommended.

Image(std::string path)

Create an image, loaded from an already existing at path "path".

Image(unsigned short width, unsigned short height, unsigned char red, unsigned char green, unsigned char blue, unsigned char alpha = 255, unsigned int color_type = 6)

Create an image with the size ("width", "height"), and filled with the specified color.

Image(unsigned short width, unsigned short height, Color color, unsigned int color_type = 6)

Create an image with the size ("width", "height"), and filled with the specified color.

Image(Image& image_copy)
Image(Image* image_copy)

Copy constructors of the image.

Returns the number of bit taken by a component in the image.

scls_var member_content

inline unsigned int buffer_size() const

Returns the size taken by the image in the memory.

inline unsigned int color_type() const

Returns the type of color used in the image. The type 4 means RGB image. The type 6 means RGBA image.

inline unsigned char components() const

Returns the number of components in a pixel in the image.

inline Bytes_Set* datas() const

Returns a pointer to the Bytes_Set containing the datas of the image.

inline std::shared_ptr datas_filtered()

Returns a Bytes_Set with the datas filtered on it. The datas filtered represents the datas, as returned by datas(). However, the first byte of each line represents the filtering type of the line.

void fill(Color color)

Fill the image with the color "color".

void fill(unsigned char red, unsigned char green, unsigned char blue, unsigned char alpha = 255)

Fill the image with the color RGBA ("red", "green", "blue", "alpha").

inline void free_memory()

Frees the memory from the image (but also unloads it).

inline int height() const

Returns the height of the image in pixels.

Color pixel(unsigned short x, unsigned short y)

Returns the color of the pixel at the "x";"y" position in the image. If the position is out of the image, returns the color RGB (0, 0, 0) and prints a warning.

Color pixel_by_number(unsigned int position)

Returns the color of the "position"th pixel. If the position is out of the image, returns the RGB(0, 0, 0) color and prints a warning.

inline void set_pixel(int x, int y, Color color, unsigned short width = 1)

Sets the color of the "x";"y" pixel at "color", and does the same for the pixel in a "width" radius. If the position is out of the image, prints an error.

inline void set_pixel(int x, int y, unsigned char red, unsigned char green, unsigned char blue, unsigned char alpha = 255, unsigned short width_point = 1)

Sets the pixel at the "x";"y" position at the color RGBA("red", "green", "blue", "alpha"). If the position is out of the image, prints an error.

inline void set_pixel_alpha(unsigned short x, unsigned short y, unsigned char alpha)

Set the alpha component of the "x";"y" pixel at "alpha". If the position is out of the image, prints an error.

inline void set_pixel_blue(unsigned short x, unsigned short y, unsigned char blue, unsigned char alpha = 255)

Set the blue component of the "x";"y" pixel at "blue" with a possible alpha effet on it. If the position is out of the image, prints an error.

inline void set_pixel_green(unsigned short x, unsigned short y, unsigned char green, unsigned char alpha = 255)

Set the green component of the "x";"y" pixel at "green" with a possible alpha effet on it. If the position is out of the image, prints an error.

inline void set_pixel_rgba_directly(unsigned int position, unsigned char red, unsigned char green, unsigned char blue, unsigned char alpha, unsigned char multiplier)

Set the pixel at the given "position" at the needed color RGBA("red", "green", "blue", alpha"), by applying the given multiplier datas. This method is faster than set_pixel(), because it does not apply the security check for the binary handling. However, DON'T USE IT if you are not sure that binaries error can't occurs.

inline void set_pixel_red(unsigned short x, unsigned short y, unsigned char red, unsigned char alpha = 255)

Set the red component of the "x";"y" pixel at "red" with an alpha effet on it. If the position is out of the image, prints an error.

inline void set_pixel_by_number(unsigned int position, unsigned char red, unsigned char green, unsigned char blue, unsigned char alpha = 255)

Set the "position"th pixel at the color RGBA("red", "green", "blue", "alpha"). If the position is out of the image, prints an error.

inline void set_pixel_by_number(unsigned int position, Color color)

Set the "position"th pixel at the color "color". If the position is out of the image, prints an error.

inline bool use_alpha() const

Returns if the image uses an alpha component.

inline int width() const

Returns the width of the image in pixels.

Draw on an "Image"

void draw_line(int x_1, int y_1, int x_2, int y_2, unsigned char red, unsigned char green, unsigned char blue, unsigned char alpha = 255, unsigned short line_width = 1)
inline void draw_line(int x_1, int y_1, int x_2, int y_2, Color color, unsigned short width = 1)

Draw a line in the image, between the pixel "x_1";"y_1" and "x_2";"y_2",with the color "color" (or RGBA("red", "green", "blue", "alpha")). Each point placed have the width "line_width".

void draw_rect(unsigned short x, unsigned short y, unsigned short width, unsigned short height, unsigned int rect_width, unsigned char red, unsigned char green, unsigned char blue, unsigned char alpha = 255)
void draw_rect(unsigned short x, unsigned short y, unsigned short width, unsigned short height, unsigned int rect_width, Color color) void draw_rect(unsigned short x, unsigned short y, unsigned short width, unsigned short height, unsigned int rect_width, Color color, Color fill_color)

Draw the boundaries of the rect "x";"y";"width";"height", with the "rect_width" line width and the color "color" (or RGBA("red", "green", "blue", "alpha")). If "fill_color" is specified, the surface of the rectangle is filled with this color.

void fill_rect(int x, int y, unsigned short rect_width, unsigned short rect_height, unsigned char red, unsigned char green, unsigned char blue, unsigned char alpha = 255)
inline void fill_rect(int x, int y, unsigned short width, unsigned short height, Color color, unsigned char alpha = 255)

Fill the "x";"y";"width";"height" rect with the color "color" (or RGBA("red", "green", "blue", "alpha")).

void fill_triangle(short x_1, short y_1, short x_2, short y_2, short x_3, short y_3, unsigned char red, unsigned char green, unsigned char blue, unsigned char alpha = 255)
inline void fill_triangle(short x_1, short y_1, short x_2, short y_2, short x_3, short y_3, Color color)

Fill the "x_1";"y1";"x_2";"y_2";"x_3";"y_3" triangle with the color "color" (or RGBA("red", "green", "blue", "alpha")).

void flip_x()

Flips the image on the X axis.

void flip_x()

Flips the image on the Y axis.

inline void paste(Image* to_paste, short x, short y, double opacity = 1.0)
inline void paste(std::string path, unsigned short x, unsigned short y, float opacity = 1.0)

Paste the image "to_paste" on this image, starting at the pixel "x";"y". If a "path" is used, the image is directly loaded from the path and pasted.

inline std::shared_ptr<Image> resize_adaptative(unsigned short new_width, unsigned short new_height)

Returns a shared pointer of the image with a new size, and an adaptated display.

std::shared_ptr<Image> resize_adaptative_height(unsigned short new_height)

Returns a shared pointer of the image with a new height, and an adaptated display.

std::shared_ptr<Image> resize_adaptative_width(unsigned short new_width)

Returns a shared pointer of the image with a new width, and an adaptated display.

Store / load an "Image"

inline Bytes_Set* datas_png()

Returns a new Bytes_Set of the datas of the image, formatted in the PNG format.

std::shared_ptr<__Image_Error> load_from_path(std::string path)

Loads the image from the path "path", and returns the result of the loading.

inline std::vector png_signature()

Returns the file signature of a PNG file.

inline void save_png(std::string path)

Saves the datas of the image given by datas_png() in the path "path".