
/** @file  psImage.c
 *
 *  @brief Contains basic image definitions and operations.
 *
 *  This file defines the basic type for an image struct and functions useful
 *  in manupulating images.
 *
 *  @author Robert DeSonia, MHPCC
 *  @author Ross Harman, MHPCC
 *
 *  @version $Revision: 1.50 $ $Name: not supported by cvs2svn $
 *  @date $Date: 2004-10-14 01:22:59 $
 *
 *  Copyright 2004 Maui High Performance Computing Center, University of Hawaii
 *
 *  That is the routine used to generate matrices.
 */

#include <string.h>
#include <math.h>

#include "psMemory.h"
#include "psError.h"
#include "psImage.h"

#include "psImageErrors.h"

static void imageFree(psImage* image);

psImage* psImageAlloc(unsigned int numCols,
                      unsigned int numRows,
                      const psElemType type)
{
    int area = 0;
    int elementSize = PSELEMTYPE_SIZEOF(type);  // element size in bytes
    int rowSize = numCols * elementSize;        // row size in bytes.

    area = numCols * numRows;

    if (area < 1) {
        psErrorMsg(PS_ERRORNAME_DOMAIN "psImageAlloc",
                   PS_ERR_BAD_PARAMETER_VALUE, true,
                   PS_ERRORTEXT_psImage_AREA_NEGATIVE,
                   numRows, numCols);
        return NULL;
    }

    psImage* image = (psImage* ) psAlloc(sizeof(psImage));

    p_psMemSetDeallocator(image, (psFreeFcn) imageFree);

    image->data.V = psAlloc(sizeof(void *) * numRows);

    image->rawDataBuffer = psAlloc(area * elementSize);

    // set the row pointers.
    image->data.V[0] = image->rawDataBuffer;
    for (int i = 1; i < numRows; i++) {
        image->data.V[i] = (void *)((int8_t *) image->data.V[i - 1] + rowSize);
    }

    *(int *)&image->col0 = 0;
    *(int *)&image->row0 = 0;
    *(unsigned int *)&image->numCols = numCols;
    *(unsigned int *)&image->numRows = numRows;
    *(psDimen* ) & image->type.dimen = PS_DIMEN_IMAGE;
    *(psElemType* ) & image->type.type = type;
    image->parent = NULL;
    image->children = NULL;

    return image;
}

static void imageFree(psImage* image)
{
    if (image == NULL) {
        return;
    }

    if (image->type.type == PS_TYPE_PTR) {
        // 2-D array of pointers -- must dereference elements
        unsigned int oldNumRows = image->numRows;
        unsigned int oldNumCols = image->numCols;
        psPTR* rowPtr;

        for (unsigned int row = 0; row < oldNumRows; row++) {
            rowPtr = image->data.PTR[row];
            for (unsigned int col = 0; col < oldNumCols; col++) {
                psMemDecrRefCounter(rowPtr[col]);
            }
        }
    }

    if (image->parent != NULL) {
        psArrayRemove(image->parent->children,image);
        image->parent = NULL;
    }

    psImageFreeChildren(image);

    psFree(image->rawDataBuffer);
    psFree(image->data.V);
}

