import config, image, random

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)] == ' ' ? -127.5 : 127.5

Image *def get_test_image_wh(char const *filename, int w, h):
    Image *v = None
    if not filename: return v
    if strlen(filename) == 15 and not strncmp(filename, "random_", 7):
        uint32_t key = strtoll(filename + 7, None, 16)
        mersenne_seed(key)
        v = image_new(None, w, h, IT_FLOAT)
        for int i = 0; i < v->w * v->h; i++:
            v->data.v[i] = mersenne_rnd(-127.5, 127.5)
    if not strcmp(filename, "blank"):
        v = image_new(None, w, h, IT_FLOAT)
        for int i = 0; i < v->w * v->h; i++:
            v->data.v[i] = 0
    elif not strcmp(filename, "black"):
        v = image_new(None, w, h, IT_FLOAT)
        for int i = 0; i < v->w * v->h; i++:
            v->data.v[i] = -127.5
    elif not strcmp(filename, "noise"):
        v = image_new(None, w, h, IT_FLOAT)
        for int i = 0; i < v->w * v->h; i++:
            v->data.v[i] = 255 * ((double)rand() / (double)RAND_MAX) - 127.5
    elif not strcmp(filename, "vstripes1"):
        v = image_new(None, w, h, IT_FLOAT)
        char const *pattern[] = {
            "# # # # "
            "# # # # "
            "# # # # "
            "# # # # "
            "# # # # "
            "# # # # "
            "# # # # "
            "# # # # "}
        patternfill_v(v, pattern[0])
    elif not strcmp(filename, "vstripes2"):
        v = image_new(None, w, h, IT_FLOAT)
        char const *pattern[] = {
            "##  ##  "
            "##  ##  "
            "##  ##  "
            "##  ##  "
            "##  ##  "
            "##  ##  "
            "##  ##  "
            "##  ##  "}
        patternfill_v(v, pattern[0])
    elif not strcmp(filename, "check2"):
        v = image_new(None, w, h, IT_FLOAT)
        char const *pattern[] = {
            "##  ##  "
            "##  ##  "
            "  ##  ##"
            "  ##  ##"
            "##  ##  "
            "##  ##  "
            "  ##  ##"
            "  ##  ##"}
        patternfill_v(v, pattern[0])
    elif not strcmp(filename, "circle"):
        v = image_new(None, w, h, IT_FLOAT)
        double mx = v->w * 0.5
        double my = v->h * 0.5
        for int j = 0; j < v->h; j++:
            for int i = 0; i < v->w; i++:
                double dx = (i + 0.5) - mx
                double dy = (j + 0.5) - my
                double d = sqrt(dx * dx + dy * dy)
                d = ((int)d) & 255
                v->data.v[j * v->w + i] = d -127.5
    else:
        Image *image = image_load_b(filename)
        if image:
            v = BtoV(image)
            # Center around origin
            shift_multiply_image(v, -127.5, 1)

    return v

Image *def get_test_image(char const *filename):
    return get_test_image_wh(filename, 512, 64)
