Merge pull request #85 from Callum-/dilatometer
[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 /**
12  * Image stream handler. Returns an image to the user.
13  * @param context The context to work in
14  * @param params User specified parameters
15  */
16 void Image_Handler(FCGIContext * context, char * params)
17 {
18
19         int num = 0, width = 1600, height = 1200;       // Set Default values
20         FCGIValue val[] = {
21                 {"num", &num, FCGI_INT_T},
22                 {"width", &width, FCGI_INT_T},
23                 {"height", &height, FCGI_INT_T}
24         };
25         if (!FCGI_ParseRequest(context, params, val, 3))        // Populate val
26                 return;
27         // 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
28         else if (num < 0 || num > 1) {                          
29                 FCGI_RejectJSON(context, "Invalid capture number");
30                 return;
31         // Ensure valid widths
32         } else if (width <= 0 || height <= 0) {
33                 FCGI_RejectJSON(context, "Invalid width/height");
34                 return;
35         }
36         
37         IplImage * src = NULL;   // Source Image
38         CvMat * encoded = NULL;         // Encoded Image
39
40         Camera_GetImage( num, width, height ,&src); 
41
42         Log(LOGDEBUG, "About to encode");
43         encoded = cvEncodeImage(".jpg",src,0);
44         Log(LOGDEBUG, "Encoded");
45
46         Log(LOGNOTE, "Sending image!");
47         FCGI_PrintRaw("Content-type: image/jpg\r\n");
48         FCGI_PrintRaw("Cache-Control: no-cache, no-store, must-revalidate\r\n\r\n");
49         //FCGI_PrintRaw("Content-Length: %d", g_encoded->rows*g_encoded->cols);
50         FCGI_WriteBinary(encoded->data.ptr,1,encoded->rows*encoded->cols);
51         
52         cvReleaseMat(&encoded);
53         cvReleaseImageHeader(&src);
54 }
55         
56 /**
57  * Attempts to get an image from a camera
58  * @param num - Camera id
59  * @param width - Width to force
60  * @param height - Height to force
61  * @param frame - Pointer to IplImage* to set with result
62  * @returns true on success, false on error 
63  */
64  bool Camera_GetImage(int num, int width, int height,  IplImage ** frame)
65  {
66         Log(LOGDEBUG, "Called with arguments num=%d width=%d height=%d frame=%p", num,width,height, frame);
67         static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; // Need to use a mutex to ensure 2 captures are not open at once
68         pthread_mutex_lock(&mutex);
69         bool result = false;
70
71         if( g_capture == NULL)
72         {
73                 g_capture = cvCreateCameraCapture(num);
74                 g_captureID = num;
75         }
76         else if( num != g_captureID)
77         {
78                 cvReleaseCapture(&g_capture);
79                 g_capture = cvCreateCameraCapture(num); 
80                 g_captureID = num;
81         }
82
83         if (g_capture != NULL)
84         {
85
86                 cvSetCaptureProperty(g_capture, CV_CAP_PROP_FRAME_WIDTH, width);
87                 cvSetCaptureProperty(g_capture, CV_CAP_PROP_FRAME_HEIGHT, height);
88
89                 *frame = cvQueryFrame(g_capture);
90                 result = (*frame != NULL);
91
92         //cvShowImage("display", *image);
93         //cvWaitKey(0); 
94         //cvSaveImage("test.jpg",*image,0);
95                 
96                 Log(LOGDEBUG, "At end of mutex");
97         }
98
99         pthread_mutex_unlock(&mutex);   //Close the mutex
100
101         //NOTE: Never have a "return" statement before the mutex is unlocked; it causes deadlocks!
102         return result;
103 }
104
105 /**
106  * Executed on cleanup. Releases the OpenCV Capture structs.
107  */
108 void Image_Cleanup()
109 {
110         // Release the capture and IplImage pointers
111         //cvReleaseImageHeader(&g_frame);
112         cvReleaseCapture(&g_capture);
113 }
114

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