psImage* psImageRecycle(psImage* old,
                        unsigned int numCols,
                        unsigned int numRows,
                        const psElemType type)
{
    int elementSize = PSELEMTYPE_SIZEOF(type);  // element size in bytes
    int rowSize = numCols * elementSize;        // row size in bytes.

    if (old == NULL) {
        old = psImageAlloc(numCols, numRows, type);
        return old;
    }

    if (old->type.dimen != PS_DIMEN_IMAGE) {
        psFree(old);
        psErrorMsg(PS_ERRORNAME_DOMAIN "psImageRecycle",
                   PS_ERR_BAD_PARAMETER_TYPE, true,
                   PS_ERRORTEXT_psImage_NOT_AN_IMAGE);
        return NULL;
    }

    if (old->type.type == PS_TYPE_PTR) {
        // 2-D array of pointers -- must
        // dereference
        unsigned int oldNumRows = old->numRows;
        unsigned int oldNumCols = old->numCols;
        psPTR* rowPtr;

        for (unsigned int row = 0; row < oldNumRows; row++) {
            rowPtr = old->data.PTR[row];
            for (unsigned int col = 0; col < oldNumCols; col++) {
                psMemDecrRefCounter(rowPtr[col]);
                rowPtr[col] = NULL;
            }
        }
    }

    /* image already the right size/type? */
    if (numCols == old->numCols && numRows == old->numRows &&
            type == old->type.type) {
        return old;
    }
    // Resize the image buffer
    old->rawDataBuffer = psRealloc(old->data.V[0],
                                   numCols * numRows * elementSize);
    old->data.V = (void **)psRealloc(old->data.V, numRows * sizeof(void *));

    // recreate the row pointers
    old->data.V[0] = old->rawDataBuffer;
    for (int i = 1; i < numRows; i++) {
        old->data.V[i] = (void *)((int8_t *) old->data.V[i - 1] + rowSize);
    }

    *(unsigned int *)&old->numCols = numCols;
    *(unsigned int *)&old->numRows = numRows;
    *(psElemType* ) & old->type.type = type;

    return old;
}

