vtkImageMatSource.cpp
Go to the documentation of this file.
1 /*M///////////////////////////////////////////////////////////////////////////////////////
2 //
3 // IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
4 //
5 // By downloading, copying, installing or using the software you agree to this license.
6 // If you do not agree to this license, do not download, install,
7 // copy or use the software.
8 //
9 //
10 // License Agreement
11 // For Open Source Computer Vision Library
12 //
13 // Copyright (C) 2013, OpenCV Foundation, all rights reserved.
14 // Third party copyrights are property of their respective owners.
15 //
16 // Redistribution and use in source and binary forms, with or without modification,
17 // are permitted provided that the following conditions are met:
18 //
19 // * Redistribution's of source code must retain the above copyright notice,
20 // this list of conditions and the following disclaimer.
21 //
22 // * Redistribution's in binary form must reproduce the above copyright notice,
23 // this list of conditions and the following disclaimer in the documentation
24 // and/or other materials provided with the distribution.
25 //
26 // * The name of the copyright holders may not be used to endorse or promote products
27 // derived from this software without specific prior written permission.
28 //
29 // This software is provided by the copyright holders and contributors "as is" and
30 // any express or implied warranties, including, but not limited to, the implied
31 // warranties of merchantability and fitness for a particular purpose are disclaimed.
32 // In no event shall the Intel Corporation or contributors be liable for any direct,
33 // indirect, incidental, special, exemplary, or consequential damages
34 // (including, but not limited to, procurement of substitute goods or services;
35 // loss of use, data, or profits; or business interruption) however caused
36 // and on any theory of liability, whether in contract, strict liability,
37 // or tort (including negligence or otherwise) arising in any way out of
38 // the use of this software, even if advised of the possibility of such damage.
39 //
40 // Authors:
41 // * Anatoly Baksheev, Itseez Inc. myname.mysurname <> mycompany.com
42 //
43 //M*/
44 
45 #include "vtkImageMatSource.h"
46 #include <vtkImageData.h>
47 #include <vtkInformation.h>
48 #include <vtkInformationVector.h>
49 #include <vtkStreamingDemandDrivenPipeline.h>
50 #include <vtkObjectFactory.h>
51 #include <vtkVersionMacros.h>
52 
53 namespace rtabmap {
54  vtkStandardNewMacro(vtkImageMatSource);
55 }
56 
58 {
59  this->SetNumberOfInputPorts(0);
61 }
62 
63 int rtabmap::vtkImageMatSource::RequestInformation(vtkInformation *, vtkInformationVector**, vtkInformationVector *outputVector)
64 {
65  vtkInformation* outInfo = outputVector->GetInformationObject(0);
66 
67  outInfo->Set(vtkStreamingDemandDrivenPipeline::WHOLE_EXTENT(), this->ImageData->GetExtent(), 6);
68  outInfo->Set(vtkDataObject::SPACING(), 1.0, 1.0, 1.0);
69  outInfo->Set(vtkDataObject::ORIGIN(), 0.0, 0.0, 0.0);
70 
71  vtkDataObject::SetPointDataActiveScalarInfo(outInfo, this->ImageData->GetScalarType(), this->ImageData->GetNumberOfScalarComponents());
72  return 1;
73 }
74 
75 int rtabmap::vtkImageMatSource::RequestData(vtkInformation*, vtkInformationVector**, vtkInformationVector *outputVector)
76 {
77  vtkInformation *outInfo = outputVector->GetInformationObject(0);
78 
79  vtkImageData *output = vtkImageData::SafeDownCast(outInfo->Get(vtkDataObject::DATA_OBJECT()) );
80  output->ShallowCopy(this->ImageData);
81  return 1;
82 }
83 
84 void rtabmap::vtkImageMatSource::SetImage(cv::InputArray _image)
85 {
86  CV_Assert(_image.depth() == CV_8U && (_image.channels() == 1 || _image.channels() == 3 || _image.channels() == 4));
87 
88  cv::Mat image = _image.getMat();
89 
90  this->ImageData->SetDimensions(image.cols, image.rows, 1);
91 #if VTK_MAJOR_VERSION <= 5
92  this->ImageData->SetNumberOfScalarComponents(image.channels());
93  this->ImageData->SetScalarTypeToUnsignedChar();
94  this->ImageData->AllocateScalars();
95 #else
96  this->ImageData->AllocateScalars(VTK_UNSIGNED_CHAR, image.channels());
97 #endif
98 
99  switch(image.channels())
100  {
101  case 1: copyGrayImage(image, this->ImageData); break;
102  case 3: copyRGBImage (image, this->ImageData); break;
103  case 4: copyRGBAImage(image, this->ImageData); break;
104  }
105  this->ImageData->Modified();
106 }
107 
109 {
110  unsigned char* dptr = reinterpret_cast<unsigned char*>(output->GetScalarPointer());
111  size_t elem_step = output->GetIncrements()[1]/sizeof(unsigned char);
112 
113  for (int y = 0; y < source.rows; ++y)
114  {
115  unsigned char* drow = dptr + elem_step * y;
116  const unsigned char *srow = source.ptr<unsigned char>(source.rows-(y+1)); // vertical flip for texturing
117  for (int x = 0; x < source.cols; ++x)
118  drow[x] = *srow++;
119  }
120 }
121 
123 {
124  cv::Vec3b* dptr = reinterpret_cast<cv::Vec3b*>(output->GetScalarPointer());
125  size_t elem_step = output->GetIncrements()[1]/sizeof(cv::Vec3b);
126 
127  for (int y = 0; y < source.rows; ++y)
128  {
129  cv::Vec3b* drow = dptr + elem_step * y;
130  const unsigned char *srow = source.ptr<unsigned char>(source.rows - (y + 1)); // vertical flip for texturing
131  for (int x = 0; x < source.cols; ++x, srow += source.channels())
132  drow[x] = cv::Vec3b(srow[2], srow[1], srow[0]);
133  }
134 }
135 
137 {
138  cv::Vec4b* dptr = reinterpret_cast<cv::Vec4b*>(output->GetScalarPointer());
139  size_t elem_step = output->GetIncrements()[1]/sizeof(cv::Vec4b);
140 
141  for (int y = 0; y < source.rows; ++y)
142  {
143  cv::Vec4b* drow = dptr + elem_step * y;
144  const unsigned char *srow = source.ptr<unsigned char>(source.rows - (y + 1)); // vertical flip for texturing
145  for (int x = 0; x < source.cols; ++x, srow += source.channels())
146  drow[x] = cv::Vec4b(srow[2], srow[1], srow[0], srow[3]);
147  }
148 }
rtabmap::vtkImageMatSource::copyRGBAImage
static void copyRGBAImage(const cv::Mat &source, vtkSmartPointer< vtkImageData > output)
Definition: vtkImageMatSource.cpp:136
rtabmap::vtkImageMatSource::copyRGBImage
static void copyRGBImage(const cv::Mat &source, vtkSmartPointer< vtkImageData > output)
Definition: vtkImageMatSource.cpp:122
rtabmap::vtkImageMatSource::copyGrayImage
static void copyGrayImage(const cv::Mat &source, vtkSmartPointer< vtkImageData > output)
Definition: vtkImageMatSource.cpp:108
rtabmap::vtkImageMatSource::vtkImageMatSource
vtkImageMatSource()
Definition: vtkImageMatSource.cpp:57
this
this
rtabmap::vtkImageMatSource::SetImage
void SetImage(cv::InputArray image)
Definition: vtkImageMatSource.cpp:84
vtkSmartPointer
Definition: CloudViewer.h:72
y
Matrix3f y
rtabmap::vtkImageMatSource::RequestData
int RequestData(vtkInformation *, vtkInformationVector **, vtkInformationVector *)
Definition: vtkImageMatSource.cpp:75
source
const char * source
Definition: lz4hc.h:181
vtkImageMatSource.h
rtabmap::vtkImageMatSource::ImageData
vtkSmartPointer< vtkImageData > ImageData
Definition: vtkImageMatSource.h:66
x
x
rtabmap::vtkImageMatSource::RequestInformation
int RequestInformation(vtkInformation *, vtkInformationVector **, vtkInformationVector *)
Definition: vtkImageMatSource.cpp:63
rtabmap
Definition: CameraARCore.cpp:35
rtabmap::vtkStandardNewMacro
vtkStandardNewMacro(CloudViewerCellPicker)


rtabmap
Author(s): Mathieu Labbe
autogenerated on Thu Jul 25 2024 02:50:24