/* This file was generated by scramble.py. */
#line 1 "src/bumpmap.py"
#include "bumpmap.h"
#line 2
#include "rgba.h"
#line 12
Bumpmap * bumpmap_create(int w, int h) {
    Bumpmap * self;
#line 13
    land_alloc(self);
    self->w = w;
    self->h = h;
    self->bumpmap = land_calloc(w * h * sizeof (* self->bumpmap));
    self->lightmap = land_calloc(w * h * sizeof (* self->lightmap));
    return self;
}
void bumpmap_light(Bumpmap * self) {
    int w = self->w;
    int h = self->h;
    for (int y = 0; y < h; y += 1) {
        for (int x = 0; x < w; x += 1) {
            float a1 = rgba_get(self->bumpmap, x + 1, y, w, h);
            float a2 = rgba_get(self->bumpmap, x - 1, y, w, h);
            float a3 = rgba_get(self->bumpmap, x, y + 1, w, h);
            float a4 = rgba_get(self->bumpmap, x, y - 1, w, h);
#line 30
            self->lightmap [x + y * w] = a2 + a4 - a1 - a3;
        }
    }
}
#line 32
void bumpmap_nolight(Bumpmap * self) {
    int w = self->w;
    int h = self->h;
    for (int y = 0; y < h; y += 1) {
        for (int x = 0; x < w; x += 1) {
#line 38
            self->lightmap [x + y * w] = rgba_get(self->bumpmap, x, y, w, h);
        }
    }
}
#line 40
void bumpmap_outline(Bumpmap * self) {
    int w = self->w;
    int h = self->h;
    for (int y = 0; y < h; y += 1) {
        for (int x = 0; x < w; x += 1) {
#line 46
            float v = rgba_get(self->bumpmap, x, y, w, h);
            v = 1 - v * v;
            self->bumpmap [x + y * w] = v;
        }
    }
}
#line 50
void bumpmap_sphere(Bumpmap * self, float cx, float cy, float r) {
    int w = self->w;
    int h = self->h;
    for (int y = 0; y < h; y += 1) {
        for (int x = 0; x < w; x += 1) {
            float px = x - cx;
            float py = y - cy;
            float zz = r * r - px * px - py * py;
            if (zz < 0) {
                continue;
            }
#line 60
            float z = sqrt(zz);
            self->bumpmap [x + w * y] -= z;
        }
    }
}
#line 63
void bumpmap_perlin(Bumpmap * self, float xs, float ys) {
    int w = self->w;
    int h = self->h;
    Perlin * noise = perlin_create(w / xs, h / ys);
    for (int y = 0; y < h; y += 1) {
        for (int x = 0; x < w; x += 1) {
            self->bumpmap [x + w * y] = perlin_at(noise, x / xs, y / ys);
        }
    }
#line 70
    perlin_destroy(noise);
}
void bumpmap_voronoi(Bumpmap * self, int n, float randomness) {
    int w = self->w;
    int h = self->h;
    Voronoi * noise = voronoi_create(w, h, n, randomness);
    for (int y = 0; y < h; y += 1) {
        for (int x = 0; x < w; x += 1) {
            self->bumpmap [x + w * y] = voronoi_at(noise, x, y);
        }
    }
#line 79
    voronoi_destroy(noise);
}
void bumpmap_mul_tile(Bumpmap * self, Tile * tile, Stencil * mask, float mul, float add) {
#line 83
    int w = self->w;
    int h = self->h;
    for (int y = 0; y < h; y += 1) {
        for (int x = 0; x < w; x += 1) {
            float v = self->lightmap [x + w * y];
            if (mask) {
                v *= mask->mask [x + w * y];
            }
#line 90
            v *= mul;
            v += add;
            rgba_colormul(v, v, v, tile->rgba + (y * w + x) * 4);
        }
    }
}
#line 94
void bumpmap_destroy(Bumpmap * self) {
    land_free(self->bumpmap);
    land_free(self->lightmap);
    land_free(self);
}
/* This file was generated by scramble.py. */
