Go back to Richel Bilderbeek's homepage.

Go back to Richel Bilderbeek's C++ page.

 

 

 

 

 

(C++) ThresholdFilterer

 

ThresholdFilterer is a tool to perform threshold filter operations.

 

 

 

 

 

Downloads

 

Technical facts

 

Operating system(s) or programming environment(s)

IDE(s):

Project type:

C++ standard:

Compiler(s):

Libraries used:

 

 

 

 

 

Qt project file: ./ToolThresholdFilterer/ToolThresholdFiltererConsole.pro

 

include(../../ConsoleApplication.pri)

include(../../Libraries/Boost.pri)
include(../../Libraries/GeneralConsole.pri)


include(ToolThresholdFiltererConsole.pri)

SOURCES += main.cpp

 

 

 

 

 

Qt project file: ./ToolThresholdFilterer/ToolThresholdFiltererDesktop.pro

 

include(../../DesktopApplication.pri)
include(../../Libraries/Boost.pri)
include(../../Libraries/GeneralConsole.pri)
include(../../Libraries/GeneralDesktop.pri)

include(../../Classes/CppTrace/CppTrace.pri)
include(../../Tools/ToolThresholdFilterer/ToolThresholdFiltererDesktop.pri)

SOURCES += qtmain.cpp

 

 

 

 

 

./ToolThresholdFilterer/ToolThresholdFiltererConsole.pri

 

INCLUDEPATH += \
    ../../Tools/ToolThresholdFilterer

HEADERS += \
    ../../Tools/ToolThresholdFilterer/thresholdfilterermaindialog.h \
    ../../Tools/ToolThresholdFilterer/thresholdfilterermenudialog.h

SOURCES += \
    ../../Tools/ToolThresholdFilterer/thresholdfilterermaindialog.cpp \
    ../../Tools/ToolThresholdFilterer/thresholdfilterermenudialog.cpp

RESOURCES += \
    ../../Tools/ToolThresholdFilterer/ToolThresholdFilterer.qrc

 

 

 

 

 

./ToolThresholdFilterer/ToolThresholdFiltererDesktop.pri

 

include(../../Tools/ToolThresholdFilterer/ToolThresholdFiltererConsole.pri)

SOURCES += \
    ../../Tools/ToolThresholdFilterer/qtthresholdfilterermaindialog.cpp \
    ../../Tools/ToolThresholdFilterer/qtthresholdfilterermenudialog.cpp

HEADERS += \
    ../../Tools/ToolThresholdFilterer/qtthresholdfilterermaindialog.h \
    ../../Tools/ToolThresholdFilterer/qtthresholdfilterermenudialog.h

FORMS += \
    ../../Tools/ToolThresholdFilterer/qtthresholdfilterermenudialog.ui \
    ../../Tools/ToolThresholdFilterer/qtthresholdfilterermaindialog.ui

 

 

 

 

 

./ToolThresholdFilterer/ToolThresholdFiltererWebsite.pri

 

include(../../Tools/ToolThresholdFilterer/ToolThresholdFiltererConsole.pri)

 

 

 

 

 

./ToolThresholdFilterer/main.cpp

 

#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Weffc++"
#pragma GCC diagnostic ignored "-Wunused-local-typedefs"

#include "thresholdfilterermenudialog.h"
#pragma GCC diagnostic pop

int main(int argc, char *argv[])
{
  const std::vector<std::string> args {
    ribi::MenuDialog::ConvertArguments(argc,argv)
  };
  ribi::ThresholdFiltererMenuDialog d;
  return d.Execute(args);
}

 

 

 

 

 

./ToolThresholdFilterer/qtmain.cpp

 

#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Weffc++"
#pragma GCC diagnostic ignored "-Wunused-local-typedefs"
#include <QApplication>
#include "qtthresholdfilterermenudialog.h"
#pragma GCC diagnostic pop

int main(int argc, char *argv[])
{
  QApplication a(argc, argv);
  ribi::QtThresholdFiltererMenuDialog w;
  w.show();
  return a.exec();
}

 

 

 

 

 

./ToolThresholdFilterer/qtthresholdfilterermaindialog.h

 

#ifndef QTTHRESHOLDFILTERERMAINDIALOG_H
#define QTTHRESHOLDFILTERERMAINDIALOG_H

#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Weffc++"
#include "qthideandshowdialog.h"
#pragma GCC diagnostic pop

