root / trunk / code / projects / colonet / server / vision / shmservetest.cpp @ 1428
History | View | Annotate | Download (2.94 KB)
1 | 939 | rcahoon | #include <cv.h> |
---|---|---|---|
2 | #include <highgui.h> |
||
3 | |||
4 | #include <stdio.h> |
||
5 | |||
6 | #define CAPTURE
|
||
7 | |||
8 | #ifdef CAPTURE
|
||
9 | static CvCapture *capture;
|
||
10 | #endif
|
||
11 | |||
12 | #include <sys/types.h> |
||
13 | #include <sys/shm.h> |
||
14 | #include <string.h> |
||
15 | #include <stdlib.h> |
||
16 | |||
17 | #include "php_sysvsem.h" |
||
18 | |||
19 | #include <errno.h> |
||
20 | |||
21 | int write_JPEG_to_mem (unsigned char *storage, int length, IplImage *_img, int quality); |
||
22 | |||
23 | static sysvsem *sem;
|
||
24 | static int shm_id; |
||
25 | |||
26 | int sendImage(IplImage *image)
|
||
27 | { |
||
28 | printf("Send image\n");
|
||
29 | |||
30 | if (sem->sem_acquire())
|
||
31 | { |
||
32 | printf("Semaphore acquired\n");
|
||
33 | |||
34 | unsigned char *p = (unsigned char *)shmat(shm_id, NULL, 0); |
||
35 | if (p!=(void *)-1) |
||
36 | { |
||
37 | printf("Memory mapped at %p\n", p);
|
||
38 | |||
39 | int len = write_JPEG_to_mem (p+4, 150000-4, image, 30); |
||
40 | |||
41 | int i;
|
||
42 | int length = len;
|
||
43 | printf("%d\n", length);
|
||
44 | for(i=0; i < 4; i++) |
||
45 | { |
||
46 | char digit = (char)(length & 0xff); |
||
47 | length >>= 8;
|
||
48 | memcpy(p+i, &digit, sizeof(char)); |
||
49 | } |
||
50 | |||
51 | printf("Memory copied\n");
|
||
52 | |||
53 | sem->sem_release(); |
||
54 | printf("Semaphore released\n");
|
||
55 | |||
56 | shmdt(p); |
||
57 | printf("Memory detached\n");
|
||
58 | } |
||
59 | } |
||
60 | |||
61 | if (errno!=0) |
||
62 | { |
||
63 | int errnum = errno;
|
||
64 | errno = 0;
|
||
65 | |||
66 | fprintf(stderr, "error %d %s\n", errnum, strerror(errnum));
|
||
67 | return errnum;
|
||
68 | } |
||
69 | |||
70 | return 0; |
||
71 | } |
||
72 | |||
73 | void openShM()
|
||
74 | { |
||
75 | shm_id = shmget(0x40305163, 150000, IPC_CREAT | 0666); |
||
76 | if (shm_id > 0) |
||
77 | printf("Shared memory opened\n");
|
||
78 | else
|
||
79 | { |
||
80 | fprintf(stderr, "Error opening shared memory\n");
|
||
81 | exit(-2);
|
||
82 | } |
||
83 | |||
84 | sem = new (std::nothrow)sysvsem(0x40309999); |
||
85 | if (sem)
|
||
86 | printf("Semaphore opened\n");
|
||
87 | else
|
||
88 | { |
||
89 | fprintf(stderr, "Error opening semaphore\n");
|
||
90 | exit(-1);
|
||
91 | } |
||
92 | } |
||
93 | |||
94 | void releaseShM()
|
||
95 | { |
||
96 | sem->sem_remove(); |
||
97 | delete sem;
|
||
98 | printf("Semaphore deleted\n");
|
||
99 | |||
100 | shmctl(shm_id, IPC_RMID, NULL);
|
||
101 | printf("Memory marked for deletion\n");
|
||
102 | } |
||
103 | |||
104 | int main(int argc, char *argv[]) |
||
105 | { |
||
106 | #ifdef CAPTURE
|
||
107 | capture = cvCaptureFromCAM( 0 );
|
||
108 | if( !capture ) {
|
||
109 | fprintf( stderr, "ERROR: capture is NULL \n" );
|
||
110 | return -1; |
||
111 | } |
||
112 | #endif
|
||
113 | |||
114 | openShM(); |
||
115 | |||
116 | // Create a window in which the captured images will be presented
|
||
117 | //cvNamedWindow( "mywindow1", CV_WINDOW_AUTOSIZE );
|
||
118 | |||
119 | // Show the image captured from the camera in the window and repeat
|
||
120 | while((cvWaitKey(10) & 255) != 27) |
||
121 | { |
||
122 | usleep(15000);
|
||
123 | |||
124 | #ifdef CAPTURE
|
||
125 | if(!cvGrabFrame(capture)){ // capture a frame |
||
126 | printf("Could not grab a frame\n\7");
|
||
127 | return -1; |
||
128 | } |
||
129 | IplImage *image03=cvRetrieveFrame(capture); |
||
130 | #else
|
||
131 | IplImage *image03; |
||
132 | // load image and force it to be grayscale
|
||
133 | if ((image03 = cvLoadImage(argv[1], 0)) == 0) { |
||
134 | fprintf(stderr, "Failed to load image.\n");
|
||
135 | return -1; |
||
136 | } |
||
137 | #endif
|
||
138 | |||
139 | //cvShowImage( "mywindow1", image03 );
|
||
140 | |||
141 | sendImage(image03); |
||
142 | |||
143 | #ifndef CAPTURE
|
||
144 | cvReleaseImage(&image03); |
||
145 | #endif
|
||
146 | |||
147 | //If ESC key pressed, Key=0x10001B under OpenCV 0.9.7(linux version),
|
||
148 | //remove higher bits using AND operator
|
||
149 | } |
||
150 | |||
151 | #ifdef CAPTURE
|
||
152 | // Release the capture device housekeeping
|
||
153 | cvReleaseCapture( &capture ); |
||
154 | #endif
|
||
155 | |||
156 | releaseShM(); |
||
157 | |||
158 | cvDestroyWindow( "mywindow1" );
|
||
159 | return 0; |
||
160 | } |