Ejemplo n.º 1
0
NOCONSOLE_MAIN_QTHREAD(int, argc, char**, argv) {
#else
  MAIN_QTHREAD(int, argc, char**, argv) {
#endif /* NOCONSOLE */
#else
    int main(int argc, char **argv) {
      ERROR_MSG("QT not found, install and recompile.");
#endif /* __GUI__ */
      try {
        if (!parse_args(argc, argv))
          return -1;
        // variables
        range.push_back(-1.0); // default range min
        range.push_back(1.0); // default range max
        string conf_fname, diff_fname;
        // show mat images
        list<string> *argmats = new list<string>();
        list<string>::iterator i;
        for (int i = 1; i < argc; ++i) {
          // check for options
          try {
            if (!strcmp(argv[i], "-conf")) {
              ++i; if (i >= argc) throw 0;
              conf_fname = argv[i];
            } else if (!strcmp(argv[i], "-diff")) {
              ++i; if (i >= argc) throw 0;
              diff_fname = argv[i];
            } else if (!strcmp(argv[i], "-zoom")) {
              ++i; if (i >= argc) throw 0;
              zoom = (float) atof(argv[i]);
            } else if (!strcmp(argv[i], "-maxwidth")) {
              ++i; if (i >= argc) throw 0;
              maxwidth = (int) atoi(argv[i]);
            } else if (!strcmp(argv[i], "-video")) {
              video = true;
            } else if (!strcmp(argv[i], "-print")) {
              print = true;
            } else if (!strcmp(argv[i], "-interleaved")) {
              interleaved = true;
            } else if (!strcmp(argv[i], "-save_individually")) {
              save_individually = true;
            } else if (!strcmp(argv[i], "-filename")) {
              show_filename = true;
            } else if (!strcmp(argv[i], "-range")) {
              range.clear();
              ++i; if (i >= argc) throw 0;
              string s = argv[i];
              int k = 0;
              while (s.size()) {
                uint j;
                for (j = 0; j < s.size(); ++j)
                  if (s[j] == ',')
                    break ;
                string s0 = s.substr(0, j);
                if (j >= s.size())
                  s = "";
                else
                  s = s.substr(j + 1, s.size());
                range.push_back(atof(s0.c_str()));
                k++;
              }
              cout << "Fixing input range to " << range[0] << " .. "
                   << range[1] << endl;
              fixed_range = true;
            }
            // enqueue file names
            else {
              cout << argv[i] << endl;
              argmats->push_back(argv[i]);
            }
          } catch (int err) {
            cerr << "input error: ";
            switch (err) {
              case 0: cerr << "expecting string after " << argv[i-1]; break;
              case 1: cerr << "expecting integer after " << argv[i-1]; break;
              case 2: cerr << "unknown parameter " << argv[i-1]; break;
              case 3: cerr << "unknown channel mode " << argv[i-1]; break;
              default: cerr << "undefined error";
            }
            cerr << endl << endl;
            return false;
          }
        }

        // load configuration in conf mode
        if (conf_fname.size() > 0) {
          conf = new configuration(conf_fname);
          if (!conf->exists("root2")) {
            string dir = dirname(conf_fname.c_str());
            cout << "Looking for trained files in: " << dir << endl;
            conf->set("root2", dir.c_str());
            conf->resolve();
          }
          // enable auto range by default in conf mode
          if (!fixed_range)
            autorange = true;
        }
        // diff mode, just print matrices differences
        if (!diff_fname.empty()) {
          i = argmats->begin();
          idx<double> m1 = load_matrix<double>((*i).c_str());
          idx<double> m2 = load_matrix<double>(diff_fname);
          if (!m1.same_dim(m2.get_idxdim())) {
            eblerror("cannot compare matrices of different dimensions " << m1
                     << " and " << m2);
            return -1;
          }
          idx_sub(m1, m2);
          double sum = idx_sum(m1);
          cout << "sum(m1 - m2) = " << sum << endl;
          return 0;
        }

        // display first matrix/image
        i = argmats->begin();
        if (!video) {
          if (!load_display(i, true, argmats)) {
            ERROR_MSG("failed to load image(s)");
            return -1;
          }
        }
        // explore working directory for more images only if a
        // single file was passed
        if (argmats->size() == 1)
          explore = true;

#ifdef __GUI__
#ifdef __BOOST__
        // list all other mat files in image directory
        string dir = argv[1];
        string imgname, tmpname;
        size_t pos = dir.find_last_of('/');
        if (pos == string::npos) {
          imgname = dir;
          dir = "./";
        } else { // it contains a directory
          imgname = dir.substr(pos + 1, dir.size() - pos + 1);
          dir = dir.substr(0, pos);
        }
        list<string> *mats = argmats;
        if (explore)
          mats = find_fullfiles(dir, IMAGE_PATTERN_MAT,
                                NULL, true, false);
        // video mode
        if (video) {
          string out;
          out << "video_" << tstamp();
          mkdir_full(out);
          uint j = 0;
          for (i = mats->begin(); i != mats->end(); ++i, ++j) {
            cout << "video mode: displaying " << *i << endl;
            if (!load_display(i, true, mats)) {
              ERROR_MSG("failed to load image(s)");
              return -1;
            }
            millisleep(3000);
            ostringstream fname;
            fname << out << "/" << setfill('0') << setw(4) << j << "_"
                  << ebl::basename(i->c_str()) << ".png";
            save_window(fname.str().c_str());
            cout << "video mode: saved " << fname.str() << endl;
          }
        } else { // interactive mode
          if ((mats) && (mats->size() >= 1)) {
            // find current position in this list
            for (i = mats->begin(); i != mats->end(); ++i) {
              tmpname = ebl::basename(i->c_str());
              if (!imgname.compare(tmpname))
                break ;
            }
            if (i == mats->end())
              i = mats->begin();
            // loop and wait for key pressed
            while (1) {
              millisleep(50);
              int key = gui.pop_key_pressed();
              // next/previous images only if not everuything is already displayed
              if (mats->size() > nh * nw) {
                if ((key == Qt::Key_Space) || (key == Qt::Key_Right)) {
                  // show next image
                  for (uint k = 0; k < nw * nh; ++k) {
                    i++;
                    if (i == mats->end()) {
                      i = mats->begin();
                    }
                  }
                  load_display(i, true, mats);
                } else if ((key == Qt::Key_Backspace) || (key == Qt::Key_Left)) {
                  // show previous image
                  for (uint k = 0; k < nw * nh; ++k) {
                    if (i == mats->begin())
                      i = mats->end();
                    i--;
                  }
                  load_display(i, true, mats);
                }
              }
              if (key == Qt::Key_I) {
                // show info
                show_info = !show_info;
                if (show_info)
                  show_help = false;
                load_display(i, false, mats);
              } else if (key == Qt::Key_A) {
                // enable autorange
                autorange = !autorange;
                if (autorange)
                  cout << "Enabling automatic input range." << endl;
                else
                  cout << "Disabling automatic input range." << endl;
                load_display(i, false, mats);
              } else if (key == Qt::Key_H) {
                // show help
                show_help = !show_help;
                if (show_help)
                  show_info = false;
                load_display(i, false, mats);
              } else if (key == Qt::Key_Y) {
                // increase number of images shown on height axis
                if (nh * nw < mats->size())
                  nh++;
                load_display(i, false, mats);
              } else if (key == Qt::Key_T) {
                // decrease number of images shown on height axis
                nh = (std::max)((uint) 1, nh - 1);
                load_display(i, false, mats);
              } else if (key == Qt::Key_X) {
                // increase number of images shown on width axis
                if (nh * nw < mats->size())
                  nw++;
                load_display(i, false, mats);
              } else if (key == Qt::Key_Z) {
                // decrease number of images shown on width axis
                nw = (std::max)((uint) 1, nw - 1);
                load_display(i, false, mats);
              } else if (key == Qt::Key_0) {
                // show only channel 0
                chans = 0;
                load_display(i, false, mats);
                cout << "Showing channel 0 only." << endl;
              } else if (key == Qt::Key_1) {
                // show only channel 1
                chans = 1;
                load_display(i, false, mats);
                cout << "Showing channel 1 only." << endl;
              } else if (key == Qt::Key_2) {
                // show only channel 2
                chans = 2;
                load_display(i, false, mats);
                cout << "Showing channel 2 only." << endl;
              } else if (key == Qt::Key_9) {
                // show all channels
                chans = -1;
                load_display(i, false, mats);
                cout << "Showing alls channel." << endl;
              }
            }
          }
        }
        // free objects
        if (mats) delete mats;
#endif /* __BOOST__ */
#endif /* __GUI__ */
        if (argmats) delete argmats;
        if (conf) delete conf;
      } catch(string &err) {
        ERROR_MSG(err.c_str());
      }
      millisleep(500); // TODO: this lets time for window to open, fix this issue
      return 0;
    }
Ejemplo n.º 2
0
Archivo: pilot.c Proyecto: jgraef/aNXT
int main(int argc,char *argv[]) {
  SDL_Event event;
  SDL_Surface *screen;
  SDL_Surface *bg,*left,*enter,*right,*exit,*display_surface;
  SDL_Rect rect_left = {
    .x = 51,
    .y = 172,
    .w = 27,
    .h = 31,
  };
  SDL_Rect rect_enter = {
    .x = 91,
    .y = 173,
    .w = 32,
    .h = 31,
  };
  SDL_Rect rect_right = {
    .x = 136,
    .y = 172,
    .w = 27,
    .h = 31,
  };
  SDL_Rect rect_exit = {
    .x = 91,
    .y = 216,
    .w = 31,
    .h = 20,
  };
  SDL_Rect rect_display = {
    .x = 57,
    .y = 62,
    .w = 100,
    .h = 64,
  };
  int c;
  char *name = NULL;
  int force = 0;

  while ((c = getopt(argc,argv,":hn:f"))!=-1) {
    switch (c) {
      case 'h':
        usage(argv[0],0);
        break;
      case 'f':
        force = 1;
        break;
      case 'n':
        name = optarg;
        break;
      case ':':
        fprintf(stderr,"Option -%c requires an operand\n",optopt);
        usage(argv[0],1);
        break;
      case '?':
        fprintf(stderr,"Unrecognized option: -%c\n", optopt);
        usage(argv[0],1);
        break;
    }
  }

  // init NXT
  nxt_t *nxt = nxt_open(name);
  if (nxt==NULL) {
    fprintf(stderr,"Could not find NXT\n");
    return 1;
  }
  if (nxt_get_connection_type(nxt)==NXT_CON_BT && !force) {
    fprintf(stderr,"Warning! Using NXT Pilot over Bluetooth can make trouble. Are you sure to continue (y/n)[n]: ");
    if (fgetc(stdin)!='y') {
      nxt_close(nxt);
      return 0;
    }
  }
  nxt_display_t *display = nxt_display_open(nxt);
  if (display==NULL) {
    fprintf(stderr,"Could not open display\n");
    nxt_close(nxt);
    return 1;
  }

  // init window
  if (SDL_Init(SDL_INIT_VIDEO)==-1) {
    fprintf(stderr,"Can't init SDL:  %s\n",SDL_GetError());
    return 1;
  }
  atexit(SDL_Quit);
  screen = SDL_SetVideoMode(215,322,32,SDL_HWSURFACE);
  if (screen==NULL) {
    fprintf(stderr,"Can't open window: %s\n",SDL_GetError());
    return 1;
  }
  SDL_WM_SetCaption("NXT Pilot","NXT Pilot");

  // load images
  bg = IMG_ReadXPMFromArray(pilot_bg_xpm);
  left = IMG_ReadXPMFromArray(pilot_left_xpm);
  enter = IMG_ReadXPMFromArray(pilot_enter_xpm);
  right = IMG_ReadXPMFromArray(pilot_right_xpm);
  exit = IMG_ReadXPMFromArray(pilot_exit_xpm);

  // display NXT
  SDL_BlitSurface(bg,NULL,screen,NULL);
  SDL_BlitSurface(left,NULL,screen,&rect_left);
  SDL_BlitSurface(enter,NULL,screen,&rect_enter);
  SDL_BlitSurface(right,NULL,screen,&rect_right);
  SDL_BlitSurface(exit,NULL,screen,&rect_exit);

  int done = 0;
  unsigned int pause = nxt_get_connection_type(nxt)==NXT_CON_BT?500:100;
  while (!done) {
    while (SDL_PollEvent(&event)) {
      switch(event.type) {
        case SDL_QUIT:
          done = 1;
          break;
        case SDL_KEYUP:
          if (event.key.keysym.sym==SDLK_ESCAPE) done = 1;
          else if (event.key.keysym.sym==SDLK_RETURN) nxt_set_button(nxt,NXT_UI_BUTTON_ENTER);
          else if (event.key.keysym.sym==SDLK_BACKSPACE) nxt_set_button(nxt,NXT_UI_BUTTON_EXIT);
          else if (event.key.keysym.sym==SDLK_LEFT) nxt_set_button(nxt,NXT_UI_BUTTON_LEFT);
          else if (event.key.keysym.sym==SDLK_RIGHT) nxt_set_button(nxt,NXT_UI_BUTTON_RIGHT);
          break;
        case SDL_MOUSEBUTTONUP:
          if (in_rect(rect_left,event.button)) nxt_set_button(nxt,NXT_UI_BUTTON_LEFT);
          else if (in_rect(rect_enter,event.button)) nxt_set_button(nxt,NXT_UI_BUTTON_ENTER);
          else if (in_rect(rect_right,event.button)) nxt_set_button(nxt,NXT_UI_BUTTON_RIGHT);
          else if (in_rect(rect_exit,event.button)) nxt_set_button(nxt,NXT_UI_BUTTON_EXIT);
          break;
      }
    }

    display_surface = load_display(display);
    if (display!=NULL) {
      SDL_BlitSurface(display_surface,NULL,screen,&rect_display);
      SDL_UpdateRect(screen,0,0,0,0);
      SDL_FreeSurface(display_surface);
    }

    SDL_Delay(pause);
  }

  // free images
  SDL_FreeSurface(bg);
  SDL_FreeSurface(left);
  SDL_FreeSurface(enter);
  SDL_FreeSurface(right);
  SDL_FreeSurface(exit);

  // close nxt
  int ret = nxt_error(nxt);
  nxt_display_flush(display,1);
  nxt_display_close(display);
  nxt_close(nxt);

  return ret;
}