namespace Ui {
  class QtThresholdFiltererMainDialog;
}

struct QLabel;

namespace ribi {

class QtThresholdFiltererMainDialog : public QtHideAndShowDialog
{
    Q_OBJECT
    
public:
  explicit QtThresholdFiltererMainDialog(QWidget *parent = 0);
  QtThresholdFiltererMainDialog(const QtThresholdFiltererMainDialog&) = delete;
  QtThresholdFiltererMainDialog& operator=(const QtThresholdFiltererMainDialog&) = delete;
  ~QtThresholdFiltererMainDialog() noexcept;

private slots:
  void on_button_load_clicked();
  void on_button_save_clicked();

  void on_pixels_valueChanged(int value);

private:
  Ui::QtThresholdFiltererMainDialog *ui;

  QPixmap * m_source;
  QLabel * m_target;

  #ifndef NDEBUG
  static void Test() noexcept;
  #endif

};

} //~namespace ribi

#endif // QTTHRESHOLDFILTERERMAINDIALOG_H

 

 

 

 

 

./ToolThresholdFilterer/qtthresholdfilterermaindialog.cpp

 

#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Weffc++"
#pragma GCC diagnostic ignored "-Wunused-local-typedefs"
#include "qtthresholdfilterermaindialog.h"

#include <boost/lexical_cast.hpp>
#include <boost/math/constants/constants.hpp>
#include <QFileDialog>
#include <QLabel>

#include "thresholdfilterermaindialog.h"
#include "testtimer.h"
#include "trace.h"
#include "ui_qtthresholdfilterermaindialog.h"
#pragma GCC diagnostic pop

ribi::QtThresholdFiltererMainDialog::QtThresholdFiltererMainDialog(QWidget *parent)
  : QtHideAndShowDialog(parent),
    ui(new Ui::QtThresholdFiltererMainDialog),
    m_source{nullptr},
    m_target{nullptr}
{
  #ifndef NDEBUG
  Test();
  #endif
  ui->setupUi(this);

}

ribi::QtThresholdFiltererMainDialog::~QtThresholdFiltererMainDialog() noexcept
{
  delete ui;
}

void ribi::QtThresholdFiltererMainDialog::on_button_load_clicked()
{
  const std::string filename {
    QFileDialog::getOpenFileName(0,"Please select a file").toStdString()
  };
  if (filename.empty()) return;

  //Check if the pixmap is valid
  {
    const QPixmap pixmap(filename.c_str());
    if (pixmap.isNull()) return;
  }
  //Refresh source
  if (m_source)
  {
    delete m_source;
    m_source = nullptr;
  }
  assert(!m_source);
  m_source = new QPixmap(filename.c_str());
  assert(m_source);
  assert(!m_source->isNull());

  //Refresh target
  {
    if (ui->contents->layout())
    {
      delete m_target;
      m_target = nullptr;
      delete ui->contents->layout();
    }
    assert(!ui->contents->layout());

    QVBoxLayout * const layout = new QVBoxLayout;
    ui->contents->setLayout(layout);
    m_target = new QLabel;
    layout->addWidget(m_target);

    const int pixel_size { ui->pixels->value() };

    const QPixmap target {
      ThresholdFiltererMainDialog::DoThresholdFiltering(*m_source,pixel_size)
    };
    assert(!target.isNull());
    m_target->setPixmap(target);
  }
}

void ribi::QtThresholdFiltererMainDialog::on_button_save_clicked()
{
  const std::string filename {
    QFileDialog::getSaveFileName(0,"Please name the target file").toStdString()
  };
  if (filename.empty()) return;

  m_target->pixmap()->save(filename.c_str());
}

#ifndef NDEBUG
void ribi::QtThresholdFiltererMainDialog::Test() noexcept
{
  {
    static bool is_tested{false};
    if (is_tested) return;
    is_tested = true;
  }
  const TestTimer test_timer(__func__,__FILE__,1.0);
  QPixmap source(":/thresholdfilterer/images/ToolThresholdFiltererTest.png");
  assert(!source.isNull());
  assert(source.width() > 0);
  assert(source.height() > 0);
  const int max = source.width() + 1; //Be nasty
  for (int pixel_size = 1; pixel_size != max; ++pixel_size)
  {
    const QPixmap target {
      ThresholdFiltererMainDialog::DoThresholdFiltering(source,pixel_size)
    };
    assert(!target.isNull());
    assert(target.width() > 0);
    assert(target.height() > 0);
  }
}
#endif

