-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.c
More file actions
125 lines (106 loc) · 3.9 KB
/
Copy pathmain.c
File metadata and controls
125 lines (106 loc) · 3.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
#include "image_view.c"
#include "capture_mmap.c"
#include "jpeg_blockdiff.c"
#include <stdlib.h>
// Global buffers to store previous and current frames
static unsigned char* prev_frame_buffer = NULL;
static unsigned long prev_frame_size = 0;
static unsigned char* temp_rgba_buffer = NULL;
static int first_frame = 1;
// Function to display camera feed in the image viewer window with difference highlighting
void display_camera_frame(Buffer* buffer, uint8_t t) {
// Request a new frame from camera
n_iv_cap_request(NULL); // NULL since we'll use the global buffer
// Allocate the temp RGBA buffer if not already done
if (!temp_rgba_buffer) {
temp_rgba_buffer = malloc(buffer->length);
if (!temp_rgba_buffer) {
fprintf(stderr, "Failed to allocate RGBA buffer\n");
return;
}
}
// Clear output buffer to transparent black
memset(buffer->start, 0, buffer->length);
if (buf.bytesused > 0 && bufs[buf.index].start) {
// First frame - just store it
if (first_frame) {
if (prev_frame_buffer) {
free(prev_frame_buffer);
}
prev_frame_buffer = malloc(buf.bytesused);
if (prev_frame_buffer) {
memcpy(prev_frame_buffer, bufs[buf.index].start, buf.bytesused);
prev_frame_size = buf.bytesused;
first_frame = 0;
}
// For the first frame, we can't do a comparison, so just show something
memcpy(buffer->start, bufs[buf.index].start,
buffer->length > buf.bytesused ? buf.bytesused : buffer->length);
} else {
// Compare current frame with previous frame
int w = 640; // Assuming 640x480 - get these from the image viewer size
int h = 480;
if (jpeg_compare_diff(
prev_frame_buffer, prev_frame_size,
bufs[buf.index].start, buf.bytesused,
buffer->start, w, h) != 0) {
fprintf(stderr, "JPEG comparison failed\n");
}
// Store current frame as previous for next iteration
if (prev_frame_buffer) {
if (prev_frame_size != buf.bytesused) {
free(prev_frame_buffer);
prev_frame_buffer = malloc(buf.bytesused);
}
if (prev_frame_buffer) {
memcpy(prev_frame_buffer, bufs[buf.index].start, buf.bytesused);
prev_frame_size = buf.bytesused;
}
}
}
}
}
int main(int argc, char **argv)
{
// Check if camera device number is provided
if (argc < 2) {
fprintf(stderr, "Usage: %s <camera_device_number>\n", argv[0]);
return 1;
}
int camera_num = atoi(argv[1]);
// Initialize the camera
if (n_iv_cap_open(camera_num) != 0) {
fprintf(stderr, "Failed to open camera %d\n", camera_num);
return 1;
}
if (n_iv_cap_init_buffers() != 0) {
fprintf(stderr, "Failed to initialize camera buffers\n");
n_iv_cap_close();
return 1;
}
// Initialize the image viewer
if (n_iv_init() < 0) {
fprintf(stderr, "Failed to initialize image viewer\n");
n_iv_cap_close();
return 1;
}
printf("Displaying camera motion detection. Press any key to exit.\n");
// Main loop - display camera frames
for (uint8_t t = 0;; ++t)
{
// Update the display with camera feed highlighting differences
if (n_iv_updateframe(display_camera_frame, t)) break;
}
// Cleanup
if (prev_frame_buffer) {
free(prev_frame_buffer);
prev_frame_buffer = NULL;
}
if (temp_rgba_buffer) {
free(temp_rgba_buffer);
temp_rgba_buffer = NULL;
}
n_iv_cap_close();
n_iv_quit();
return 0;
}