From 623d6dd63eaa1d2cca89bffe6048a4e5b2b74b1b Mon Sep 17 00:00:00 2001 From: Alex 'AdUser' Z Date: Wed, 12 Mar 2014 21:30:59 +1100 Subject: [PATCH] * change sources --- src/Makefile | 11 +++++++++++ src/bitmap.c | 35 +++++++++++++++++++++++++++++++++++ src/bitmap.h | 3 +++ src/image.c | 17 ----------------- src/image.h | 5 ----- src/test-image.c | 17 ----------------- tests/bitmap.c | 34 ++++++++++++++++++++++++++++++++++ 7 files changed, 83 insertions(+), 39 deletions(-) create mode 100644 src/Makefile create mode 100644 src/bitmap.c create mode 100644 src/bitmap.h delete mode 100644 src/test-image.c create mode 100644 tests/bitmap.c diff --git a/src/Makefile b/src/Makefile new file mode 100644 index 0000000..596ad5a --- /dev/null +++ b/src/Makefile @@ -0,0 +1,11 @@ +all: test-bitmap + +%.o: %.c + gcc -O0 -Wall -Wextra -pedantic -ggdb $< -o $@ + +test-bitmap: test-bitmap.c bitmap.c + gcc -Wall -Wextra -pedantic -g -ggdb test-bitmap.c bitmap.c -o test-bitmap + +clean: + rm -f *.o + rm -f test-bitmap diff --git a/src/bitmap.c b/src/bitmap.c new file mode 100644 index 0000000..6753e79 --- /dev/null +++ b/src/bitmap.c @@ -0,0 +1,35 @@ +/* + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Library General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor Boston, MA 02110-1301, USA + */ + +#include "main.h" +#include "bitmap.h" + +int bitmap_compare(bitmap *a, bitmap *b) +{ + uint16_t row = 0; + uint16_t bit = 0; + size_t cnt = 0; + + for (row = 0; row < 16; row++) { + bit = 1 << 15; + while (bit) { + cnt += !!(((*a)[row] & bit) ^ ((*b)[row] & bit)); + bit >>= 1; + } + } + + return cnt; +} diff --git a/src/bitmap.h b/src/bitmap.h new file mode 100644 index 0000000..85eda6b --- /dev/null +++ b/src/bitmap.h @@ -0,0 +1,3 @@ +typedef uint16_t bitmap[16]; + +int bitmap_compare(bitmap *a, bitmap *b); \ No newline at end of file diff --git a/src/image.c b/src/image.c index ba9afa8..26541cd 100644 --- a/src/image.c +++ b/src/image.c @@ -19,23 +19,6 @@ #include -int bitmap_compare(image_bitmap * const a, image_bitmap * const b) -{ - uint16_t row = 0; - uint16_t bit = 0; - size_t cnt = 0; - - for (row = 0; row < 16; row++) { - bit = 1 << 15; - while (bit) { - cnt += !!(((*a)[row] & bit) ^ ((*b)[row] & bit)); - bit >>= 1; - } - } - - return cnt; -} - int image_load(void) { return 1; diff --git a/src/image.h b/src/image.h index a991cb2..0551ace 100644 --- a/src/image.h +++ b/src/image.h @@ -1,6 +1 @@ -#define SAMPLE_SIZE sizeof(uint16_t) - -typedef uint16_t image_bitmap[16]; - -int bitmap_compare(image_bitmap * const a, image_bitmap * const b); int image_load(void); diff --git a/src/test-image.c b/src/test-image.c deleted file mode 100644 index f6a61b0..0000000 --- a/src/test-image.c +++ /dev/null @@ -1,17 +0,0 @@ -#include "main.h" -#include "image.h" - -int main(int argc, char **argv) -{ - int i = 0; - image_bitmap a, b; - - for (i = 0; i < 16; i++) { - a[i] = 0x0000; - b[i] = 0xFFFF; - } - - printf("%u\n", bitmap_compare(&a, &b)); - - return 1; -} diff --git a/tests/bitmap.c b/tests/bitmap.c new file mode 100644 index 0000000..24a6097 --- /dev/null +++ b/tests/bitmap.c @@ -0,0 +1,34 @@ +#include "main.h" +#include "bitmap.h" + +int +main(int argc, char **argv) +{ + bitmap a, b; + + memset (&a, 0x00, 16 * sizeof(uint16_t)); + memset (&b, 0x00, 16 * sizeof(uint16_t)); + assert(bitmap_compare(&a, &b) == 0); + + memset (&a, 0xFE, 16 * sizeof(uint16_t)); + memset (&b, 0xFF, 16 * sizeof(uint16_t)); + assert(bitmap_compare(&a, &b) == 32); + + memset (&a, 0x00, 16 * sizeof(uint16_t)); + memset (&b, 0x55, 16 * sizeof(uint16_t)); + assert(bitmap_compare(&a, &b) == 128); + + memset (&a, 0x00, 16 * sizeof(uint16_t)); + memset (&b, 0xAA, 16 * sizeof(uint16_t)); + assert(bitmap_compare(&a, &b) == 128); + + memset (&a, 0xAA, 16 * sizeof(uint16_t)); + memset (&b, 0x55, 16 * sizeof(uint16_t)); + assert(bitmap_compare(&a, &b) == 256); + + memset (&a, 0x00, 16 * sizeof(uint16_t)); + memset (&b, 0xFF, 16 * sizeof(uint16_t)); + assert(bitmap_compare(&a, &b) == 256); + + return 0; +}