import config, wavelets/wptree, wavelets/daubechies_d4, wavelets/haar
import wavelets/mallat_qsw, wavelets/zou
import util
import tests/testimage

int def test_wp(char const *filename, char const *what):
    Wavelet *wavelet = ALLOCATE0(sizeof *wavelet)
    char const *algo = "haar"
    char const *key = None
    if hasword(what, "haar"):
        wavelet->transform = haar_transform_no_key
    elif hasword(what, "d4"):
        wavelet->transform = daubechies_d4_transform_no_key
        algo = "d4"
    elif hasword(what, "zou"):
        wavelet->transform = zou_transform
        for int i = 0; i < (int)strlen(what); i++:
            if not strncmp(what + i, "key", 3):
                int j
                for j = 0; ; j++:
                    char c = what[i + 3 + j]
                    if not c: break
                    if c == ' ': break
                char k[j + 1]
                strncpy(k, what + i + 3, j)
                k[j]= '\0'
                wavelet->key = strdup(k)
                key = wavelet->key
        algo = "zou"
    elif hasword(what, "qsw"):
        wavelet->transform = qsw_transform_no_key
        algo = "qsw"
    else:
        wavelet->transform = haar_transform_no_key

    WPNode *tree = wptree_create_pyramidal_tree(wavelet, 5, 512, 64, False)

    if not filename:
        fprintf(stderr, "Error: No filename!\n")
        return -1

    printf("%s %s %s\n", filename, algo, key)

    Image *v = get_test_image(filename)

    char name[MAX_PATH] = ""
    if v->name:
        get_image_name_algo_key(v, "wp", algo, key, name)

    if not name[0]:
        if key and key[0]: sprintf(name, "%s_wp_%s_%s.tiff", filename, algo, key)
        else: sprintf(name, "%s_wp_%s.tiff", filename, algo)

    Image *result = wptree_transform(tree, v)

    Image *r2 = image_copy(result)
    shift_multiply_image(r2, 127.5, 1)
    Image *b = VtoB(r2)
    save_tiff_luminance(name, b)

    return 0
