Boost.GILで遊んでみる
遊んでいるというか、遊んではいないんだけど。
PNG画像を読み込んでアスキーアートぽく出力するサンプル。
#include <iostream> #include <boost/gil/typedefs.hpp> #include <boost/gil/extension/io/png_io.hpp> int main(int argc, char** argv) { namespace g = boost::gil; if (argc <= 1) return -1; g::rgba8_image_t img; g::png_read_and_convert_image(argv[1], img); int scale = (img.width() + 79) / 80; g::dynamic_xy_step_type<g::rgba8_image_t::view_t>::type img_v( g::subsampled_view(g::view(img), scale, scale)); 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; }
こんな Makefile で。
CXX=g++ LD=g++ CPPFLAGS=$(shell pkg-config --cflags libpng) -I/usr/include/boost_1_35 LDFLAGS=$(shell pkg-config --libs libpng) CXXFLAGS= all: test .cpp.o: $(CXX) $(CPPFLAGS) $(CXXFLAGS) $(CXXFLAGS) -c -o $@ $< test.o: test.cpp test: test.o $(LD) $(LDFLAGS) -o $@ $< .PHONY: test
んで、出力例。
moriyoshi ~/Sources/test/gil% ./test boost.png |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||| |||||||||||||||||||||||::||||||||||||||||||||||||||||||||||||::||||||| ||||||||||||||||||||||::!!!!||||||!!!!||||||!!!!||||!!!!!!|!::!!|||||| ||||||||||||||||||||||::;;;::!|!::;;;::!|!::;;;::!!:;;;;;;|;::;;|||||| |||||||!!||||||||||||!::|||!:;!::||||::;!::||||::;|;:::::|||::|||||||| ||||||!:::!||||||||||::!!!!:;|;::!|!!:;|;::!|!!:;|!!!!!::||::!!||||||| ||||||;::::||||||||||;;;;;;|||||;;;;;|||||;;;;;|||;;;;;;||||;;;||||||| |||||||||||||||||||||||||||||||||||||!||||||!|||!||||||||!||||!||||||| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
比較的楽しく書けるのはいいんだけど、typedef ***_t という名前づけが気にかかるなあ。