diff options
-rw-r--r-- | LICENSE | 2 | ||||
-rw-r--r-- | libslim.h | 94 |
2 files changed, 83 insertions, 13 deletions
@@ -1,6 +1,6 @@ ISC License -© 2019 Mattias Andrée <maandree@kth.se> +© 2019 Mattias Andrée <m@maandree.se> Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted, provided that the above @@ -1,7 +1,7 @@ /** * ISC License * - * © 2019 Mattias Andrée <maandree@kth.se> + * © 2019 Mattias Andrée <m@maandree.se> * * Permission to use, copy, modify, and/or distribute this software for any * purpose with or without fee is hereby granted, provided that the above @@ -85,7 +85,7 @@ LIBSLIM_DECLARE_FORMAT(rgb_ld, { long double r, g, b; }); do {\ size_t i__;\ size_t n__ = (IN)->meta.width - 1;\ - for (i__ = 0; i__ < n__; i__++)\ + for (i__ = 0; i__ <= n__; i__++)\ (OUT)->data[n__ - i__] = (IN)->data[i__];\ } while (0) @@ -101,7 +101,7 @@ LIBSLIM_DECLARE_FORMAT(rgb_ld, { long double r, g, b; }); (OUT)->meta.width = (IN)->meta.width;\ (OUT)->meta.height = (IN)->meta.height;\ while (h__--) {\ - for (i__ = 0; i__ < n__; i__++)\ + for (i__ = 0; i__ <= n__; i__++)\ (OUT)->data[n__ - i__] = (IN)->data[i__];\ (IN)->data += (IN)->meta.width + (IN)->meta.hblank;\ (OUT)->data += (OUT)->meta.width + (OUT)->meta.hblank;\ @@ -115,13 +115,13 @@ LIBSLIM_DECLARE_FORMAT(rgb_ld, { long double r, g, b; }); #define libslim_flip(OUT, IN)\ do {\ size_t i__;\ - size_t n__ = (IN)->meta.height - 1;\ + size_t n__ = (IN)->meta.height;\ size_t w__ = (IN)->meta.width * sizeof(*(IN)->data);\ void *in__ = (IN)->data;\ void *out__ = (OUT)->data;\ (OUT)->meta.width = (IN)->meta.width;\ (OUT)->meta.height = (IN)->meta.height;\ - (OUT)->data += n__ * ((OUT)->meta.width + (OUT)->meta.hblank);\ + (OUT)->data += (n__ - 1) * ((OUT)->meta.width + (OUT)->meta.hblank); \ for (i__ = 0; i__ < n__; i__++) {\ memcpy((OUT)->data, (IN)->data, w__);\ (IN)->data += (IN)->meta.width + (IN)->meta.hblank;\ @@ -150,31 +150,101 @@ LIBSLIM_DECLARE_FORMAT(rgb_ld, { long double r, g, b; }); (OUT)->data += 1;\ }\ (IN)->data = in__;\ - (OUT)->data = in__;\ + (OUT)->data = out__;\ } while (0) /* Rotate an image 90 degrees clockwise */ #define libslim_rotate_90(OUT, IN)\ do {\ - libslim_transpose((IN), (OUT));\ - libslim_flop((IN), (OUT));\ + /* equivalent to flip than transpose */\ + size_t x__, y__, i__;\ + size_t rw__ = (OUT)->meta.width + (OUT)->meta.hblank;\ + size_t w__ = (IN)->meta.width;\ + size_t h__ = (IN)->meta.height;\ + void *in__ = (IN)->data;\ + void *out__ = (OUT)->data;\ + (OUT)->meta.width = (IN)->meta.height;\ + (OUT)->meta.height = (IN)->meta.width;\ + (IN)->data += (h__ - 1) * ((IN)->meta.width + (IN)->meta.hblank);\ + for (y__ = 0; y__ < h__; y__++) {\ + for (x__ = i__ = 0; x__ < w__; x__++, i__ += rw__)\ + (OUT)->data[i__] = (IN)->data[x__];\ + (IN)->data -= (IN)->meta.width + (IN)->meta.hblank;\ + (OUT)->data += 1;\ + }\ + (IN)->data = in__;\ + (OUT)->data = out__;\ } while (0) /* Rotate an image 180 degrees */ #define libslim_rotate_180(OUT, IN)\ do {\ - libslim_flip((IN), (OUT));\ - libslim_flop((IN), (OUT));\ + /* equivalent to flip and flop */\ + size_t i__, j__;\ + size_t n__ = (IN)->meta.height;\ + size_t w__ = (IN)->meta.width - 1;\ + void *in__ = (IN)->data;\ + void *out__ = (OUT)->data;\ + (OUT)->meta.width = (IN)->meta.width;\ + (OUT)->meta.height = (IN)->meta.height;\ + (OUT)->data += (n__ - 1) * ((OUT)->meta.width + (OUT)->meta.hblank);\ + for (i__ = 0; i__ < n__; i__++) {\ + for (j__ = 0; j__ <= w__; j__++)\ + (OUT)->data[w__ - j__] = (IN)->data[j__];\ + (IN)->data += (IN)->meta.width + (IN)->meta.hblank;\ + (OUT)->data -= (OUT)->meta.width + (OUT)->meta.hblank;\ + }\ + (IN)->data = in__;\ + (OUT)->data = out__;\ } while (0) /* Rotate an image 270 degrees clockwise */ #define libslim_rotate_270(OUT, IN)\ do {\ - libslim_flop((IN), (OUT));\ - libslim_transpose((IN), (OUT));\ + /* equivalent to transpose than flip */\ + size_t x__, y__, i__;\ + size_t rw__ = (OUT)->meta.width + (OUT)->meta.hblank;\ + size_t w__ = (IN)->meta.width;\ + size_t h__ = (IN)->meta.height;\ + void *in__ = (IN)->data;\ + void *out__ = (OUT)->data;\ + (OUT)->meta.width = (IN)->meta.height;\ + (OUT)->meta.height = (IN)->meta.width;\ + for (y__ = 0; y__ < h__; y__++) {\ + for (x__ = 0, i__ = rw__ * (w__ - 1); x__ < w__; x__++, i__ -= rw__) \ + (OUT)->data[i__] = (IN)->data[x__];\ + (IN)->data += (IN)->meta.width + (IN)->meta.hblank;\ + (OUT)->data += 1;\ + }\ + (IN)->data = in__;\ + (OUT)->data = out__;\ + } while (0) + + +/* Transpose an image along instead of across the main diagonal */ +#define libslim_anti_transpose(OUT, IN)\ + do {\ + /* equivalent to flip than transpose than flip, or a tranpose and a 180 degree rotation */\ + size_t x__, y__, i__;\ + size_t rw__ = (OUT)->meta.width + (OUT)->meta.hblank;\ + size_t w__ = (IN)->meta.width;\ + size_t h__ = (IN)->meta.height;\ + void *in__ = (IN)->data;\ + void *out__ = (OUT)->data;\ + (OUT)->meta.width = (IN)->meta.height;\ + (OUT)->meta.height = (IN)->meta.width;\ + (IN)->data += (h__ - 1) * ((IN)->meta.width + (IN)->meta.hblank);\ + for (y__ = 0; y__ < h__; y__++) {\ + for (x__ = 0, i__ = rw__ * (w__ - 1); x__ < w__; x__++, i__ -= rw__) \ + (OUT)->data[i__] = (IN)->data[x__];\ + (IN)->data -= (IN)->meta.width + (IN)->meta.hblank;\ + (OUT)->data += 1;\ + }\ + (IN)->data = in__;\ + (OUT)->data = out__;\ } while (0) |