ACloudViewer  3.9.4
A Modern Library for 3D Data Processing
ecvImage.cpp
Go to the documentation of this file.
1 // ----------------------------------------------------------------------------
2 // - CloudViewer: www.cloudViewer.org -
3 // ----------------------------------------------------------------------------
4 // Copyright (c) 2018-2024 www.cloudViewer.org
5 // SPDX-License-Identifier: MIT
6 // ----------------------------------------------------------------------------
7 
8 #include "ecvImage.h"
9 
10 // Local
11 #include "ecvCameraSensor.h"
12 #include "ecvDisplayTools.h"
13 
14 // Qt
15 #include <QFileInfo>
16 #include <QImageReader>
17 #include <QOpenGLTexture>
18 
20  : ccHObject("Not loaded"),
21  m_width(0),
22  m_height(0),
23  m_aspectRatio(1.0f),
24  m_texAlpha(1.0f),
25  m_associatedSensor(0) {
26  setVisible(true);
27  lockVisibility(false);
28  setEnabled(false);
29 }
30 
31 ccImage::ccImage(const QImage& image, const QString& name)
32  : ccHObject(name),
33  m_width(image.width()),
34  m_height(image.height()),
35  m_aspectRatio(1.0f),
36  m_texAlpha(1.0f),
37  m_image(image),
38  m_associatedSensor(0) {
40  setVisible(true);
41  lockVisibility(false);
42  setEnabled(true);
43 }
44 
45 bool ccImage::load(const QString& filename, QString& error) {
46  QImageReader reader(filename);
47  // m_image = QImage(filename);
48  QImage image = reader.read();
49  if (image.isNull()) {
50  error = reader.errorString();
51  return false;
52  }
53 
54  setData(image);
55 
56  setName(QFileInfo(filename).fileName());
57  setEnabled(true);
58 
59  return true;
60 }
61 
62 void ccImage::setData(const QImage& image) {
63  m_image = image;
64  m_width = m_image.width();
65  m_height = m_image.height();
67 }
68 
70  setAspectRatio(m_height != 0 ? static_cast<float>(m_width) / m_height
71  : 1.0f);
72 }
73 
75  if (m_image.isNull()) return;
76 
77  if (!MACRO_Draw2D(context) || !MACRO_Foreground(context)) return;
78 
81  // QOpenGLFunctions_2_1 *glFunc =
82  // context.glFunctions<QOpenGLFunctions_2_1>(); assert( glFunc != nullptr );
83  //
84  // if ( glFunc == nullptr )
85  // return;
86 
87  // glFunc->glPushAttrib(GL_COLOR_BUFFER_BIT);
88  // glFunc->glEnable(GL_BLEND);
89  // glFunc->glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
90 
91  // glFunc->glPushAttrib(GL_ENABLE_BIT);
92  // glFunc->glEnable(GL_TEXTURE_2D);
93 
94  // QOpenGLTexture texture(m_image);
95  // texture.bind();
96  //{
97  // //we make the texture fit inside viewport
98  // int realWidth = static_cast<int>(m_height * m_aspectRatio); //take
99  // aspect ratio into account! GLfloat cw =
100  // static_cast<GLfloat>(context.glW)
103  // GLfloat zoomFactor = (cw > ch ? ch : cw) / 2;
104  // GLfloat dX = realWidth*zoomFactor;
105  // GLfloat dY = m_height*zoomFactor;
106 
107  // glFunc->glColor4f(1, 1, 1, m_texAlpha);
108  // glFunc->glBegin(GL_QUADS);
109  // glFunc->glTexCoord2f(0, 1); glFunc->glVertex2f(-dX, -dY);
110  // glFunc->glTexCoord2f(1, 1); glFunc->glVertex2f( dX, -dY);
111  // glFunc->glTexCoord2f(1, 0); glFunc->glVertex2f( dX, dY);
112  // glFunc->glTexCoord2f(0, 0); glFunc->glVertex2f(-dX, dY);
113  // glFunc->glEnd();
114  //}
115  // texture.release();
116 
117  // glFunc->glPopAttrib();
118  // glFunc->glPopAttrib();
119 }
120 
121 void ccImage::setAlpha(float value) {
122  if (value <= 0)
123  m_texAlpha = 0;
124  else if (value > 1.0f)
125  m_texAlpha = 1.0f;
126  else
127  m_texAlpha = value;
128 }
129 
131  m_associatedSensor = sensor;
132 
133  if (m_associatedSensor)
135 }
136 
139 
141 }
142 
143 bool ccImage::toFile_MeOnly(QFile& out, short dataVersion) const {
144  assert(out.isOpen() && (out.openMode() & QIODevice::WriteOnly));
145  if (dataVersion < 38) {
146  assert(false);
147  return false;
148  }
149 
150  if (!ccHObject::toFile_MeOnly(out, dataVersion)) return false;
151 
152  // we can't save the associated sensor here (as it may be shared by multiple
153  // images) so instead we save it's unique ID (dataVersion>=38) WARNING: the
154  // sensor must be saved in the same BIN file! (responsibility of the caller)
155  uint32_t sensorUniqueID =
157  : 0);
158  if (out.write((const char*)&sensorUniqueID, 4) < 0) return WriteError();
159 
160  // for backward compatibility
161  float texU = 1.0f, texV = 1.0f;
162 
163  QDataStream outStream(&out);
164  outStream << m_width;
165  outStream << m_height;
166  outStream << m_aspectRatio;
167  outStream << texU;
168  outStream << texV;
169  outStream << m_texAlpha;
170  outStream << m_image;
171  QString fakeString;
172  outStream << fakeString; // formerly: 'complete filename'
173 
174  return true;
175 }
176 
178  return std::max(static_cast<short>(38),
180 }
181 
183  short dataVersion,
184  int flags,
185  LoadedIDMap& oldToNewIDMap) {
186  if (!ccHObject::fromFile_MeOnly(in, dataVersion, flags, oldToNewIDMap))
187  return false;
188 
189  // as the associated sensor can't be saved directly (as it may be shared by
190  // multiple images) we only store its unique ID (dataVersion >= 38) --> we
191  // hope we will find it at loading time (i.e. this is the responsibility of
192  // the caller to make sure that all dependencies are saved together)
193  uint32_t sensorUniqueID = 0;
194  if (in.read((char*)&sensorUniqueID, 4) < 0) return ReadError();
195  //[DIRTY] WARNING: temporarily, we set the vertices unique ID in the
196  //'m_associatedCloud' pointer!!!
197  *(uint32_t*)(&m_associatedSensor) = sensorUniqueID;
198 
199  float texU, texV;
200 
201  QDataStream inStream(&in);
202  inStream >> m_width;
203  inStream >> m_height;
204  inStream >> m_aspectRatio;
205  inStream >> texU;
206  inStream >> texV;
207  inStream >> m_texAlpha;
208  inStream >> m_image;
209  QString fakeString;
210  inStream >> fakeString; // formerly: 'complete filename'
211 
212  return true;
213 }
std::string filename
std::shared_ptr< core::Tensor > image
int width
std::string name
int height
Camera (projective) sensor.
virtual void lockVisibility(bool state)
Locks/unlocks visibility.
virtual void setVisible(bool state)
Sets entity visibility.
Hierarchical CLOUDVIEWER Object.
Definition: ecvHObject.h:25
virtual short minimumFileVersion_MeOnly() const
virtual bool fromFile_MeOnly(QFile &in, short dataVersion, int flags, LoadedIDMap &oldToNewIDMap)
Loads own object data.
void addDependency(ccHObject *otherObject, int flags, bool additive=true)
Adds a new dependence (additive or not)
Definition: ecvHObject.cpp:455
virtual bool toFile_MeOnly(QFile &out, short dataVersion) const
Save own object data.
@ DP_NOTIFY_OTHER_ON_DELETE
Definition: ecvHObject.h:259
virtual void onDeletionOf(const ccHObject *obj)
This method is called when another object is deleted.
Definition: ecvHObject.cpp:519
float m_texAlpha
Texture transparency.
Definition: ecvImage.h:105
bool load(const QString &filename, QString &error)
Loads image from file.
Definition: ecvImage.cpp:45
unsigned m_height
Image height (in pixels)
Definition: ecvImage.h:96
ccImage()
Default constructor.
Definition: ecvImage.cpp:19
void setData(const QImage &image)
Sets image data.
Definition: ecvImage.cpp:62
void setAspectRatio(float ar)
Manually sets aspect ratio.
Definition: ecvImage.h:63
bool toFile_MeOnly(QFile &out, short dataVersion) const override
Save own object data.
Definition: ecvImage.cpp:143
short minimumFileVersion_MeOnly() const override
Definition: ecvImage.cpp:177
bool fromFile_MeOnly(QFile &in, short dataVersion, int flags, LoadedIDMap &oldToNewIDMap) override
Loads own object data.
Definition: ecvImage.cpp:182
ccCameraSensor * m_associatedSensor
Associated sensor.
Definition: ecvImage.h:111
virtual void drawMeOnly(CC_DRAW_CONTEXT &context) override
Draws the entity only (not its children)
Definition: ecvImage.cpp:74
void setAlpha(float value)
Sets image texture transparency.
Definition: ecvImage.cpp:121
float m_aspectRatio
Aspect ratio w/h.
Definition: ecvImage.h:102
void setAssociatedSensor(ccCameraSensor *sensor)
Sets associated sensor.
Definition: ecvImage.cpp:130
virtual void onDeletionOf(const ccHObject *obj) override
This method is called when another object is deleted.
Definition: ecvImage.cpp:137
unsigned m_width
Image width (in pixels)
Definition: ecvImage.h:94
void updateAspectRatio()
Updates aspect ratio.
Definition: ecvImage.cpp:69
QImage m_image
Image data.
Definition: ecvImage.h:108
virtual unsigned getUniqueID() const
Returns object unique ID.
Definition: ecvObject.h:86
virtual void setName(const QString &name)
Sets object name.
Definition: ecvObject.h:75
virtual void setEnabled(bool state)
Sets the "enabled" property.
Definition: ecvObject.h:102
QMultiMap< unsigned, unsigned > LoadedIDMap
Map of loaded unique IDs (old ID --> new ID)
static bool ReadError()
Sends a custom error message (read error) and returns 'false'.
static bool WriteError()
Sends a custom error message (write error) and returns 'false'.
static void Draw(const CC_DRAW_CONTEXT &context, const ccHObject *obj)
#define MACRO_Draw2D(context)
#define MACRO_Foreground(context)
ImGuiContext * context
Definition: Window.cpp:76
Display context.