vtkImageMatSource.cpp
Go to the documentation of this file.
00001 /*M///////////////////////////////////////////////////////////////////////////////////////
00002 //
00003 //  IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
00004 //
00005 //  By downloading, copying, installing or using the software you agree to this license.
00006 //  If you do not agree to this license, do not download, install,
00007 //  copy or use the software.
00008 //
00009 //
00010 //                           License Agreement
00011 //                For Open Source Computer Vision Library
00012 //
00013 // Copyright (C) 2013, OpenCV Foundation, all rights reserved.
00014 // Third party copyrights are property of their respective owners.
00015 //
00016 // Redistribution and use in source and binary forms, with or without modification,
00017 // are permitted provided that the following conditions are met:
00018 //
00019 //   * Redistribution's of source code must retain the above copyright notice,
00020 //     this list of conditions and the following disclaimer.
00021 //
00022 //   * Redistribution's in binary form must reproduce the above copyright notice,
00023 //     this list of conditions and the following disclaimer in the documentation
00024 //     and/or other materials provided with the distribution.
00025 //
00026 //   * The name of the copyright holders may not be used to endorse or promote products
00027 //     derived from this software without specific prior written permission.
00028 //
00029 // This software is provided by the copyright holders and contributors "as is" and
00030 // any express or implied warranties, including, but not limited to, the implied
00031 // warranties of merchantability and fitness for a particular purpose are disclaimed.
00032 // In no event shall the Intel Corporation or contributors be liable for any direct,
00033 // indirect, incidental, special, exemplary, or consequential damages
00034 // (including, but not limited to, procurement of substitute goods or services;
00035 // loss of use, data, or profits; or business interruption) however caused
00036 // and on any theory of liability, whether in contract, strict liability,
00037 // or tort (including negligence or otherwise) arising in any way out of
00038 // the use of this software, even if advised of the possibility of such damage.
00039 //
00040 // Authors:
00041 //  * Anatoly Baksheev, Itseez Inc.  myname.mysurname <> mycompany.com
00042 //
00043 //M*/
00044 
00045 #include "vtkImageMatSource.h"
00046 #include <vtkImageData.h>
00047 #include <vtkInformation.h>
00048 #include <vtkInformationVector.h>
00049 #include <vtkStreamingDemandDrivenPipeline.h>
00050 #include <vtkObjectFactory.h>
00051 
00052 namespace rtabmap {
00053         vtkStandardNewMacro(vtkImageMatSource);
00054 }
00055 
00056 rtabmap::vtkImageMatSource::vtkImageMatSource()
00057 {
00058     this->SetNumberOfInputPorts(0);
00059     this->ImageData = vtkSmartPointer<vtkImageData>::New();
00060 }
00061 
00062 int rtabmap::vtkImageMatSource::RequestInformation(vtkInformation *, vtkInformationVector**, vtkInformationVector *outputVector)
00063 {
00064     vtkInformation* outInfo = outputVector->GetInformationObject(0);
00065 
00066     outInfo->Set(vtkStreamingDemandDrivenPipeline::WHOLE_EXTENT(), this->ImageData->GetExtent(), 6);
00067     outInfo->Set(vtkDataObject::SPACING(), 1.0, 1.0, 1.0);
00068     outInfo->Set(vtkDataObject::ORIGIN(),  0.0, 0.0, 0.0);
00069 
00070     vtkDataObject::SetPointDataActiveScalarInfo(outInfo, this->ImageData->GetScalarType(), this->ImageData->GetNumberOfScalarComponents());
00071     return 1;
00072 }
00073 
00074 int rtabmap::vtkImageMatSource::RequestData(vtkInformation*, vtkInformationVector**, vtkInformationVector *outputVector)
00075 {
00076      vtkInformation *outInfo = outputVector->GetInformationObject(0);
00077 
00078      vtkImageData *output = vtkImageData::SafeDownCast(outInfo->Get(vtkDataObject::DATA_OBJECT()) );
00079      output->ShallowCopy(this->ImageData);
00080      return 1;
00081 }
00082 
00083 void rtabmap::vtkImageMatSource::SetImage(cv::InputArray _image)
00084 {
00085     CV_Assert(_image.depth() == CV_8U && (_image.channels() == 1 || _image.channels() == 3 || _image.channels() == 4));
00086 
00087         cv::Mat image = _image.getMat();
00088 
00089     this->ImageData->SetDimensions(image.cols, image.rows, 1);
00090 #if VTK_MAJOR_VERSION <= 5
00091     this->ImageData->SetNumberOfScalarComponents(image.channels());
00092     this->ImageData->SetScalarTypeToUnsignedChar();
00093     this->ImageData->AllocateScalars();
00094 #else
00095     this->ImageData->AllocateScalars(VTK_UNSIGNED_CHAR, image.channels());
00096 #endif
00097 
00098     switch(image.channels())
00099     {
00100     case 1: copyGrayImage(image, this->ImageData); break;
00101     case 3: copyRGBImage (image, this->ImageData); break;
00102     case 4: copyRGBAImage(image, this->ImageData); break;
00103     }
00104     this->ImageData->Modified();
00105 }
00106 
00107 void rtabmap::vtkImageMatSource::copyGrayImage(const cv::Mat &source, vtkSmartPointer<vtkImageData> output)
00108 {
00109     unsigned char* dptr = reinterpret_cast<unsigned char*>(output->GetScalarPointer());
00110     size_t elem_step = output->GetIncrements()[1]/sizeof(unsigned char);
00111 
00112     for (int y = 0; y < source.rows; ++y)
00113     {
00114         unsigned char* drow = dptr + elem_step * y;
00115         const unsigned char *srow = source.ptr<unsigned char>(source.rows-(y+1)); // vertical flip for texturing
00116         for (int x = 0; x < source.cols; ++x)
00117             drow[x] = *srow++;
00118     }
00119 }
00120 
00121 void rtabmap::vtkImageMatSource::copyRGBImage(const cv::Mat &source, vtkSmartPointer<vtkImageData> output)
00122 {
00123         cv::Vec3b* dptr = reinterpret_cast<cv::Vec3b*>(output->GetScalarPointer());
00124     size_t elem_step = output->GetIncrements()[1]/sizeof(cv::Vec3b);
00125 
00126     for (int y = 0; y < source.rows; ++y)
00127     {
00128                 cv::Vec3b* drow = dptr + elem_step * y;
00129         const unsigned char *srow = source.ptr<unsigned char>(source.rows - (y + 1)); // vertical flip for texturing
00130         for (int x = 0; x < source.cols; ++x, srow += source.channels())
00131             drow[x] = cv::Vec3b(srow[2], srow[1], srow[0]);
00132     }
00133 }
00134 
00135 void rtabmap::vtkImageMatSource::copyRGBAImage(const cv::Mat &source, vtkSmartPointer<vtkImageData> output)
00136 {
00137     cv::Vec4b* dptr = reinterpret_cast<cv::Vec4b*>(output->GetScalarPointer());
00138     size_t elem_step = output->GetIncrements()[1]/sizeof(cv::Vec4b);
00139 
00140     for (int y = 0; y < source.rows; ++y)
00141     {
00142                 cv::Vec4b* drow = dptr + elem_step * y;
00143         const unsigned char *srow = source.ptr<unsigned char>(source.rows - (y + 1)); // vertical flip for texturing
00144         for (int x = 0; x < source.cols; ++x, srow += source.channels())
00145             drow[x] = cv::Vec4b(srow[2], srow[1], srow[0], srow[3]);
00146     }
00147 }


rtabmap
Author(s): Mathieu Labbe
autogenerated on Thu Jun 6 2019 21:59:32