続Boost.GILで遊んでみる。

昨日のサンプルをさらに発展させて、縮拡・回転に挑戦してみた。

実際に動作させたところ:
http://video.google.com/videoplay?docid=-8908866618776833085

#include <iostream>
#include <cmath>
#include <boost/lexical_cast.hpp>
#include <boost/gil/typedefs.hpp>
#include <boost/gil/extension/io/png_io.hpp>
#include <boost/gil/extension/numeric/sampler.hpp>
#include <boost/gil/extension/numeric/resample.hpp>

int main(int argc, char** argv)
{
    namespace g = boost::gil;
    using boost::lexical_cast;
    using std::sqrt;
    using std::pow;

    if (argc <= 3)
        return -1;

    g::rgba8_image_t img;
    g::png_read_and_convert_image(argv[1], img);

    int i = sqrt(pow(static_cast<double>(img.width()), 2)
            + pow(static_cast<double>(img.height()), 2));
    g::rgba8_image_t img_rotated(80, 80);
    g::fill_pixels(view(img_rotated), g::rgba8_pixel_t(0, 0, 0, 0));
    double rad = lexical_cast<double>(std::string(argv[2])) * M_PI * 2 / 360;
    double scale = lexical_cast<double>(std::string(argv[3])) / 100;
    g::resample_pixels(g::const_view(img), g::view(img_rotated),
            g::matrix3x2<double>::get_translate(-80.0 / 2, -80.0 / 2) *
            g::matrix3x2<double>::get_rotate(rad) *
            g::matrix3x2<double>::get_scale(i / 80.0 / scale) *
            g::matrix3x2<double>::get_translate(img.width() / 2, img.height() / 2),
            g::nearest_neighbor_sampler());

    g::rgba8_view_t img_v(g::view(img_rotated));

    for (g::rgba8_view_t::y_coord_t y = 0; y < img_v.height(); y += 2) {
        for (g::rgba8_view_t::x_coord_t x = 0; x < img_v.width(); ++x) {
            g::gray8_pixel_t gu, gl;
            color_convert(img_v(x, y), gu);
            if (y + 1 < img_v.height())
                color_convert(img_v(x, y + 1), gl);
            std::cout << " .,`:;\"!||"[gl / 86 + (gu / 86) * 3];
        }
        std::cout << std::endl;
    }

    return 0;
}