<commit_before>/*
 sample.c

 Copyright (c) 2011 AIST  All Rights Reserved.
 Eclipse Public License v1.0 (http://www.eclipse.org/legal/epl-v10.html)

 Written by <NAME> <<EMAIL>>

 $Date::                            $
*/

#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <time.h>

#include <unistd.h>
#include <sys/time.h>

#include <cv.h>
#include <highgui.h>

#include <capture.h>
#include "checker_data.h"
#include "detect_checker.h"

typedef enum
{
  METHOD_OPENCV
} method_t;

typedef struct tag_opt
{
  int    pattern_col;  /* x */
  int    pattern_row;  /* y */
  double pattern_size; /* length of a side of one quadrangle */

  method_t method;
  int ieee1394b_mode;

  FILE *fp;
} opt_t;

static void show_help (const char *prog_name);
static int get_grid (const char *str, int *row, int *col);
static void parse_opt (int argc, char **argv, opt_t *opt);
static void destroy (const int err_code, opt_t *opt);
static int proc (capture_t *cap, capture_frame_t *frames, opt_t *opt);

static void s_convert_frame_to_iplimage (capture_frame_t *frame, IplImage *ipl);
static checker_coord_t *s_detect_checker (IplImage *image, opt_t *opt);
static void s_draw_checker (IplImage *image, checker_coord_t *cc, opt_t *opt);

int
main (int argc, char **argv)
{
  capture_t cap = CAPTURE_INIT_VAL;
  capture_frame_t *frames = NULL;
  int status;

  opt_t opt;

  /* parse options */
  parse_opt (argc, argv, &opt);

  /* setup a camera system */
  status = capture_init (&cap, 1);
  if (status != CAPTURE_SUCCESS)
    {
      fprintf (stderr, "error in capture_init()\n");
      goto on_capture_init_error;
    }

  cap.prefer_bmode = opt.ieee1394b_mode; /* use 1394b mode if the cameras support it */
  status = capture_setup (&cap, "ieee1394board.0");
  if (status != CAPTURE_SUCCESS)
    {
      fprintf (stderr, "error in capture_setup()\n");
      goto on_capture_setup_error;
    }

  status = capture_create_frames (&cap, &frames);
  if (status != CAPTURE_SUCCESS)
    {
      fprintf (stderr, "error in capture_create_frames()\n");
      goto on_capture_create_frames_error;
    }

  /* some image processing */
  if (cap.num_active > 0)
    {
      proc (&cap, frames, &opt);
    }


  /* clean up */
  capture_destroy_frames (&cap, &frames);
  capture_final (&cap);

  
  destroy (EXIT_SUCCESS, &opt); /* exit() will be called inside of this function */


  /* error handling */
 on_capture_create_frames_error:
  capture_final (&cap);
 on_capture_setup_error:
  ;
 on_capture_init_error:

  return EXIT_FAILURE;
}

static void
show_help (const char *prog_name)
{
  printf ("usage: %s [-o <filename>] [-s <size>] [-g <row>x<col>] [-l]\n", prog_name);

  printf ("\n");

  printf ("<filename> :\t");
  printf ("file name of output file\n");

  printf ("\n");

  printf ("<size>     :\t");
  printf ("pattern size [mm] (default: 20)\n");

  printf ("<row>x<col>:\t");
  printf ("nunber of points in each row/col (default: 8x8)\n");

  printf ("\n");

  printf ("-l         :\t");
  printf ("use IEEE 1394 legacy mode (default: IEEE 1394b mode)\n");

  printf ("\n");
}

