Automatic commit of irc logs
[matches/MCTX3420.git] / server / image.c
1 #include "cv.h"
2 #include "highgui_c.h"
3 #include "image.h"
4 #include <string.h>
5 #include <stdio.h>
6 #include <pthread.h>
7
8 static CvCapture * g_capture = NULL;
9 static int g_captureID = -1;
10
11 void Image_Handler(FCGIContext * context, char * params)
12 {
13         int num = 0, width = 1600, height = 1200;       // Set Default values
14         FCGIValue val[] = {
15                 {"num", &num, FCGI_INT_T},
16                 {"width", &width, FCGI_INT_T},
17                 {"height", &height, FCGI_INT_T}
18         };
19         if (!FCGI_ParseRequest(context, params, val, 3))        // Populate val
20                 return;
21         // Ensure the camera id is 0 or 1. Even though we plan to only have 1 camera attached at a time, this will allow 2. increase
22         else if (num < 0 || num > 1) {                          
23                 FCGI_RejectJSON(context, "Invalid capture number");
24                 return;
25         // Ensure valid widths
26         } else if (width <= 0 || height <= 0) {
27                 FCGI_RejectJSON(context, "Invalid width/height");
28                 return;
29         }
30         
31         IplImage * src = NULL;   // Source Image
32         CvMat * encoded = NULL;         // Encoded Image
33
34         Camera_GetImage( num, width, height ,&src); 
35
36         Log(LOGDEBUG, "About to encode");
37         encoded = cvEncodeImage(".jpg",src,0);
38         Log(LOGDEBUG, "Encoded");
39
40         Log(LOGNOTE, "Sending image!");
41         FCGI_PrintRaw("Content-type: image/jpg\r\n");
42         FCGI_PrintRaw("Cache-Control: no-cache, no-store, must-revalidate\r\n\r\n");
43         //FCGI_PrintRaw("Content-Length: %d", g_encoded->rows*g_encoded->cols);
44         FCGI_WriteBinary(encoded->data.ptr,1,encoded->rows*encoded->cols);
45         
46         cvReleaseMat(&encoded);
47         cvReleaseImageHeader(&src);
48 }
49         
50 /**
51  * Attempts to get an image from a camera
52  * @param num - Camera id
53  * @param width - Width to force
54  * @param height - Height to force
55  * @param image - Pointer to CvMat* to set with result
56  * @returns true on success, false on error 
57  */
58  bool Camera_GetImage(int num, int width, int height,  IplImage ** frame)
59  {
60         Log(LOGDEBUG, "Called with arguments num=%d width=%d height=%d frame=%p", num,width,height, frame);
61         static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; // Need to use a mutex to ensure 2 captures are not open at once
62         pthread_mutex_lock(&mutex);
63         bool result = false;
64
65         if( g_capture == NULL)
66         {
67                 g_capture = cvCreateCameraCapture(num);
68                 g_captureID = num;
69         }
70         else if( num != g_captureID)
71         {
72                 cvReleaseCapture(&g_capture);
73                 g_capture = cvCreateCameraCapture(num); 
74                 g_captureID = num;
75         }
76
77         cvSetCaptureProperty(g_capture, CV_CAP_PROP_FRAME_WIDTH, width);
78         cvSetCaptureProperty(g_capture, CV_CAP_PROP_FRAME_HEIGHT, height);
79
80         *frame = cvQueryFrame(g_capture);
81         result = (*frame != NULL);
82
83         //cvShowImage("display", *image);
84         //cvWaitKey(0); 
85         //cvSaveImage("test.jpg",*image,0);
86                 
87         Log(LOGDEBUG, "At end of mutex");
88         
89         pthread_mutex_unlock(&mutex);   //Close the mutex
90
91         //NOTE: Never have a "return" statement before the mutex is unlocked; it causes deadlocks!
92         return result;
93 }
94
95 void Image_Cleanup()
96 {
97         // Release the capture and IplImage pointers
98         //cvReleaseImageHeader(&g_frame);
99         cvReleaseCapture(&g_capture);
100 }
101

UCC git Repository :: git.ucc.asn.au