<commit_before>#include <glib.h>

#include "diamond_interface.h"
#include "fatfind.h"
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
#include <gtk/gtk.h>

static struct collection_t collections[MAX_ALBUMS+1] = { { NULL } };
static gid_list_t diamond_gid_list;

static void diamond_init(void) {
  int i;
  int j;
  int err;
  void *cookie;
  char *name;

  if (diamond_gid_list.num_gids != 0) {
    // already initialized
    return;
  }

  printf("reading collections...\n");
  {
    int pos = 0;
    err = nlkup_first_entry(&name, &cookie);
    while(!err && pos < MAX_ALBUMS)
      {
	collections[pos].name = name;
	collections[pos].active = pos ? 0 : 1; /* first one active */
	pos++;
	err = nlkup_next_entry(&name, &cookie);
      }
    collections[pos].name = NULL;
  }


  for (i=0; i<MAX_ALBUMS && collections[i].name; i++) {
    if (collections[i].active) {
      int err;
      int num_gids = MAX_ALBUMS;
      groupid_t gids[MAX_ALBUMS];
      err = nlkup_lookup_collection(collections[i].name, &num_gids, gids);
      g_assert(!err);
      for (j=0; j < num_gids; j++) {
	printf("adding gid: %lld\n", gids[j]);
	diamond_gid_list.gids[diamond_gid_list.num_gids++] = gids[j];
      }
    }
  }
}

static ls_search_handle_t generic_search (char *filter_spec_name) {
  ls_search_handle_t diamond_handle;
  int f1, f2;
  int err;

  char buf[1];

  diamond_init();

  diamond_handle = ls_init_search();

  err = ls_set_searchlist(diamond_handle, 1, diamond_gid_list.gids);
  g_assert(!err);

  // append our stuff
  f1 = g_open(FATFIND_FILTERDIR "/rgb-filter.txt", O_RDONLY);
  if (f1 == -1) {
    perror("can't open " FATFIND_FILTERDIR "/rgb-filter.txt");
    return NULL;
  }

  f2 = g_open(filter_spec_name, O_WRONLY | O_APPEND);
  if (f2 == -1) {
    printf("can't open %s", filter_spec_name);
    perror("");
    return NULL;
  }

  while (read(f1, buf, 1) > 0) {
    write(f2, buf, 1);   // PERF
  }

  close(f1);
  close(f2);

  err = ls_set_searchlet(diamond_handle, DEV_ISA_IA32,
			 FATFIND_FILTERDIR "/fil_rgb.so",
			 filter_spec_name);
  g_assert(!err);


  return diamond_handle;
}


