Paparazzi UAS  v5.10_stable-5-g83a0da5-dirty
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 #ifndef VIDEO_CAPTURE_PATH
36 #define VIDEO_CAPTURE_PATH /data/video/images
37 #endif
38 
39 #ifndef VIDEO_CAPTURE_JPEG_QUALITY
40 #define VIDEO_CAPTURE_JPEG_QUALITY 99
41 #endif
42 
43 // Module settings
46 
47 // Forward function declarations
48 struct image_t *video_capture_func(struct image_t *img);
49 void video_capture_save(struct image_t *img);
50 
51 
53 {
54  // Create the images directory
55  char save_name[128];
56  sprintf(save_name, "mkdir -p %s", STRINGIFY(VIDEO_CAPTURE_PATH));
57  if (system(save_name) != 0) {
58  printf("[video_capture] Could not create images directory %s.\n", STRINGIFY(VIDEO_CAPTURE_PATH));
59  return;
60  }
61 
62  // Add function to computer vision pipeline
63  cv_add_to_device(&VIDEO_CAPTURE_CAMERA, video_capture_func);
64 }
65 
66 
67 struct image_t *video_capture_func(struct image_t *img)
68 {
69  // If take_shot bool is set, save the image
71  video_capture_save(img);
73  }
74 
75  // No modification to image
76  return NULL;
77 }
78 
79 
81 {
82  // Set take_shot bool to true
84 }
85 
86 
87 void video_capture_save(struct image_t *img)
88 {
89  // Declare storage for image location
90  char save_name[128];
91 
92  // Simple shot counter to find first available image location
93  for (/* no init */; video_capture_index < 9999; ++video_capture_index) {
94  // Generate image location
95  sprintf(save_name, "%s/img_%05d.jpg", STRINGIFY(VIDEO_CAPTURE_PATH), video_capture_index);
96 
97  // Continue with next number if file exists already
98  if (access(save_name, F_OK) != -1) {
99  continue;
100  }
101 
102  printf("[video_capture] Saving image to %s.\n", save_name);
103 
104  // Open file
105  FILE *fp = fopen(save_name, "w");
106  if (fp == NULL) {
107  printf("[video_capture] Could not write shot %s.\n", save_name);
108  break;
109  }
110 
111  // Create jpg image from raw frame
112  struct image_t img_jpeg;
113  image_create(&img_jpeg, img->w, img->h, IMAGE_JPEG);
114  jpeg_encode_image(img, &img_jpeg, VIDEO_CAPTURE_JPEG_QUALITY, true);
115 
116  // Save it to the file and close it
117  fwrite(img_jpeg.buf, sizeof(uint8_t), img_jpeg.buf_size, fp);
118  fclose(fp);
119 
120  // Free image
121  image_free(&img_jpeg);
122 
123  // End loop here
124  break;
125  }
126 }
uint32_t buf_size
The buffer size.
Definition: image.h:50
void image_free(struct image_t *img)
Free the image.
Definition: image.c:63
struct video_listener * cv_add_to_device(struct video_config_t *device, cv_function func)
Definition: cv.c:45
void image_create(struct image_t *img, uint16_t width, uint16_t height, enum image_type type)
Create a new image.
Definition: image.c:38
Definition: image.h:42
void video_capture_save(struct image_t *img)
Definition: video_capture.c:87
void video_capture_shoot(void)
Definition: video_capture.c:80
#define VIDEO_CAPTURE_PATH
Definition: video_capture.c:36
uint16_t w
Image width.
Definition: image.h:44
Computer vision framework for onboard processing.
uint16_t h
Image height.
Definition: image.h:45
void * buf
Image buffer (depending on the image_type)
Definition: image.h:51
#define VIDEO_CAPTURE_JPEG_QUALITY
Definition: video_capture.c:40
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:67
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:44
int video_capture_index
Definition: video_capture.c:45
void video_capture_init(void)
Definition: video_capture.c:52
An JPEG encoded image (not per pixel encoded)
Definition: image.h:37