SOL9 Sample: SolImageTuner

SOL9 2.0 Samples

1 Screenshot


2 Source code

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



// SOL9 2.0 
// 2012/07/10 Sample program to tune an image file by using SOL::ImageView class

#define COMMONCONTROLS_V6

#include <sol\ApplicationView.h>
#include <sol\Static.h>

#include <sol\ClientDC.h>
#include <sol\DropFiles.h>
#include <sol\FileDialog.h>
#include <sol\ImageView.h>
#include <sol\ValueSlider.h>

#include "resource.h"

namespace SOL {

class SolImageTuner :public ApplicationView {

private:
  FileDialog  fileDialog;
  _bstr_t     imageFileName;

  ImageView   imageView;
    
  Static   toneLabel;
  ComboBox toneChooser;

  ValueSlider contrastScaler;  
  int      contrast;

public:
  SolImageTuner(Application& applet, const TCHAR* caption, Args& args)
        :ApplicationView(applet, caption, 
      args.set(XmNexStyle,(ulong)WS_EX_ACCEPTFILES)
      .set(XmNstyle, WS_CLIPCHILDREN|WS_CLIPSIBLINGS)) 
  {
    //1 Create imageView.
    Args ar;
    ar.reset();
    ar.set(XmNexStyle,(ulong)WS_EX_ACCEPTFILES);
    ar.set(XmNscaling, ImageBox::StretchWindow);
    ar.set(XmNx, 4);
    ar.set(XmNy, 50);
    ar.set(XmNwidth, 720);
    ar.set(XmNheight, 480);
    imageView.create(this, _T(""), ar);
    /*
    imageView.addEventHandler(WM_DROPFILES, this, 
      (Handler)&SolImageTuner::dropFiles, NULL);
    */

    //2 Create toneChooser 
    const TCHAR* items[] = {_T("Brightness"), _T("Gray"),
            _T("LightGray"), _T("Sepia"), _T("Negative"), };
    ar.reset();
    ar.set(XmNitems, (ulong)items);
    ar.set(XmNitemCount, CountOf(items));
    ar.set(XmNstyle, CBS_DROPDOWNLIST);
    toneChooser.create(this, NULL, ar);

    toneChooser.setCurSel(0);
    toneChooser.addCallback(XmNselChangeCallback, this, 
        (Callback)&SolImageTuner::toneChanged, NULL);

    ar.reset();
    toneLabel.create(this, _T("Tone"), ar);

    //3 Create contrastScaler 
    ar.reset();
    ar.set(XmNstyle, WS_VISIBLE);
    ar.set(XmNsliderRange, 200);
    ar.set(XmNsliderTitle, _T("(%)"));
    ar.set(XmNsliderPosition, 100);
    contrastScaler.create(this, NULL, ar);
    contrast = 100;

    disableTunerBar();

    //4 Add eventHandlers to this applicationView
    addEventHandler(WM_VALUESLIDER_CHANGED, this, 
      (Handler)&SolImageTuner::contrastChanged, NULL);
    
    addEventHandler(WM_DROPFILES, this, 
      (Handler)&SolImageTuner::dropFiles, NULL);


    //5 Add menuCallbacks to this applicationView
    addCallback(XmNmenuCallback, IDM_NEW, this,
      (Callback)&SolImageTuner::clear, NULL);
    addCallback(XmNmenuCallback, IDM_OPEN, this,
      (Callback)&SolImageTuner::open, NULL);
    addCallback(XmNmenuCallback, IDM_SAVEAS, this,
      (Callback)&SolImageTuner::saveAs, NULL);

    addCallback(XmNmenuCallback, IDM_EXIT, this,
      (Callback)&SolImageTuner::exit, NULL);
  
    addCallback(XmNmenuCallback, IDM_VERSION, this,
      (Callback)&SolImageTuner::version, NULL);

    //Create a fileDialog
    ar.reset();
    ar.set(XmNfilter, 
      _T("Image File(*.bmp,*.jpg,*.png,*.tif,*.ico,*.gif)\0*.bmp;*.jpg;*.png;*.tif;*.ico;*.gif\0JPEG File*.jpg)\0*.jpg\0PNG File(*.png)\0*.png\0Tiff File(*.tif)\0*.tif\0Icon File(*.ico)\0*.ico;\0GIF File(*.gif)\0*.gif;\0"));

    fileDialog.create(this, _T(""), ar);
    
    restorePlacement();
  }

private:
  void  clear(Action& action)
  {
    if (imageView.getImage()) {
      int rc = MessageBox(getWindow(), _T("Are you sure to clean the current image?"), _T("SolImageTuner"),
        MB_OKCANCEL | MB_ICONQUESTION);
      
      if (rc == IDOK) {
        imageFileName = _T("");

        imageView.setImageFileName(_T(""));

        imageView.clear();
        imageView.invalidateAll();

        imageView.update();
        setText(_T("SolImageTuner"));
        toneChooser.setCurSel(0);
        showContrastSetter(true);

        disableTunerBar();
      }
    }
  }

private:
  void showContrastSetter(bool flag)
  {
    if (flag) {
      contrastScaler.show();
    } else {
      contrastScaler.hide();
    }
  }

private:
  void toneChanged(Action& action)
  {
    int n = toneChooser.getCurSel();
    if (n >= 0) {
      TCHAR* item = toneChooser.getText();
      TCHAR format[1024];
      if (n == 0) {
        showContrastSetter(true);
        String format1 = this->getContrast();
        _stprintf_s(format,  CountOf(format), _T("%s"), (const TCHAR*)format1);
      } else {
        showContrastSetter(false);
        _stprintf_s(format, CountOf(format), _T("%s"), item);
      }
      // tuneFormat, and returns the tuned bitmap.
      if (this->imageFileName.length() >0) {
        imageView.tuneImage(imageFileName, _bstr_t(format));
      }
    }
  }

private:
  void  saveAs(Action& action)
  {
    Args ar;
    
    TCHAR dir[MAX_PATH];
    memset(dir, (TCHAR)0, CountOf(dir));
    //Call getFileFolder method in SOL::ApplicationView.
    //This gets a previously select folder from a registry(profile of this application) for fileDialog
    if (restoreFileFolder(dir, CountOf(dir))) {
      ar.set(XmNdirectory, dir);
      fileDialog.setValues(ar);
    }
  
    if(fileDialog.save()){
      TCHAR* filename = fileDialog.getFileName();

      TCHAR* ftitle = fileDialog.getFileTitle();
    
      File file;
      if (file.isExistent(filename) == false) {
        //Call saveFileFolder method in SOL::ApplicationView.
        saveFileFolder(filename);
        imageView.saveAs(filename);
    
        TCHAR title[MAX_PATH];
        imageFileName = filename;
        _stprintf_s(title, CountOf(title), _T("%s - ImageViewer"), (const TCHAR*)imageFileName);
        setText(title);
      }
    }  
  }

private:
  String getContrast()
  {
    TCHAR string[128];
    _stprintf_s(string, CountOf(string), _T("Contrast:%02f"), (float)contrast/100.0);
    
    return String(string);
  }

private:
  long contrastChanged(Event& event)
  {
    this->contrast  = (int)event.getLParam();
    HWND tracker = (HWND)event.getWParam();
    if (imageFileName.length()>0) {
      if (tracker == this->contrastScaler) {

        String scale = getContrast();

        imageView.tuneImage(this->imageFileName, (const TCHAR*)scale);
      }
    }
    return 0L;
  }

private:
  void disableTunerBar()
  {
    toneLabel.disable();
    toneChooser.disable();
    contrastScaler.disable();
  }

private:
  void enableTunerBar()
  {
    toneLabel.enable();
    toneChooser.enable();
    contrastScaler.enable();
  }

private:
  long size(Event& event)
  {
    int width, height;
    event.getSize(width, height);
    
    ClientDC dc(this);
    int h = dc.getTextHeight();

    SIZE size;
    TCHAR* label = toneLabel.getText();

    dc.getTextExtent(label, _tcslen(label), &size);

    toneLabel.reshape(4, 10, size.cx, size.cy);
    toneChooser.reshape(4+size.cx+4, 8, 100, 300);

    contrastScaler.reshape(4+size.cx+4+100+10, 6, 180, h*2, False);

    //imageView.reshape(4, h*2+10, width - 250-10, height-h*2-2);

    return 0L;
  }

private:
  void drawImage(_bstr_t& imageFieName)
  {
    TCHAR title[MAX_PATH];
    //clear();
    try {
      if (imageFileName.length()>0) {

        imageView.setImageFileName(_T(""));
        imageView.clear();
        imageView.invalidateAll();

        imageView.update();

        String scale = getContrast();
        int cont = scale.toInteger();

        //_stprintf(format, _T("contrast:%f"), (float)cont/100.0);
        imageView.tuneImage(imageFileName, _bstr_t(scale));
        imageView.invalidateAll();
        imageView.update();

        _stprintf_s(title, CountOf(title), _T("%s - ImageViewer"), (const TCHAR*)imageFileName);
      
        setText(title);
      }
    } catch (...) {

    }
  }

private:
  //2012/04/16 EventHandler for WM_DROPFILES
  long dropFiles(Event& event) 
  {
    TCHAR fileName[1024];
    DropFiles drop((HDROP)event.getWParam());
    fileName[0] = ZERO;
    int num = drop.queryFile(0, fileName, CountOf(fileName));

    if(num > 0) {
      enableTunerBar();

      toneChooser.setCurSel(0);
      showContrastSetter(true);

      imageFileName = fileName;
      drawImage(imageFileName);
    }
    return 0;
  }

private:
  void  open(Action& action)
  {
    Args ar;
    
    TCHAR dir[MAX_PATH];
    memset(dir, (TCHAR)0, CountOf(dir));
    //Call getFileFolder method in SOL::ApplicationView.
    //This gets a previously select folder from a registry(profile of this application) for fileDialog
    if (restoreFileFolder(dir, CountOf(dir))) {
      ar.set(XmNdirectory, dir);
      fileDialog.setValues(ar);
    }
  
    if(fileDialog.open()){
      //TCHAR title[MAX_PATH];

      imageFileName = fileDialog.getFileName();
      TCHAR* filename = fileDialog.getFileName();

      TCHAR* ftitle = fileDialog.getFileTitle();
    
      File file;
      if (file.isExistent(filename)) {
        //Call saveFileFolder method in SOL::ApplicationView.
        saveFileFolder(filename);
        enableTunerBar();

        //Brightness
        toneChooser.setCurSel(0);
        showContrastSetter(true);

        drawImage(imageFileName);
      }
    }  
  }

private:
  void version(Action& action)
  {    
    showMessageDialog(_T("SolImageTuner 1.0"), 
      _T("ImageTuner sample program to tune an image file."));
  }
};

}



void  Main(int argc, TCHAR** argv)
{
  ModuleFileName module(argv[0]);
  const TCHAR* name = module.getFileName();
  try {
    Application applet(name, argc, argv);

    Args args;
    args.set(XmNbackground, (COLOR_BTNFACE +1));
    //args.set(XmNclassStyle, NO_REDRAW);
    SolImageTuner appview(applet, name, args);
    appview.realize();

    applet.run();

  } catch (Exception& ex) {
    caught(ex);
  } catch (...) {
    caught(UnknownException());
  }
}


Last modified: 1 Feb 2017

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