Developer Documentation
ImageStorage.cc
1 /*===========================================================================*\
2 * *
3 * OpenFlipper *
4  * Copyright (c) 2001-2015, RWTH-Aachen University *
5  * Department of Computer Graphics and Multimedia *
6  * All rights reserved. *
7  * www.openflipper.org *
8  * *
9  *---------------------------------------------------------------------------*
10  * This file is part of OpenFlipper. *
11  *---------------------------------------------------------------------------*
12  * *
13  * Redistribution and use in source and binary forms, with or without *
14  * modification, are permitted provided that the following conditions *
15  * are met: *
16  * *
17  * 1. Redistributions of source code must retain the above copyright notice, *
18  * this list of conditions and the following disclaimer. *
19  * *
20  * 2. Redistributions in binary form must reproduce the above copyright *
21  * notice, this list of conditions and the following disclaimer in the *
22  * documentation and/or other materials provided with the distribution. *
23  * *
24  * 3. Neither the name of the copyright holder nor the names of its *
25  * contributors may be used to endorse or promote products derived from *
26  * this software without specific prior written permission. *
27  * *
28  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS *
29  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED *
30  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A *
31  * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER *
32  * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, *
33  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, *
34  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR *
35  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF *
36  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING *
37  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS *
38  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. *
39 * *
40 \*===========================================================================*/
41 
42 /*===========================================================================*\
43 * *
44 * $Revision$ *
45 * $LastChangedBy$ *
46 * $Date$ *
47 * *
48 \*===========================================================================*/
49 
50 #include "ImageStorage.hh"
51 
53 
54 static ImageStore store;
55 
56 ImageStore& imageStore() {
57  return store;
58 }
59 
60 // ---------------------------------------------------------------------------
61 
62 ImageStore::ImageStore():
63  nextId_(1)
64 {
65 }
66 
67 // ---------------------------------------------------------------------------
68 
69 int ImageStore::addImageFile( QString _fileName ) {
70 
71  // Clean error string
72  errorString_ = "No error";
73 
74  QString loadFilename;
75 
76  // Construct complete filename
77  // if it starts with "/" or "." or contains a ":" it is a global filename and we don't touch it
78  // otherwise we take a relative path from our texture dir
79  if ( _fileName.startsWith("/") || _fileName.startsWith(".") || _fileName.contains(":") )
80  loadFilename = _fileName;
81  else
82  loadFilename = OpenFlipper::Options::textureDirStr() + QDir::separator() + _fileName;
83 
84  // Check if it already exists and return the existing one rather than recreating a new one
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()) {
88 
89  // Get id of the existing image
90  int existingId = filenameMap_[loadFilename].first;
91 
92  // Update refcount
93  refCount_[existingId] = refCount_[existingId] + 1;
94 
95  // Return existing ones id
96  return existingId;
97  }
98 
99  if ( !fileInfo.exists() ) {
100  errorString_ = "addedEmptyObject: Cannot load global texture '"+ loadFilename +"'. File not found!" ;
101  return -1;
102 
103  }
104 
105  QImage image;
106 
107  //QImage cannot handle tga directly
108  if ( fileInfo.suffix().toLower() == "tga" ){
109 
110  QPixmap pic(loadFilename);
111  image = pic.toImage();
112 
113  } else {
114  //load the image
115  if ( !image.load( loadFilename ) ){
116  errorString_ = "Unable to load Image from file " + loadFilename;
117  return -1;
118  }
119  }
120 
121  // Store the mapping from filename to id
122  filenameMap_[loadFilename]= qMakePair(nextId_, fileInfo.lastModified());
123  reverseFilenameMap_[nextId_] = loadFilename;
124 
125  // Initialize refcount
126  refCount_[nextId_] = 1;
127 
128  // Store image
129  imageMap_[nextId_] = image;
130 
131  // Increment for next image
132  nextId_++;
133 
134  // return the id of the added one
135  return (nextId_ - 1);
136 
137 
138 }
139 
140 // ---------------------------------------------------------------------------
141 
142 int ImageStore::addImage( QImage _image ) {
143 
144  // No filename mapping as it does not have a filename
145 
146  // Initialize refcount
147  refCount_[nextId_] = 1;
148 
149  // Store image
150  imageMap_[nextId_] = _image;
151 
152  // Increment for next image
153  nextId_++;
154 
155  // return the id of the added one
156  return (nextId_ - 1);
157 }
158 
159 // ---------------------------------------------------------------------------
160 
161 int ImageStore::getImageID(QString _filename) {
162 
163  // Check if it already exists and return the existing one rather than recreating a new one
164  if ( filenameMap_.contains(_filename)) {
165  return filenameMap_[_filename].first;
166  }
167 
168  // Clean error string
169  errorString_ = "getImageID failed. No such image :" + _filename;
170 
171  return -1;
172 
173 }
174 
175 // ---------------------------------------------------------------------------
176 
177 QImage& ImageStore::getImage(int _id, bool* _ok) {
178 
179  if ( imageMap_.contains(_id)) {
180  if ( _ok)
181  *_ok = true;
182  return imageMap_[_id];
183  } else {
184  if ( _ok)
185  *_ok = false;
186  errorString_ = "getImage: Unknown image id : " + QString::number(_id);
187  return dummy_;
188  }
189 }
190 
191 // ---------------------------------------------------------------------------
192 
193 void ImageStore::removeImage(int _id) {
194 
195  // Check if it exists
196  if ( refCount_.contains(_id)) {
197  if ( refCount_[_id] > 1 ) {
198  refCount_[_id] = refCount_[_id] - 1;
199  return;
200  } else {
201 
202  // Remove from refCount
203  refCount_.remove(_id);
204  imageMap_.remove(_id);
205 
206  // This image is from a file and not directly added
207  if ( reverseFilenameMap_.contains(_id)) {
208 
209  // Get the filename
210  QString fileName = reverseFilenameMap_[_id];
211 
212  reverseFilenameMap_.remove(_id);
213  filenameMap_.remove(fileName);
214 
215  }
216 
217  }
218  }
219 }
220 
221 
222 
223 
224 
225 
226 
227 
228 
229 
230 
231 
232 
233