psImage* psImageCopy(psImage* restrict output,
                     const psImage* input,
                     psElemType type)
{
    psElemType inDatatype;
    int elementSize;
    int elements;
    int numRows;
    int numCols;

    if (input == NULL || input->data.V == NULL) {
        psErrorMsg(PS_ERRORNAME_DOMAIN "psImageCopy",
                   PS_ERR_BAD_PARAMETER_NULL, true,
                   PS_ERRORTEXT_psImage_IMAGE_NULL);
        psFree(output);
        return NULL;
    }

    if (input == output) {
        psErrorMsg(PS_ERRORNAME_DOMAIN "psImageCopy",
                   PS_ERR_BAD_PARAMETER_NULL, true,
                   PS_ERRORTEXT_psImage_INPLACE_NOTSUPPORTED);
        psFree(output);
        return NULL;
    }

    if (input->type.dimen != PS_DIMEN_IMAGE) {
        psErrorMsg(PS_ERRORNAME_DOMAIN "psImageCopy",
                   PS_ERR_BAD_PARAMETER_TYPE, true,
                   PS_ERRORTEXT_psImage_NOT_AN_IMAGE);
        psFree(output);
        return NULL;
    }

    inDatatype = input->type.type;
    numRows = input->numRows;
    numCols = input->numCols;
    elements = numRows * numCols;
    elementSize = PSELEMTYPE_SIZEOF(inDatatype);

    if (inDatatype == PS_TYPE_PTR || type == PS_TYPE_PTR) {
        psErrorMsg(PS_ERRORNAME_DOMAIN "psImageCopy",
                   PS_ERR_BAD_PARAMETER_TYPE, true,
                   PS_ERRORTEXT_psImage_IMAGE_TYPE_UNSUPPORTED,
                   PS_TYPE_PTR_NAME);
        psFree(output);
        return NULL;
    }

    output = psImageRecycle(output, numCols, numRows, type);

    // cover the trival case of copy of the same
    // datatype.
    if (type == inDatatype) {
        for (int row=0;row<numRows;row++) {
            memcpy(output->data.V[row], input->data.V[row], elementSize * numCols);
        }
        return output;
    }

    #define PSIMAGE_ELEMENT_COPY(IN,INTYPE,OUT,OUTTYPE,ELEMENTS) { \
        ps##INTYPE *in; \
        ps##OUTTYPE *out; \
        for(int row=0;row<numRows;row++) { \
            in = IN->data.INTYPE[row]; \
            out = OUT->data.OUTTYPE[row]; \
            for (int col=0;col<numCols;col++) { \
                *(out++) = *(in++); \
            } \
        } \
    }

    #define PSIMAGE_COPY_CASE(OUT,OUTTYPE) { \
        switch (inDatatype) { \
        case PS_TYPE_S8: \
            PSIMAGE_ELEMENT_COPY(input,S8,OUT,OUTTYPE,elements); \
            break; \
        case PS_TYPE_S16: \
            PSIMAGE_ELEMENT_COPY(input,S16,OUT,OUTTYPE,elements); \
            break; \
        case PS_TYPE_S32: \
            PSIMAGE_ELEMENT_COPY(input,S32,OUT,OUTTYPE,elements); \
            break; \
        case PS_TYPE_S64: \
            PSIMAGE_ELEMENT_COPY(input,S64,OUT,OUTTYPE,elements); \
            break; \
        case PS_TYPE_U8: \
            PSIMAGE_ELEMENT_COPY(input,U8,OUT,OUTTYPE,elements); \
            break; \
        case PS_TYPE_U16: \
            PSIMAGE_ELEMENT_COPY(input,U16,OUT,OUTTYPE,elements); \
            break; \
        case PS_TYPE_U32: \
            PSIMAGE_ELEMENT_COPY(input,U32,OUT,OUTTYPE,elements); \
            break; \
        case PS_TYPE_U64: \
            PSIMAGE_ELEMENT_COPY(input,U64,OUT,OUTTYPE,elements); \
            break; \
        case PS_TYPE_F32: \
            PSIMAGE_ELEMENT_COPY(input,F32,OUT,OUTTYPE,elements); \
            break; \
        case PS_TYPE_F64: \
            PSIMAGE_ELEMENT_COPY(input,F64,OUT,OUTTYPE,elements); \
            break; \
        case PS_TYPE_C32: \
            PSIMAGE_ELEMENT_COPY(input,C32,OUT,OUTTYPE,elements); \
            break; \
        case PS_TYPE_C64: \
            PSIMAGE_ELEMENT_COPY(input,C64,OUT,OUTTYPE,elements); \
            break; \
        default: \
            break; \
        } \
    }

    switch (type) {
    case PS_TYPE_S8:
        PSIMAGE_COPY_CASE(output, S8);
        break;
    case PS_TYPE_S16:
        PSIMAGE_COPY_CASE(output, S16);
        break;
    case PS_TYPE_S32:
        PSIMAGE_COPY_CASE(output, S32);
        break;
    case PS_TYPE_S64:
        PSIMAGE_COPY_CASE(output, S64);
        break;
    case PS_TYPE_U8:
        PSIMAGE_COPY_CASE(output, U8);
        break;
    case PS_TYPE_U16:
        PSIMAGE_COPY_CASE(output, U16);
        break;
    case PS_TYPE_U32:
        PSIMAGE_COPY_CASE(output, U32);
        break;
    case PS_TYPE_U64:
        PSIMAGE_COPY_CASE(output, U64);
        break;
    case PS_TYPE_F32:
        PSIMAGE_COPY_CASE(output, F32);
        break;
    case PS_TYPE_F64:
        PSIMAGE_COPY_CASE(output, F64);
        break;
    case PS_TYPE_C32:
        PSIMAGE_COPY_CASE(output, C32);
        break;
    case PS_TYPE_C64:
        PSIMAGE_COPY_CASE(output, C64);
        break;
    default: {
            char* typeStr;
            PS_TYPE_NAME(typeStr,type);
            psErrorMsg(PS_ERRORNAME_DOMAIN "psImageCopy",
                       PS_ERR_BAD_PARAMETER_TYPE, true,
                       PS_ERRORTEXT_psImage_IMAGE_TYPE_UNSUPPORTED,
                       typeStr);
            psFree(output);

            break;
        }
    }
    return output;
}

