Go back to Richel Bilderbeek's homepage.

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

 

 

 

 

 

(C++) TestAsciiArter source code version 4.3

 

Source code of TestAsciiArter version 4.3.

 

 

 

 

 

Technical facts

 

Application type(s)

Operating system(s) or programming environment(s)

IDE(s):

Project type:

C++ standard:

Compiler(s):

Libraries used:

 

 

 

 

 

Qt project file: ToolTestAsciiArterDesktop.pro

 

#-------------------------------------------------
#
# Project created by QtCreator 2011-03-22T16:16:52
#
#-------------------------------------------------
QT       += core gui
TARGET = ToolTestAsciiArterDesktop
TEMPLATE = app

INCLUDEPATH += \
  ../../Classes/CppAbout \
  ../../Classes/CppAsciiArter \
  ../../Classes/CppQtAboutDialog

SOURCES += qtmain.cpp \
  qttestasciiarterdialog.cpp \
  testasciiarterdialog.cpp \
  ../../Classes/CppAbout/about.cpp \
  ../../Classes/CppAsciiArter/asciiarter.cpp \
  ../../Classes/CppQtAboutDialog/qtaboutdialog.cpp

HEADERS  += \
  qttestasciiarterdialog.h \
  testasciiarterdialog.h \
  ../../Classes/CppAbout/about.h \
  ../../Classes/CppAsciiArter/asciiarter.h \
  ../../Classes/CppQtAboutDialog/qtaboutdialog.h

FORMS  += \
  qttestasciiarterdialog.ui \
  ../../Classes/CppQtAboutDialog/qtaboutdialog.ui

 

 

 

 

 

Qt project file: ToolTestAsciiArterWebsite.pro

 


#-------------------------------------------------
#
# Project created by QtCreator 2011-03-06T10:58:35
#
#-------------------------------------------------
#This application uses QImage for image conversion
QT       += core gui
LIBS += -lwt -lwthttp -lboost_program_options
QMAKE_CXXFLAGS += -DNDEBUG
TARGET = TestAsciiArterWebsite
CONFIG   += console
CONFIG   -= app_bundle
TEMPLATE = app

INCLUDEPATH += \
  ../../Classes/CppAbout \
  ../../Classes/CppAsciiArter \
  ../../Classes/CppWtAboutDialog \
  ../../Classes/CppWtAutoConfig

SOURCES +=\
  testasciiarterdialog.cpp \
  wtmain.cpp \
  wttestasciiarterdialog.cpp \
  ../../Classes/CppAsciiArter/asciiarter.cpp \
  ../../Classes/CppWtAboutDialog/wtaboutdialog.cpp \
    ../../Classes/CppAbout/about.cpp \
    ../../Classes/CppWtAutoConfig/wtautoconfig.cpp

HEADERS  += \
  testasciiarterdialog.h \
  wttestasciiarterdialog.h \
  ../../Classes/CppAsciiArter/asciiarter.h \
  ../../Classes/CppWtAboutDialog/wtaboutdialog.h \
  ../../Classes/CppAbout/about.h \
    ../../Classes/CppWtAutoConfig/wtautoconfig.h

RESOURCES += \
    ToolTestAsciiArter.qrc

 

 

 

 

 

qtmain.cpp

 

