]> mj.ucw.cz Git - libucw.git/blobdiff - images/image-sim-test.c
ucw docs: init and commit hooks described in-place
[libucw.git] / images / image-sim-test.c
index e0a1899e2f94ce8277ba083931a227fc4c8e6cc7..ed43e0f2d6f24dd5aff474a28d0022b369bb6878 100644 (file)
@@ -7,12 +7,15 @@
  *     of the GNU General Public License.
  */
 
-#include "lib/lib.h"
-#include "lib/getopt.h"
-#include "lib/fastbuf.h"
+#include "ucw/lib.h"
+#include "ucw/getopt.h"
+#include "ucw/fastbuf.h"
+#include "ucw/base64.h"
+#include "ucw/base224.h"
 #include "images/images.h"
 #include "images/color.h"
 #include "images/signature.h"
+
 #include <stdlib.h>
 #include <fcntl.h>
 #include <errno.h>
@@ -33,11 +36,13 @@ Usage: image-sim-test [options] image1 [image2] \n\
 -g --background      background color (hexadecimal RRGGBB)\n\
 -r --segmentation-1  writes image1 segmentation to given file\n\
 -R --segmentation-2  writes image2 segmentation to given file\n\
+-6 --base64          display base64 encoded signature(s)\n\
+-2 --base224         display base224 encoded signature(s)\n\
 ", stderr);
   exit(1);
 }
 
-static char *shortopts = "qf:F:g:t:r:R:" CF_SHORT_OPTS;
+static char *shortopts = "qf:F:g:t:r:R:62" CF_SHORT_OPTS;
 static struct option longopts[] =
 {
   CF_LONG_OPTS
@@ -47,6 +52,8 @@ static struct option longopts[] =
   { "background",      0, 0, 'g' },
   { "segmentation-1",  0, 0, 'r' },
   { "segmentation-2",  0, 0, 'R' },
+  { "base64",          0, 0, '6' },
+  { "base224",         0, 0, '2' },
   { NULL,              0, 0, 0 }
 };
 
@@ -58,9 +65,17 @@ static enum image_format format_2;
 static struct color background_color;
 static byte *segmentation_name_1;
 static byte *segmentation_name_2;
+static uns display_base64;
+static uns display_base224;
+
+#define MSG(x...) do{ if (verbose) msg(L_INFO, ##x); }while(0)
+#define TRY(x) do{ if (!(x)) exit(1); }while(0)
 
-#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)
+static void
+msg_str(byte *s, void *param UNUSED)
+{
+  MSG("%s", s);
+}
 
 static void
 dump_signature(struct image_signature *sig)
@@ -73,20 +88,35 @@ dump_signature(struct image_signature *sig)
       image_region_dump(buf, sig->reg + i);
       MSG("region %u: %s", i, buf);
     }
+  uns sig_size = image_signature_size(sig->len);
+  if (display_base64)
+    {
+      byte buf[BASE64_ENC_LENGTH(sig_size) + 1];
+      uns enc_size = base64_encode(buf, (byte *)sig, sig_size);
+      buf[enc_size] = 0;
+      MSG("base64 encoded: %s", buf);
+    }
+  if (display_base224)
+    {
+      byte buf[BASE224_ENC_LENGTH(sig_size) + 1];
+      uns enc_size = base224_encode(buf, (byte *)sig, sig_size);
+      buf[enc_size] = 0;
+      MSG("base224 encoded: %s", buf);
+    }
 }
 
-static struct image_thread it;
+static struct image_context ctx;
 static struct image_io io;
 
 static void
 write_segmentation(struct image_sig_data *data, byte *fn)
 {
   MSG("Writing segmentation to %s", 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++)
     {
@@ -95,11 +125,11 @@ write_segmentation(struct image_sig_data *data, byte *fn)
       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_slow(xyz, luv);
-      xyz_to_srgb_slow(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); 
+      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;
@@ -122,7 +152,7 @@ write_segmentation(struct image_sig_data *data, byte *fn)
 
   io.fastbuf = fb;
   io.image = img;
-  io.format = image_file_name_to_format(fn); 
+  io.format = image_file_name_to_format(fn);
   TRY(image_io_write(&io));
   image_io_reset(&io);
 
@@ -167,6 +197,12 @@ main(int argc, char **argv)
        case 'R':
          segmentation_name_2 = optarg;
          break;
+       case '6':
+         display_base64++;
+         break;
+       case '2':
+         display_base224++;
+         break;
        default:
          usage();
       }
@@ -180,12 +216,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)
     {
@@ -232,7 +267,7 @@ 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)
         {
@@ -248,7 +283,7 @@ 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)
         {
@@ -268,7 +303,7 @@ main(int argc, char **argv)
       if (verbose)
         {
           struct fastbuf *fb = bfdopen(0, 4096);
-          dist = image_signatures_dist_explain(&sig1, &sig2, fb);
+          dist = image_signatures_dist_explain(&sig1, &sig2, msg_str, NULL);
           bclose(fb);
        }
       else
@@ -282,7 +317,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;
 }