static int
get_grid (const char *str, int *row, int *col)
{
  int i, n, r = -1, c = -1;
  char buf[10];
  
  buf[sizeof (buf) - 1] = '\0';

  /* read row element */
  i = 0;
  while (i < sizeof (buf) - 1 && isdigit (str[i]))
    {
      buf[i] = str[i];
      ++i;
    }
  buf[i] = '\0';

  if (i < 1)<commit_msg>added help messages about keyboard operation.<commit_after>/*
 sample.c

 Copyright (c) 2011 AIST  All Rights Reserved.
 Eclipse Public License v1.0 (http://www.eclipse.org/legal/epl-v10.html)

 Written by <NAME> <<EMAIL>>

 $Date::                            $
*/

#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <time.h>

#include <unistd.h>
#include <sys/time.h>

#include <cv.h>
#include <highgui.h>

#include <capture.h>
#include "checker_data.h"
#include "detect_checker.h"

typedef enum
{
  METHOD_OPENCV
} method_t;

typedef struct tag_opt
{
  int    pattern_col;  /* x */
  int    pattern_row;  /* y */
  double pattern_size; /* length of a side of one quadrangle */

  method_t method;
  int ieee1394b_mode;

  FILE *fp;
} opt_t;

static void show_help (const char *prog_name);
static int get_grid (const char *str, int *row, int *col);
static void parse_opt (int argc, char **argv, opt_t *opt);
static void destroy (const int err_code, opt_t *opt);
static int proc (capture_t *cap, capture_frame_t *frames, opt_t *opt);

static void s_convert_frame_to_iplimage (capture_frame_t *frame, IplImage *ipl);
static checker_coord_t *s_detect_checker (IplImage *image, opt_t *opt);
static void s_draw_checker (IplImage *image, checker_coord_t *cc, opt_t *opt);

int
main (int argc, char **argv)
{
  capture_t cap = CAPTURE_INIT_VAL;
  capture_frame_t *frames = NULL;
  int status;

  opt_t opt;

  /* parse options */
  parse_opt (argc, argv, &opt);

  /* setup a camera system */
  status = capture_init (&cap, 1);
  if (status != CAPTURE_SUCCESS)
    {
      fprintf (stderr, "error in capture_init()\n");
      goto on_capture_init_error;
    }

  cap.prefer_bmode = opt.ieee1394b_mode; /* use 1394b mode if the cameras support it */
  status = capture_setup (&cap, "ieee1394board.0");
  if (status != CAPTURE_SUCCESS)
    {
      fprintf (stderr, "error in capture_setup()\n");
      goto on_capture_setup_error;
    }

  status = capture_create_frames (&cap, &frames);
  if (status != CAPTURE_SUCCESS)
    {
      fprintf (stderr, "error in capture_create_frames()\n");
      goto on_capture_create_frames_error;
    }

  /* some image processing */
  if (cap.num_active > 0)
    {
      proc (&cap, frames, &opt);
    }


  /* clean up */
  capture_destroy_frames (&cap, &frames);
  capture_final (&cap);

  
  destroy (EXIT_SUCCESS, &opt); /* exit() will be called inside of this function */


  /* error handling */
 on_capture_create_frames_error:
  capture_final (&cap);
 on_capture_setup_error:
  ;
 on_capture_init_error:

  return EXIT_FAILURE;
}

static void
show_help (const char *prog_name)
{
  printf ("usage: %s [-o <filename>] [-s <size>] [-g <row>x<col>] [-l]\n", prog_name);

  printf ("\n");

  printf ("<filename> :\t");
  printf ("file name of output file\n");

  printf ("\n");

  printf ("<size>     :\t");
  printf ("pattern size [mm] (default: 20)\n");

  printf ("<row>x<col>:\t");
  printf ("nunber of points in each row/col (default: 8x8)\n");

  printf ("\n");

  printf ("-l         :\t");
  printf ("use IEEE 1394 legacy mode (default: IEEE 1394b mode)\n");

  printf ("\n");

  printf ("--- Keyboard operations ---\n");
  printf ("<mode change>\n");
  printf ("l\tlive mode; show captured images.\n");
  printf ("c\tchessboard detection mode; search a chessboard pattern using OpenCV.\n");
  printf ("q\tterminate this program.\n");

  printf ("\n");

  printf ("<in a detection mode>\n");
  printf ("[ret]\t\tadd current detection result to a queue.\n");
  printf ("W (shift-w)\twrite the queue to stdout or a specified file (-o <filename>).\n");

  printf ("\n");
}

static int
get_grid (const char *str, int *row, int *col)
{
  int i, n, r = -1, c = -1;
  char buf[10];
  
  buf[sizeof (buf) - 1] = '\0';

  /* read row element */
  i = 0;
  while (i < sizeof (buf) - 1 && isdigit (str[i]))
    {
      buf[i] = str[i];
      ++i;
    }
  buf[i] = '\0';

  if (i < 1)
{'fraction_non_alphanumeric': 0.15386713954440048, 'fraction_numerical': 0.013367889783112808, 'mean_word_length': 3.2627906976744185, 'pattern_counts': {'":': 0, '<': 50, '<?xml version=': 0, '>': 48, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '2746480', 'n_tokens_mistral': 2811, 'n_tokens_neox': 2644, 'n_words': 781}