ec7bbb68e4256850b459d7e357950cd518795bea
[libam7xxx.git] / src / picoproj.c
1 /* picoproj - test program for libam7xxx
2  *
3  * Copyright (C) 2012  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 2 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\t\t\t\t2 - YUV - NV12\n");
39         printf("\t-l <log level>\t\tthe verbosity level of libam7xxx output (0-5)\n");
40         printf("\t-W <image width>\tthe width of the image to upload\n");
41         printf("\t-H <image height>\tthe height of the image to upload\n");
42         printf("\t-h \t\t\tthis help message\n");
43 }
44
45 int main(int argc, char *argv[])
46 {
47         int ret;
48         int exit_code = EXIT_SUCCESS;
49         int opt;
50
51         char filename[FILENAME_MAX] = {0};
52         int image_fd;
53         struct stat st;
54         am7xxx_context *ctx;
55         am7xxx_device *dev;
56         int log_level = AM7XXX_LOG_INFO;
57         int format = AM7XXX_IMAGE_FORMAT_JPEG;
58         int width = 800;
59         int height = 480;
60         unsigned char *image;
61         unsigned int size;
62         unsigned int native_width;
63         unsigned int native_height;
64         unsigned int unknown0;
65         unsigned int unknown1;
66
67         while ((opt = getopt(argc, argv, "f:F:l:W:H:h")) != -1) {
68                 switch (opt) {
69                 case 'f':
70                         strncpy(filename, optarg, FILENAME_MAX);
71                         break;
72                 case 'F':
73                         format = atoi(optarg);
74                         switch(format) {
75                         case AM7XXX_IMAGE_FORMAT_JPEG:
76                                 fprintf(stdout, "JPEG format\n");
77                                 break;
78                         case AM7XXX_IMAGE_FORMAT_NV12:
79                                 fprintf(stdout, "NV12 format\n");
80                                 break;
81                         default:
82                                 fprintf(stderr, "Unsupported format\n");
83                                 exit(EXIT_FAILURE);
84                         }
85                         break;
86                 case 'l':
87                         log_level = atoi(optarg);
88                         if (log_level < AM7XXX_LOG_FATAL || log_level > AM7XXX_LOG_TRACE) {
89                                 fprintf(stderr, "Unsupported log level, falling back to AM7XXX_LOG_ERROR\n");
90                                 log_level = AM7XXX_LOG_ERROR;
91                         }
92                         break;
93                 case 'W':
94                         width = atoi(optarg);
95                         if (width < 0) {
96                                 fprintf(stderr, "Unsupported width\n");
97                                 exit(EXIT_FAILURE);
98                         }
99                         break;
100                 case 'H':
101                         height = atoi(optarg);
102                         if (height < 0) {
103                                 fprintf(stderr, "Unsupported height\n");
104                                 exit(EXIT_FAILURE);
105                         }
106                         break;
107                 case 'h':
108                         usage(argv[0]);
109                         exit(EXIT_SUCCESS);
110                         break;
111                 default: /* '?' */
112                         usage(argv[0]);
113                         exit(EXIT_FAILURE);
114                 }
115         }
116
117         if (filename[0] == '\0') {
118                 fprintf(stderr, "An image file MUST be specified.\n");
119                 exit_code = EXIT_FAILURE;
120                 goto out;
121         }
122
123         image_fd = open(filename, O_RDONLY);
124         if (image_fd < 0) {
125                 perror("open");
126                 exit_code = EXIT_FAILURE;
127                 goto out;
128         }
129         if (fstat(image_fd, &st) < 0) {
130                 perror("fstat");
131                 exit_code = EXIT_FAILURE;
132                 goto out_close_image_fd;
133         }
134         size = st.st_size;
135
136         image = mmap(NULL, st.st_size, PROT_READ, MAP_SHARED, image_fd, 0);
137         if (image == NULL) {
138                 perror("mmap");
139                 exit_code = EXIT_FAILURE;
140                 goto out_close_image_fd;
141         }
142
143         ret = am7xxx_init(&ctx);
144         if (ret < 0) {
145                 perror("am7xxx_init");
146                 exit_code = EXIT_FAILURE;
147                 goto out_munmap;
148         }
149
150         am7xxx_set_log_level(ctx, log_level);
151
152         ret = am7xxx_open_device(ctx, &dev, 0);
153         if (ret < 0) {
154                 perror("am7xxx_open_device");
155                 exit_code = EXIT_FAILURE;
156                 goto cleanup;
157         }
158
159
160         ret = am7xxx_close_device(dev);
161         if (ret < 0) {
162                 perror("am7xxx_close_device");
163                 exit_code = EXIT_FAILURE;
164                 goto cleanup;
165         }
166
167         ret = am7xxx_open_device(ctx, &dev, 0);
168         if (ret < 0) {
169                 perror("am7xxx_open_device");
170                 exit_code = EXIT_FAILURE;
171                 goto cleanup;
172         }
173
174         ret = am7xxx_get_device_info(dev, &native_width, &native_height, &unknown0, &unknown1);
175         if (ret < 0) {
176                 perror("am7xxx_get_info");
177                 exit_code = EXIT_FAILURE;
178                 goto cleanup;
179         }
180         printf("Native resolution: %dx%d\n", native_width, native_height);
181         printf("Unknown0: %d\n", unknown0);
182         printf("Unknown1: %d\n", unknown1);
183
184         ret = am7xxx_set_power_mode(dev, AM7XXX_POWER_LOW);
185         if (ret < 0) {
186                 perror("am7xxx_set_power_mode");
187                 exit_code = EXIT_FAILURE;
188                 goto cleanup;
189         }
190
191         ret = am7xxx_send_image(dev, format, width, height, image, size);
192         if (ret < 0) {
193                 perror("am7xxx_send_image");
194                 exit_code = EXIT_FAILURE;
195                 goto cleanup;
196         }
197
198         exit_code = EXIT_SUCCESS;
199
200 cleanup:
201         am7xxx_shutdown(ctx);
202
203 out_munmap:
204         ret = munmap(image, size);
205         if (ret < 0)
206                 perror("munmap");
207
208 out_close_image_fd:
209         ret = close(image_fd);
210         if (ret < 0)
211                 perror("close");
212
213 out:
214         exit(exit_code);
215 }