Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
libgui: Added ImageBank, a Bank containing Images
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
Showing
4 changed files
with
150 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
#include "gui/imagebank.h" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |