Go back to Richel Bilderbeek's homepage.

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

 

 

 

 

 

(C++) QtImage

 

Technical facts

 

 

 

 

 

 

./CppQtImage/CppQtImage.pri

 

INCLUDEPATH += \
    ../../Classes/CppQtImage

SOURCES += \
    ../../Classes/CppQtImage/qtimage.cpp

HEADERS  += \
    ../../Classes/CppQtImage/qtimage.h

OTHER_FILES += \
    ../../Classes/CppQtImage/Licence.txt

 

 

 

 

 

./CppQtImage/qtimage.h

 

#ifndef QTIMAGE_H
#define QTIMAGE_H

#include <string>
#include <vector>

#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Weffc++"
#pragma GCC diagnostic ignored "-Wunused-local-typedefs"
#pragma GCC diagnostic ignored "-Wunused-but-set-parameter"
#include <QImage>
#pragma GCC diagnostic pop

struct QGraphicsItem;

namespace ribi {

///Some handy functions on QImage
struct QtImage
{
  QtImage() noexcept;
  QImage Difference(const QImage& base, const QImage& to_xor) noexcept;
  static std::string GetVersion() noexcept;
  static std::vector<std::string> GetVersionHistory() noexcept;

  //Render a QGraphicsItem to a QImage
  QImage Paint(const QGraphicsItem& item) noexcept;

  QImage Xor(const QImage& base, const QImage& to_xor) noexcept;
  private:
  #ifndef NDEBUG
  static void Test() noexcept;
  #endif
};

} //namespace ribi

#endif // QTIMAGE_H

 

 

 

 

 

./CppQtImage/qtimage.cpp

 

#include "qtimage.h"

#include <cassert>

#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Weffc++"
#pragma GCC diagnostic ignored "-Wunused-local-typedefs"
#pragma GCC diagnostic ignored "-Wunused-but-set-parameter"
#include <QGraphicsItem>
#include <QGraphicsScene>
#include <QPainter>

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

ribi::QtImage::QtImage() noexcept
{
  #ifndef NDEBUG
  Test();
  #endif // NDEBUG
}

QImage ribi::QtImage::Difference(const QImage& base, const QImage& to_xor) noexcept
{
  QImage result{base};
  {
    QPainter painter(&result);
    painter.setCompositionMode(QPainter::CompositionMode_Difference);
    painter.drawImage(0,0,to_xor);
  }
  return result;
}

std::string ribi::QtImage::GetVersion() noexcept
{
  return "1.0";
}

std::vector<std::string> ribi::QtImage::GetVersionHistory() noexcept
{
  return {
    "2014-08-05: Version 1.0: initial version",
  };
}

QImage ribi::QtImage::Paint(const QGraphicsItem& item) noexcept
{
  if (item.scene())
  {
    auto& scene = *item.scene();
    // Create the image with the exact size of the shrunk scene
    const QSize old_size{scene.sceneRect().size().toSize()};
    //Rescaled by a factor two to fix BUG_260
    //const QSize new_size(old_size.scaled(2,2, Qt::KeepAspectRatio));
    QImage image(old_size, QImage::Format_ARGB32);
    // Start all pixels transparent
    image.fill(Qt::transparent);
    QPainter painter(&image);
    scene.render(&painter);
    return image;
  }
  else
  {
    QGraphicsScene scene;
    scene.addItem(&const_cast<QGraphicsItem&>(item)); //Temporarily add the item, won't modify it

    const QSize old_size{scene.sceneRect().size().toSize()};
    //Rescaled by a factor two to fix BUG_260
    //const QSize new_size(old_size.scaled(2,2, Qt::KeepAspectRatio));
    QImage image(old_size, QImage::Format_ARGB32);
    // Start all pixels transparent
    image.fill(Qt::transparent);
    QPainter painter(&image);
    scene.render(&painter);
    scene.removeItem(&const_cast<QGraphicsItem&>(item)); //Prevent item being deleted
    return image;
  }
}


#ifndef NDEBUG
void ribi::QtImage::Test() noexcept
{
  {
    static bool is_tested{false};
    if (is_tested) return;
    is_tested = true;
  }
  const TestTimer test_timer(__func__,__FILE__,1.0);
  const bool verbose{false};
  if (verbose) { TRACE("Default-construction of QtImage"); }
  {
    const QtImage q;
  }
  if (verbose) { TRACE("QImage pixel manipultion"); }
  {
    QImage q(1,1,QImage::Format_RGB32);
    q.setPixel(0,0,qRgb(1,2,3));
    assert(q.pixel(0,0) == qRgb(1,2,3));
  }
  if (verbose) { TRACE("QGraphicsItem rendering"); }
  {
    QGraphicsSimpleTextItem item;
    item.setText(__func__);
    QImage image{QtImage().Paint(item)};
    assert(image.width() > 1);
    assert(image.height() > 1);
    image.save("tmp_ribi_QtImage_Test.png");
  }
}
#endif

QImage ribi::QtImage::Xor(const QImage& base, const QImage& to_xor) noexcept
{
  QImage result{base};
  {
    QPainter painter(&result);
    //painter.setCompositionMode(QPainter::CompositionMode_Xor);
    painter.setCompositionMode(QPainter::CompositionMode_Multiply);
    painter.drawImage(0,0,to_xor);
  }
  return result;
}

 

 

 

 

 

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