010d4d1db293fb2f22b1db1401673df771e5b6ed
[libam7xxx.git] / picoproj.c
1 /* picoproj - test program for libam7xxx
2  *
3  * Copyright (C) 2011  Antonio Ospite <ospite@studenti.unina.it>
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
17  */
18
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <string.h>
22 #include <sys/mman.h>
23 #include <sys/types.h>
24 #include <sys/stat.h>
25 #include <fcntl.h>
26 #include <unistd.h>
27
28 #include "am7xxx.h"
29
30 static void usage(char *name)
31 {
32         printf("usage: %s [OPTIONS]\n\n", name);
33         printf("OPTIONS:\n");
34         printf("\t-f <filename>\t\tthe image file to upload\n");
35         printf("\t-F <format>\t\tthe image format to use (default is JPEG).\n");
36         printf("\t\t\t\tSUPPORTED FORMATS:\n");
37         printf("\t\t\t\t\t1 - JPEG\n");
38         printf("\t-W <image width>\tthe width of the image to upload\n");
39         printf("\t-H <image height>\tthe height of the image to upload\n");
40         printf("\t-h \t\t\tthis help message\n");
41 }
42
43 int main(int argc, char *argv[])
44 {
45         int ret;
46         int exit_code = EXIT_SUCCESS;
47         int opt;
48
49         char filename[FILENAME_MAX] = {0};
50         int image_fd = -1;
51         am7xxx_device dev;
52         int format = AM7XXX_IMAGE_FORMAT_JPEG;
53         int width = 800;
54         int height = 480;
55         uint8_t *image = NULL;
56         unsigned int size = 59475;
57
58         while ((opt = getopt(argc, argv, "f:F:W:H:h")) != -1) {
59                 switch (opt) {
60                 case 'f':
61                         strncpy(filename, optarg, FILENAME_MAX);
62                         break;
63                 case 'F':
64                         format = atoi(optarg);
65                         if (format != 1) {
66                                 fprintf(stderr, "Unsupported format\n");
67                                 exit(EXIT_FAILURE);
68                         }
69                         break;
70                 case 'W':
71                         width = atoi(optarg);
72                         if (width < 0) {
73                                 fprintf(stderr, "Unsupported width\n");
74                                 exit(EXIT_FAILURE);
75                         }
76                         break;
77                 case 'H':
78                         height = atoi(optarg);
79                         if (height < 0) {
80                                 fprintf(stderr, "Unsupported height\n");
81                                 exit(EXIT_FAILURE);
82                         }
83                         break;
84                 default: /* '?' */
85                         usage(argv[0]);
86                         exit(EXIT_FAILURE);
87                 }
88         }
89
90         if (filename[0] != '\0') {
91                 struct stat st;
92                 
93                 image_fd = open(filename, O_RDONLY);
94                 if (image_fd < 0) {
95                         perror("open");
96                         exit_code = EXIT_FAILURE;
97                         goto out;
98                 }
99                 if (fstat(image_fd, &st) < 0) {
100                         perror("fstat");
101                         exit_code = EXIT_FAILURE;
102                         goto out_close_image_fd;
103                 }
104                 size = st.st_size;
105
106                 image = mmap(NULL, st.st_size, PROT_READ, MAP_SHARED, image_fd, 0);
107                 if (image == NULL) {
108                         perror("mmap");
109                         exit_code = EXIT_FAILURE;
110                         goto out_close_image_fd;
111                 }
112         }
113
114         dev = am7xxx_init();
115         if (dev == NULL) {
116                 perror("am7xxx_init");
117                 exit_code = EXIT_FAILURE;
118                 goto out_munmap;
119         }
120
121         ret = am7xxx_send_image(dev, format, width, height, image, size);
122         if (ret < 0) {
123                 perror("am7xxx_send_image");
124                 exit_code = EXIT_FAILURE;
125                 goto cleanup;
126         }
127
128         exit_code = EXIT_SUCCESS;
129
130 cleanup:
131         am7xxx_shutdown(dev);
132
133 out_munmap:
134         if (image != NULL) {
135                 ret = munmap(image, size);
136                 if (ret < 0)
137                         perror("munmap");
138         }
139
140 out_close_image_fd:
141         if (image_fd >= 0) {
142                 ret = close(image_fd);
143                 if (ret < 0)
144                         perror("close");
145         }
146
147 out:
148         exit(exit_code);
149 }