install ruby-vips(Ruby画像処理ライブラリいろいろ入れてみる その1)

はじめに

いま画像処理(といっても切ったり縮めたりですが)にRMagickつまりImageMagickを使っているわけですが、若干遅い。ということで他のやり方もあるだろう多分、と調べたところ、候補に挙がったのがこの3つ。ひとまず全部入れてみる。

VIPS?

VIPS is a free image processing system. It is good with large images (images larger than the amount of RAM you have available), with many CPUs (see Benchmarks for examples of SMP scaling, VIPS is also part of the PARSEC suite), for working with colour, for scientific analysis and for general research & development. As well as JPEG, TIFF and PNG images, it also supports scientific formats like FITS, Matlab, Analyze, PFM, Radiance and OpenSlide.
http://www.vips.ecs.soton.ac.uk/index.php?title=VIPS

いちおうベンチマークではImageMagickより格段に速いしメモリも喰わない。
i5-3210M CPU @ 2.50GHz (Dell Vostro laptop)

Software Run time (secs real) Memory (peak RSS MB) Times slower
VIPS C++ 7.32 0.33 10 1.0
ruby-vips 7.32 0.37 12 1.12
OpenCV 2.3.1-11ubuntu2 1.40 185 4.24
RMagick 2.13.2 (ImageMagick 6.7.7.10-2ubuntu4) 1.98 670 6.00
ImageMagick 6.7.7.10-2ubuntu4 2.46 480 7.45

http://www.vips.ecs.soton.ac.uk/index.php?title=Speed_and_Memory_Use
(あ、OpenCVって速くないな…まぁいいや。)
あと、VIPSのRubyラッパとしてはinsano_image_resizerというのがあるようです。
GitHub - populr/insano_image_resizer: A high-speed image resizing gem based on VIPS
ただ、今回は私の用途と若干合わないので略します。

インストール

mini:~ admin$ brew search vips
homebrew/science/vips
mini:~ admin$ brew install vips
Error: No available formula for vips 

おや?

VIPS is on homebrew. Install homebrew and type:

$ brew tap homebrew/science
$ brew install vips

http://www.vips.ecs.soton.ac.uk/index.php?title=Build_on_OS_X

なるほどhomebrew tapというのをするわけですね。

このコマンドを使う事で、公式以外でフォーミュラを公開していたリポジトリをhomebrewの中に取り込むことができ、$ brewコマンドで扱えるようになります。
今までhomebrewにはなかったvimApachephpといったフォーミュラを簡単に追加できるようになりました。また、自分のgithubアカウントにアップしているフォーミュラがある場合は、それも取り込むことができます。
これは便利!Homebrewに追加されたtapコマンドはリポジトリを追加して簡単にフォーミュラを増やせる | Macとかの雑記帳

リポジトリの追加、か。ということはhomebrew/scienceにあるものはもうErrorじゃなく問題なくインストール出来るわけだ。
ということでやりなおし。
まずリポジトリ追加。

mini:~ admin$ brew tap homebrew/science
Cloning into '/usr/local/Library/Taps/homebrew-science'...
remote: Counting objects: 1870, done.
remote: Compressing objects: 100% (1064/1064), done.
remote: Total 1870 (delta 910), reused 1679 (delta 787)
Receiving objects: 100% (1870/1870), 391.61 KiB | 212 KiB/s, done.
Resolving deltas: 100% (910/910), done.
Tapped 134 formula

VIPSインストール。

mini:~ admin$ brew install vips
==> Installing vips dependency: gettext
==> Downloading https://downloads.sf.net/project/machomebrew/Bottles/gettext-0.1
######################################################################## 100.0%
==> Pouring gettext-0.18.2.lion.bottle.tar.gz
==> Caveats
This formula is keg-only: so it was not symlinked into /usr/local.

OS X provides the BSD gettext library and some software gets confused if both are in the library path.

Generally there are no consequences of this for you. If you build your
own software and it requires this formula, you'll need to add to your
build variables:

    LDFLAGS:  -L/usr/local/opt/gettext/lib
    CPPFLAGS: -I/usr/local/opt/gettext/include

