/**
 * TODO: improve seam interpolation
 *
 * Copyright © 2010-2011 Kirill Gavrilov <kirill@sview.ru>
 *
 * Distributed under the Boost Software License, Version 1.0.
 * See accompanying file license-boost.txt or copy at
 * http://www.boost.org/LICENSE_1_0.txt
 */

uniform sampler2D uTexture;
uniform vec4 uTexData;
uniform vec2 uTexSizePx;
uniform vec2 uTexelSize;

vec4 getColor(in vec2 texCoord) {
    vec2 txCoord_CC = floor(uTexSizePx * texCoord) / uTexSizePx;
    vec2 diff = (texCoord - txCoord_CC) * uTexSizePx;
    if(txCoord_CC.y >= (uTexData.w - uTexelSize.y)) {
        diff.y = 0.0;
    }
    vec2 diffSign = sign(diff);
    vec2 diffAbs = abs(diff);
    vec2 txCoord_R = txCoord_CC + diffSign * vec2(uTexelSize.x, 0.0);
    if(txCoord_R.x > (uTexData.z - uTexelSize.x)) {
        txCoord_R.x = uTexelSize.x;
    } else if(txCoord_R.x < uTexelSize.x) {
        txCoord_R.x = uTexData.z - uTexelSize.x;
    }
    vec4 color_CC = texture2D(uTexture, txCoord_CC);
    vec4 colorXX1 = mix(color_CC,
                        texture2D(uTexture,
                                  txCoord_R),
                        diffAbs.x);
    vec4 colorXX2 = mix(texture2D(uTexture,
                                  txCoord_CC + diffSign * vec2(0.0, uTexelSize.y)),
                        texture2D(uTexture,
                                  txCoord_R + diffSign * vec2(0.0, uTexelSize.y)),
                        diffAbs.x);
    return mix(colorXX1, colorXX2, diffAbs.y);
}