int psImageFreeChildren(psImage* image)
{
    int numFreed = 0;

    if (image == NULL) {
        return numFreed;
    }

    if (image->children != NULL) {
        psImage** children = (psImage**)image->children->data;
        numFreed = image->children->n;

        // orphan the children first
        // (so psFree doesn't try to modify the parent's children array while I'm using it)
        for (int i=0;i<numFreed;i++) {
            children[i]->parent = NULL;
        }

        psFree(image->children);
        image->children = NULL;
    }

    return numFreed;
}

/*****************************************************************************
p_psImagePixelInterpolation(image, x, y): this routine takes as input an
image and coordinates (x, y) and produces as output the corresponding pixel
value at the those coordinates.  For fractional corrdinates (x, y), 2-D
linear interpolation is performed on the image.
 *****************************************************************************/
psC64 psImagePixelInterpolate(const psImage* input,
                              float x,
                              float y,
                              const psImage* mask,
                              unsigned int maskVal,
                              psC64 unexposedValue,
                              psImageInterpolateMode mode)
{

    if (input == NULL) {
        psErrorMsg(PS_ERRORNAME_DOMAIN "psImagePixelInterpolate",
                   PS_ERR_BAD_PARAMETER_NULL,true,
                   PS_ERRORTEXT_psImage_IMAGE_NULL);
        return unexposedValue;
    }

    #define PSIMAGE_PIXEL_INTERPOLATE_CASE(TYPE)                     \
case PS_TYPE_##TYPE:                                                 \
    switch (mode) {                                                  \
    case PS_INTERPOLATE_FLAT:                                        \
        return p_psImagePixelInterpolateFLAT_##TYPE(                 \
                input,                                               \
                x,                                                   \
                y,                                                   \
                mask,                                                \
                maskVal,                                             \
                unexposedValue);                                     \
        break;                                                       \
    case PS_INTERPOLATE_BILINEAR:                                    \
        return p_psImagePixelInterpolateBILINEAR_##TYPE(             \
                input,                                               \
                x,                                                   \
                y,                                                   \
                mask,                                                \
                maskVal,                                             \
                unexposedValue);                                     \
        break;                                                       \
    default:                                                         \
        psErrorMsg(PS_ERRORNAME_DOMAIN "psImagePixelInterpolate",    \
                   PS_ERR_BAD_PARAMETER_VALUE,true,                  \
                   PS_ERRORTEXT_psImage_INTERPOLATE_METHOD_INVALID,  \
                   mode);                                            \
    }                                                                \
    break

    switch (input->type.type) {
        PSIMAGE_PIXEL_INTERPOLATE_CASE(U8);
        PSIMAGE_PIXEL_INTERPOLATE_CASE(U16);
        PSIMAGE_PIXEL_INTERPOLATE_CASE(U32);
        PSIMAGE_PIXEL_INTERPOLATE_CASE(U64);
        PSIMAGE_PIXEL_INTERPOLATE_CASE(S8);
        PSIMAGE_PIXEL_INTERPOLATE_CASE(S16);
        PSIMAGE_PIXEL_INTERPOLATE_CASE(S32);
        PSIMAGE_PIXEL_INTERPOLATE_CASE(S64);
        PSIMAGE_PIXEL_INTERPOLATE_CASE(F32);
        PSIMAGE_PIXEL_INTERPOLATE_CASE(F64);
        PSIMAGE_PIXEL_INTERPOLATE_CASE(C32);
        PSIMAGE_PIXEL_INTERPOLATE_CASE(C64);
    default: {
            char* typeStr;
            PS_TYPE_NAME(typeStr,input->type.type);
            psErrorMsg(PS_ERRORNAME_DOMAIN "psImagePixelInterpolate",
                       PS_ERR_BAD_PARAMETER_TYPE,true,
                       PS_ERRORTEXT_psImage_IMAGE_TYPE_UNSUPPORTED,
                       typeStr);
        }
    }

    return unexposedValue;
}

