Skip to content
Christian Henning edited this page Mar 22, 2018 · 19 revisions

Gil strength is its expressiveness by using concepts and plenty of meta-programming. This makes gil hard to learn and worse hard to extend without creating a mess.

This wiki will show some of the power of gil.

// Always assume
using namespace std;
using namespace boost::gil;

How to get the color space type from an image/view?

typedef rgb8_image_t image_t;
typedef typename color_space_type<image_t::view_t::value_type>::type colour_space_t;

There are rgb8_pixel_t, bgr8_pixel_t, and any other number of channel combinations. So, how do you get the red channel value?

auto get_red(pixel_t p)
{
    return get_color(p, red_t());
}

Each pixel type is defined with a bunch of channel types. Look here for the correct way of defining a new pixel type.

SDL

#include <boost/test/unit_test.hpp>
#include <boost/gil/gil_all.hpp>


using namespace std;

unsigned int width  = 640;
unsigned int height = 480;

// format is ARGB8888 -> 4 bytes

// scanline size in bytes
unsigned int scanline = width  * sizeof( Uint32 );
unsigned int frame_size = scanline * height;

SDL_Renderer* ren = NULL;
SDL_Texture* tex = NULL;
Uint32* pixels = NULL;

Uint32 draw( Uint32 interval, void* param )
{
    using namespace boost;
    using namespace gil;

    argb8_view_t v = interleaved_view( width, height, (argb8_pixel_t*) pixels, scanline );
    fill_pixels( v, argb8_pixel_t( 255, 255, 0 , 0 ));

    //memset( pixels, 0, frame_size );

    SDL_UpdateTexture( tex, NULL, pixels, scanline );

    SDL_RenderClear( ren );
   
    SDL_RenderCopy( ren, tex, NULL, NULL );   

    SDL_RenderPresent( ren );

    return interval;
}

BOOST_AUTO_TEST_CASE( sdl_test )
{
    if( SDL_Init( SDL_INIT_EVERYTHING ) == -1 )
    {
        cout << SDL_GetError() << endl;
    }

    // Create Window

    SDL_Window* win = SDL_CreateWindow( "First"
                                      , 100
                                      , 100
                                      , width
                                      , height
                                      , SDL_WINDOW_SHOWN
                                      );

    if( win == NULL )
    {
        cout << SDL_GetError() << endl;
    }

    // Create Renderer

    ren = SDL_CreateRenderer( win
                            , -1 // SDL selects video driver
                            , SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC
                            );

    if( ren == NULL )
    {
        cout << SDL_GetError() << endl;
    }

    tex = SDL_CreateTexture( ren
                           , SDL_PIXELFORMAT_ARGB8888
                           , SDL_TEXTUREACCESS_STREAMING
                           , width
                           , height
                           );

    if( tex == NULL )
    {
        cout << SDL_GetError() << endl;
    }


    pixels = (Uint32*) malloc( frame_size );

    // Add Timer
    SDL_AddTimer( 100, draw, NULL );

    // Wait for user to quit
    bool quit = false;
    SDL_Event e;

    while( quit == false )
    {
        while( SDL_PollEvent( &e ))
        {
            if( e.type == SDL_WINDOWEVENT )
            {
                auto id = e.window.windowID;

                break;
            }

            if( e.type == SDL_QUIT )
            {
                quit = true;
                break;
            }

            if( e.type == SDL_KEYDOWN )
            {
                quit = true;
                break;
            }
        }
    }


    // Clean up
    SDL_DestroyTexture( tex );
    SDL_DestroyRenderer( ren );
    SDL_DestroyWindow( win );

    SDL_Quit();
}

Clone this wiki locally