Path: blob/master/modules/viz/src/vtk/vtkImageMatSource.cpp
16358 views
/*M///////////////////////////////////////////////////////////////////////////////////////1//2// IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.3//4// By downloading, copying, installing or using the software you agree to this license.5// If you do not agree to this license, do not download, install,6// copy or use the software.7//8//9// License Agreement10// For Open Source Computer Vision Library11//12// Copyright (C) 2013, OpenCV Foundation, all rights reserved.13// Third party copyrights are property of their respective owners.14//15// Redistribution and use in source and binary forms, with or without modification,16// are permitted provided that the following conditions are met:17//18// * Redistribution's of source code must retain the above copyright notice,19// this list of conditions and the following disclaimer.20//21// * Redistribution's in binary form must reproduce the above copyright notice,22// this list of conditions and the following disclaimer in the documentation23// and/or other materials provided with the distribution.24//25// * The name of the copyright holders may not be used to endorse or promote products26// derived from this software without specific prior written permission.27//28// This software is provided by the copyright holders and contributors "as is" and29// any express or implied warranties, including, but not limited to, the implied30// warranties of merchantability and fitness for a particular purpose are disclaimed.31// In no event shall the Intel Corporation or contributors be liable for any direct,32// indirect, incidental, special, exemplary, or consequential damages33// (including, but not limited to, procurement of substitute goods or services;34// loss of use, data, or profits; or business interruption) however caused35// and on any theory of liability, whether in contract, strict liability,36// or tort (including negligence or otherwise) arising in any way out of37// the use of this software, even if advised of the possibility of such damage.38//39// Authors:40// * Anatoly Baksheev, Itseez Inc. myname.mysurname <> mycompany.com41//42//M*/4344#include "../precomp.hpp"4546namespace cv { namespace viz47{48vtkStandardNewMacro(vtkImageMatSource);49}}5051cv::viz::vtkImageMatSource::vtkImageMatSource()52{53this->SetNumberOfInputPorts(0);54this->ImageData = vtkSmartPointer<vtkImageData>::New();55}5657int cv::viz::vtkImageMatSource::RequestInformation(vtkInformation *, vtkInformationVector**, vtkInformationVector *outputVector)58{59vtkInformation* outInfo = outputVector->GetInformationObject(0);6061outInfo->Set(vtkStreamingDemandDrivenPipeline::WHOLE_EXTENT(), this->ImageData->GetExtent(), 6);62outInfo->Set(vtkDataObject::SPACING(), 1.0, 1.0, 1.0);63outInfo->Set(vtkDataObject::ORIGIN(), 0.0, 0.0, 0.0);6465vtkDataObject::SetPointDataActiveScalarInfo(outInfo, this->ImageData->GetScalarType(), this->ImageData->GetNumberOfScalarComponents());66return 1;67}6869int cv::viz::vtkImageMatSource::RequestData(vtkInformation*, vtkInformationVector**, vtkInformationVector *outputVector)70{71vtkInformation *outInfo = outputVector->GetInformationObject(0);7273vtkImageData *output = vtkImageData::SafeDownCast(outInfo->Get(vtkDataObject::DATA_OBJECT()) );74output->ShallowCopy(this->ImageData);75return 1;76}7778void cv::viz::vtkImageMatSource::SetImage(InputArray _image)79{80CV_Assert(_image.depth() == CV_8U && (_image.channels() == 1 || _image.channels() == 3 || _image.channels() == 4));8182Mat image = _image.getMat();8384this->ImageData->SetDimensions(image.cols, image.rows, 1);85#if VTK_MAJOR_VERSION <= 586this->ImageData->SetNumberOfScalarComponents(image.channels());87this->ImageData->SetScalarTypeToUnsignedChar();88this->ImageData->AllocateScalars();89#else90this->ImageData->AllocateScalars(VTK_UNSIGNED_CHAR, image.channels());91#endif9293switch(image.channels())94{95case 1: copyGrayImage(image, this->ImageData); break;96case 3: copyRGBImage (image, this->ImageData); break;97case 4: copyRGBAImage(image, this->ImageData); break;98}99this->ImageData->Modified();100}101102void cv::viz::vtkImageMatSource::copyGrayImage(const Mat &source, vtkSmartPointer<vtkImageData> output)103{104unsigned char* dptr = reinterpret_cast<unsigned char*>(output->GetScalarPointer());105size_t elem_step = output->GetIncrements()[1]/sizeof(unsigned char);106107for (int y = 0; y < source.rows; ++y)108{109unsigned char* drow = dptr + elem_step * y;110const unsigned char *srow = source.ptr<unsigned char>(y);111for (int x = 0; x < source.cols; ++x)112drow[x] = *srow++;113}114}115116void cv::viz::vtkImageMatSource::copyRGBImage(const Mat &source, vtkSmartPointer<vtkImageData> output)117{118Vec3b* dptr = reinterpret_cast<Vec3b*>(output->GetScalarPointer());119size_t elem_step = output->GetIncrements()[1]/sizeof(Vec3b);120121for (int y = 0; y < source.rows; ++y)122{123Vec3b* drow = dptr + elem_step * y;124const unsigned char *srow = source.ptr<unsigned char>(y);125for (int x = 0; x < source.cols; ++x, srow += source.channels())126drow[x] = Vec3b(srow[2], srow[1], srow[0]);127}128}129130void cv::viz::vtkImageMatSource::copyRGBAImage(const Mat &source, vtkSmartPointer<vtkImageData> output)131{132Vec4b* dptr = reinterpret_cast<Vec4b*>(output->GetScalarPointer());133size_t elem_step = output->GetIncrements()[1]/sizeof(Vec4b);134135for (int y = 0; y < source.rows; ++y)136{137Vec4b* drow = dptr + elem_step * y;138const unsigned char *srow = source.ptr<unsigned char>(y);139for (int x = 0; x < source.cols; ++x, srow += source.channels())140drow[x] = Vec4b(srow[2], srow[1], srow[0], srow[3]);141}142}143144145