44 #include "ImageStorage.hh" 56 ImageStore::ImageStore():
63 int ImageStore::addImageFile( QString _fileName ) {
66 errorString_ =
"No error";
73 if ( _fileName.startsWith(
"/") || _fileName.startsWith(
".") || _fileName.contains(
":") )
74 loadFilename = _fileName;
76 loadFilename = OpenFlipper::Options::textureDirStr() + QDir::separator() + _fileName;
79 QMap< QString,QPair<int,QDateTime> >::const_iterator it = filenameMap_.find(loadFilename);
80 QFileInfo fileInfo(loadFilename);
81 if ( it != filenameMap_.end() && it->second == fileInfo.lastModified()) {
84 int existingId = filenameMap_[loadFilename].first;
87 refCount_[existingId] = refCount_[existingId] + 1;
93 if ( !fileInfo.exists() ) {
94 errorString_ =
"addedEmptyObject: Cannot load global texture '"+ loadFilename +
"'. File not found!" ;
102 if ( fileInfo.suffix().toLower() ==
"tga" ){
104 QPixmap pic(loadFilename);
105 image = pic.toImage();
109 if ( !image.load( loadFilename ) ){
110 errorString_ =
"Unable to load Image from file " + loadFilename;
116 filenameMap_[loadFilename]= qMakePair(nextId_, fileInfo.lastModified());
117 reverseFilenameMap_[nextId_] = loadFilename;
120 refCount_[nextId_] = 1;
123 imageMap_[nextId_] = image;
129 return (nextId_ - 1);
136 int ImageStore::addImage( QImage _image ) {
141 refCount_[nextId_] = 1;
144 imageMap_[nextId_] = _image;
150 return (nextId_ - 1);
155 int ImageStore::getImageID(QString _filename) {
158 if ( filenameMap_.contains(_filename)) {
159 return filenameMap_[_filename].first;
163 errorString_ =
"getImageID failed. No such image :" + _filename;
171 QImage& ImageStore::getImage(
int _id,
bool* _ok) {
173 if ( imageMap_.contains(_id)) {
176 return imageMap_[_id];
180 errorString_ =
"getImage: Unknown image id : " + QString::number(_id);
187 void ImageStore::removeImage(
int _id) {
190 if ( refCount_.contains(_id)) {
191 if ( refCount_[_id] > 1 ) {
192 refCount_[_id] = refCount_[_id] - 1;
197 refCount_.remove(_id);
198 imageMap_.remove(_id);
201 if ( reverseFilenameMap_.contains(_id)) {
204 QString fileName = reverseFilenameMap_[_id];
206 reverseFilenameMap_.remove(_id);
207 filenameMap_.remove(fileName);