==> Summary
🍺  /usr/local/Cellar/gettext/0.18.2: 366 files, 11M
==> Installing vips dependency: xz
==> Downloading http://tukaani.org/xz/xz-5.0.4.tar.bz2
######################################################################## 100.0%
==> ./configure --prefix=/usr/local/Cellar/xz/5.0.4
==> make install
🍺  /usr/local/Cellar/xz/5.0.4: 58 files, 1.5M, built in 27 seconds
==> Installing vips dependency: libffi
==> Downloading http://mirrors.kernel.org/sources.redhat.com/libffi/libffi-3.0.1
######################################################################## 100.0%
==> ./configure --prefix=/usr/local/Cellar/libffi/3.0.13
==> make install
==> Caveats
This formula is keg-only: so it was not symlinked into /usr/local.

Mac OS X already provides this software and installing another version in
parallel can cause all kinds of trouble.

Some formulae require a newer version of libffi.

Generally there are no consequences of this for you. If you build your
own software and it requires this formula, you'll need to add to your
build variables:

    LDFLAGS:  -L/usr/local/opt/libffi/lib

==> Summary
🍺  /usr/local/Cellar/libffi/3.0.13: 13 files, 376K, built in 11 seconds
==> Installing vips dependency: glib
==> Downloading http://ftp.gnome.org/pub/gnome/sources/glib/2.36/glib-2.36.2.tar
######################################################################## 100.0%
==> Downloading patches
######################################################################## 100.0%
==> Patching
patching file aclocal.m4
Hunk #1 succeeded at 1526 (offset -10 lines).
patching file config.h.in
patching file configure
Hunk #2 succeeded at 27381 (offset 72 lines).
patching file configure.ac
patching file gio/gdbusprivate.c
patching file gio/xdgmime/xdgmime.c
==> ./configure --disable-maintainer-mode --disable-dtrace --disable-modular-tes
==> make
==> make install
🍺  /usr/local/Cellar/glib/2.36.2: 410 files, 16M, built in 115 seconds
==> Installing vips
==> Downloading http://www.vips.ecs.soton.ac.uk/supported/7.32/vips-7.32.1.tar.g
######################################################################## 100.0%
==> ./configure --prefix=/usr/local/Cellar/vips/7.32.1
==> make install
🍺  /usr/local/Cellar/vips/7.32.1: 185 files, 11M, built in 112 seconds

VIPS入った。

mini:~ admin$ which vips
/usr/local/bin/vips
mini:~ admin$ vips --version
vips-7.32.1-Wed May 29 **:03:08 JST 2013

でも

いま入れたのは

This will give you a very bare-bones vips, missing things like imagemagick loading, openslide support, FFTs, and so on.
http://www.vips.ecs.soton.ac.uk/index.php?title=Build_on_OS_X

だそうです。ちゃんと見てませんでしたorz
ので一旦消します。

mini:~ admin$ brew uninstall vips
Uninstalling /usr/local/Cellar/vips/7.32.1...

完全版というかfull-fat versionを

mini:~ admin$ brew info vips
vips: stable 7.32.1
http://www.vips.ecs.soton.ac.uk/
/usr/local/Cellar/vips/7.32.1 (185 files, 11M) *
  Built from source with: --with-openslide, --with-libexif, --with-openexr, --with-libtiff, --with-fftw, --with-cfitsio, --with-pango, --with-liboil, --with-little-cms, --with-imagemagick
https://github.com/homebrew/homebrew-science/commits/master/vips.rb
==> Dependencies
Build: pkg-config
Required: gettext, glib, jpeg
Optional: openslide, libtiff, imagemagick, fftw, little-cms, pango, libexif, liboil, openexr, cfitsio
==> Options
--with-cfitsio
	Build with cfitsio support
--with-fftw
	Build with fftw support
--with-imagemagick
	Build with imagemagick support
--with-libexif
	Build with libexif support
--with-liboil
	Build with liboil support
--with-libtiff
	Build with libtiff support
--with-little-cms
	Build with little-cms support
--with-openexr
	Build with openexr support
--with-openslide
	Build with openslide support
--with-pango
	Build with pango support

For a full-fat version, try:

$ brew install vips --with-cfitsio --with-fftw --with-imagemagick \
    --with-libexif --with-liboil --with-libtiff --with-little-cms \
    --with-openexr --with-openslide --with-pango

http://www.vips.ecs.soton.ac.uk/index.php?title=Build_on_OS_X