#define PSIMAGE_PIXEL_INTERPOLATE_FLAT(TYPE) \
inline psF64 p_psImagePixelInterpolateFLAT_##TYPE(const psImage* input, \
        float x, \
        float y, \
        const psImage* mask, \
        unsigned int maskVal, \
        psF64 unexposedValue) \
{ \
    int intX = (int) round((psF64)(x) - 0.5 + FLT_EPSILON); \
    int intY = (int) round((psF64)(y) - 0.5 + FLT_EPSILON); \
    int lastX = input->numCols - 1; \
    int lastY = input->numRows - 1; \
    \
    if ((intX < 0) || \
            (intX > lastX) || \
            (intY < 0) || \
            (intY > lastY) || \
            ( (mask!=NULL) && \
              ((mask->data.PS_TYPE_MASK_DATA[intY][intX] & maskVal) != 0) ) ) { \
        return unexposedValue; \
    } \
    \
    return input->data.TYPE[intY][intX]; \
}

#define PSIMAGE_PIXEL_INTERPOLATE_FLAT_COMPLEX(TYPE) \
inline psC64 p_psImagePixelInterpolateFLAT_##TYPE(const psImage* input, \
        float x, \
        float y, \
        const psImage* mask, \
        unsigned int maskVal, \
        psC64 unexposedValue) \
{ \
    int intX = (int) round((psF64)(x) - 0.5); \
    int intY = (int) round((psF64)(y) - 0.5); \
    int lastX = input->numCols - 1; \
    int lastY = input->numRows - 1; \
    \
    if ((intX < 0) || \
            (intX > lastX) || \
            (intY < 0) || \
            (intY > lastY) || \
            ( (mask!=NULL) && \
              ((mask->data.PS_TYPE_MASK_DATA[intY][intX] & maskVal) != 0) ) ) { \
        return unexposedValue; \
    } \
    \
    return input->data.TYPE[intY][intX]; \
}

PSIMAGE_PIXEL_INTERPOLATE_FLAT(U8)
PSIMAGE_PIXEL_INTERPOLATE_FLAT(U16)
PSIMAGE_PIXEL_INTERPOLATE_FLAT(U32)
PSIMAGE_PIXEL_INTERPOLATE_FLAT(U64)
PSIMAGE_PIXEL_INTERPOLATE_FLAT(S8)
PSIMAGE_PIXEL_INTERPOLATE_FLAT(S16)
PSIMAGE_PIXEL_INTERPOLATE_FLAT(S32)
PSIMAGE_PIXEL_INTERPOLATE_FLAT(S64)
PSIMAGE_PIXEL_INTERPOLATE_FLAT(F32)
PSIMAGE_PIXEL_INTERPOLATE_FLAT(F64)
PSIMAGE_PIXEL_INTERPOLATE_FLAT_COMPLEX(C32)
PSIMAGE_PIXEL_INTERPOLATE_FLAT_COMPLEX(C64)

