Siv3Dでドロップした画像をグレイスケール化する【C++,Visual Studio】

前回の続きです

前回の記事

ayousanz.hatenadiary.jp

実行結果

f:id:ayousanz:20200903145323p:plain

操作?画面

f:id:ayousanz:20200903145348g:plain

グレイスケールにする

siv3d.github.io

画像をドロップして表示する

siv3d.github.io

Code

# include <Siv3D.hpp>
using namespace std;

void Main()
{
    Texture textureInput;
    Texture textureOutput;

    double scale = 1.0;

    Array<Rect> detectedFaces;
    const PixelShader ps(U"example/shader/2d/grayscale" SIV3D_SELECT_SHADER(U".hlsl", U".frag"),
        { { U"PSConstants2D", 0 } });

    if (!ps)
    {
        throw Error(U"Failed to load a shader file");
    }

    while (System::Update())
    {
         //ファイルがドロップされた
        if (DragDrop::HasNewFilePaths())
        {
            // ファイルを画像として読み込めた
            if (const Image image{ DragDrop::GetDroppedFilePaths().front().path })
            {
                // 画像の拡大縮小率
                scale = static_cast<double>(textureInput.width()) / image.width()/2;

                // 画面のサイズに合うように画像を拡大縮小
                textureInput = textureInput = Texture(image.fitted(Scene::Size()));
                textureOutput = textureOutput = Texture(image.fitted(Scene::Size()));
                
            }
        }
        if (KeyA.down()) {
            Print << U"拡大率:" << scale;
        }

        if (textureInput)
        {
            //textureOutput.draw(100,100);
            Rect(0, 0, 400, 400)(textureInput).draw();
            ScopedCustomShader2D shader(ps);
            //textureOutput.draw(400,400);
            Rect(400, 0, 400, 400)(textureOutput).draw();
        }
    }
}