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 "precomp.hpp"
46
47 namespace cv { namespace viz
48 {
49 vtkStandardNewMacro(vtkImageMatSource);
50 }}
51
vtkImageMatSource()52 cv::viz::vtkImageMatSource::vtkImageMatSource()
53 {
54 this->SetNumberOfInputPorts(0);
55 this->ImageData = vtkSmartPointer<vtkImageData>::New();
56 }
57
RequestInformation(vtkInformation *,vtkInformationVector **,vtkInformationVector * outputVector)58 int cv::viz::vtkImageMatSource::RequestInformation(vtkInformation *, vtkInformationVector**, vtkInformationVector *outputVector)
59 {
60 vtkInformation* outInfo = outputVector->GetInformationObject(0);
61
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);
65
66 vtkDataObject::SetPointDataActiveScalarInfo(outInfo, this->ImageData->GetScalarType(), this->ImageData->GetNumberOfScalarComponents());
67 return 1;
68 }
69
RequestData(vtkInformation *,vtkInformationVector **,vtkInformationVector * outputVector)70 int cv::viz::vtkImageMatSource::RequestData(vtkInformation*, vtkInformationVector**, vtkInformationVector *outputVector)
71 {
72 vtkInformation *outInfo = outputVector->GetInformationObject(0);
73
74 vtkImageData *output = vtkImageData::SafeDownCast(outInfo->Get(vtkDataObject::DATA_OBJECT()) );
75 output->ShallowCopy(this->ImageData);
76 return 1;
77 }
78
SetImage(InputArray _image)79 void cv::viz::vtkImageMatSource::SetImage(InputArray _image)
80 {
81 CV_Assert(_image.depth() == CV_8U && (_image.channels() == 1 || _image.channels() == 3 || _image.channels() == 4));
82
83 Mat image = _image.getMat();
84
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();
90 #else
91 this->ImageData->AllocateScalars(VTK_UNSIGNED_CHAR, image.channels());
92 #endif
93
94 switch(image.channels())
95 {
96 case 1: copyGrayImage(image, this->ImageData); break;
97 case 3: copyRGBImage (image, this->ImageData); break;
98 case 4: copyRGBAImage(image, this->ImageData); break;
99 }
100 this->ImageData->Modified();
101 }
102
copyGrayImage(const Mat & source,vtkSmartPointer<vtkImageData> output)103 void cv::viz::vtkImageMatSource::copyGrayImage(const Mat &source, vtkSmartPointer<vtkImageData> output)
104 {
105 unsigned char* dptr = reinterpret_cast<unsigned char*>(output->GetScalarPointer());
106 size_t elem_step = output->GetIncrements()[1]/sizeof(unsigned char);
107
108 for (int y = 0; y < source.rows; ++y)
109 {
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)
113 drow[x] = *srow++;
114 }
115 }
116
copyRGBImage(const Mat & source,vtkSmartPointer<vtkImageData> output)117 void cv::viz::vtkImageMatSource::copyRGBImage(const Mat &source, vtkSmartPointer<vtkImageData> output)
118 {
119 Vec3b* dptr = reinterpret_cast<Vec3b*>(output->GetScalarPointer());
120 size_t elem_step = output->GetIncrements()[1]/sizeof(Vec3b);
121
122 for (int y = 0; y < source.rows; ++y)
123 {
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]);
128 }
129 }
130
copyRGBAImage(const Mat & source,vtkSmartPointer<vtkImageData> output)131 void cv::viz::vtkImageMatSource::copyRGBAImage(const Mat &source, vtkSmartPointer<vtkImageData> output)
132 {
133 Vec4b* dptr = reinterpret_cast<Vec4b*>(output->GetScalarPointer());
134 size_t elem_step = output->GetIncrements()[1]/sizeof(Vec4b);
135
136 for (int y = 0; y < source.rows; ++y)
137 {
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]);
142 }
143 }
144