2 * psychlops_FFTW_bridge.cpp
\r
3 * Psychlops Standard Library (Universal)
\r
5 * Last Modified 2010/03/05 by Kenchi HOSOKAWA
\r
6 * (C) 2010 Kenchi HOSOKAWA, Kazushi MARUYA, Takao SATO
\r
10 #include "../../../psychlops_core.h"
\r
11 #include "psychlops_cv1_bridge.h"
\r
12 #include <opencv/cv.h>
\r
15 using namespace Psychlops;
\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
42 IplImage formatIPLImageByPImage(Image &img) {
\r
45 iplimg.nSize = sizeof(IplImage);
\r
47 //iplimg.colorModel = NULL;
\r
48 //iplimg.channelSeq = 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
56 switch(img.getComponentKind()) {
\r
58 iplimg.nChannels = 1;
\r
61 iplimg.nChannels = 3;
\r
64 iplimg.nChannels = 4;
\r
67 switch(img.getPrecisionKind()) {
\r
72 iplimg.alphaChannel = 0;
\r
73 iplimg.dataOrder = 0;
\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
84 IplImage adaptPImageToIPLImage(Image &img) {
\r
85 IplImage iplimg = formatIPLImageByPImage(img);
\r
87 iplimg.imageData = (char*)img.getElementPtr();
\r
88 iplimg.imageDataOrigin = (char*)img.getElementPtr();
\r
93 IplImage *convertPImageToIplImage(Image &source) {
\r
101 switch(source.getComponentKind()) {
\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
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
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
146 void convertIplImageToPImage(IplImage *source, Image &target) {
\r
147 int widthStep = source->widthStep;
\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
156 switch(source->nChannels) {
\r
158 if(target.getComponentKind() != Image::GRAY || target.getWidth() != source->width || target.getHeight() != source->height ) {
\r
160 target.set(source->width, source->height, Image::GRAY);
\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
174 if(target.getComponentKind() != Image::RGB || target.getWidth() != source->width || target.getHeight() != source->height) {
\r
176 target.set(source->width, source->height, Image::RGB);
\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
192 if(target.getComponentKind() != Image::RGBA || target.getWidth() != source->width || target.getHeight() != source->height) {
\r
194 target.set(source->width, source->height, Image::RGBA);
\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
215 /* This code works (at least)
\r
216 switch(source->nChannels) {
\r
218 if(target.getComponentKind() != Image::GRAY || target.getWidth() != source->width || target.getHeight() != source->height ) {
\r
220 target.set(source->width, source->height, Image::GRAY);
\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
230 if(target.getComponentKind() != Image::RGB || target.getWidth() != source->width || target.getHeight() != source->height) {
\r
232 target.set(source->width, source->height, Image::RGB);
\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
244 if(target.getComponentKind() != Image::RGBA || target.getWidth() != source->width || target.getHeight() != source->height) {
\r
246 target.set(source->width, source->height, Image::RGBA);
\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
264 /* Imcompatible between RGBA and BGRA
\r
265 switch(source->nChannels) {
\r
267 if(target.getComponentKind() != Image::GRAY || target.getWidth() != source->width || target.getHeight() != source->height ) {
\r
269 target.set(source->width, source->height, Image::GRAY);
\r
273 if(target.getComponentKind() != Image::RGB || target.getWidth() != source->width || target.getHeight() != source->height ) {
\r
275 target.set(source->width, source->height, Image::GRAY);
\r
279 if(target.getComponentKind() != Image::RGBA || target.getWidth() != source->width || target.getHeight() != source->height ) {
\r
281 target.set(source->width, source->height, Image::GRAY);
\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
299 namespace Psychlops {
\r
301 void Image::to(IplImage *target) const
\r
303 target = convertPImageToIplImage(*this);
\r
306 void Image::from(IplImage &source)
\r
308 convertIplImageToPImage(&source, *this);
\r
312 } /* <- namespace Psycholops */
\r