From add301e51ad5ed3338ec1360ee8f1db8b3f648a6 Mon Sep 17 00:00:00 2001 From: skyjake Date: Thu, 25 Apr 2013 15:44:48 +0300 Subject: [PATCH] libgui|Image: Added basic image editing methods (using QImage) Clearing the image, filling rects, copying parts of the image. --- doomsday/libgui/include/de/gui/image.h | 11 +++++- doomsday/libgui/src/image.cpp | 53 ++++++++++++++++++++++++++ 2 files changed, 63 insertions(+), 1 deletion(-) diff --git a/doomsday/libgui/include/de/gui/image.h b/doomsday/libgui/include/de/gui/image.h index bb4fa6dac7..354cd7ca50 100644 --- a/doomsday/libgui/include/de/gui/image.h +++ b/doomsday/libgui/include/de/gui/image.h @@ -22,6 +22,7 @@ #include #include #include +#include #include #include "libgui.h" @@ -61,6 +62,7 @@ class LIBGUI_PUBLIC Image }; typedef Vector2ui Size; + typedef Vector4ub Color; /// GL format + type for glTex(Sub)Image. struct GLFormat { @@ -91,10 +93,10 @@ class LIBGUI_PUBLIC Image Image &operator = (QImage const &other); Format format() const; - QImage::Format qtFormat() const; Size size() const; + Rectanglei rect() const; /** * Number of bits per pixel. @@ -133,6 +135,13 @@ class LIBGUI_PUBLIC Image GLFormat glFormat() const; + // Drawing/editing methods. + Image subImage(Rectanglei const &subArea) const; + void resize(Size const &size); + void fill(Color const &color); + void fill(Rectanglei const &rect, Color const &color); + void draw(Image const &image, Vector2i const &topLeft); + public: static GLFormat glFormat(Format imageFormat); static GLFormat glFormat(QImage::Format qtImageFormat); diff --git a/doomsday/libgui/src/image.cpp b/doomsday/libgui/src/image.cpp index 7ed616edf9..050f85cbcd 100644 --- a/doomsday/libgui/src/image.cpp +++ b/doomsday/libgui/src/image.cpp @@ -19,8 +19,12 @@ #include "de/Image" #include "de/gui/opengl.h" +#include + namespace de { +#define IMAGE_ASSERT_EDITABLE(d) DENG2_ASSERT(d->format == UseQImageFormat) + DENG2_PIMPL(Image) { Format format; @@ -101,6 +105,11 @@ Image::Size Image::size() const return d->size; } +Rectanglei Image::rect() const +{ + return Rectanglei(0, 0, d->size.x, d->size.y); +} + int Image::depth() const { switch(d->format) @@ -266,6 +275,50 @@ Image::GLFormat Image::glFormat() const return glFormat(d->format); } +Image Image::subImage(Rectanglei const &subArea) const +{ + IMAGE_ASSERT_EDITABLE(d); + + return Image(d->image.copy(subArea.topLeft.x, subArea.topLeft.y, + subArea.width(), subArea.height())); +} + +void Image::resize(Size const &size) +{ + IMAGE_ASSERT_EDITABLE(d); + + QImage resized(QSize(size.x, size.y), d->image.format()); + QPainter painter(&resized); + painter.drawImage(QPoint(0, 0), d->image); + d->image = resized; + d->size = size; +} + +void Image::fill(Color const &color) +{ + IMAGE_ASSERT_EDITABLE(d); + + d->image.fill(QColor(color.x, color.y, color.z, color.w)); +} + +void Image::fill(Rectanglei const &rect, Color const &color) +{ + IMAGE_ASSERT_EDITABLE(d); + + QPainter painter(&d->image); + painter.fillRect(QRect(rect.topLeft.x, rect.topLeft.y, rect.width(), rect.height()), + QColor(color.x, color.y, color.z, color.w)); +} + +void Image::draw(Image const &image, Vector2i const &topLeft) +{ + IMAGE_ASSERT_EDITABLE(d); + IMAGE_ASSERT_EDITABLE(image.d); + + QPainter painter(&d->image); + painter.drawImage(QPoint(topLeft.x, topLeft.y), image.d->image); +} + Image::GLFormat Image::glFormat(Format imageFormat) { DENG2_ASSERT(imageFormat >= Luminance_8 && imageFormat <= RGBx_8888);