gboolean diamond_result_callback(gpointer g_data) {
  // this gets 0 or 1 result from diamond and puts it into the
  // icon view

  ls_obj_handle_t obj;
  void *data;
  char *name;
  void *cookie;
  int len;
  int err;
  int w;
  int h;
  GdkPixbuf *pix, *pix2;

  int i;

  GList *clist = NULL;
  gchar *title;

  GtkTreeIter iter;


  // get handle
  ls_search_handle_t dr = (ls_search_handle_t) g_data;

  err = ls_next_object(dr, &obj, LSEARCH_NO_BLOCK);

  // XXX should be able to use select()
  if (err == EWOULDBLOCK) {
    // no results right now
    return TRUE;
  } else if (err) {
    // no more results
    ls_abort_search(dr);
    return FALSE;
  }

  printf("got object: %p\n", obj);

  err = lf_ref_attr(obj, "circle-data", &len, &data);
  g_assert(!err);

  // XXX
  for (i = 0; i < len / sizeof(float); i += 3) {
    float *p = ((float *) data) + i;
    circle_type *c = g_malloc(sizeof(circle_type));

    c->x = p[0];
    c->y = p[1];
    c->r = p[2];

    clist = g_list_prepend(clist, c);
  }

  // text
  title = g_strdup_printf("%d items", g_list_length(clist));

  // thumbnail
  err = lf_ref_attr(obj, "_cols.int", &len, &data);
  g_assert(!err);
  w = *((int *) data);

  err = lf_ref_attr(obj, "_rows.int", &len, &data);
  g_assert(!err);
  h = *((int *) data);

  err = lf_ref_attr(obj, "_rgb_image.rgbimage", &len, &data);
  g_assert(!err);

  printf(" img %dx%d (%d bytes)\n", w, h, len);

  pix = gdk_pixbuf_new_from_data(data, GDK_COLORSPACE_RGB,
				 TRUE, 8, w, h, w*4, NULL, NULL);
  pix2 = gdk_pixbuf_scale_simple(pix, 150, 150, GDK_INTERP_BILINEAR);

  gtk_list_store_append(found_items, &iter);
  gtk_list_store_set(found_items, &iter,
		     0, pix2,
		     1, title,
		     2, clist,
		     -1);

  g_object_unref(pix);
  g_object_unref(pix2);

  //  err = lf_first_attr(obj, &name, &len, &data, &cookie);
  //  while (!err) {
  //    printf(" attr name: %s, length: %d\n", name, len);
  //    err = lf_next_attr(obj, &name, &len, &data, &cookie);
  //  }

  err = ls_release_object(dr, obj);
  g_assert(!err);

  return TRUE;
}


