/alps/ufodecode

To get this branch, use:
bzr branch http://darksoft.org/webbzr/alps/ufodecode

« back to all changes in this revision

Viewing changes to test/ipedec.c

  • Committer: Matthias Vogelgesang
  • Date: 2012-04-13 13:38:01 UTC
  • Revision ID: matthias.vogelgesang@kit.edu-20120413133801-4vn1hjvpokmetxca
Use getopt to eat program options

Show diffs side-by-side

added added

removed removed

Lines of Context:
3
3
#include <stdint.h>
4
4
#include <string.h>
5
5
#include <unistd.h>
 
6
#include <errno.h>
6
7
#include <sys/time.h>
7
 
#include <errno.h>
 
8
#include <getopt.h>
8
9
#include <ufodecode.h>
9
10
 
10
11
 
11
 
static const int CLEAR_FRAME = 1;
12
 
 
13
 
int read_raw_file(const char *filename, char **buffer, size_t *length)
 
12
static int read_raw_file(const char *filename, char **buffer, size_t *length)
14
13
{
15
14
    FILE *fp = fopen(filename, "rb"); 
16
15
    if (fp == NULL)
35
34
    return 0;
36
35
}
37
36
 
38
 
 
39
 
int main(int argc, char const* argv[])
 
37
static void process_file(const char *filename, int rows, int clear_frame)
40
38
{
41
 
    if (argc < 2) {
42
 
        fprintf(stderr, "Usage: ipedec <filename> <number of lines per frame>\n");
43
 
        return EXIT_FAILURE;
44
 
    }
45
 
 
46
39
    char *buffer = NULL;
47
40
    size_t num_bytes = 0;
48
 
    int error = read_raw_file(argv[1], &buffer, &num_bytes);
49
 
    if (error) {
50
 
        printf("file reading error: %s\n", strerror(error));
51
 
        return EXIT_FAILURE;
52
 
    }
53
 
 
54
 
    const int rows = argc > 2 ? atoi(argv[2]) : -1;
55
 
 
56
 
    ufo_decoder decoder = ufo_decoder_new(rows, 2048, (uint32_t *) buffer, num_bytes);
57
41
    int err = 0;
58
42
    uint16_t *pixels = (uint16_t *) malloc(2048 * 1088 * sizeof(uint16_t));
59
43
    uint32_t num_rows, frame_number, time_stamp;
60
44
    int num_frames = 0;
61
45
    struct timeval start, end;
62
46
    long seconds = 0L, useconds = 0L;
 
47
    int error = read_raw_file(filename, &buffer, &num_bytes);
 
48
 
 
49
    if (error) {
 
50
        fprintf(stderr, "Error processing %s: %s\n", filename, strerror(error));
 
51
        return;
 
52
    }
 
53
 
 
54
    ufo_decoder decoder = ufo_decoder_new(rows, 2048, (uint32_t *) buffer, num_bytes);
63
55
 
64
56
    if (!decoder) {
65
 
        fprintf(stderr, "Failed to initialize decoder\n");
66
 
        return EXIT_FAILURE;
 
57
        fprintf(stderr, "Failed to initialize decoder\n");
 
58
        return;
67
59
    }
68
60
 
69
 
    FILE *fp = fopen("test.raw", "wb");
 
61
    char output_name[256];
 
62
    snprintf(output_name, 256, "%s.raw", filename);
 
63
    FILE *fp = fopen(output_name, "wb");
 
64
 
70
65
    if (!fp) {
71
 
        fprintf(stderr, "Failed to open file for writting\n");
72
 
        return EXIT_FAILURE;
 
66
        fprintf(stderr, "Failed to open file for writing\n");
 
67
        return;
73
68
    }
74
69
 
75
70
    while (!err) {
 
71
        if (clear_frame)
 
72
            memset(pixels, 0, 2048 * 1088 * sizeof(uint16_t));
 
73
 
76
74
        gettimeofday(&start, NULL);
77
 
        if (CLEAR_FRAME)
78
 
            memset(pixels, 0, 2048 * 1088 * sizeof(uint16_t));
79
75
        err = ufo_decoder_get_next_frame(decoder, &pixels, &num_rows, &frame_number, &time_stamp, NULL);
80
76
        gettimeofday(&end, NULL);
81
77
 
86
82
            fwrite(pixels, sizeof(uint16_t), 2048 * 1088, fp);
87
83
        }
88
84
    }
 
85
 
89
86
    fclose(fp);
90
87
 
91
88
    float mtime = seconds * 1000.0 + useconds / 1000.0;
94
91
    free(pixels);
95
92
    ufo_decoder_free(decoder);
96
93
    free(buffer);
 
94
}
 
95
 
 
96
int main(int argc, char const* argv[])
 
97
{
 
98
    int getopt_ret, index;
 
99
 
 
100
    static struct option long_options[] = {
 
101
        { "num-rows", required_argument, 0, 'r' },
 
102
        { "clear-frame", no_argument, 0, 'c' },
 
103
        { "help", no_argument, 0, '?' },
 
104
        { 0, 0, 0, 0 }
 
105
    };
 
106
 
 
107
    int clear_frame = 0;
 
108
    int rows = -1;
 
109
 
 
110
    while ((getopt_ret = getopt_long(argc, (char *const *) argv, "r:c:?", long_options, &index)) != -1) {
 
111
        switch (getopt_ret) {
 
112
            case 'r': 
 
113
                rows = atoi(optarg);
 
114
                break;
 
115
            case 'c':
 
116
                clear_frame = 1;
 
117
                break;
 
118
            default:
 
119
                break;
 
120
        } 
 
121
    }
 
122
 
 
123
    while (optind < argc)
 
124
        process_file(argv[optind++], rows, clear_frame);
97
125
 
98
126
    return 0;
99
127
}