import config, wavelets/wptree, wavelets/daubechies_d4, wavelets/haar
import algo2/process

static def patternfill_v(Image *v, char const *pattern):
    for int j = 0; j < v->h; j++:
        for int i = 0; i < v->w; i++:
            v->data.v[j * v->w + i] = pattern[
                (j & 7) * 8 + (i & 7)] == ' ' ? 0 : 255

int def test_noisewp():
    Wavelet *wavelet = ALLOCATE0(sizeof *wavelet)
    wavelet->transform = daubechies_d4_transform_no_key
    #wavelet->transform = haar_transform
    WPNode *tree = wptree_create_pyramidal_tree(wavelet, 5, 512, 64, False)

    Image *v = image_new(None, 512, 64, IT_FLOAT)

    if 0: # noise
        for int i = 0; i < v->w * v->h; i++:
            v->data.v[i] = 255 * (double)rand() / (double)RAND_MAX
    if 0: # grid
        char const *pattern[] = {
            "# # # # "
            "# # # # "
            "# # # # "
            "# # # # "
            "# # # # "
            "# # # # "
            "# # # # "
            "# # # # "}
        patternfill_v(v, pattern[0])
    if 1: # grid
        char const *pattern[] = {
            "########"
            "#       "
            "# ##### "
            "# #   # "
            "# #   # "
            "# #   # "
            "# ##### "
            "#       "}
        patternfill_v(v, pattern[0])
    
    # 0 to 255 -> -127.5 to 127.5 (just for display effect)
    shift_multiply_image(v, -127.5, 1)
    Image *result = wptree_transform(tree, v)

    Image *r2 = image_copy(result)
    # 0 -> 127.5
    shift_multiply_image(r2, 127.5, 1)
    Image *b2 = VtoB(r2)
    save_tiff_luminance("randwp.tiff", b2)
    image_del(b2)

    Image *o2 = image_copy(v)
    shift_multiply_image(o2, 127.5, 1)
    Image *b3 = VtoB(o2)
    save_tiff_luminance("rand.tiff", b3)
    image_del(b3)

    printf("mean: %f\n", image_mean_area(v, 0, 0, v->w, v->h))
    printf("sdev: %f\n", sqrt(image_variance_area(v, 0, 0, v->w, v->h)))

    get_features_1(result)
    return 0