Skip to content

Commit

Permalink
libgui: Added ImageBank, a Bank containing Images
Browse files Browse the repository at this point in the history
ImageBank serves two purposes: maps logical identifiers (represented
as Paths) to actual files in the file system, and caches the raw pixel
data in memory when needed.
  • Loading branch information
skyjake committed May 1, 2013
1 parent a65d08c commit 7a193bc
Show file tree
Hide file tree
Showing 4 changed files with 150 additions and 0 deletions.
1 change: 1 addition & 0 deletions doomsday/libgui/include/de/ImageBank
@@ -0,0 +1 @@
#include "gui/imagebank.h"
62 changes: 62 additions & 0 deletions doomsday/libgui/include/de/gui/imagebank.h
@@ -0,0 +1,62 @@
/** @file imagebank.h Bank containing Image instances loaded from files.
*
* @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_IMAGEBANK_H
#define LIBGUI_IMAGEBANK_H

#include "libgui.h"
#include "../Image"

#include <de/Bank>

namespace de {

/**
* Bank containing Image instances loaded from files.
*
* @ingroup data
*/
class LIBGUI_PUBLIC ImageBank : public Bank
{
public:
/**
* Constructs a new image bank.
*
* @param flags Properties for the bank. By default, the bank uses a
* background thread because large images may take some
* time to load. Hot storage is disabled because loading
* images from their potentially encoded source data is
* faster/more efficient than deserializating raw pixel data.
*/
ImageBank(Flags const &flags = BackgroundThread | DisableHotStorage);

void add(Path const &path, String const &imageFilePath);

Image &image(Path const &path) const;

protected:
IData *loadFromSource(ISource &source);
IData *newData();

private:
DENG2_PRIVATE(d)
};

} // namespace de

#endif // LIBGUI_IMAGEBANK_H
3 changes: 3 additions & 0 deletions doomsday/libgui/libgui.pro
Expand Up @@ -61,6 +61,7 @@ HEADERS += \
include/de/GLUniform \
include/de/GuiApp \
include/de/Image \
include/de/ImageBank \
include/de/KeyEvent \
include/de/KeyEventSource \
include/de/MouseEventSource \
Expand All @@ -85,6 +86,7 @@ HEADERS += \
include/de/gui/gluniform.h \
include/de/gui/guiapp.h \
include/de/gui/image.h \
include/de/gui/imagebank.h \
include/de/gui/keyevent.h \
include/de/gui/keyeventsource.h \
include/de/gui/libgui.h \
Expand All @@ -111,6 +113,7 @@ SOURCES += \
src/gluniform.cpp \
src/guiapp.cpp \
src/image.cpp \
src/imagebank.cpp \
src/keyevent.cpp \
src/persistentcanvaswindow.cpp \
src/rowatlasallocator.cpp
Expand Down
84 changes: 84 additions & 0 deletions doomsday/libgui/src/imagebank.cpp
@@ -0,0 +1,84 @@
/** @file imagebank.h Bank containing Image instances loaded from files.
*
* @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>
*/

#include "de/ImageBank"
#include "de/App"

namespace de {

DENG2_PIMPL_NOREF(ImageBank)
{
struct ImageSource : public ISource
{
String filePath;

ImageSource(String const &path) : filePath(path)
{}

Time modifiedAt() const
{
return App::rootFolder().locate<File>(filePath).status().modifiedAt;
}

Image load() const
{
Block imageData;
App::rootFolder().locate<File const>(filePath) >> imageData;
return QImage::fromData(imageData);
}
};

struct ImageData : public IData
{
Image image;

ImageData() {}
ImageData(Image const &img) : image(img) {}

duint sizeInMemory() const
{
return image.byteCount();
}
};
};

ImageBank::ImageBank(Flags const &flags)
: Bank(flags), d(new Instance)
{}

void ImageBank::add(Path const &path, String const &imageFilePath)
{
Bank::add(path, new Instance::ImageSource(imageFilePath));
}

Image &ImageBank::image(Path const &path) const
{
return static_cast<Instance::ImageData &>(data(path)).image;
}

Bank::IData *ImageBank::loadFromSource(ISource &source)
{
return new Instance::ImageData(static_cast<Instance::ImageSource &>(source).load());
}

Bank::IData *ImageBank::newData()
{
return new Instance::ImageData();
}

} // namespace de

0 comments on commit 7a193bc

Please sign in to comment.