diff options
author | kdx <kikoodx@paranoici.org> | 2023-07-25 01:29:16 +0200 |
---|---|---|
committer | kdx <kikoodx@paranoici.org> | 2023-07-25 01:29:16 +0200 |
commit | 1984f60480a050e16f671dbbb18922a90d7a9b1a (patch) | |
tree | 204a4cdb3c8445ba20c91fe28e63132eafa6efd1 | |
parent | 4cd7ee8210aafaf927e381603e4995fb3b1e68d9 (diff) | |
download | catgy-1984f60480a050e16f671dbbb18922a90d7a9b1a.tar.gz |
reduce duplication
-rw-r--r-- | catgy.c | 74 |
1 files changed, 53 insertions, 21 deletions
@@ -12,12 +12,15 @@ struct image { uint8_t *data; }; -static int process(int argc, char **argv); static void bit3(struct image *img); static void chaos1(struct image *img); static void chaos3(struct image *img); static void blurge(struct image *img); static void gol(struct image *img); +static int process(int argc, char **argv); +static int duplicate(struct image *dest, struct image *img); +static void move(struct image *dest, struct image *img); +static uint8_t get(struct image *img, int x, int y, int o); int main(int argc, char **argv) { @@ -80,42 +83,37 @@ blurge(struct image *img) static int neighbors(struct image *img, int x, int y, int o) { - const int stride = img->w * 3; - return (img->data[o + (x - 1) * 3 + (y ) * stride] != 0) + - (img->data[o + (x + 1) * 3 + (y ) * stride] != 0) + - (img->data[o + (x ) * 3 + (y - 1) * stride] != 0) + - (img->data[o + (x ) * 3 + (y + 1) * stride] != 0) + - (img->data[o + (x - 1) * 3 + (y - 1) * stride] != 0) + - (img->data[o + (x - 1) * 3 + (y + 1) * stride] != 0) + - (img->data[o + (x + 1) * 3 + (y - 1) * stride] != 0) + - (img->data[o + (x + 1) * 3 + (y + 1) * stride] != 0); + return (get(img, x - 1, y , o) != 0) + + (get(img, x + 1, y , o) != 0) + + (get(img, x , y - 1, o) != 0) + + (get(img, x , y + 1, o) != 0) + + (get(img, x - 1, y - 1, o) != 0) + + (get(img, x - 1, y + 1, o) != 0) + + (get(img, x + 1, y - 1, o) != 0) + + (get(img, x + 1, y + 1, o) != 0); } static void gol(struct image *img) { - uint8_t *copy = malloc(img->w * img->h * img->chans); - if (copy == NULL) { - perror("malloc"); + struct image copy; + if (duplicate(©, img)) return; - } - memcpy(copy, img->data, img->w * img->h * img->chans); - for (int y = 1; y < img->h - 1; y++) { - for (int x = 1; x < img->w - 1; x++) { + for (int y = 0; y < img->h; y++) { + for (int x = 0; x < img->w; x++) { for (int o = 0; o < 3; o++) { const int near = neighbors(img, x, y, o); const int i = x * 3 + y * img->w * 3 + o; if (near < 2 || near > 3) - copy[i] = 0; + copy.data[i] = 0; else - copy[i] = 0xff; + copy.data[i] = 0xff; } } } - memcpy(img->data, copy, img->w * img->h * img->chans); - free(copy); + move(img, ©); } static int @@ -181,3 +179,37 @@ process(int argc, char **argv) stbi_image_free(img.data); return 0; } + +static int +duplicate(struct image *dest, struct image *src) +{ + memcpy(dest, src, sizeof(*dest)); + dest->data = malloc(src->w * src->h * src->chans); + if (dest->data == NULL) { + perror("malloc"); + return 1; + } + memcpy(dest->data, src->data, src->w * src->h * src->chans); + return 0; +} + +static void +move(struct image *dest, struct image *src) +{ + memcpy(dest->data, src->data, src->w * src->h * src->chans); + free(src->data); +} + +static uint8_t +get(struct image *img, int x, int y, int o) +{ + if (x < 0) + x = img->w + x; + if (x >= img->w) + x = x - img->w; + if (y < 0) + y = img->h + y; + if (y >= img->h) + y = y - img->h; + return img->data[o + x * 3 + y * img->w * 3]; +} |