Paparazzi UAS  v5.14.0_stable-0-g3f680d1
Paparazzi is a free software Unmanned Aircraft System.
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
video_capture.c
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2015
3  *
4  * This file is part of Paparazzi.
5  *
6  * Paparazzi is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2, or (at your option)
9  * any later version.
10  *
11  * Paparazzi is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with paparazzi; see the file COPYING. If not, see
18  * <http://www.gnu.org/licenses/>.
19  *
20  */
21 
26 #include <stdio.h>
27 #include <unistd.h>
28 #include <stdlib.h>
29 
32 
33 #include "lib/encoding/jpeg.h"
34 
35 // Note: this define is set automatically when the video_exif module is included,
36 // and exposes functions to write data in the image exif headers.
37 #if JPEG_WITH_EXIF_HEADER
38 #include "lib/exif/exif_module.h"
39 #endif
40 
41 #ifndef VIDEO_CAPTURE_PATH
42 #define VIDEO_CAPTURE_PATH /data/video/images
43 #endif
44 
45 #ifndef VIDEO_CAPTURE_JPEG_QUALITY
46 #define VIDEO_CAPTURE_JPEG_QUALITY 99
47 #endif
48 
49 #ifndef VIDEO_CAPTURE_FPS
50 #define VIDEO_CAPTURE_FPS 0
51 #endif
52 PRINT_CONFIG_VAR(VIDEO_CAPTURE_FPS)
53 
54 // Module settings
57 
58 // Forward function declarations
59 struct image_t *video_capture_func(struct image_t *img);
60 void video_capture_save(struct image_t *img);
61 
62 
64 {
65  // Create the images directory
66  char save_name[128];
67  sprintf(save_name, "mkdir -p %s", STRINGIFY(VIDEO_CAPTURE_PATH));
68  if (system(save_name) != 0) {
69  printf("[video_capture] Could not create images directory %s.\n", STRINGIFY(VIDEO_CAPTURE_PATH));
70  return;
71  }
72 
73  // Add function to computer vision pipeline
74  cv_add_to_device(&VIDEO_CAPTURE_CAMERA, video_capture_func, VIDEO_CAPTURE_FPS);
75 }
76 
77 
78 struct image_t *video_capture_func(struct image_t *img)
79 {
80  // If take_shot bool is set, save the image
82  video_capture_save(img);
84  }
85 
86  // No modification to image
87  return NULL;
88 }
89 
90 
92 {
93  // Set take_shot bool to true
95 }
96 
97 
98 void video_capture_save(struct image_t *img)
99 {
100  // Declare storage for image location
101  char save_name[128];
102 
103  // Simple shot counter to find first available image location
104  for (/* no init */; video_capture_index < 9999; ++video_capture_index) {
105  // Generate image location
106  sprintf(save_name, "%s/img_%05d.jpg", STRINGIFY(VIDEO_CAPTURE_PATH), video_capture_index);
107 
108  // Continue with next number if file exists already
109  if (access(save_name, F_OK) != -1) {
110  continue;
111  }
112 
113  printf("[video_capture] Saving image to %s.\n", save_name);
114 
115  // Create jpg image from raw frame
116  struct image_t img_jpeg;
117  image_create(&img_jpeg, img->w, img->h, IMAGE_JPEG);
118  jpeg_encode_image(img, &img_jpeg, VIDEO_CAPTURE_JPEG_QUALITY, true);
119 
120 #if JPEG_WITH_EXIF_HEADER
121  write_exif_jpeg(save_name, img_jpeg.buf, img_jpeg.buf_size, img_jpeg.w, img_jpeg.h);
122 #else
123  // Open file
124  FILE *fp = fopen(save_name, "w");
125  if (fp == NULL) {
126  printf("[video_capture] Could not write shot %s.\n", save_name);
127  break;
128  }
129 
130  // Save it to the file and close it
131  fwrite(img_jpeg.buf, sizeof(uint8_t), img_jpeg.buf_size, fp);
132  fclose(fp);
133 #endif
134 
135  // Free image
136  image_free(&img_jpeg);
137 
138  // End loop here
139  break;
140  }
141 }
uint32_t buf_size
The buffer size.
Definition: image.h:52
void image_free(struct image_t *img)
Free the image.
Definition: image.c:64
void image_create(struct image_t *img, uint16_t width, uint16_t height, enum image_type type)
Create a new image.
Definition: image.c:39
Definition: image.h:43
void video_capture_save(struct image_t *img)
Definition: video_capture.c:98
void video_capture_shoot(void)
Definition: video_capture.c:91
struct video_listener * cv_add_to_device(struct video_config_t *device, cv_function func, uint16_t fps)
Definition: cv.c:46
int write_exif_jpeg(char *filename, const unsigned char *image_jpg, const unsigned int image_jpg_len, const unsigned int image_jpg_x, const unsigned int image_jpg_y)
Definition: exif_module.c:146
#define VIDEO_CAPTURE_PATH
Definition: video_capture.c:42
uint16_t w
Image width.
Definition: image.h:45
Computer vision framework for onboard processing.
uint16_t h
Image height.
Definition: image.h:46
void * buf
Image buffer (depending on the image_type)
Definition: image.h:53
#define VIDEO_CAPTURE_JPEG_QUALITY
Definition: video_capture.c:46
#define VIDEO_CAPTURE_FPS
Default FPS (zero means run at camera fps)
Definition: video_capture.c:50
Encode images with the use of the JPEG encoding.
unsigned char uint8_t
Definition: types.h:14
struct image_t * video_capture_func(struct image_t *img)
Definition: video_capture.c:78
void jpeg_encode_image(struct image_t *in, struct image_t *out, uint32_t quality_factor, bool add_dri_header)
Encode an YUV422 image.
Definition: jpeg.c:408
bool video_capture_take_shot
Definition: video_capture.c:55
Write JPEG images containing EXIF headers with GPS coordinates.
#define false
Definition: rtwtypes.h:24
int video_capture_index
Definition: video_capture.c:56
void video_capture_init(void)
Definition: video_capture.c:63
An JPEG encoded image (not per pixel encoded)
Definition: image.h:38