(C++) QImageExample2
February 24, 2017 · View on GitHub
(C++) QImageExample2
Technical facts
Operating system(s) or programming environment(s)
Lubuntu 15.04 (vivid)
Qt Creator 3.1.1
- G++ 4.9.2
Libraries used:
Qt project file: ./CppQImageExample2/CppQImageExample2.pro
win32 { # Windows only message("Console application, built for Windows") QMAKE_CXXFLAGS += -std=c++11 -Wall -Wextra -Weffc++ } macx { # Mac only message("Console application, built for Mac") QMAKE_CXXFLAGS = -mmacosx-version-min=10.7 -std=gnu0x -stdlib=libc+ CONFIG +=c++11 } unix:!macx{ # Linux only message("Console application, built for Linux") QMAKE_CXXFLAGS += -Werror QMAKE_CXXFLAGS += -std=c++11 -Wall -Wextra -Weffc++ } cross_compile { # Crosscompile only message("Console application, cross-compiling from Linux to Windows") QMAKE_CXXFLAGS += -std=c++11 -Wall -Wextra -Weffc++ } # Go ahead and use Qt.Core: it is about as platform-independent as # the STL and Boost QT += core # Go ahead and use Qt.Gui: it is about as platform-independent as # the STL and Boost. It is needed for QImage QT += gui # Don't define widgets: it would defy the purpose of this console # application to work non-GUI #greaterThan(QT_MAJOR_VERSION, 4): QT += widgets CONFIG += console CONFIG -= app_bundle TEMPLATE = app # # # Type of compile # # CONFIG(release, debug|release) { DEFINES += NDEBUG NTRACE_BILDERBIKKEL } SOURCES += main.cpp
./CppQImageExample2/main.cpp
#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 <QImage> #pragma GCC diagnostic pop QImage CreateImage(const int width, const int height, const int z) noexcept { QImage image(width,height,QImage::Format_ARGB32); for (int y=0;y!=height;++y) { for (int x=0;x!=width;++x) { image.setPixel( x,y, qRgb((x+z+0)%256,(y+z+0)%256,(x+y+z)%256) //Color ); } } return image; } void DrawImage( QImage& target, const QImage& source, const int left, const int top ) noexcept { const int width = source.width(); const int height = source.height(); for (int y=0; y!=height; ++y) { for (int x=0; x!=width; ++x) { target.setPixel(left+x,top+y,source.pixel(x,y)); } } } int main() { QImage a = CreateImage(256,256,64); const QImage b = CreateImage(196,156,196); DrawImage(a,b,32,64); a.save("CppQImageExample2.png"); }