Image 확장

이미지 만들고 다루기

이미지 만들고 다루기

한 주제 안에서 차근차근 배워 봅니다. 예제를 하나씩 실행하고, 중간에 쉬었다가 이어서 해도 좋습니다. 마지막의 추가 연습은 원하는 만큼 해 보세요.

Creating Images

첫 Image extension

Image는 Core가 아니라 extension입니다. 그래서 첫 줄에서 #include <small/image.h>를 적습니다. Small IDE는 이 include를 보고 Image extension을 자동으로 연결합니다.

먼저 실행해 보세요

예제 · 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와 Window는 서로 다른 것

Image는 이미지 데이터를 소유하고 width(), height(), pixel() 같은 intrinsic operation을 제공합니다. draw_image(window, image, ...)는 Image와 Window의 관계이므로 free function입니다.

크기를 생략하면 원래 크기로 그리고, width와 height를 주면 그 크기로 부드럽게 확대/축소합니다.

한 단계 더

예제 · 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();
    }
}

Extension도 그냥 C++입니다

Image를 사용한다고 새로운 언어를 배우는 것은 아닙니다. header 하나를 include하고 평범한 C++ object와 function을 사용하는 것입니다.

Pixels

이미지는 픽셀의 격자입니다

set_pixel(x, y, color)로 한 픽셀의 색을 바꾸고 pixel(x, y)로 읽을 수 있습니다. 좌표는 Window와 마찬가지로 왼쪽 위가 (0, 0)입니다.

먼저 실행해 보세요

예제 · 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();
    }
}

직접 픽셀을 만들면 알고리즘이 눈에 보입니다

두 겹의 loop로 모든 픽셀을 방문하면 gradient, pattern, 간단한 image processing을 직접 만들 수 있습니다. 범위를 벗어난 pixel 좌표는 오류로 처리됩니다.

한 단계 더

예제 · 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 작업과 drawing은 분리되어 있습니다

Image 안의 데이터를 바꾸는 것은 member function이고, 그 결과를 Window에 보여주는 것은 draw_image입니다.

Exercise — 두 크기로 그리기

120×80 Green 이미지를 만들고 원래 크기와 두 배 크기로 한 창에 그리세요.

연습 문제 코드 · 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

draw_image의 4-argument와 6-argument 형태를 각각 사용하세요.

풀이 보기
#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 — 크기 확인

64×48 이미지를 만들고 width와 height를 출력하세요.

연습 문제 코드 · exercise2_starter.cpp
#include <small/image.h>
void small_main()
{
    Image image(64, 48, Blue);
    // print its size.
}

Hint

print(image.width(), " x ", image.height());를 사용하세요.

풀이 보기
#include <small/image.h>
void small_main()
{
    Image image(64, 48, Blue);
    print(image.width(), " x ", image.height());
}
더 해 보기 (선택)

Exercise — 대각선

100×100 Black 이미지에 (0,0)부터 (99,99)까지 Yellow 대각선을 그리세요.

연습 문제 코드 · 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

loop의 i를 x와 y에 모두 사용하세요.

풀이 보기
#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 — 픽셀 읽기

Red 이미지의 (10,10) 픽셀을 읽어 Red 성분을 출력하세요.

연습 문제 코드 · pixels_exercise2_starter.cpp
#include <small/image.h>
void small_main()
{
    Image image(30, 30, Red);
    // Read and print the red component.
}

Hint

Color color = image.pixel(10, 10); 다음 color.red()를 사용하세요.

풀이 보기
#include <small/image.h>
void small_main()
{
    Image image(30, 30, Red);
    Color color = image.pixel(10, 10);
    print(color.red());
}
← 이전 레슨다음 레슨 →