Image extension

Create and Manipulate Images

Create and Manipulate Images

Explore one topic in small steps. Try the examples one at a time and take a break whenever you need. Choose how many extra exercises you want to try.

Creating Images

The first Image extension

Image belongs to an extension, rather than Core. Write #include <small/image.h> at the top. Small IDE sees this include and automatically links the Image extension.

Try it first

Example · example1.cpp
#include <small/image.h>

void small_main()
{
    Image image(160, 120, Blue);

    for (int y = 20; y < 100; y++)
        for (int x = 20; x < 140; x++)
            image.set_pixel(x, y, Yellow);

    Window window;
    window.open(640, 400);

    while (window.is_open())
    {
        window.clear(Black);
        draw_image(window, image, 80, 80);
        draw_image(window, image, 300, 80, 240, 180);
        window.show();
    }
}

Image and Window are different things

Image owns image data and provides operations intrinsic to an image, such as width(), height(), and pixel(). draw_image(window, image, ...) is a free function because it connects Image and Window.

Omitting a size draws at the original size. Supplying width and height smoothly scales it to that size.

One step further

Example · example2.cpp
#include <small/image.h>

void small_main()
{
    Image image(100, 100, Red);
    print(image.width(), " x ", image.height());

    Window window;
    window.open(400, 250);

    while (window.is_open())
    {
        window.clear(White);
        draw_image(window, image, 150, 75);
        window.show();
    }
}

Extensions are ordinary C++ too

Using Image does not mean learning a new language. You include a header and use ordinary C++ objects and functions.

Pixels

An image is a grid of pixels

Change a pixel's color with set_pixel(x, y, color) and read it with pixel(x, y). As in Window, (0, 0) is the top-left corner.

Try it first

Example · pixels_example1.cpp
#include <small/image.h>

void small_main()
{
    Image image(256, 256, Black);

    for (int y = 0; y < image.height(); y++)
        for (int x = 0; x < image.width(); x++)
            image.set_pixel(x, y, rgb(x, y, 180));

    Window window;
    window.open(520, 320);

    while (window.is_open())
    {
        window.clear(Black);
        draw_image(window, image, 32, 32);
        window.show();
    }
}

Creating pixels makes algorithms visible

Two nested loops visiting every pixel let you build gradients, patterns, and simple image processing yourself. Out-of-range pixel coordinates are reported as errors.

One step further

Example · pixels_example2.cpp
#include <small/image.h>

void small_main()
{
    Image image(200, 200, Black);

    for (int y = 0; y < image.height(); y++)
        for (int x = 0; x < image.width(); x++)
            if ((x / 20 + y / 20) % 2 == 0)
                image.set_pixel(x, y, White);

    save_image(image, "checker.png");
    print("Saved checker.png");
}

Pixel work and drawing are separate

Changing data inside Image is a member operation. Displaying the result in a Window uses draw_image.

Exercise — Draw at two sizes

Create a 120 × 80 Green image. Draw it at its original size and twice that size in one window.

Exercise starter · exercise1_starter.cpp
#include <small/image.h>
void small_main()
{
    Image image(120, 80, Green);
    Window window;
    window.open(600, 300);
    while (window.is_open())
    {
        window.clear(Black);
        // Draw twice here.
        window.show();
    }
}

Hint

Use both the four-argument and six-argument forms of draw_image.

show solution
#include <small/image.h>
void small_main()
{
    Image image(120, 80, Green);
    Window window;
    window.open(600, 300);
    while (window.is_open())
    {
        window.clear(Black);
        draw_image(window, image, 40, 60);
        draw_image(window, image, 260, 40, 240, 160);
        window.show();
    }
}

Exercise — Check the size

Create a 64 × 48 image and print its Width and Height.

Exercise starter · exercise2_starter.cpp
#include <small/image.h>
void small_main()
{
    Image image(64, 48, Blue);
    // print its size.
}

Hint

Use print(image.width(), " x ", image.height());.

show solution
#include <small/image.h>
void small_main()
{
    Image image(64, 48, Blue);
    print(image.width(), " x ", image.height());
}
Try More (Optional)

Exercise — A diagonal

Draw a Yellow diagonal from (0, 0) to (99, 99) in a 100 × 100 Black image.

Exercise starter · pixels_exercise1_starter.cpp
#include <small/image.h>
void small_main()
{
    Image image(100, 100, Black);
    // Draw a yellow diagonal with set_pixel.
    save_image(image, "diagonal.png");
}

Hint

Use the loop's i for both x and y.

show solution
#include <small/image.h>
void small_main()
{
    Image image(100, 100, Black);
    for (int i = 0; i < 100; i++)
        image.set_pixel(i, i, Yellow);
    save_image(image, "diagonal.png");
}

Exercise — Read a pixel

Read pixel (10, 10) in a Red image and print its red component.

Exercise starter · pixels_exercise2_starter.cpp
#include <small/image.h>
void small_main()
{
    Image image(30, 30, Red);
    // Read and print the red component.
}

Hint

After Color color = image.pixel(10, 10);, use color.red().

show solution
#include <small/image.h>
void small_main()
{
    Image image(30, 30, Red);
    Color color = image.pixel(10, 10);
    print(color.red());
}
← Previous lessonNext lesson →