]> mj.ucw.cz Git - libucw.git/blobdiff - images/image-sim-test.c
* dev-threads merged to dev-img
[libucw.git] / images / image-sim-test.c
index c58c9d08ed8ed1d399ec63c9e52af3440a780f87..22f3951cc7348d95ef3b952045b2714212ece442 100644 (file)
@@ -13,6 +13,7 @@
 #include "images/images.h"
 #include "images/color.h"
 #include "images/signature.h"
+
 #include <stdlib.h>
 #include <fcntl.h>
 #include <errno.h>
@@ -31,13 +32,13 @@ Usage: image-sim-test [options] image1 [image2] \n\
 -f --format-1        image1 format (jpeg, gif, png)\n\
 -F --format-2        image2 format\n\
 -g --background      background color (hexadecimal RRGGBB)\n\
--s --segmentation-1  writes image1 segmentation to given file\n\
--S --segmentation-2  writes image2 segmentation to given file\n\
+-r --segmentation-1  writes image1 segmentation to given file\n\
+-R --segmentation-2  writes image2 segmentation to given file\n\
 ", stderr);
   exit(1);
 }
 
-static char *shortopts = "qf:F:g:t:s:S:" CF_SHORT_OPTS;
+static char *shortopts = "qf:F:g:t:r:R:" CF_SHORT_OPTS;
 static struct option longopts[] =
 {
   CF_LONG_OPTS
@@ -45,8 +46,8 @@ static struct option longopts[] =
   { "format-1",                0, 0, 'f' },
   { "format-2",                0, 0, 'F' },
   { "background",      0, 0, 'g' },
-  { "segmentation-1",  0, 0, 's' },
-  { "segmentation-2",  0, 0, 'S' },
+  { "segmentation-1",  0, 0, 'r' },
+  { "segmentation-2",  0, 0, 'R' },
   { NULL,              0, 0, 0 }
 };
 
@@ -60,7 +61,13 @@ static byte *segmentation_name_1;
 static byte *segmentation_name_2;
 
 #define MSG(x...) do{ if (verbose) log(L_INFO, ##x); }while(0)
-#define TRY(x) do{ if (!(x)) die("Error: %s", it.err_msg); }while(0)
+#define TRY(x) do{ if (!(x)) exit(1); }while(0)
+
+static void
+msg_str(byte *s, void *param UNUSED)
+{
+  MSG("%s", s);
+}
 
 static void
 dump_signature(struct image_signature *sig)
@@ -75,7 +82,7 @@ dump_signature(struct image_signature *sig)
     }
 }
 
-static struct image_thread it;
+static struct image_context ctx;
 static struct image_io io;
 
 static void
@@ -85,16 +92,21 @@ write_segmentation(struct image_sig_data *data, byte *fn)
   
   struct fastbuf *fb = bopen(fn, O_WRONLY | O_CREAT | O_TRUNC, 4096);
   struct image *img;
-  TRY(img = image_new(&it, data->image->cols, data->image->rows, COLOR_SPACE_RGB, NULL));
-  image_clear(&it, img);
+  TRY(img = image_new(&ctx, data->image->cols, data->image->rows, COLOR_SPACE_RGB, NULL));
+  image_clear(&ctx, img);
 
   for (uns i = 0; i < data->regions_count; i++)
     {
       byte c[3];
-      // FIXME: convert from Luv to RGB
-      c[0] = data->regions[i].a[0];
-      c[1] = data->regions[i].a[1];
-      c[2] = data->regions[i].a[2];
+      double luv[3], xyz[3], srgb[3];
+      luv[0] = data->regions[i].a[0] * (4 / 2.55);
+      luv[1] = ((int)data->regions[i].a[1] - 128) * (4 / 2.55);
+      luv[2] = ((int)data->regions[i].a[2] - 128) * (4 / 2.55);
+      luv_to_xyz_exact(xyz, luv);
+      xyz_to_srgb_exact(srgb, xyz);
+      c[0] = CLAMP(srgb[0] * 255, 0, 255); 
+      c[1] = CLAMP(srgb[1] * 255, 0, 255); 
+      c[2] = CLAMP(srgb[2] * 255, 0, 255); 
       for (struct image_sig_block *block = data->regions[i].blocks; block; block = block->next)
         {
          uns x1 = block->x * 4;
@@ -156,10 +168,10 @@ main(int argc, char **argv)
            color_make_rgb(&background_color, (v >> 16) & 255, (v >> 8) & 255, v & 255);
          }
          break;
-       case 's':
+       case 'r':
          segmentation_name_1 = optarg;
          break;
-       case 'S':
+       case 'R':
          segmentation_name_2 = optarg;
          break;
        default:
@@ -175,12 +187,11 @@ main(int argc, char **argv)
   MSG("Initializing image library");
   srandom(time(NULL) ^ getpid());
   srgb_to_luv_init();
-  image_thread_init(&it);
+  image_context_init(&ctx);
 
   struct image *img1, *img2;
 
-  if (!image_io_init(&it, &io))
-    die("Cannot initialize image I/O (%s)", it.err_msg);
+  TRY(image_io_init(&ctx, &io));
 
   if (file_name_1)
     {
@@ -227,10 +238,13 @@ main(int argc, char **argv)
   if (img1)
     {
       struct image_sig_data data;
-      TRY(image_sig_init(&it, &data, img1));
+      TRY(image_sig_init(&ctx, &data, img1));
       image_sig_preprocess(&data);
       if (data.valid)
-       image_sig_segmentation(&data);
+        {
+         image_sig_segmentation(&data);
+         image_sig_detect_textured(&data);
+       }
       if (segmentation_name_1)
        write_segmentation(&data, segmentation_name_1);
       image_sig_finish(&data, &sig1);
@@ -240,10 +254,13 @@ main(int argc, char **argv)
   if (img2)
     {
       struct image_sig_data data;
-      TRY(image_sig_init(&it, &data, img2));
+      TRY(image_sig_init(&ctx, &data, img2));
       image_sig_preprocess(&data);
       if (data.valid)
-       image_sig_segmentation(&data);
+        {
+         image_sig_segmentation(&data);
+         image_sig_detect_textured(&data);
+       }
       if (segmentation_name_2)
        write_segmentation(&data, segmentation_name_2);
       image_sig_finish(&data, &sig2);
@@ -253,8 +270,16 @@ main(int argc, char **argv)
 
   if (img1 && img2)
     {
-      uns dist = image_signatures_dist(&sig1, &sig2);
-      MSG("dist=%.6f", dist / (double)(1 << IMAGE_SIG_DIST_SCALE));
+      uns dist;
+      if (verbose)
+        {
+          struct fastbuf *fb = bfdopen(0, 4096);
+          dist = image_signatures_dist_explain(&sig1, &sig2, msg_str, NULL);
+          bclose(fb);
+       }
+      else
+       dist = image_signatures_dist(&sig1, &sig2);
+      MSG("dist=%u", dist);
     }
 
   if (img1)
@@ -263,7 +288,7 @@ main(int argc, char **argv)
     image_destroy(img2);
 
   image_io_cleanup(&io);
-  image_thread_cleanup(&it);
+  image_context_cleanup(&ctx);
   MSG("Done.");
   return 0;
 }