void ribi::QtThresholdFiltererMainDialog::on_pixels_valueChanged(int value)
{
  const int pixel_size { value };

  const QPixmap target {
    ThresholdFiltererMainDialog::DoThresholdFiltering(*m_source,pixel_size)
  };
  assert(!target.isNull());
  m_target->setPixmap(target);
}

 

 

 

 

 

./ToolThresholdFilterer/qtthresholdfilterermenudialog.h

 

#ifndef QTTHRESHOLDFILTERERMENUDIALOG_H
#define QTTHRESHOLDFILTERERMENUDIALOG_H

#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Weffc++"
#include "qthideandshowdialog.h"
#pragma GCC diagnostic pop

namespace Ui {
  class QtThresholdFiltererMenuDialog;
}

namespace ribi {

class QtThresholdFiltererMenuDialog : public QtHideAndShowDialog
{
  Q_OBJECT

public:
  explicit QtThresholdFiltererMenuDialog(QWidget *parent = 0);
  QtThresholdFiltererMenuDialog(const QtThresholdFiltererMenuDialog&) = delete;
  QtThresholdFiltererMenuDialog& operator=(const QtThresholdFiltererMenuDialog&) = delete;
  ~QtThresholdFiltererMenuDialog() noexcept;

private slots:
  void on_button_about_clicked();
  void on_button_quit_clicked();
  void on_button_start_clicked();

private:
  Ui::QtThresholdFiltererMenuDialog *ui;

  #ifndef NDEBUG
  static void Test() noexcept;
  #endif
};

} //~namespace ribi

#endif // QTTHRESHOLDFILTERERMENUDIALOG_H

 

 

 

 

 

./ToolThresholdFilterer/qtthresholdfilterermenudialog.cpp

 

#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Weffc++"
#include "qtthresholdfilterermenudialog.h"

#include <cassert>

#include "thresholdfilterermenudialog.h"
#include "qtaboutdialog.h"
#include "qtthresholdfilterermaindialog.h"
#include "testtimer.h"
#include "trace.h"
#include "ui_qtthresholdfilterermenudialog.h"
#pragma GCC diagnostic pop

ribi::QtThresholdFiltererMenuDialog::QtThresholdFiltererMenuDialog(QWidget *parent) :
  QtHideAndShowDialog(parent),
  ui(new Ui::QtThresholdFiltererMenuDialog)
{
  #ifndef NDEBUG
  Test();
  #endif
  ui->setupUi(this);
}

ribi::QtThresholdFiltererMenuDialog::~QtThresholdFiltererMenuDialog() noexcept
{
  delete ui;
}

void ribi::QtThresholdFiltererMenuDialog::on_button_start_clicked()
{
  QtThresholdFiltererMainDialog d;
  this->ShowChild(&d);
}

void ribi::QtThresholdFiltererMenuDialog::on_button_about_clicked()
{
  QtAboutDialog d(ThresholdFiltererMenuDialog().GetAbout());
  this->ShowChild(&d);
}

void ribi::QtThresholdFiltererMenuDialog::on_button_quit_clicked()
{
  this->close();
}

#ifndef NDEBUG
void ribi::QtThresholdFiltererMenuDialog::Test() noexcept
{
  {
    static bool is_tested{false};
    if (is_tested) return;
    is_tested = true;
  }
  QtThresholdFiltererMainDialog();
  const TestTimer test_timer(__func__,__FILE__,1.0);
}
#endif

 

 

 

 

 

./ToolThresholdFilterer/thresholdfilterermaindialog.h

 

#ifndef PIXELATORMAINDIALOG_H
#define PIXELATORMAINDIALOG_H

#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Weffc++"
#pragma GCC diagnostic ignored "-Wunused-local-typedefs"
#include <vector>
#include <QPixmap>
#pragma GCC diagnostic pop

struct QImage;

namespace ribi {

struct ThresholdFiltererMainDialog
{
  static QPixmap DoThresholdFiltering(const QPixmap& source, const int threshold) noexcept;
};

} //~namespace ribi


#endif

 

 

 

 

 

./ToolThresholdFilterer/thresholdfilterermaindialog.cpp

 

#include "thresholdfilterermaindialog.h"

#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Weffc++"
#pragma GCC diagnostic ignored "-Wunused-local-typedefs"
#include <cassert>
#include <vector>
#include <boost/scoped_ptr.hpp>
#include <QImage>
#include "trace.h"
#pragma GCC diagnostic pop

