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.
10 // Intel License Agreement
11 // For Open Source Computer Vision Library
13 // Copyright (C) 2000, Intel Corporation, 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 Intel Corporation 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.
43 // 2004-03-16, Gabriel Schreiber <schreiber@ient.rwth-aachen.de>
44 // Mark Asbach <asbach@ient.rwth-aachen.de>
45 // Institute of Communications Engineering, RWTH Aachen University
47 // 2006-08-29 Roman Stanchak -- converted to use CvMat rather than IplImage
52 /// Accessor to convert a Python string into the imageData.
53 void CvMat_imageData_set(CvMat * self, PyObject* object)
55 char* py_string = PyString_AsString(object);
56 int depth = CV_MAT_DEPTH(self->type);
57 int cn = CV_MAT_CN(self->type);
59 int step = self->step ? self->step :
60 step = CV_ELEM_SIZE(self->type) * self->cols;
62 if (depth == CV_8U && cn==3){
64 // The data is reordered beause OpenCV uses BGR instead of RGB
66 for (long line = 0; line < self->rows; ++line)
67 for (long pixel = 0; pixel < self->cols; ++pixel)
69 // In OpenCV the beginning of the lines are aligned
70 // to 4 Bytes. So use step instead of cols.
71 long position = line*step + pixel*3;
72 long sourcepos = line*self->cols*3 + pixel*3;
73 self->data.ptr[position ] = py_string[sourcepos+2];
74 self->data.ptr[position+1] = py_string[sourcepos+1];
75 self->data.ptr[position+2] = py_string[sourcepos ];
78 else if (depth == CV_8U && cn==1)
80 // Grayscale 8bit case
82 for (long line = 0; line < self->rows; ++line)
84 // In OpenCV the beginning of the lines are aligned
85 // to 4 Bytes. So use step instead of cols.
88 self->data.ptr + line*step,
89 py_string + line*self->cols,
94 else if ( depth == CV_32F )
97 for (long line = 0; line < self->rows; ++line)
99 // here we don not have to care about alignment as the Floats are
100 // as long as the alignment
103 self->data.ptr + line*step,
104 py_string + line*self->cols*sizeof(float),
109 else if ( depth == CV_64F )
111 // double (64bit) case
112 for (long line = 0; line < self->rows; ++line)
114 // here we don not have to care about alignment as the Floats are
115 // as long as the alignment
118 self->data.ptr + line*step,
119 py_string + line*self->cols*sizeof(double),
127 SendErrorToPython (SWIG_TypeError,
128 "CvMat_imageData_set",
129 "cannot convert string data to this image format",
130 __FILE__, __LINE__, NULL);
134 /// Accessor to convert the imageData into a Python string.
135 PyObject* CvMat_imageData_get(CvMat * self)
139 PyErr_SetString(PyExc_TypeError, "Data pointer of CvMat is NULL");
142 int step = self->step ? self->step :
143 step = CV_ELEM_SIZE(self->type) * self->cols;
144 return PyString_FromStringAndSize((const char *)self->data.ptr, self->rows*step);
149 // add virtual member variable to CvMat
151 PyObject * imageData;