ImageStorage.cc 7.64 KB
Newer Older
1
2
3
/*===========================================================================*\
*                                                                            *
*                              OpenFlipper                                   *
Martin Schultz's avatar
Martin Schultz committed
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
 *           Copyright (c) 2001-2015, RWTH-Aachen University                 *
 *           Department of Computer Graphics and Multimedia                  *
 *                          All rights reserved.                             *
 *                            www.openflipper.org                            *
 *                                                                           *
 *---------------------------------------------------------------------------*
 * This file is part of OpenFlipper.                                         *
 *---------------------------------------------------------------------------*
 *                                                                           *
 * Redistribution and use in source and binary forms, with or without        *
 * modification, are permitted provided that the following conditions        *
 * are met:                                                                  *
 *                                                                           *
 * 1. Redistributions of source code must retain the above copyright notice, *
 *    this list of conditions and the following disclaimer.                  *
 *                                                                           *
 * 2. Redistributions in binary form must reproduce the above copyright      *
 *    notice, this list of conditions and the following disclaimer in the    *
 *    documentation and/or other materials provided with the distribution.   *
 *                                                                           *
 * 3. Neither the name of the copyright holder nor the names of its          *
 *    contributors may be used to endorse or promote products derived from   *
 *    this software without specific prior written permission.               *
 *                                                                           *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS       *
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED *
 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A           *
 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER *
 * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,  *
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,       *
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR        *
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF    *
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING      *
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS        *
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.              *
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
*                                                                            *
\*===========================================================================*/

/*===========================================================================*\
*                                                                            *
*   $Revision$                                                       *
*   $LastChangedBy$                                                *
*   $Date$                     *
*                                                                            *
\*===========================================================================*/

#include "ImageStorage.hh"

#include <iostream>
#include <OpenFlipper/common/GlobalOptions.hh>

static ImageStore store;

ImageStore& imageStore() {
  return store;
}

// ---------------------------------------------------------------------------

ImageStore::ImageStore():
    nextId_(1)
{
}

// ---------------------------------------------------------------------------

int ImageStore::addImageFile( QString _fileName ) {

  // Clean error string
  errorString_ = "No error";

  QString loadFilename;

  // Construct complete filename
  // if it starts with "/" or "." or contains a ":" it is a global filename and we don't touch it
  // otherwise we take a relative path from our texture dir
  if ( _fileName.startsWith("/") || _fileName.startsWith(".") || _fileName.contains(":") )
    loadFilename = _fileName;
  else
    loadFilename = OpenFlipper::Options::textureDirStr() + QDir::separator() + _fileName;

  // Check if it already exists and return the existing one rather than recreating a new one
86
87
88
  QMap< QString,QPair<int,QDateTime> >::const_iterator it = filenameMap_.find(loadFilename);
  QFileInfo fileInfo(loadFilename);
  if ( it != filenameMap_.end() && it->second == fileInfo.lastModified()) {
89
90

    // Get id of the existing image
91
    int existingId = filenameMap_[loadFilename].first;
92
93
94
95
96
97
98
99

    // Update refcount
    refCount_[existingId] = refCount_[existingId] + 1;

    // Return existing ones id
    return existingId;
  }

100
  if ( !fileInfo.exists() ) {
101
102
103
104
105
106
107
108
    errorString_ =  "addedEmptyObject: Cannot load global texture '"+ loadFilename +"'. File not found!" ;
    return -1;

  }

  QImage image;

  //QImage cannot handle tga directly
109
  if ( fileInfo.suffix().toLower() == "tga" ){
110
111
112
113
114
115
116
117
118
119
120
121
122

    QPixmap pic(loadFilename);
    image = pic.toImage();

  } else {
    //load the image
    if ( !image.load( loadFilename ) ){
      errorString_ = "Unable to load Image from file " + loadFilename;
      return -1;
    }
  }

  // Store the mapping from filename to id
123
  filenameMap_[loadFilename]= qMakePair(nextId_, fileInfo.lastModified());
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
  reverseFilenameMap_[nextId_] = loadFilename;

  // Initialize refcount
  refCount_[nextId_] = 1;

  // Store image
  imageMap_[nextId_] = image;

  // Increment for next image
  nextId_++;

  // return the id of the added one
  return (nextId_ - 1);


}

// ---------------------------------------------------------------------------

int  ImageStore::addImage( QImage _image ) {

  // No filename mapping as it does not have a filename

  // Initialize refcount
  refCount_[nextId_] = 1;

  // Store image
  imageMap_[nextId_] = _image;

  // Increment for next image
  nextId_++;

  // return the id of the added one
  return (nextId_ - 1);
}

// ---------------------------------------------------------------------------

int  ImageStore::getImageID(QString _filename) {

  // Check if it already exists and return the existing one rather than recreating a new one
165
166
  if ( filenameMap_.contains(_filename)) {
    return filenameMap_[_filename].first;
167
168
169
170
171
172
173
174
175
176
177
178
179
  }

  // Clean error string
  errorString_ = "getImageID failed. No such image :" + _filename;

  return -1;

}

// ---------------------------------------------------------------------------

QImage& ImageStore::getImage(int _id, bool* _ok) {

180
  if ( imageMap_.contains(_id)) {
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
    if ( _ok)
      *_ok = true;
    return imageMap_[_id];
  } else {
    if ( _ok)
      *_ok = false;
    errorString_ = "getImage: Unknown image id : " + QString::number(_id);
    return dummy_;
  }
}

// ---------------------------------------------------------------------------

void ImageStore::removeImage(int _id) {

  // Check if it exists
197
  if ( refCount_.contains(_id)) {
198
199
200
201
202
203
204
205
206
207
    if ( refCount_[_id] > 1 ) {
      refCount_[_id] = refCount_[_id] - 1;
      return;
    } else {

      // Remove from refCount
      refCount_.remove(_id);
      imageMap_.remove(_id);

      // This image is from a file and not directly added
208
      if ( reverseFilenameMap_.contains(_id))  {
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234

        // Get the filename
        QString fileName = reverseFilenameMap_[_id];

        reverseFilenameMap_.remove(_id);
        filenameMap_.remove(fileName);

      }

    }
  }
}