TextureFactory.cpp
Go to the documentation of this file.
1 
38 
39 #include "lvr2/io/PPMIO.hpp"
40 #include "lvr2/io/Timestamp.hpp"
41 #include "lvr2/texture/Texture.hpp"
42 
43 #include <opencv2/core.hpp>
44 #include <opencv2/imgcodecs.hpp>
45 #include <opencv2/imgproc.hpp>
46 
47 #include <iostream>
48 using std::cout;
49 using std::endl;
50 
51 namespace lvr2
52 {
53 
55 {
56  // returns 8-bit image with BGR order
57  cv::Mat mat = cv::imread(filename);
58 
59 
60  // if unable to read file
61  if (mat.data == NULL)
62  {
63  cout << timestamp << "TextureFactory: Unable to read file '"
64  << filename << "'. Returning empty Texture." << endl;
65 
66  // return empty Texture
67  return Texture();
68  }
69 
70  // convert it to RGB order
71  cv::cvtColor(mat, mat, cv::COLOR_BGR2RGB);
72 
73  Texture ret(0, mat.cols, mat.rows, 3, 1, 1.0);
74  std::copy(mat.datastart, mat.dataend, ret.m_data);
75 
76  return ret;
77 }
78 
79 void TextureFactory::saveTexture(const Texture& tex, std::string filename)
80 {
81  // if Texture has no data to be saved
82  if (tex.m_data == NULL || tex.m_width == 0 || tex.m_height == 0 ||
83  tex.m_numChannels == 0 || tex.m_numBytesPerChan == 0)
84  {
85  cout << timestamp << "TextureFactory: Texture will not be saved to file '"
86  << filename << "' because the texture has no data." << endl;
87 
88  return;
89  }
90 
91  // TODO convert the data instead of only allowing 1 byte channels
92  if (tex.m_numBytesPerChan != 1)
93  {
94  cout << timestamp << "TextureFactory: Texture will not be saved to file '"
95  << filename << "' because texture has more than 1 byte \
96  per channel (currently only 1-byte channels are supported)." << endl;
97 
98  return;
99  }
100 
101  if (tex.m_numChannels != 1 && tex.m_numChannels != 3 && tex.m_numChannels != 4)
102  {
103  cout << timestamp << "TextureFactory: Texture will not be saved to file '"
104  << filename << "' because the texture has an unsupported amount of channels \
105  (currently only 1, 3 and 4 channels per pixel are supported)." << endl;
106 
107  return;
108  }
109 
110  // convert texture data to be saved with OpenCV
111  int mat_type = CV_8UC1;
112  int conversionCode = cv::COLOR_GRAY2BGR;
113 
114  if (tex.m_numChannels == 3)
115  {
116  mat_type = CV_8UC3;
117  conversionCode = cv::COLOR_RGB2BGR;
118  }
119  else if (tex.m_numChannels == 4)
120  {
121  // currently we just ignore the alpha channel but it would be
122  // possible to save the alpha channel with OpenCV for PNG images.
123  mat_type = CV_8UC3;
124  conversionCode = cv::COLOR_RGBA2BGR;
125  }
126 
127  cv::Mat mat;
128  mat.create(tex.m_height, tex.m_width, mat_type);
129 
130  size_t bytesToCopy = tex.m_width * tex.m_height * tex.m_numChannels * tex.m_numBytesPerChan;
131  std::copy(tex.m_data, tex.m_data + bytesToCopy, mat.data);
132 
133  if (!(mat_type == CV_8UC1))
134  {
135  cv::cvtColor(mat, mat, conversionCode);
136  }
137 
138  // todo include params like binary mode for ppm files for example...
139  if (!cv::imwrite(filename, mat))
140  {
141  cout << timestamp << "TextureFactory: Unable to save texture to file '"
142  << filename << "'." << endl;
143  };
144 }
145 
146 } // namespace lvr2
lvr2::Texture::m_data
unsigned char * m_data
The texture data.
Definition: Texture.hpp:114
lvr2::TextureFactory::saveTexture
static void saveTexture(const Texture &texture, std::string filename)
TODO.
Definition: TextureFactory.cpp:79
lvr2::TextureFactory::readTexture
static Texture readTexture(std::string filename)
Returns a new texture if the file contains readable image data or a null point if the file couldn't b...
Definition: TextureFactory.cpp:54
lvr2::Texture::m_numChannels
unsigned char m_numChannels
The number of color channels.
Definition: Texture.hpp:117
NULL
#define NULL
Definition: mydefs.hpp:141
lvr2::Texture::m_height
unsigned short int m_height
Definition: Texture.hpp:111
Texture.hpp
scripts.normalize_multiple.filename
filename
Definition: normalize_multiple.py:60
PPMIO.hpp
lvr2::timestamp
static Timestamp timestamp
A global time stamp object for program runtime measurement.
Definition: Timestamp.hpp:116
TextureFactory.hpp
lvr2
Definition: BaseBufferManipulators.hpp:39
Timestamp.hpp
lvr2::Texture
This class represents a texture.
Definition: Texture.hpp:54
lvr2::Texture::m_width
unsigned short int m_width
The dimensions of the texture.
Definition: Texture.hpp:111
lvr2::Texture::m_numBytesPerChan
unsigned char m_numBytesPerChan
The number of bytes per channel.
Definition: Texture.hpp:120


lvr2
Author(s): Thomas Wiemann , Sebastian Pütz , Alexander Mock , Lars Kiesow , Lukas Kalbertodt , Tristan Igelbrink , Johan M. von Behren , Dominik Feldschnieders , Alexander Löhr
autogenerated on Wed Mar 2 2022 00:37:25