では実作業。

hako:~ admin$ brew install vips --with-cfitsio --with-fftw --with-imagemagick \
>     --with-libexif --with-liboil --with-libtiff --with-little-cms \
>     --with-openexr --with-openslide --with-pango
==> Installing vips dependency: libxml2
(後略)[Bash] brew install vips (full-fat version) - Pastebin.com

ruby-vipsインストール(失敗してるので読み飛ばしてください)

$ gem install ruby-vips
Building native extensions.  This could take a while...
ERROR:  Error installing ruby-vips:
	ERROR: Failed to build gem native extension.

        /Users/hoge/.rbenv/versions/1.9.3-p392/bin/ruby extconf.rb
*** extconf.rb failed ***
Could not create Makefile due to some reason, probably lack of
necessary libraries and/or headers.  Check the mkmf.log file for more
details.  You may need configuration options.

Provided configuration options:
	--with-opt-dir
	--without-opt-dir
	--with-opt-include
	--without-opt-include=${opt-dir}/include
	--with-opt-lib
	--without-opt-lib=${opt-dir}/lib
	--with-make-prog
	--without-make-prog
	--srcdir=.
	--curdir
	--ruby=/Users/hoge/.rbenv/versions/1.9.3-p392/bin/ruby
	--enable-debug
	--disable-debug
	--with-vips-config
	--without-vips-config
	--with-pkg-config
	--without-pkg-config
	--with-vips-7.29-config
	--without-vips-7.29-config
	--with-vips-7.28-config
	--without-vips-7.28-config
	--with-vips-7.27-config
	--without-vips-7.27-config
	--with-vips-7.26-config
	--without-vips-7.26-config
	--with-vips-7.24-config
	--without-vips-7.24-config
	--with-vips-7.23-config
	--without-vips-7.23-config
	--with-vips-7.22-config
	--without-vips-7.22-config
	--with-vips-7.20-config
	--without-vips-7.20-config
