LoginSignup
7
9

More than 5 years have passed since last update.

FreeImageで画像の読み込みと保存

Last updated at Posted at 2016-01-30

C++で簡単に使える画像読み込みライブラリが欲しかったので。

Official site

対応フォーマット

http://freeimage.sourceforge.net/features.html のSUPPORTED FORMATを参照。有名どころは使える。

サンプル

#include<iostream>
#include<FreeImage.h>

int main() {
    std::cout << "FreeImage ver. " << FreeImage_GetVersion() << std::endl;
    std::cout << FreeImage_GetCopyrightMessage() << std::endl;

    try {
        // GIF画像を読み込み
        auto filename = "lena_color.gif";
        auto image    = std::shared_ptr<FIBITMAP>(
                            FreeImage_Load(FIF_GIF, filename, GIF_DEFAULT),FreeImage_Unload);
        if(!image) {
            throw std::runtime_error("Load failed");
        }

        // グレースケールに変換
        auto greyimg = std::shared_ptr<FIBITMAP>(
                            FreeImage_ConvertToGreyscale(image.get()),  FreeImage_Unload);
        if(!greyimg) {
            throw std::runtime_error("Convert failed");
        }

        // PNGとして保存
        if(!FreeImage_Save(FIF_PNG, greyimg.get(), "convert.png", PNG_DEFAULT)) {
            throw std::runtime_error("Save failed");
        }
    }
    catch(std::exception& e) {
        std::cout << "exception!\n" << e.what() << std::endl;
    }
}

ライブラリの初期化(FreeImage_Initialise)は動的リンクする場合は明示的な呼び出し不要。静的リンクの場合は一度だけ呼ばなければならない。FreeImage_DeInitializeも同様。

7
9
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
7
9