#define PSIMAGE_PIXEL_INTERPOLATE_BILINEAR(TYPE) \
inline psF64 p_psImagePixelInterpolateBILINEAR_##TYPE(const psImage* input, \
        float x, \
        float y, \
        const psImage* mask, \
        unsigned int maskVal, \
        psF64 unexposedValue) \
{ \
    double floorX = floor((psF64)(x) - 0.5); \
    double floorY = floor((psF64)(y) - 0.5); \
    psF64 fracX = x - 0.5 - floorX; \
    psF64 fracY = y - 0.5 - floorY; \
    int intFloorX = (int) floorX; \
    int intFloorY = (int) floorY; \
    int lastX = input->numCols - 1; \
    int lastY = input->numRows - 1; \
    ps##TYPE V00; \
    ps##TYPE V01; \
    ps##TYPE V10; \
    ps##TYPE V11; \
    bool valid00 = false; \
    bool valid01 = false; \
    bool valid10 = false; \
    bool valid11 = false; \
    \
    if (intFloorY >= 0 && intFloorY <= lastY) { \
        if (intFloorX >= 0 && intFloorX <= lastX) { \
            V00 = input->data.TYPE[intFloorY][intFloorX]; \
            valid00 = (mask == NULL) || \
                      ((mask->data.PS_TYPE_MASK_DATA[intFloorY][intFloorX] & maskVal) == 0); \
        } \
        if (intFloorX >= -1 && intFloorX < lastX) { \
            V10 = input->data.TYPE[intFloorY][intFloorX+1]; \
            valid10 = (mask == NULL) || \
                      ((mask->data.PS_TYPE_MASK_DATA[intFloorY][intFloorX+1] & maskVal) == 0); \
        } \
    } \
    if (intFloorY >= -1 && intFloorY < lastY) { \
        if (intFloorX >= 0 && intFloorX <= lastX) { \
            V01 = input->data.TYPE[intFloorY+1][intFloorX]; \
            valid01 = (mask == NULL) || \
                      ((mask->data.PS_TYPE_MASK_DATA[intFloorY+1][intFloorX] & maskVal) == 0); \
        } \
        if (intFloorX >= -1 && intFloorX < lastX) { \
            V11 = input->data.TYPE[intFloorY+1][intFloorX+1]; \
            valid11 = (mask == NULL) || \
                      ((mask->data.PS_TYPE_MASK_DATA[intFloorY+1][intFloorX+1] & maskVal) == 0); \
        } \
    } \
    \
    /* cover likely case of all pixels being valid more efficiently */  \
    if (valid00 && valid10 && valid01 && valid11) { \
        /* formula from the ADD */ \
        psF64 rx = V00 + (V10-V00)*fracX; \
        return (rx + (V01 + (V11 - V01) * fracX - rx) * fracY); \
    } \
    \
    /* OK, at least one pixel is not valid - need to do it piecemeal */ \
    \
    psF64 V0; \
    bool valid0 = true; \
    if (valid00 && valid10) { \
        V0 = V00*(1-fracX)+V10*fracX; \
    } else if (valid00) { \
        V0 = V00; \
    } else if (valid10) { \
        V0 = V10; \
    } else { \
        valid0 = false; \
    } \
    \
    psF64 V1; \
    bool valid1 = true; \
    if (valid01 && valid11) { \
        V1 = V01*(1-fracX)+V11*fracX; \
    } else if (valid01) { \
        V1 = V01; \
    } else if (valid11) { \
        V1 = V11; \
    } else { \
        valid1 = false; \
    } \
    \
    if (valid0 && valid1) { \
        return ( V0*(1-fracY) + V1*fracY ); \
    } else if (valid0) { \
        return V0; \
    } else if (valid1) { \
        return V1; \
    } \
    \
    return unexposedValue; \
}

