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 
52 namespace rtabmap {
53  vtkStandardNewMacro(vtkImageMatSource);
54 }
55 
57 {
58  this->SetNumberOfInputPorts(0);
59  this->ImageData = vtkSmartPointer<vtkImageData>::New();
60 }
61 
62 int rtabmap::vtkImageMatSource::RequestInformation(vtkInformation *, vtkInformationVector**, vtkInformationVector *outputVector)
63 {
64  vtkInformation* outInfo = outputVector->GetInformationObject(0);
65 
66  outInfo->Set(vtkStreamingDemandDrivenPipeline::WHOLE_EXTENT(), this->ImageData->GetExtent(), 6);
67  outInfo->Set(vtkDataObject::SPACING(), 1.0, 1.0, 1.0);
68  outInfo->Set(vtkDataObject::ORIGIN(), 0.0, 0.0, 0.0);
69 
70  vtkDataObject::SetPointDataActiveScalarInfo(outInfo, this->ImageData->GetScalarType(), this->ImageData->GetNumberOfScalarComponents());
71  return 1;
72 }
73 
74 int rtabmap::vtkImageMatSource::RequestData(vtkInformation*, vtkInformationVector**, vtkInformationVector *outputVector)
75 {
76  vtkInformation *outInfo = outputVector->GetInformationObject(0);
77 
78  vtkImageData *output = vtkImageData::SafeDownCast(outInfo->Get(vtkDataObject::DATA_OBJECT()) );
79  output->ShallowCopy(this->ImageData);
80  return 1;
81 }
82 
83 void rtabmap::vtkImageMatSource::SetImage(cv::InputArray _image)
84 {
85  CV_Assert(_image.depth() == CV_8U && (_image.channels() == 1 || _image.channels() == 3 || _image.channels() == 4));
86 
87  cv::Mat image = _image.getMat();
88 
89  this->ImageData->SetDimensions(image.cols, image.rows, 1);
90 #if VTK_MAJOR_VERSION <= 5
91  this->ImageData->SetNumberOfScalarComponents(image.channels());
92  this->ImageData->SetScalarTypeToUnsignedChar();
93  this->ImageData->AllocateScalars();
94 #else
95  this->ImageData->AllocateScalars(VTK_UNSIGNED_CHAR, image.channels());
96 #endif
97 
98  switch(image.channels())
99  {
100  case 1: copyGrayImage(image, this->ImageData); break;
101  case 3: copyRGBImage (image, this->ImageData); break;
102  case 4: copyRGBAImage(image, this->ImageData); break;
103  }
104  this->ImageData->Modified();
105 }
106 
108 {
109  unsigned char* dptr = reinterpret_cast<unsigned char*>(output->GetScalarPointer());
110  size_t elem_step = output->GetIncrements()[1]/sizeof(unsigned char);
111 
112  for (int y = 0; y < source.rows; ++y)
113  {
114  unsigned char* drow = dptr + elem_step * y;
115  const unsigned char *srow = source.ptr<unsigned char>(source.rows-(y+1)); // vertical flip for texturing
116  for (int x = 0; x < source.cols; ++x)
117  drow[x] = *srow++;
118  }
119 }
120 
122 {
123  cv::Vec3b* dptr = reinterpret_cast<cv::Vec3b*>(output->GetScalarPointer());
124  size_t elem_step = output->GetIncrements()[1]/sizeof(cv::Vec3b);
125 
126  for (int y = 0; y < source.rows; ++y)
127  {
128  cv::Vec3b* drow = dptr + elem_step * y;
129  const unsigned char *srow = source.ptr<unsigned char>(source.rows - (y + 1)); // vertical flip for texturing
130  for (int x = 0; x < source.cols; ++x, srow += source.channels())
131  drow[x] = cv::Vec3b(srow[2], srow[1], srow[0]);
132  }
133 }
134 
136 {
137  cv::Vec4b* dptr = reinterpret_cast<cv::Vec4b*>(output->GetScalarPointer());
138  size_t elem_step = output->GetIncrements()[1]/sizeof(cv::Vec4b);
139 
140  for (int y = 0; y < source.rows; ++y)
141  {
142  cv::Vec4b* drow = dptr + elem_step * y;
143  const unsigned char *srow = source.ptr<unsigned char>(source.rows - (y + 1)); // vertical flip for texturing
144  for (int x = 0; x < source.cols; ++x, srow += source.channels())
145  drow[x] = cv::Vec4b(srow[2], srow[1], srow[0], srow[3]);
146  }
147 }
vtkStandardNewMacro(CloudViewerCellPicker)
int RequestInformation(vtkInformation *, vtkInformationVector **, vtkInformationVector *)
const char * source
Definition: lz4hc.h:181
void SetImage(cv::InputArray image)
int RequestData(vtkInformation *, vtkInformationVector **, vtkInformationVector *)
static void copyRGBAImage(const cv::Mat &source, vtkSmartPointer< vtkImageData > output)
static void copyRGBImage(const cv::Mat &source, vtkSmartPointer< vtkImageData > output)
static void copyGrayImage(const cv::Mat &source, vtkSmartPointer< vtkImageData > output)


rtabmap
Author(s): Mathieu Labbe
autogenerated on Mon Dec 14 2020 03:37:06