Skip to content

Commit

Permalink
libgui: Added de::Image, a wrapper that extends QImage
Browse files Browse the repository at this point in the history
The pixel formats supported by QImage are only partially compatible
with OpenGL (ES). de::Image defines a full set of GLES friendly
formats that can be used to pass raw pixel data (or QImages) to
GLTexture.
  • Loading branch information
skyjake committed Apr 24, 2013
1 parent 6e3415a commit 9bd66d1
Show file tree
Hide file tree
Showing 4 changed files with 494 additions and 0 deletions.
1 change: 1 addition & 0 deletions doomsday/libgui/include/de/Image
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
#include "gui/image.h"
146 changes: 146 additions & 0 deletions doomsday/libgui/include/de/gui/image.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
/** @file image.h Wrapper over QImage.
*
* @authors Copyright (c) 2013 Jaakko Keränen <jaakko.keranen@iki.fi>
*
* @par License
* GPL: http://www.gnu.org/licenses/gpl.html
*
* <small>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 2 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</small>
*/

#ifndef LIBGUI_IMAGE_H
#define LIBGUI_IMAGE_H

#include <utility>
#include <QImage>
#include <de/Vector>
#include <de/ByteRefArray>

#include "libgui.h"

namespace de {

/**
* Thin wrapper over QImage allowing use of some custom/raw image formats.
*
* Some image formats do not allow drawing. As a rule, all QImage-based formats
* support drawing (via QPainter).
*
* @todo Merge image_s and the related Image_* routines into here.
*/
class LIBGUI_PUBLIC Image
{
public:
/**
* Supported GL-friendly formats. Note that all QImage formats cannot be
* uploaded to OpenGL.
*/
enum Format
{
UseQImageFormat, ///< May not be GL friendly.

Luminance_8,
LuminanceAlpha_88,
Alpha_8,
RGB_555,
RGB_565,
RGB_444,
RGB_888, ///< 24-bit depth.
RGBA_4444,
RGBA_5551,
RGBA_8888,
RGBx_8888 ///< 32-bit depth, alpha data ignored.
};

typedef Vector2ui Size;

/// GL format + type for glTex(Sub)Image.
struct GLFormat {
duint format;
duint type;
duint rowAlignment;

GLFormat(duint f, duint t, duint ra) : format(f), type(t), rowAlignment(ra) {}
};

public:
Image();
Image(Image const &other);
Image(QImage const &image);

/**
* Constructs an image, taking a copy of the pixel data.
*
* @param size Size of the image.
* @param format Data format.
* @param pixels Pixel data.
*/
Image(Size const &size, Format format, IByteArray const &pixels);

Image(Size const &size, Format format, ByteRefArray const &refPixels);

Image &operator = (Image const &other);
Image &operator = (QImage const &other);

Format format() const;

QImage::Format qtFormat() const;

Size size() const;

/**
* Number of bits per pixel.
*/
int depth() const;

/**
* Number of bytes between rows in the pixel data.
*/
int stride() const;

/**
* Total number of bytes in the pixel data.
*/
int byteCount() const;

void const *bits() const;

void *bits();

/**
* Determines if the image format can be uploaded to OpenGL without
* conversion of any kind.
*/
bool isGLCompatible() const;

bool canConvertToQImage() const;

/**
* Converts the image to a QImage. Only allowed if canConvertToQImage()
* returns @c true.
*
* @return QImage instance.
*/
QImage toQImage() const;

GLFormat glFormat() const;

public:
static GLFormat glFormat(Format imageFormat);
static GLFormat glFormat(QImage::Format qtImageFormat);

private:
DENG2_PRIVATE(d)
};

} // namespace de

#endif // LIBGUI_IMAGE_H
3 changes: 3 additions & 0 deletions doomsday/libgui/libgui.pro
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ HEADERS += \
include/de/GLTexture \
include/de/GLUniform \
include/de/GuiApp \
include/de/Image \
include/de/KeyEvent \
include/de/KeyEventSource \
include/de/MouseEventSource \
Expand All @@ -78,6 +79,7 @@ HEADERS += \
include/de/gui/gltexture.h \
include/de/gui/gluniform.h \
include/de/gui/guiapp.h \
include/de/gui/image.h \
include/de/gui/keyevent.h \
include/de/gui/keyeventsource.h \
include/de/gui/libgui.h \
Expand All @@ -99,6 +101,7 @@ SOURCES += \
src/gltexture.cpp \
src/gluniform.cpp \
src/guiapp.cpp \
src/image.cpp \
src/keyevent.cpp \
src/persistentcanvaswindow.cpp \
src/glentrypoints.cpp
Expand Down
Loading

0 comments on commit 9bd66d1

Please sign in to comment.