-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCCamera.cpp
More file actions
336 lines (289 loc) · 8.21 KB
/
CCamera.cpp
File metadata and controls
336 lines (289 loc) · 8.21 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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
/*
* File name: CCamera.cpp
* Date: 2010
* Author: Tom Krajnik
*/
#include "CCamera.h"
#include <stdio.h>
#include <stdlib.h>
extern "C" {
#include "utils.h"
#include "color.h"
}
//-----------------------------------------------------------------------------
CCamera::CCamera()
{
cameraType = CT_UNKNOWN;
autoexposure = true;
gain = exposition = 0;
width = height = 0;
exposition = 0;
fileNum = 0;
videoIn = (struct vdIn*) calloc(1,sizeof(struct vdIn));
globalTimer.reset();
globalTimer.start();
return;
}
CCamera::~CCamera()
{
close_v4l2(videoIn);
free(videoIn);
videoIn = NULL;
freeLut();
}
int CCamera::initFileLoader(const char *deviceName,int *wi,int *he)
{
char fileName[1000];
strcpy(directory,deviceName);
sprintf(fileName,"%s/0000.bmp",directory);
fprintf(stdout,"Camera type: dummy camera\n");
FILE* file = fopen(fileName,"r");
if (file == NULL){
fprintf(stderr,"File %s not found.\n",fileName);
return -1;
}else{
fclose(file);
CRawImage *image = new CRawImage(*wi,*he);
image->loadBmp(fileName);
*wi = image->width;
*he = image->height;
delete image;
}
return 0;
}
int CCamera::init(const char *deviceName,int *wi,int *he,bool saveI)
{
save = saveI;
format = V4L2_PIX_FMT_YUYV;
//format = V4L2_PIX_FMT_MJPEG;
if (save) videoIn->toggleAvi = 1; else videoIn->toggleAvi = 0;
const float fps = 5.0;
const int grabemethod = 1;
time_t timeNow;
time(&timeNow);
char timeStr[100];
strftime(timeStr, sizeof(timeStr), "%Y-%m-%d_%H:%M:%S",localtime(&timeNow));
sprintf(avifilename,"output/%s.avi",timeStr);
if (strncmp(deviceName,"/dev/",5)==0) cameraType = CT_WEBCAM; else cameraType = CT_FILELOADER;
if (cameraType == CT_WEBCAM){
int ret = init_videoIn(videoIn,(char *)deviceName, wi,he,fps,format,grabemethod,avifilename);
if (ret < 0) {
fprintf(stderr,"Cannot init camera device %s.\n",deviceName);
return -1;
} else {
fprintf(stderr,"Camera %s initialized.\n",deviceName);
}
initLut();
// gets camera parameters
gain = getDeviceGain();
exposition = getDeviceExposition();
brightness = getDeviceBrightness();
int min,max,def;
min=max=def=0;
width = *wi;
height = *he;
getParamInfo(V4L2_CID_GAIN,min,max,def);
fprintf(stderr,"Gain parameter: min=%d max=%d default=%d\n",min,max,def);
getParamInfo(V4L2_CID_BRIGHTNESS,min,max,def);
fprintf(stderr,"Brightness parameter: min=%d max=%d default=%d\n",min,max,def);
getParamInfo(V4L2_CID_EXPOSURE_ABSOLUTE,min,max,def);
fprintf(stderr,"Exposure parameter: min=%d max=%d default=%d\n",min,max,def);
getParamInfo(V4L2_CID_EXPOSURE_AUTO,min,max,def);
fprintf(stderr,"Exposure auto parameter: min=%d max=%d default=%d\n",min,max,def);
getParamInfo(V4L2_CID_WHITE_BALANCE_TEMPERATURE,min,max,def);
fprintf(stderr,"White temperature balance: min=%d max=%d default=%d\n",min,max,def);
getParamInfo(V4L2_CID_AUTO_WHITE_BALANCE,min,max,def);
fprintf(stderr,"White auto balance: min=%d max=%d default=%d\n",min,max,def);
setDeviceAutoExposure(0);
setDeviceWhiteBalanceAuto(1);
fprintf(stderr,"Camera gain = %d\n",gain);
fprintf(stderr,"Camera exposition = %d\n",exposition);
fprintf(stderr,"Camera brightness = %d\n",brightness);
}
if (cameraType == CT_FILELOADER){
if (initFileLoader(deviceName,wi,he)==0){
printf("Images will be loaded from directory %s.\n",deviceName);
}else{
printf("Camera initialization failed.\n");
}
}
return -1;
}
int CCamera::renewImage(CRawImage* image,bool move)
{
if (cameraType == CT_WEBCAM){
if (move){
int ret = uvcGrab(videoIn);
if (ret < 0) {
fprintf(stderr,"Cannot grab a frame from a camera!\n");
return ret;
}
}
Pyuv422torgb24(videoIn->framebuffer,image->data,videoIn->width,videoIn->height);
if (save && format == V4L2_PIX_FMT_YUYV){
char a[100];
char b[100];
time_t timeNow;
time(&timeNow);
strftime(a, sizeof(a), "%Y-%m-%d_%H:%M:%S",localtime(&timeNow));
sprintf(b,"output/%s-%08i.bmp",a,globalTimer.getTime());
image->saveBmp(b);
}
return 0;
}
if (cameraType == CT_FILELOADER){
char fileName[1000];
if (move) fileNum++;
sprintf(fileName,"%s/%04i.bmp",directory,fileNum);
if (image->loadBmp(fileName)==false){
fileNum=0;
sprintf(fileName,"%s/%04i.bmp",directory,fileNum);
image->loadBmp(fileName);
return -1;
}
return 0;
}
return -1;
}
int CCamera::getGain()
{
gain = getDeviceGain();
return gain;
}
int CCamera::getContrast()
{
contrast = getDeviceGain();
return contrast;
}
int CCamera::getExposition()
{
exposition = getDeviceExposition();
return exposition;
}
int CCamera::getBrightness()
{
brightness = getDeviceBrightness();
return brightness;
}
void CCamera::setGain(int value)
{
gain = value;
setDeviceGain(value);
}
void CCamera::setContrast(int value)
{
contrast = value;
setDeviceContrast(contrast);
}
void CCamera::changeBrightness(int brt)
{
setBrightness(brightness+brt);
}
void CCamera::changeExposition(int exp)
{
setExposition(exposition+exp);
}
void CCamera::changeGain(int exp)
{
setGain(gain+exp);
}
void CCamera::changeContrast(int expc)
{
setContrast(contrast+expc);
}
void CCamera::setExposition(int exp)
{
exposition = exp;
setDeviceExposition(exp);
fprintf(stdout,"Exposure is set to %i\n",v4l2GetControl(videoIn,V4L2_CID_EXPOSURE_ABSOLUTE));
}
void CCamera::setBrightness(int val)
{
brightness = val;
setDeviceBrightness(val);
}
int CCamera::getDeviceGain() {
return v4l2GetControl(videoIn,V4L2_CID_GAIN);
}
int CCamera::getDeviceContrast() {
return v4l2GetControl(videoIn,V4L2_CID_CONTRAST);
}
int CCamera::getDeviceBrightness() {
fprintf(stdout,"Brightness is set to %i\n",v4l2GetControl(videoIn,V4L2_CID_BRIGHTNESS));
return v4l2GetControl(videoIn,V4L2_CID_BRIGHTNESS);
}
int CCamera::getDeviceExposition() {
int rawValue = v4l2GetControl(videoIn,V4L2_CID_EXPOSURE_ABSOLUTE);
int value = (int)log2(rawValue);
fprintf(stdout,"Exposition is set to %i-%i\n",value,rawValue);
return value;
}
int CCamera::setDeviceExposition(int value) {
if (autoexposure){
autoexposure = false;
setDeviceAutoExposure(1);
}
value = exp2(value);
return v4l2SetControl(videoIn,V4L2_CID_EXPOSURE_ABSOLUTE,value);
}
int CCamera::getDeviceSharpness() {
return v4l2GetControl(videoIn,V4L2_CID_BASE+27);
}
int CCamera::setDeviceSharpness(const int value) {
return v4l2SetControl(videoIn,V4L2_CID_BASE+27,value);
}
int CCamera::setDeviceGain(const int value) {
fprintf(stdout,"Gain is set to %i\n",v4l2GetControl(videoIn,V4L2_CID_GAIN));
return v4l2SetControl(videoIn,V4L2_CID_GAIN,value);
}
int CCamera::setDeviceContrast(const int value) {
fprintf(stdout,"Contrast is set to %i\n",v4l2GetControl(videoIn,V4L2_CID_CONTRAST));
return v4l2SetControl(videoIn,V4L2_CID_CONTRAST,value);
}
int CCamera::setDeviceBrightness(const int value) {
fprintf(stdout,"Brightness is set to %i\n",v4l2GetControl(videoIn,V4L2_CID_BRIGHTNESS));
return v4l2SetControl(videoIn,V4L2_CID_BRIGHTNESS,value);
}
int CCamera::setDeviceWhiteTemperature(const int value) {
return v4l2SetControl(videoIn,V4L2_CID_WHITE_BALANCE_TEMPERATURE,value);
}
int CCamera::getDeviceWhiteTemperature() {
fprintf(stderr,"Camera white temperature = %d\n",v4l2GetControl(videoIn,V4L2_CID_WHITE_BALANCE_TEMPERATURE));
return v4l2GetControl(videoIn,V4L2_CID_WHITE_BALANCE_TEMPERATURE);
}
int CCamera::getParamInfo(const int paramType, int &min, int &max, int &default_val)
{
struct v4l2_queryctrl queryctrl;
if (isv4l2Control(videoIn,paramType,&queryctrl) < 0) {
fprintf(stderr,"Error getting camera info.\n");
return -1;
}
min = queryctrl.minimum;
max = queryctrl.maximum;
default_val = queryctrl.default_value;
return 0;
}
int CCamera::setDeviceAutoExposure(const int val) {
struct v4l2_control control;
control.id = V4L2_CID_EXPOSURE_AUTO;
control.value =(int)val;
int ret;
if ((ret = ioctl(videoIn->fd, VIDIOC_S_CTRL, &control)) < 0) {
printf("Set Auto Exposure on error\n");
} else {
printf("Auto Exposure set to %d\n", control.value);
}
return 0;
}
int CCamera::setDeviceWhiteBalanceAuto(const int val) {
struct v4l2_control control;
control.id = V4L2_CID_AUTO_WHITE_BALANCE;
control.value =(int)val;
int ret;
if ((ret = ioctl(videoIn->fd, VIDIOC_S_CTRL, &control)) < 0) {
printf("Set Auto white balance on error\n");
} else {
printf("Auto white balance set to %d\n", control.value);
}
return 0;
}