Update the changelog
[opencv] / otherlibs / cvcam / src / unix / render.cpp
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 //                        Intel License Agreement
11 //                For Open Source Computer Vision Library
12 //
13 // Copyright (C) 2000, Intel Corporation, 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 Intel Corporation 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 //M*/
41
42 #include "render.h"
43 #include "convert.h"
44
45 #include <pthread.h>
46 #include <stdio.h>
47 #include <assert.h>
48 #include <stdlib.h>
49 #include <string.h>
50
51 #define RENDER_FRAMERATE 1
52
53 #define Assert(exp)                                                 \
54     if( !(exp) )                                                    \
55     {                                                               \
56         printf("Assertion: %s  %s: %d\n", #exp, __FILE__, __LINE__);\
57         assert(exp);                                                \
58     }
59
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,
62                              int depth )
63 {
64
65     int x, y;
66
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);
71     char* _src;
72
73     Assert( src );
74     Assert( dst );
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 );
77
78     if( src_w == dst_w && src_h == dst_h && src_s == dst_s )
79     {
80         memcpy( dst, src, src_s * src_h );
81         return;
82     }
83
84
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);
89
90     Assert(x_array[dst_w - 1] == src_w - 1);
91     Assert(y_array[dst_h - 1] == src_h - 1);
92
93     switch( depth )
94     {
95         case 8:
96             for( y = 0; y < dst_h; y++, dst += dst_s )
97             {
98                 _src = src + y_array[y] * src_s;
99                 for( x = 0; x < dst_w; x++ )
100                 {
101                     int offset = x_array[x];
102                     dst[x] = _src[offset];
103                 }
104             }
105             break;
106          case 16:
107             for( y = 0; y < dst_h; y++, dst += dst_s )
108             {
109                 _src = src + y_array[y] * src_s;
110                 for( x = 0; x < dst_w; x++ )
111                 {
112                     int offset = x_array[x];
113                     ((short*)dst)[x] = ((short*)_src)[offset];
114                 }
115             }
116             break;
117         case 24:
118             for( y = 0; y < dst_h; y++, dst += dst_s )
119             {
120                 _src = src + y_array[y] * src_s;
121                 for( x = 0; x < dst_w; x++ )
122                 {
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];
127                 }
128             }
129             break;
130          case 32:
131             for( y = 0; y < dst_h; y++, dst += dst_s )
132             {
133                 _src = src + y_array[y] * src_s;
134                 for( x = 0; x < dst_w; x++ )
135                 {
136                     int offset = x_array[x];
137                     ((int*)dst)[x] = ((int*)_src)[offset];
138                 }
139             }
140             break;
141     }
142
143     free(x_array);
144     free(y_array);
145 }
146 int icvVideoRenderStart(int cameraid)
147 {
148     pthread_t   thread;
149
150     if( !cameras[cameraid].rendered)
151         return 0;
152
153     if( !cameras[cameraid].window)
154         return 0;
155
156     if(pthread_create(&thread, NULL, icvVideoRender, (void*)cameraid))
157     {
158         fprintf(stderr, "icvVideoRenderStart: failed create thread for rendering");
159         return 0;
160     }
161
162     return 1;
163 }
164
165 ////////////////////////////////////////////////////////////////////////////////
166 void* icvVideoRender(void* data)
167 {
168     int cameraid = (int)data;
169     CvVideoCamera *const camera = &(cameras[cameraid]);
170     Display* display;
171     int screen_num;
172     GC gc;
173     char* display_name = NULL;
174     Window window = camera->window;
175     XWindowAttributes windowattr;
176     Visual* visual;
177     int windowdepth;
178     XImage* image;
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;
183     int pixelsize;
184     XGCValues values;
185     IplImage* iplimage;
186     time_t start, now;
187     int frames = 0;
188     float rate = 0;
189     Status XShm;
190     uchar* imgdata = NULL;
191     uchar* tmpbuff = NULL;
192
193     pthread_mutex_lock(&(camera->capturestatemutex));
194     if(camera->capturestate != CAPTURING)
195     {
196         pthread_mutex_unlock(&(camera->capturestatemutex));
197         pthread_exit( NULL );
198     }
199     camera->renderstate=1;
200     pthread_cond_signal(&(camera->capturestatecond));
201     pthread_mutex_unlock(&(camera->capturestatemutex));
202     XInitThreads();
203
204     if ( (display=XOpenDisplay(display_name)) == NULL )
205
206     {
207         fprintf( stderr, "cvVideo: cannot connect to X server %s\n",
208             XDisplayName(display_name));
209         pthread_exit( NULL );
210     }
211
212     screen_num = DefaultScreen(display);
213
214     if (XGetWindowAttributes(display, window,
215         &windowattr) == 0)
216     {
217         fprintf(stderr, "icvVideoRender: failed to get window attributes.\n" );
218         pthread_exit(NULL);
219     }
220
221     if(windowattr.map_state == IsUnmapped)
222     {
223         fprintf(stderr, "icvVideoRender: window is not mapped \n" );
224         pthread_exit(NULL);
225     }
226
227     windowdepth = windowattr.depth;
228     visual      = windowattr.visual;
229
230     pixelsize = icvVideoWindowPixelsize(windowdepth);
231
232     XShm = XShmQueryExtension(display);
233     if(XShm)
234     {
235         image = XShmCreateImage(display, visual, windowdepth, ZPixmap, NULL,
236             &xshmseg, width, height );
237
238         assert(image);
239
240         xshmseg.shmid = shmget (IPC_PRIVATE,
241             width*height*pixelsize, IPC_CREAT|0777);
242
243         assert(xshmseg.shmid != -1);
244         xshmseg.shmaddr = image->data=(char*)shmat (xshmseg.shmid, 0, 0) ;
245
246         xshmseg.readOnly = False;
247
248         XShmAttach (display, &xshmseg);
249     }
250     else
251     {
252         imgdata = (uchar*)malloc(width*height*icvVideoWindowPixelsize(windowdepth)) ;
253         image = XCreateImage(display, visual, windowdepth, ZPixmap, 0,
254             (char*)imgdata, width,
255             height, 8,
256             icvVideoWindowPixelsize(windowdepth)
257             *width);
258
259         assert(image);
260         XInitImage(image);
261     }
262
263     gc = XCreateGC(display,window,0, &values );
264 #ifdef RENDER_FRAMERATE
265     start = time(NULL);
266 #endif
267
268     pthread_mutex_lock(&(camera->capturestatemutex));
269     while((camera->capturestate == CAPTURING) && (camera->rendered))
270     {
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));
275         camera->updated = 0;
276         pthread_mutex_unlock(&(camera->updatedmutex));
277         if(cvcamGetProperty(cameraid, "raw_image",&iplimage ))
278         {
279             assert(image->data);
280             if(camera->callback != NULL)
281                 camera->callback(iplimage);
282             if((width==camera->videopp.width)&&
283                (height==camera->videopp.height))
284             {
285                 icvvConvert(width, height, width*picturedepth/8, picturedepth,
286                             iplimage->imageData, width*pixelsize, pixelsize*8, image->data
287                     );
288                 cvReleaseImage(&iplimage);
289             }
290             else
291             {
292                 tmpbuff = (uchar*)malloc(camera->videopp.width*camera->videopp.height*
293                                          pixelsize) ;
294                 
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);
300                 
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);
305                 
306                 free(tmpbuff);
307                 
308             }
309             
310             //fprintf(stdout, "cvVideoRendering:image converted!!!!\n");
311             
312             if(XShm)
313             {
314                 XShmPutImage(display, window, gc,
315                              image,0,0,0,0, width,
316                              height, False);
317             }
318             else
319             {
320                 XPutImage(display, window, gc,
321                           image,0,0,0,0, width,
322                           height);
323             }
324             
325             XSync(display, False);
326 #ifdef RENDER_FRAMERATE            
327             now = time(NULL);
328             frames++;
329             if (now-start)
330                 rate = frames/(float)(now-start);
331             if((frames%30) == 0)
332                 fprintf(stdout, "camera %d fps = %f\n", cameraid, rate);
333 #endif
334         }//if(cvcamGetProperty(CAMERA, "raw_image",&image ))
335
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));
342
343     pthread_mutex_lock(&(camera->capturestatemutex));
344 #if 0
345     if(camera->state != CAPTURING) {
346         // we ended because the camera is not capturing anymore
347         while (camera->capturestate != FINISHED )
348         {
349             pthread_cond_wait(&(camera->capturestatecond),&(camera->capturestatemutex));
350         }
351     }
352 #endif
353     camera->renderstate=0;
354     pthread_cond_signal(&(camera->capturestatecond));
355     pthread_mutex_unlock(&(camera->capturestatemutex));
356
357     XShmDetach (display, &xshmseg);
358     XDestroyImage (image);
359     XFreeGC(display,gc);
360     shmdt (xshmseg.shmaddr);
361     shmctl (xshmseg.shmid, IPC_RMID, 0);
362     if(imgdata)
363         free(imgdata);  
364     pthread_exit(NULL);
365 }
366
367 ////////////////////////////////////////////////////////////////////////////////
368 int icvVideoWindowPixelsize(int depth)
369 {
370     switch (depth)
371     {
372     case 32:
373         return 4;
374         
375     case 24:
376         return 4;
377         
378     case 16:
379         return 2;
380         
381     case 8:
382         return 1;
383         
384     default:
385         return 0;
386     }
387     return 0;
388 }
389
390 ////////////////////////////////////////////////////////////////////////////////