/*
*  This program was created by AXELL CORPORATION.
*  Copyright (C) 2017 AXELL CORPORATION, all rights reserved.
*/
#include "gfx_sample.h"

/* VGImageオブジェクト */
static VGImage image1, image2;

static void *image_data1, *image_data2;

static void TestInit(int width, int height)
{
    VGfloat ClearColor[4]  = {0.5F, 0.5F, 0.5F, 0.5F};

    /* 画像を読込 */
    image_data1 = GfxGetFreeArea(BLEND_WIDTH, BLEND_HEIGHT, BLEND_BPP<<3);
    GfxFsRead((int8_t*)BLEND_NAME1, BLEND_STRIDE * BLEND_HEIGHT, (void*)image_data1);
    image_data2 = GfxGetFreeArea(BLEND_WIDTH, BLEND_HEIGHT, BLEND_BPP<<3);
    GfxFsRead((int8_t*)BLEND_NAME2, BLEND_STRIDE * BLEND_HEIGHT, (void*)image_data2);

    /* 描画サーフェスのクリアカラーを指定 */
    vgSetfv(VG_CLEAR_COLOR, 4, ClearColor);

    /* 描画サーフェス全体をクリア */
    vgClear(0, 0, width, height);

    /* VGImageオブジェクトの生成 (Dst) */
    image1 = vgCreateImage(VG_sRGBA_8888, BLEND_WIDTH, BLEND_HEIGHT, VG_IMAGE_QUALITY_NONANTIALIASED);

    /* VGImageオブジェクトにイメージデータを登録 */
    vgImageSubData(image1, image_data1, BLEND_STRIDE, VG_sRGBA_8888, 0, 0, BLEND_WIDTH, BLEND_HEIGHT);

    /* VGImageオブジェクトの生成 (Src) */
    image2 = vgCreateImage(VG_sRGBA_8888, BLEND_WIDTH, BLEND_HEIGHT, VG_IMAGE_QUALITY_NONANTIALIASED);

    /* VGImageオブジェクトにイメージデータを登録 */
    vgImageSubData(image2, image_data2, BLEND_STRIDE, VG_sRGBA_8888, 0, 0, BLEND_WIDTH, BLEND_HEIGHT);

    /* ポイントサンプル */
    vgSeti(VG_IMAGE_QUALITY, VG_IMAGE_QUALITY_NONANTIALIASED);

    /* IMAGE_USER_TO_SURFACEを単位行列に設定 */
    vgSeti(VG_MATRIX_MODE, VG_MATRIX_IMAGE_USER_TO_SURFACE);
    vgLoadIdentity();
}

static void DrawWithBlend(void)
{
    /* SRC（ブレンド無効） */
    vgTranslate(15, 30);
    vgSeti(VG_BLEND_MODE, VG_BLEND_SRC);
    vgDrawImage(image1);
    vgSeti(VG_BLEND_MODE, VG_BLEND_SRC);
    vgDrawImage(image2);

    /* SRC_OVER */
    vgTranslate(160, 0);
    vgSeti(VG_BLEND_MODE, VG_BLEND_SRC);
    vgDrawImage(image1);
    vgSeti(VG_BLEND_MODE, VG_BLEND_SRC_OVER);
    vgDrawImage(image2);

    /* DST_OVER */
    vgTranslate(160, 0);
    vgSeti(VG_BLEND_MODE, VG_BLEND_SRC);
    vgDrawImage(image1);
    vgSeti(VG_BLEND_MODE, VG_BLEND_DST_OVER);
    vgDrawImage(image2);

    /* SRC_IN */
    vgTranslate(160, 0);
    vgSeti(VG_BLEND_MODE, VG_BLEND_SRC);
    vgDrawImage(image1);
    vgSeti(VG_BLEND_MODE, VG_BLEND_SRC_IN);
    vgDrawImage(image2);

    /* DST_IN */
    vgTranslate(-480, 150);
    vgSeti(VG_BLEND_MODE, VG_BLEND_SRC);
    vgDrawImage(image1);
    vgSeti(VG_BLEND_MODE, VG_BLEND_DST_IN);
    vgDrawImage(image2);

    /* MULTIPLY */
    vgTranslate(160, 0);
    vgSeti(VG_BLEND_MODE, VG_BLEND_SRC);
    vgDrawImage(image1);
    vgSeti(VG_BLEND_MODE, VG_BLEND_MULTIPLY);
    vgDrawImage(image2);

    /* SCREEN */
    vgTranslate(160, 0);
    vgSeti(VG_BLEND_MODE, VG_BLEND_SRC);
    vgDrawImage(image1);
    vgSeti(VG_BLEND_MODE, VG_BLEND_SCREEN);
    vgDrawImage(image2);

    /* DARKEN */
    vgTranslate(160, 0);
    vgSeti(VG_BLEND_MODE, VG_BLEND_SRC);
    vgDrawImage(image1);
    vgSeti(VG_BLEND_MODE, VG_BLEND_DARKEN);
    vgDrawImage(image2);

    /* LIGHTEN */
    vgTranslate(-480, 150);
    vgSeti(VG_BLEND_MODE, VG_BLEND_SRC);
    vgDrawImage(image1);
    vgSeti(VG_BLEND_MODE, VG_BLEND_LIGHTEN);
    vgDrawImage(image2);

    /* ADDITIVE */
    vgTranslate(160, 0);
    vgSeti(VG_BLEND_MODE, VG_BLEND_SRC);
    vgDrawImage(image1);
    vgSeti(VG_BLEND_MODE, VG_BLEND_ADDITIVE);
    vgDrawImage(image2);

    /* VG_BLEND_MODEをデフォルトに設定 */
    vgSeti(VG_BLEND_MODE, VG_BLEND_SRC_OVER);
}

static void TestTerm(void)
{
    GfxReleaseFreeArea(image_data1);
    GfxReleaseFreeArea(image_data2);

    /* オブジェクトの破棄 */
    vgDestroyImage(image1);
    vgDestroyImage(image2);
}

void BlendMain(void)
{
    /* 初期化処理 */
    TestInit(GFX_WIDTH, GFX_HEIGHT);

    /* ブレンド処理 */
    DrawWithBlend();

    /* バッファスワップ */
    GfxSwapBuffer();

    /* 終了処理 */
    TestTerm();
}

