1
/*M///////////////////////////////////////////////////////////////////////////////////////
3
// IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
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.
11
// For Open Source Computer Vision Library
13
// Copyright (C) 2013, OpenCV Foundation, all rights reserved.
14
// Third party copyrights are property of their respective owners.
16
// Redistribution and use in source and binary forms, with or without modification,
17
// are permitted provided that the following conditions are met:
19
// * Redistribution's of source code must retain the above copyright notice,
20
// this list of conditions and the following disclaimer.
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.
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.
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.
41
// * Anatoly Baksheev, Itseez Inc. myname.mysurname <> mycompany.com
45
#include "precomp.hpp"
47
namespace cv { namespace viz
49
vtkStandardNewMacro(vtkImageMatSource);
52
cv::viz::vtkImageMatSource::vtkImageMatSource()
54
this->SetNumberOfInputPorts(0);
55
this->ImageData = vtkSmartPointer<vtkImageData>::New();
58
int cv::viz::vtkImageMatSource::RequestInformation(vtkInformation *, vtkInformationVector**, vtkInformationVector *outputVector)
60
vtkInformation* outInfo = outputVector->GetInformationObject(0);
62
outInfo->Set(vtkStreamingDemandDrivenPipeline::WHOLE_EXTENT(), this->ImageData->GetExtent(), 6);
63
outInfo->Set(vtkDataObject::SPACING(), 1.0, 1.0, 1.0);
64
outInfo->Set(vtkDataObject::ORIGIN(), 0.0, 0.0, 0.0);
66
vtkDataObject::SetPointDataActiveScalarInfo(outInfo, this->ImageData->GetScalarType(), this->ImageData->GetNumberOfScalarComponents());
70
int cv::viz::vtkImageMatSource::RequestData(vtkInformation*, vtkInformationVector**, vtkInformationVector *outputVector)
72
vtkInformation *outInfo = outputVector->GetInformationObject(0);
74
vtkImageData *output = vtkImageData::SafeDownCast(outInfo->Get(vtkDataObject::DATA_OBJECT()) );
75
output->ShallowCopy(this->ImageData);
79
void cv::viz::vtkImageMatSource::SetImage(InputArray _image)
81
CV_Assert(_image.depth() == CV_8U && (_image.channels() == 1 || _image.channels() == 3 || _image.channels() == 4));
83
Mat image = _image.getMat();
85
this->ImageData->SetDimensions(image.cols, image.rows, 1);
86
#if VTK_MAJOR_VERSION <= 5
87
this->ImageData->SetNumberOfScalarComponents(image.channels());
88
this->ImageData->SetScalarTypeToUnsignedChar();
89
this->ImageData->AllocateScalars();
91
this->ImageData->AllocateScalars(VTK_UNSIGNED_CHAR, image.channels());
94
switch(image.channels())
96
case 1: copyGrayImage(image, this->ImageData); break;
97
case 3: copyRGBImage (image, this->ImageData); break;
98
case 4: copyRGBAImage(image, this->ImageData); break;
100
this->ImageData->Modified();
103
void cv::viz::vtkImageMatSource::copyGrayImage(const Mat &source, vtkSmartPointer<vtkImageData> output)
105
unsigned char* dptr = reinterpret_cast<unsigned char*>(output->GetScalarPointer());
106
size_t elem_step = output->GetIncrements()[1]/sizeof(unsigned char);
108
for (int y = 0; y < source.rows; ++y)
110
unsigned char* drow = dptr + elem_step * y;
111
const unsigned char *srow = source.ptr<unsigned char>(y);
112
for (int x = 0; x < source.cols; ++x)
117
void cv::viz::vtkImageMatSource::copyRGBImage(const Mat &source, vtkSmartPointer<vtkImageData> output)
119
Vec3b* dptr = reinterpret_cast<Vec3b*>(output->GetScalarPointer());
120
size_t elem_step = output->GetIncrements()[1]/sizeof(Vec3b);
122
for (int y = 0; y < source.rows; ++y)
124
Vec3b* drow = dptr + elem_step * y;
125
const unsigned char *srow = source.ptr<unsigned char>(y);
126
for (int x = 0; x < source.cols; ++x, srow += source.channels())
127
drow[x] = Vec3b(srow[2], srow[1], srow[0]);
131
void cv::viz::vtkImageMatSource::copyRGBAImage(const Mat &source, vtkSmartPointer<vtkImageData> output)
133
Vec4b* dptr = reinterpret_cast<Vec4b*>(output->GetScalarPointer());
134
size_t elem_step = output->GetIncrements()[1]/sizeof(Vec4b);
136
for (int y = 0; y < source.rows; ++y)
138
Vec4b* drow = dptr + elem_step * y;
139
const unsigned char *srow = source.ptr<unsigned char>(y);
140
for (int x = 0; x < source.cols; ++x, srow += source.channels())
141
drow[x] = Vec4b(srow[2], srow[1], srow[0], srow[3]);