extconf.rb:27:in `<main>': no pkg_config for any of following libvips versions: 7.29, 7.28, 7.27, 7.26, 7.24, 7.23, 7.22, 7.20 (RuntimeError)


Gem files will remain installed in /Users/hoge/.rbenv/versions/1.9.3-p392/lib/ruby/gems/1.9.1/gems/ruby-vips-0.3.5 for inspection.
Results logged to /Users/hoge/.rbenv/versions/1.9.3-p392/lib/ruby/gems/1.9.1/gems/ruby-vips-0.3.5/ext/gem_make.out

エラー…。
このgemが古い(3年前)ということもあるのでしょうか。これ勘違い。githubの古いものを見ていた。

If you want to build things outside homebrew which depend on vips, such as ruby-vips, you will need to get your pkg-config working. You need to point pkg-config at the homebrew area, and, additionally, at homebrew's libxml2 area:
In your .profile, add something like:

export PKG_CONFIG_PATH=/usr/local/lib/pkgconfig:/usr/local/opt/libxml2/lib/pkgconfig

http://www.vips.ecs.soton.ac.uk/index.php?title=Build_on_OS_X

これを参考にして~/.bashrcに

export PKG_CONFIG_PATH=/usr/local/lib/pkgconfig:/usr/local/opt/libxml2/lib/pkgconfig:/usr/local/sbin:/usr/local/bin

を追加してから実行。

$ gem install ruby-vips
Building native extensions.  This could take a while...
ERROR:  Error installing ruby-vips:
	ERROR: Failed to build gem native extension.

        

Gem files will remain installed in /Users/hoge/.rbenv/versions/1.9.3-p392/lib/ruby/gems/1.9.1/gems/ruby-vips-0.3.5 for inspection.
Results logged to /Users/hoge/.rbenv/versions/1.9.3-p392/lib/ruby/gems/1.9.1/gems/ruby-vips-0.3.5/ext/gem_make.out

エラーは減ったのだけどgem_make.outが空だから原因が分からない…。

PKG_CONFIG_PATHを替えてみる

export PKG_CONFIG_PATH=/usr/local/lib/pkgconfig:/usr/lib/pkgconfig:/usr/X11/lib/pkgconfig
VIPS 7.30 support? · Issue #24 · libvips/ruby-vips · GitHub

libvips relies on pkg-config, so it likes to be aware of every possible pkg-config path you have. Unfortunately, $PKG_CONFIG_PATH variable has almost never been seen to be set automatically on a bunch of Linux systems and Mac OS X. Workaround here is to add the following line into $/.bash_profile:

export PKG_CONFIG_PATH=/usr/local/lib/pkgconfig:/usr/lib/pkgconfig:/usr/X11/lib/pkgconfig

This hack works perfect, though any solutions from more sysadmin-skilled people would be appreciated.
Installation on various platforms · libvips/ruby-vips Wiki · GitHub

という記載を発見。
これに従ってみる。

$ export PKG_CONFIG_PATH=/usr/local/lib/pkgconfig:/usr/lib/pkgconfig:/usr/X11/lib/pkgconfig
$ gem install ruby-vips
Building native extensions.  This could take a while...
Successfully installed ruby-vips-0.3.5
1 gem installed
Installing ri documentation for ruby-vips-0.3.5...
Building YARD (yri) index for ruby-vips-0.3.5...
Installing RDoc documentation for ruby-vips-0.3.5...

あっさり成功w

ということで.bashrcに

export PKG_CONFIG_PATH=/usr/local/lib/pkgconfig:/usr/lib/pkgconfig:/usr/X11/lib/pkgconfig

を書きました。

libvipsを入れる(ここ以降はオマケになりました)

やはりこれを入れないとまずいのか、と思い直してlibvipsをgit cloneする。

mini:~/git admin$ git clone https://github.com/jcupitt/libvips.git
Cloning into 'libvips'...
remote: Counting objects: 22224, done.
remote: Compressing objects: 100% (9343/9343), done.
remote: Total 22224 (delta 13399), reused 21553 (delta 12731)
Receiving objects: 100% (22224/22224), 20.93 MiB | 1.05 MiB/s, done.
Resolving deltas: 100% (13399/13399), done.

手順に従って./bootstrap.sh。

mini:~/git/libvips admin$ ./bootstrap.sh 
./bootstrap.sh: line 20: aclocal: command not found
cp: /codeset.m4: No such file or directory
cp: /gettext.m4: No such file or directory
cp: /glibc21.m4: No such file or directory
cp: /iconv.m4: No such file or directory
cp: /isc-posix.m4: No such file or directory
cp: /lcmessage.m4: No such file or directory
cp: /progtest.m4: No such file or directory
cp: /introspection.m4: No such file or directory
./bootstrap.sh: line 39: gtkdocize: command not found

Tesseract won't install with Xcode 4.3 due to missing aclocal · Issue #10423 · Homebrew/legacy-homebrew · GitHub
に従ってaclocalのためにlibtoolを…

mini:~/git/libvips admin$ brew install libtool
Warning: libtool-2.4.2 already installed

…あるやん。

mini:~/git/libvips admin$ which libtool
/usr/bin/libtool
mini:~/git/libvips admin$ which autoconf
/usr/local/bin/autoconf
mini:~/git/libvips admin$ which automake

ということでautomakeいれる。

mini:~/git/libvips admin$ brew install automake
==> Downloading http://ftpmirror.gnu.org/automake/automake-1.13.2.tar.gz
######################################################################## 100.0%
==> ./configure --prefix=/usr/local/Cellar/automake/1.13.2
==> make install
🍺  /usr/local/Cellar/automake/1.13.2: 126 files, 2.3M, built in 8 seconds

やり直し。

mini:~/git/libvips admin$ ./bootstrap.sh 
cp: /usr/local/share/aclocal/codeset.m4: No such file or directory
cp: /usr/local/share/aclocal/gettext.m4: No such file or directory
cp: /usr/local/share/aclocal/glibc21.m4: No such file or directory
cp: /usr/local/share/aclocal/iconv.m4: No such file or directory
cp: /usr/local/share/aclocal/isc-posix.m4: No such file or directory
cp: /usr/local/share/aclocal/lcmessage.m4: No such file or directory
cp: /usr/local/share/aclocal/progtest.m4: No such file or directory
cp: /usr/local/share/aclocal/introspection.m4: No such file or directory
./bootstrap.sh: line 39: gtkdocize: command not found

gtkdocizeを用意することを考えてみよう。
(続く→homebrewでgtk-docを入れる - 別館 子子子子子子(ねこのここねこ)