ImageStorage.cc 6.95 KB
Newer Older
1
2
3
/*===========================================================================*\
*                                                                            *
*                              OpenFlipper                                   *
Jan Möbius's avatar
Jan Möbius committed
4
*      Copyright (C) 2001-2014 by Computer Graphics Group, RWTH Aachen       *
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
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
*                           www.openflipper.org                              *
*                                                                            *
*--------------------------------------------------------------------------- *
*  This file is part of OpenFlipper.                                         *
*                                                                            *
*  OpenFlipper is free software: you can redistribute it and/or modify       *
*  it under the terms of the GNU Lesser General Public License as            *
*  published by the Free Software Foundation, either version 3 of            *
*  the License, or (at your option) any later version with the               *
*  following exceptions:                                                     *
*                                                                            *
*  If other files instantiate templates or use macros                        *
*  or inline functions from this file, or you compile this file and          *
*  link it with other files to produce an executable, this file does         *
*  not by itself cause the resulting executable to be covered by the         *
*  GNU Lesser General Public License. This exception does not however        *
*  invalidate any other reasons why the executable file might be             *
*  covered by the GNU Lesser General Public License.                         *
*                                                                            *
*  OpenFlipper 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 Lesser General Public License for more details.                       *
*                                                                            *
*  You should have received a copy of the GNU LesserGeneral Public           *
*  License along with OpenFlipper. If not,                                   *
*  see <http://www.gnu.org/licenses/>.                                       *
*                                                                            *
\*===========================================================================*/

/*===========================================================================*\
*                                                                            *
*   $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
  if ( filenameMap_.count(loadFilename) > 0 ) {

    // Get id of the existing image
    int existingId = filenameMap_[loadFilename];

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

    // Return existing ones id
    return existingId;
  }

  QFile file(loadFilename);
  if ( !file.exists() ) {
    errorString_ =  "addedEmptyObject: Cannot load global texture '"+ loadFilename +"'. File not found!" ;
    return -1;

  }

  QImage image;

  //QImage cannot handle tga directly
  if ( QFileInfo(loadFilename).suffix().toLower() == "tga" ){

    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
  filenameMap_[loadFilename]= nextId_;
  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
  if ( filenameMap_.count(_filename) > 0 ) {
    return filenameMap_[_filename];
  }

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

  return -1;

}

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

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

  if ( imageMap_.count(_id) > 0 ) {
    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
  if ( refCount_.count(_id) > 0 ) {
    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
      if ( reverseFilenameMap_.count(_id) > 0 )  {

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

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

      }

    }
  }
}