File: save_image.c

package info (click to toggle)
guvcview 2.2.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 3,748 kB
  • sloc: ansic: 26,250; cpp: 3,786; makefile: 28
file content (129 lines) | stat: -rw-r--r-- 4,230 bytes parent folder | download | duplicates (2)
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
/******************************************************************************#
#           guvcview              http://guvcview.sourceforge.net              #
#                                                                              #
#           Paulo Assis <pj.assis@gmail.com>                                   #
#                                                                              #
# This program is free software; you can redistribute it and/or modify         #
# it under the terms of the GNU General Public License as published by         #
# the Free Software Foundation; either version 2 of the License, or            #
# (at your option) any later version.                                          #
#                                                                              #
# This program is distributed in the hope that it will be useful,              #
# but WITHOUT ANY WARRANTY; without even the implied warranty of               #
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the                #
# GNU General Public License for more details.                                 #
#                                                                              #
# You should have received a copy of the GNU General Public License            #
# along with this program; if not, write to the Free Software                  #
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA    #
#                                                                              #
*******************************************************************************/

#include <fcntl.h>
#include <inttypes.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <unistd.h>
// #include <errno.h>
#include <assert.h>

#include "colorspaces.h"
#include "gviewv4l2core.h"
#include "save_image.h"

extern int verbosity;
/*
 * save data to file
 * args:
 *   filename - string with filename
 *   data - pointer to data
 *   size - data size in bytes = sizeof(uint8_t)
 *
 * asserts:
 *   none
 *
 * returns: error code
 */
int v4l2core_save_data_to_file(const char *filename, uint8_t *data, int size) {
  FILE *fp;
  int ret = 0;

  if ((fp = fopen(filename, "wb")) != NULL) {
    /* allocate internal buffer
     * and make fwrite full buffered */
    setvbuf(fp, NULL, _IOFBF, size);
    ret = fwrite(data, size, 1, fp);

    if (ret < 1)
      ret = 1; /*write error*/
    else
      ret = 0;

    // fflush(fp); /*flush data stream to file system*/
    // if(fsync(fileno(fp)) || fclose(fp))
    //	fprintf(stderr, "V4L2_CORE: (save_data_to_file) error - couldn't write
    // buffer to file: %s\n", strerror(errno)); else if(verbosity > 0)
    //	printf("V4L2_CORE: saved data to %s\n", filename);
    fclose(fp);
    if (verbosity > 0)
      printf("V4L2_CORE: saved data to %s\n", filename);
  } else
    ret = 1;

  return (ret);
}

/*
 * save the current frame to file
 * args:
 *    frame - pointer to frame buffer
 *    filename - output file name
 *    format - image type
 *           (IMG_FMT_RAW, IMG_FMT_JPG, IMG_FMT_PNG, IMG_FMT_BMP)
 *
 * asserts:
 *    none
 *
 * returns: error code
 */
int save_frame_image(v4l2_frame_buff_t *frame, const char *filename,
                     int format) {
  int ret = E_OK;

  switch (format) {
  case IMG_FMT_RAW:
    if (verbosity > 0)
      printf("V4L2_CORE: saving raw data to %s\n", filename);
    ret = v4l2core_save_data_to_file(filename, frame->raw_frame,
                                     frame->raw_frame_size);
    break;

  case IMG_FMT_JPG:
    if (verbosity > 0)
      printf("V4L2_CORE: saving jpeg frame to %s\n", filename);
    ret = save_image_jpeg(frame, filename);
    break;

  case IMG_FMT_BMP:
    if (verbosity > 0)
      printf("V4L2_CORE: saving bmp frame to %s\n", filename);
    ret = save_image_bmp(frame, filename);
    break;

  case IMG_FMT_PNG:
    if (verbosity > 0)
      printf("V4L2_CORE: saving png frame to %s\n", filename);
    ret = save_image_png(frame, filename);
    break;

  default:
    fprintf(stderr, "V4L2_CORE: (save_image) Image format %i not supported\n",
            format);
    ret = E_FORMAT_ERR;
    break;
  }

  return ret;
}