//---------------------------------------------------------------------------
/*
TestAsciiArter, tool to test the AsciiArter class
Copyright (C) 2006-2011 Richel Bilderbeek

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
//---------------------------------------------------------------------------
//From http://www.richelbilderbeek.nl/ToolTestAsciiArter.htm
//---------------------------------------------------------------------------
#include <QtGui/QApplication>
#include "qttestasciiarterdialog.h"
//---------------------------------------------------------------------------
int main(int argc, char *argv[])
{
  QApplication a(argc, argv);
  QtTestAsciiArterDialog w;
  w.show();
  return a.exec();
}
//---------------------------------------------------------------------------


 

 

 

 

 

qttestasciiarterdialog.cpp

 

//---------------------------------------------------------------------------
/*
TestAsciiArter, tool to test the AsciiArter class
Copyright (C) 2006-2011 Richel Bilderbeek

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
//---------------------------------------------------------------------------
//From http://www.richelbilderbeek.nl/ToolTestAsciiArter.htm
//---------------------------------------------------------------------------
#include <boost/foreach.hpp>
#include <boost/lexical_cast.hpp>
#include <boost/numeric/conversion/cast.hpp>
//---------------------------------------------------------------------------
#include <QDesktopWidget>
#include <QFileDialog>
#include <QImage>
#include <QPixmap>
//---------------------------------------------------------------------------
#include "asciiarter.h"
#include "testasciiarterdialog.h"
#include "qtaboutdialog.h"
#include "qttestasciiarterdialog.h"
#include "ui_qttestasciiarterdialog.h"
//---------------------------------------------------------------------------
QtTestAsciiArterDialog::QtTestAsciiArterDialog(QWidget *parent)
  : QDialog(parent, Qt::Window),
    ui(new Ui::QtTestAsciiArterDialog),
    m_dialog(new TestAsciiArterDialog)
{
  ui->setupUi(this);
  ui->edit_width->setText("80");
  on_edit_width_textChanged("80");


  //Put the dialog in the screen center
  const QRect screen = QApplication::desktop()->screenGeometry();
  this->move( screen.center() - this->rect().center() );
}
//---------------------------------------------------------------------------
QtTestAsciiArterDialog::~QtTestAsciiArterDialog()
{
  delete ui;
}
//---------------------------------------------------------------------------
void QtTestAsciiArterDialog::changeEvent(QEvent *e)
{
  QDialog::changeEvent(e);
  switch (e->type()) {
  case QEvent::LanguageChange:
    ui->retranslateUi(this);
    break;
  default:
    break;
  }
}
//---------------------------------------------------------------------------
void QtTestAsciiArterDialog::DrawAsciiArt()
{
  if (!m_dialog->CanConvert()) return;

  m_dialog->Convert();

  const std::vector<std::string>& v = m_dialog->GetAsciiArt();
  ui->text->clear();

  BOOST_FOREACH(const std::string& s,v)
  {
    ui->text->appendPlainText(s.c_str());
  }
}
//---------------------------------------------------------------------------
void QtTestAsciiArterDialog::on_button_load_clicked()
{
  QFileDialog d;
  const QString filename = d.getOpenFileName();

  if (!QFile::exists(filename))
  {
    return;
  }

  QImage p(filename);
  const std::vector<std::vector<double> > v
    = ConvertToGreyYx(&p);

  m_dialog->SetImage(v);
  DrawAsciiArt();
}
//---------------------------------------------------------------------------
//Returns a Y-X-ordered std::vector of greynesses.
const std::vector<std::vector<double> >
  QtTestAsciiArterDialog::ConvertToGreyYx(const QImage * const i)
{
  const int maxy = i->height();
  const int maxx = i->width();
  const int n_bytes = i->bytesPerLine() / maxx;

  std::vector<std::vector<double> > v;
  for (int y=0; y!=maxy; ++y)
  {
    v.push_back(std::vector<double>());
    const unsigned char * const line = i->scanLine(y);
    for (int x=0; x!=maxx; ++x)
    {
      int sum = 0;
      for (int byte=0; byte!=n_bytes; ++byte)
      {
        sum += line[(x * n_bytes) + byte];
      }
      const double greyness
        = (boost::numeric_cast<double>(sum)
        / boost::numeric_cast<double>(n_bytes))
        / 256.0;
      assert(greyness >= 0.0);
      assert(greyness <= 1.0);
      v.back().push_back(greyness);
    }
  }
  return v;
}
//---------------------------------------------------------------------------
void QtTestAsciiArterDialog::on_edit_width_textChanged(QString q)
{
  const std::string s = q.toStdString();
  try
  {
    boost::lexical_cast<int>(s);
  }
  catch (boost::bad_lexical_cast&)
  {
    return;
  }
  const int width = boost::lexical_cast<int>(s);
  if (width <= 5) return;
  this->m_dialog->SetWidth(width);
  DrawAsciiArt();

}
//---------------------------------------------------------------------------
void QtTestAsciiArterDialog::on_button_about_clicked()
{
  About a = TestAsciiArterDialog::GetAbout();
  QtAboutDialog d(a);
  d.exec();
}
//---------------------------------------------------------------------------

 

 

 

 

 

qttestasciiarterdialog.h

 

//---------------------------------------------------------------------------
/*
TestAsciiArter, tool to test the AsciiArter class
Copyright (C) 2006-2011 Richel Bilderbeek

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
//---------------------------------------------------------------------------
//From http://www.richelbilderbeek.nl/ToolTestAsciiArter.htm
//---------------------------------------------------------------------------
#ifndef QTTESTASCIIARTERDIALOG_H
#define QTTESTASCIIARTERDIALOG_H
//---------------------------------------------------------------------------
#include <QDialog>
//---------------------------------------------------------------------------
#include <boost/scoped_ptr.hpp>
//---------------------------------------------------------------------------
#include "testasciiarterdialog.h"
//---------------------------------------------------------------------------
namespace Ui {
  class QtTestAsciiArterDialog;
}
//---------------------------------------------------------------------------
struct QImage;
//---------------------------------------------------------------------------
class QtTestAsciiArterDialog : public QDialog
{
  Q_OBJECT

public:
  explicit QtTestAsciiArterDialog(QWidget *parent = 0);
  ~QtTestAsciiArterDialog();

protected:
  void changeEvent(QEvent *e);

private:
  Ui::QtTestAsciiArterDialog *ui;
  const boost::scoped_ptr<TestAsciiArterDialog> m_dialog;
  void DrawAsciiArt();

  static const std::vector<std::vector<double> >
    ConvertToGreyYx(const QImage * const i);

private slots:
  void on_button_about_clicked();
  void on_edit_width_textChanged(QString );
  void on_button_load_clicked();
};
//---------------------------------------------------------------------------
#endif // QTTESTASCIIARTERDIALOG_H

 

 

 

 

 

testasciiarterdialog.cpp

 

//---------------------------------------------------------------------------
/*
TestAsciiArter, tool to test the AsciiArter class
Copyright (C) 2006-2011 Richel Bilderbeek

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
//---------------------------------------------------------------------------
//From http://www.richelbilderbeek.nl/ToolTestAsciiArter.htm
//---------------------------------------------------------------------------
#include "asciiarter.h"
#include "testasciiarterdialog.h"
//---------------------------------------------------------------------------
TestAsciiArterDialog::TestAsciiArterDialog()
  : m_width(0),
    m_asciiarter(new AsciiArter)
{

}
//---------------------------------------------------------------------------
bool TestAsciiArterDialog::CanConvert() const
{
  return !m_image.empty() && m_width > 5;
}
//---------------------------------------------------------------------------
void TestAsciiArterDialog::Convert()
{
  m_asciiart = m_asciiarter->ImageToAscii(m_image,m_width);
}
//---------------------------------------------------------------------------
const About TestAsciiArterDialog::GetAbout()
{
  About a(
    "Richel Bilderbeek",
    "TestAsciiArter",
    "tool to test the AsciiArter class",
    "the 25th of April 2011",
    "2006-2011",
    "http://www.richelbilderbeek.nl/ToolTestAsciiArter.htm",
    GetVersion(),
    GetVersionHistory());
  a.AddLibrary("AsciiArter version: " + AsciiArter::GetVersion());
  return a;
}
//---------------------------------------------------------------------------
const std::string TestAsciiArterDialog::GetVersion()
{
  return "4.3";
}
//---------------------------------------------------------------------------
const std::vector<std::string> TestAsciiArterDialog::GetVersionHistory()
{
  std::vector<std::string> v;
  v.push_back("2006-12-13: Version 1.0: initial version (then called 'AsciiArter') programmed in C++ Builder");
  v.push_back("2006-12-16: Version 2.0: minor improvements");
  v.push_back("2008-06-21: Version 3.0: minor improvements");
  v.push_back("2011-03-23: Version 4.0: port to Qt Creator");
  v.push_back("2011-03-27: Version 4.1: added web application version");
  v.push_back("2011-04-04: Version 4.2: fix bug under the web application its Convert button");
  v.push_back("2011-04-25: Version 4.3: removed website version\'s Close button, use of Wt::WMenu in website version");
  return v;
}
//---------------------------------------------------------------------------
void TestAsciiArterDialog::SetImage(const std::vector<std::vector<double> >& image)
{
  m_image = image;
}
//---------------------------------------------------------------------------
void TestAsciiArterDialog::SetWidth(const int width)
{
  assert(width > 5);
  m_width = width;
}
//---------------------------------------------------------------------------


 

 

 

 

 

testasciiarterdialog.h

 

//---------------------------------------------------------------------------
/*
TestAsciiArter, tool to test the AsciiArter class
Copyright (C) 2006-2011 Richel Bilderbeek

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
//---------------------------------------------------------------------------
//From http://www.richelbilderbeek.nl/ToolTestAsciiArter.htm
//---------------------------------------------------------------------------
#ifndef TESTASCIIARTERDIALOG_H
#define TESTASCIIARTERDIALOG_H
//---------------------------------------------------------------------------
#include <string>
#include <vector>
//---------------------------------------------------------------------------
#include <boost/scoped_ptr.hpp>
//---------------------------------------------------------------------------
#include "about.h"
struct AsciiArter;
//---------------------------------------------------------------------------
struct TestAsciiArterDialog
{
  TestAsciiArterDialog();
  const std::vector<std::string>& GetAsciiArt() const { return m_asciiart; }
  bool CanConvert() const;
  void Convert();

  static const About GetAbout();
  static const std::string GetVersion();
  static const std::vector<std::string> GetVersionHistory();
  int GetWidth() const { return m_width; }

  void SetImage(const std::vector<std::vector<double> >& image);
  void SetWidth(const int width);

  private:
  std::vector<std::string> m_asciiart;
  std::vector<std::vector<double> > m_image;
  int m_width;

  const boost::scoped_ptr<AsciiArter> m_asciiarter;
};
//---------------------------------------------------------------------------
#endif // TESTASCIIARTERDIALOG_H

 

 

 

 

 

wtmain.cpp

 

//---------------------------------------------------------------------------
/*
TestAsciiArter, tool to test the AsciiArter class
Copyright (C) 2006-2011 Richel Bilderbeek

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
//---------------------------------------------------------------------------
//From http://www.richelbilderbeek.nl/ToolTestAsciiArter.htm
//---------------------------------------------------------------------------
#include <boost/program_options.hpp>
#include <boost/signals2.hpp>
#include <Wt/WApplication>
#include <Wt/WContainerWidget>
#include <Wt/WEnvironment>
#include <Wt/WPaintDevice>
#include <Wt/WPaintedWidget>
#include <Wt/WPainter>
#include <Wt/WPushButton>
//---------------------------------------------------------------------------
#include "wtautoconfig.h"
#include "wttestasciiarterdialog.h"
//---------------------------------------------------------------------------
#include <QFile>
//---------------------------------------------------------------------------
struct WtApplication : public Wt::WApplication
{
  WtApplication(const Wt::WEnvironment& env)
    : Wt::WApplication(env),
    m_dialog(new WtTestAsciiArterDialog)
  {
    this->setTitle("TestAsciiArter");
    this->useStyleSheet("wt.css");
    root()->addWidget(m_dialog);
  }
  private:
  WtTestAsciiArterDialog * const m_dialog;
};
//---------------------------------------------------------------------------
Wt::WApplication *createApplication(
  const Wt::WEnvironment& env)
{
  return new WtApplication(env);
}
//---------------------------------------------------------------------------
int main(int argc, char **argv)
{
  if (!QFile::exists("RichelbilderbeekNlBackground.png"))
  {
    QFile file(":/images/RichelbilderbeekNlBackground.png");
    file.copy("RichelbilderbeekNlBackground.png");
  }

  WtAutoConfig a(argc,argv,createApplication);
  WtAutoConfig::SaveDefaultStylesheet();
  return a.Run();
}
//---------------------------------------------------------------------------

 

 

 

 

 

wttestasciiarterdialog.cpp

 

//---------------------------------------------------------------------------
/*
TestAsciiArter, tool to test the AsciiArter class
Copyright (C) 2006-2011 Richel Bilderbeek

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
//---------------------------------------------------------------------------
//From http://www.richelbilderbeek.nl/ToolTestAsciiArter.htm
//---------------------------------------------------------------------------
#include <boost/filesystem.hpp>
#include <boost/foreach.hpp>
#include <boost/lexical_cast.hpp>
#include <boost/numeric/conversion/cast.hpp>
//---------------------------------------------------------------------------
#include <Wt/WBreak>
#include <Wt/WFileUpload>
#include <Wt/WImage>
#include <Wt/WLabel>
#include <Wt/WLineEdit>
#include <Wt/WMenu>
#include <Wt/WPushButton>
#include <Wt/WStackedWidget>
#include <Wt/WTextArea>
//---------------------------------------------------------------------------
#include <QtGui/QImage>
//---------------------------------------------------------------------------
#include "about.h"
#include "asciiarter.h"
#include "testasciiarterdialog.h"
#include "wtaboutdialog.h"
#include "wtautoconfig.h"
#include "wttestasciiarterdialog.h"
//---------------------------------------------------------------------------
//Enable debugging
#undef NDEBUG
#include <cassert>
//---------------------------------------------------------------------------
WtTestAsciiArterDialog::WtTestAsciiArterDialog()
  : m_dialog(new TestAsciiArterDialog),
    m_edit_width(0),
    m_text(0)
{
  this->setContentAlignment(Wt::AlignCenter);
  m_dialog->SetWidth(79);

  this->clear();
  //Title
  {
    Wt::WLabel * const title = new Wt::WLabel("TestAsciiArter");
    title->setStyleClass("title");
    this->addWidget(title);
  }
  this->addWidget(new Wt::WBreak);
  //Menu
  {
    Wt::WStackedWidget * const contents = new Wt::WStackedWidget;
    Wt::WMenu * const menu = new Wt::WMenu(contents,Wt::Horizontal);
    //Using CSS styleclass is the best (only?) way to display the menu well
    menu->setStyleClass("menu");
    {
      Wt::WMenuItem * const item = new Wt::WMenuItem(
        "Welcome",
        CreateNewWelcomeDialog());
      menu->addItem(item);
    }
    {
      Wt::WMenuItem * const item = new Wt::WMenuItem(
        "Start",
        CreateNewMainDialog());
      menu->addItem(item);
    }
    {
      Wt::WMenuItem * const item = new Wt::WMenuItem(
        "About",
        CreateNewAboutDialog());
      menu->addItem(item);
    }
    //Display menu on top
    this->addWidget(menu);
    this->addWidget(new Wt::WBreak);
    //Display contents below menu
    this->addWidget(contents);
  }

  this->addWidget(new Wt::WBreak);

  assert(m_edit_width);
  assert(m_text);
}
//---------------------------------------------------------------------------
Wt::WWidget * WtTestAsciiArterDialog::CreateNewAboutDialog() const
{
  About a = TestAsciiArterDialog::GetAbout();
  a.AddLibrary("WtAutoConfig version: " + WtAutoConfig::GetVersion());
  WtAboutDialog * const d = new WtAboutDialog(a);
  return d;
}
//---------------------------------------------------------------------------
Wt::WWidget * WtTestAsciiArterDialog::CreateNewMainDialog()
{
  Wt::WContainerWidget * dialog = new Wt::WContainerWidget;
  //File upload
  {
    m_fileupload = new Wt::WFileUpload;

    //Upload automatically when the user entered a file
    m_fileupload->changed().connect(
      m_fileupload,
      &Wt::WFileUpload::upload);

    //Call WtTextUploadDialog::on_upload_done when file is uploaded
    m_fileupload->uploaded().connect(
      this,
      &WtTestAsciiArterDialog::OnUploadDone);
    dialog->addWidget(m_fileupload);
  }
  dialog->addWidget(new Wt::WBreak);
  dialog->addWidget(new Wt::WBreak);
  //Width edit
  {
    m_edit_width = new Wt::WLineEdit(
      boost::lexical_cast<std::string>(m_dialog->GetWidth()));
    //Respond to if user presses enter
    m_edit_width->enterPressed().connect(
      this, &WtTestAsciiArterDialog::OnEditWidthChange);
    dialog->addWidget(m_edit_width);
  }
  //Convert button
  {
    Wt::WPushButton * const button = new Wt::WPushButton("Convert");
    button->clicked().connect(
      this, &WtTestAsciiArterDialog::OnConvertClick);
    dialog->addWidget(button);
  }
  dialog->addWidget(new Wt::WBreak);
  dialog->addWidget(new Wt::WBreak);
  //text
  {
    m_text = new Wt::WTextArea;
    m_text->setMinimumSize(600,600);
    dialog->addWidget(m_text);
  }
  //OnAnyChange();
  return dialog;
}
//---------------------------------------------------------------------------
Wt::WWidget * WtTestAsciiArterDialog::CreateNewWelcomeDialog() const
{
  Wt::WContainerWidget * dialog = new Wt::WContainerWidget;
  dialog->setContentAlignment(Wt::AlignCenter);
  dialog->addWidget(new Wt::WBreak);
  new Wt::WLabel("Welcome to AsciiArter!",dialog);
  new Wt::WBreak(dialog);
  new Wt::WBreak(dialog);
  new Wt::WLabel("AsciiArter is a tool to convert images to ascii art.",dialog);
  return dialog;
}
//---------------------------------------------------------------------------
void WtTestAsciiArterDialog::OnAnyChange()
{
  if (m_dialog->CanConvert())
  {
    m_dialog->Convert();
    std::vector<std::string> v = m_dialog->GetAsciiArt();
    Wt::WString ws;
    BOOST_FOREACH(const std::string& s,v)
    {
      ws += s.c_str();
      ws += "\n";
    }
    m_text->setText(ws);
  }
}
//---------------------------------------------------------------------------
void WtTestAsciiArterDialog::OnConvertClick()
{
  OnEditWidthChange();
}
//---------------------------------------------------------------------------
void WtTestAsciiArterDialog::OnEditWidthChange()
{
  try
  {
    const std::string s = m_edit_width->text().toUTF8();
    const int i = boost::lexical_cast<int>(s);
    m_dialog->SetWidth(i);
  }
  catch(boost::bad_lexical_cast&)
  {
    //No problem
  }

  OnAnyChange();
}
//---------------------------------------------------------------------------
void WtTestAsciiArterDialog::OnUploadDone()
{
  const std::string filename = m_fileupload->spoolFileName();
  assert(boost::filesystem::exists(filename));
  const std::vector<std::vector<double> > v
    = ConvertToGreyYx(filename);
  assert(!v.empty());
  m_dialog->SetImage(v);
  OnAnyChange();
}
//---------------------------------------------------------------------------
//Returns a Y-X-ordered std::vector of greynesses.
const std::vector<std::vector<double> >
  WtTestAsciiArterDialog::ConvertToGreyYx(const std::string& filename)
{
  assert(boost::filesystem::exists(filename));
  const QImage * const i = new QImage(filename.c_str());
  const int maxy = i->height();
  const int maxx = i->width();
  const int n_bytes = i->bytesPerLine() / maxx;

  std::vector<std::vector<double> > v;
  for (int y=0; y!=maxy; ++y)
  {
    v.push_back(std::vector<double>());
    const unsigned char * const line = i->scanLine(y);
    for (int x=0; x!=maxx; ++x)
    {
      int sum = 0;
      for (int byte=0; byte!=n_bytes; ++byte)
      {
        sum += line[(x * n_bytes) + byte];
      }
      const double greyness
        = (boost::numeric_cast<double>(sum)
        / boost::numeric_cast<double>(n_bytes))
        / 256.0;
      assert(greyness >= 0.0);
      assert(greyness <= 1.0);
      v.back().push_back(greyness);
    }
  }
  return v;
}
//---------------------------------------------------------------------------

 

 

 

 

 

wttestasciiarterdialog.h

 

//---------------------------------------------------------------------------
/*
TestAsciiArter, tool to test the AsciiArter class
Copyright (C) 2006-2011 Richel Bilderbeek

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
//---------------------------------------------------------------------------
//From http://www.richelbilderbeek.nl/ToolTestAsciiArter.htm
//---------------------------------------------------------------------------
#ifndef WTTESTASCIIARTERDIALOG_H
#define WTTESTASCIIARTERDIALOG_H
//---------------------------------------------------------------------------
#include <boost/scoped_ptr.hpp>
#include <boost/signals2.hpp>
//---------------------------------------------------------------------------
#include <Wt/WContainerWidget>
//---------------------------------------------------------------------------
struct TestAsciiArterDialog;
namespace Wt
{
  struct WFileUpload;
  struct WLineEdit;
  struct WTextArea;
  struct WWidget;
}
//---------------------------------------------------------------------------
struct WtTestAsciiArterDialog : public Wt::WContainerWidget
{
  WtTestAsciiArterDialog();

  private:
  boost::scoped_ptr<TestAsciiArterDialog> m_dialog;
  Wt::WLineEdit * m_edit_width;
  Wt::WFileUpload * m_fileupload;
  Wt::WTextArea * m_text;

  boost::signals2::signal<void ()> m_signal_close;

  Wt::WWidget * CreateNewAboutDialog() const;
  Wt::WWidget * CreateNewMainDialog();
  Wt::WWidget * CreateNewWelcomeDialog() const;


  void OnAboutClick();
  void OnAnyChange();
  void OnConvertClick();
  void OnEditWidthChange();
  void OnUploadDone();

  void ShowAbout();
  void ShowMain();


  //Returns a Y-X-ordered std::vector of greynesses.
  static const std::vector<std::vector<double> >
    ConvertToGreyYx(const std::string& filename);

};
//---------------------------------------------------------------------------
#endif // WTTESTASCIIARTERDIALOG_H

 

 

 

 

 

 

 

 

 

 

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

Go back to Richel Bilderbeek's homepage.

 

Valid XHTML 1.0 Strict