50 #include "ImageStorage.hh" 62 ImageStore::ImageStore():
69 int ImageStore::addImageFile( QString _fileName ) {
72 errorString_ =
"No error";
79 if ( _fileName.startsWith(
"/") || _fileName.startsWith(
".") || _fileName.contains(
":") )
80 loadFilename = _fileName;
82 loadFilename = OpenFlipper::Options::textureDirStr() + QDir::separator() + _fileName;
85 QMap< QString,QPair<int,QDateTime> >::const_iterator it = filenameMap_.find(loadFilename);
86 QFileInfo fileInfo(loadFilename);
87 if ( it != filenameMap_.end() && it->second == fileInfo.lastModified()) {
90 int existingId = filenameMap_[loadFilename].first;
93 refCount_[existingId] = refCount_[existingId] + 1;
99 if ( !fileInfo.exists() ) {
100 errorString_ =
"addedEmptyObject: Cannot load global texture '"+ loadFilename +
"'. File not found!" ;
108 if ( fileInfo.suffix().toLower() ==
"tga" ){
110 QPixmap pic(loadFilename);
111 image = pic.toImage();
115 if ( !image.load( loadFilename ) ){
116 errorString_ =
"Unable to load Image from file " + loadFilename;
122 filenameMap_[loadFilename]= qMakePair(nextId_, fileInfo.lastModified());
123 reverseFilenameMap_[nextId_] = loadFilename;
126 refCount_[nextId_] = 1;
129 imageMap_[nextId_] = image;
135 return (nextId_ - 1);
142 int ImageStore::addImage( QImage _image ) {
147 refCount_[nextId_] = 1;
150 imageMap_[nextId_] = _image;
156 return (nextId_ - 1);
161 int ImageStore::getImageID(QString _filename) {
164 if ( filenameMap_.contains(_filename)) {
165 return filenameMap_[_filename].first;
169 errorString_ =
"getImageID failed. No such image :" + _filename;
177 QImage& ImageStore::getImage(
int _id,
bool* _ok) {
179 if ( imageMap_.contains(_id)) {
182 return imageMap_[_id];
186 errorString_ =
"getImage: Unknown image id : " + QString::number(_id);
193 void ImageStore::removeImage(
int _id) {
196 if ( refCount_.contains(_id)) {
197 if ( refCount_[_id] > 1 ) {
198 refCount_[_id] = refCount_[_id] - 1;
203 refCount_.remove(_id);
204 imageMap_.remove(_id);
207 if ( reverseFilenameMap_.contains(_id)) {
210 QString fileName = reverseFilenameMap_[_id];
212 reverseFilenameMap_.remove(_id);
213 filenameMap_.remove(fileName);