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.
51 #define RENDER_FRAMERATE 1
56 printf("Assertion: %s %s: %d\n", #exp, __FILE__, __LINE__);\
60 static void icvvResizeImage( int src_w, int src_h, int src_s, char* src,
61 int dst_w, int dst_h, int dst_s, char* dst,
67 int* x_array = (int*)malloc(sizeof(int) * dst_w);
68 int* y_array = (int*)malloc(sizeof(int) * dst_h);
69 float x_step = (float)(src_w - 1) / (dst_w - 1);
70 float y_step = (float)(src_h - 1) / (dst_h - 1);
75 Assert( src_w > 0 && src_h > 0 && src_s >= src_w * depth / 8 );
76 Assert( dst_w > 0 && dst_h > 0 && dst_s >= dst_w * depth / 8 );
78 if( src_w == dst_w && src_h == dst_h && src_s == dst_s )
80 memcpy( dst, src, src_s * src_h );
85 for( x = 0; x < dst_w; x++ )
86 x_array[x] = (int)(x_step * x + 0.5);
87 for( y = 0; y < dst_h; y++ )
88 y_array[y] = (int)(y_step * y + 0.5);
90 Assert(x_array[dst_w - 1] == src_w - 1);
91 Assert(y_array[dst_h - 1] == src_h - 1);
96 for( y = 0; y < dst_h; y++, dst += dst_s )
98 _src = src + y_array[y] * src_s;
99 for( x = 0; x < dst_w; x++ )
101 int offset = x_array[x];
102 dst[x] = _src[offset];
107 for( y = 0; y < dst_h; y++, dst += dst_s )
109 _src = src + y_array[y] * src_s;
110 for( x = 0; x < dst_w; x++ )
112 int offset = x_array[x];
113 ((short*)dst)[x] = ((short*)_src)[offset];
118 for( y = 0; y < dst_h; y++, dst += dst_s )
120 _src = src + y_array[y] * src_s;
121 for( x = 0; x < dst_w; x++ )
123 int offset = x_array[x] * 3;
124 dst[x * 3] = _src[offset];
125 dst[x * 3 + 1] = _src[offset + 1];
126 dst[x * 3 + 2] = _src[offset + 2];
131 for( y = 0; y < dst_h; y++, dst += dst_s )
133 _src = src + y_array[y] * src_s;
134 for( x = 0; x < dst_w; x++ )
136 int offset = x_array[x];
137 ((int*)dst)[x] = ((int*)_src)[offset];
146 int icvVideoRenderStart(int cameraid)
150 if( !cameras[cameraid].rendered)
153 if( !cameras[cameraid].window)
156 if(pthread_create(&thread, NULL, icvVideoRender, (void*)cameraid))
158 fprintf(stderr, "icvVideoRenderStart: failed create thread for rendering");
165 ////////////////////////////////////////////////////////////////////////////////
166 void* icvVideoRender(void* data)
168 int cameraid = (int)data;
169 CvVideoCamera *const camera = &(cameras[cameraid]);
173 char* display_name = NULL;
174 Window window = camera->window;
175 XWindowAttributes windowattr;
179 XShmSegmentInfo xshmseg;
180 int width = (camera->renderwidth>0)?camera->renderwidth:camera->videopp.width;
181 int height = camera->renderheight?camera->renderheight:camera->videopp.height;
182 int picturedepth = camera->videopp.picture.depth;
190 uchar* imgdata = NULL;
191 uchar* tmpbuff = NULL;
193 pthread_mutex_lock(&(camera->capturestatemutex));
194 if(camera->capturestate != CAPTURING)
196 pthread_mutex_unlock(&(camera->capturestatemutex));
197 pthread_exit( NULL );
199 camera->renderstate=1;
200 pthread_cond_signal(&(camera->capturestatecond));
201 pthread_mutex_unlock(&(camera->capturestatemutex));
204 if ( (display=XOpenDisplay(display_name)) == NULL )
207 fprintf( stderr, "cvVideo: cannot connect to X server %s\n",
208 XDisplayName(display_name));
209 pthread_exit( NULL );
212 screen_num = DefaultScreen(display);
214 if (XGetWindowAttributes(display, window,
217 fprintf(stderr, "icvVideoRender: failed to get window attributes.\n" );
221 if(windowattr.map_state == IsUnmapped)
223 fprintf(stderr, "icvVideoRender: window is not mapped \n" );
227 windowdepth = windowattr.depth;
228 visual = windowattr.visual;
230 pixelsize = icvVideoWindowPixelsize(windowdepth);
232 XShm = XShmQueryExtension(display);
235 image = XShmCreateImage(display, visual, windowdepth, ZPixmap, NULL,
236 &xshmseg, width, height );
240 xshmseg.shmid = shmget (IPC_PRIVATE,
241 width*height*pixelsize, IPC_CREAT|0777);
243 assert(xshmseg.shmid != -1);
244 xshmseg.shmaddr = image->data=(char*)shmat (xshmseg.shmid, 0, 0) ;
246 xshmseg.readOnly = False;
248 XShmAttach (display, &xshmseg);
252 imgdata = (uchar*)malloc(width*height*icvVideoWindowPixelsize(windowdepth)) ;
253 image = XCreateImage(display, visual, windowdepth, ZPixmap, 0,
254 (char*)imgdata, width,
256 icvVideoWindowPixelsize(windowdepth)
263 gc = XCreateGC(display,window,0, &values );
264 #ifdef RENDER_FRAMERATE
268 pthread_mutex_lock(&(camera->capturestatemutex));
269 while((camera->capturestate == CAPTURING) && (camera->rendered))
271 pthread_mutex_unlock(&(camera->capturestatemutex));
272 pthread_mutex_lock(&(camera->updatedmutex));
273 while(camera->updated == 0)
274 pthread_cond_wait(&(camera->updatedcond), &(camera->updatedmutex));
276 pthread_mutex_unlock(&(camera->updatedmutex));
277 if(cvcamGetProperty(cameraid, "raw_image",&iplimage ))
280 if(camera->callback != NULL)
281 camera->callback(iplimage);
282 if((width==camera->videopp.width)&&
283 (height==camera->videopp.height))
285 icvvConvert(width, height, width*picturedepth/8, picturedepth,
286 iplimage->imageData, width*pixelsize, pixelsize*8, image->data
288 cvReleaseImage(&iplimage);
292 tmpbuff = (uchar*)malloc(camera->videopp.width*camera->videopp.height*
295 icvvConvert(camera->videopp.width, camera->videopp.height,
296 camera->videopp.width*picturedepth/8, picturedepth,
297 iplimage->imageData, camera->videopp.width*pixelsize,
298 pixelsize*8, (char*)tmpbuff);
299 cvReleaseImage(&iplimage);
301 icvvResizeImage(camera->videopp.width,
302 camera->videopp.height,
303 (camera->videopp.width)*pixelsize, (char*)tmpbuff,
304 width, height,width*pixelsize, image->data, pixelsize*8);
310 //fprintf(stdout, "cvVideoRendering:image converted!!!!\n");
314 XShmPutImage(display, window, gc,
315 image,0,0,0,0, width,
320 XPutImage(display, window, gc,
321 image,0,0,0,0, width,
325 XSync(display, False);
326 #ifdef RENDER_FRAMERATE
330 rate = frames/(float)(now-start);
332 fprintf(stdout, "camera %d fps = %f\n", cameraid, rate);
334 }//if(cvcamGetProperty(CAMERA, "raw_image",&image ))
336 // stop here if we're paused
337 pthread_mutex_lock(&(camera->pausemutex));
338 pthread_mutex_unlock(&(camera->pausemutex));
339 pthread_mutex_lock(&(camera->capturestatemutex));
340 }//while (camera->state == CAPTURING && camera->rendered)
341 pthread_mutex_unlock(&(camera->capturestatemutex));
343 pthread_mutex_lock(&(camera->capturestatemutex));
345 if(camera->state != CAPTURING) {
346 // we ended because the camera is not capturing anymore
347 while (camera->capturestate != FINISHED )
349 pthread_cond_wait(&(camera->capturestatecond),&(camera->capturestatemutex));
353 camera->renderstate=0;
354 pthread_cond_signal(&(camera->capturestatecond));
355 pthread_mutex_unlock(&(camera->capturestatemutex));
357 XShmDetach (display, &xshmseg);
358 XDestroyImage (image);
360 shmdt (xshmseg.shmaddr);
361 shmctl (xshmseg.shmid, IPC_RMID, 0);
367 ////////////////////////////////////////////////////////////////////////////////
368 int icvVideoWindowPixelsize(int depth)
390 ////////////////////////////////////////////////////////////////////////////////