OSDN Git Service

first
[psychlops/cpp.git] / psychlops / extension / standard / cv / psychlops_cv1_bridge.cpp
1 /*\r
2  *  psychlops_FFTW_bridge.cpp\r
3  *  Psychlops Standard Library (Universal)\r
4  *\r
5  *  Last Modified 2010/03/05 by Kenchi HOSOKAWA\r
6  *  (C) 2010 Kenchi HOSOKAWA, Kazushi MARUYA, Takao SATO\r
7  */\r
8 \r
9 \r
10 #include "../../../psychlops_core.h"\r
11 #include "psychlops_cv1_bridge.h"\r
12 #include <opencv/cv.h>\r
13 \r
14
15 using namespace Psychlops;\r
16 \r
17 void putIPLImage(IplImage *img) {\r
18         std::cout << img->nSize << std::endl;\r
19         std::cout << img->ID << std::endl;\r
20         std::cout << img->nChannels << std::endl;\r
21         std::cout << img->alphaChannel << std::endl;\r
22         std::cout << img->depth << std::endl;\r
23         // std::cout << img->colorModel[4] << std::endl;\r
24         // std::cout << img->channelSeq[4] << std::endl;\r
25         std::cout << img->dataOrder << std::endl;\r
26         std::cout << img->origin << std::endl;\r
27         std::cout << img->align << std::endl;\r
28         std::cout << img->width << std::endl;\r
29         std::cout << img->height << std::endl;\r
30         //    struct _IplROI *roi << std::endl;\r
31         //    struct _IplImage *maskROI << std::endl;\r
32         //    void  *imageId << std::endl;\r
33         //    struct _IplTileInfo *tileInfo << std::endl;\r
34         std::cout << img->imageSize << std::endl;\r
35         //    char *imageData << std::endl;\r
36         std::cout << img->widthStep << std::endl;\r
37         // std::cout << img->BorderMode[4] << std::endl;\r
38         // std::cout << img->BorderConst[4] << std::endl;\r
39         //    char *imageDataOrigin << std::endl;\r
40 }\r
41 \r
42 IplImage formatIPLImageByPImage(Image &img) {\r
43         IplImage iplimg;\r
44 \r
45         iplimg.nSize = sizeof(IplImage);\r
46         iplimg.ID = 0;\r
47         //iplimg.colorModel = NULL;\r
48         //iplimg.channelSeq = NULL;\r
49         iplimg.roi = NULL;\r
50         iplimg.maskROI = NULL;\r
51         iplimg.imageId = NULL;\r
52         iplimg.tileInfo = NULL;\r
53         //iplimg.BorderMode = NULL;\r
54         //iplimg.BorderConst = NULL;\r
55 \r
56         switch(img.getComponentKind()) {\r
57                 case Image::GRAY:\r
58                         iplimg.nChannels = 1;\r
59                         break;\r
60                 case Image::RGB:\r
61                         iplimg.nChannels = 3;\r
62                         break;\r
63                 case Image::RGBA:\r
64                         iplimg.nChannels = 4;\r
65                         break;\r
66         }\r
67         switch(img.getPrecisionKind()) {\r
68                 case Image::BYTE:\r
69                         iplimg.depth = 8;\r
70                         break;\r
71         }\r
72         iplimg.alphaChannel = 0;\r
73         iplimg.dataOrder = 0;\r
74         iplimg.origin = 0;\r
75         iplimg.align = 4;\r
76         iplimg.width = img.getWidth();\r
77         iplimg.height = img.getHeight();\r
78         iplimg.widthStep = img.getLineBytes();\r
79         iplimg.imageSize = iplimg.widthStep * iplimg.height;\r
80 \r
81         return iplimg;\r
82 }\r
83 \r
84 IplImage adaptPImageToIPLImage(Image &img) {\r
85         IplImage iplimg = formatIPLImageByPImage(img);\r
86 \r
87         iplimg.imageData = (char*)img.getElementPtr();\r
88         iplimg.imageDataOrigin = (char*)img.getElementPtr();\r
89 \r
90         return iplimg;\r
91 }\r
92 \r
93 IplImage *convertPImageToIplImage(Image &source) {\r
94     IplImage* target;\r
95 \r
96         int widthStep;\r
97         char *img;\r
98         char pix[4];\r
99         Color c;\r
100 \r
101         switch(source.getComponentKind()) {\r
102                 case Image::GRAY:\r
103                         target = cvCreateImage(cvSize(source.getWidth(), source.getHeight()), IPL_DEPTH_8U, 1 );\r
104                         widthStep = target->widthStep;\r
105                         img = target->imageData;\r
106                         for(int y=0; y<source.getHeight(); y++) {\r
107                                 for(int x=0; x<source.getWidth(); x++) {\r
108                                         c = source.getPix(x,y);\r
109                                         img[widthStep * y + x] = (unsigned char)(c.getR()*255.0+.5);\r
110                                 }\r
111                         }\r
112                         break;\r
113                 case Image::RGB:\r
114                         target = cvCreateImage(cvSize(source.getWidth(), source.getHeight()), IPL_DEPTH_8U, 3 );\r
115                         widthStep = target->widthStep;\r
116                         img = target->imageData;\r
117                         for(int y=0; y<source.getHeight(); y++) {\r
118                                 for(int x=0; x<source.getWidth(); x++) {\r
119                                         c = source.getPix(x,y);\r
120                                         img[widthStep * y + x * 3    ] = (unsigned char)(c.getB()*255.0+.5);\r
121                                         img[widthStep * y + x * 3 + 1] = (unsigned char)(c.getG()*255.0+.5);\r
122                                         img[widthStep * y + x * 3 + 2] = (unsigned char)(c.getR()*255.0+.5);\r
123                                 }\r
124                         }\r
125                         break;\r
126                 case Image::RGBA:\r
127                         target = cvCreateImage(cvSize(source.getWidth(), source.getHeight()), IPL_DEPTH_8U, 4 );\r
128                         widthStep = target->widthStep;\r
129                         img = target->imageData;\r
130                         for(int y=0; y<source.getHeight(); y++) {\r
131                                 for(int x=0; x<source.getWidth(); x++) {\r
132                                         c = source.getPix(x,y);\r
133                                         img[widthStep * y + x * 4    ] = (unsigned char)(c.getB()*255.0+.5);\r
134                                         img[widthStep * y + x * 4 + 1] = (unsigned char)(c.getG()*255.0+.5);\r
135                                         img[widthStep * y + x * 4 + 2] = (unsigned char)(c.getR()*255.0+.5);\r
136                                         img[widthStep * y + x * 4 + 3] = (unsigned char)(c.getA()*255.0+.5);\r
137                                 }\r
138                         }\r
139                         break;\r
140                 default:\r
141                         break;\r
142         }\r
143         return target;\r
144 }\r
145 \r
146 void convertIplImageToPImage(IplImage *source, Image &target) {\r
147         int widthStep = source->widthStep;\r
148         int lineBytes;\r
149         unsigned char *img_b = (unsigned char *)source->imageData;\r
150         unsigned char *img = img_b;\r
151         unsigned char *timg;\r
152         unsigned char *timg_b;\r
153         unsigned char pix[4];\r
154         Color c;\r
155 \r
156         switch(source->nChannels) {\r
157                 case 1:\r
158                         if(target.getComponentKind() != Image::GRAY || target.getWidth() != source->width || target.getHeight() != source->height ) {\r
159                                 target.release();\r
160                                 target.set(source->width, source->height, Image::GRAY);\r
161                         }\r
162                         lineBytes = target.getLineBytes();\r
163                         timg_b = target.getElementPtr();\r
164                         for(int y=0; y<target.getHeight(); y++) {\r
165                                 img = img_b + widthStep*y;\r
166                                 timg = timg_b + lineBytes*((source->height-y)-1);\r
167                                 for(int x=0; x<target.getWidth(); x++) {\r
168                                         *(timg++) = (unsigned char)img[0];\r
169                                         img += 1;\r
170                                 }\r
171                         }\r
172                         break;\r
173                 case 3:\r
174                         if(target.getComponentKind() != Image::RGB || target.getWidth() != source->width || target.getHeight() != source->height) {\r
175                                 target.release();\r
176                                 target.set(source->width, source->height, Image::RGB);\r
177                         }\r
178                         lineBytes = target.getLineBytes();\r
179                         timg_b = target.getElementPtr();\r
180                         for(int y=0; y<target.getHeight(); y++) {\r
181                                 img = img_b + widthStep*y;\r
182                                 timg = timg_b + lineBytes*((source->height-y)-1);\r
183                                 for(int x=0; x<target.getWidth(); x++) {\r
184                                         *(timg++) = (unsigned char)img[2];\r
185                                         *(timg++) = (unsigned char)img[1];\r
186                                         *(timg++) = (unsigned char)img[0];\r
187                                         img += 3;\r
188                                 }\r
189                         }\r
190                         break;\r
191                 case 4:\r
192                         if(target.getComponentKind() != Image::RGBA || target.getWidth() != source->width || target.getHeight() != source->height) {\r
193                                 target.release();\r
194                                 target.set(source->width, source->height, Image::RGBA);\r
195                         }\r
196                         lineBytes = target.getLineBytes();\r
197                         timg_b = target.getElementPtr();\r
198                         for(int y=0; y<target.getHeight(); y++) {\r
199                                 img = img_b + widthStep*y;\r
200                                 timg = timg_b + lineBytes*((source->height-y)-1);\r
201                                 for(int x=0; x<target.getWidth(); x++) {\r
202                                         *(timg++) = (unsigned char)img[2];\r
203                                         *(timg++) = (unsigned char)img[1];\r
204                                         *(timg++) = (unsigned char)img[0];\r
205                                         *(timg++) = (unsigned char)img[3];\r
206                                         img += 4;\r
207                                 }\r
208                         }\r
209                         break;\r
210                 default:\r
211                         break;\r
212         }\r
213 \r
214 \r
215         /* This code works (at least)\r
216         switch(source->nChannels) {\r
217                 case 1:\r
218                         if(target.getComponentKind() != Image::GRAY || target.getWidth() != source->width || target.getHeight() != source->height ) {\r
219                                 target.release();\r
220                                 target.set(source->width, source->height, Image::GRAY);\r
221                         }\r
222                         for(int y=0; y<target.getHeight(); y++) {\r
223                                 for(int x=0; x<target.getWidth(); x++) {\r
224                                         pix[0] = (unsigned char)img[widthStep * y + x];\r
225                                         target.pix_direct(x,y, c.set(pix[0]/255.0));\r
226                                 }\r
227                         }\r
228                         break;\r
229                 case 3:\r
230                         if(target.getComponentKind() != Image::RGB || target.getWidth() != source->width || target.getHeight() != source->height) {\r
231                                 target.release();\r
232                                 target.set(source->width, source->height, Image::RGB);\r
233                         }\r
234                         for(int y=0; y<target.getHeight(); y++) {\r
235                                 for(int x=0; x<target.getWidth(); x++) {\r
236                                         pix[0] = (unsigned char)img[widthStep * y + x * 3];\r
237                                         pix[1] = (unsigned char)img[widthStep * y + x * 3 + 1];\r
238                                         pix[2] = (unsigned char)img[widthStep * y + x * 3 + 2];\r
239                                         target.pix_direct(x,y, c.set(pix[2]/255.0, pix[1]/255.0, pix[0]/255.0));\r
240                                 }\r
241                         }\r
242                         break;\r
243                 case 4:\r
244                         if(target.getComponentKind() != Image::RGBA || target.getWidth() != source->width || target.getHeight() != source->height) {\r
245                                 target.release();\r
246                                 target.set(source->width, source->height, Image::RGBA);\r
247                         }\r
248                         for(int y=0; y<target.getHeight(); y++) {\r
249                                 for(int x=0; x<target.getWidth(); x++) {\r
250                                         pix[0] = (unsigned char)img[widthStep * y + x * 4];\r
251                                         pix[1] = (unsigned char)img[widthStep * y + x * 4 + 1];\r
252                                         pix[2] = (unsigned char)img[widthStep * y + x * 4 + 2];\r
253                                         pix[3] = (unsigned char)img[widthStep * y + x * 4 + 3];\r
254                                         target.pix_direct(x,y, c.set(pix[2]/255.0, pix[1]/255.0, pix[0]/255.0, pix[3]/255.0));\r
255                                 }\r
256                         }\r
257                         break;\r
258                 default:\r
259                         break;\r
260         }\r
261         */\r
262 \r
263 \r
264         /* Imcompatible between RGBA and BGRA\r
265         switch(source->nChannels) {\r
266                 case 1:\r
267                         if(target.getComponentKind() != Image::GRAY || target.getWidth() != source->width || target.getHeight() != source->height ) {\r
268                                 target.release();\r
269                                 target.set(source->width, source->height, Image::GRAY);\r
270                         }\r
271                         break;\r
272                 case 3:\r
273                         if(target.getComponentKind() != Image::RGB || target.getWidth() != source->width || target.getHeight() != source->height ) {\r
274                                 target.release();\r
275                                 target.set(source->width, source->height, Image::GRAY);\r
276                         }\r
277                         break;\r
278                 case 4:\r
279                         if(target.getComponentKind() != Image::RGBA || target.getWidth() != source->width || target.getHeight() != source->height ) {\r
280                                 target.release();\r
281                                 target.set(source->width, source->height, Image::GRAY);\r
282                         }\r
283                         break;\r
284                 default:\r
285                         break;\r
286         }\r
287         lineBytes = target.getLineBytes();\r
288         timg = target.getElementPtr() + lineBytes*(source->height-1);\r
289         for(int y=0; y<target.getHeight(); y++) {\r
290                 memcpy(timg, img, widthStep);\r
291                 img += widthStep;\r
292                 timg -= lineBytes;\r
293         }\r
294         */\r
295 }\r
296 \r
297 \r
298 \r
299 namespace Psychlops {\r
300 /*\r
301         void Image::to(IplImage *target) const\r
302         {\r
303                 target = convertPImageToIplImage(*this);\r
304         }
305 \r
306         void Image::from(IplImage &source)\r
307         {\r
308                 convertIplImageToPImage(&source, *this);\r
309         }\r
310 */\r
311 \r
312 }       /*      <- namespace Psycholops         */\r