tile.cpp
Go to the documentation of this file.
1 // *****************************************************************************
2 //
3 // Copyright (c) 2014, Southwest Research Institute® (SwRI®)
4 // All rights reserved.
5 //
6 // Redistribution and use in source and binary forms, with or without
7 // modification, are permitted provided that the following conditions are met:
8 // * Redistributions of source code must retain the above copyright
9 // notice, this list of conditions and the following disclaimer.
10 // * Redistributions in binary form must reproduce the above copyright
11 // notice, this list of conditions and the following disclaimer in the
12 // documentation and/or other materials provided with the distribution.
13 // * Neither the name of Southwest Research Institute® (SwRI®) nor the
14 // names of its contributors may be used to endorse or promote products
15 // derived from this software without specific prior written permission.
16 //
17 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 // ARE DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
21 // DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22 // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23 // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24 // ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
26 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 //
28 // *****************************************************************************
29 
30 #include <multires_image/tile.h>
31 
32 // C++ standard libraries
33 #include <cmath>
34 #include <algorithm>
35 #include <exception>
36 #include <iostream>
37 
38 // QT libraries
39 #include <QGLWidget>
40 #include <QFile>
41 
43 
44 namespace multires_image
45 {
47  const std::string& path, int column, int row, int level,
48  const tf::Point& topLeft, const tf::Point& topRight,
49  const tf::Point& bottomLeft, const tf::Point& bottomRight) :
50  m_path(path),
51  m_column(column),
52  m_row(row),
53  m_level(level),
54  m_top_left(topLeft),
55  m_top_right(topRight),
56  m_bottom_right(bottomRight),
57  m_bottom_left(bottomLeft),
58  m_transformed_top_left(topLeft),
59  m_transformed_top_right(topRight),
60  m_transformed_bottom_right(bottomRight),
61  m_transformed_bottom_left(bottomLeft),
62  m_failed(false),
63  m_textureLoaded(false),
64  m_dimension(0),
65  m_textureId(0),
66  m_tileId(1000000 * level + 1000 * column + row),
67  m_memorySize(0)
68  {
69  }
70 
72  {
73  }
74 
75  bool Tile::Exists()
76  {
77  return QFile::exists(m_path.c_str());
78  }
79 
81  {
82  if (!m_failed)
83  {
84  m_mutex.lock();
85 
86  try
87  {
88  QImage nullImage;
89  m_image = nullImage;
90 
91  if (m_image.load(m_path.c_str()))
92  {
93  if (gl)
94  {
95  int width = m_image.width();
96  int height = m_image.height();
97 
98  float max_dim = std::max(width, height);
100  std::pow(2.0f, std::ceil(std::log(max_dim)/std::log(2.0f))));
101 
102  if (width != m_dimension || height != m_dimension)
103  {
104  m_image = m_image.scaled(m_dimension, m_dimension, Qt::IgnoreAspectRatio, Qt::FastTransformation);
105  }
106 
108 
109  m_image = QGLWidget::convertToGLFormat(m_image);
110  }
111  }
112  else
113  {
114  m_failed = true;
115  }
116  }
117  catch(std::exception& e)
118  {
119  std::cout << "An exception occurred loading image: " << e.what() << std::endl;
120  m_failed = true;
121  }
122 
123  m_mutex.unlock();
124  }
125 
126  return !m_failed;
127  }
128 
130  {
131  m_mutex.lock();
132 
133  QImage nullImage;
134  m_image = nullImage;
135 
136  m_mutex.unlock();
137  }
138 
140  {
141  if (!m_textureLoaded && !m_failed)
142  {
143  m_mutex.lock();
144 
145  try
146  {
147  GLuint ids[1];
148  glGenTextures(1, &ids[0]);
149  m_textureId = ids[0];
150 
151  glBindTexture(GL_TEXTURE_2D, m_textureId);
152  glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, m_dimension, m_dimension, 0, GL_RGBA, GL_UNSIGNED_BYTE, m_image.bits());
153 
154  // TODO(malban): check for GL error
155 
156  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
157  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
158 
159  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
160  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
161 
162  m_textureLoaded = true;
163  }
164  catch (const std::exception& e)
165  {
166  std::cout << "An exception occured loading texture: " << e.what() << std::endl;
167  m_failed = true;
168  }
169 
170  m_mutex.unlock();
171  }
172 
173  return m_textureLoaded;
174  }
175 
177  {
178  m_mutex.lock();
179 
180  if (m_textureLoaded)
181  {
182  m_textureLoaded = false;
183  GLuint ids[1];
184  ids[0] = m_textureId;
185  glDeleteTextures(1, &ids[0]);
186  }
187 
188  m_mutex.unlock();
189  }
190 
191  void Tile::Draw()
192  {
193  if (!m_failed)
194  {
195  if (m_textureLoaded)
196  {
197  glBindTexture(GL_TEXTURE_2D, m_textureId);
198 
199  glBegin(GL_QUADS);
200 
201  glTexCoord2f(0, 1); glVertex2f(m_transformed_top_left.x(), m_transformed_top_left.y());
202  glTexCoord2f(1, 1); glVertex2f(m_transformed_top_right.x(), m_transformed_top_right.y());
203  glTexCoord2f(1, 0); glVertex2f(m_transformed_bottom_right.x(), m_transformed_bottom_right.y());
204  glTexCoord2f(0, 0); glVertex2f(m_transformed_bottom_left.x(), m_transformed_bottom_left.y());
205 
206  glEnd();
207  }
208  }
209  }
210 
212  {
213  m_transformed_top_left = transform * m_top_left;
214  m_transformed_top_right = transform * m_top_right;
217  }
218 
220  {
221  m_transformed_top_left = offset_tf * (transform * m_top_left);
222  m_transformed_top_right = offset_tf * (transform * m_top_right);
223  m_transformed_bottom_left = offset_tf * (transform * m_bottom_left);
224  m_transformed_bottom_right = offset_tf * (transform * m_bottom_right);
225  }
226 }
227 
QMutex m_mutex
Definition: tile.h:105
Tile(const std::string &path, int column, int row, int level, const tf::Point &topLeft, const tf::Point &topRight, const tf::Point &bottomLeft, const tf::Point &bottomRight)
Definition: tile.cpp:46
f
bool LoadImageToMemory(bool gl=true)
Definition: tile.cpp:80
void UnloadImage()
Definition: tile.cpp:129
TFSIMD_FORCE_INLINE const tfScalar & x() const
double Round(double value)
const std::string m_path
Definition: tile.h:83
tf::Point m_bottom_right
Definition: tile.h:90
tf::Point m_bottom_left
Definition: tile.h:91
tf::Point m_top_right
Definition: tile.h:89
TFSIMD_FORCE_INLINE const tfScalar & y() const
tf::Point m_transformed_top_right
Definition: tile.h:94
bool m_textureLoaded
Definition: tile.h:99
tf::Point m_top_left
Definition: tile.h:88
tf::Point m_transformed_bottom_left
Definition: tile.h:96
tf::Point m_transformed_top_left
Definition: tile.h:93
void UnloadTexture()
Definition: tile.cpp:176
QImage m_image
Definition: tile.h:104
#define GL_CLAMP_TO_EDGE
Definition: tile.h:45
bool LoadTexture()
Definition: tile.cpp:139
tf::Point m_transformed_bottom_right
Definition: tile.h:95
void Transform(const swri_transform_util::Transform &transform)
Definition: tile.cpp:211


multires_image
Author(s): Marc Alban
autogenerated on Fri Mar 19 2021 02:44:42