JeVois  1.20
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>
22 #include <opencv2/imgcodecs.hpp>
23 #include <opencv2/imgproc/imgproc.hpp>
24 #include <imgui.h>
25 
26 // ##############################################################################################################
28 { }
29 
30 // ##############################################################################################################
31 void jevois::ImGuiImage::load(cv::Mat const & img, bool isbgr)
32 {
33  clear();
34 
35  if (img.depth() != CV_8U) LFATAL("Only 8-bit/channel images are supported");
36 
37  // OpenGL-ES does not support BGR textures, so we need to convert to RGB if image is BGR:
38  cv::Mat cvt; GLint fmt;
39  switch (img.channels())
40  {
41  case 1: cvt = img;
42  fmt = GL_LUMINANCE;
43  break;
44 
45  case 3:
46  if (isbgr) cv::cvtColor(img, cvt, cv::COLOR_BGR2RGB); else cvt = img;
47  fmt = GL_RGB;
48  break;
49 
50  case 4: if (isbgr) cv::cvtColor(img, cvt, cv::COLOR_BGRA2RGBA); else cvt = img;
51  fmt = GL_RGBA;
52  break;
53 
54  default: LFATAL("Unsupported image format with " << img.channels() << " channels, should be 1, 3, or 4");
55  }
56 
57  // OpenGL requires width to be a multiple of 8:
58  if (cvt.cols % 8)
59  {
60  int const newcols = cvt.cols + 8 - (cvt.cols % 8);
61  cvt = jevois::rescaleCv(cvt, cv::Size(newcols, cvt.rows * newcols / cvt.cols));
62  }
63 
64  // Create an OpenGL texture:
65  glGenTextures(1, &itsId);
66  glBindTexture(GL_TEXTURE_2D, itsId);
67  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
68  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
69  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
70  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
71  glTexImage2D(GL_TEXTURE_2D, 0, fmt, cvt.cols, cvt.rows, 0, fmt, GL_UNSIGNED_BYTE, cvt.ptr());
72  glBindTexture(GL_TEXTURE_2D, 0);
73 }
74 
75 // ##############################################################################################################
76 void jevois::ImGuiImage::load(std::string const & fname)
77 {
78  cv::Mat img = cv::imread(fname, cv::IMREAD_UNCHANGED);
79  if (img.data == nullptr) LFATAL("Failed to load image [" << fname << ']');
80  load(img, true);
81 }
82 
83 // ##############################################################################################################
85 { return (itsId != 0); }
86 
87 // ##############################################################################################################
89 {
90  if (itsId) { glDeleteTextures(1, &itsId); itsId = 0; }
91 }
92 
93 // ##############################################################################################################
95 { clear(); }
96 
97 // ##############################################################################################################
98 void jevois::ImGuiImage::draw(ImVec2 const & pos, ImVec2 const & size, ImDrawList * dl)
99 {
100  if (dl == nullptr) dl = ImGui::GetWindowDrawList();
101 
102  dl->AddImage(reinterpret_cast<ImTextureID>(itsId), pos, ImVec2(pos.x + size.x, pos.y + size.y));
103 }
104 
105 #endif // JEVOIS_PRO
jevois::ImGuiImage::load
void load(std::string const &fname)
Load from file.
Definition: ImGuiImage.C:76
RawImageOps.H
jevois::rescaleCv
cv::Mat rescaleCv(cv::Mat const &img, cv::Size const &newdims)
Rescale an OpenCV image, choosing the right kind of interpolation.
Definition: RawImageOps.C:1624
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:98
jevois::ImGuiImage::ImGuiImage
ImGuiImage()
Constructor, image is uninitialized and loaded() returns false.
Definition: ImGuiImage.C:27
jevois::ImGuiImage::~ImGuiImage
~ImGuiImage()
Destructor.
Definition: ImGuiImage.C:94
jevois::ImGuiImage::clear
void clear()
Free the texture and revert to un-initialized state.
Definition: ImGuiImage.C:88
LFATAL
#define LFATAL(msg)
Convenience macro for users to print out console or syslog messages, FATAL level.
jevois::ImGuiImage::loaded
bool loaded() const
Returns true if an image has been loaded through load()
Definition: ImGuiImage.C:84
ImGuiImage.H