]> mj.ucw.cz Git - libucw.git/blobdiff - images/dup-cmp.c
Merge with git+ssh://git.ucw.cz/projects/sherlock/GIT/sherlock.git
[libucw.git] / images / dup-cmp.c
index 6b8155ae20bc1e4d99a10063cab4c7d2779a8ffe..5fec1760c56275522fbfc5ea061d61f780bef272 100644 (file)
  *
  *      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
  */
 
-#define LOCAL_DEBUG
+#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 <fcntl.h>
 
 static uns image_dup_ratio_threshold = 140;
-static uns image_dup_error_threshold = 600;
-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;
-}
-
-int
-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 uns image_dup_error_threshold = 100;
 
 static inline uns
 err (int a, int b)
@@ -300,7 +206,7 @@ same_size_compare(struct image_dup *dup1, struct image_dup *dup2, uns trans)
   return err <= image_dup_error_threshold;
 }
 
-int
+uns
 image_dup_compare(struct image_dup *dup1, struct image_dup *dup2, uns flags)
 {
   DBG("image_dup_compare()");
@@ -348,6 +254,8 @@ image_dup_compare(struct image_dup *dup1, struct image_dup *dup2, uns flags)
                    result |= 1 << t;
                    if (!(flags & IMAGE_DUP_WANT_ALL))
                      return result;
+                   else
+                     break;
                  }
              }
          }
@@ -373,6 +281,8 @@ image_dup_compare(struct image_dup *dup1, struct image_dup *dup2, uns flags)
                    result |= 1 << t;
                    if (!(flags & IMAGE_DUP_WANT_ALL))
                      return result;
+                   else
+                     break;
                  }
              }
          }