QPixmap ribi::ThresholdFiltererMainDialog::DoThresholdFiltering(
  const QPixmap& source,
  const int threshold) noexcept
{
  assert(!source.isNull());
  QImage image { source.toImage() };

  assert(source.width() == image.width());
  assert(source.height() == image.height());

  const int width  = source.width();
  const int height = source.height();

  for (int y=0; y!=height; ++y)
  {
    for (int x=0; x!=width; ++x)
    {
      const QRgb p {
        image.pixel(x,y)
      };
      const int grey {
        (qRed(p) + qGreen(p) + qBlue(p)) / 3
      };
      const QRgb q = grey < threshold ? qRgb(0,0,0) : p;
      image.setPixel(x,y,q);
    }
  }
  QPixmap result { QPixmap::fromImage(image) };
  return result;
}

 

 

 

 

 

./ToolThresholdFilterer/thresholdfilterermenudialog.h

 

#ifndef THRESHOLDFILTERERMENUDIALOG_H
#define THRESHOLDFILTERERMENUDIALOG_H

#include "menudialog.h"

namespace ribi {

///GUI independent thresholdfilterer menu dialog
struct ThresholdFiltererMenuDialog final : public MenuDialog
{
  About GetAbout() const noexcept override;
  Help GetHelp() const noexcept override;
  boost::shared_ptr<const Program> GetProgram() const noexcept override;
  std::string GetVersion() const noexcept override;
  std::vector<std::string> GetVersionHistory() const noexcept override;

  private:
  int ExecuteSpecific(const std::vector<std::string>& argv) noexcept override;

  #ifndef NDEBUG
  static void Test() noexcept;
  #endif
};

} //~namespace ribi

#endif // THRESHOLDFILTERERMENUDIALOG_H

 

 

 

 

 

./ToolThresholdFilterer/thresholdfilterermenudialog.cpp

 

#include "thresholdfilterermenudialog.h"

#include <cassert>
#include <iostream>

#include "richelbilderbeekprogram.h"
#include "trace.h"
#include "testtimer.h"

int ribi::ThresholdFiltererMenuDialog::ExecuteSpecific(const std::vector<std::string>& argv) noexcept
{
  #ifndef NDEBUG
  Test();
  #endif
  const int argc = static_cast<int>(argv.size());
  if (argc == 1)
  {
    std::cout << GetHelp() << '\n';
    return 1;
  }
  assert(!"TODO");
  return 0;
}

ribi::About ribi::ThresholdFiltererMenuDialog::GetAbout() const noexcept
{
  About a(
    "Richel Bilderbeek",
    "ThresholdFilterer",
    "tool to pixelate image",
    "the 28th of November 2013",
    "2008-2015",
    "http://www.richelbilderbeek.nl/ToolThresholdFilterer.htm",
    GetVersion(),
    GetVersionHistory());
  //a.AddLibrary("ProFile version: " + QtCreatorProFile::GetVersion());
  return a;
}

ribi::Help ribi::ThresholdFiltererMenuDialog::GetHelp() const noexcept
{
  return ribi::Help(
    this->GetAbout().GetFileTitle(),
    this->GetAbout().GetFileDescription(),
    {

    },
    {

    }
  );
}

boost::shared_ptr<const ribi::Program> ribi::ThresholdFiltererMenuDialog::GetProgram() const noexcept
{
  boost::shared_ptr<const ribi::Program> p {
    new ribi::ProgramThresholdFilterer
  };
  assert(p);
  return p;
}

std::string ribi::ThresholdFiltererMenuDialog::GetVersion() const noexcept
{
  return "2.0";
}

std::vector<std::string> ribi::ThresholdFiltererMenuDialog::GetVersionHistory() const noexcept
{
  return {
    "2008-03-01: version 1.0: initial Windows-only version",
    "2013-11-28: version 2.0: port to Qt"
  };
}

#ifndef NDEBUG
void ribi::ThresholdFiltererMenuDialog::Test() noexcept
{
  {
    static bool is_tested{false};
    if (is_tested) return;
    is_tested = true;
  }
  const TestTimer test_timer(__func__,__FILE__,1.0);
}
#endif

 

 

 

 

 

Go back to Richel Bilderbeek's C++ page.

Go back to Richel Bilderbeek's homepage.

 

Valid XHTML 1.0 Strict

This page has been created by the tool CodeToHtml