JeVois  1.16
JeVois Smart Embedded Machine Vision Toolkit
Share this page:
ImGuiImage.C
Go to the documentation of this file.
1 // ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
2 //
3 // JeVois Smart Embedded Machine Vision Toolkit - Copyright (C) 2020 by Laurent Itti, the University of Southern
4 // California (USC), and iLab at USC. See http://iLab.usc.edu and http://jevois.org for information about this project.
5 //
6 // This file is part of the JeVois Smart Embedded Machine Vision Toolkit. This program is free software; you can
7 // redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software
8 // Foundation, version 2. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
9 // without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
10 // License for more details. You should have received a copy of the GNU General Public License along with this program;
11 // if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
12 //
13 // Contact information: Laurent Itti - 3641 Watt Way, HNB-07A - Los Angeles, CA 90089-2520 - USA.
14 // Tel: +1 213 740 3527 - itti@pollux.usc.edu - http://iLab.usc.edu - http://jevois.org
15 // ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
16 /*! \file */
17 
18 #ifdef JEVOIS_PRO
19 
20 #include <jevois/GPU/ImGuiImage.H>
21 #include <opencv2/imgcodecs.hpp>
22 #include <opencv2/imgproc/imgproc.hpp>
23 #include <imgui.h>
24 
25 // ##############################################################################################################
27 { }
28 
29 // ##############################################################################################################
30 void jevois::ImGuiImage::load(cv::Mat const & img, bool isbgr)
31 {
32  clear();
33 
34  if (img.depth() != CV_8U) LFATAL("Only 8-bit/channel images are supported");
35 
36  // OpenGL-ES does not support BGR textures, so we need to convert to RGB if image is BGR:
37  cv::Mat cvt; GLint fmt;
38  switch (img.channels())
39  {
40  case 1: cvt = img;
41  fmt = GL_LUMINANCE;
42  break;
43 
44  case 3:
45  if (isbgr) cv::cvtColor(img, cvt, cv::COLOR_BGR2RGB); else cvt = img;
46  fmt = GL_RGB;
47  break;
48 
49  case 4: if (isbgr) cv::cvtColor(img, cvt, cv::COLOR_BGRA2RGBA); else cvt = img;
50  fmt = GL_RGBA;
51  break;
52 
53  default: LFATAL("Unsupported image format with " << img.channels() << " channels, should be 1, 3, or 4");
54  }
55 
56  glGenTextures(1, &itsId);
57  glBindTexture(GL_TEXTURE_2D, itsId);
58  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
59  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
60  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
61  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
62  glTexImage2D(GL_TEXTURE_2D, 0, fmt, cvt.cols, cvt.rows, 0, fmt, GL_UNSIGNED_BYTE, cvt.ptr());
63  glBindTexture(GL_TEXTURE_2D, 0);
64 }
65 
66 // ##############################################################################################################
67 void jevois::ImGuiImage::load(std::string const & fname)
68 {
69  cv::Mat img = cv::imread(fname, cv::IMREAD_UNCHANGED);
70  if (img.data == nullptr) LFATAL("Failed to load image [" << fname << ']');
71  load(img, true);
72 }
73 
74 // ##############################################################################################################
76 { return (itsId != 0); }
77 
78 // ##############################################################################################################
80 {
81  if (itsId) { glDeleteTextures(1, &itsId); itsId = 0; }
82 }
83 
84 // ##############################################################################################################
86 { clear(); }
87 
88 // ##############################################################################################################
89 void jevois::ImGuiImage::draw(ImVec2 const & pos, ImVec2 const & size, ImDrawList * dl)
90 {
91  if (dl == nullptr) dl = ImGui::GetWindowDrawList();
92 
93  dl->AddImage(reinterpret_cast<ImTextureID>(itsId), pos, ImVec2(pos.x + size.x, pos.y + size.y));
94 }
95 
96 #endif // JEVOIS_PRO
jevois::ImGuiImage::load
void load(std::string const &fname)
Load from file.
Definition: ImGuiImage.C:67
jevois::ImGuiImage::draw
void draw(ImVec2 const &pos, ImVec2 const &size, ImDrawList *dl=nullptr)
Draw into into current ImGui window or a drawlist.
Definition: ImGuiImage.C:89
jevois::ImGuiImage::ImGuiImage
ImGuiImage()
Constructor, image is uninitialized and loaded() returns false.
Definition: ImGuiImage.C:26
jevois::ImGuiImage::~ImGuiImage
~ImGuiImage()
Destructor.
Definition: ImGuiImage.C:85
jevois::ImGuiImage::clear
void clear()
Free the texture and revert to un-initialized state.
Definition: ImGuiImage.C:79
LFATAL
#define LFATAL(msg)
Convenience macro for users to print out console or syslog messages, FATAL level.
Definition: Log.H:217
jevois::ImGuiImage::loaded
bool loaded() const
Returns true if an image has been loaded through load()
Definition: ImGuiImage.C:75
ImGuiImage.H