#define PSIMAGE_PIXEL_INTERPOLATE_BILINEAR_COMPLEX(TYPE) \
inline psC64 p_psImagePixelInterpolateBILINEAR_##TYPE(const psImage* input, \
        float x, \
        float y, \
        const psImage* mask, \
        unsigned int maskVal, \
        psC64 unexposedValue) \
{ \
    double floorX = floor((psF64)(x) - 0.5); \
    double floorY = floor((psF64)(y) - 0.5); \
    psF64 fracX = x - 0.5 - floorX; \
    psF64 fracY = y - 0.5 - floorY; \
    int intFloorX = (int) floorX; \
    int intFloorY = (int) floorY; \
    int lastX = input->numCols - 1; \
    int lastY = input->numRows - 1; \
    ps##TYPE V00; \
    ps##TYPE V01; \
    ps##TYPE V10; \
    ps##TYPE V11; \
    bool valid00 = false; \
    bool valid01 = false; \
    bool valid10 = false; \
    bool valid11 = false; \
    \
    if (intFloorY >= 0 && intFloorY <= lastY) { \
        if (intFloorX >= 0 && intFloorX <= lastX) { \
            V00 = input->data.TYPE[intFloorY][intFloorX]; \
            valid00 = (mask == NULL) || \
                      ((mask->data.PS_TYPE_MASK_DATA[intFloorY][intFloorX] & maskVal) == 0); \
        } \
        if (intFloorX >= -1 && intFloorX < lastX) { \
            V10 = input->data.TYPE[intFloorY][intFloorX+1]; \
            valid10 = (mask == NULL) || \
                      ((mask->data.PS_TYPE_MASK_DATA[intFloorY][intFloorX+1] & maskVal) == 0); \
        } \
    } \
    if (intFloorY >= -1 && intFloorY < lastY) { \
        if (intFloorX >= 0 && intFloorX <= lastX) { \
            V01 = input->data.TYPE[intFloorY+1][intFloorX]; \
            valid01 = (mask == NULL) || \
                      ((mask->data.PS_TYPE_MASK_DATA[intFloorY+1][intFloorX] & maskVal) == 0); \
        } \
        if (intFloorX >= -1 && intFloorX < lastX) { \
            V11 = input->data.TYPE[intFloorY+1][intFloorX+1]; \
            valid11 = (mask == NULL) || \
                      ((mask->data.PS_TYPE_MASK_DATA[intFloorY+1][intFloorX+1] & maskVal) == 0); \
        } \
    } \
    \
    /* cover likely case of all pixels being valid */  \
    if (valid00 && valid10 && valid01 && valid11) { \
        /* formula from the ADD */ \
        psC64 rx = V00 + (V10-V00)*fracX; \
        return (rx + (V01 + (V11 - V01) * fracX - rx) * fracY); \
    } \
    \
    /* OK, at least one pixel is not valid - need to do it piecemeal */ \
    \
    psC64 V0; \
    bool valid0 = true; \
    if (valid00 && valid10) { \
        V0 = V00*(1-fracX)+V10*fracX; \
    } else if (valid00) { \
        V0 = V00; \
    } else if (valid10) { \
        V0 = V10; \
    } else { \
        valid0 = false; \
    } \
    \
    psC64 V1; \
    bool valid1 = true; \
    if (valid01 && valid11) { \
        V0 = V01*(1-fracX)+V11*fracX; \
    } else if (valid01) { \
        V0 = V01; \
    } else if (valid11) { \
        V0 = V11; \
    } else { \
        valid1 = false; \
    } \
    \
    if (valid0 && valid1) { \
        return ( V0*(1-fracY) + V1*fracY ); \
    } else if (valid0) { \
        return V0; \
    } else if (valid1) { \
        return V1; \
    } \
    \
    return unexposedValue; \
}

PSIMAGE_PIXEL_INTERPOLATE_BILINEAR(U8)
PSIMAGE_PIXEL_INTERPOLATE_BILINEAR(U16)
PSIMAGE_PIXEL_INTERPOLATE_BILINEAR(U32)
PSIMAGE_PIXEL_INTERPOLATE_BILINEAR(U64)
PSIMAGE_PIXEL_INTERPOLATE_BILINEAR(S8)
PSIMAGE_PIXEL_INTERPOLATE_BILINEAR(S16)
PSIMAGE_PIXEL_INTERPOLATE_BILINEAR(S32)
PSIMAGE_PIXEL_INTERPOLATE_BILINEAR(S64)
PSIMAGE_PIXEL_INTERPOLATE_BILINEAR(F32)
PSIMAGE_PIXEL_INTERPOLATE_BILINEAR(F64)
PSIMAGE_PIXEL_INTERPOLATE_BILINEAR_COMPLEX(C32)
PSIMAGE_PIXEL_INTERPOLATE_BILINEAR_COMPLEX(C64)