ls_search_handle_t diamond_circle_search(int dp, int minDist,
					 int blur, int accumulatorThresh,
					 int minRadius, int maxRadius,
					 int radiusStep, int cannyThreshold) {
  ls_search_handle_t dr;<commit_msg>perfect<commit_after>#include <glib.h>

#include "diamond_interface.h"
#include "fatfind.h"
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
#include <gtk/gtk.h>

static struct collection_t collections[MAX_ALBUMS+1] = { { NULL } };
static gid_list_t diamond_gid_list;

static void diamond_init(void) {
  int i;
  int j;
  int err;
  void *cookie;
  char *name;

  if (diamond_gid_list.num_gids != 0) {
    // already initialized
    return;
  }

  printf("reading collections...\n");
  {
    int pos = 0;
    err = nlkup_first_entry(&name, &cookie);
    while(!err && pos < MAX_ALBUMS)
      {
	collections[pos].name = name;
	collections[pos].active = pos ? 0 : 1; /* first one active */
	pos++;
	err = nlkup_next_entry(&name, &cookie);
      }
    collections[pos].name = NULL;
  }


  for (i=0; i<MAX_ALBUMS && collections[i].name; i++) {
    if (collections[i].active) {
      int err;
      int num_gids = MAX_ALBUMS;
      groupid_t gids[MAX_ALBUMS];
      err = nlkup_lookup_collection(collections[i].name, &num_gids, gids);
      g_assert(!err);
      for (j=0; j < num_gids; j++) {
	printf("adding gid: %lld\n", gids[j]);
	diamond_gid_list.gids[diamond_gid_list.num_gids++] = gids[j];
      }
    }
  }
}

static ls_search_handle_t generic_search (char *filter_spec_name) {
  ls_search_handle_t diamond_handle;
  int f1, f2;
  int err;

  char buf[1];

  diamond_init();

  diamond_handle = ls_init_search();

  err = ls_set_searchlist(diamond_handle, 1, diamond_gid_list.gids);
  g_assert(!err);

  // append our stuff
  f1 = g_open(FATFIND_FILTERDIR "/rgb-filter.txt", O_RDONLY);
  if (f1 == -1) {
    perror("can't open " FATFIND_FILTERDIR "/rgb-filter.txt");
    return NULL;
  }

  f2 = g_open(filter_spec_name, O_WRONLY | O_APPEND);
  if (f2 == -1) {
    printf("can't open %s", filter_spec_name);
    perror("");
    return NULL;
  }

  while (read(f1, buf, 1) > 0) {
    write(f2, buf, 1);   // PERF
  }

  close(f1);
  close(f2);

  err = ls_set_searchlet(diamond_handle, DEV_ISA_IA32,
			 FATFIND_FILTERDIR "/fil_rgb.so",
			 filter_spec_name);
  g_assert(!err);


  return diamond_handle;
}


gboolean diamond_result_callback(gpointer g_data) {
  // this gets 0 or 1 result from diamond and puts it into the
  // icon view

  ls_obj_handle_t obj;
  void *data;
  char *name;
  void *cookie;
  int len;
  int err;
  int w;
  int h;
  GdkPixbuf *pix, *pix2;

  float p_aspect;

  int i;

  GList *clist = NULL;
  gchar *title;

  GtkTreeIter iter;


  // get handle
  ls_search_handle_t dr = (ls_search_handle_t) g_data;

  err = ls_next_object(dr, &obj, LSEARCH_NO_BLOCK);

  // XXX should be able to use select()
  if (err == EWOULDBLOCK) {
    // no results right now
    return TRUE;
  } else if (err) {
    // no more results
    ls_abort_search(dr);
    return FALSE;
  }

  printf("got object: %p\n", obj);

  err = lf_ref_attr(obj, "circle-data", &len, &data);
  g_assert(!err);

  // XXX
  for (i = 0; i < len / sizeof(float); i += 3) {
    float *p = ((float *) data) + i;
    circle_type *c = g_malloc(sizeof(circle_type));

    c->x = p[0];
    c->y = p[1];
    c->r = p[2];

    clist = g_list_prepend(clist, c);
  }

  // text
  title = g_strdup_printf("%d items", g_list_length(clist));

  // thumbnail
  err = lf_ref_attr(obj, "_cols.int", &len, &data);
  g_assert(!err);
  w = *((int *) data);

  err = lf_ref_attr(obj, "_rows.int", &len, &data);
  g_assert(!err);
  h = *((int *) data);

  err = lf_ref_attr(obj, "_rgb_image.rgbimage", &len, &data);
  g_assert(!err);

  printf(" img %dx%d (%d bytes)\n", w, h, len);

  pix = gdk_pixbuf_new_from_data(data, GDK_COLORSPACE_RGB,
				 TRUE, 8, w, h, w*4, NULL, NULL);
  // draw into thumbnail?
  p_aspect = (float) w / (float) h;
  if (p_aspect < 1) {
    /* then calculate width from height */
    h = 150;
    w = h * p_aspect;
  } else {
    /* else calculate height from width */
    w = 150;
    h = w / p_aspect;
  }
  pix2 = gdk_pixbuf_scale_simple(pix, w, h, GDK_INTERP_BILINEAR);



  gtk_list_store_append(found_items, &iter);
  gtk_list_store_set(found_items, &iter,
		     0, pix2,
		     1, title,
		     2, clist,
		     -1);

  g_object_unref(pix);
  g_object_unref(pix2);

  //  err = lf_first_attr(obj, &name, &len, &data, &cookie);
  //  while (!err) {
  //    printf(" attr name: %s, length: %d\n", name, len);
  //    err = lf_next_attr(obj, &name, &len, &data, &cookie);
  //  }

  err = ls_release_object(dr, obj);
  g_assert(!err);

  return TRUE;
}


ls_search_handle_t diamond_circle_search(int dp, int minDist,
					 int blur, int accumulatorThresh,
					 int minRadius, int maxRadius,
					 int radiusStep, int cannyThreshold) {
  ls_search_handle_t dr;
{'fraction_non_alphanumeric': 0.15286343612334802, 'fraction_numerical': 0.01090308370044053, 'mean_word_length': 3.1352459016393444, 'pattern_counts': {'":': 0, '<': 22, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6852316', 'n_tokens_mistral': 3903, 'n_tokens_neox': 3622, 'n_words': 831}