Skip to content

Commit

Permalink
libgui|Image: Added basic image editing methods (using QImage)
Browse files Browse the repository at this point in the history
Clearing the image, filling rects, copying parts of the image.
  • Loading branch information
skyjake committed Apr 25, 2013
1 parent b0b8ff8 commit add301e
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 1 deletion.
11 changes: 10 additions & 1 deletion doomsday/libgui/include/de/gui/image.h
Expand Up @@ -22,6 +22,7 @@
#include <utility>
#include <QImage>
#include <de/Vector>
#include <de/Rectangle>
#include <de/ByteRefArray>

#include "libgui.h"
Expand Down Expand Up @@ -61,6 +62,7 @@ class LIBGUI_PUBLIC Image
};

typedef Vector2ui Size;
typedef Vector4ub Color;

/// GL format + type for glTex(Sub)Image.
struct GLFormat {
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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);
Expand Down
53 changes: 53 additions & 0 deletions doomsday/libgui/src/image.cpp
Expand Up @@ -19,8 +19,12 @@
#include "de/Image"
#include "de/gui/opengl.h"

#include <QPainter>

namespace de {

#define IMAGE_ASSERT_EDITABLE(d) DENG2_ASSERT(d->format == UseQImageFormat)

DENG2_PIMPL(Image)
{
Format format;
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit add301e

Please sign in to comment.