SOL9 Sample: ProportionalImageViewer

SOL9 2.0 Samples

1 Screenshot





2 Source code

/*
 * ProportionalImageViewer.cpp 
 * Copyright (c) 2015 Antillia.com TOSHIYUKI ARAI. ALL RIGHTS RESERVED. 
 */


//2017/04/10

// This ProportionalImageViewer keeps the aspect ratio of cv::Mat image.

#define _CONSOLE_

#include <sol/FileDialog.h>
#include <sol/DropFiles.h>
#include <sol/ModuleFileName.h>
#include <sol/opencv/OpenCVApplicationView.h>

#include <sol/opencv/OpenCVImageView.h>


namespace SOL {

class MainView :public OpenCVApplicationView {

private:
  ////////////////////////////////////////////////////////////////////////////////////////
  //Inner class starts.
  class SimpleView :public OpenCVImageView {
  private:
    cv::Mat image;
    
    cv::Mat& getMat()
    {
      return image;
    }
    
    void display()
    {
      show(image);
    }
    
  public:
    SimpleView(View* parent, const TCHAR* name, Args& args)
    :OpenCVImageView(parent, name, args)
    {
      try {
        const char* filename = (const char*)args.get(XmNimageFileName);
        int imageLoadingFlag = args.get(XmNimageLoadingFlag);

        image = imread(filename, imageLoadingFlag);
        
      } catch (SOL::Exception ex) {
        caught(ex);
      }
    }
    
    ~SimpleView()
    {
    }
    
    void loadImage(const char* filename, int flag = CV_LOAD_IMAGE_COLOR)
    {
      try {
        image = readImage(filename, flag);
        postResizeRequest();
        refresh();
      } catch (Exception& ex) {
        caught(ex);
      }
    }
  };
  //Inner class ends.
  ////////////////////////////////////////////////////////////////////////////////////////

  SmartPtr<SimpleView>  view;
  FileDialog            filedlg;
  
  void resize(int w, int h)
  {
    if (view) {
      view -> reshape(0, 0, w, h);
    }
  }

  void openFile(const char* filename)
  {
    try {  
      view -> loadImage(filename);
      char title[MAX_PATH];
      sprintf_s(title, CountOf(title), "%s - ImageViewer.exe", filename);
      setText(title);
      
    } catch (Exception& ex) {
      caught(ex);
    }
  }
  
  void dropFiles(Action& action)
  {
    char fileName[MAX_PATH] = { 0 };
    DropFiles drop((HDROP)action.getWParam());
    //fileName[0] = ZERO;
    int num = drop.queryFile(0, fileName, CountOf(fileName));
    if(num > 0) {
      if (filedlg.isImageFileName(fileName)) {
        openFile(fileName);
        bringUp();
      } else {        
        bringUp(); //Activate and raise this view
        
        showErrorDialog("Invalid image filename", fileName,  MB_OK);
      }
    }    
  }

  void confirm(Action& action)
  {
    int rc = MessageBox(NULL, "Are you sure to close this window?", "Confirmation", 
                MB_OKCANCEL|MB_ICONEXCLAMATION);
    if (rc == IDOK) {
      exit(action);
    }
  }
  
public:
  MainView(OpenCVApplication& applet, const TCHAR* name, Args& args)
  :OpenCVApplicationView(applet, name, args)
  {
    try {
      Args ar;
      ar.set(XmNimageFileName, "..\\images\\flower.png");
      ar.set(XmNimageLoadingFlag, CV_LOAD_IMAGE_COLOR);
      view = new SimpleView(this, "cvwindow", ar); 
      view -> addCallback(XmNdropCallback, this,
        (Callback)&MainView::dropFiles, NULL);
      
      ar.reset();
      ar.set(XmNfilter, FileDialog::getImageFilesFilter());
      filedlg.create(this, "OpenFile", ar);
           
      addCallback(XmNmenuCallback, IDM_EXIT, this,
          (Callback)&MainView::confirm, NULL);
      
    } catch (Exception& ex) {
      caught(ex);
    }
  }

  ~MainView()
  {
  }
  
  void open(Action& action)
  {
    Args ar;
    
    char dir[MAX_PATH];
    memset(dir, (TCHAR)0, CountOf(dir));
    //Restore previously select folder from a registry(profile of this application) for fileDialog
    if (restoreFileFolder(dir, CountOf(dir))) {
      ar.set(XmNdirectory, dir);
      filedlg.setValues(ar);
    }
    
    try {    
      if(filedlg.open()) {
    
        const char* filename = filedlg.getFileName();
        saveFileFolder(filename);
        
        openFile(filename);
      }
    } catch (Exception& ex) {
      caught(ex);
    }
  }
};
}

//
void main(int argc, TCHAR** argv) 
{
  try {
    ModuleFileName module(argv[0]);
    
    const char*  name = module.getAppName();

    OpenCVApplication applet(name, argc, argv);

    Args args;
    args.set(XmNwidth,  640);
    args.set(XmNheight, 480);
    MainView view(applet, name, args);
    view.realize();

    applet.run();
    
  } catch (SOL::Exception& ex) {
    caught(ex);
  }
}


Last modified: 2 Dec. 2017

Copyright (c) 2017 Antillia.com ALL RIGHTS RESERVED.