From f9a73200a660323a23122c06ffc8c82002883e43 Mon Sep 17 00:00:00 2001 From: Pavel Charvat Date: Tue, 8 Aug 2006 10:26:50 +0200 Subject: [PATCH] images/dup-cmp.c split in two files --- images/Makefile | 2 +- images/dup-cmp.c | 107 +---------------------------- images/dup-init.c | 104 ++++++++++++++++++++++++++++ images/{dup-cmp.h => duplicates.h} | 16 ++++- images/image-dup-test.c | 3 +- 5 files changed, 124 insertions(+), 108 deletions(-) create mode 100644 images/dup-init.c rename images/{dup-cmp.h => duplicates.h} (77%) diff --git a/images/Makefile b/images/Makefile index 5c60744b..97abc79b 100644 --- a/images/Makefile +++ b/images/Makefile @@ -4,7 +4,7 @@ DIRS+=images PROGS+=$(addprefix $(o)/images/,image-tool image-dup-test image-sim-test) -LIBIMAGES_MODS=image scale color alpha io-main dup-cmp sig-dump sig-init sig-cmp object +LIBIMAGES_MODS=image scale color alpha io-main dup-init dup-cmp sig-dump sig-init sig-cmp object LIBIMAGES_LIBS=-lm diff --git a/images/dup-cmp.c b/images/dup-cmp.c index 35e1ec8e..5d365039 100644 --- a/images/dup-cmp.c +++ b/images/dup-cmp.c @@ -5,123 +5,20 @@ * * This software may be freely distributed and used according to the terms * of the GNU Lesser General Public License. - * - * - * FIXME: - * - many possible optimization - * - compare normalized pictures (brightness, ...) - * - a blur should help to deal with scaling errors - * - maybe better/slower last step - * - different thresholds for various transformations - * - do not test all transformations for symetric pictures - * - allocated memory could be easily decreased to about 1/3 - * for aspect ratio threshold near one */ #undef LOCAL_DEBUG #include "sherlock/sherlock.h" #include "lib/mempool.h" +#include "lib/fastbuf.h" #include "images/images.h" -#include "images/dup-cmp.h" +#include "images/duplicates.h" -#include "lib/mempool.h" -#include "lib/fastbuf.h" #include static uns image_dup_ratio_threshold = 140; static uns image_dup_error_threshold = 800; -static uns image_dup_tab_limit = 8; - -static inline byte * -image_dup_block(struct image_dup *dup, uns tab_col, uns tab_row) -{ - return dup->tab_pixels + (dup->tab_row_size << tab_row) + (3 << (tab_row + tab_col)); -} - -static inline struct image * -image_dup_subimage(struct image_thread *thread, struct image_dup *dup, struct image *block, uns tab_col, uns tab_row) -{ - return image_init_matrix(thread, block, image_dup_block(dup, tab_col, tab_row), - 1 << tab_col, 1 << tab_row, 3 << tab_col, COLOR_SPACE_RGB); -} - -static inline void -pixels_average(byte *dest, byte *src1, byte *src2) -{ - dest[0] = ((uns)src1[0] + (uns)src2[0]) >> 1; - dest[1] = ((uns)src1[1] + (uns)src2[1]) >> 1; - dest[2] = ((uns)src1[2] + (uns)src2[2]) >> 1; -} - -uns -image_dup_estimate_size(uns cols, uns rows) -{ - uns tab_cols, tab_rows; - for (tab_cols = 0; (uns)(2 << tab_cols) < cols && tab_cols < image_dup_tab_limit; tab_cols++); - for (tab_rows = 0; (uns)(2 << tab_rows) < rows && tab_rows < image_dup_tab_limit; tab_rows++); - return sizeof(struct image) + cols * rows * 3 + sizeof(struct image_dup) + (12 << (tab_cols + tab_rows)) + 64; -} - -uns -image_dup_init(struct image_thread *thread, struct image_dup *dup, struct image *img, struct mempool *pool) -{ - DBG("image_dup_init()"); - - ASSERT((img->flags & IMAGE_PIXEL_FORMAT) == COLOR_SPACE_RGB); - - dup->image = img; - for (dup->tab_cols = 0; (uns)(2 << dup->tab_cols) < img->cols && dup->tab_cols < image_dup_tab_limit; dup->tab_cols++); - for (dup->tab_rows = 0; (uns)(2 << dup->tab_rows) < img->rows && dup->tab_rows < image_dup_tab_limit; dup->tab_rows++); - dup->tab_pixels = mp_alloc(pool, dup->tab_size = (12 << (dup->tab_cols + dup->tab_rows))); - dup->tab_row_size = 6 << dup->tab_cols; - - /* Scale original image to right bottom block */ - { - struct image block; - if (!image_dup_subimage(thread, dup, &block, dup->tab_cols, dup->tab_rows)) - return 0; - if (!image_scale(thread, &block, img)) - return 0; - } - - /* Complete bottom row */ - for (uns i = dup->tab_cols; i--; ) - { - byte *d = image_dup_block(dup, i, dup->tab_rows); - byte *s = image_dup_block(dup, i + 1, dup->tab_rows); - for (uns y = 0; y < (uns)(1 << dup->tab_rows); y++) - for (uns x = 0; x < (uns)(1 << i); x++) - { - pixels_average(d, s, s + 3); - d += 3; - s += 6; - } - } - - /* Complete remaining blocks */ - for (uns i = 0; i <= dup->tab_cols; i++) - { - uns line_size = (3 << i); - for (uns j = dup->tab_rows; j--; ) - { - byte *d = image_dup_block(dup, i, j); - byte *s = image_dup_block(dup, i, j + 1); - for (uns y = 0; y < (uns)(1 << j); y++) - { - for (uns x = 0; x < (uns)(1 << i); x++) - { - pixels_average(d, s, s + line_size); - d += 3; - s += 3; - } - s += line_size; - } - } - } - - return 1; -} static inline uns err (int a, int b) diff --git a/images/dup-init.c b/images/dup-init.c new file mode 100644 index 00000000..da4b2be4 --- /dev/null +++ b/images/dup-init.c @@ -0,0 +1,104 @@ +/* + * Image Library -- Duplicates Comparison + * + * (c) 2006 Pavel Charvat + * + * This software may be freely distributed and used according to the terms + * of the GNU Lesser General Public License. + */ + +#undef LOCAL_DEBUG + +#include "sherlock/sherlock.h" +#include "lib/mempool.h" +#include "lib/fastbuf.h" +#include "images/images.h" +#include "images/duplicates.h" + +#include + +static uns image_dup_tab_limit = 8; + +static inline struct image * +image_dup_subimage(struct image_thread *thread, struct image_dup *dup, struct image *block, uns tab_col, uns tab_row) +{ + return image_init_matrix(thread, block, image_dup_block(dup, tab_col, tab_row), + 1 << tab_col, 1 << tab_row, 3 << tab_col, COLOR_SPACE_RGB); +} + +static inline void +pixels_average(byte *dest, byte *src1, byte *src2) +{ + dest[0] = ((uns)src1[0] + (uns)src2[0]) >> 1; + dest[1] = ((uns)src1[1] + (uns)src2[1]) >> 1; + dest[2] = ((uns)src1[2] + (uns)src2[2]) >> 1; +} + +uns +image_dup_estimate_size(uns cols, uns rows) +{ + uns tab_cols, tab_rows; + for (tab_cols = 0; (uns)(2 << tab_cols) < cols && tab_cols < image_dup_tab_limit; tab_cols++); + for (tab_rows = 0; (uns)(2 << tab_rows) < rows && tab_rows < image_dup_tab_limit; tab_rows++); + return sizeof(struct image) + cols * rows * 3 + sizeof(struct image_dup) + (12 << (tab_cols + tab_rows)) + 64; +} + +uns +image_dup_init(struct image_thread *thread, struct image_dup *dup, struct image *img, struct mempool *pool) +{ + DBG("image_dup_init()"); + + ASSERT((img->flags & IMAGE_PIXEL_FORMAT) == COLOR_SPACE_RGB); + + dup->image = img; + for (dup->tab_cols = 0; (uns)(2 << dup->tab_cols) < img->cols && dup->tab_cols < image_dup_tab_limit; dup->tab_cols++); + for (dup->tab_rows = 0; (uns)(2 << dup->tab_rows) < img->rows && dup->tab_rows < image_dup_tab_limit; dup->tab_rows++); + dup->tab_pixels = mp_alloc(pool, dup->tab_size = (12 << (dup->tab_cols + dup->tab_rows))); + dup->tab_row_size = 6 << dup->tab_cols; + + /* Scale original image to right bottom block */ + { + struct image block; + if (!image_dup_subimage(thread, dup, &block, dup->tab_cols, dup->tab_rows)) + return 0; + if (!image_scale(thread, &block, img)) + return 0; + } + + /* Complete bottom row */ + for (uns i = dup->tab_cols; i--; ) + { + byte *d = image_dup_block(dup, i, dup->tab_rows); + byte *s = image_dup_block(dup, i + 1, dup->tab_rows); + for (uns y = 0; y < (uns)(1 << dup->tab_rows); y++) + for (uns x = 0; x < (uns)(1 << i); x++) + { + pixels_average(d, s, s + 3); + d += 3; + s += 6; + } + } + + /* Complete remaining blocks */ + for (uns i = 0; i <= dup->tab_cols; i++) + { + uns line_size = (3 << i); + for (uns j = dup->tab_rows; j--; ) + { + byte *d = image_dup_block(dup, i, j); + byte *s = image_dup_block(dup, i, j + 1); + for (uns y = 0; y < (uns)(1 << j); y++) + { + for (uns x = 0; x < (uns)(1 << i); x++) + { + pixels_average(d, s, s + line_size); + d += 3; + s += 3; + } + s += line_size; + } + } + } + + return 1; +} diff --git a/images/dup-cmp.h b/images/duplicates.h similarity index 77% rename from images/dup-cmp.h rename to images/duplicates.h index bfb24483..07db2afe 100644 --- a/images/dup-cmp.h +++ b/images/duplicates.h @@ -22,8 +22,22 @@ struct image_dup { #define IMAGE_DUP_SCALE 0x100 #define IMAGE_DUP_WANT_ALL 0x200 +/* dup-init.c */ + uns image_dup_init(struct image_thread *thread, struct image_dup *dup, struct image *image, struct mempool *pool); -uns image_dup_compare(struct image_dup *dup1, struct image_dup *dup2, uns flags); uns image_dup_estimate_size(uns cols, uns rows); +/* dup-cmp.c */ + +uns image_dup_compare(struct image_dup *dup1, struct image_dup *dup2, uns flags); + +/* internals */ + +static inline byte * +image_dup_block(struct image_dup *dup, uns tab_col, uns tab_row) +{ + return dup->tab_pixels + (dup->tab_row_size << tab_row) + (3 << (tab_row + tab_col)); +} + + #endif diff --git a/images/image-dup-test.c b/images/image-dup-test.c index e0d0558c..0d18ece0 100644 --- a/images/image-dup-test.c +++ b/images/image-dup-test.c @@ -12,7 +12,8 @@ #include "lib/fastbuf.h" #include "images/images.h" #include "images/color.h" -#include "images/dup-cmp.h" +#include "images/duplicates.h" + #include #include #include -- 2.39.2